采购金额
This commit is contained in:
@@ -670,6 +670,8 @@ class PurchaseOrderController extends BaseController
|
||||
|
||||
/**
|
||||
* 商品行修改:品名/供应商/包规/单位/成本(同步更新商品档案;商品已删除则跳过档案同步)
|
||||
* 成本价变化时,按各门店客户等级的上浮比例重算明细单价与金额(无等级按成本价兜底),
|
||||
* 并级联重算涉及订单与采购单汇总
|
||||
* @throws Throwable
|
||||
*/
|
||||
#[PutRoute(route: '/{id}/row/{productId}', authorize: 'update', where: ['id' => '[0-9]+', 'productId' => '[0-9]+'])]
|
||||
@@ -692,8 +694,34 @@ class PurchaseOrderController extends BaseController
|
||||
if ($items->isEmpty()) {
|
||||
throw new RepositoryException('该采购单下无此商品的订货明细');
|
||||
}
|
||||
|
||||
// 成本价是否变化(未变化时保留明细单价,避免覆盖单元格/单品的手工改价)
|
||||
$newCost = bcadd((string) $validated['cost_price'], '0', 2);
|
||||
$costChanged = bccomp($newCost, bcadd((string) $items->first()->cost_price, '0', 2), 2) !== 0;
|
||||
|
||||
// 成本变化 → 按门店等级上浮比例重算单价(一次性取回涉及门店的等级,避免逐行查询)
|
||||
$levelPercents = [];
|
||||
if ($costChanged) {
|
||||
$levelPercents = StoreModel::withTrashed()
|
||||
->with('level:id,percent')
|
||||
->whereIn('id', $items->pluck('store_id')->unique())
|
||||
->get(['id', 'level_id'])
|
||||
->mapWithKeys(static fn (StoreModel $store) => [
|
||||
$store->id => (float) ($store->level?->percent ?? 0),
|
||||
])
|
||||
->all();
|
||||
}
|
||||
|
||||
foreach ($items as $item) {
|
||||
$item->fill($validated)->save();
|
||||
$item->fill($validated);
|
||||
if ($costChanged) {
|
||||
$item->price = CustomerLevelModel::calcLevelPrice(
|
||||
$newCost,
|
||||
$levelPercents[(int) $item->store_id] ?? 0,
|
||||
);
|
||||
$item->amount = bcmul($item->price, (string) $item->quantity, 2);
|
||||
}
|
||||
$item->save();
|
||||
}
|
||||
|
||||
// 同步保存到商品档案(软删除商品跳过,明细照常更新)
|
||||
@@ -709,14 +737,20 @@ class PurchaseOrderController extends BaseController
|
||||
]);
|
||||
}
|
||||
|
||||
app(PurchaseItemService::class)->recalcPurchase($purchase->id);
|
||||
$service = app(PurchaseItemService::class);
|
||||
// 成本变化会引起明细金额变动,涉及订单需逐一重算
|
||||
if ($costChanged) {
|
||||
foreach ($items->pluck('order_id')->unique() as $orderId) {
|
||||
$service->recalcOrder((int) $orderId);
|
||||
}
|
||||
}
|
||||
$service->recalcPurchase($purchase->id);
|
||||
|
||||
return $this->success(
|
||||
['count' => $items->count()],
|
||||
$productSynced
|
||||
? '已同步 ' . $items->count() . ' 条订货明细与商品档案'
|
||||
: '已同步 ' . $items->count() . ' 条订货明细;商品已删除,档案未同步'
|
||||
);
|
||||
$message = '已同步 ' . $items->count() . ' 条订货明细'
|
||||
. ($productSynced ? '与商品档案' : ';商品已删除,档案未同步')
|
||||
. ($costChanged ? ';门店单价已按等级上浮比例重算' : '');
|
||||
|
||||
return $this->success(['count' => $items->count()], $message);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user