> $records * @return list */ public function statementMonths(array $records): array { $months = []; foreach ($records as $record) { $date = trim((string) ($record['createdAt'] ?? $record['summary_month'] ?? '')); if (preg_match('/^(\d{4})[-\/.](\d{1,2})/', $date, $matches) !== 1) { continue; } $month = (int) $matches[2]; if ($month >= 1 && $month <= 12) { $months[sprintf('%04d-%02d', (int) $matches[1], $month)] = true; } } $months = array_keys($months); sort($months); return $months; } /** * @return array{ * eligible:bool, * month:string, * reason:string, * qty:float, * amount:float, * difference_count:int * } */ public function assess(string $supplier, int $companyId, string $month): array { if (preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $month) !== 1) { return $this->assessment(false, $month, '账期格式无效'); } $detail = $this->pageData->financeDetailForFilters($supplier, $companyId, [ 'month' => $month, ]); if (($detail['awaitingUpload'] ?? false) === true) { return $this->assessment(false, $month, '厂家尚未上传该账期对账单'); } $rows = (array) ($detail['rows'] ?? []); $differenceCount = (int) ($detail['difference_count'] ?? 0); if ($rows === []) { return $this->assessment(false, $month, '该账期没有可比对数据', 0, 0, $differenceCount); } if ($differenceCount > 0) { return $this->assessment( false, $month, "仍有 {$differenceCount} 条差异或未定价记录", 0, 0, $differenceCount, ); } $summary = (array) ($detail['summary'] ?? []); $sourceTotals = (array) ($detail['sourceTotals'] ?? []); $differenceRows = (int) ($summary['supplierExtra'] ?? 0) + (int) ($summary['mismatch'] ?? 0) + (int) ($summary['supplierMissing'] ?? 0) + (int) ($summary['unpriced'] ?? 0); $mineQty = (float) ($sourceTotals['mineQty'] ?? 0); $mineAmount = (float) ($sourceTotals['mineAmount'] ?? 0); $theirsQty = (float) ($sourceTotals['theirsQty'] ?? 0); $theirsAmount = (float) ($sourceTotals['theirsAmount'] ?? 0); if ( $differenceRows > 0 || abs($mineQty - $theirsQty) >= 0.005 || abs($mineAmount - $theirsAmount) >= 0.005 ) { return $this->assessment( false, $month, '双方件数或货款金额仍不一致', 0, 0, max($differenceCount, $differenceRows), ); } $qty = (float) ($summary['settleQty'] ?? $theirsQty); $amount = (float) ($summary['settleAmount'] ?? $theirsAmount); if (abs($qty) < 0.00005 && abs($amount) < 0.005) { return $this->assessment(false, $month, '该账期没有可确认的货款数据', 0, 0, 0); } return [ 'eligible' => true, 'month' => $month, 'reason' => '双方数据完全一致', 'qty' => round($qty, 4), 'amount' => round($amount, 2), 'difference_count' => 0, ]; } /** * @return array{ * status:'pushed'|'skipped'|'failed', * month:string, * reason:string, * qty:float, * amount:float, * difference_count:int * } */ public function pushMonth( string $supplier, int $companyId, string $month, ?int $userId = null, bool $skipExistingConfirmation = true, ): array { $assessment = $this->assess($supplier, $companyId, $month); if ($assessment['eligible'] !== true) { return ['status' => 'skipped'] + $assessment; } if ($skipExistingConfirmation) { $existing = $this->summaryConfirmations->latest( $supplier, $month, 'settlement', $companyId, ); $existingStatus = trim((string) ($existing['status'] ?? '')); if (in_array($existingStatus, [ SupplierSummaryConfirmation::STATUS_SENDING, SupplierSummaryConfirmation::STATUS_PENDING, SupplierSummaryConfirmation::STATUS_CONFIRMED, ], true)) { return [ 'status' => 'skipped', 'month' => $month, 'reason' => $existingStatus === SupplierSummaryConfirmation::STATUS_CONFIRMED ? '厂家已确认,默认不重复推送' : '该账期已有待确认推送,默认不重复推送', 'qty' => $assessment['qty'], 'amount' => $assessment['amount'], 'difference_count' => 0, ]; } } try { $result = $this->dingTalkNotifier->pushReconciliationSummary( $supplier, $month, $assessment['qty'], $assessment['amount'], $companyId, 'settlement', $userId, ); } catch (Throwable $exception) { return [ 'status' => 'failed', 'month' => $month, 'reason' => $exception->getMessage(), 'qty' => $assessment['qty'], 'amount' => $assessment['amount'], 'difference_count' => 0, ]; } return [ 'status' => 'pushed', 'month' => $month, 'reason' => '已发送货款确认', 'qty' => (float) ($result['qty'] ?? $assessment['qty']), 'amount' => (float) ($result['amount'] ?? $assessment['amount']), 'difference_count' => 0, ]; } /** * @param list> $records * @return array{ * pushed:list>, * skipped:list>, * failed:list> * } */ public function afterStatementUpload( string $supplier, int $companyId, array $records, ?int $userId = null, ): array { $result = ['pushed' => [], 'skipped' => [], 'failed' => []]; foreach ($this->statementMonths($records) as $month) { $push = $this->pushMonth( $supplier, $companyId, $month, $userId, false, ); $result[$push['status']][] = $push; } return $result; } /** @param array{pushed:list>,failed:list>} $result */ public function uploadNotice(array $result): string { $notices = []; $pushed = (array) ($result['pushed'] ?? []); if ($pushed !== []) { $notices[] = '零差异对账确认已自动推送到该厂家钉钉群(' . implode('、', array_map( fn (array $row): string => $this->displayMonth((string) $row['month']), $pushed, )) . ')'; } $failed = (array) ($result['failed'] ?? []); if ($failed !== []) { $notices[] = '零差异对账确认自动推送失败(' . implode('、', array_map( fn (array $row): string => $this->displayMonth((string) $row['month']) . ':' . (string) ($row['reason'] ?? '未知错误'), $failed, )) . ')'; } return implode(';', array_filter($notices)); } private function assessment( bool $eligible, string $month, string $reason, float $qty = 0, float $amount = 0, int $differenceCount = 0, ): array { return [ 'eligible' => $eligible, 'month' => $month, 'reason' => $reason, 'qty' => $qty, 'amount' => $amount, 'difference_count' => $differenceCount, ]; } private function displayMonth(string $month): string { if (preg_match('/^(\d{4})-(\d{1,2})$/', $month, $matches) !== 1) { return $month; } return (int) $matches[1] . '年' . (int) $matches[2] . '月'; } }