From e3064ec271d8ac948313ae961868b8368f9a2c29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=9C=A8?= <594326199@qq.com> Date: Wed, 4 Nov 2026 10:59:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=81=A2=E5=A4=8D=E5=AF=B9=E8=B4=A6?= =?UTF-8?q?=E4=BA=BA=E9=80=89=E9=A1=B9=E5=B9=B6=E6=94=AF=E6=8C=81=E6=89=B9?= =?UTF-8?q?=E9=87=8F=E6=8E=A8=E9=80=81=E9=80=80=E8=B4=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Business/SupplierReconciliation.php | 70 +++++++++++ .../View/SupplierReconciliationPageData.php | 84 ++++++++++--- .../finance-data.blade.php | 110 ++++++++++++++++- routes/admin.php | 4 + .../SupplierReconciliationDingTalkTest.php | 116 ++++++++++++++++++ 5 files changed, 364 insertions(+), 20 deletions(-) diff --git a/app/Http/Controllers/Business/SupplierReconciliation.php b/app/Http/Controllers/Business/SupplierReconciliation.php index 1fa85343..91f29ad5 100644 --- a/app/Http/Controllers/Business/SupplierReconciliation.php +++ b/app/Http/Controllers/Business/SupplierReconciliation.php @@ -7,6 +7,7 @@ use Illuminate\Http\JsonResponse; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Routing\Controller; +use Illuminate\Validation\ValidationException; use Modules\EcommerceFinance\Services\SupplierComparisonCsvExporter; use Modules\EcommerceFinance\Services\SupplierDingTalkNotifier; use Modules\EcommerceFinance\Services\SupplierReturnCsvExporter; @@ -234,6 +235,75 @@ class SupplierReconciliation extends Controller ); } + public function pushReturnsBatch(Request $request): RedirectResponse + { + $request->validate([ + 'selected_returns' => ['required', 'array', 'min:1', 'max:200'], + 'selected_returns.*' => ['string', 'max:2048'], + ]); + + $targets = []; + foreach ((array) $request->input('selected_returns', []) as $encoded) { + $selection = json_decode((string) $encoded, true); + $supplier = is_array($selection) ? trim((string) ($selection['supplier'] ?? '')) : ''; + $month = is_array($selection) ? trim((string) ($selection['month'] ?? '')) : ''; + if ($supplier === '' || preg_match('/^\d{4}-\d{2}$/', $month) !== 1) { + throw ValidationException::withMessages([ + 'batch_return_push' => '批量退货选择无效,请刷新页面后重新选择。', + ]); + } + + $targets[$supplier] = $month; + } + + $companyId = (int) (company_id() ?: 1); + $returnsBySupplier = $this->pageData->returnRowsForMonthTargets($targets, $companyId); + $pushedSuppliers = 0; + $pushedRows = 0; + $messages = 0; + $skipped = []; + + foreach ($targets as $supplier => $month) { + $returns = $returnsBySupplier[$supplier] ?? []; + if ($returns === []) { + $skipped[] = "{$supplier}({$month} 无退货)"; + continue; + } + + try { + $result = $this->dingTalkNotifier->pushReturns($supplier, $returns, $month); + } catch (Throwable $exception) { + $reason = $exception instanceof ValidationException + ? (string) (collect($exception->errors())->flatten()->first() ?: $exception->getMessage()) + : $exception->getMessage(); + $skipped[] = $supplier . '(' . ($reason !== '' ? $reason : '推送失败') . ')'; + continue; + } + + $pushedSuppliers++; + $pushedRows += (int) $result['rows']; + $messages += (int) $result['messages']; + } + + $skippedNote = ''; + if ($skipped !== []) { + $preview = implode('、', array_slice($skipped, 0, 6)); + $skippedNote = ';未推送 ' . count($skipped) . ' 家:' . $preview + . (count($skipped) > 6 ? ' 等' : ''); + } + + if ($pushedSuppliers === 0) { + return back()->withErrors([ + 'batch_return_push' => '批量退货推送未完成' . $skippedNote, + ]); + } + + return back()->with( + 'success', + "已批量推送 {$pushedSuppliers} 家供应商,共 {$pushedRows} 条退货({$messages} 条钉钉消息){$skippedNote}。", + ); + } + private function markReturn(Request $request, string $status, string $message): RedirectResponse { $request->validate(['key' => ['required', 'string', 'max:191']]); diff --git a/modules/EcommerceFinance/View/SupplierReconciliationPageData.php b/modules/EcommerceFinance/View/SupplierReconciliationPageData.php index b2b224a9..2dc3e8ab 100644 --- a/modules/EcommerceFinance/View/SupplierReconciliationPageData.php +++ b/modules/EcommerceFinance/View/SupplierReconciliationPageData.php @@ -54,6 +54,22 @@ class SupplierReconciliationPageData $companyId, ); + $reconcilers = []; + foreach ($mine as $row) { + $name = trim((string) ($row['reconciler'] ?? '')); + if ($name !== '') { + $reconcilers[$name] = true; + } + } + foreach ($returnRows as $row) { + $name = trim((string) ($row['reconciler'] ?? '')); + if ($name !== '') { + $reconcilers[$name] = true; + } + } + $reconcilers = array_keys($reconcilers); + sort($reconcilers); + $months = []; $returnSupplierMonths = []; foreach ($returnRows as $row) { @@ -86,16 +102,6 @@ class SupplierReconciliationPageData $month = ''; } - $reconcilers = []; - foreach ($mine as $row) { - $name = trim((string) ($row['reconciler'] ?? '')); - if ($name !== '') { - $reconcilers[$name] = true; - } - } - $reconcilers = array_keys($reconcilers); - sort($reconcilers); - $reconciler = trim((string) request()->query('reconciler', '')); if ($reconciler !== '') { $mine = array_values(array_filter( @@ -542,18 +548,62 @@ class SupplierReconciliationPageData return []; } - $statuses = $this->returnConfirmations->statuses($supplier, $companyId); - $rows = []; - foreach ($this->returnRows($supplier, $companyId) as $row) { - if ($this->returnRowMonth($row) !== $month) { + return $this->returnRowsForMonthTargets([$supplier => $month], $companyId)[$supplier] ?? []; + } + + /** + * 一次读取数据集,为多家供应商分别筛选指定月份的退货,供批量推送使用。 + * + * @param array $targets 厂家 => 月份 + * @return array>> + */ + public function returnRowsForMonthTargets(array $targets, int $companyId): array + { + $normalizedTargets = []; + foreach ($targets as $supplier => $month) { + $supplier = is_string($supplier) ? trim($supplier) : ''; + $month = is_string($month) ? trim($month) : ''; + if ($supplier !== '' && preg_match('/^\d{4}-\d{2}$/', $month) === 1) { + $normalizedTargets[$supplier] = $month; + } + } + if ($normalizedTargets === []) { + return []; + } + + $result = []; + foreach ($normalizedTargets as $supplier => $month) { + $result[$supplier] = []; + } + + $allRows = $this->store->listRows( + PayablesFinalActions::DATASET_PREFIX . 'supplier:return-reconciliation', + $companyId, + ); + foreach ($allRows as $row) { + $supplier = trim((string) ($row['supplier'] ?? '')); + if (! array_key_exists($supplier, $result) + || $this->returnRowMonth($row) !== (string) $normalizedTargets[$supplier]) { continue; } - $row['_status'] = $statuses[(string) ($row['_action_key'] ?? '')] ?? ''; - $rows[] = $this->returnAccounting->enrich($row); + $result[$supplier][] = $row; } - return $rows; + foreach ($result as $supplier => &$rows) { + if ($rows === []) { + continue; + } + $statuses = $this->returnConfirmations->statuses($supplier, $companyId); + foreach ($rows as &$row) { + $row['_status'] = $statuses[(string) ($row['_action_key'] ?? '')] ?? ''; + $row = $this->returnAccounting->enrich($row); + } + unset($row); + } + unset($rows); + + return $result; } /** diff --git a/resources/views/business/pages/supplier-reconciliation/finance-data.blade.php b/resources/views/business/pages/supplier-reconciliation/finance-data.blade.php index dab6f6e6..97a83370 100644 --- a/resources/views/business/pages/supplier-reconciliation/finance-data.blade.php +++ b/resources/views/business/pages/supplier-reconciliation/finance-data.blade.php @@ -17,6 +17,12 @@ .supplier-reconciliation-page .sr-button.is-small { height: 26px; padding: 0 9px; font-size: .75rem; } .supplier-reconciliation-page .sr-list-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; } .supplier-reconciliation-page .sr-list-actions form { margin: 0; } + .supplier-reconciliation-page .sr-batch-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; padding: 10px 12px; border: 1px solid #E4DCCB; border-radius: 8px; background: var(--vip-content-accent-soft, #F7F0DE); } + .supplier-reconciliation-page .sr-batch-selection { display: flex; align-items: center; gap: 7px; color: #5B5343; font-size: .8125rem; font-weight: 600; } + .supplier-reconciliation-page .sr-batch-selection input { width: 15px; height: 15px; accent-color: var(--vip-content-accent-dark, #856632); } + .supplier-reconciliation-page .sr-batch-summary { color: var(--vip-content-muted, #595959); font-size: .75rem; } + .supplier-reconciliation-page .sr-list-select-cell { width: 48px; text-align: center; } + .supplier-reconciliation-page .sr-list-select-cell input { width: 15px; height: 15px; accent-color: var(--vip-content-accent-dark, #856632); } .supplier-reconciliation-page .sr-hint { margin: 0; color: var(--vip-content-muted, #595959); font-size: .75rem; line-height: 1.7; } .supplier-reconciliation-page .sr-scroll { min-width: 0; max-width: 100%; overflow-x: auto; scrollbar-width: thin; } .supplier-reconciliation-page table { width: 100%; min-width: 720px; border-collapse: collapse; font-size: .8125rem; } @@ -64,10 +70,11 @@ @if ($errors->has('import')) @endif - @if ($errors->has('dingtalk_push') || $errors->has('dingtalk_return_push') || $errors->has('dingtalk_webhook') || $errors->has('dingtalk_secret') || $errors->has('month')) + @if ($errors->has('dingtalk_push') || $errors->has('dingtalk_return_push') || $errors->has('batch_return_push') || $errors->has('dingtalk_webhook') || $errors->has('dingtalk_secret') || $errors->has('month'))