采购单优化

This commit is contained in:
liu
2026-08-17 18:13:33 +08:00
parent a06c45dc49
commit 3dbb9205a4
16 changed files with 2215 additions and 437 deletions
+180 -103
View File
@@ -12,8 +12,9 @@ use App\Models\SupplierModel;
use App\Models\UserModel;
/**
* C4 采购单数据修改:详情矩阵(商品行 × 门店列)、门店单元格下钻编辑/同步、行级成本,
* 修改同步订货明细并重算订单/采购单汇总(金额口径:数量(包) × 每包价格,单价/包规不参与金额计算)
* C4 采购单数据修改:详情矩阵(商品行 × 门店列)、门店单元格下钻编辑、行级修改同步商品档案、
* 门店购买详情单品增删改,修改后重算订单/采购单汇总
* (金额口径:订单=数量×单价;采购单预估成本=Σ数量×每包成本价)
*/
class PurchaseEditTest extends ProcurementTestCase
{
@@ -69,6 +70,7 @@ class PurchaseEditTest extends ProcurementTestCase
$this->assertSame('10斤/箱', $row['product_spec']);
$this->assertSame('斤', $row['unit']);
$this->assertSame('10.00', (string) $row['cost_price']);
$this->assertSame('50.00', (string) $row['amount'], '订货金额 = Σ明细 amount5 × 10.00');
$this->assertSame(5, $row['quantity'], '2+3');
$cells = $row['cells'];
@@ -76,16 +78,15 @@ class PurchaseEditTest extends ProcurementTestCase
$this->assertSame(3, $cells[$stores[1]->id]);
}
/** 门店单元格数量修改 → 明细金额重算(数量×单价),订单与采购单汇总同步 */
/** 门店单元格数量/单价修改 → 明细金额重算(数量×单价),订单与采购单汇总同步 */
public function test_update_cell_syncs_order_item_and_totals(): void
{
[$purchase, , $stores] = $this->buildPurchase();
$this->actingAsSysUser();
$item = StoreOrderItemModel::where('store_id', $stores[0]->id)->first();
$this->putJson("/purchase/order/cell/{$item->id}", ['quantity' => 5])
->assertJsonPath('success', true)
->assertJsonPath('data.amount', 50);
$this->putJson("/purchase/order/cell/{$item->id}", ['quantity' => 5, 'price' => 10])
->assertJsonPath('success', true);
$item = $item->fresh();
$this->assertSame(5, $item->quantity);
@@ -93,45 +94,59 @@ class PurchaseEditTest extends ProcurementTestCase
$order = StoreOrderModel::find($item->order_id);
$this->assertSame(5, $order->total_quantity);
$this->assertSame('50.00', (string) $order->product_amount);
$this->assertSame('50.00', (string) $order->total_amount);
$purchase = $purchase->fresh();
$this->assertSame('8.00', (string) $purchase->total_quantity, '5+3');
$this->assertSame('80.00', (string) $purchase->estimate_amount, '50+30');
$this->assertSame('80.00', (string) $purchase->actual_amount, '8 包 × 每包成本 10.00');
$this->assertSame('80.00', (string) $purchase->estimate_amount, '8 包 × 每包成本 10.00');
}
/** 行级成本修改 → 同步该商品全部订货明细,采购单实际金额按 数量×每包成本 重算 */
/** 行级成本修改 → 同步该商品全部订货明细与商品档案,采购单预估成本重算 */
public function test_update_row_cost_syncs_all_order_items(): void
{
[$purchase, $product] = $this->buildPurchase();
$this->actingAsSysUser();
$this->putJson("/purchase/order/{$purchase->id}/row/{$product->id}", ['cost_price' => 30])
->assertJsonPath('success', true)
$this->putJson("/purchase/order/{$purchase->id}/row/{$product->id}", [
'product_name' => $product->name,
'supplier_id' => SupplierModel::factory()->create()->id,
'product_spec' => $product->spec,
'unit' => $product->unit,
'cost_price' => 30,
])->assertJsonPath('success', true)
->assertJsonPath('data.count', 2);
$this->assertSame(2, StoreOrderItemModel::where('cost_price', '30.00')->count());
$this->assertSame('30.00', (string) $product->fresh()->cost_price, '商品档案成本价应同步更新');
$purchase = $purchase->fresh();
$this->assertSame('150.00', (string) $purchase->actual_amount, '5 包 × 每包成本 30.00');
$this->assertSame('50.00', (string) $purchase->estimate_amount, '订货金额不受成本修改影响');
$this->assertSame('150.00', (string) $purchase->estimate_amount, '5 包 × 每包成本 30.00');
}
/** 行级属性修改:品名/供应商/包规/单位/成本一键同步该商品全部订货明细(重量为手动录入,不受行修改影响 */
/** 行级属性修改不影响称重(重量为手动录入,仅作参考 */
public function test_update_row_does_not_touch_manual_weight(): void
{
[$purchase, $product] = $this->buildPurchase();
[$purchase, $product, $stores] = $this->buildPurchase();
$supplier = SupplierModel::factory()->create();
$this->actingAsSysUser();
$this->putJson("/purchase/order/{$purchase->id}/row/{$product->id}", ['cost_price' => 30])
// 先录入一笔手动称重
$item = StoreOrderItemModel::where('store_id', $stores[0]->id)->first();
$this->putJson("/purchase/order/cell/{$item->id}", ['quantity' => 2, 'price' => 10, 'weight' => 4.5])
->assertJsonPath('success', true);
$this->assertSame(0, StoreOrderItemModel::where('weight', '<>', 0)->count(), '重量保持手动录入值,不自动计算');
$this->putJson("/purchase/order/{$purchase->id}/row/{$product->id}", [
'product_name' => $product->name,
'supplier_id' => $supplier->id,
'product_spec' => $product->spec,
'unit' => $product->unit,
'cost_price' => 30,
])->assertJsonPath('success', true);
$this->assertSame('4.500', (string) $item->fresh()->weight, '重量保持手动录入值,不受行修改影响');
}
/** 行级属性修改:品名/供应商/包规/单位/成本一键同步该商品全部订货明细 */
/** 行级属性修改:品名/供应商/包规/单位/成本一键同步该商品全部订货明细与商品档案 */
public function test_update_row_syncs_product_attributes(): void
{
[$purchase, $product] = $this->buildPurchase();
@@ -157,29 +172,7 @@ class PurchaseEditTest extends ProcurementTestCase
$this->assertSame('25.00', (string) $item->cost_price);
}
// 未传 sync_product:商品档案保持原值
$product = $product->fresh();
$this->assertNotSame('优选土豆', $product->name);
$this->assertSame('125.00', (string) $purchase->fresh()->actual_amount, '5 包 × 每包成本 25.00');
}
/** sync_product=true:订货明细与商品档案同步更新;软删除商品拒绝 */
public function test_update_row_syncs_to_product_catalog(): void
{
[$purchase, $product] = $this->buildPurchase();
$supplier = SupplierModel::factory()->create();
$this->actingAsSysUser();
$this->putJson("/purchase/order/{$purchase->id}/row/{$product->id}", [
'product_name' => '优选土豆',
'supplier_id' => $supplier->id,
'product_spec' => '5斤/袋',
'unit' => '袋',
'cost_price' => 25,
'sync_product' => true,
])->assertJsonPath('success', true);
// 商品档案同步保存(商品列表可见)
$product = $product->fresh();
$this->assertSame('优选土豆', $product->name);
$this->assertSame($supplier->id, $product->supplier_id);
@@ -187,16 +180,33 @@ class PurchaseEditTest extends ProcurementTestCase
$this->assertSame('袋', $product->unit);
$this->assertSame('25.00', (string) $product->cost_price);
// 软删除商品 → 拒绝并回滚(订货明细不同步)
$product->delete();
$this->putJson("/purchase/order/{$purchase->id}/row/{$product->id}", [
'product_name' => '再次改名',
'sync_product' => true,
])->assertJsonPath('success', false);
$this->assertSame('优选土豆', StoreOrderItemModel::first()->product_name, '回滚后明细保持原值');
$this->assertSame('125.00', (string) $purchase->fresh()->estimate_amount, '5 包 × 每包成本 25.00');
}
/** 行级修改:属性与称重至少一项 */
/** 行级修改:商品已软删除时明细照常更新、跳过档案同步 */
public function test_update_row_skips_catalog_sync_when_product_deleted(): void
{
[$purchase, $product] = $this->buildPurchase();
$supplier = SupplierModel::factory()->create();
$this->actingAsSysUser();
$product->delete(); // 软删除
$this->putJson("/purchase/order/{$purchase->id}/row/{$product->id}", [
'product_name' => '已删商品新名',
'supplier_id' => $supplier->id,
'product_spec' => '5斤/袋',
'unit' => '袋',
'cost_price' => 25,
])->assertOk()
->assertJsonPath('success', true)
->assertJsonPath('msg', '已同步 2 条订货明细;商品已删除,档案未同步');
$this->assertSame('已删商品新名', StoreOrderItemModel::first()->product_name, '订货明细照常更新');
$this->assertNotSame('已删商品新名', $product->fresh()->name, '商品档案不同步');
}
/** 行级修改:必填字段校验(空 payload 拒绝) */
public function test_update_row_requires_at_least_one_field(): void
{
[$purchase, $product] = $this->buildPurchase();
@@ -213,11 +223,11 @@ class PurchaseEditTest extends ProcurementTestCase
$this->actingAsSysUser();
$item = StoreOrderItemModel::where('store_id', $stores[0]->id)->first();
$this->putJson("/purchase/order/cell/{$item->id}", ['quantity' => -1])
$this->putJson("/purchase/order/cell/{$item->id}", ['quantity' => -1, 'price' => 10])
->assertJsonPath('success', false);
}
/** 单元格下钻:返回该门店该商品的全部订货明细(订单号/状态/快照/可编辑标记),其他门店无明细 */
/** 单元格下钻:返回该门店该商品的全部订货明细(订单号/状态/快照),其他门店无明细 */
public function test_cell_detail_returns_items_with_order_info(): void
{
[$purchase, $product, $stores] = $this->buildPurchase();
@@ -237,7 +247,6 @@ class PurchaseEditTest extends ProcurementTestCase
$this->assertSame(2, $item['quantity']);
$this->assertSame('20.00', (string) $item['amount']);
$this->assertSame(StoreOrderModel::STATUS_DELIVERING, $item['order_status'], '生成采购单后源订单为采购中');
$this->assertTrue($item['editable']);
// 其他门店 → 另一条明细
$this->getJson("/purchase/order/{$purchase->id}/cell?product_id={$product->id}&store_id={$stores[1]->id}")
@@ -262,9 +271,8 @@ class PurchaseEditTest extends ProcurementTestCase
$this->actingAsSysUser();
$item = StoreOrderItemModel::where('store_id', $stores[0]->id)->first();
$this->putJson("/purchase/order/cell/{$item->id}", ['quantity' => 5, 'weight' => 4.5])
->assertJsonPath('success', true)
->assertJsonPath('data.amount', 50);
$this->putJson("/purchase/order/cell/{$item->id}", ['quantity' => 5, 'price' => 10, 'weight' => 4.5])
->assertJsonPath('success', true);
$item = $item->fresh();
$this->assertSame('4.500', (string) $item->weight);
@@ -272,60 +280,120 @@ class PurchaseEditTest extends ProcurementTestCase
$order = StoreOrderModel::find($item->order_id);
$this->assertSame('4.500', (string) $order->total_weight);
$this->assertSame('50.00', (string) $order->product_amount);
$this->assertSame('50.00', (string) $order->total_amount);
$purchase = $purchase->fresh();
$this->assertSame('4.500', (string) $purchase->total_weight);
$this->assertSame('80.00', (string) $purchase->estimate_amount, '50+30 订货金额');
$this->assertSame('80.00', (string) $purchase->actual_amount, '8 包 × 每包成本 10.00(称重仅参考,不参与金额)');
$this->assertSame('80.00', (string) $purchase->estimate_amount, '8 包 × 每包成本 10.00(称重仅参考,不参与金额');
}
/** 单元格一键同步:按商品ID同步档案 + 等级上浮价重算,订货单与采购单汇总级联 */
public function test_cell_sync_refreshes_snapshot_and_cascades(): void
/** 门店购买详情-新增单品:挂靠最新一笔订单,汇总级联重算;重复添加拒绝 */
public function test_store_item_add_creates_item_and_recalculates(): void
{
// 等级上浮 30%:下单时成本 10.00 → 售价 13.00
$level = CustomerLevelModel::factory()->create(['percent' => 30]);
$store = StoreModel::factory()->create(['level_id' => $level->id]);
$product = ProductModel::factory()->create([
'status' => ProductModel::STATUS_ON,
'cost_price' => '10.00',
'spec' => '1斤/袋',
'unit' => '斤',
]);
[$purchase, , $stores] = $this->buildPurchase();
$extra = ProductModel::factory()->create(['status' => ProductModel::STATUS_ON, 'cost_price' => 8]);
$this->actingAsSysUser();
$this->actingAsMiniUser(UserModel::factory()->forStore($store->id)->create());
$this->postJson('/mini/order', ['items' => [['product_id' => $product->id, 'quantity' => 2]]])
->assertJsonPath('success', true);
$this->postJson("/purchase/order/{$purchase->id}/store/{$stores[0]->id}/item", [
'product_id' => $extra->id,
'quantity' => 4,
'weight' => 1.5,
])->assertOk()->assertJsonPath('success', true);
$item = StoreOrderItemModel::where('product_id', $extra->id)->first();
$this->assertNotNull($item);
$this->assertSame($stores[0]->id, $item->store_id);
$this->assertSame($purchase->id, $item->purchase_id);
$this->assertSame('8.00', (string) $item->price, '等级上浮 0% → 单价=成本价');
$this->assertSame('32.00', (string) $item->amount, '8.00 × 4');
$this->assertSame('1.500', (string) $item->weight);
$order = StoreOrderModel::find($item->order_id);
$this->assertSame(6, $order->total_quantity, '2+4');
$this->assertSame('52.00', (string) $order->total_amount, '20+32');
$purchase = $purchase->fresh();
$this->assertSame('9.00', (string) $purchase->total_quantity, '2+3+4');
$this->assertSame('82.00', (string) $purchase->estimate_amount, '50+324 包 × 成本 8.00');
$this->assertSame('1.500', (string) $purchase->total_weight);
// 重复添加同一商品 → 拒绝
$this->postJson("/purchase/order/{$purchase->id}/store/{$stores[0]->id}/item", [
'product_id' => $extra->id,
'quantity' => 1,
])->assertJsonPath('success', false)
->assertJsonPath('msg', '该商品已在此门店采购明细中,请直接修改数量');
}
/** 门店购买详情-修改单品:同商品多笔订单明细合并到最早一条,涉及订单逐一重算 */
public function test_store_item_update_merges_multi_order_rows(): void
{
$level = CustomerLevelModel::factory()->create();
$store = StoreModel::factory()->create(['level_id' => $level->id]);
$product = ProductModel::factory()->create(['status' => ProductModel::STATUS_ON, 'cost_price' => 10]);
// 同一门店两笔订单各订 2 件(同商品)→ 同一采购单
$user = UserModel::factory()->forStore($store->id)->create();
$this->actingAsMiniUser($user);
foreach ([2, 2] as $qty) {
$this->postJson('/mini/order', ['items' => [['product_id' => $product->id, 'quantity' => $qty]]])
->assertJsonPath('success', true);
}
StoreOrderModel::query()->update(['status' => StoreOrderModel::STATUS_SUMMARIZED]);
$this->actingAsSysUser();
$this->postJson('/purchase/order/generate', ['purchase_date' => now()->toDateString()])
->assertJsonPath('success', true);
$purchase = PurchaseOrderModel::first();
$this->assertSame(2, StoreOrderItemModel::count(), '两笔订单各一条明细');
$this->putJson("/purchase/order/{$purchase->id}/store/{$store->id}/item/{$product->id}", [
'quantity' => 10,
'price' => 12,
'weight' => 5,
])->assertJsonPath('success', true);
$this->assertSame(1, StoreOrderItemModel::count(), '多笔明细合并为一条');
$item = StoreOrderItemModel::first();
$this->assertSame('13.00', (string) $item->price, '下单时 10 元上浮 30%');
$this->assertSame('26.00', (string) $purchase->estimate_amount);
$this->assertSame(10, $item->quantity);
$this->assertSame('12.00', (string) $item->price);
$this->assertSame('120.00', (string) $item->amount);
$this->assertSame('5.000', (string) $item->weight);
// 成本上调后同步:单价按最新成本重算,金额与两级汇总级联
$product->update(['cost_price' => '20.00']);
$this->putJson("/purchase/order/cell/{$item->id}/sync")
->assertJsonPath('success', true)
->assertJsonPath('data.amount', 52);
$item->refresh();
$this->assertSame('20.00', (string) $item->cost_price);
$this->assertSame('26.00', (string) $item->price, '20 元上浮 30% = 26.00');
$this->assertSame('52.00', (string) $item->amount, '26.00 × 2');
$order = StoreOrderModel::first();
$this->assertSame('52.00', (string) $order->product_amount);
$this->assertSame('52.00', (string) $order->total_amount);
// 保留行所在订单重算;被合并订单清零
$survivedOrder = StoreOrderModel::find($item->order_id);
$this->assertSame(10, $survivedOrder->total_quantity);
$this->assertSame('120.00', (string) $survivedOrder->total_amount);
$mergedOrder = StoreOrderModel::where('id', '<>', $item->order_id)->first();
$this->assertSame(0, $mergedOrder->total_quantity);
$this->assertSame('0.00', (string) $mergedOrder->total_amount);
$purchase = $purchase->fresh();
$this->assertSame('52.00', (string) $purchase->estimate_amount);
$this->assertSame('40.00', (string) $purchase->actual_amount, '2× 每包成本 20.00');
$this->assertSame('10.00', (string) $purchase->total_quantity);
$this->assertSame('100.00', (string) $purchase->estimate_amount, '10× 成本 10.00');
$this->assertSame('5.000', (string) $purchase->total_weight);
}
/** 门店购买详情-移除单品:明细删除,订单与采购单汇总级联重算 */
public function test_store_item_remove_deletes_and_recalculates(): void
{
[$purchase, $product, $stores] = $this->buildPurchase();
$this->actingAsSysUser();
$this->deleteJson("/purchase/order/{$purchase->id}/store/{$stores[0]->id}/item/{$product->id}")
->assertOk()->assertJsonPath('success', true);
$this->assertSame(0, StoreOrderItemModel::where('store_id', $stores[0]->id)->count());
$this->assertSame(1, StoreOrderItemModel::count(), '门店B 明细不受影响');
$order = StoreOrderModel::where('store_id', $stores[0]->id)->first();
$this->assertSame(0, $order->total_quantity);
$this->assertSame('0.00', (string) $order->total_amount);
$purchase = $purchase->fresh();
$this->assertSame('3.00', (string) $purchase->total_quantity, '仅剩门店B 3 件');
$this->assertSame('30.00', (string) $purchase->estimate_amount);
}
/** 门店购买详情:按商品聚合该门店采购汇总(数量/包规/单位/单价/预计金额),仅含该门店明细 */
@@ -408,28 +476,37 @@ class PurchaseEditTest extends ProcurementTestCase
->assertJsonPath('msg', '采购单不存在');
}
/** 采购单已完成:单元格编辑/同步、行修改均拒绝,明细不变 */
/** 采购单已完成:单元格编辑、行修改、门店单品增删改均拒绝,明细不变 */
public function test_edits_rejected_when_purchase_completed(): void
{
[$purchase, $product, $stores] = $this->buildPurchase();
$item = StoreOrderItemModel::where('store_id', $stores[0]->id)->first();
$supplier = SupplierModel::factory()->create();
$purchase->update(['status' => PurchaseOrderModel::STATUS_COMPLETED]);
$this->actingAsSysUser();
$this->putJson("/purchase/order/cell/{$item->id}", ['quantity' => 5])
$this->putJson("/purchase/order/cell/{$item->id}", ['quantity' => 5, 'price' => 10])
->assertJsonPath('success', false)
->assertJsonPath('msg', '采购单已完成,不允许修改明细');
$this->putJson("/purchase/order/cell/{$item->id}/sync")
->assertJsonPath('success', false);
$this->putJson("/purchase/order/{$purchase->id}/row/{$product->id}", ['cost_price' => 30])
$this->putJson("/purchase/order/{$purchase->id}/row/{$product->id}", [
'product_name' => '改名',
'supplier_id' => $supplier->id,
'product_spec' => '5斤/袋',
'unit' => '袋',
'cost_price' => 30,
])->assertJsonPath('success', false)
->assertJsonPath('msg', '采购单已完成,不允许修改明细');
$this->postJson("/purchase/order/{$purchase->id}/store/{$stores[0]->id}/item", [
'product_id' => $product->id,
'quantity' => 1,
])->assertJsonPath('success', false);
$this->putJson("/purchase/order/{$purchase->id}/store/{$stores[0]->id}/item/{$product->id}", [
'quantity' => 5,
])->assertJsonPath('success', false);
$this->deleteJson("/purchase/order/{$purchase->id}/store/{$stores[0]->id}/item/{$product->id}")
->assertJsonPath('success', false);
$this->assertSame(2, $item->fresh()->quantity, '被拒绝后明细不变');
$this->assertSame('10.00', (string) $item->fresh()->cost_price);
// 下钻明细同步标记不可编辑
$this->getJson("/purchase/order/{$purchase->id}/cell?product_id={$product->id}&store_id={$stores[0]->id}")
->assertOk()
->assertJsonPath('data.items.0.editable', false);
}
}