StockWarning.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace addons\shopro\library\traits;
  3. use addons\shopro\exception\Exception;
  4. use addons\shopro\model\StockWarning as StockWarningModel;
  5. use addons\shopro\model\Goods;
  6. use addons\shopro\model\GoodsSkuPrice;
  7. /**
  8. * 库存预警
  9. */
  10. trait StockWarning
  11. {
  12. /**
  13. * 获取全局库存配置
  14. *
  15. * @return void
  16. */
  17. public function getStockConfig() {
  18. $config = json_decode(\addons\shopro\model\Config::cache(60)->where(['name' => 'order'])->value('value'), true);
  19. $stockConfig = $config && isset($config['goods']) ? $config['goods'] : [];
  20. if (!isset($stockConfig['stock_warning'])) {
  21. $stockConfig['stock_warning'] = 0;
  22. }
  23. return $stockConfig;
  24. }
  25. /**
  26. * 获取库存预警阀值
  27. *
  28. * @param [type] $goodsSkuPrice
  29. * @return void
  30. */
  31. public function getStockWarning($goodsSkuPrice) {
  32. if (!is_null($goodsSkuPrice['stock_warning'])) {
  33. $stock_warning = $goodsSkuPrice['stock_warning'];
  34. } else {
  35. $stockConfig = $this->getStockConfig();
  36. $stock_warning = $stockConfig['stock_warning'];
  37. }
  38. return $stock_warning;
  39. }
  40. /**
  41. * 检测库存是否低于预警阀值,并且记录
  42. *
  43. * @param [type] $goodsSkuPrice
  44. * @return void
  45. */
  46. public function checkStockWarning ($goodsSkuPrice, $type = 'edit') {
  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. foreach ($goodsSkuPrices as $key => $goodsSkuPrice) {
  67. $this->checkStockWarning($goodsSkuPrice, $type);
  68. }
  69. }
  70. /**
  71. * 记录库存低于预警值
  72. *
  73. * @param [type] $goodsSkuPrice
  74. * @param [type] $stock_warning
  75. * @return void
  76. */
  77. public function addStockWarning($goodsSkuPrice, $stock_warning) {
  78. $stockWarning = StockWarningModel::where('goods_sku_price_id', $goodsSkuPrice['id'])
  79. ->where('goods_id', $goodsSkuPrice['goods_id'])->find();
  80. if ($stockWarning) {
  81. if ($stockWarning['stock_warning'] != $stock_warning
  82. || $stockWarning->goods_sku_text != $goodsSkuPrice['goods_sku_text']
  83. ) {
  84. $stockWarning->goods_sku_text = is_array($goodsSkuPrice['goods_sku_text']) ? join(',', $goodsSkuPrice['goods_sku_text']) : $goodsSkuPrice['goods_sku_text'];;
  85. $stockWarning->stock_warning = $stock_warning;
  86. $stockWarning->save();
  87. }
  88. } else {
  89. $stockWarning = new StockWarningModel();
  90. $stockWarning->goods_id = $goodsSkuPrice['goods_id'];
  91. $stockWarning->goods_sku_price_id = $goodsSkuPrice['id'];
  92. $stockWarning->goods_sku_text = is_array($goodsSkuPrice['goods_sku_text']) ? join(',', $goodsSkuPrice['goods_sku_text']) : $goodsSkuPrice['goods_sku_text'];
  93. $stockWarning->stock_warning = $stock_warning;
  94. $stockWarning->save();
  95. }
  96. return $stockWarning;
  97. }
  98. /**
  99. * 删除规格预警,比如:多规格编辑之后,作废的规格预警,补充库存之后的规格预警
  100. *
  101. * @param array $ids
  102. * @param integer $goods_id
  103. * @return void
  104. */
  105. public function delStockWarning($goodsSkuPriceIds = [], $goods_id = 0) {
  106. $goodsSkuPriceIds = is_array($goodsSkuPriceIds) ? $goodsSkuPriceIds : [$goodsSkuPriceIds];
  107. StockWarningModel::destroy(function ($query) use ($goods_id, $goodsSkuPriceIds) {
  108. $query->where('goods_id', $goods_id)
  109. ->where('goods_sku_price_id', 'in', $goodsSkuPriceIds);
  110. });
  111. }
  112. /**
  113. * 删除商品除了这些规格之外的规格预警
  114. *
  115. * @param array $ids
  116. * @param integer $goods_id
  117. * @return void
  118. */
  119. public function delNotStockWarning($goodsSkuPriceIds = [], $goods_id = 0) {
  120. $goodsSkuPriceIds = is_array($goodsSkuPriceIds) ? $goodsSkuPriceIds : [$goodsSkuPriceIds];
  121. StockWarningModel::destroy(function ($query) use ($goods_id, $goodsSkuPriceIds) {
  122. $query->where('goods_id', $goods_id)
  123. ->where('goods_sku_price_id', 'not in', $goodsSkuPriceIds);
  124. });
  125. }
  126. }