getCurrentPage(); // 每页显示行数 $perPage = $model->getPerPage(); $query = $this->getQuery($model); $list = $query->paginate($perPage, ['*'], 'page', $currentPage); $total = $query->count(); $tags = StockLog::tags()->get(); $data = []; foreach($list as $item) { $stock = $item->stockLogs; $in = $stock->where('product_sku_id', $item->product_sku_id)->where('tag_id', StockLog::TAG_IN)->sum('amount'); $loss = $stock->where('product_sku_id', $item->product_sku_id)->where('tag_id', StockLog::TAG_LOSS)->sum('amount'); $self = $stock->where('product_sku_id', $item->product_sku_id)->where('tag_id', StockLog::TAG_SELF)->sum('amount'); $sell_price = data_get($item->productSku, 'sell_price') / 100; $cost_price = data_get($item->productSku, 'cost_price') / 100; $current_stock = data_get($item, 'amount'); $subData = [ 'id' => $item->product_sku_id, 'store_name' => data_get($item->store, 'title'), 'name' => data_get($item->productSku, 'name'), 'category_id' => data_get($item->productSku, 'category.id'), 'category_name' => data_get($item->productSku, 'category.name'), 'cost_price' => $cost_price, 'sell_price' => $sell_price, 'stock' => $current_stock, // 入库数 'tag_in' => $in, // 销售数 = 提货数 + 发货数 'tag_sell' => $stock->where('product_sku_id', $item->product_sku_id)->whereIn('tag_id', [StockLog::TAG_CARRY, StockLog::TAG_SEND])->sum('amount'), // 线上销售数 = 发货数 'tag_online' => $stock->where('product_sku_id', $item->product_sku_id)->where('tag_id', StockLog::TAG_SEND)->sum('amount'), // 报损数 'tag_loss' => $loss, // 自用数 'tag_self' => $self, // 初期库存 'init_stock' => data_get($item->stockLog, 'balance', 0), // 调货数 = 出库数 'tag_out' => $stock->where('product_sku_id', $item->product_sku_id)->where('tag_id', StockLog::TAG_OUT)->sum('amount'), // 毛利 = 现有库存 x (售价-成本) 'total_profit' => round($current_stock * ($sell_price - $cost_price), PHP_ROUND_HALF_DOWN), ]; array_push($data, $subData); } return $model->makePaginator($total, $data); } public function getQuery(Grid\Model $model) { $query = ProductSku::with([ 'store', 'productSku.category', 'stockLogs' => function ($q) use ($model) { $q->select('id', 'product_sku_id', DB::raw('abs(`amount`) as `amount`'), 'tag_id'); $filter = $model->filter(); if ($start = $filter->input('date.start')) { $start = Carbon::createFromFormat('Y-m-d', $start)->startOfDay(); $q->where('created_at', '>=', $start); } if ($end = $filter->input('date.end')) { $end = Carbon::createFromFormat('Y-m-d', $end)->endOfDay(); $q->where('created_at', '<=', $end); } if ($store_id = $filter->input('store_id')) { $q->where('store_id', $store_id); } // $tag_ids = data_get($filter->filters(), 3); // if ($tag_ids && $tag_ids->getValue()) { // $q->whereIn('tag_id', $tag_ids->getValue()); // } if ($tag_id = $filter->input('tag_id')) { $q->where('tag_id', $tag_id); } }, 'stockLog' => function ($q) use ($model) { if ($store_id = $model->filter()->input('store_id')) { $q->where('store_id', $store_id); } if ($start = $model->filter()->input('date.start')) { $start = Carbon::createFromFormat('Y-m-d', $start)->startOfDay(); $q->where('created_at', '<=', $start); } $q->orderBy('created_at', 'desc'); }, ]); // 查询条件 $filters = $model->filter()->input(); foreach($filters as $key => $value) { if ($key == 'category_id') { $query->whereHas('productSku', fn($q1) => $q1->filter(['category' => $value], \App\Endpoint\Api\Filters\ProductSkuFilter::class)); } else if ($key == 'store_id') { $query->where('store_id', $value); } else if ($key == 'product_name') { $query->whereHas('productSku', fn($q1) => $q1->filter(['keyword' => $value], \App\Endpoint\Api\Filters\ProductSkuFilter::class)); } } // 排序条件 [$orderColumn, $orderType] = $model->getSort(); if ($orderColumn == 'stock') { $query->orderBy('amount', $orderType); } return $query; } public function getTotal(Grid\Model $model) { $query = $this->getQuery($model); $list = $query->get(); $data = [ // 入库总成本 = 总数量 * 成本价 'total_in' => 0, // 销售数 = (提货数 + 发货数) * 售价 'total_sell' => 0, // 线上销售总金额 = 发货数 * 售价 'total_online' => 0, // 报损总成本 = 报损数 * 成本价 'total_loss' => 0, // 自用总成本 = 自用数 * 成本价 'total_self' => 0, // 调货总成本 = 调货数(出库) * 成本价 'total_out' => 0, // 现有总成本 = 库存 * 成本价 'total_cost_price' => 0, // 现有总价值 = 库存 * 售价 'total_sell_price' => 0, // 毛利 = 库存 x (售价-成本) 'total_profit' => 0, ]; foreach ($list as $item) { $stock = $item->stockLogs; $cost_price = data_get($item, 'productSku.cost_price', 0) / 100; $sell_price = data_get($item, 'productSku.sell_price', 0) / 100; $current_stock = $item->amount; $in_amount = $stock->where('product_sku_id', $item->product_sku_id)->where('tag_id', StockLog::TAG_IN)->sum('amount'); $loss_amount = $stock->where('product_sku_id', $item->product_sku_id)->where('tag_id', StockLog::TAG_LOSS)->sum('amount'); $self_amount = $stock->where('product_sku_id', $item->product_sku_id)->where('tag_id', StockLog::TAG_SELF)->sum('amount'); $data['total_in'] += $in_amount * $cost_price; $data['total_sell'] += $stock->where('product_sku_id', $item->product_sku_id)->whereIn('tag_id', [StockLog::TAG_CARRY, StockLog::TAG_SEND])->sum('amount') * $sell_price; $data['total_online'] += $stock->where('product_sku_id', $item->product_sku_id)->where('tag_id', StockLog::TAG_SEND)->sum('amount') * $sell_price; $data['total_loss'] += $loss_amount * $cost_price; $data['total_self'] += $self_amount * $cost_price; $data['total_out'] += $stock->where('product_sku_id', $item->product_sku_id)->where('tag_id', StockLog::TAG_OUT)->sum('amount') * $cost_price; $data['total_cost_price'] += $current_stock * $cost_price; $data['total_sell_price'] += $current_stock * $sell_price; $data['total_profit'] += $current_stock * ($sell_price - $cost_price); } return $data; } }