SystemStoreStock.php 8.5 KB

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