From 4c038e47b10e83c2463f0ceea560ff82f4814fae Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 3 Dec 2026 13:52:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BF=AB=E9=80=92=E6=89=B9=E9=87=8F=E6=8E=A8?= =?UTF-8?q?=E9=80=81=E4=B8=8A=E4=BC=A0=E9=93=BE=E6=8E=A5=E8=B7=B3=E8=BF=87?= =?UTF-8?q?=E5=B7=B2=E6=8E=A8/=E5=B7=B2=E6=9C=89=E8=B4=A6=E5=8D=95?= =?UTF-8?q?=EF=BC=9B=E4=BB=A3=E5=8F=91=E5=B0=8F=E6=B1=87=E6=80=BB=E9=99=84?= =?UTF-8?q?=E5=8A=A0=E8=B4=B9=E8=A1=A5=E8=AF=BB=E8=80=81=E6=96=87=E4=BB=B6?= =?UTF-8?q?=EF=BC=88=E9=87=8D=E5=81=9A=E8=A1=A5=E4=B8=81=2035=E3=80=8136?= =?UTF-8?q?=EF=BC=8C=E5=9F=BA=E4=BA=8E=20main=2075cba0dde938=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 批量「推送上传链接到快递群」:这个月已推过、已有账单的分组跳过;对账分组里单独点照发。 推送记录按月存 supplier_payable_overrides(courier-upload-link-pushed:YYYY-MM),无迁移。 - 代发:之前入库的厂家账单 sheets 里没记附加费,导出小汇总和各厂家汇总时现读原文件补记一次;表名带「附加费」都认。 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UNTa9mVziToe5w52mmrrar --- .../Business/CourierBillController.php | 15 +++++ .../CourierBills/CourierUploadPushes.php | 34 ++++++++++ .../CourierBills/FreightBillTemplate.php | 2 +- .../DropshipFreight/DropshipExporter.php | 11 ++- app/Support/DropshipFreight/DropshipFees.php | 18 +++-- .../DropshipFreight/DropshipFreightStore.php | 67 +++++++++++++++---- .../courier-bills/upload.blade.php | 4 +- .../Feature/Business/DropshipFreightTest.php | 6 ++ .../Business/PortalFreightBillTest.php | 15 +++++ 9 files changed, 139 insertions(+), 33 deletions(-) create mode 100644 app/Support/CourierBills/CourierUploadPushes.php diff --git a/app/Http/Controllers/Business/CourierBillController.php b/app/Http/Controllers/Business/CourierBillController.php index 2733f0bd7..d7fd754ac 100644 --- a/app/Http/Controllers/Business/CourierBillController.php +++ b/app/Http/Controllers/Business/CourierBillController.php @@ -511,7 +511,19 @@ final class CourierBillController extends Controller $companyId = (int) (company_id() ?: 1); $sent = $failed = []; $skipped = 0; + // 批量推送:这个月已经推过、已经有账单的跳过;单独推某一家照发 + $pushes = app(\App\Support\CourierBills\CourierUploadPushes::class); + $done = empty($data['group']) ? $pushes->done($data['month'], $companyId) : ['pushed' => [], 'uploaded' => []]; + $alreadyPushed = $alreadyUploaded = 0; foreach ($groups as $g) { + if (isset($done['uploaded'][(int) $g->id])) { + $alreadyUploaded++; + continue; + } + if (isset($done['pushed'][(int) $g->id])) { + $alreadyPushed++; + continue; + } if (! isset($configured[$g->name])) { $skipped++; continue; @@ -520,6 +532,7 @@ final class CourierBillController extends Controller $notifier->pushCourierUploadLink((string) $g->name, $data['month'], app(\App\Support\CourierBills\CourierUploadLink::class)->pageUrl($companyId, (int) $g->id, $data['month'])); $sent[] = $g->name; + $pushes->record($data['month'], (int) $g->id, $companyId, user()?->id); } catch (Throwable $error) { report($error); $failed[] = $g->name . '(发送失败,请检查群机器人配置)'; @@ -531,6 +544,8 @@ final class CourierBillController extends Controller return $this->back($request, [], '已推送 ' . $data['month'] . ' 上传链接 ' . count($sent) . ' 家' . ($sent !== [] ? ':' . implode('、', array_slice($sent, 0, 30)) : '') . ($failed !== [] ? ';失败 ' . count($failed) . ' 家:' . implode('、', $failed) : '') + . ($alreadyUploaded > 0 ? ';' . $alreadyUploaded . ' 家这个月已经有账单,没再推' : '') + . ($alreadyPushed > 0 ? ';' . $alreadyPushed . ' 家这个月已经推过,没再推(要重发到「对账分组」里单独点)' : '') . ($skipped > 0 && empty($data['group']) ? ';' . $skipped . ' 家没配钉钉群机器人,没推' : '') . '。'); } diff --git a/app/Support/CourierBills/CourierUploadPushes.php b/app/Support/CourierBills/CourierUploadPushes.php new file mode 100644 index 000000000..7e1ba559d --- /dev/null +++ b/app/Support/CourierBills/CourierUploadPushes.php @@ -0,0 +1,34 @@ +,uploaded:array} 这个月已推过 / 已有账单的分组 id */ + public function done(string $month, int $companyId): array + { + $pushed = []; + foreach ($this->store->listRows(self::DATASET . $month, $companyId) as $row) { + $pushed[(int) $row['_action_key']] = true; + } + $uploaded = CourierBillMatcher::db()->table('courier_bill_statements')->where('bill_month', $month)->where('line_count', '>', 0) + ->distinct()->pluck('group_id')->mapWithKeys(static fn ($id): array => [(int) $id => true])->all(); + + return ['pushed' => $pushed, 'uploaded' => $uploaded]; + } + + public function record(string $month, int $groupId, int $companyId, ?int $userId): void + { + $this->store->save(self::DATASET . $month, (string) $groupId, ['pushed_at' => now()->toDateTimeString()], $companyId, $userId, 'courier_upload_link'); + } +} diff --git a/app/Support/CourierBills/FreightBillTemplate.php b/app/Support/CourierBills/FreightBillTemplate.php index 0b5a15c7d..3c3d2b3eb 100644 --- a/app/Support/CourierBills/FreightBillTemplate.php +++ b/app/Support/CourierBills/FreightBillTemplate.php @@ -81,7 +81,7 @@ final class FreightBillTemplate $result = []; try { foreach ($this->reader->sheets($path, $extension) as $name => $rows) { - if (trim((string) $name) !== self::SURCHARGE_SHEET) { + if (! str_contains(trim((string) $name), self::SURCHARGE_SHEET)) { // 「附加费」「厂家账单-附加费」都认 continue; } $header = null; diff --git a/app/Support/DropshipFreight/DropshipExporter.php b/app/Support/DropshipFreight/DropshipExporter.php index 87322690e..66db365a2 100644 --- a/app/Support/DropshipFreight/DropshipExporter.php +++ b/app/Support/DropshipFreight/DropshipExporter.php @@ -178,12 +178,11 @@ final class DropshipExporter $fee = 0.0; $hasFee = false; foreach ($this->store->db()->table('dropship_bill_files')->where('factory_id', $factory->id)->where('bill_month', $month) - ->where('status', 'parsed')->pluck('sheets') as $sheets) { - foreach ((array) json_decode((string) $sheets, true) as $sheet) { - if (! empty($sheet['fees'])) { - $fee += array_sum(array_map('floatval', (array) $sheet['fees'])); - $hasFee = true; - } + ->where('status', 'parsed')->get(['id', 'stored_path', 'sheets']) as $file) { + $fees = $this->store->feesFor($file); + if ($fees !== []) { + $fee += array_sum($fees); + $hasFee = true; } } // 上月手工汇总里的分类行(快递不符、未发出、跨月订单-X月……)这个月没有就不留空行 diff --git a/app/Support/DropshipFreight/DropshipFees.php b/app/Support/DropshipFreight/DropshipFees.php index 736eeca99..6072126cb 100644 --- a/app/Support/DropshipFreight/DropshipFees.php +++ b/app/Support/DropshipFreight/DropshipFees.php @@ -113,17 +113,15 @@ final class DropshipFees { $result = []; $files = $this->store->db()->table('dropship_bill_files')->where('bill_month', $month)->where('status', 'parsed') - ->whereNotNull('factory_id')->get(['factory_id', 'sheets']); + ->whereNotNull('factory_id')->get(['id', 'factory_id', 'stored_path', 'sheets']); foreach ($files as $file) { - foreach ((array) json_decode((string) $file->sheets, true) as $sheet) { - foreach ((array) ($sheet['fees'] ?? []) as $type => $amount) { - $id = (int) $file->factory_id; - $key = self::classify((string) $type); - if ($key === null) { - $result[$id]['other'][(string) $type] = round(($result[$id]['other'][(string) $type] ?? 0) + (float) $amount, 2); - } else { - $result[$id]['fees'][$key] = round(($result[$id]['fees'][$key] ?? 0) + (float) $amount, 2); - } + foreach ($this->store->feesFor($file) as $type => $amount) { + $id = (int) $file->factory_id; + $key = self::classify((string) $type); + if ($key === null) { + $result[$id]['other'][(string) $type] = round(($result[$id]['other'][(string) $type] ?? 0) + (float) $amount, 2); + } else { + $result[$id]['fees'][$key] = round(($result[$id]['fees'][$key] ?? 0) + (float) $amount, 2); } } } diff --git a/app/Support/DropshipFreight/DropshipFreightStore.php b/app/Support/DropshipFreight/DropshipFreightStore.php index a6050a920..3ab8ad4f0 100644 --- a/app/Support/DropshipFreight/DropshipFreightStore.php +++ b/app/Support/DropshipFreight/DropshipFreightStore.php @@ -32,6 +32,58 @@ final class DropshipFreightStore private readonly CourierBillStore $courierBills, ) {} + /** + * 这份账单「附加费」表按类型的金额(类型 => 金额)。入库时记在 sheets 里;之前入库的老文件现读原文件补记一次。 + * + * @return array + */ + public function feesFor(object $file): array + { + $sheets = (array) json_decode((string) ($file->sheets ?? ''), true); + $surcharge = array_filter($sheets, static fn ($sheet): bool => is_array($sheet) + && str_contains((string) ($sheet['sheet'] ?? ''), FreightBillTemplate::SURCHARGE_SHEET)); + if ($surcharge === []) { + return []; // sheets 里列着原文件每张表,没有附加费表就不用读文件 + } + $fees = []; + if (array_filter($surcharge, static fn (array $sheet): bool => array_key_exists('fees', $sheet)) !== []) { + foreach ($surcharge as $sheet) { + foreach ((array) ($sheet['fees'] ?? []) as $type => $amount) { + $fees[(string) $type] = round(($fees[(string) $type] ?? 0) + (float) $amount, 2); + } + } + + return $fees; + } + if (empty($file->stored_path) || ! Storage::disk(self::DISK)->exists((string) $file->stored_path)) { + return []; + } + $fees = (new FreightBillTemplate(new BillWorkbookReader()))->surchargesByType(Storage::disk(self::DISK)->path((string) $file->stored_path), + pathinfo((string) $file->stored_path, PATHINFO_EXTENSION)); + if (! empty($file->id)) { + $this->db()->table('dropship_bill_files')->where('id', $file->id) + ->update(['sheets' => json_encode(self::withFees($sheets, $fees), JSON_UNESCAPED_UNICODE)]); + } + + return $fees; + } + + /** 把附加费金额记到 sheets 里「附加费」那张表的记录上(没有附加费也记个空的,下次不用再读文件);不新增记录,导出按记录数给原表起名。 */ + private static function withFees(array $sheets, array $fees): array + { + $note = $fees === [] ? '没有附加费' : '附加费按类型记入各厂家汇总和小汇总'; + $done = false; + foreach ($sheets as $i => $sheet) { + if (! $done && is_array($sheet) && str_contains((string) ($sheet['sheet'] ?? ''), FreightBillTemplate::SURCHARGE_SHEET)) { + $sheets[$i]['fees'] = $fees; + $sheets[$i]['note'] = $note; + $done = true; + } + } + + return $sheets; + } + public function db(): ConnectionInterface { return DB::connection('business_mirror'); @@ -335,20 +387,7 @@ final class DropshipFreightStore // 按上传模板填的「附加费」表:按类型记下金额,各厂家汇总里自动带入退回费、拦截费等列 $fees = (new FreightBillTemplate(new BillWorkbookReader()))->surchargesByType(Storage::disk(self::DISK)->path($file->stored_path), pathinfo($file->stored_path, PATHINFO_EXTENSION)); - if ($fees !== []) { - $found = false; - foreach ($result['sheets'] as &$sheet) { - if ($sheet['sheet'] === FreightBillTemplate::SURCHARGE_SHEET) { - $sheet['fees'] = $fees; - $sheet['note'] = '附加费按类型记入各厂家汇总'; - $found = true; - } - } - unset($sheet); - if (! $found) { - $result['sheets'][] = ['sheet' => FreightBillTemplate::SURCHARGE_SHEET, 'read' => false, 'note' => '附加费按类型记入各厂家汇总', 'fees' => $fees]; - } - } + $result['sheets'] = self::withFees($result['sheets'], $fees); $notes = []; if ($lines === []) { $notes[] = '没读到厂家运费:要有一张表同时有单号列(快递单号/运单号/单号…)和运费列(快递运费/运费/快递费…),且运费不为空'; diff --git a/resources/views/business/pages/courier-payables/courier-bills/upload.blade.php b/resources/views/business/pages/courier-payables/courier-bills/upload.blade.php index 50ea45d8c..600b900f3 100644 --- a/resources/views/business/pages/courier-payables/courier-bills/upload.blade.php +++ b/resources/views/business/pages/courier-payables/courier-bills/upload.blade.php @@ -1,11 +1,11 @@

