diff --git a/app/Http/Controllers/Business/DropshipFreightController.php b/app/Http/Controllers/Business/DropshipFreightController.php
index 0d9570391..44ca4b7fd 100644
--- a/app/Http/Controllers/Business/DropshipFreightController.php
+++ b/app/Http/Controllers/Business/DropshipFreightController.php
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Business;
use App\Support\CourierBills\Category;
use App\Support\CourierBills\StreamingWorkbook;
+use App\Support\CourierBills\SurchargeSettlementMatcher;
use App\Support\DropshipFreight\DropshipConfirmation;
use App\Support\DropshipFreight\DropshipBillSummary;
use App\Support\DropshipFreight\DropshipExporter;
@@ -225,6 +226,26 @@ final class DropshipFreightController extends Controller
. ($result['skipped'] !== [] ? '。没加的代发仓:' . implode('、', $result['skipped']) : '') . '。请逐家核对关键字。');
}
+ public function rematch(Request $request, int $factory)
+ {
+ $data = $request->validate([
+ 'month' => ['required', 'date_format:Y-m'],
+ 'df_view' => ['nullable', 'in:overview,factory'],
+ ]);
+ $f = $this->factory($factory);
+ $state = $this->confirmations->state($data['month'], (int) $f->id, self::companyId());
+ if (($state['status'] ?? null) === DropshipConfirmation::CONFIRMED) {
+ throw ValidationException::withMessages(['dropship' => '厂家已确认的账单不能重新匹配。']);
+ }
+ $this->guard(fn () => app(SurchargeSettlementMatcher::class)->dropship((int) $f->id, $data['month']));
+
+ // 运费分类实时计算,返回页面时读取最新发货和核算数据;附加费需刷新持久化的匹配结果。
+ $view = $data['df_view'] ?? 'factory';
+ return $this->back($request, ['df_view' => $view, 'month' => $data['month'],
+ ...($view === 'factory' ? ['factory' => (int) $f->id] : [])],
+ '已按最新发货数据、核算运费和附加费重新匹配「' . $f->name . '」' . $data['month'] . ' 的账单。');
+ }
+
/** 财务点「核对完成」/「撤销」:核对完成的厂家才能推送给厂家确认。 */
public function check(Request $request, int $factory)
{
diff --git a/resources/views/business/pages/courier-payables/dropship-freight/factory.blade.php b/resources/views/business/pages/courier-payables/dropship-freight/factory.blade.php
index bcc0b0758..96b3b292b 100644
--- a/resources/views/business/pages/courier-payables/dropship-freight/factory.blade.php
+++ b/resources/views/business/pages/courier-payables/dropship-freight/factory.blade.php
@@ -19,6 +19,11 @@ $feedbackPageFor = function (string $waybill) use ($dfCurrentDisputes): int {
+
@if($version>0)
厂家版账单预览@endif
导出内部明细
diff --git a/resources/views/business/pages/courier-payables/dropship-freight/overview.blade.php b/resources/views/business/pages/courier-payables/dropship-freight/overview.blade.php
index 7334915e8..795197b87 100644
--- a/resources/views/business/pages/courier-payables/dropship-freight/overview.blade.php
+++ b/resources/views/business/pages/courier-payables/dropship-freight/overview.blade.php
@@ -72,7 +72,15 @@
@if ($other > 0){{ number_format($other) }}@else 0 @endif |
@include('business.pages.courier-payables.dropship-freight._confirm', ['fid' => $row['factory_id'], 'fname' => $row['factory'],
'state' => $dfStates[$row['factory_id']] ?? null, 'robot' => $dfRobots[$row['factory']] ?? []]) |
-
打开明细 |
+
+ 打开明细
+
+ |
@empty
| {{ $month }} 还没有上传厂家账单。先在「代发厂家设置」里建好厂家,再到「上传厂家账单」批量上传。 |
diff --git a/routes/admin.php b/routes/admin.php
index 5c8cacdff..06f1c08dc 100644
--- a/routes/admin.php
+++ b/routes/admin.php
@@ -844,6 +844,7 @@ foreach (['courier-payables', 'courier-payables-final'] as $courierBillSection)
Route::post('factories/generate', [$controller, 'generate'])->name('generate')->defaults('section', $courierBillSection);
Route::post('factories/{factory}/delete', [$controller, 'deleteFactory'])->whereNumber('factory')->name('delete-factory')->defaults('section', $courierBillSection);
Route::post('factories/{factory}/check', [$controller, 'check'])->whereNumber('factory')->name('check')->defaults('section', $courierBillSection);
+ Route::post('factories/{factory}/rematch', [$controller, 'rematch'])->whereNumber('factory')->name('rematch')->defaults('section', $courierBillSection);
Route::post('factories/{factory}/push', [$controller, 'push'])->whereNumber('factory')->name('push')->defaults('section', $courierBillSection);
Route::post('factories/{factory}/fees', [$controller, 'saveFees'])->whereNumber('factory')->name('save-fees')->defaults('section', $courierBillSection);
Route::post('notify-upload', [$controller, 'notifyUpload'])->name('notify-upload')->defaults('section', $courierBillSection);
diff --git a/tests/Feature/Business/DropshipFreightTest.php b/tests/Feature/Business/DropshipFreightTest.php
index 7126c74a9..a80723749 100644
--- a/tests/Feature/Business/DropshipFreightTest.php
+++ b/tests/Feature/Business/DropshipFreightTest.php
@@ -543,6 +543,84 @@ final class DropshipFreightTest extends FeatureTestCase
}
}
+ public function test_rematch_refreshes_current_factory_month_without_changing_source_or_manual_fees(): void
+ {
+ Storage::fake('local');
+ $this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail());
+ $this->loginAs()->withExceptionHandling();
+ $db = CourierBillMatcher::db();
+ $store = app(DropshipFreightStore::class);
+ $id = $db->table('dropship_factories')->insertGetId(['name' => '临沂卿德客', 'warehouse' => self::WAREHOUSE,
+ 'keywords' => '卿德客', 'created_at' => now(), 'updated_at' => now()]);
+ // 当前生产单家最多 71 条附加费,按这一规模覆盖重新匹配及页面返回。
+ $feeRows = [['快递单号', '附加费金额', '附加费类型']];
+ for ($i = 1; $i <= 71; $i++) $feeRows[] = [sprintf('773444%06d', $i), 4, '退回费'];
+ $file = $store->uploadFor('卿德客11月.xlsx', $this->xlsx([
+ '账单' => [['单号', '运费'], ['773444000001', 8]],
+ '附加费' => $feeRows,
+ ]), '2026-11', $id, '测试');
+ $source = $db->table('dropship_bill_lines')->where('file_id', $file)->get()->toJson();
+ $factory = collect($store->factories())->sole();
+ $this->assertSame(Category::UNMATCHED, app(DropshipReconciler::class)->factoryMonth($factory, '2026-11')['rows'][0]['status']);
+
+ $fees = app(\App\Support\DropshipFreight\DropshipFees::class);
+ $fees->save('2026-11', $id, ['booking' => 2], '保留手工备注', (int) $this->company->id, $this->user->id);
+ BillSurchargeMatchingTest::createSettlementTable();
+ for ($i = 1; $i <= 71; $i++) {
+ $waybill = sprintf('773444%06d', $i);
+ $this->ship($waybill, '卿德客申通', self::WAREHOUSE, '2026-11-03 09:00:00', $i, 6);
+ $db->table('courier_surcharge_settlement_rows')->insert(['waybill' => $waybill, 'surcharge_type' => '退回费',
+ 'courier_name' => '卿德客申通', 'settlement_amount' => 3, 'settlement_status' => '已计算', 'shipped_at' => '2026-11-03 00:00:00']);
+ }
+ $matcher = app(\App\Support\CourierBills\SurchargeSettlementMatcher::class);
+ // 其他月份的旧匹配记录不能被这次操作清除。
+ $other = (array) $matcher->query('dropship', $id, '2026-11')->first();
+ unset($other['id']);
+ $other['bill_month'] = '2026-10';
+ $other['row_no'] = 999;
+ $other['waybill_no'] = 'OTHER-MONTH';
+ $otherId = $db->table($matcher::TABLE)->insertGetId($other);
+ $otherBefore = (array) $db->table($matcher::TABLE)->find($otherId);
+
+ foreach (['courier-payables', 'courier-payables-final'] as $section) {
+ $page = route('business.pages.show', ['section' => $section, 'page' => 'dropship-freight-reconciliation',
+ 'df_view' => 'overview', 'month' => '2026-11']);
+ $url = route("business.$section.dropship-freight.rematch", ['factory' => $id]);
+ $this->get($page)->assertOk()->assertSee('data-df-rematch', false)->assertSeeText('重新匹配');
+ $this->post($url, ['month' => '2026-11', 'df_view' => 'overview'])->assertRedirect($page)->assertSessionHas('dropship_message');
+ $this->get($page)->assertOk()->assertSeeText('217.00')->assertSeeText('保留手工备注');
+ $detail = route('business.pages.show', ['section' => $section, 'page' => 'dropship-freight-reconciliation',
+ 'df_view' => 'factory', 'month' => '2026-11', 'factory' => $id]);
+ $this->post($url, ['month' => '2026-11'])->assertRedirect($detail);
+ $this->get($detail)->assertOk()->assertSee('data-df-rematch', false)->assertSeeText('一致但运费有差');
+ $this->post($url, ['month' => '2026-13'])->assertSessionHasErrors('month');
+ $this->post($url, ['month' => '2026-11', 'df_view' => 'upload'])->assertSessionHasErrors('df_view');
+ }
+ $this->assertSame(71, $matcher->query('dropship', $id, '2026-11')->count());
+ $matched = $matcher->query('dropship', $id, '2026-11')->where('waybill_no', '773444000001')->sole();
+ $this->assertSame('已匹配', $matched->match_status);
+ $this->assertEquals(3, $matched->self_amount);
+ $this->assertSame($source, $db->table('dropship_bill_lines')->where('file_id', $file)->get()->toJson());
+ $this->assertEquals($otherBefore, (array) $db->table($matcher::TABLE)->find($otherId));
+ $row = $fees->rows('2026-11', [$id => 6], (int) $this->company->id)[$id];
+ $this->assertEquals(2, $row['values']['booking']);
+ $this->assertEquals(213, $row['values']['returned']);
+ $this->assertSame('保留手工备注', $row['note']);
+ $this->post(route('business.courier-payables.dropship-freight.rematch', ['factory' => $id + 999]),
+ ['month' => '2026-11'], ['X-Requested-With' => 'XMLHttpRequest'])->assertNotFound();
+
+ $confirmed = ['month' => '2026-11', 'factory_id' => $id, 'status' => 'confirmed', 'version' => 1];
+ app(\Modules\EcommerceFinance\Services\SupplierPayableOverrideStore::class)->save(
+ \App\Support\DropshipFreight\DropshipConfirmation::DATASET, '2026-11|' . $id, $confirmed,
+ (int) $this->company->id, $this->user->id, 'test');
+ $before = $matcher->query('dropship', $id, '2026-11')->get()->toJson();
+ $this->post(route('business.courier-payables.dropship-freight.rematch', ['factory' => $id]), ['month' => '2026-11'])
+ ->assertSessionHasErrors('dropship');
+ $this->assertSame($before, $matcher->query('dropship', $id, '2026-11')->get()->toJson());
+ $this->assertSame('confirmed', app(\App\Support\DropshipFreight\DropshipConfirmation::class)
+ ->state('2026-11', $id, (int) $this->company->id)['status']);
+ }
+
public function test_keyword_helpers(): void
{
$this->assertSame('卿德客', DropshipFreightStore::warehouseCore('工厂-山东卿德客代发仓'));