field('s.*') ->join('store_product p', 's.product_id = p.id', 'right') ->join('store_product_attr_value pv', 's.unique = pv.unique', 'right') ->where('s.store_id', $store_id); if (isset($where['product_id']) && $where['product_id'] != '') { $model = $model->where('s.product_id', $where['product_id']); } if (isset($where['type']) && $where['type'] != '') { switch ($where['type']) { case 1: $model = $model->where('s.stock', '<=', sys_config('store_stock') > 0 ? sys_config('store_stock') : 20); break; } } $count = $model->count(); $model = $model->order('s.product_id asc,s.id desc'); if (!(isset($where['excel']) && $where['excel'] == 1)) { $model = $model->page((int)$where['page'], (int)$where['limit']); } $data = $model->select()->each(function ($item) use ($store_id, $where) { $item['product_info'] = StoreProduct::get($item['product_id']); if ($item['product_info']) $item['product_info']['product_attr_info'] = StoreProductAttrValue::get(['unique' => $item['unique']]); if (!(isset($where['start_time']) && $where['start_time'] != '')) { $where['start_time'] = 0; } else { $where['start_time'] = strtotime($where['start_time']); } if (!(isset($where['end_time']) && $where['end_time'] != '')) { $where['end_time'] = 9999999999; } else { $where['end_time'] = strtotime($where['end_time']); } $deal_function = function ($type) use ($store_id, $where, $item) { $sum = 0; if (isset($where['data']) && $where['data'] != '') { $model = StoreOrder::getModelTime($where, new StoreOrder(), 'pay_time'); } else { $model = new StoreOrder(); } if (!(isset($where['data']) && $where['data'] != '')) { $model = $model->where('pay_time', 'between', [$where['start_time'], $where['end_time']]); } if ($type == 'wait') { $orders = $model ->where('store_id', $store_id) ->where('paid', 1) ->where('status', 0) ->where('is_del',0) ->where('is_system_del',0) ->where('refund_status', 0) ->select(); } else if ($type == "wait_check") { $orders = $model ->where('store_id', $store_id) ->where('paid', 1) ->where('shipping_type', 2) ->where('status', 0) ->where('refund_status', 0) ->select(); } else if ($type == "wait_send") { $orders = $model ->where('store_id', $store_id) ->where('paid', 1) ->where('status', 0) ->where('refund_status', 0) ->select(); } else { $orders = $model->where('store_id', $store_id) ->where('paid', 1) ->where('refund_status', 0) ->select(); } foreach ($orders as $v) { if (!$v['seckill_id'] && !$v['bargain_id'] && !$v['combination_id'] && !$v['integral_id']) $sum += StoreCart::where('is_del', 0) ->where('is_pay', 1) ->where('id', 'in', json_decode($v['cart_id'], true)) ->where('product_id', $item['product_id']) ->where('product_attr_unique', $item['unique']) ->sum('cart_num'); else { $info = StoreProductAttrValue::where(['unique' => $item['unique']])->find(); $type = 0; $product_id = $item['product_id']; if ($v['seckill_id']) { $type = 1; $product_id = $v['seckill_id']; } if ($v['bargain_id']) { $type = 2; $product_id = $v['bargain_id']; } if ($v['combination_id']) { $type = 3; $product_id = $v['combination_id']; } if ($v['integral_id']) { $type = 4; $product_id = $v['integral_id']; } $unique = StoreProductAttrValue::where('suk', $info['suk'])->where('product_id', $product_id)->where('type', $type)->value('unique'); $sum += StoreCart::where('is_del', 0) ->where('is_pay', 1) ->where('id', 'in', json_decode($v['cart_id'], true)) ->where('product_id', $item['product_id']) ->where('product_attr_unique', $unique) ->sum('cart_num'); } } return $sum; }; $wait = $deal_function('wait'); $wait_check = $deal_function('wait_check'); $wait_send = $deal_function('wait_send'); $sales = $deal_function('sales'); $item['wait'] = $wait; $item['wait_check'] = $wait_check; $item['wait_send'] = $wait_send; $item['sales'] = $sales; }); if (isset($where['excel']) && $where['excel'] == 1) { $export = []; foreach ($data as $index => $item) { $export[] = [ $item['product_info']['store_name'], $item['product_info']['product_attr_info']['suk'], $item['stock'], $item['wait_check'], $item['wait_send'], $item['sales'], ]; } PHPExcelService::setExcelHeader(['商品名称', '规格', '库存', '待核销', '待送货', '销量']) ->setExcelTile('门店库存导出', '门店库存信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time())) ->setExcelContent($export) ->ExcelSave(); } return compact('count', 'data'); } public static function getWarning($store_id) { $model = self::where('store_id', $store_id); $model = $model->where('stock', '<=', sys_config('store_stock') > 0 ? sys_config('store_stock') : 20); $count = $model->count(); return $count; } }