feat: #20220804 7点库存盘点

This commit is contained in:
赵世界 2022-08-04 13:21:45 +08:00
parent c45c004ad8
commit 9553c73c42
5 changed files with 83 additions and 1 deletions

View File

@ -0,0 +1,79 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Models\DailyStockRecord;
use App\Models\GoodsSku;
use App\Models\Log;
class Inventory extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'timing:inventory';
/**
* The console command description.
*
* @var string
*/
protected $description = '定时盘点';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
DB::beginTransaction();
try {
// 数据库存储过程,7点定时执行
$skus = GoodsSku::query()->get(['id', 'stock', 'two_days_ago_num', 'yesterday_num']);
$data = [];
$date = date('Y-m-d');
foreach ($skus as $sku) {
$data[] = [
'sku_id' => $sku->id,
'day' => $date,
];
GoodsSku::where('id', $sku->id)->update([
'stock' => $sku->stock + $sku->two_days_ago_num + $sku->yesterday_num,
'yesterday_num' => $sku->stock,
'two_days_ago_num' => $sku->two_days_ago_num + $sku->yesterday_num,
]);
}
$log = new Log();
$log->module = 'goods';
$log->action = 'PATCH';
$log->target_type = 'goods_sku';
$log->target_id = 0;
$log->target_field = 'timingInventory';
$log->user_id = 1;
$record = new DailyStockRecord();
$record->batchInsert($data);
$log->message = '成功';
DB::commit();
} catch (\Exception $exception) {
$log->message = '失败';
DB::rollBack();
}
$log->save();
$this->info($log->message);
}
}

View File

@ -36,6 +36,7 @@ class GoodsSkusController extends Controller
$goods = Goods::query()->filter()->get()->toArray();
$goodsIds = array_column($goods, 'id');
// 状态变更时间查询,日志
$day = FormatUtils::date();
$goodsSkus = GoodsSku::query()
->whereIn('goods_id', $goodsIds)

View File

@ -87,7 +87,7 @@ class Log extends Model
public function setUserIdAttribute($value)
{
$this->attributes['user_id'] = Auth::id();
$this->attributes['user_id'] = Auth::id() ?: $value;
}
public function add($targetId = 0, $targetField = '')

View File

@ -20,6 +20,7 @@ class CreateDailyStockRecordsTable extends Migration
$table->unsignedInteger('arrived_today_num')->default(0)->comment('今日到货');
$table->unsignedInteger('loss_num')->default(0)->comment('损耗');
$table->unsignedInteger('inventory')->default(0)->comment('库存盘点');
$table->unique('sku_id', 'day');
$table->timestamps();
});
}

View File

@ -24,6 +24,7 @@ class CreateLogsTable extends Migration
$table->text('after_update')->nullable()->comment('更新后数据');
$table->text('message')->nullable()->comment('备注信息');
$table->bigInteger('user_id')->comment('操作人id');
$table->index('target_type', 'target_id', 'target_field');
$table->timestamps();
});
}