SystemStoreProductStock.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace app\admin\model\system;
  3. use app\admin\model\order\StoreOrder;
  4. use app\admin\model\store\StoreProduct;
  5. use app\admin\model\store\StoreProductAttrValue;
  6. use app\admin\model\ump\StoreSeckill;
  7. use app\models\store\StoreCart;
  8. use crmeb\services\PHPExcelService;
  9. use crmeb\traits\ModelTrait;
  10. use crmeb\basic\BaseModel;
  11. use think\facade\Db;
  12. use think\session\Store;
  13. /**
  14. * 店铺仓库明细 model
  15. * Class User
  16. * @package app\admin\model\user
  17. */
  18. class SystemStoreProductStock extends BaseModel
  19. {
  20. /**
  21. * 数据表主键
  22. * @var string
  23. */
  24. protected $pk = 'id';
  25. /**
  26. * 模型名称
  27. * @var string
  28. */
  29. protected $name = 'system_store_product_stock';
  30. use ModelTrait;
  31. public static function getStockList($where, $store_id)
  32. {
  33. $model = self::alias('s')
  34. ->field('s.*')
  35. ->join('store_product p', 's.product_id = p.id', 'right')
  36. ->join('store_product_attr_value pv', 's.unique = pv.unique', 'right')
  37. ->where('s.store_id', $store_id);
  38. if (isset($where['product_id']) && $where['product_id'] != '') {
  39. $model = $model->where('s.product_id', $where['product_id']);
  40. }
  41. if (isset($where['type']) && $where['type'] != '') {
  42. switch ($where['type']) {
  43. case 1:
  44. $model = $model->where('s.stock', '<=', sys_config('store_stock') > 0 ? sys_config('store_stock') : 20);
  45. break;
  46. }
  47. }
  48. $count = $model->count();
  49. $model = $model->order('s.product_id asc,s.id desc');
  50. if (!(isset($where['excel']) && $where['excel'] == 1)) {
  51. $model = $model->page((int)$where['page'], (int)$where['limit']);
  52. }
  53. $data = $model->select()->each(function ($item) use ($store_id, $where) {
  54. $item['product_info'] = StoreProduct::get($item['product_id']);
  55. if ($item['product_info'])
  56. $item['product_info']['product_attr_info'] = StoreProductAttrValue::get(['unique' => $item['unique']]);
  57. if (!(isset($where['start_time']) && $where['start_time'] != '')) {
  58. $where['start_time'] = 0;
  59. } else {
  60. $where['start_time'] = strtotime($where['start_time']);
  61. }
  62. if (!(isset($where['end_time']) && $where['end_time'] != '')) {
  63. $where['end_time'] = 9999999999;
  64. } else {
  65. $where['end_time'] = strtotime($where['end_time']);
  66. }
  67. $deal_function = function ($type) use ($store_id, $where, $item) {
  68. $sum = 0;
  69. if (isset($where['data']) && $where['data'] != '') {
  70. $model = StoreOrder::getModelTime($where, new StoreOrder(), 'pay_time');
  71. } else {
  72. $model = new StoreOrder();
  73. }
  74. if (!(isset($where['data']) && $where['data'] != '')) {
  75. $model = $model->where('pay_time', 'between', [$where['start_time'], $where['end_time']]);
  76. }
  77. if ($type == 'wait') {
  78. $orders = $model
  79. ->where('store_id', $store_id)
  80. ->where('paid', 1)
  81. ->where('status', 0)
  82. ->where('is_del',0)
  83. ->where('is_system_del',0)
  84. ->where('refund_status', 0)
  85. ->select();
  86. } else if ($type == "wait_check") {
  87. $orders = $model
  88. ->where('store_id', $store_id)
  89. ->where('paid', 1)
  90. ->where('shipping_type', 2)
  91. ->where('status', 0)
  92. ->where('refund_status', 0)
  93. ->select();
  94. } else if ($type == "wait_send") {
  95. $orders = $model
  96. ->where('store_id', $store_id)
  97. ->where('paid', 1)
  98. ->where('status', 0)
  99. ->where('refund_status', 0)
  100. ->select();
  101. } else {
  102. $orders = $model->where('store_id', $store_id)
  103. ->where('paid', 1)
  104. ->where('refund_status', 0)
  105. ->select();
  106. }
  107. foreach ($orders as $v) {
  108. if (!$v['seckill_id'] && !$v['bargain_id'] && !$v['combination_id'] && !$v['integral_id'])
  109. $sum += StoreCart::where('is_del', 0)
  110. ->where('is_pay', 1)
  111. ->where('id', 'in', json_decode($v['cart_id'], true))
  112. ->where('product_id', $item['product_id'])
  113. ->where('product_attr_unique', $item['unique'])
  114. ->sum('cart_num');
  115. else {
  116. $info = StoreProductAttrValue::where(['unique' => $item['unique']])->find();
  117. $type = 0;
  118. $product_id = $item['product_id'];
  119. if ($v['seckill_id']) {
  120. $type = 1;
  121. $product_id = $v['seckill_id'];
  122. }
  123. if ($v['bargain_id']) {
  124. $type = 2;
  125. $product_id = $v['bargain_id'];
  126. }
  127. if ($v['combination_id']) {
  128. $type = 3;
  129. $product_id = $v['combination_id'];
  130. }
  131. if ($v['integral_id']) {
  132. $type = 4;
  133. $product_id = $v['integral_id'];
  134. }
  135. $unique = StoreProductAttrValue::where('suk', $info['suk'])->where('product_id', $product_id)->where('type', $type)->value('unique');
  136. $sum += StoreCart::where('is_del', 0)
  137. ->where('is_pay', 1)
  138. ->where('id', 'in', json_decode($v['cart_id'], true))
  139. ->where('product_id', $item['product_id'])
  140. ->where('product_attr_unique', $unique)
  141. ->sum('cart_num');
  142. }
  143. }
  144. return $sum;
  145. };
  146. $wait = $deal_function('wait');
  147. $wait_check = $deal_function('wait_check');
  148. $wait_send = $deal_function('wait_send');
  149. $sales = $deal_function('sales');
  150. $item['wait'] = $wait;
  151. $item['wait_check'] = $wait_check;
  152. $item['wait_send'] = $wait_send;
  153. $item['sales'] = $sales;
  154. });
  155. if (isset($where['excel']) && $where['excel'] == 1) {
  156. $export = [];
  157. foreach ($data as $index => $item) {
  158. $export[] = [
  159. $item['product_info']['store_name'],
  160. $item['product_info']['product_attr_info']['suk'],
  161. $item['stock'],
  162. $item['wait_check'],
  163. $item['wait_send'],
  164. $item['sales'],
  165. ];
  166. }
  167. PHPExcelService::setExcelHeader(['商品名称', '规格', '库存', '待核销', '待送货', '销量'])
  168. ->setExcelTile('门店库存导出', '门店库存信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  169. ->setExcelContent($export)
  170. ->ExcelSave();
  171. }
  172. return compact('count', 'data');
  173. }
  174. public static function getWarning($store_id)
  175. {
  176. $model = self::where('store_id', $store_id);
  177. $model = $model->where('stock', '<=', sys_config('store_stock') > 0 ? sys_config('store_stock') : 20);
  178. $count = $model->count();
  179. return $count;
  180. }
  181. }