StockWarning.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace addons\shopro\traits;
  3. use app\admin\model\shopro\Config;
  4. use app\admin\model\shopro\goods\StockWarning as StockWarningModel;
  5. use app\admin\model\shopro\goods\StockLog as StockLogModel;
  6. use app\admin\model\shopro\goods\SkuPrice as SkuPriceModel;
  7. /**
  8. * 库存预警
  9. */
  10. trait StockWarning
  11. {
  12. /**
  13. * 获取全局库存配置
  14. *
  15. * @return void
  16. */
  17. public function getStockConfig()
  18. {
  19. $stock_warning = Config::getConfigField('shop.goods.stock_warning');
  20. return intval($stock_warning);
  21. }
  22. /**
  23. * 获取库存预警阀值
  24. *
  25. * @param [type] $goodsSkuPrice
  26. * @return void
  27. */
  28. public function getStockWarning($goodsSkuPrice)
  29. {
  30. if (!is_null($goodsSkuPrice['stock_warning'])) {
  31. // 商品存在库存预警值
  32. $stock_warning = $goodsSkuPrice['stock_warning'];
  33. } else {
  34. // 默认库存预警值
  35. $stock_warning = $this->getStockConfig();
  36. }
  37. return $stock_warning;
  38. }
  39. /**
  40. * 检测库存是否低于预警阀值,并且记录
  41. *
  42. * @param [type] $goodsSkuPrice
  43. * @return void
  44. */
  45. public function checkStockWarning($goodsSkuPrice, $type = 'edit')
  46. {
  47. $stock_warning = $this->getStockWarning($goodsSkuPrice);
  48. // 读取系统配置库存预警值
  49. if ($goodsSkuPrice['stock'] < $stock_warning) {
  50. // 增加库存不足记录
  51. $this->addStockWarning($goodsSkuPrice, $stock_warning);
  52. } else {
  53. if ($type == 'edit') {
  54. // 如果编辑了并且库存大于预警值需要检查并把记录删除
  55. $this->delStockWarning($goodsSkuPrice['id'], $goodsSkuPrice['goods_id']);
  56. }
  57. }
  58. }
  59. /**
  60. * 检测这个商品的所有规格库存预警
  61. *
  62. * @param [type] $goodsSkuPrices
  63. * @return void
  64. */
  65. public function checkAllStockWarning($goodsSkuPrices, $type = 'add')
  66. {
  67. foreach ($goodsSkuPrices as $key => $goodsSkuPrice) {
  68. $this->checkStockWarning($goodsSkuPrice, $type);
  69. }
  70. }
  71. /**
  72. * 记录库存低于预警值
  73. *
  74. * @param [type] $goodsSkuPrice
  75. * @param [type] $stock_warning
  76. * @return void
  77. */
  78. public function addStockWarning($goodsSkuPrice, $stock_warning)
  79. {
  80. $stockWarning = StockWarningModel::where('goods_sku_price_id', $goodsSkuPrice['id'])
  81. ->where('goods_id', $goodsSkuPrice['goods_id'])->find();
  82. if ($stockWarning) {
  83. if ($stockWarning['stock_warning'] != $stock_warning
  84. || $stockWarning->goods_sku_text != $goodsSkuPrice['goods_sku_text']
  85. ) {
  86. $stockWarning->goods_sku_text = is_array($goodsSkuPrice['goods_sku_text']) ? join(',', $goodsSkuPrice['goods_sku_text']) : $goodsSkuPrice['goods_sku_text'];;
  87. $stockWarning->stock_warning = $stock_warning;
  88. $stockWarning->save();
  89. }
  90. } else {
  91. $stockWarning = new StockWarningModel();
  92. $stockWarning->goods_id = $goodsSkuPrice['goods_id'];
  93. $stockWarning->goods_sku_price_id = $goodsSkuPrice['id'];
  94. $stockWarning->goods_sku_text = is_array($goodsSkuPrice['goods_sku_text']) ? join(',', $goodsSkuPrice['goods_sku_text']) : $goodsSkuPrice['goods_sku_text'];
  95. $stockWarning->stock_warning = $stock_warning;
  96. $stockWarning->save();
  97. }
  98. // 库存预警变动事件
  99. $data = ['goodsSkuPrice' => $goodsSkuPrice, 'stock_warning' => $stock_warning];
  100. \think\Hook::listen('goods_stock_warning', $data);
  101. return $stockWarning;
  102. }
  103. /**
  104. * 删除规格预警,比如:多规格编辑之后,作废的规格预警,补充库存之后的规格预警
  105. *
  106. * @param array $ids
  107. * @param integer $goods_id
  108. * @return void
  109. */
  110. public function delStockWarning($goodsSkuPriceIds = [], $goods_id = 0)
  111. {
  112. $goodsSkuPriceIds = is_array($goodsSkuPriceIds) ? $goodsSkuPriceIds : [$goodsSkuPriceIds];
  113. StockWarningModel::destroy(function ($query) use ($goods_id, $goodsSkuPriceIds) {
  114. $query->where('goods_id', $goods_id)
  115. ->where('goods_sku_price_id', 'in', $goodsSkuPriceIds);
  116. });
  117. }
  118. /**
  119. * 删除商品除了这些规格之外的规格预警
  120. *
  121. * @param array $ids
  122. * @param integer $goods_id
  123. * @return void
  124. */
  125. public function delNotStockWarning($goodsSkuPriceIds = [], $goods_id = 0)
  126. {
  127. $goodsSkuPriceIds = is_array($goodsSkuPriceIds) ? $goodsSkuPriceIds : [$goodsSkuPriceIds];
  128. StockWarningModel::destroy(function ($query) use ($goods_id, $goodsSkuPriceIds) {
  129. $query->where('goods_id', $goods_id)
  130. ->where('goods_sku_price_id', 'not in', $goodsSkuPriceIds);
  131. });
  132. }
  133. /**
  134. * 补货
  135. *
  136. * @param think\model $goodsSkuPrice
  137. * @param integer $stock
  138. * @return void
  139. */
  140. public function addStockToSkuPrice($goodsSkuPrice, $stock, $type)
  141. {
  142. $before = $goodsSkuPrice->stock;
  143. // 补充库存
  144. $goodsSkuPrice->setInc('stock', $stock);
  145. // 添加补货记录
  146. $this->addStockLog($goodsSkuPrice, $before, $stock, $type);
  147. // 检测库存预警
  148. $goodsSkuPrice = SkuPriceModel::find($goodsSkuPrice->id); // 重新获取 skuPrice
  149. $this->checkStockWarning($goodsSkuPrice);
  150. }
  151. /**
  152. * 添加补货记录
  153. *
  154. * @param array $goodsSkuPrice
  155. * @param int $stock
  156. * @return void
  157. */
  158. public function addStockLog($goodsSkuPrice, $before, $stock, $type = 'add')
  159. {
  160. $admin = auth_admin();
  161. $stockWarning = new StockLogModel();
  162. $stockWarning->goods_id = $goodsSkuPrice['goods_id'];
  163. $stockWarning->admin_id = $admin['id'];
  164. $stockWarning->goods_sku_price_id = $goodsSkuPrice['id'];
  165. $stockWarning->goods_sku_text = is_array($goodsSkuPrice['goods_sku_text']) ? join(',', $goodsSkuPrice['goods_sku_text']) : $goodsSkuPrice['goods_sku_text'];
  166. $stockWarning->before = $before;
  167. $stockWarning->stock = $stock;
  168. switch ($type) {
  169. case 'add':
  170. $msg = '添加商品';
  171. break;
  172. case 'goods':
  173. $msg = '商品列表补库存';
  174. break;
  175. case 'stock_warning':
  176. $msg = '库存预警补库存';
  177. break;
  178. default :
  179. $msg = '';
  180. break;
  181. }
  182. $stockWarning->msg = $msg;
  183. $stockWarning->save();
  184. }
  185. }