CaiWuXiTong/tests/Feature/Console/StoreReceiptProductCostWarehouseRemapTest.php
chenxi 4401b0450a 商品成本:重映射兼容仓库名空格
仓库简称定向重映射使用原始仓库值更新,兼容仓库名尾部空格。
2026-10-25 21:14:51 +08:00

82 lines
3.1 KiB
PHP

<?php
namespace Tests\Feature\Console;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class StoreReceiptProductCostWarehouseRemapTest extends TestCase
{
public function test_only_warehouse_short_names_are_remapped_even_when_source_has_spaces(): void
{
$now = now();
DB::table('business_inventory_warehouse_name_mappings')->insert([
'company_id' => 1,
'warehouse_provider' => '新增仓储方',
'warehouse' => '新增仓储方主仓',
'warehouse_short_name' => '新仓简称',
'created_at' => $now,
'updated_at' => $now,
]);
$target = DB::connection('business_mirror')->table('store_receipt_product_cost_rows');
$target->insert([
$this->row('mapped-september', '2026-09-01', '新增仓储方 ', '旧简称', $now),
$this->row('unmapped-september', '2026-09-01', '未映射仓储方', '保持简称', $now),
$this->row('mapped-august', '2026-08-01', '新增仓储方', '八月旧简称', $now),
]);
$this->artisan('store-receipt:product-cost:sync', [
'month' => '2026-09',
'--remap-warehouses-only' => true,
])->assertExitCode(Command::SUCCESS);
$mapped = DB::connection('business_mirror')->table('store_receipt_product_cost_rows')
->where('row_key', 'mapped-september')->first();
$this->assertSame('新仓简称', $mapped->warehouse_short_name);
$this->assertSame('SKU-mapped-september', $mapped->product_code);
$this->assertSame(2.0, (float) $mapped->product_quantity);
$this->assertSame(12.0, (float) $mapped->unit_price);
$this->assertSame(24.0, (float) $mapped->product_cost);
$this->assertSame('保留原备注', $mapped->remark);
$this->assertSame(
'保持简称',
DB::connection('business_mirror')->table('store_receipt_product_cost_rows')
->where('row_key', 'unmapped-september')->value('warehouse_short_name'),
);
$this->assertSame(
'八月旧简称',
DB::connection('business_mirror')->table('store_receipt_product_cost_rows')
->where('row_key', 'mapped-august')->value('warehouse_short_name'),
);
}
/** @return array<string,mixed> */
private function row(
string $rowKey,
string $reportMonth,
string $warehouse,
string $warehouseShortName,
mixed $now,
): array {
return [
'report_month' => $reportMonth,
'row_key' => $rowKey,
'platform' => '京东账单',
'occurred_date' => $reportMonth,
'store_name' => '测试店铺',
'online_order_no' => 'ORDER-' . $rowKey,
'warehouse' => $warehouse,
'warehouse_short_name' => $warehouseShortName,
'product_code' => 'SKU-' . $rowKey,
'product_quantity' => 2,
'unit_price' => 12,
'product_cost' => 24,
'remark' => '保留原备注',
'imported_at' => $now,
];
}
}