Goods.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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\{FormBuilder, 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 edit_stock($bar_code)
  99. {
  100. $f = [];
  101. $f[] = Form::hidden('bar_code', $bar_code);
  102. $f[] = Form::radio('type', '增减', 1)->setOptions([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  103. $f[] = Form::number('in_stock', '数量', 0)->step(1)->min(1);
  104. $f[] = Form::textarea('info', '增减缘由', 0)->rows(10);
  105. $form = Form::make_post_form('添加用户通知', $f, Url::buildUrl('save'));
  106. $this->assign(compact('form'));
  107. return $this->fetch('public/form-builder');
  108. }
  109. public function save()
  110. {
  111. $where = Util::getMore([
  112. ['bar_code', 20],
  113. ['in_stock', 0],
  114. ['type', 1],
  115. ['info', ''],
  116. ]);
  117. if (!$this->adminInfo['store_id']) return app('json')->fail('门店铺货用通道');
  118. if ($where['in_stock'] < 1) return app('json')->fail('补货数量不能少于1');
  119. $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();
  120. if (!$info) {
  121. $store_product = StoreProductAttrValue::where('type', 0)->where('bar_code', $where['bar_code'])->find();
  122. if (!$store_product)
  123. return app('json')->fail('商品条形码不存在');
  124. SystemStoreStock::create([
  125. 'store_id' => $this->adminInfo['store_id'],
  126. 'product_id' => $store_product['product_id'],
  127. 'in_stock' => 0,
  128. 'store_sales' => 0,
  129. 'repair_sales' => 0,
  130. 'in_last_time' => 0,
  131. 'unique' => $store_product['unique'],
  132. 'bar_code' => $store_product['bar_code'],
  133. 'price' => $store_product['price'],
  134. 'image' => $store_product['image'],
  135. 'is_consumer' => StoreProduct::where('id', $store_product['product_id'])->value('is_consumer'),
  136. ]
  137. );
  138. $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();
  139. }
  140. if (!$info)
  141. return app('json')->fail('商品条形码不存在');
  142. $info = $info->toarray();
  143. $info['in_stock'] = $where['in_stock'];
  144. $info['admin_id'] = $this->adminId;
  145. $info['type'] = $where['type'] == 1 ? 1 : 2;
  146. $info['info'] = $where['info'];
  147. if (SystemStoreStockBill::order_create($this->adminInfo['store_id'], $info)) {
  148. return Json::successful('入库成功');
  149. } else {
  150. return Json::fail('入库失败');
  151. }
  152. }
  153. /**
  154. * 门店库存始初化
  155. * @param int $store_id
  156. * @param int $num
  157. */
  158. public function init($store_id = 0, $num = 10)
  159. {
  160. if ($store_id < 1) return app('json')->fail('参数错误!');
  161. if ($this->adminInfo['type'] == 1) return app('json')->fail('无权限!');
  162. SystemStoreStock::store_init($store_id, $num);
  163. }
  164. public function bill()
  165. {
  166. if ($this->adminInfo['type'] == 0) {
  167. $this->assign('store', SystemStore::where('is_show', 1)->where('is_del', 0)->field('id,name')->select());
  168. }
  169. $this->assign('type', $this->adminInfo['type']);
  170. return $this->fetch();
  171. }
  172. public function bill_lst()
  173. {
  174. $where = Util::getMore([
  175. ['store_id', $this->adminInfo['store_id']],
  176. ['key', ''],
  177. ['page', 0],
  178. ['limit', 0],
  179. ['status', -2],
  180. ]);
  181. return Json::successlayui(SystemStoreStockBill::lst($where));
  182. }
  183. public function bill_view($product_id, $store_id)
  184. {
  185. $this->assign('product_id', $product_id);
  186. $this->assign('store_id', $store_id);
  187. $this->assign('count', SystemStoreStockBill::where(compact('product_id', 'store_id'))->value('count(id)') ?: 0);
  188. return $this->fetch();
  189. }
  190. public function bill_view_lst()
  191. {
  192. $where = Util::getMore([
  193. ['store_id', 0],
  194. ['product_id', 0],
  195. ['key', ''],
  196. ['page', 0],
  197. ['limit', 0],
  198. ['status', -2],
  199. ]);
  200. return app('json')->successful(SystemStoreStockBill::lst($where));
  201. }
  202. public function audit($id)
  203. {
  204. if (!$id) return $this->failed('数据不存在');
  205. $Bill = SystemStoreStockBill::where('id', $id)->find();
  206. if (!$Bill) return Json::fail('数据不存在!');
  207. $f = array();
  208. $f[] = Form::radio('status', '状态', $Bill->getData('status'))->options([['value' => 1, 'label' => '通过'], ['value' => -1, 'label' => '拒绝']]);
  209. $f[] = Form::textarea('refuse_msg', '备注');
  210. $form = Form::make_post_form('入库审核', $f, Url::buildUrl('audit_save', array('id' => $id)), 2);
  211. $this->assign(compact('form'));
  212. return $this->fetch('public/form-builder');
  213. }
  214. public function batch_audit($id)
  215. {
  216. if (!$id) return $this->failed('数据不存在');
  217. $f = array();
  218. $f[] = Form::radio('status', '状态')->options([['value' => 1, 'label' => '通过'], ['value' => -1, 'label' => '拒绝']]);
  219. $f[] = Form::textarea('refuse_msg', '备注');
  220. $form = Form::make_post_form('入库审核', $f, Url::buildUrl('batch_audit_save', array('id' => $id)), 2);
  221. $this->assign(compact('form'));
  222. return $this->fetch('public/form-builder');
  223. }
  224. public function audit_save($id)
  225. {
  226. $where = Util::postMore([
  227. ['refuse_msg', ''],
  228. ['status', 1],
  229. ]);
  230. if (!SystemStoreStockBill::be(['id' => $id, 'status' => 0])) return $this->failed('数据不存在或已经审核');
  231. $where['audit_time'] = time();
  232. $where['audit_admin'] = $this->adminId;
  233. SystemStoreStockBill::edit($where, $id);
  234. if ($where['status'] == 1) {
  235. $info = SystemStoreStockBill::find($id)->toArray();
  236. $rs = SystemStoreStock::replenish($info['store_id'], $info);
  237. }
  238. return Json::successful('审核成功');
  239. }
  240. /**
  241. * 批量审核
  242. */
  243. public function batch_audit_save()
  244. {
  245. $where = Util::postMore([
  246. ['refuse_msg', ''],
  247. ['status', 1],
  248. ]);
  249. $ids = explode(',', input('id'));
  250. if ($where['status'] == 1) {
  251. foreach ($ids as $v) {
  252. $info = SystemStoreStockBill::where('id', $v)->where('status', 0)->find();
  253. if ($info) {
  254. $rs = SystemStoreStock::replenish($info['store_id'], $info);
  255. }
  256. }
  257. }
  258. $where['audit_time'] = time();
  259. $where['audit_admin'] = $this->adminId;
  260. SystemStoreStockBill::where('id', 'in', join(",", $ids))->update($where);
  261. return Json::successful('批量审核成功');
  262. }
  263. }