默认重量

This commit is contained in:
liu
2026-09-05 14:37:22 +08:00
parent 364471a086
commit 12cbed411b
10 changed files with 205 additions and 8 deletions
+24 -2
View File
@@ -282,7 +282,7 @@ class PurchaseEditTest extends ProcurementTestCase
$this->assertSame('50.00', (string) $order->total_amount);
$purchase = $purchase->fresh();
$this->assertSame('4.500', (string) $purchase->total_weight);
$this->assertSame('7.500', (string) $purchase->total_weight, '4.500 手动称重 + B 店参考重量 3.000');
$this->assertSame('80.00', (string) $purchase->estimate_amount, '8 包 × 每包成本 10.00(称重仅参考,不参与金额)');
}
@@ -392,7 +392,7 @@ class PurchaseEditTest extends ProcurementTestCase
$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->assertSame('6.500', (string) $purchase->total_weight, '原有参考重量 2+3 斤 + 新增 1.500');
// 重复添加同一商品 → 拒绝
$this->postJson("/purchase/order/{$purchase->id}/store/{$stores[0]->id}/item", [
@@ -402,6 +402,28 @@ class PurchaseEditTest extends ProcurementTestCase
->assertJsonPath('msg', '该商品已在此门店采购明细中,请直接修改数量');
}
/** 门店购买详情-新增单品未传称重:按订货量 × 规格预填参考重量并级联汇总 */
public function test_store_item_add_defaults_reference_weight(): void
{
[$purchase, , $stores] = $this->buildPurchase();
$extra = ProductModel::factory()->create([
'status' => ProductModel::STATUS_ON,
'cost_price' => 8,
'spec' => '25斤/袋',
'unit' => '袋',
]);
$this->actingAsSysUser();
$this->postJson("/purchase/order/{$purchase->id}/store/{$stores[0]->id}/item", [
'product_id' => $extra->id,
'quantity' => 2,
])->assertOk()->assertJsonPath('success', true);
$item = StoreOrderItemModel::where('product_id', $extra->id)->first();
$this->assertSame('50.000', (string) $item->weight, '2 袋 × 25斤/袋');
$this->assertSame('55.000', (string) $purchase->fresh()->total_weight, '原有参考重量 5.000 + 新增 50.000');
}
/** 门店购买详情-修改单品:同商品多笔订单明细合并到最早一条,涉及订单逐一重算 */
public function test_store_item_update_merges_multi_order_rows(): void
{
+26
View File
@@ -139,4 +139,30 @@ class PurchaseGenerateTest extends ProcurementTestCase
$this->assertSame(1, PurchaseOrderModel::count(), '第二次生成应被拒绝,不产生新采购单');
}
/** 采购单总重量 = 订货明细参考重量合计(下单时已按订货量 × 规格预填) */
public function test_generate_sums_reference_weight(): void
{
$level = CustomerLevelModel::factory()->create();
$store = StoreModel::factory()->create(['level_id' => $level->id]);
$product = ProductModel::factory()->create([
'status' => ProductModel::STATUS_ON,
'cost_price' => '6.00',
'spec' => '10斤/箱',
'unit' => '箱',
]);
$this->actingAsMiniStore($store);
foreach ([3, 4] 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);
$this->assertSame('70.000', (string) PurchaseOrderModel::first()->total_weight, '(3+4) 箱 × 10斤/箱');
}
}
+34
View File
@@ -72,6 +72,40 @@ class StoreOrderTest extends ProcurementTestCase
$this->assertSame('10.00', (string) $order->total_amount, '应按等级价 5.00×2 计算,忽略前端金额');
}
/** 下单预填参考重量 = 订货量 × 规格折算(按重量计价的商品订货量即重量),订单总重量 = 明细合计 */
public function test_place_order_prefills_reference_weight(): void
{
$level = CustomerLevelModel::factory()->create();
$store = StoreModel::factory()->create(['level_id' => $level->id]);
$byBox = ProductModel::factory()->create([
'status' => ProductModel::STATUS_ON,
'cost_price' => '5.00',
'spec' => '10斤/箱',
'unit' => '箱',
]);
$byJin = ProductModel::factory()->create([
'status' => ProductModel::STATUS_ON,
'cost_price' => '5.00',
'spec' => '散装',
'unit' => '斤',
]);
$this->actingAsMiniStore($store);
$this->postJson('/mini/order', [
'items' => [
['product_id' => $byBox->id, 'quantity' => 3],
['product_id' => $byJin->id, 'quantity' => 5],
],
])->assertOk()->assertJsonPath('success', true);
$order = StoreOrderModel::where('store_id', $store->id)->first();
$boxItem = $order->items->firstWhere('product_id', $byBox->id);
$jinItem = $order->items->firstWhere('product_id', $byJin->id);
$this->assertSame('30.000', (string) $boxItem->weight, '3 箱 × 10斤/箱');
$this->assertSame('5.000', (string) $jinItem->weight, '按斤计价:订货量即重量');
$this->assertSame('35.000', (string) $order->total_weight, '订单总重量 = 明细参考重量合计');
}
/** 门店绑定的客户等级被删除时拒绝下单 */
public function test_store_level_missing_rejected(): void
{
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace Tests\Unit;
use App\Services\WeightEstimator;
use PHPUnit\Framework\TestCase;
/**
* 参考重量估算:重量单位直算、规格解析折算、无法解析兜底(单位:斤)
*/
class WeightEstimatorTest extends TestCase
{
/** 计价单位本身是重量单位:订货量即重量(忽略规格) */
public function test_weight_unit_quantity_is_weight(): void
{
$this->assertSame('5.000', WeightEstimator::estimate('10斤/箱', '斤', '5'));
$this->assertSame('6.000', WeightEstimator::estimate('', '公斤', '3'));
$this->assertSame('6.000', WeightEstimator::estimate('', 'KG', '3'));
$this->assertSame('1.000', WeightEstimator::estimate('', '克', '500'));
}
/** 规格解析每件重量 × 订货量 */
public function test_spec_per_unit_weight(): void
{
$this->assertSame('30.000', WeightEstimator::estimate('10斤/箱', '箱', '3'));
$this->assertSame('75.000', WeightEstimator::estimate('25斤/袋', '袋', '3'));
$this->assertSame('3.000', WeightEstimator::estimate('500g/袋', '袋', '3'));
$this->assertSame('3.000', WeightEstimator::estimate('500克/袋', '袋', '3'));
$this->assertSame('9.000', WeightEstimator::estimate('1.5kg/箱', '箱', '3'));
$this->assertSame('12.000', WeightEstimator::estimate('2公斤/筐', '筐', '3'));
$this->assertSame('15.000', WeightEstimator::estimate('10斤/箱', '箱', '1.5'), '订货量可为小数');
}
/** 规格无法解析:裸数字按斤计,无数字计 0 */
public function test_unparsable_spec_fallback(): void
{
$this->assertSame('20.000', WeightEstimator::estimate('10/箱', '箱', '2'), '裸数字按斤计');
$this->assertSame('0.000', WeightEstimator::estimate('散装', '箱', '3'));
$this->assertSame('0.000', WeightEstimator::estimate('', '箱', '3'));
}
}