CaiWuXiTong/tests/Feature/Business/CourierBillPagesTest.php

173 lines
12 KiB
PHP

<?php
namespace Tests\Feature\Business;
use App\Models\Auth\Permission;
use App\Support\CourierBills\CourierBillMatcher;
use App\Support\CourierBills\CourierBillStore;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Tests\Feature\FeatureTestCase;
final class CourierBillPagesTest extends FeatureTestCase
{
public function test_pages_render_and_main_actions_work_in_both_sections(): void
{
Storage::fake('local');
$this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail());
$this->loginAs()->withExceptionHandling();
$db = CourierBillMatcher::db();
$shipping = app(\App\Support\CourierBills\ShippingLookup::class)->connection();
$shipping->table('jst_oms_shipping')->insert(['express_no' => 'ZT100', 'express_name1' => '河北中通-江三', 'ship_time' => '2026-10-03 08:00:00', 'source_id' => 1]);
$shipping->table('jst_oms_shipping_freight')->insert(['source_id' => 1, 'freight' => 8, 'status' => 'OK', 'charge_weight' => 1]);
foreach (['courier-payables', 'courier-payables-final'] as $section) {
$directory = route('business.pages.show', ['section' => $section]);
$this->get($directory)->assertOk()->assertSeeText('快递应付汇总')->assertSeeText('快递账单对账');
$page = fn (array $q = []) => route('business.pages.show', ['section' => $section, 'page' => 'courier-bill-reconciliation', ...$q]);
foreach (['overview', 'details', 'upload', 'ledger', 'groups'] as $view) {
$this->get($page(['cb_view' => $view, 'month' => '2026-10']))->assertOk()->assertSee('data-cbr-root', false);
}
}
$section = 'courier-payables-final';
$this->post(route("business.$section.courier-bills.save-group"), ['name' => '河北中通-江三/小件', 'express_names' => "河北中通-江三\n河北中通小件-江三",
'summary_carrier' => '河北中通', 'owner' => '张三'])->assertRedirect();
$this->assertSame(1, $db->table('courier_bill_groups')->count());
$this->post(route("business.$section.courier-bills.upload"), ['month' => '2026-10', 'files' => [$this->bill('河北中通10月.xlsx')]])
->assertRedirect()->assertSessionHas('courier_bills_message');
$statement = $db->table('courier_bill_statements')->sole();
$this->assertSame(2, (int) $statement->line_count);
$statementUrl = route('business.pages.show', ['section' => $section, 'page' => 'courier-bill-reconciliation', 'cb_view' => 'statement', 'statement' => $statement->id]);
$this->get($statementUrl)->assertOk()->assertSeeText('10月运费')->assertSeeText('匹配不到')->assertSeeText('ZT100');
$db->table('courier_bill_lines')->where('waybill_no', 'ZT100')->update(['bill_amount' => 8.004]);
app(\App\Support\CourierBills\CourierBillMatcher::class)->refreshSummary((int) $statement->id);
$this->get($statementUrl)->assertOk()->assertSeeText('8.0040')->assertSeeText('13.0040');
$this->post(route("business.$section.courier-bills.add-item", ['statement' => $statement->id]), ['label' => '上楼费', 'bill' => 20, 'self' => 15])->assertRedirect();
$line = $db->table('courier_bill_lines')->where('waybill_no', 'ZT999')->first();
$this->from($statementUrl)->post(route("business.$section.courier-bills.resolve"), ['lines' => [$line->id], 'resolution' => 'exclude'])->assertRedirect($statementUrl);
$this->get(route("business.$section.courier-bills.export-statement", ['statement' => $statement->id]))->assertOk();
$this->get(route("business.$section.courier-bills.export-details", ['month' => '2026-10', 'resolution' => 'all']))->assertOk();
$this->post(route("business.$section.courier-bills.confirm", ['statement' => $statement->id]))->assertRedirect();
$this->assertSame('confirmed', $db->table('courier_bill_statements')->value('status'));
$this->from($statementUrl)->post(route("business.$section.courier-bills.add-item", ['statement' => $statement->id]), ['label' => 'x'])
->assertRedirect($statementUrl)->assertSessionHasErrors('courier_bills');
// 快递应付汇总
$summary = route('business.pages.show', ['section' => $section, 'page' => 'courier-payable-summary']);
$this->get($summary)->assertOk()->assertSeeText('导入手工快递汇总表');
$this->post(route("business.$section.courier-payable-summary.import"), ['month' => '2026-10', 'summary' => $this->summaryFile()])->assertRedirect();
$row = $db->table('courier_payable_summary_rows')->where('carrier', '河北中通')->sole();
$this->get($summary . '?month=2026-10')->assertOk()->assertSeeText('河北中通')->assertSeeText('核对无误')->assertSeeText('上楼费5元');
$this->postJson(route("business.$section.courier-payable-summary.update", ['row' => $row->id]), ['column' => 'S', 'value' => '13'])->assertOk();
$this->postJson(route("business.$section.courier-payable-summary.update", ['row' => $row->id]), ['column' => 'K', 'value' => '1'])->assertUnprocessable();
$this->post(route("business.$section.courier-payable-summary.create-month"), ['month' => '2026-11'])->assertRedirect();
$this->get(route("business.$section.courier-payable-summary.export", ['month' => '2026-11']))->assertOk();
// 对账分组:从汇总生成、导出、检查未归组、删除
$this->post(route("business.$section.courier-bills.groups-from-summary"), ['summary_month' => '2026-11'])->assertRedirect()->assertSessionHas('courier_bills_message');
$this->get(route("business.$section.courier-bills.export-groups"))->assertOk();
$this->get(route('business.pages.show', ['section' => $section, 'page' => 'courier-bill-reconciliation', 'cb_view' => 'groups', 'scan' => '2026-10']))
->assertOk()->assertSeeText('发货数据里未归组的快递名称1');
$this->post(route("business.$section.courier-bills.import-groups"), ['groups' => new UploadedFile($this->xlsx(['分组' => [['分组名称', '快递名称1'], ['新分组', '新快递']]]), 'g.xlsx', null, null, true)])
->assertRedirect()->assertSessionHas('courier_bills_message');
$newGroup = $db->table('courier_bill_groups')->where('name', '新分组')->value('id');
$this->post(route("business.$section.courier-bills.delete-group", ['group' => $newGroup]))->assertRedirect();
$this->assertFalse($db->table('courier_bill_groups')->where('name', '新分组')->exists());
}
public function test_upload_identifies_each_file_by_name_and_allows_an_unrecognized_file_to_be_assigned(): void
{
Storage::fake('local');
$this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail());
$this->loginAs()->withExceptionHandling();
$store = app(CourierBillStore::class);
$first = $store->saveGroup(null, ['name' => '河北中通', 'express_names' => ['河北中通']]);
$second = $store->saveGroup(null, ['name' => '北京韵达', 'express_names' => ['北京韵达']]);
$section = 'courier-payables-final';
$uploadPage = route('business.pages.show', ['section' => $section, 'page' => 'courier-bill-reconciliation', 'cb_view' => 'upload', 'month' => '2026-10']);
$this->get($uploadPage)->assertOk()->assertDontSee('name="group_ids[]"', false);
$this->post(route("business.$section.courier-bills.upload"), [
'month' => '2026-10', 'group_id' => $first, 'group_ids' => [$first],
'files' => [
UploadedFile::fake()->createWithContent('河北中通10月.csv', "运单号,运费\nZT100,8\n"),
UploadedFile::fake()->createWithContent('北京韵达10月.csv', "运单号,运费\nYD200,9\n"),
UploadedFile::fake()->createWithContent('月结账单.csv', "运单号,运费\nYD300,10\n"),
],
])->assertRedirect($uploadPage);
$files = $store->db()->table('courier_bill_files')->get()->keyBy('original_name');
$this->assertEquals($first, $files['河北中通10月.csv']->group_id);
$this->assertEquals($second, $files['北京韵达10月.csv']->group_id);
$pending = $files['月结账单.csv'];
$this->assertSame('pending_group', $pending->status);
$this->assertNull($pending->group_id);
$this->get($uploadPage)->assertOk()->assertSeeText('待选分组')->assertSee('name="group_id"', false);
$this->from($uploadPage)->post(route("business.$section.courier-bills.assign", ['file' => $pending->id]), [
'group_id' => $second,
])->assertRedirect($uploadPage);
$this->assertEquals($second, $store->db()->table('courier_bill_files')->where('id', $pending->id)->value('group_id'));
$this->assertSame('parsed', $store->db()->table('courier_bill_files')->where('id', $pending->id)->value('status'));
}
public function test_upload_page_deletion_and_reread_feedback_preserve_confirmed_bills(): void
{
Storage::fake('local');
$this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail());
$this->loginAs()->withExceptionHandling();
$store = app(CourierBillStore::class);
$group = $store->saveGroup(null, ['name' => '测试快递', 'express_names' => ['测试快递']]);
$file = $store->upload([UploadedFile::fake()->createWithContent("账单');alert(1);//.csv", "运单号,运费\nZT100,8\n")], '2026-10', [$group], 'tester')[0];
$section = 'courier-payables-final';
$page = route('business.pages.show', ['section' => $section, 'page' => 'courier-bill-reconciliation', 'cb_view' => 'upload', 'month' => '2026-10']);
$delete = route("business.$section.courier-bills.delete-file", ['file' => $file]);
$this->get($page)->assertOk()->assertSeeText('一半及以上单号相同')->assertSeeText('删除')
->assertSee($delete)->assertSee("return confirm('删除这个文件和它读入的账单行?')", false)
->assertDontSee("confirm('删除「账单", false);
$statement = (int) $store->db()->table('courier_bill_files')->where('id', $file)->value('statement_id');
$this->post(route("business.$section.courier-bills.rematch", ['statement' => $statement]))
->assertRedirect()->assertSessionHas('courier_bills_message', fn ($message) => str_contains($message, '按当前规则重新读取账单文件'));
$store->confirm($statement, 'tester');
$this->from($page)->post($delete)->assertRedirect($page)->assertSessionHasErrors('courier_bills');
$this->assertNotNull($store->db()->table('courier_bill_files')->find($file));
$this->assertSame(1, $store->db()->table('courier_bill_lines')->where('file_id', $file)->count());
$store->unconfirm($statement);
$this->post($delete)->assertRedirect();
$this->assertNull($store->db()->table('courier_bill_files')->find($file));
}
private function bill(string $name): UploadedFile
{
return new UploadedFile($this->xlsx(['Sheet1' => [['运单号', '运费'], ['ZT100', 8], ['ZT999', 5]]]), $name, null, null, true);
}
private function summaryFile(): UploadedFile
{
return new UploadedFile($this->xlsx(['汇总' => [
[null, null, '标题'],
['合同开始时间', '合同截止时间', '签订公司', '负责人', '承运状态', '序号', '快递公司主体名称', '承运商'],
[],
['2026-01-01', '2026-12-31', '示例公司', '张三', '承运', 1, '河北某物流', '河北中通', 0],
]]), '10月快递汇总.xlsx', null, null, true);
}
private function xlsx(array $sheets): string
{
$book = new Spreadsheet();
$book->removeSheetByIndex(0);
foreach ($sheets as $title => $rows) {
$sheet = $book->createSheet();
$sheet->setTitle($title);
$sheet->fromArray($rows, null, 'A1', true);
}
$path = tempnam(sys_get_temp_dir(), 'cbp_') . '.xlsx';
(new Xlsx($book))->save($path);
return $path;
}
}