mirror of
https://gitee.com/hzchunfen/erp.git
synced 2025-12-01 06:30:49 +00:00
feat: #10000 库存盘点修改
This commit is contained in:
parent
f16224e2ab
commit
d37635f2ab
@ -7,9 +7,11 @@ use App\Exports\GoodsSkusExport;
|
|||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Http\Requests\GoodsRequest;
|
use App\Http\Requests\GoodsRequest;
|
||||||
use App\Http\Requests\GoodsSkuRequest;
|
use App\Http\Requests\GoodsSkuRequest;
|
||||||
|
use App\Models\BusinessOrderItem;
|
||||||
use App\Models\Goods;
|
use App\Models\Goods;
|
||||||
use App\Models\Log;
|
use App\Models\Log;
|
||||||
use App\Models\Log as LogModel;
|
use App\Models\Log as LogModel;
|
||||||
|
use App\Utils\ArrayUtils;
|
||||||
use App\Utils\DateTimeUtils;
|
use App\Utils\DateTimeUtils;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use App\Models\GoodsSku;
|
use App\Models\GoodsSku;
|
||||||
@ -61,6 +63,23 @@ class GoodsSkusController extends Controller
|
|||||||
}])
|
}])
|
||||||
->orderBy('updated_at', 'desc')
|
->orderBy('updated_at', 'desc')
|
||||||
->paginate($request->get('per_page'));
|
->paginate($request->get('per_page'));
|
||||||
|
foreach ($goodsSkus as &$sku) {
|
||||||
|
$externalSkuId = $sku['goods']['goods_code'] . '_' . $sku['sku_code'];
|
||||||
|
$lastInventoryTime = $sku['daily']['inventory_time'];
|
||||||
|
$addOrderGoodsNum = BusinessOrderItem::query()
|
||||||
|
->where('external_sku_id', $externalSkuId)
|
||||||
|
->when($lastInventoryTime, function ($query) use ($lastInventoryTime) {
|
||||||
|
$query->where('updated_at', '>', $lastInventoryTime);
|
||||||
|
})
|
||||||
|
->sum('goods_number');
|
||||||
|
$reduceOrderGoodsNum = BusinessOrderItem::query()
|
||||||
|
->where('external_sku_id', $externalSkuId)
|
||||||
|
->when($lastInventoryTime, function ($query) use ($lastInventoryTime) {
|
||||||
|
$query->where('updated_at', '>', $lastInventoryTime);
|
||||||
|
})
|
||||||
|
->sum('already_cancel_number');
|
||||||
|
$sku['order_goods_num'] = $addOrderGoodsNum - $reduceOrderGoodsNum;
|
||||||
|
}
|
||||||
|
|
||||||
return GoodsSkuResource::collection($goodsSkus);
|
return GoodsSkuResource::collection($goodsSkus);
|
||||||
}
|
}
|
||||||
@ -204,21 +223,61 @@ class GoodsSkusController extends Controller
|
|||||||
DB::beginTransaction();
|
DB::beginTransaction();
|
||||||
try {
|
try {
|
||||||
$logs = [];
|
$logs = [];
|
||||||
foreach ($request->skus as $sku) {
|
$requestSkus = ArrayUtils::index($request->skus, 'id');
|
||||||
|
$skus = GoodsSku::query()
|
||||||
|
->whereIn('id', array_keys($requestSkus))
|
||||||
|
->with(['goods:id,goods_code'])
|
||||||
|
->get(['id', 'goods_id', 'sku_code'])
|
||||||
|
->toArray();
|
||||||
|
$today = DateTimeUtils::getToday();
|
||||||
|
$nextDay = DateTimeUtils::getNextDay();
|
||||||
|
$dateTime = date('Y-m-d H:i:s');
|
||||||
|
foreach ($skus as $sku) {
|
||||||
|
if (!isset($requestSkus[$sku['id']])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// 更新今天
|
||||||
|
$record = DailyStockRecord::query()->where('sku_id', $sku['id'])->where('day', $today)->first();
|
||||||
|
// 日志记录
|
||||||
$inventoryLog = [
|
$inventoryLog = [
|
||||||
'module' => 'goods',
|
'module' => 'goods',
|
||||||
'action' => $request->getMethod(),
|
'action' => $request->getMethod(),
|
||||||
'target_type' => 'goods_sku',
|
'target_type' => 'goods_sku',
|
||||||
'target_id' => $sku['id'],
|
'target_id' => $sku['id'],
|
||||||
'user_id' => $request->user()->id
|
'user_id' => $request->user()->id,
|
||||||
|
'target_field' => 'inventory',
|
||||||
|
'before_update' => $record->inventory,
|
||||||
|
'after_update' => $requestSkus[$sku['id']]['inventory']
|
||||||
];
|
];
|
||||||
$record = DailyStockRecord::query()->where('sku_id', $sku['id'])->where('day', DateTimeUtils::getToday())->first(['id', 'inventory']);
|
|
||||||
$inventoryLog['target_field'] = 'inventory';
|
|
||||||
$inventoryLog['before_update'] = $record->inventory;
|
|
||||||
$record->inventory = $sku['inventory'];
|
|
||||||
$record->save();
|
|
||||||
$inventoryLog['after_update'] = $record->inventory;
|
|
||||||
$logs[] = $inventoryLog;
|
$logs[] = $inventoryLog;
|
||||||
|
$externalSkuId = $sku['goods']['goods_code'] . '_' . $sku['sku_code'];
|
||||||
|
// 自上一次盘点过后有订单发生的数量(包含退单)
|
||||||
|
$lastInventoryTime = $record->inventory_time;
|
||||||
|
$addOrderGoodsNum = BusinessOrderItem::query()
|
||||||
|
->where('external_sku_id', $externalSkuId)
|
||||||
|
->when($lastInventoryTime, function ($query) use ($lastInventoryTime) {
|
||||||
|
$query->where('updated_at', '>', $lastInventoryTime);
|
||||||
|
})
|
||||||
|
->sum('goods_number');
|
||||||
|
$reduceOrderGoodsNum = BusinessOrderItem::query()
|
||||||
|
->where('external_sku_id', $externalSkuId)
|
||||||
|
->when($lastInventoryTime, function ($query) use ($lastInventoryTime) {
|
||||||
|
$query->where('updated_at', '>', $lastInventoryTime);
|
||||||
|
})
|
||||||
|
->sum('already_cancel_number');
|
||||||
|
$orderGoodsNum = $addOrderGoodsNum - $reduceOrderGoodsNum;
|
||||||
|
$record->inventory = $requestSkus[$sku['id']]['inventory'];
|
||||||
|
$record->inventory_time = $dateTime;
|
||||||
|
$record->order_goods_num += $orderGoodsNum;
|
||||||
|
$record->save();
|
||||||
|
// 更新明天
|
||||||
|
DailyStockRecord::updateOrCreate(
|
||||||
|
['sku_id' => $sku['id'], 'day' => $nextDay],
|
||||||
|
[
|
||||||
|
'inventory' => $record->inventory,
|
||||||
|
'inventory_time' => $dateTime,
|
||||||
|
]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
$log = new LogModel();
|
$log = new LogModel();
|
||||||
$log->batchInsert($logs);
|
$log->batchInsert($logs);
|
||||||
|
|||||||
@ -5,4 +5,6 @@ namespace App\Models;
|
|||||||
class DailyStockRecord extends Model
|
class DailyStockRecord extends Model
|
||||||
{
|
{
|
||||||
protected $hidden = ['created_at', 'updated_at'];
|
protected $hidden = ['created_at', 'updated_at'];
|
||||||
|
|
||||||
|
protected $guarded = [];
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,6 +18,17 @@ class DateTimeUtils
|
|||||||
return date('Y-m-d', $time);
|
return date('Y-m-d', $time);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function getNextDay()
|
||||||
|
{
|
||||||
|
$time = time();
|
||||||
|
$inventoryTime = strtotime(date('Y-m-d 07:00:00'));
|
||||||
|
if ($time > $inventoryTime) {
|
||||||
|
$time = strtotime('+1 day');
|
||||||
|
}
|
||||||
|
|
||||||
|
return date('Y-m-d', $time);
|
||||||
|
}
|
||||||
|
|
||||||
public static function getMicroTime($dateTime = null)
|
public static function getMicroTime($dateTime = null)
|
||||||
{
|
{
|
||||||
$time = microtime(true);
|
$time = microtime(true);
|
||||||
|
|||||||
@ -19,7 +19,7 @@ class CreateDailyStockRecordsTable extends Migration
|
|||||||
$table->date('day');
|
$table->date('day');
|
||||||
$table->unsignedInteger('arrived_today_num')->default(0)->comment('今日到货');
|
$table->unsignedInteger('arrived_today_num')->default(0)->comment('今日到货');
|
||||||
$table->unsignedInteger('loss_num')->default(0)->comment('损耗');
|
$table->unsignedInteger('loss_num')->default(0)->comment('损耗');
|
||||||
$table->unsignedInteger('inventory')->default(0)->comment('库存盘点');
|
$table->integer('inventory')->default(0)->comment('库存盘点');
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
// 索引
|
// 索引
|
||||||
$table->unique(['sku_id', 'day']);
|
$table->unique(['sku_id', 'day']);
|
||||||
|
|||||||
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class AddFieldsWithDailyStockRecordsTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table('daily_stock_records', function (Blueprint $table) {
|
||||||
|
$table->integer('order_goods_num')->default(0)->comment('截止盘点时间订单商品数量');
|
||||||
|
$table->timestamp('inventory_time')->nullable()->comment('盘点时间');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user