diff --git a/app/Business/CourierContractPageData.php b/app/Business/CourierContractPageData.php index a5a5d1e12..1be548e21 100644 --- a/app/Business/CourierContractPageData.php +++ b/app/Business/CourierContractPageData.php @@ -96,6 +96,36 @@ class CourierContractPageData } } + /** @return array 同名快递取最新归档合作合同的日期。 */ + public function datesByCourierNames(array $names): array + { + $names = array_values(array_unique(array_filter(array_map('trim', $names)))); + if ($names === [] || ! Schema::hasTable('business_courier_agreement_signatures')) { + return []; + } + + $dates = []; + foreach (array_chunk($names, 500) as $chunk) { + $signatures = CourierAgreementSignature::query()->where('company_id', (int) company_id()) + ->where('document_type', 'cooperation-contract')->where('status', CourierAgreementSignature::STATUS_ARCHIVED) + ->whereIn('courier_name', $chunk) + ->select(['id', 'courier_name', 'payload', 'document_type', 'status']) + ->orderByDesc('archived_at')->orderByDesc('id')->cursor(); + foreach ($signatures as $signature) { + $name = trim((string) $signature->courier_name); + if (isset($dates[$name]) || ($payload = $signature->archivedCourierContractPayload()) === null) { + continue; + } + $dates[$name] = [ + 'start_date' => $this->date($payload['cooperation_start_date'] ?? null), + 'end_date' => $this->date($payload['cooperation_end_date'] ?? null), + ]; + } + } + + return $dates; + } + private function filters(): array { $search = request()->query('contract_search'); diff --git a/app/Support/CourierBills/PayableSummary.php b/app/Support/CourierBills/PayableSummary.php index 036d12ec0..a182ae302 100644 --- a/app/Support/CourierBills/PayableSummary.php +++ b/app/Support/CourierBills/PayableSummary.php @@ -2,6 +2,7 @@ namespace App\Support\CourierBills; +use App\Business\CourierContractPageData; use Illuminate\Database\ConnectionInterface; use Modules\EcommerceFinance\Repositories\CourierInformationMirrorRepository; use RuntimeException; @@ -38,7 +39,10 @@ final class PayableSummary /** 有对账单时由小汇总覆盖的列。 */ private const FROM_STATEMENTS = ['J', 'K', 'L', 'N', 'O', 'P', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT']; - public function __construct(private readonly CourierInformationMirrorRepository $couriers) {} + public function __construct( + private readonly CourierInformationMirrorRepository $couriers, + private readonly CourierContractPageData $contractDates, + ) {} public function db(): ConnectionInterface { @@ -66,6 +70,7 @@ final class PayableSummary report($error); $contracts = []; } + $contractDates = $this->contractDates->datesByCourierNames($stored->pluck('carrier')->all()); $today = now()->toDateString(); $rows = []; foreach ($stored as $row) { @@ -77,6 +82,9 @@ final class PayableSummary $cells['C'] = $contract['company'] !== '' ? $contract['company'] : ($cells['C'] ?? null); $cells['G'] = $contract['body'] !== '' ? $contract['body'] : ($cells['G'] ?? null); } + $dates = $contractDates[$row->carrier] ?? []; + $cells['A'] = $dates['start_date'] ?? ($cells['A'] ?? null); + $cells['B'] = $dates['end_date'] ?? ($cells['B'] ?? null); $mine = $statements[$row->carrier] ?? []; // 导入的汇总里已经有金额(手工做完的月份):以导入为准,对账单算出来的只用来比对; // 新建的月份金额是空的,才用对账单的小汇总填。 @@ -182,6 +190,13 @@ final class PayableSummary if ($row === null) { throw new RuntimeException('汇总行不存在。'); } + if ($column === 'S') { + foreach ($this->rows((string) $row->month) as $current) { + if ($current['id'] === $rowId && $current['cells']['R'] > 0) { + throw new RuntimeException('本月应付金额大于 0 时,本次实付金额自动等于本月应付金额,无需手工修改。'); + } + } + } $cells = (array) json_decode((string) $row->cells, true); if (in_array($column, self::MONEY, true)) { $number = BillParser::number($value); diff --git a/app/Support/CourierBills/SummaryMapper.php b/app/Support/CourierBills/SummaryMapper.php index aacfa184f..e52893645 100644 --- a/app/Support/CourierBills/SummaryMapper.php +++ b/app/Support/CourierBills/SummaryMapper.php @@ -126,12 +126,15 @@ final class SummaryMapper ]; } - /** 本月应付 R = K − L − N − O − P + Q + J + I;核对确定 M = K − L。 */ + /** 本月应付 R = K − L − N − O − P + Q + J + I;核对确定 M = K − L;R > 0 时实付 S = R。 */ public static function totals(array $cells): array { $n = static fn (string $key): float => (float) ($cells[$key] ?? 0); $cells['M'] = round($n('K') - $n('L'), 2); $cells['R'] = round($n('K') - $n('L') - $n('N') - $n('O') - $n('P') + $n('Q') + $n('J') + $n('I'), 2); + if ($cells['R'] > 0) { + $cells['S'] = $cells['R']; + } return $cells; } diff --git a/resources/views/business/pages/courier-payables/courier-payable-summary.blade.php b/resources/views/business/pages/courier-payables/courier-payable-summary.blade.php index 81b70c065..a9a676510 100644 --- a/resources/views/business/pages/courier-payables/courier-payable-summary.blade.php +++ b/resources/views/business/pages/courier-payables/courier-payable-summary.blade.php @@ -33,8 +33,8 @@ + - @if ($psStatus)@endif @@ -80,7 +80,7 @@ {{ $c['H'] ?? '' }} {{ $row['status'] === '已确认' ? '核对无误' : $row['status'] }} @foreach ($main as $k) - @if (isset($editable[$k])) + @if (isset($editable[$k]) && ! ($k === 'S' && ($c['R'] ?? 0) > 0)) @else {{ $money($c[$k] ?? 0) }} @@ -106,7 +106,7 @@ - + @endif @@ -148,7 +148,8 @@

已用 10 月 36 份手工对账表里的 42 个小汇总验证:按下面的规则归集,与手工快递汇总表逐行、逐列一致。小汇总里的项目名称决定归到哪一列,依次判断,命中即停。

- + + @@ -167,7 +168,8 @@ - + +
合同开始/截止时间、签订公司、快递公司主体名称取「快递资料」(按承运商名称);快递资料里没有的保留导入值。截止时间早于今天标「已到期」。
合同开始/截止时间优先取「快递资料 → 合同管理」中同名承运商最新归档合作合同的日期;未填写的日期沿用快递资料或导入值。截止时间早于今天标「已到期」。
签订公司、快递公司主体名称取「快递资料」(按承运商名称);快递资料里没有的保留导入值。
月初结余上月「本月应付金额 − 本次实付金额」结转(没填实付按 0 算);可手工改。签署完结后再按工作平台状态自动判断,细则后定。
原单补发配件项目名称含「补发配件」或「配件补发」(排在第一条,「成品补发配件扣快递」也算这里)。
补给快递以「补」开头、含「记账补款」或以「需补」结尾的项目,取正数;名称含「记账」的同时计入「记账补款金额」。不进差异明细。
对账差异金额差异明细 13 列合计 − 违约金 − 借支。
核对确定金额快递提供金额 − 对账差异金额。
本月应付金额快递提供 − 对账差异 − 面单 − 借支 − 赔付 + 其他补款 + 补给快递 + 月初结余。
其他补款、本次实付金额手工填。
其他补款手工填。
本次实付金额本月应付金额大于 0 时自动等于本月应付金额,随应付金额重算;本月应付金额为 0 或负数时保留原值,可手工填。
@@ -223,7 +225,7 @@ if (!response.ok) throw new Error((await response.json().catch(() => ({})))?.message || 'save'); await swap(location.href); const next = document.querySelector('[data-ps-status]'); - if (next) next.textContent = '已保存,本月应付已重算。'; + if (next) next.textContent = '已保存,本月应付及正数应付对应的本次实付已重算。'; } catch (error) { input.disabled = false; if (status) status.textContent = '保存失败:' + (error.message === 'save' ? '请刷新后重试' : error.message); diff --git a/tests/Feature/Business/CourierBillPagesTest.php b/tests/Feature/Business/CourierBillPagesTest.php index d67d873c1..2f1d025b4 100644 --- a/tests/Feature/Business/CourierBillPagesTest.php +++ b/tests/Feature/Business/CourierBillPagesTest.php @@ -81,7 +81,7 @@ final class CourierBillPagesTest extends FeatureTestCase $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' => 'S', 'value' => '13'])->assertUnprocessable(); $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(); diff --git a/tests/Feature/Business/CourierPayableSummaryRulesTest.php b/tests/Feature/Business/CourierPayableSummaryRulesTest.php new file mode 100644 index 000000000..381d7c88e --- /dev/null +++ b/tests/Feature/Business/CourierPayableSummaryRulesTest.php @@ -0,0 +1,197 @@ +shouldReceive('contractsByName')->andReturn([ + '合同快递' => ['contract_start' => '2025-01-01', 'contract_end' => '2025-12-31', 'company' => '原签订公司', 'body' => '原合同主体'], + ]); + $this->app->instance(CourierInformationMirrorRepository::class, $mirror); + } + + public function test_positive_payables_drive_paid_amount_export_and_next_month_balance(): void + { + $service = app(PayableSummary::class); + $expected = []; + foreach ([ + ['正数快递', ['K' => 2000, 'L' => 50, 'I' => 100.5, 'S' => 1000], 2050.5, 2050.5], + ['一分钱快递', ['K' => 0.01, 'S' => null], 0.01, 0.01], + ['零数快递', ['K' => 0.004, 'S' => 9], 0.0, 9.0], + ['负数快递', ['I' => -50, 'S' => -5], -50.0, -5.0], + ['空值快递', ['I' => -20], -20.0, null], + ] as [$carrier, $cells, $payable, $paid]) { + $this->row($carrier, $cells); + $expected[$carrier] = [$payable, $paid]; + } + $rows = $service->rows('2026-11'); + foreach ($rows as $row) { + [$payable, $paid] = $expected[$row['cells']['H']]; + $this->assertSame($payable, $row['cells']['R']); + $this->assertSame($paid, $row['cells']['S'] ?? null); + } + $stored = json_decode($service->db()->table('courier_payable_summary_rows')->where('carrier', '正数快递')->value('cells'), true); + $this->assertSame(1000, $stored['S']); + + $file = tempnam(sys_get_temp_dir(), 'courier-auto-paid-'); + try { + $handle = fopen($file, 'wb'); + $service->export('2026-11', $handle); + fclose($handle); + $book = IOFactory::load($file); + $sheet = $book->getSheetByName('汇总'); + $this->assertEquals(2050.5, $sheet->getCell('R4')->getValue()); + $this->assertEquals(2050.5, $sheet->getCell('S4')->getValue()); + $this->assertEquals(9, $sheet->getCell('S6')->getValue()); + $this->assertEquals(2054.51, $sheet->getCell('S9')->getValue()); + $book->disconnectWorksheets(); + } finally { + @unlink($file); + } + + $this->assertSame(5, $service->createMonth('2026-12')); + $next = collect($service->rows('2026-12'))->keyBy('cells.H'); + $this->assertSame(0.0, $next['正数快递']['cells']['I']); + $this->assertSame(0.0, $next['一分钱快递']['cells']['I']); + $this->assertSame(-9.0, $next['零数快递']['cells']['I']); + $this->assertSame(-45.0, $next['负数快递']['cells']['I']); + $this->assertSame(-20.0, $next['空值快递']['cells']['I']); + } + + public function test_pages_and_updates_keep_positive_paid_amount_automatic(): void + { + $positive = $this->row('正数快递', ['K' => 100, 'S' => 7]); + $negative = $this->row('负数快递', ['I' => -10, 'S' => 2]); + $this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail()); + $this->loginAs(); + foreach (['courier-payables', 'courier-payables-final'] as $section) { + $page = route('business.pages.show', ['section' => $section, 'page' => 'courier-payable-summary', 'month' => '2026-11']); + $response = $this->get($page)->assertOk() + ->assertDontSee('aria-label="正数快递 本次实付金额"', false) + ->assertSee('aria-label="负数快递 本次实付金额"', false); + $this->assertSame(102.0, $response->viewData('content')['psTotals']['S']); + $this->withExceptionHandling(); + $this->postJson(route("business.$section.courier-payable-summary.update", ['row' => $positive]), ['column' => 'S', 'value' => '5']) + ->assertUnprocessable()->assertJsonValidationErrors('courier_summary'); + } + $update = fn (int $id) => route('business.courier-payables.courier-payable-summary.update', ['row' => $id]); + $this->postJson($update($positive), ['column' => 'Q', 'value' => '12.34'])->assertOk(); + $this->postJson($update($negative), ['column' => 'S', 'value' => '-3'])->assertOk(); + $rows = collect(app(PayableSummary::class)->rows('2026-11'))->keyBy('cells.H'); + $this->assertSame(112.34, $rows['正数快递']['cells']['R']); + $this->assertSame(112.34, $rows['正数快递']['cells']['S']); + $this->assertSame(-3.0, $rows['负数快递']['cells']['S']); + } + + public function test_reconciliation_status_filter_combines_with_other_filters_and_updates_totals(): void + { + $this->row('已确认快递', ['K' => 100, 'Y' => '已确认', 'C' => '甲公司', 'D' => '负责人甲', 'E' => '承运']); + $this->row('待处理快递', ['K' => 80, 'C' => '甲公司', 'D' => '负责人甲', 'E' => '承运']); + $this->row('旧账快递', ['I' => -20]); + $this->row('无账单快递', []); + $this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail()); + $this->loginAs(); + foreach (['courier-payables', 'courier-payables-final'] as $section) { + $url = route('business.pages.show', ['section' => $section, 'page' => 'courier-payable-summary', 'month' => '2026-11']); + $response = $this->get($url . '&' . http_build_query(['status' => '已确认', 'company' => '甲公司', 'owner' => '负责人甲', 'state' => '承运', 'q' => '快递'])) + ->assertOk()->assertSee('