CaiWuXiTong/tests/Feature/Business/CourierFreightPageTest.php
qiuxiang f805920fda 快递运费:新增「店铺名称 / 快递单号 / 条形码」列,并按「财务系统运费整理.xlsx」重新导入
- 迁移:courier_freight_rows 增加 express_no / barcode / shop_name(可空)
- 导入器支持可选列(快递单号 / 条形码 / 店铺名称),老文件缺这三列也能导;XlsxSheetReader 支持可选列表头
- 页面与导出新增三列;运费差 / 结算运费 / 其他附加费汇总 / 退件费 表头与取数逻辑不变
- 测试:新增新列导入、老文件兼容、页面展示用例
2026-10-12 11:56:00 +08:00

122 lines
4.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Tests\Feature\Business;
use App\Business\CourierFreightImporter;
use App\Models\Auth\Permission;
use App\Models\Business\CourierFreightRow;
use Tests\Feature\FeatureTestCase;
/**
* 财务经营报表 › 快递运费(导入 + 页面列)。
*
* 来源 / 成品配件 / 交易单号 / 提供成本运费 来自导入文件;
* 快递单号 / 条形码 / 店铺名称 为「财务系统运费整理.xlsx」起新增的可选列(老文件没有也能导)。
*/
class CourierFreightPageTest extends FeatureTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->user->attachPermission(
Permission::where('name', 'read-admin-panel')->firstOrFail()
);
}
public function test_import_reads_new_optional_columns(): void
{
$path = $this->makeCsv([
['来源', '快递单号', '交易单号', '条形码', '成品配件', '提供成本运费', '店铺名称'],
['提供成本7月汇总', '79015585514691', '5121853455486000631', 'cbz000203N1221V01', '成品运费', '6.7', '淘宝-亿家达家居鞋柜电脑桌'],
['提供成本7月汇总', '', '5121853455486000632', 'DNZ023905N1221V01', '配件运费', '1.5', ''],
]);
try {
$result = app(CourierFreightImporter::class)->import($path, $this->company->id, true, 'test');
} finally {
@unlink($path);
}
$this->assertSame(2, $result['inserted']);
$row = CourierFreightRow::query()->where('company_id', $this->company->id)
->where('transaction_no', '5121853455486000631')->firstOrFail();
$this->assertSame('提供成本7月汇总', $row->source);
$this->assertSame('79015585514691', $row->express_no);
$this->assertSame('cbz000203N1221V01', $row->barcode);
$this->assertSame('淘宝-亿家达家居鞋柜电脑桌', $row->shop_name);
$this->assertSame('成品运费', $row->part);
$this->assertSame(6.7, (float) $row->freight_amount);
$blank = CourierFreightRow::query()->where('company_id', $this->company->id)
->where('transaction_no', '5121853455486000632')->firstOrFail();
$this->assertNull($blank->express_no);
$this->assertNull($blank->shop_name);
}
public function test_import_old_file_without_optional_columns_still_works(): void
{
$path = $this->makeCsv([
['来源', '成品配件', '交易单号', '提供成本运费'],
['8月提供成本', '成品运费', 'X-1', '2.5'],
]);
try {
$result = app(CourierFreightImporter::class)->import($path, $this->company->id, true, 'test');
} finally {
@unlink($path);
}
$this->assertSame(1, $result['inserted']);
$row = CourierFreightRow::query()->where('company_id', $this->company->id)->firstOrFail();
$this->assertNull($row->express_no);
$this->assertNull($row->barcode);
$this->assertNull($row->shop_name);
}
public function test_page_shows_new_stored_columns(): void
{
$path = $this->makeCsv([
['来源', '快递单号', '交易单号', '条形码', '成品配件', '提供成本运费', '店铺名称'],
['提供成本7月汇总', '79015585514691', 'T-0001', 'cbz000203N1221V01', '成品运费', '6.7', '淘宝-亿家达家居鞋柜电脑桌'],
]);
try {
app(CourierFreightImporter::class)->import($path, $this->company->id, true, 'test');
} finally {
@unlink($path);
}
$page = $this->loginAs()->get(route('business.pages.show', [
'section' => 'financial-operations',
'page' => 'courier-freight',
]));
$page->assertOk()
->assertSee('快递运费')
->assertSee('快递单号')
->assertSee('条形码')
->assertSee('店铺名称')
->assertSee('T-0001')
->assertSee('79015585514691')
->assertSee('cbz000203N1221V01')
->assertSee('淘宝-亿家达家居鞋柜电脑桌');
}
/** @param list<list<string>> $rows */
private function makeCsv(array $rows): string
{
$path = tempnam(sys_get_temp_dir(), 'courier-') . '.csv';
$handle = fopen($path, 'wb');
fwrite($handle, "\xEF\xBB\xBF");
foreach ($rows as $row) {
fputcsv($handle, $row);
}
fclose($handle);
return $path;
}
}