Goods.php 12 KB

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