StockWarning.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace app\admin\controller\shopro\goods;
  3. use app\common\controller\Backend;
  4. use app\admin\controller\shopro\Base;
  5. use think\Db;
  6. use Exception;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. use addons\shopro\library\traits\StockWarning as StockWarningTrait;
  10. /**
  11. * 库存预警
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class StockWarning extends Base
  16. {
  17. use StockWarningTrait;
  18. /**
  19. * GoodsSkuPrice模型对象
  20. * @var \app\admin\model\shopro\goods\SkuPrice
  21. */
  22. protected $model = null;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\admin\model\shopro\goods\StockWarning;
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 查看
  35. */
  36. public function index()
  37. {
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if ($this->request->isAjax()) {
  41. //如果发送的来源是Selectpage,则转发到Selectpage
  42. if ($this->request->request('keyField')) {
  43. return $this->selectpage();
  44. }
  45. $nobuildfields = ['goods_title', 'stock_type'];
  46. list($where, $sort, $order, $offset, $limit) = $this->custombuildparams(null, $nobuildfields);
  47. $skuPriceTableName = (new \app\admin\model\shopro\goods\SkuPrice())->getQuery()->getTable();
  48. $total = $this->buildSearch()
  49. ->alias('g')
  50. ->join($skuPriceTableName . ' sp', 'g.goods_sku_price_id = sp.id', 'left')
  51. ->field('g.*,sp.stock')
  52. ->where($where)
  53. ->count();
  54. $list = $this->buildSearch()
  55. ->alias('g')
  56. ->with(['goods' => function ($query) {
  57. $query->field('id,type,title,subtitle,status,category_ids,image,images,price,original_price,is_sku,dispatch_type,service_ids,dispatch_ids');
  58. }])
  59. ->join($skuPriceTableName . ' sp', 'g.goods_sku_price_id = sp.id', 'left')
  60. ->field('g.*,sp.stock')
  61. ->where($where)
  62. ->order($sort, $order)
  63. ->limit($offset, $limit)
  64. ->select();
  65. $warning_total = $this->buildSearch()->alias('g')
  66. ->join($skuPriceTableName . ' sp', 'g.goods_sku_price_id = sp.id', 'left')
  67. ->field('g.*,sp.stock')
  68. ->where($where)
  69. ->where('stock', '>', 0)
  70. ->count();
  71. $over_total = $this->buildSearch()->alias('g')
  72. ->join($skuPriceTableName . ' sp', 'g.goods_sku_price_id = sp.id', 'left')
  73. ->field('g.*,sp.stock')
  74. ->where($where)
  75. ->where('stock', '<=', 0)
  76. ->count();
  77. $result = [
  78. "total" => $total,
  79. 'warning_total' => $warning_total,
  80. 'over_total' => $over_total,
  81. "rows" => $list
  82. ];
  83. return $this->success('操作成功', null, $result);
  84. }
  85. return $this->view->fetch();
  86. }
  87. /**
  88. * 补货
  89. *
  90. * @param [type] $ids
  91. * @param [type] $stock
  92. * @return void
  93. */
  94. public function addStock ($ids) {
  95. if (!$ids) {
  96. $ids = $this->request->get('id');
  97. }
  98. $stock = $this->request->post('stock', 0);
  99. if ($stock <= 0) {
  100. $this->error('请输入补货数量');
  101. }
  102. $row = $this->model->with(['skuPrice'])->where('id', $ids)->find();
  103. if (!$row) {
  104. $this->error(__('No Results were found'));
  105. }
  106. Db::startTrans();
  107. try {
  108. $skuPrice = $row['skuPrice'];
  109. if ($skuPrice) {
  110. $skuPrice->setInc('stock', $stock);
  111. }
  112. // 检测库存预警
  113. $this->checkStockWarning($skuPrice);
  114. Db::commit();
  115. } catch (PDOException $e) {
  116. Db::rollback();
  117. $this->error($e->getMessage());
  118. } catch (Exception $e) {
  119. Db::rollback();
  120. $this->error($e->getMessage());
  121. }
  122. $this->success('补货成功');
  123. }
  124. /**
  125. * 回收站
  126. */
  127. public function recyclebin()
  128. {
  129. //设置过滤方法
  130. $this->request->filter(['strip_tags', 'trim']);
  131. if ($this->request->isAjax()) {
  132. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  133. $list = $this->model
  134. ->onlyTrashed()
  135. ->with('goods')
  136. ->where($where)
  137. ->order($sort, $order)
  138. ->paginate($limit);
  139. $result = array("total" => $list->total(), "rows" => $list->items());
  140. return json($result);
  141. }
  142. return $this->view->fetch();
  143. }
  144. private function buildSearch () {
  145. $filter = $this->request->get("filter", '');
  146. $filter = (array)json_decode($filter, true);
  147. $filter = $filter ? $filter : [];
  148. $goods_title = isset($filter['goods_title']) ? $filter['goods_title'] : '';
  149. $stock_type = isset($filter['stock_type']) ? $filter['stock_type'] : 'all';
  150. $name = $this->model->getQuery()->getTable();
  151. $tableName = $name . '.';
  152. $stockWarning = $this->model;
  153. // 商品名称
  154. if ($goods_title) {
  155. $stockWarning = $stockWarning->whereExists(function ($query) use ($goods_title, $tableName) {
  156. $goodsTableName = (new \app\admin\model\shopro\goods\Goods())->getQuery()->getTable();
  157. $query = $query->table($goodsTableName)->where($goodsTableName . '.id=g.goods_id');
  158. $query = $query->where('title', 'like', "%{$goods_title}%");
  159. return $query;
  160. });
  161. }
  162. if ($stock_type != 'all') {
  163. if ($stock_type == 'over') {
  164. $stockWarning = $stockWarning->where('stock', '<=', 0);
  165. } else {
  166. $stockWarning = $stockWarning->where('stock', '>', 0);
  167. }
  168. }
  169. return $stockWarning;
  170. }
  171. }