Goods.php 12 KB

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