SystemStoreStock.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace app\models\system;
  3. use app\admin\model\store\StoreProduct;
  4. use app\admin\model\store\StoreProductAttrValue;
  5. use app\models\store\StoreOrderCartInfo;
  6. use crmeb\basic\BaseModel;
  7. use crmeb\traits\ModelTrait;
  8. class SystemStoreStock extends BaseModel
  9. {
  10. use ModelTrait;
  11. public static function lst($where)
  12. {
  13. $model = new self;
  14. $model = $model->alias("a")->join("store_product b", "a.product_id=b.id", "right");
  15. if ($where['store_id'] > 0) $model = $model->where('a.store_id', $where['store_id']);
  16. if ($where['key']) $model = $model->wherelike('b.store_name', "%" . $where['key'] . "%");
  17. if (isset($where['is_warn']) && $where['is_warn'] == 1) $model = $model->where('a.in_stock', '<=', sys_config('warn_stock'));
  18. $model = $model->where('a.id', '>', 0);
  19. $model = $model->field('a.product_id,b.store_name,b.image,b.is_show,b.is_del,a.store_id,a.in_stock,a.store_sales,a.repair_sales,a.in_last_time,a.bar_code,a.price,a.is_consumer');
  20. $count = $model->count();
  21. $data = $model->page($where['page'], $where['limit'])->order("product_id desc")->select()->toarray();
  22. foreach ($data as &$v) {
  23. $v['store'] = SystemStore::where('id', $v['store_id'])->value('name');
  24. }
  25. return compact('count', 'data');
  26. }
  27. /**
  28. * 店铺初始商品库存
  29. * @param int $num
  30. */
  31. public static function store_init($store_id, $num = 100)
  32. {
  33. $store = SystemStore::get($store_id);
  34. $where['store_id'] = 0;
  35. if ($store['is_triple']) $where['store_id'] = $store_id;
  36. $list = StoreProduct::where('is_del', 0)->where('is_show', 1)->where($where)->column('id,is_consumer', 'id');
  37. $data['store_id'] = $store_id;
  38. $data['in_stock'] = $num;
  39. $all = [];
  40. $rs = StoreProductAttrValue::where('product_id', 'in', array_keys($list))->where('type', 0)->field('product_id,unique,image,bar_code,price')->select()->toArray();
  41. foreach ($rs as $v) {
  42. $data['product_id'] = $v['product_id'];
  43. $data['in_last_time'] = time();
  44. $data['unique'] = $v['unique'];
  45. $data['price'] = $v['price'];
  46. $data['image'] = $v['image'];
  47. $data['bar_code'] = $v['bar_code'];
  48. $data['is_consumer'] = $list[$v['product_id']]['is_consumer'];
  49. $all[] = $data;
  50. }
  51. self::insertAll($all);
  52. return true;
  53. }
  54. /**
  55. * 补货
  56. * @param $store_id
  57. * @param $data
  58. * @return bool
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public static function replenish($store_id, $data)
  64. {
  65. if ($data['product_id'] > 0 && $data['in_stock'] > 0) {
  66. $info = self::where(['store_id' => $store_id, 'product_id' => $data['product_id'], 'bar_code' => $data['bar_code']])->find();
  67. if ($info) {
  68. if ($data['type'] == 1) {
  69. $add = bcsub($data['in_stock'], $info['repair_sales']);
  70. if ($add > 0) {
  71. self::where(['store_id' => $store_id, 'product_id' => $data['product_id'], 'bar_code' => $data['bar_code']])->update(['in_stock' => bcadd($info['in_stock'], $add), 'repair_sales' => 0]);
  72. } else {
  73. self::where(['store_id' => $store_id, 'product_id' => $data['product_id'], 'bar_code' => $data['bar_code']])->update(['repair_sales' => bcsub($info['repair_sales'], $data['in_stock'])]);
  74. }
  75. @file_put_contents("stock.txt", self::getlastsql());
  76. } else {
  77. if ($info['in_stock'] < $data['in_stock']) {
  78. return self::setErrorInfo('库存不足');
  79. }
  80. self::where(['store_id' => $store_id, 'product_id' => $data['product_id'], 'bar_code' => $data['bar_code']])->update(['in_stock' => bcsub($info['in_stock'], $data['in_stock'])]);
  81. @file_put_contents("stock.txt", self::getlastsql());
  82. }
  83. return true;
  84. } else {
  85. if ($data['type'] == 2) {
  86. return self::setErrorInfo('库存无法减少');
  87. }
  88. $data1['product_id'] = $data['product_id'];
  89. $data1['in_last_time'] = time();
  90. $data1['in_stock'] = $data['in_stock'];
  91. $data1['unique'] = StoreProductAttrValue::where('product_id', $data['product_id'])->value('unique');
  92. $data1['price'] = $data['price'];
  93. $data1['image'] = $data['image'];
  94. $data1['bar_code'] = $data['bar_code'];
  95. $data1['is_consumer'] = $data['is_consumer'];
  96. $data1['store_id'] = $store_id;
  97. self::create($data1);
  98. return true;
  99. }
  100. }
  101. return self::setErrorInfo('参数异常');
  102. }
  103. public static function Refund($orderInfo, $data)
  104. {
  105. $reg_store_id = $orderInfo['reg_store_id'];
  106. if ($orderInfo['create_store_id'] > 0) {
  107. $reg_store_id = $orderInfo['create_store_id'];
  108. }
  109. $store_id = $orderInfo['store_id'];
  110. $field = "store_sales";
  111. if (intval($reg_store_id) > 0 && intval($reg_store_id) != $store_id) {
  112. $field = "repair_sales";
  113. }
  114. self::where("product_id", $data['product_id'])->where('unique', $data['product_attr_unique'])->where('store_id', $store_id)->inc('in_stock', $data['cart_num'])->dec($field, $data['cart_num'])->update();
  115. return true;
  116. }
  117. /**
  118. * 门店销量
  119. * @param $order
  120. */
  121. public static function add_store_sales($orderInfo)
  122. {
  123. if ($orderInfo['suit']) return;
  124. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  125. $cartInfo = StoreOrderCartInfo::whereIn('cart_id', $cartId)->column('cart_info');
  126. $reg_store_id = $orderInfo['reg_store_id'];
  127. if ($orderInfo['create_store_id'] > 0) {
  128. $reg_store_id = $orderInfo['create_store_id'];
  129. }
  130. $store_id = $orderInfo['store_id'];
  131. $field = "store_sales";
  132. if (intval($reg_store_id) > 0 && intval($reg_store_id) != $store_id) {
  133. $field = "repair_sales";
  134. }
  135. foreach ($cartInfo as $value) {
  136. $product = json_decode($value, true);
  137. $cartNum = $product['cart_num'] ?? 0;
  138. $is_consumer = $product['is_consumer'] ?? 0;
  139. $unique = $product['product_attr_unique'];
  140. $product_id = $product['product_id'];
  141. $bar_code = $product['productInfo']['attrInfo']['bar_code'];
  142. $price = $product['productInfo']['attrInfo']['price'];
  143. $image = $product['productInfo']['attrInfo']['image'];
  144. if (self::be(compact('product_id', 'unique', 'is_consumer', 'store_id'))) {
  145. self::where(compact('product_id', 'unique', 'is_consumer', 'store_id'))->inc($field, $cartNum)->dec('in_stock', $cartNum)->update();
  146. } else {
  147. $in_stock = 0 - intval($cartNum);
  148. self::create(compact('product_id', 'unique', 'is_consumer', 'store_id', 'bar_code', 'price', 'image', 'in_stock'));
  149. }
  150. }
  151. }
  152. }