CaiWuXiTong/tests/Feature/Business/AfterSalesCostImportTest.php
chenxi 06707791e8 商品成本使用售后导入明细补拆组合装
订单镜像缺少组合组件时,按店铺和线上订单号使用售后导入明细唯一补拆;保留既有组件与历史画像优先级。
2026-10-25 09:47:08 +08:00

122 lines
5.0 KiB
PHP

<?php
namespace Tests\Feature\Business;
use Illuminate\Support\Facades\DB;
use Modules\EcommerceFinance\Repositories\AfterSalesCostRowRepository;
use Modules\EcommerceFinance\View\OrderDataPageData;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Tests\Feature\FeatureTestCase;
class AfterSalesCostImportTest extends FeatureTestCase
{
public function test_imported_rows_are_served_before_the_mirror(): void
{
DB::table('ops_after_sales_cost_rows')->insert(AfterSalesCostRowRepository::toRecord([
'shop_name' => '天猫-亿家达旗舰店',
'shop_code' => '18526685',
'online_order_no' => '5120748758529014849',
'internal_order_no' => '8064260',
'product_code' => 'SJ038020N1410V02',
'order_time' => '2026-06-21 18:00:00',
'product_quantity' => 1,
'product_amount' => 385.43,
'product_cost' => 110.53,
'cost_source' => '商品成本',
'combine_type' => '组合单品',
]));
DB::table('ops_after_sales_cost_rows')->insert(AfterSalesCostRowRepository::toRecord([
'shop_name' => '天猫-亿家达旗舰店',
'online_order_no' => '5120748758529014000',
'product_code' => 'SJ038999N1410V01',
'order_time' => '2026-06-01 00:00:00',
'product_quantity' => 1,
'product_amount' => 10,
'product_cost' => 5,
]));
DB::table('ops_after_sales_cost_rows')->insert(AfterSalesCostRowRepository::toRecord([
'shop_name' => '天猫-亿家达旗舰店',
'online_order_no' => '5120748758529014001',
'product_code' => 'SJ038998N1410V01',
'order_time' => '2026-06-30 23:00:00',
'product_quantity' => 1,
'product_amount' => 10,
'product_cost' => 5,
]));
$result = app(OrderDataPageData::class)->afterSalesRows([
'from' => '2026-06-01',
'to' => '2026-06-30',
'shop_names' => ['天猫-亿家达旗舰店'],
'order_no' => '',
'sku_id' => '',
], 1, 20);
$this->assertSame('imported', $result['status']['X-Caiwu-Data-Status']);
$this->assertCount(3, $result['rows']);
$target = collect($result['rows'])->firstWhere('product_code', 'SJ038020N1410V02');
$this->assertNotNull($target);
$this->assertEquals(110.53, $target['product_cost']);
}
public function test_import_command_reads_the_jst_export_file(): void
{
$path = tempnam(sys_get_temp_dir(), 'jst') . '.xlsx';
$sheet = new Spreadsheet();
$sheet->getActiveSheet()->fromArray([
['内部订单号', '线上订单号', '店铺名称', '商品编码', '商品数量', '商品金额', '商品成本', '成本来源', '组合装', '订单时间'],
['8064260', '5120748758529014849', '天猫-亿家达旗舰店', 'SJ038051N1410V01', 1, 14.54, 4.17, '商品成本', '组合单品', '2026-06-21 18:00:00'],
]);
(new Xlsx($sheet))->save($path);
$this->artisan('order-data:import-after-sales-cost', ['file' => $path])->assertSuccessful();
$this->assertDatabaseHas('ops_after_sales_cost_rows', [
'shop_name' => '天猫-亿家达旗舰店',
'product_code' => 'SJ038051N1410V01',
'cost_source' => '商品成本',
]);
@unlink($path);
}
public function test_component_rows_for_orders_reads_split_components_from_payload(): void
{
$repository = app(AfterSalesCostRowRepository::class);
DB::table('ops_after_sales_cost_rows')->insert(AfterSalesCostRowRepository::toRecord([
'shop_name' => '天猫-亿家达旗舰店',
'online_order_no' => '5120748758529014849',
'internal_order_no' => '8064260',
'product_code' => 'SJ038020N1410V02',
'shop_product_code' => '6208843810051',
'shop_style_code' => '1030426322500',
'product_quantity' => 1,
'product_amount' => 385.43,
]));
DB::table('ops_after_sales_cost_rows')->insert(AfterSalesCostRowRepository::toRecord([
'shop_name' => '天猫-亿家达旗舰店',
'online_order_no' => '5120748758529014849',
'internal_order_no' => '8064260',
'product_code' => 'SJ038051N1410V01',
'shop_product_code' => '6208843810051',
'shop_style_code' => '1030426322500',
'product_quantity' => 1,
'product_amount' => 14.54,
]));
$rows = $repository->componentRowsForOrders(['5120748758529014849']);
$this->assertCount(2, $rows);
$this->assertSame('SJ038020N1410V02', $rows[0]['product_code']);
$this->assertSame('6208843810051', $rows[0]['shop_product_code']);
$this->assertSame('1030426322500', $rows[0]['shop_style_code']);
$this->assertSame(385.43, $rows[0]['product_amount']);
}
}