diff --git a/app/Console/Commands/GoodsSkuDailyReport.php b/app/Console/Commands/GoodsSkuDailyReport.php index a7cacd8..4cda899 100644 --- a/app/Console/Commands/GoodsSkuDailyReport.php +++ b/app/Console/Commands/GoodsSkuDailyReport.php @@ -100,13 +100,7 @@ class GoodsSkuDailyReport extends Command $data[$orderItem->external_sku_id]['total_goods_number'] += $orderItem->goods_number; $data[$orderItem->external_sku_id]['total_cancel_number'] += $orderItem->already_cancel_number; - if (isset($data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id])) { - $data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id]['total_goods_price'] += $orderItem->goods_price; - $data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id]['total_goods_cost_price'] += $orderItem->goods_cost_price; - $data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id]['total_goods_amount'] += $orderItem->goods_amount; - $data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id]['total_goods_number'] += $orderItem->goods_number; - $data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id]['total_cancel_number'] += $orderItem->already_cancel_number; - } else { + if (!isset($data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id])) { $data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id] = [ 'total_goods_price' => 0, 'total_goods_cost_price' => 0, @@ -115,6 +109,12 @@ class GoodsSkuDailyReport extends Command 'total_cancel_number' => 0, ]; } + + $data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id]['total_goods_price'] += $orderItem->goods_price; + $data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id]['total_goods_cost_price'] += $orderItem->goods_cost_price; + $data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id]['total_goods_amount'] += $orderItem->goods_amount; + $data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id]['total_goods_number'] += $orderItem->goods_number; + $data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id]['total_cancel_number'] += $orderItem->already_cancel_number; } foreach ($data as $externalSkuId => $datum) { diff --git a/app/Exports/WeekDataExport.php b/app/Exports/WeekDataExport.php new file mode 100644 index 0000000..49d9090 --- /dev/null +++ b/app/Exports/WeekDataExport.php @@ -0,0 +1,112 @@ +data = $this->createData(); + } + + /** + * @return \Illuminate\Support\Collection + */ + public function collection() + { + return new Collection($this->data); + } + + private function createData() + { + $headTitle = [ + '商品名称', + '品类', + '规格', + '品牌', + '商品编码', + '成本', + '销售数量', + ]; + $shops = Shop::query()->orderBy('id')->pluck('name', 'id')->toArray(); + $headTitle = array_merge($headTitle, array_values($shops)); + + $startDate = Carbon::now()->subWeek()->startOfWeek()->toDateString(); + $endDate = Carbon::now()->subWeek()->endOfWeek()->toDateString(); + + $dailyReports = DailyReport::query() + ->with([ + 'goods:id,title', + 'goodsType:id,name', + 'goodsSku:id,title', + 'goodsBrand:id,name', + ]) +// ->where('date', '>=', $startDate) +// ->where('date', '<=', $endDate) + ->get(); + if ($dailyReports->isEmpty()) { + return [$headTitle]; + } + + $arr = []; + foreach ($dailyReports as $item) { + $number = $item->total_goods_number - $item->total_cancel_number; + if (!isset($arr[$item->external_sku_id])) { + $arr[$item->external_sku_id] = [ + 'goodsTitle' => $item->goods->title, + 'goodsTypeName' => $item->goodsType->name, + 'goodsSkuTitle' => $item->goodsSku->title, + 'goodsBrandName' => $item->goodsBrand->name, + 'external_sku_id' => $item->external_sku_id, + 'cost' => [], + 'number' => [], + 'shops' => [] + ]; + } + $arr[$item->external_sku_id]['cost'][] = $item->cost; + $arr[$item->external_sku_id]['number'][] = $number; + foreach ($item->shop_data as $shopId => $shopData) { + $number = $shopData['total_goods_number'] - $shopData['total_cancel_number']; + if (!isset($arr[$item->external_sku_id]['shops'][$shopId])) { + $arr[$item->external_sku_id]['shops'][$shopId] = 0; + } + $arr[$item->external_sku_id]['shops'][$shopId] += $number; + } + } + $bodyData = []; + foreach ($arr as $item) { + $cost = array_sum($item['cost']) / count($item['cost']); + $shopData = []; + foreach ($shops as $shopId => $shopName) { + $shopData[] = $item['shops'][$shopId] ?? 0; + } + $data = [ + $item['goodsTitle'], + $item['goodsTypeName'], + $item['goodsSkuTitle'], + $item['goodsBrandName'], + $item['external_sku_id'], + $cost, + array_sum($item['number']), + ]; + + $bodyData[] = array_merge($data, $shopData); + } + + return [$headTitle, $bodyData]; + } +} diff --git a/app/Http/Controllers/Goods/GoodsSkusController.php b/app/Http/Controllers/Goods/GoodsSkusController.php index 69871c4..d7229bb 100644 --- a/app/Http/Controllers/Goods/GoodsSkusController.php +++ b/app/Http/Controllers/Goods/GoodsSkusController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Goods; use App\Events\BatchStockUpdateEvent; use App\Events\StockUpdateEvent; use App\Exports\GoodsSkusExport; +use App\Exports\WeekDataExport; use App\Http\Controllers\Controller; use App\Http\Requests\GoodsRequest; use App\Http\Requests\GoodsSkuRequest; @@ -523,6 +524,9 @@ class GoodsSkusController extends Controller { $type = $request->get('exportType'); ob_end_clean(); + if ('week_data' === $type) { + return Excel::download(new WeekDataExport(), $type . '.xlsx'); + } return Excel::download(new GoodsSkusExport($type), $type . '.xlsx'); } diff --git a/app/Models/DailyReport.php b/app/Models/DailyReport.php index b06bba6..d629f2a 100644 --- a/app/Models/DailyReport.php +++ b/app/Models/DailyReport.php @@ -11,4 +11,24 @@ class DailyReport extends Model protected $casts = [ 'shop_data' => 'array', ]; + + public function goods() + { + return $this->hasOne(Goods::class, 'id', 'goods_id'); + } + + public function goodsBrand() + { + return $this->belongsTo(GoodsBrand::class, 'brand_id', 'id'); + } + + public function goodsType() + { + return $this->belongsTo(GoodsType::class, 'type_id', 'id'); + } + + public function goodsSku() + { + return $this->hasOne(GoodsSku::class, 'id', 'goods_sku_id'); + } } diff --git a/database/migrations/2024_02_04_155743_create_daily_reports_table.php b/database/migrations/2024_02_04_155743_create_daily_reports_table.php index a4e0bda..516cbfe 100644 --- a/database/migrations/2024_02_04_155743_create_daily_reports_table.php +++ b/database/migrations/2024_02_04_155743_create_daily_reports_table.php @@ -26,7 +26,7 @@ class CreateDailyReportsTable extends Migration $table->unsignedInteger('total_goods_amount')->default(0); $table->unsignedInteger('total_goods_number')->default(0); $table->unsignedInteger('total_cancel_number')->default(0); - $table->unsignedInteger('cost')->default(0); + $table->unsignedDecimal('cost')->default(0); $table->text('shop_data'); $table->timestamps();