SystemStoreStock.php 5.7 KB

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