SystemStoreStock.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. $list = StoreProduct::where('is_del', 0)->where('is_show', 1)->column('id,is_consumer', 'id');
  34. $data['store_id'] = $store_id;
  35. $data['in_stock'] = $num;
  36. $all = [];
  37. $rs = StoreProductAttrValue::where('product_id', 'in', array_keys($list))->field('product_id,unique,image,bar_code,price')->select()->toArray();
  38. foreach ($rs as $v) {
  39. $data['product_id'] = $v['product_id'];
  40. $data['in_last_time'] = time();
  41. $data['unique'] = $v['unique'];
  42. $data['price'] = $v['price'];
  43. $data['image'] = $v['image'];
  44. $data['bar_code'] = $v['bar_code'];
  45. $data['is_consumer'] = $list[$v['product_id']]['is_consumer'];
  46. $all[] = $data;
  47. }
  48. self::insertAll($all);
  49. return true;
  50. }
  51. /**
  52. * 补货
  53. * @param $store_id
  54. * @param $data
  55. * @return bool
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\DbException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. */
  60. public static function replenish($store_id, $data)
  61. {
  62. if ($data['product_id'] > 0 && $data['in_stock'] > 0) {
  63. $info = self::where(['store_id' => $store_id, 'product_id' => $data['product_id'], 'bar_code' => $data['bar_code']])->find();
  64. if ($info) {
  65. $add = bcsub($data['in_stock'], $info['repair_sales']);
  66. if ($add > 0) {
  67. 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]);
  68. } else {
  69. 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'])]);
  70. }
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. public static function Refund($orderInfo, $data)
  77. {
  78. $reg_store_id = $orderInfo['reg_store_id'];
  79. if ($orderInfo['create_store_id'] > 0) {
  80. $reg_store_id = $orderInfo['create_store_id'];
  81. }
  82. $store_id = $orderInfo['store_id'];
  83. $field = "store_sales";
  84. if (intval($reg_store_id) > 0 && intval($reg_store_id) != $store_id) {
  85. $field = "repair_sales";
  86. }
  87. 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();
  88. return true;
  89. }
  90. /**
  91. * 门店销量
  92. * @param $order
  93. */
  94. public static function add_store_sales($orderInfo)
  95. {
  96. if ($orderInfo['suit']) return;
  97. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  98. $cartInfo = StoreOrderCartInfo::whereIn('cart_id', $cartId)->column('cart_info');
  99. $reg_store_id = $orderInfo['reg_store_id'];
  100. if ($orderInfo['create_store_id'] > 0) {
  101. $reg_store_id = $orderInfo['create_store_id'];
  102. }
  103. $store_id = $orderInfo['store_id'];
  104. $field = "store_sales";
  105. if (intval($reg_store_id) > 0 && intval($reg_store_id) != $store_id) {
  106. $field = "repair_sales";
  107. }
  108. foreach ($cartInfo as $value) {
  109. $product = json_decode($value, true);
  110. $cartNum = $product['cart_num'] ?? 0;
  111. $is_consumer = $product['is_consumer'] ?? 0;
  112. $unique = $product['product_attr_unique'];
  113. $product_id = $product['product_id'];
  114. $bar_code = $product['productInfo']['attrInfo']['bar_code'];
  115. $price = $product['productInfo']['attrInfo']['price'];
  116. $image = $product['productInfo']['attrInfo']['image'];
  117. if (self::be(compact('product_id', 'unique', 'is_consumer', 'store_id'))) {
  118. self::where(compact('product_id', 'unique', 'is_consumer', 'store_id'))->inc($field, $cartNum)->dec('in_stock', $cartNum)->update();
  119. } else {
  120. $in_stock = 0 - intval($cartNum);
  121. self::create(compact('product_id', 'unique', 'is_consumer', 'store_id', 'bar_code', 'price', 'image', 'in_stock'));
  122. }
  123. }
  124. }
  125. }