Goods.php 10 KB

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