109 lines
7.4 KiB
PHP
109 lines
7.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Business;
|
|
|
|
use App\Models\Auth\Permission;
|
|
use App\Support\CourierBills\CourierBillMatcher;
|
|
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');
|
|
$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());
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|