feat: 厂家上传对账单后发送钉钉回执 (#940)
厂家通过钉钉或厂家门户上传对账单后,向对应供应商机器人发送上传回执。 Co-authored-by: 苏木 <594326199@qq.com> Co-committed-by: 苏木 <594326199@qq.com>
This commit is contained in:
parent
a15711645b
commit
533d25783f
@ -53,6 +53,8 @@ class SupplierReconciliation extends Controller
|
||||
'import' => ['required', 'file', 'mimes:xlsx,xls,csv', 'max:51200'],
|
||||
]);
|
||||
|
||||
$supplierPortalUpload = $request->user()
|
||||
&& SupplierPortalAccess::isSupplierUser($request->user());
|
||||
$supplier = $this->effectiveSupplier($request);
|
||||
|
||||
$records = $this->template->parse($request->file('import'), $supplier);
|
||||
@ -64,7 +66,24 @@ class SupplierReconciliation extends Controller
|
||||
user_id() ? (int) user_id() : null,
|
||||
);
|
||||
|
||||
$message = "对账单已保存:{$supplier} 共 {$rows} 行。";
|
||||
$message = "对账单已保存:{$supplier} 共 {$rows} 行";
|
||||
$receiptNotice = '';
|
||||
if ($supplierPortalUpload) {
|
||||
try {
|
||||
$receiptSent = $this->dingTalkNotifier->sendStatementUploadReceipt(
|
||||
$supplier,
|
||||
(string) $request->file('import')->getClientOriginalName(),
|
||||
$records,
|
||||
);
|
||||
if ($receiptSent) {
|
||||
$receiptNotice = ';上传回执已发送到该厂家钉钉群。';
|
||||
}
|
||||
} catch (Throwable $exception) {
|
||||
report($exception);
|
||||
$receiptNotice = ';上传回执发送失败,对账单已正常保存。';
|
||||
}
|
||||
}
|
||||
$message .= $receiptNotice !== '' ? $receiptNotice : '。';
|
||||
|
||||
return $request->expectsJson()
|
||||
? response()->json(['message' => $message, 'rows' => $rows])
|
||||
|
||||
@ -8,6 +8,7 @@ use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Illuminate\View\View;
|
||||
use Modules\EcommerceFinance\Services\SupplierDingTalkNotifier;
|
||||
use Modules\EcommerceFinance\Services\SupplierReturnCsvExporter;
|
||||
use Modules\EcommerceFinance\Services\SupplierReturnConfirmation;
|
||||
use Modules\EcommerceFinance\Services\SupplierStatementStore;
|
||||
@ -15,6 +16,7 @@ use Modules\EcommerceFinance\Services\SupplierStatementTemplate;
|
||||
use Modules\EcommerceFinance\View\SupplierReconciliationPageData;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* 厂家门户:账号绑定供应商后,仅可查看和操作自己的对账数据。
|
||||
@ -27,6 +29,7 @@ class SupplierReconciliation extends Controller
|
||||
private readonly SupplierReturnConfirmation $returnConfirmations,
|
||||
private readonly SupplierReconciliationPageData $pageData,
|
||||
private readonly SupplierReturnCsvExporter $returnCsvExporter,
|
||||
private readonly SupplierDingTalkNotifier $dingTalkNotifier,
|
||||
) {
|
||||
}
|
||||
|
||||
@ -70,7 +73,22 @@ class SupplierReconciliation extends Controller
|
||||
$companyId,
|
||||
user_id() ? (int) user_id() : null,
|
||||
);
|
||||
$message = "对账单已保存:{$supplier} 共 {$rows} 行。";
|
||||
$message = "对账单已保存:{$supplier} 共 {$rows} 行";
|
||||
$receiptNotice = '';
|
||||
try {
|
||||
$receiptSent = $this->dingTalkNotifier->sendStatementUploadReceipt(
|
||||
$supplier,
|
||||
(string) $request->file('import')->getClientOriginalName(),
|
||||
$records,
|
||||
);
|
||||
if ($receiptSent) {
|
||||
$receiptNotice = ';上传回执已发送到该厂家钉钉群。';
|
||||
}
|
||||
} catch (Throwable $exception) {
|
||||
report($exception);
|
||||
$receiptNotice = ';上传回执发送失败,对账单已正常保存。';
|
||||
}
|
||||
$message .= $receiptNotice !== '' ? $receiptNotice : '。';
|
||||
|
||||
return $request->expectsJson()
|
||||
? response()->json(['message' => $message, 'rows' => $rows])
|
||||
|
||||
@ -225,6 +225,69 @@ class SupplierDingTalkNotifier
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @param list<array<string,mixed>> $records */
|
||||
public function sendStatementUploadReceipt(string $supplier, string $fileName, array $records): bool
|
||||
{
|
||||
$configs = $this->configs();
|
||||
$config = is_array($configs[$supplier] ?? null) ? $configs[$supplier] : [];
|
||||
$webhook = trim((string) ($config['webhook'] ?? ''));
|
||||
if ($webhook === '') {
|
||||
return false;
|
||||
}
|
||||
$this->assertWebhook($webhook);
|
||||
|
||||
$month = $this->statementMonths($records);
|
||||
$fileName = trim((string) preg_replace('/[\r\n`]+/u', ' ', basename($fileName)));
|
||||
$fileName = mb_strimwidth($fileName, 0, 120, '…', 'UTF-8');
|
||||
if ($fileName === '') {
|
||||
$fileName = '未命名文件';
|
||||
}
|
||||
|
||||
$lines = [
|
||||
'### 供应商对账单上传回执 · ' . $supplier,
|
||||
'- 对账月份:**' . $month . '**',
|
||||
'- 上传文件:' . $fileName,
|
||||
'- 上传行数:**' . count($records) . '** 行',
|
||||
'- 上传时间:' . $this->platformTimestamp(),
|
||||
'- 数据状态:**已进入财务核对**',
|
||||
];
|
||||
$title = $supplier . ' · 对账单上传回执';
|
||||
if (! str_contains($month, '、') && $month !== '未识别') {
|
||||
$title .= ' · ' . $month;
|
||||
}
|
||||
|
||||
$this->send(
|
||||
$webhook,
|
||||
trim((string) ($config['secret'] ?? '')),
|
||||
(bool) ($config['at_all'] ?? false),
|
||||
$title,
|
||||
implode("\n", $lines),
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @param list<array<string,mixed>> $records */
|
||||
private function statementMonths(array $records): string
|
||||
{
|
||||
$months = [];
|
||||
foreach ($records as $record) {
|
||||
$date = trim((string) ($record['createdAt'] ?? ''));
|
||||
if (preg_match('/^(\d{4}-\d{2})-/', $date, $matches) === 1) {
|
||||
$months[$matches[1]] = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($months === []) {
|
||||
return '未识别';
|
||||
}
|
||||
|
||||
$months = array_keys($months);
|
||||
sort($months);
|
||||
|
||||
return implode('、', $months);
|
||||
}
|
||||
|
||||
private function platformTimestamp(): string
|
||||
{
|
||||
$timezone = trim((string) config('app.platform_timezone', ''));
|
||||
|
||||
@ -12,8 +12,10 @@ use App\Models\Auth\User;
|
||||
use App\Models\Common\Contact;
|
||||
use App\Services\Auth\SupplierPortalAccess;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Modules\EcommerceFinance\Services\SupplierDingTalkNotifier;
|
||||
use Modules\EcommerceFinance\Services\SupplierPayableOverrideStore;
|
||||
use Modules\EcommerceFinance\Services\SupplierStatementMatcher;
|
||||
use Modules\EcommerceFinance\Services\SupplierStatementStore;
|
||||
@ -530,6 +532,122 @@ class SupplierReconciliationTest extends FeatureTestCase
|
||||
$this->assertCount(0, app(SupplierStatementStore::class)->rows('鸣飞家具', $companyId));
|
||||
}
|
||||
|
||||
public function test_supplier_statement_upload_sends_a_dingtalk_receipt(): void
|
||||
{
|
||||
$supplierUser = $this->createSupplierPortalUser('晨丰');
|
||||
app(SupplierDingTalkNotifier::class)->save(
|
||||
'晨丰',
|
||||
'https://oapi.dingtalk.com/robot/send?access_token=statement-upload-test',
|
||||
'',
|
||||
false,
|
||||
);
|
||||
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
|
||||
$file = UploadedFile::fake()->createWithContent(
|
||||
'晨丰_对账单_202609.csv',
|
||||
"\xEF\xBB\xBF" . self::HEADER . "\n"
|
||||
. ",,CF-UPLOAD,2026-09-30,晨丰,ZSJ-UPLOAD,规格甲,2,128.00,256.00\n",
|
||||
);
|
||||
|
||||
$this->actingAsSupplierPortal($supplierUser)
|
||||
->post(route('business.supplier-reconciliation.statement.upload', ['company_id' => $this->company->id]), [
|
||||
'supplier' => '晨丰',
|
||||
'import' => $file,
|
||||
])
|
||||
->assertStatus(302)
|
||||
->assertSessionHas(
|
||||
'success',
|
||||
'对账单已保存:晨丰 共 1 行;上传回执已发送到该厂家钉钉群。',
|
||||
);
|
||||
|
||||
Http::assertSentCount(1);
|
||||
Http::assertSent(fn ($request): bool => str_contains(
|
||||
(string) ($request['markdown']['text'] ?? ''),
|
||||
'供应商对账单上传回执 · 晨丰',
|
||||
) && str_contains(
|
||||
(string) ($request['markdown']['text'] ?? ''),
|
||||
'对账月份:**2026-09**',
|
||||
) && str_contains(
|
||||
(string) ($request['markdown']['text'] ?? ''),
|
||||
'上传文件:晨丰_对账单_202609.csv',
|
||||
) && str_contains(
|
||||
(string) ($request['markdown']['text'] ?? ''),
|
||||
'上传行数:**1** 行',
|
||||
) && str_contains(
|
||||
(string) ($request['markdown']['text'] ?? ''),
|
||||
'数据状态:**已进入财务核对**',
|
||||
));
|
||||
}
|
||||
|
||||
public function test_vendor_portal_statement_upload_sends_a_dingtalk_receipt(): void
|
||||
{
|
||||
$vendor = $this->createVendorPortalUser('晨丰');
|
||||
app(SupplierDingTalkNotifier::class)->save(
|
||||
'晨丰',
|
||||
'https://oapi.dingtalk.com/robot/send?access_token=portal-statement-upload-test',
|
||||
'',
|
||||
false,
|
||||
);
|
||||
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
|
||||
$file = UploadedFile::fake()->createWithContent(
|
||||
'portal_202609.csv',
|
||||
"\xEF\xBB\xBF" . self::HEADER . "\n"
|
||||
. ",,PORTAL-RECEIPT,2026-09-30,晨丰,ZSJ-PORTAL,规格甲,1,100.00,100.00\n",
|
||||
);
|
||||
|
||||
$this->loginAs($vendor)
|
||||
->post(route('portal.supplier-reconciliation.statement.upload', ['company_id' => $this->company->id]), [
|
||||
'import' => $file,
|
||||
])
|
||||
->assertStatus(302)
|
||||
->assertSessionHas(
|
||||
'success',
|
||||
'对账单已保存:晨丰 共 1 行;上传回执已发送到该厂家钉钉群。',
|
||||
);
|
||||
|
||||
Http::assertSentCount(1);
|
||||
Http::assertSent(fn ($request): bool => str_contains(
|
||||
(string) ($request['markdown']['text'] ?? ''),
|
||||
'供应商对账单上传回执 · 晨丰',
|
||||
) && str_contains(
|
||||
(string) ($request['markdown']['text'] ?? ''),
|
||||
'上传文件:portal_202609.csv',
|
||||
));
|
||||
}
|
||||
|
||||
public function test_dingtalk_receipt_failure_does_not_roll_back_the_statement_upload(): void
|
||||
{
|
||||
$supplierUser = $this->createSupplierPortalUser('晨丰');
|
||||
app(SupplierDingTalkNotifier::class)->save(
|
||||
'晨丰',
|
||||
'https://oapi.dingtalk.com/robot/send?access_token=statement-failure-test',
|
||||
'',
|
||||
false,
|
||||
);
|
||||
Http::fake(['*' => Http::response(['errcode' => 500, 'errmsg' => 'robot failed'], 500)]);
|
||||
$file = UploadedFile::fake()->createWithContent(
|
||||
'receipt_failure.csv',
|
||||
"\xEF\xBB\xBF" . self::HEADER . "\n"
|
||||
. ",,RECEIPT-FAILURE,2026-09-30,晨丰,ZSJ-FAILURE,规格甲,1,100.00,100.00\n",
|
||||
);
|
||||
|
||||
$this->actingAsSupplierPortal($supplierUser)
|
||||
->post(route('business.supplier-reconciliation.statement.upload', ['company_id' => $this->company->id]), [
|
||||
'supplier' => '晨丰',
|
||||
'import' => $file,
|
||||
])
|
||||
->assertStatus(302)
|
||||
->assertSessionHas(
|
||||
'success',
|
||||
'对账单已保存:晨丰 共 1 行;上传回执发送失败,对账单已正常保存。',
|
||||
);
|
||||
|
||||
Http::assertSentCount(1);
|
||||
$this->assertCount(
|
||||
1,
|
||||
app(SupplierStatementStore::class)->rows('晨丰', (int) $this->company->id),
|
||||
);
|
||||
}
|
||||
|
||||
public function test_vendor_portal_shows_only_all_returns_for_the_logged_in_supplier(): void
|
||||
{
|
||||
$store = app(SupplierPayableOverrideStore::class);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user