diff --git a/app/Console/Commands/GoodsSkuDailyReport.php b/app/Console/Commands/GoodsSkuDailyReport.php new file mode 100644 index 0000000..a7cacd8 --- /dev/null +++ b/app/Console/Commands/GoodsSkuDailyReport.php @@ -0,0 +1,127 @@ +argument('date'); + if (is_null($date)) { + $date = Carbon::yesterday()->format('Y-m-d'); + } + $startDateTime = Carbon::parse($date)->startOfDay()->toDateTimeString(); + $endDateTime = Carbon::parse($date)->endOfDay()->toDateTimeString(); + + $orderItems = BusinessOrderItem::query() + ->select(['shop_id', 'already_cancel_number', 'external_sku_id', 'goods_amount', 'goods_cost_price', 'goods_number', 'goods_price']) + ->where('external_sku_id', '<>', '') + ->where('created_at', '>=', $startDateTime) + ->where('created_at', '<=', $endDateTime) + ->get(); + if ($orderItems->isEmpty()) { + return; + } + + $externalSkuIds = array_unique(array_column($orderItems->toArray(), 'external_sku_id')); + $goodsSkus = GoodsSku::query() + ->select(['id', 'goods_id', 'cost', 'external_sku_id']) + ->with([ + 'goods:id,type_id,brand_id' + ]) + ->where('is_combination', 0) + ->whereIn('external_sku_id', $externalSkuIds) + ->get() + ->pluck(null, 'external_sku_id') + ->toArray(); + if (empty($goodsSkus)) { + return; + } + + $data = []; + foreach ($orderItems as $orderItem) { + if (!isset($goodsSkus[$orderItem->external_sku_id])) { + continue; + } + if (!isset($data[$orderItem->external_sku_id])) { + $data[$orderItem->external_sku_id] = [ + 'goods_id' => $goodsSkus[$orderItem->external_sku_id]['goods']['id'], + 'type_id' => $goodsSkus[$orderItem->external_sku_id]['goods']['type_id'], + 'brand_id' => $goodsSkus[$orderItem->external_sku_id]['goods']['brand_id'], + 'goods_sku_id' => $goodsSkus[$orderItem->external_sku_id]['id'], + 'cost' => $goodsSkus[$orderItem->external_sku_id]['cost'], + 'total_goods_price' => 0, + 'total_goods_cost_price' => 0, + 'total_goods_amount' => 0, + 'total_goods_number' => 0, + 'total_cancel_number' => 0, + 'shop_data' => [], + ]; + } + + $data[$orderItem->external_sku_id]['total_goods_price'] += $orderItem->goods_price; + $data[$orderItem->external_sku_id]['total_goods_cost_price'] += $orderItem->goods_cost_price; + $data[$orderItem->external_sku_id]['total_goods_amount'] += $orderItem->goods_amount; + $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 { + $data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id] = [ + 'total_goods_price' => 0, + 'total_goods_cost_price' => 0, + 'total_goods_amount' => 0, + 'total_goods_number' => 0, + 'total_cancel_number' => 0, + ]; + } + } + + foreach ($data as $externalSkuId => $datum) { + DailyReport::query()->firstOrCreate([ + 'date' => $date, + 'external_sku_id' => $externalSkuId + ], $datum); + } + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 4700d82..2818a03 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -2,6 +2,7 @@ namespace App\Console; +use App\Console\Commands\GoodsSkuDailyReport; use App\Console\Commands\Inventory; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; @@ -29,6 +30,7 @@ class Kernel extends ConsoleKernel { // 服务器/etc/crontab添加cron入口 // * * * * * cd /home/wwwroot/erp.chutang66.com && php artisan schedule:run >> /dev/null 2>&1 + $schedule->command(GoodsSkuDailyReport::class)->dailyAt('06:00'); $schedule->command(Inventory::class)->dailyAt('07:00'); $schedule->command(KttOrderQuery::class)->everyMinute(); $schedule->command(DeleteKttQuery::class)->daily(); diff --git a/app/Models/DailyReport.php b/app/Models/DailyReport.php new file mode 100644 index 0000000..b06bba6 --- /dev/null +++ b/app/Models/DailyReport.php @@ -0,0 +1,14 @@ + 'array', + ]; +} 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 new file mode 100644 index 0000000..a4e0bda --- /dev/null +++ b/database/migrations/2024_02_04_155743_create_daily_reports_table.php @@ -0,0 +1,46 @@ +bigIncrements('id'); + $table->date('date'); + $table->unsignedInteger('goods_id'); + $table->unsignedInteger('type_id'); + $table->unsignedInteger('brand_id'); + $table->unsignedInteger('goods_sku_id'); + $table->string('external_sku_id', 64); + $table->unsignedInteger('total_goods_price')->default(0); + $table->unsignedInteger('total_goods_cost_price')->default(0); + $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->text('shop_data'); + + $table->timestamps(); + $table->index(['date', 'external_sku_id']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('week_reports'); + } +}