SystemStoreStock.php 8.9 KB

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