周转箱
This commit is contained in:
@@ -8,6 +8,9 @@ use App\Models\ProductPriceModel;
|
||||
use App\Models\StoreModel;
|
||||
use App\Models\StoreOrderModel;
|
||||
use App\Models\UserModel;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Modules\SystemTool\Models\SysSiteConfigGroupModel;
|
||||
use Modules\SystemTool\Models\SysSiteConfigItemsModel;
|
||||
|
||||
/**
|
||||
* 小程序下单:等级价快照、服务端重算总价、取消限制、门店数据隔离
|
||||
@@ -129,4 +132,122 @@ class StoreOrderTest extends ProcurementTestCase
|
||||
$this->getJson("/mini/order/{$orderOfA->id}")->assertJsonPath('success', false);
|
||||
$this->getJson('/mini/order')->assertJsonPath('data.total', 0);
|
||||
}
|
||||
|
||||
/** 种子化周转框/托盘单价配置(框 2.00、托盘 10.00) */
|
||||
private function seedContainerConfig(): void
|
||||
{
|
||||
$group = SysSiteConfigGroupModel::create(['title' => '业务配置', 'key' => 'services']);
|
||||
foreach ([['box_amount', '周转框单价', '2.00'], ['tray_amount', '周转托盘单价', '10.00']] as [$key, $title, $value]) {
|
||||
SysSiteConfigItemsModel::create([
|
||||
'group_id' => $group->id,
|
||||
'key' => $key,
|
||||
'title' => $title,
|
||||
'type' => 'InputNumber',
|
||||
'values' => $value,
|
||||
'sort' => 0,
|
||||
]);
|
||||
}
|
||||
Cache::forget('site_config');
|
||||
}
|
||||
|
||||
/** 造一笔指定状态的订单(商品 5.00 × 2 = 10.00) */
|
||||
private function makeOrderWithStatus(int $status): StoreOrderModel
|
||||
{
|
||||
[$store, $product, $user] = $this->makeStoreWithProduct('5.00');
|
||||
$this->actingAsMiniUser($user);
|
||||
$this->postJson('/mini/order', ['items' => [['product_id' => $product->id, 'quantity' => 2]]])
|
||||
->assertJsonPath('success', true);
|
||||
$order = StoreOrderModel::where('store_id', $store->id)->first();
|
||||
$order->update(['status' => $status]);
|
||||
|
||||
return $order;
|
||||
}
|
||||
|
||||
/** 修改周转框/托盘:自动重算附加金额与订单总金额 */
|
||||
public function test_container_update_recalculates_amounts(): void
|
||||
{
|
||||
$this->seedContainerConfig();
|
||||
$order = $this->makeOrderWithStatus(StoreOrderModel::STATUS_SUMMARIZED);
|
||||
$this->assertSame('10.00', (string) $order->product_amount, '下单时应写入商品金额');
|
||||
|
||||
$this->actingAsSysUser();
|
||||
$this->putJson("/order/store/{$order->id}/container", ['box_num' => 3, 'tray_num' => 1])
|
||||
->assertOk()->assertJsonPath('success', true);
|
||||
|
||||
$order->refresh();
|
||||
$this->assertSame(3, $order->box_num);
|
||||
$this->assertSame(1, $order->tray_num);
|
||||
$this->assertSame('16.00', (string) $order->added_amount, '3×2.00 + 1×10.00');
|
||||
$this->assertSame('10.00', (string) $order->product_amount);
|
||||
$this->assertSame('26.00', (string) $order->total_amount, '10.00 + 16.00');
|
||||
}
|
||||
|
||||
/** 已接单、采购中、配送中均可修改 */
|
||||
public function test_container_update_allowed_in_editable_statuses(): void
|
||||
{
|
||||
$this->seedContainerConfig();
|
||||
$order = $this->makeOrderWithStatus(StoreOrderModel::STATUS_SUMMARIZED);
|
||||
$this->actingAsSysUser();
|
||||
|
||||
foreach ([
|
||||
StoreOrderModel::STATUS_SUMMARIZED,
|
||||
StoreOrderModel::STATUS_DELIVERING,
|
||||
StoreOrderModel::STATUS_DISTRIBUTION,
|
||||
] as $status) {
|
||||
$order->update(['status' => $status]);
|
||||
$this->putJson("/order/store/{$order->id}/container", ['box_num' => 1, 'tray_num' => 0])
|
||||
->assertOk()->assertJsonPath('success', true);
|
||||
$this->assertSame('2.00', (string) $order->fresh()->added_amount);
|
||||
}
|
||||
}
|
||||
|
||||
/** 待接单、已完成、已取消不允许修改 */
|
||||
public function test_container_update_rejected_in_other_statuses(): void
|
||||
{
|
||||
$this->seedContainerConfig();
|
||||
$order = $this->makeOrderWithStatus(StoreOrderModel::STATUS_PENDING);
|
||||
$this->actingAsSysUser();
|
||||
|
||||
foreach ([
|
||||
StoreOrderModel::STATUS_PENDING,
|
||||
StoreOrderModel::STATUS_COMPLETED,
|
||||
StoreOrderModel::STATUS_CANCELLED,
|
||||
] as $status) {
|
||||
$order->update(['status' => $status]);
|
||||
$this->putJson("/order/store/{$order->id}/container", ['box_num' => 5, 'tray_num' => 5])
|
||||
->assertOk()->assertJsonPath('success', false);
|
||||
}
|
||||
$order->refresh();
|
||||
$this->assertSame(0, $order->box_num, '被拒绝后数量不变');
|
||||
$this->assertSame('10.00', (string) $order->total_amount, '被拒绝后总金额不变');
|
||||
}
|
||||
|
||||
/** 历史订单未写商品金额时按「总额 - 附加」反推回写 */
|
||||
public function test_container_update_heals_legacy_product_amount(): void
|
||||
{
|
||||
$this->seedContainerConfig();
|
||||
$order = $this->makeOrderWithStatus(StoreOrderModel::STATUS_SUMMARIZED);
|
||||
$order->update(['product_amount' => 0]); // 模拟历史数据
|
||||
|
||||
$this->actingAsSysUser();
|
||||
$this->putJson("/order/store/{$order->id}/container", ['box_num' => 2, 'tray_num' => 0])
|
||||
->assertOk()->assertJsonPath('success', true);
|
||||
|
||||
$order->refresh();
|
||||
$this->assertSame('10.00', (string) $order->product_amount, '反推回写商品金额');
|
||||
$this->assertSame('4.00', (string) $order->added_amount, '2×2.00');
|
||||
$this->assertSame('14.00', (string) $order->total_amount);
|
||||
}
|
||||
|
||||
/** 数量为负时校验失败 */
|
||||
public function test_container_update_validates_negative_numbers(): void
|
||||
{
|
||||
$this->seedContainerConfig();
|
||||
$order = $this->makeOrderWithStatus(StoreOrderModel::STATUS_SUMMARIZED);
|
||||
|
||||
$this->actingAsSysUser();
|
||||
$this->putJson("/order/store/{$order->id}/container", ['box_num' => -1, 'tray_num' => 0])
|
||||
->assertJsonPath('success', false)
|
||||
->assertJsonPath('msg', '周转框数量不能小于 0');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user