diff --git a/app/Http/Controllers/Business/DropshipFreightController.php b/app/Http/Controllers/Business/DropshipFreightController.php index f44c1ed3f..0f3260b19 100644 --- a/app/Http/Controllers/Business/DropshipFreightController.php +++ b/app/Http/Controllers/Business/DropshipFreightController.php @@ -233,6 +233,52 @@ final class DropshipFreightController extends Controller return $this->back($request, [], '已推送到「' . $f->name . '」钉钉群(第 ' . $version . ' 次),等厂家在确认页确认。'); } + /** + * 通知厂家上传代发运费账单:传 factory 只通知这一家;不传就通知这个月还没上传、且配了钉钉群机器人的全部厂家。 + */ + public function notifyUpload(Request $request) + { + $data = $request->validate(['month' => ['required', 'date_format:Y-m'], 'factory' => ['nullable', 'integer']]); + $month = $data['month']; + $notifier = app(SupplierDingTalkNotifier::class); + $factories = collect($this->store->factories()); + $single = ! empty($data['factory']); + if ($single) { + $factories = $factories->where('id', (int) $data['factory'])->values(); + abort_if($factories->isEmpty(), 404); + } + $uploaded = array_flip($this->store->db()->table('dropship_bill_files')->where('bill_month', $month)->whereNotNull('factory_id') + ->distinct()->pluck('factory_id')->map('intval')->all()); + $robots = $notifier->configurationMap($factories->pluck('name')->map('strval')->all()); + $portal = route('business.supplier-portal.home', ['company_id' => self::companyId()]); + $sent = $failed = []; + $noRobot = $done = 0; + foreach ($factories as $f) { + if (! $single && isset($uploaded[(int) $f->id])) { + $done++; + continue; + } + if (! ($robots[$f->name]['configured'] ?? false)) { + $noRobot++; + continue; + } + try { + $notifier->pushDropshipUploadReminder((string) $f->name, $month, $portal); + $sent[] = $f->name; + } catch (Throwable $error) { + report($error); + $failed[] = $f->name; + } + } + if ($single && $noRobot > 0) { + throw ValidationException::withMessages(['dropship' => '这家还没有配置钉钉群机器人(在供应商对账里配置,和货款对账同一个群)。']); + } + + return $this->back($request, [], '已通知 ' . count($sent) . ' 家上传 ' . $month . ' 代发运费账单' . ($sent !== [] ? ':' . implode('、', array_slice($sent, 0, 40)) : '') + . ($done > 0 ? ';' . $done . ' 家已经上传,没再通知' : '') . ($noRobot > 0 ? ';' . $noRobot . ' 家没配钉钉群机器人' : '') + . ($failed !== [] ? ';发送失败:' . implode('、', $failed) : '') . '。'); + } + /** 厂家开了运费、但不是「一致」的单:不结算或运费有差,确认页上请厂家重点看。 */ public static function attention(array $rows): int { diff --git a/modules/EcommerceFinance/Services/SupplierDingTalkNotifier.php b/modules/EcommerceFinance/Services/SupplierDingTalkNotifier.php index aff81c0e5..49791461d 100644 --- a/modules/EcommerceFinance/Services/SupplierDingTalkNotifier.php +++ b/modules/EcommerceFinance/Services/SupplierDingTalkNotifier.php @@ -505,6 +505,23 @@ class SupplierDingTalkNotifier return true; } + /** 代发运费:提醒厂家到厂家对账入口(钉钉登录)上传这个月的代发运费账单。 */ + public function pushDropshipUploadReminder(string $supplier, string $month, string $portalUrl): void + { + [$webhook, $config] = $this->robotFor($supplier, 'dropship_upload_reminder'); + $this->send($webhook, trim((string) ($config['secret'] ?? '')), (bool) ($config['at_all'] ?? false), + $supplier . ' · 代发运费账单上传提醒 · ' . $this->displayMonth($month), implode("\n", [ + '### 代发运费账单上传提醒 · ' . $supplier, + '- 请上传 **' . $this->displayMonth($month) . '** 代发运费账单', + '- 按上传模板填写(单号、省、重量、运费、送货备注、面单、费率),单号和运费必填', + '', + '#### [打开厂家对账入口](' . $portalUrl . ')', + '- 钉钉登录后,在「上传运费账单」里选「代发运费账单」,可先下载上传模板。', + '- 上传后群里会收到回执;如无法登录或上传,请在群里联系财务。', + ])); + $this->touch($supplier, 1); + } + /** 快递账单:往这家快递的钉钉对账群推免登录上传链接(机器人在快递账单对账 → 对账分组里配置)。 */ public function pushCourierUploadLink(string $courier, string $month, string $url): void { diff --git a/resources/views/business/pages/courier-payables/dropship-freight/factories.blade.php b/resources/views/business/pages/courier-payables/dropship-freight/factories.blade.php index 3fac89da3..2d0c28581 100644 --- a/resources/views/business/pages/courier-payables/dropship-freight/factories.blade.php +++ b/resources/views/business/pages/courier-payables/dropship-freight/factories.blade.php @@ -34,6 +34,10 @@ @php $editData = json_encode(['id' => $f->id, 'name' => $f->name, 'warehouse' => $f->warehouse, 'keywords' => $f->keywords, 'note' => $f->note], JSON_UNESCAPED_UNICODE); @endphp +
@csrf + +
@csrf
diff --git a/resources/views/business/pages/courier-payables/dropship-freight/upload.blade.php b/resources/views/business/pages/courier-payables/dropship-freight/upload.blade.php index b2937e6c3..c731568ea 100644 --- a/resources/views/business/pages/courier-payables/dropship-freight/upload.blade.php +++ b/resources/views/business/pages/courier-payables/dropship-freight/upload.blade.php @@ -1,3 +1,14 @@ +
+

通知厂家上传

+
+ @csrf + + + 只通知这个月还没上传、且配了钉钉群机器人(供应商对账里配置,和货款对账同一个群)的厂家,已上传的不打扰。群里收到厂家对账入口链接,厂家钉钉登录后在「上传运费账单」里按模板上传。 +
+
+

上传厂家账单 · {{ $month }}

diff --git a/routes/admin.php b/routes/admin.php index 14f310d1b..393bf002b 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -822,6 +822,7 @@ foreach (['courier-payables', 'courier-payables-final'] as $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}/push', [$controller, 'push'])->whereNumber('factory')->name('push')->defaults('section', $courierBillSection); + Route::post('notify-upload', [$controller, 'notifyUpload'])->name('notify-upload')->defaults('section', $courierBillSection); Route::get('export', [$controller, 'export'])->name('export')->defaults('section', $courierBillSection); }); diff --git a/tests/Feature/Business/DropshipFreightConfirmTest.php b/tests/Feature/Business/DropshipFreightConfirmTest.php index 94eccff10..60a9621da 100644 --- a/tests/Feature/Business/DropshipFreightConfirmTest.php +++ b/tests/Feature/Business/DropshipFreightConfirmTest.php @@ -131,6 +131,43 @@ final class DropshipFreightConfirmTest extends FeatureTestCase $this->travelBack(); } + public function test_notify_factories_that_have_not_uploaded_this_month(): void + { + Storage::fake('local'); + $this->company->makeCurrent(true); + $this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail()); + $this->loginAs()->withExceptionHandling(); + $db = CourierBillMatcher::db(); + foreach (['临沂卿德客' => '工厂-山东卿德客代发仓', '河北丽彪' => '工厂-河北丽彪代发仓', '没配群' => '工厂-没配群代发仓'] as $name => $wh) { + $db->table('dropship_factories')->insert(['name' => $name, 'warehouse' => $wh, 'keywords' => mb_substr($name, -2), + 'created_at' => now(), 'updated_at' => now()]); + } + $notifier = app(SupplierDingTalkNotifier::class); + $notifier->save('临沂卿德客', 'https://oapi.dingtalk.com/robot/send?access_token=a', '', false); + $notifier->save('河北丽彪', 'https://oapi.dingtalk.com/robot/send?access_token=b', '', false); + $db->table('dropship_bill_files')->insert(['bill_month' => '2026-11', 'factory_id' => $db->table('dropship_factories')->where('name', '河北丽彪')->value('id'), + 'original_name' => 'x.xlsx', 'stored_path' => 'x', 'status' => 'parsed', 'created_at' => now(), 'updated_at' => now()]); + Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]); + $url = route('business.' . self::SECTION . '.dropship-freight.notify-upload'); + + $this->post($url, ['month' => '2026-11'])->assertSessionHas('dropship_message', '已通知 1 家上传 2026-11 代发运费账单:临沂卿德客;1 家已经上传,没再通知;1 家没配钉钉群机器人。'); + $this->assertCount(1, Http::recorded()); + $text = (string) (collect(Http::recorded())->last()[0]['markdown']['text'] ?? ''); + $this->assertStringContainsString('代发运费账单上传提醒 · 临沂卿德客', $text); + $this->assertStringContainsString('请上传 **2026年11月** 代发运费账单', $text); + $this->assertStringContainsString('[打开厂家对账入口](', $text); + // 单独通知一家:已上传也照发;没配机器人报错 + $this->post($url, ['month' => '2026-11', 'factory' => $db->table('dropship_factories')->where('name', '河北丽彪')->value('id')]) + ->assertSessionHas('dropship_message'); + $this->assertCount(2, Http::recorded()); + $this->post($url, ['month' => '2026-11', 'factory' => $db->table('dropship_factories')->where('name', '没配群')->value('id')]) + ->assertSessionHasErrors('dropship'); + $this->get(route('business.pages.show', ['section' => self::SECTION, 'page' => 'dropship-freight-reconciliation', 'df_view' => 'factories', 'month' => '2026-11'])) + ->assertOk()->assertSeeText('通知上传'); + $this->get(route('business.pages.show', ['section' => self::SECTION, 'page' => 'dropship-freight-reconciliation', 'df_view' => 'upload', 'month' => '2026-11'])) + ->assertOk()->assertSeeText('通知厂家上传'); + } + private function shipping() { return app(ShippingLookup::class)->connection();