feat(代发运费): 通知厂家上传账单

This commit is contained in:
limeng 2026-12-02 21:26:50 +08:00
parent 2dca095bff
commit 8e1135bc59
6 changed files with 116 additions and 0 deletions

View File

@ -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
{

View File

@ -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
{

View File

@ -34,6 +34,10 @@
<td>
@php $editData = json_encode(['id' => $f->id, 'name' => $f->name, 'warehouse' => $f->warehouse, 'keywords' => $f->keywords, 'note' => $f->note], JSON_UNESCAPED_UNICODE); @endphp
<button type="button" data-df-edit="{{ $editData }}">修改</button>
<form method="POST" action="{{ $action('notify-upload') }}" style="display:inline"
onsubmit="return confirm('通知「{{ $f->name }}」上传 {{ $month }} 代发运费账单?')">@csrf
<input type="hidden" name="month" value="{{ $month }}"><input type="hidden" name="factory" value="{{ $f->id }}">
<button type="submit">通知上传</button></form>
<form method="POST" action="{{ $action('delete-factory', ['factory' => $f->id]) }}" onsubmit="return confirm('删除厂家「{{ $f->name }}」?')" style="display:inline">@csrf<button type="submit" class="cbr-danger">删除</button></form>
</td>
</tr>

View File

@ -1,3 +1,14 @@
<section class="cbr-panel" aria-label="通知厂家上传">
<h3>通知厂家上传</h3>
<form method="POST" action="{{ $action('notify-upload') }}" class="cbr-panel-body cbr-form"
onsubmit="return confirm('往这个月还没上传代发运费账单的厂家钉钉群发上传提醒?')">
@csrf
<label>账单月份<input type="month" name="month" value="{{ $month }}" required></label>
<button type="submit" class="cbr-primary">通知厂家上传</button>
<span class="cbr-muted">只通知这个月还没上传、且配了钉钉群机器人(供应商对账里配置,和货款对账同一个群)的厂家,已上传的不打扰。群里收到厂家对账入口链接,厂家钉钉登录后在「上传运费账单」里按模板上传。</span>
</form>
</section>
<section class="cbr-panel" aria-label="上传厂家账单">
<h3>上传厂家账单 · {{ $month }}</h3>
<form method="POST" action="{{ $action('upload') }}" enctype="multipart/form-data" class="cbr-panel-body" style="display:grid;gap:12px">

View File

@ -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);
});

View File

@ -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();