mirror of
https://gitee.com/hzchunfen/erp.git
synced 2025-12-01 06:30:49 +00:00
feat: 销售记录
This commit is contained in:
parent
ab9e209910
commit
c3a5f39ae6
148
app/Console/Commands/DailySalesReport.php
Normal file
148
app/Console/Commands/DailySalesReport.php
Normal file
@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\BusinessOrder;
|
||||
use App\Models\BusinessOrderItem;
|
||||
use App\Models\GoodsSku;
|
||||
use App\Utils\ArrayUtils;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class DailySalesReport extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'daily:report:sales {S}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = '每日销量数据记录';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$s = $this->argument('S');
|
||||
$map = [
|
||||
'S1' => [
|
||||
'startTime' => date('Y-m-d 11:00:00'),
|
||||
'endTime' => date('Y-m-d 12:00:00'),
|
||||
],
|
||||
'S2' => [
|
||||
'startTime' => date('Y-m-d 11:00:00'),
|
||||
'endTime' => date('Y-m-d 13:30:00'),
|
||||
],
|
||||
'S3' => [
|
||||
'startTime' => date('Y-m-d 11:00:00'),
|
||||
'endTime' => date('Y-m-d 15:00:00'),
|
||||
],
|
||||
'S4' => [
|
||||
'startTime' => date('Y-m-d 15:00:00'),
|
||||
'endTime' => date('Y-m-d 16:00:00'),
|
||||
],
|
||||
'S5' => [
|
||||
'startTime' => date('Y-m-d 11:00:00'),
|
||||
'endTime' => date('Y-m-d 17:30:00'),
|
||||
],
|
||||
'S6' => [
|
||||
'startTime' => date('Y-m-d 11:00:00'),
|
||||
'endTime' => date('Y-m-d 20:00:00'),
|
||||
],
|
||||
'S7' => [
|
||||
'startTime' => Carbon::yesterday()->format('Y-m-d 11:00:00'),
|
||||
'endTime' => date('Y-m-d 09:30:00'),
|
||||
],
|
||||
];
|
||||
if (!isset($map[$s])) {
|
||||
return;
|
||||
}
|
||||
$startTime = $map[$s]['startTime'];
|
||||
$endTime = $map[$s]['endTime'];
|
||||
|
||||
$businessOrderIds = BusinessOrder::query()
|
||||
->where('confirm_at', '>=', Carbon::parse($startTime)->getPreciseTimestamp(3))
|
||||
->where('confirm_at', '<=', Carbon::parse($endTime)->getPreciseTimestamp(3))
|
||||
->pluck('id');
|
||||
$businessOrderIds = $businessOrderIds->chunk(200);
|
||||
$fields = implode(',', [
|
||||
'external_sku_id',
|
||||
'SUM(already_cancel_number) as total_already_cancel_number',
|
||||
'SUM(goods_number) as total_goods_number',
|
||||
]);
|
||||
|
||||
$data = [];
|
||||
foreach ($businessOrderIds as $ids) {
|
||||
$businessOrderItems = BusinessOrderItem::query()
|
||||
->select(DB::raw($fields))
|
||||
->whereIn('business_order_id', $ids)
|
||||
->where('external_sku_id', '<>', '')
|
||||
->groupBy('external_sku_id')
|
||||
->get();
|
||||
if ($businessOrderItems->isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
foreach ($businessOrderItems as $businessOrderItem) {
|
||||
$arr = explode('_', $businessOrderItem['external_sku_id']);
|
||||
if (2 !== count($arr)) {
|
||||
continue;
|
||||
}
|
||||
if (!isset($data[$businessOrderItem['external_sku_id']])) {
|
||||
$data[$businessOrderItem['external_sku_id']] = [
|
||||
'total_already_cancel_number' => 0,
|
||||
'total_goods_number' => 0,
|
||||
];
|
||||
}
|
||||
$data[$businessOrderItem['external_sku_id']]['total_already_cancel_number'] += $businessOrderItem['total_already_cancel_number'];
|
||||
$data[$businessOrderItem['external_sku_id']]['total_goods_number'] += $businessOrderItem['total_goods_number'];
|
||||
}
|
||||
}
|
||||
|
||||
$date = Carbon::parse($startTime)->format('Y-m-d');
|
||||
$goodsSkus = GoodsSku::query()
|
||||
->select(['goods_id', 'external_sku_id', 'name', 'id'])
|
||||
->with(['daily' => function ($query) use ($date) {
|
||||
$query->where('day', $date);
|
||||
}])
|
||||
->whereIn('external_sku_id', array_keys($data))
|
||||
->get();
|
||||
$goodsSkus = ArrayUtils::index($goodsSkus->toArray(), 'external_sku_id');
|
||||
foreach ($data as $externalSkuId => $datum) {
|
||||
if (!isset($goodsSkus[$externalSkuId])) {
|
||||
continue;
|
||||
}
|
||||
\App\Models\DailySalesReport::query()
|
||||
->updateOrCreate([
|
||||
'date' => $date,
|
||||
'external_sku_id' => $externalSkuId,
|
||||
], [
|
||||
'goods_id' => $goodsSkus[$externalSkuId]['goods_id'],
|
||||
'goods_sku_id' => $goodsSkus[$externalSkuId]['id'],
|
||||
'name' => $goodsSkus[$externalSkuId]['name'],
|
||||
'inventory' => $goodsSkus[$externalSkuId]['daily']['inventory'],
|
||||
'arrived_today_num' => $goodsSkus[$externalSkuId]['daily']['arrived_today_num'],
|
||||
'already_cancel_number' => $datum['total_already_cancel_number'],
|
||||
'loss_num' => $goodsSkus[$externalSkuId]['daily']['loss_num'],
|
||||
$s => $datum['total_goods_number'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
10
app/Models/DailySalesReport.php
Normal file
10
app/Models/DailySalesReport.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class DailySalesReport extends Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
}
|
||||
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateDailySalesReportsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
if (Schema::hasTable('daily_sales_reports')) {
|
||||
return;
|
||||
}
|
||||
Schema::create('daily_sales_reports', function (Blueprint $table) {
|
||||
$table->bigIncrements('id');
|
||||
$table->date('date');
|
||||
$table->unsignedInteger('goods_id');
|
||||
$table->unsignedInteger('goods_sku_id');
|
||||
$table->string('name');
|
||||
$table->string('external_sku_id', 64);
|
||||
$table->integer('inventory')->default(0)->comment('盘点数量');
|
||||
$table->integer('arrived_today_num')->default(0)->comment('今日到货');
|
||||
// $table->integer('sales_num')->default(0)->comment('销量');
|
||||
$table->integer('already_cancel_number')->default(0)->comment('已取消数量');
|
||||
$table->unsignedInteger('loss_num')->default(0)->comment('损耗');
|
||||
$table->decimal('goal_rate')->default(0)->comment('目标去化率');
|
||||
$table->integer('S1')->default(0)->comment('11-12');
|
||||
$table->integer('S2')->default(0)->comment('11-13:30');
|
||||
$table->integer('S3')->default(0)->comment('11-15');
|
||||
$table->integer('S4')->default(0)->comment('15-16');
|
||||
$table->integer('S5')->default(0)->comment('11-17:30');
|
||||
$table->integer('S6')->default(0)->comment('11-20');
|
||||
$table->integer('S7')->default(0)->comment('11-9:30');
|
||||
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('daily_sales_reports');
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user