Goods.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. namespace app\admin\controller\company;
  3. use app\admin\controller\AuthController;
  4. use app\models\store\StoreProduct;
  5. use app\models\system\SystemStore;
  6. use app\models\system\SystemStoreStock;
  7. use app\models\system\SystemStoreStockBill;
  8. use app\admin\model\store\{StoreCategory as CategoryModel, StoreProduct as ProductModel, StoreProductAttrValue};
  9. use crmeb\services\{JsonService, JsonService as Json, UtilService as Util, FormBuilder as Form};
  10. use crmeb\traits\CurdControllerTrait;
  11. use think\facade\Route as Url;
  12. /**
  13. * 产品管理
  14. * Class StoreProduct
  15. * @package app\admin\controller\store
  16. */
  17. class Goods extends AuthController
  18. {
  19. use CurdControllerTrait;
  20. protected $bindModel = ProductModel::class;
  21. /**
  22. * 显示资源列表
  23. *
  24. * @return \think\Response
  25. */
  26. public function index()
  27. {
  28. $type = $this->request->param('type', 1);
  29. //获取分类
  30. $this->assign('cate', CategoryModel::getTierList(null, 1));
  31. //出售中产品
  32. $onsale = ProductModel::where('is_del', 0)->where('is_consumer', 0)->where('is_show', 1)->count();
  33. //待上架产品
  34. $forsale = ProductModel::where('is_del', 0)->where('is_consumer', 0)->where('is_show', 0)->count();
  35. //仓库中产品
  36. $warehouse = ProductModel::where('is_del', 0)->where('is_consumer', 0)->count();
  37. //已经售馨产品
  38. $outofstock = ProductModel::getModelObject(['type' => 4])->where('is_consumer', 0)->count();
  39. //警戒库存
  40. $policeforce = ProductModel::getModelObject(['type' => 5])->where('is_consumer', 0)->count();
  41. //回收站
  42. $recycle = ProductModel::where('is_del', 1)->where('is_consumer', 0)->count();
  43. $this->assign(compact('type', 'onsale', 'forsale', 'warehouse', 'outofstock', 'policeforce', 'recycle'));
  44. $this->assign('type', $this->adminInfo['type']);
  45. if ($this->adminInfo['type'] == 0) {
  46. $this->assign('store', SystemStore::where('is_show', 1)->where('is_del', 0)->field('id,name')->select());
  47. }
  48. return $this->fetch();
  49. }
  50. public function consumer()
  51. {
  52. $type = $this->request->param('type', 1);
  53. //获取分类
  54. $this->assign('cate', CategoryModel::getTierList(null, 1, 1));
  55. //出售中产品
  56. $onsale = ProductModel::where('is_del', 0)->where('is_show', 1)->where('is_consumer', 1)->count();
  57. //待上架产品
  58. $forsale = ProductModel::where('is_del', 0)->where('is_show', 0)->where('is_consumer', 1)->count();
  59. //仓库中产品
  60. $warehouse = ProductModel::where('is_del', 0)->where('is_consumer', 1)->count();
  61. //已经售馨产品
  62. $outofstock = ProductModel::getModelObject(['type' => 4])->where('is_consumer', 1)->count();
  63. //警戒库存
  64. $policeforce = ProductModel::getModelObject(['type' => 5])->where('is_consumer', 1)->count();
  65. //回收站
  66. $recycle = ProductModel::where('is_del', 1)->where('is_consumer', 1)->count();
  67. $this->assign(compact('type', 'onsale', 'forsale', 'warehouse', 'outofstock', 'policeforce', 'recycle'));
  68. return $this->fetch();
  69. }
  70. /**
  71. * 异步查找产品
  72. *
  73. * @return json
  74. */
  75. public function product_ist()
  76. {
  77. $where = Util::getMore([
  78. ['page', 1],
  79. ['limit', 20],
  80. ['key', ''],
  81. ['excel', 0],
  82. ['store_id', $this->adminInfo['store_id']],
  83. ['is_warn', 0],
  84. ]);
  85. return Json::successlayui(SystemStoreStock::lst($where));
  86. }
  87. /**
  88. * 显示创建资源表单页.
  89. *
  90. * @return \think\Response
  91. */
  92. public function create($id = 0, $consumer = 0)
  93. {
  94. $this->assign('id', (int)$id);
  95. $this->assign('consumer', (int)$consumer);
  96. return $this->fetch();
  97. }
  98. public function save()
  99. {
  100. $where = Util::getMore([
  101. ['bar_code', 20],
  102. ['in_stock', 0],
  103. ]);
  104. if ($where['in_stock'] < 1) return app('json')->fail('补货数量不能少于1');
  105. $info = SystemStoreStock::alias('a')->join("StoreProduct b", "a.product_id=b.id", "left")->where('a.bar_code', $where['bar_code'])->field('a.price,a.unique,a.in_stock,a.is_consumer,a.product_id,b.store_name,b.image,a.bar_code')->find();
  106. if (!$info) {
  107. $store_product = StoreProductAttrValue::where('type', 0)->where('bar_code', $where['bar_code'])->find();
  108. if (!$store_product)
  109. return app('json')->fail('商品条形码不存在');
  110. SystemStoreStock::create([
  111. 'store_id' => $this->adminInfo['store_id'] ?: 1,
  112. 'product_id' => $store_product['product_id'],
  113. 'in_stock' => 0,
  114. 'store_sales' => 0,
  115. 'repair_sales' => 0,
  116. 'in_last_time' => 0,
  117. 'unique' => $store_product['unique'],
  118. 'bar_code' => $store_product['bar_code'],
  119. 'price' => $store_product['price'],
  120. 'image' => $store_product['image'],
  121. 'is_consumer' => StoreProduct::where('id', $store_product['product_id'])->value('is_consumer'),
  122. ]
  123. );
  124. $info = SystemStoreStock::alias('a')->join("StoreProduct b", "a.product_id=b.id", "left")->where('a.bar_code', $where['bar_code'])->field('a.price,a.unique,a.in_stock,a.is_consumer,a.product_id,b.store_name,b.image,a.bar_code')->find();
  125. }
  126. if (!$info)
  127. return app('json')->fail('商品条形码不存在');
  128. $info = $info->toarray();
  129. $info['in_stock'] = $where['in_stock'];
  130. $info['admin_id'] = $this->adminId;
  131. if (SystemStoreStockBill::order_create($this->adminInfo['store_id'], $info)) {
  132. return Json::successful('入库成功');
  133. } else {
  134. return Json::fail('入库失败');
  135. }
  136. }
  137. /**
  138. * 门店库存始初化
  139. * @param int $store_id
  140. * @param int $num
  141. */
  142. public function init($store_id = 0, $num = 10)
  143. {
  144. if ($store_id < 1) return app('json')->fail('参数错误!');
  145. if ($this->adminInfo['type'] == 1) return app('json')->fail('无权限!');
  146. SystemStoreStock::store_init($store_id, $num);
  147. }
  148. public function bill()
  149. {
  150. if ($this->adminInfo['type'] == 0) {
  151. $this->assign('store', SystemStore::where('is_show', 1)->where('is_del', 0)->field('id,name')->select());
  152. }
  153. $this->assign('type', $this->adminInfo['type']);
  154. return $this->fetch();
  155. }
  156. public function bill_lst()
  157. {
  158. $where = Util::getMore([
  159. ['store_id', $this->adminInfo['store_id']],
  160. ['key', ''],
  161. ['page', 0],
  162. ['limit', 0],
  163. ['status', -2],
  164. ]);
  165. return Json::successlayui(SystemStoreStockBill::lst($where));
  166. }
  167. public function bill_view($product_id, $store_id)
  168. {
  169. $this->assign('product_id', $product_id);
  170. $this->assign('store_id', $store_id);
  171. $this->assign('count', SystemStoreStockBill::where(compact('product_id', 'store_id'))->value('count(id)') ?: 0);
  172. return $this->fetch();
  173. }
  174. public function bill_view_lst()
  175. {
  176. $where = Util::getMore([
  177. ['store_id', 0],
  178. ['product_id', 0],
  179. ['key', ''],
  180. ['page', 0],
  181. ['limit', 0],
  182. ['status', -2],
  183. ]);
  184. return app('json')->successful(SystemStoreStockBill::lst($where));
  185. }
  186. public function audit($id)
  187. {
  188. if (!$id) return $this->failed('数据不存在');
  189. $Bill = SystemStoreStockBill::where('id', $id)->find();
  190. if (!$Bill) return Json::fail('数据不存在!');
  191. $f = array();
  192. $f[] = Form::radio('status', '状态', $Bill->getData('status'))->options([['value' => 1, 'label' => '通过'], ['value' => -1, 'label' => '拒绝']]);
  193. $f[] = Form::textarea('refuse_msg', '备注');
  194. $form = Form::make_post_form('入库审核', $f, Url::buildUrl('audit_save', array('id' => $id)), 2);
  195. $this->assign(compact('form'));
  196. return $this->fetch('public/form-builder');
  197. }
  198. public function batch_audit($id)
  199. {
  200. if (!$id) return $this->failed('数据不存在');
  201. $f = array();
  202. $f[] = Form::radio('status', '状态')->options([['value' => 1, 'label' => '通过'], ['value' => -1, 'label' => '拒绝']]);
  203. $f[] = Form::textarea('refuse_msg', '备注');
  204. $form = Form::make_post_form('入库审核', $f, Url::buildUrl('batch_audit_save', array('id' => $id)), 2);
  205. $this->assign(compact('form'));
  206. return $this->fetch('public/form-builder');
  207. }
  208. public function audit_save($id)
  209. {
  210. $where = Util::postMore([
  211. ['refuse_msg', ''],
  212. ['status', 1],
  213. ]);
  214. if (!SystemStoreStockBill::be(['id' => $id, 'status' => 0])) return $this->failed('数据不存在或已经审核');
  215. $where['audit_time'] = time();
  216. $where['audit_admin'] = $this->adminId;
  217. SystemStoreStockBill::edit($where, $id);
  218. if ($where['status'] == 1) {
  219. $info = SystemStoreStockBill::find($id)->toArray();
  220. $rs = SystemStoreStock::replenish($info['store_id'], $info);
  221. }
  222. return Json::successful('审核成功');
  223. }
  224. /**
  225. * 批量审核
  226. */
  227. public function batch_audit_save()
  228. {
  229. $where = Util::postMore([
  230. ['refuse_msg', ''],
  231. ['status', 1],
  232. ]);
  233. $ids = explode(',', input('id'));
  234. if ($where['status'] == 1) {
  235. foreach ($ids as $v) {
  236. $info = SystemStoreStockBill::where('id', $v)->where('status', 0)->find();
  237. if ($info) {
  238. $rs = SystemStoreStock::replenish($info['store_id'], $info);
  239. }
  240. }
  241. }
  242. $where['audit_time'] = time();
  243. $where['audit_admin'] = $this->adminId;
  244. SystemStoreStockBill::where('id', 'in', join(",", $ids))->update($where);
  245. return Json::successful('批量审核成功');
  246. }
  247. }