CaiWuXiTong/tests/Feature/Business/AfterSalesCostImportTest.php
renshen d5cc75082c 优化月度导入数据首页与批量编码查询 (#582)
增加月份订单索引并优化批量编码查询
2026-10-26 11:30:01 +08:00

217 lines
8.8 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_period_import_keeps_fee_only_rows_visible_for_the_whole_month(): void
{
DB::table('ops_after_sales_cost_rows')->insert(AfterSalesCostRowRepository::toRecord([
'shop_name' => '天猫-亿家达旗舰店',
'online_order_no' => '5120748758529014900',
'product_code' => 'SJ038051N1410V01',
'order_time' => '2025-03-20 10:00:00',
'product_cost' => 4.17,
], null, '2026-06'));
DB::table('ops_after_sales_cost_rows')->insert(AfterSalesCostRowRepository::toRecord([
'shop_name' => '天猫-亿家达旗舰店',
'online_order_no' => '5120748758529014901',
'product_code' => 'SJ038052N1410V01',
'product_cost' => 0,
'account_8001201' => 2.5,
], null, '2026-06'));
$result = app(OrderDataPageData::class)->afterSalesRows([
'from' => '2026-06-01',
'to' => '2026-06-30',
'shop_names' => ['天猫-亿家达旗舰店'],
'shop_names_exact' => true,
'order_no' => '',
'sku_id' => '',
], 1, 20);
$this->assertSame('imported', $result['status']['X-Caiwu-Data-Status']);
$this->assertCount(2, $result['rows']);
$partial = app(AfterSalesCostRowRepository::class)->covers([
'from' => '2026-06-01',
'to' => '2026-06-15',
'shop_names' => ['天猫-亿家达旗舰店'],
'shop_names_exact' => true,
'order_no' => '',
'sku_id' => '',
]);
$this->assertFalse($partial);
}
public function test_period_import_matches_online_and_internal_order_numbers(): void
{
DB::table('ops_after_sales_cost_rows')->insert(AfterSalesCostRowRepository::toRecord([
'shop_name' => '天猫-亿家达旗舰店',
'online_order_no' => '5120748758529015001',
'internal_order_no' => '8064501',
'product_code' => 'SKU-ONLINE',
'style_code' => 'STYLE-ONLINE',
'order_time' => '2025-03-20 10:00:00',
], null, '2026-06'));
DB::table('ops_after_sales_cost_rows')->insert(AfterSalesCostRowRepository::toRecord([
'shop_name' => '天猫-亿家达旗舰店',
'online_order_no' => '5120748758529015002',
'internal_order_no' => '8064502',
'product_code' => 'SKU-OTHER',
'style_code' => 'STYLE-BATCH',
'order_time' => '2025-03-21 10:00:00',
], null, '2026-06'));
$filters = [
'from' => '2026-06-01',
'to' => '2026-06-30',
'shop_names' => [],
'shop_names_exact' => false,
'sku_id' => '',
];
$online = app(OrderDataPageData::class)->afterSalesRows(
array_replace($filters, ['order_no' => '5120748758529015001']),
1,
20,
);
$internal = app(OrderDataPageData::class)->afterSalesRows(
array_replace($filters, ['order_no' => '8064501']),
1,
20,
);
$batchSku = app(OrderDataPageData::class)->afterSalesRows(
array_replace($filters, ['order_no' => '', 'sku_id' => "SKU-ONLINE\nSTYLE-BATCH"]),
1,
20,
);
$this->assertSame('imported', $online['status']['X-Caiwu-Data-Status']);
$this->assertSame('SKU-ONLINE', $online['rows'][0]['product_code']);
$this->assertSame('imported', $internal['status']['X-Caiwu-Data-Status']);
$this->assertSame('SKU-ONLINE', $internal['rows'][0]['product_code']);
$this->assertSame('imported', $batchSku['status']['X-Caiwu-Data-Status']);
$this->assertEqualsCanonicalizing(
['SKU-ONLINE', 'SKU-OTHER'],
array_column($batchSku['rows'], 'product_code'),
);
}
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']);
}
}