采购单优化
This commit is contained in:
@@ -9,6 +9,7 @@ use App\Models\PurchaseOrderModel;
|
||||
use App\Models\StoreModel;
|
||||
use App\Models\StoreOrderItemModel;
|
||||
use App\Models\StoreOrderModel;
|
||||
use App\Models\SupplierModel;
|
||||
use App\Models\UserModel;
|
||||
|
||||
/**
|
||||
@@ -51,7 +52,7 @@ class PurchaseEditTest extends ProcurementTestCase
|
||||
return [PurchaseOrderModel::first(), $product, [$storeA, $storeB]];
|
||||
}
|
||||
|
||||
/** 详情返回「商品行 × 门店列」矩阵:单价 = 成本/包规,单元格金额 = 数量×单价 */
|
||||
/** 详情返回「商品行 × 门店列」矩阵:cells 按门店聚合数量 */
|
||||
public function test_detail_returns_store_matrix(): void
|
||||
{
|
||||
[$purchase, $product, $stores] = $this->buildPurchase();
|
||||
@@ -69,16 +70,12 @@ class PurchaseEditTest extends ProcurementTestCase
|
||||
$this->assertSame($product->id, $row['product_id']);
|
||||
$this->assertSame('10斤/箱', $row['product_spec']);
|
||||
$this->assertSame('斤', $row['unit']);
|
||||
$this->assertEquals(20.0, $row['cost_price']);
|
||||
$this->assertEquals(2.0, $row['unit_cost'], '单价 = 20 ÷ 10');
|
||||
$this->assertEquals(5.0, $row['quantity'], '2+3');
|
||||
$this->assertEquals(10.0, $row['amount'], '5 × 2.00');
|
||||
$this->assertSame('20.00', (string) $row['cost_price']);
|
||||
$this->assertSame(5, $row['quantity'], '2+3');
|
||||
|
||||
$cells = collect($row['cells'])->keyBy('store_id');
|
||||
$this->assertSame(2, $cells[$stores[0]->id]['quantity']);
|
||||
$this->assertEquals(4.0, $cells[$stores[0]->id]['amount'], '2 × 2.00');
|
||||
$this->assertSame(3, $cells[$stores[1]->id]['quantity']);
|
||||
$this->assertEquals(6.0, $cells[$stores[1]->id]['amount'], '3 × 2.00');
|
||||
$cells = $row['cells'];
|
||||
$this->assertSame(2, $cells[$stores[0]->id]);
|
||||
$this->assertSame(3, $cells[$stores[1]->id]);
|
||||
}
|
||||
|
||||
/** 门店单元格数量修改 → 明细金额重算(数量×单价),订单与采购单汇总同步 */
|
||||
@@ -148,7 +145,73 @@ class PurchaseEditTest extends ProcurementTestCase
|
||||
$this->assertSame('20.00', (string) $purchase->actual_amount);
|
||||
}
|
||||
|
||||
/** 行级修改:成本与称重至少一项 */
|
||||
/** 行级属性修改:品名/供应商/包规/单位/成本一键同步该商品全部订货明细 */
|
||||
public function test_update_row_syncs_product_attributes(): 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,
|
||||
])->assertJsonPath('success', true)
|
||||
->assertJsonPath('data.count', 2);
|
||||
|
||||
$items = StoreOrderItemModel::all();
|
||||
$this->assertCount(2, $items);
|
||||
foreach ($items as $item) {
|
||||
$this->assertSame('优选土豆', $item->product_name);
|
||||
$this->assertSame($supplier->id, $item->supplier_id);
|
||||
$this->assertSame('5斤/袋', $item->product_spec);
|
||||
$this->assertSame('袋', $item->unit);
|
||||
$this->assertSame('25.00', (string) $item->cost_price);
|
||||
}
|
||||
|
||||
// 未传 sync_product:商品档案保持原值
|
||||
$product = $product->fresh();
|
||||
$this->assertNotSame('优选土豆', $product->name);
|
||||
|
||||
// 新单价 = 25 ÷ 5 = 5.00,实际金额 = 5 件 × 5.00
|
||||
$this->assertSame('25.00', (string) $purchase->fresh()->actual_amount);
|
||||
}
|
||||
|
||||
/** 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);
|
||||
$this->assertSame('5斤/袋', $product->spec);
|
||||
$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, '回滚后明细保持原值');
|
||||
}
|
||||
|
||||
/** 行级修改:属性与称重至少一项 */
|
||||
public function test_update_row_requires_at_least_one_field(): void
|
||||
{
|
||||
[$purchase, $product] = $this->buildPurchase();
|
||||
|
||||
Reference in New Issue
Block a user