让快递自己上传账单

+ onsubmit="return confirm('往配了钉钉对账群机器人、这个月还没推过也还没有账单的快递群推送上传链接?')"> @csrf - 对账分组已复用工作平台「应付群机器人配置」中对应快递的群机器人;未匹配的可在「对账分组」里补充。这里一键往已配置的群推本月的免登录上传链接。 + 对账分组已复用工作平台「应付群机器人配置」中对应快递的群机器人;未匹配的可在「对账分组」里补充。这里一键往已配置的群推本月的免登录上传链接;这个月已经推过、已经有账单的不再推(要重发某一家,到「对账分组」里单独点)。 快递点开链接、下载模板(单号、省、重量、运费、快递名称、面单、费率)填好上传,后台校验读入后进入对账;链接只能传那一家那个月,30 天有效。已有 {{ collect($cbRobots ?? [])->where('configured', true)->count() }} 家配了机器人。
diff --git a/tests/Feature/Business/DropshipFreightTest.php b/tests/Feature/Business/DropshipFreightTest.php index a97a546be..47c2162a1 100644 --- a/tests/Feature/Business/DropshipFreightTest.php +++ b/tests/Feature/Business/DropshipFreightTest.php @@ -424,6 +424,9 @@ final class DropshipFreightTest extends FeatureTestCase // 「拦截退回」算拦截费 $this->assertSame('intercept', \App\Support\DropshipFreight\DropshipFees::classify('拦截退回')); + // 补丁前入库的老文件 sheets 里没记附加费:导出时现读原文件补记 + $sheets = collect(json_decode((string) $db->table('dropship_bill_files')->value('sheets'), true))->map(fn (array $x) => \Illuminate\Support\Arr::except($x, 'fees'))->all(); + $db->table('dropship_bill_files')->update(['sheets' => json_encode($sheets, JSON_UNESCAPED_UNICODE)]); // 导出的小汇总:附加费表金额合计放「厂家账单-附加费」一行,简尚核对空着 $out = tempnam(sys_get_temp_dir(), 'dropship_export_'); $handle = fopen($out, 'wb'); @@ -443,6 +446,9 @@ final class DropshipFreightTest extends FeatureTestCase $this->assertEquals(6.5, $sheet->getCell('D5')->getCalculatedValue()); $book->disconnectWorksheets(); @unlink($out); + $file = $db->table('dropship_bill_files')->first(); + $this->assertStringContainsString('"fees"', (string) $file->sheets); + $this->assertSame(['退回费' => 3.0, '拦截费' => 2.0, '上楼费' => 1.5], app(DropshipFreightStore::class)->feesFor($file)); } public function test_export_keeps_uncalculated_settlement_blank(): void diff --git a/tests/Feature/Business/PortalFreightBillTest.php b/tests/Feature/Business/PortalFreightBillTest.php index 86f2feeec..39c2aa2a0 100644 --- a/tests/Feature/Business/PortalFreightBillTest.php +++ b/tests/Feature/Business/PortalFreightBillTest.php @@ -288,6 +288,21 @@ final class PortalFreightBillTest extends FeatureTestCase $this->assertStringContainsString('运费账单上传 · 曹县韵达', $text); preg_match('/\[打开账单上传页\]\(([^)]+)\)/', $text, $m); $link = $m[1]; + // 再点批量推送:这个月已经推过的不再推;单独点这一家照发 + $count = count(Http::recorded()); + $this->post($action('push-upload-links'), ['month' => '2026-11']) + ->assertSessionHas('courier_bills_message', fn (string $m): bool => str_contains($m, '已推送 2026-11 上传链接 0 家') && str_contains($m, '1 家这个月已经推过')); + $this->assertCount($count, Http::recorded()); + $this->post($action('push-upload-links'), ['month' => '2026-11', 'group' => $groupId])->assertSessionHas('courier_bills_message'); + $this->assertCount($count + 1, Http::recorded()); + // 别的月份没推过,照推;这个月已经有账单的也跳过 + $db->table('courier_bill_statements')->insert(['group_id' => $groupId, 'bill_month' => '2026-09', 'line_count' => 3, 'created_at' => now(), 'updated_at' => now()]); + $this->post($action('push-upload-links'), ['month' => '2026-09']) + ->assertSessionHas('courier_bills_message', fn (string $m): bool => str_contains($m, '1 家这个月已经有账单')); + $this->post($action('push-upload-links'), ['month' => '2026-08']) + ->assertSessionHas('courier_bills_message', fn (string $m): bool => str_contains($m, '上传链接 1 家')); + $this->assertCount($count + 2, Http::recorded()); + $db->table('courier_bill_statements')->where('bill_month', '2026-09')->delete(); auth()->logout(); $this->get($link . '&month=2026-10')->assertOk()->assertSeeText('2026年11月'); -- 2.47.2