diff --git a/app/Business/CourierContractPageData.php b/app/Business/CourierContractPageData.php index 3293fe7e..00fb065c 100644 --- a/app/Business/CourierContractPageData.php +++ b/app/Business/CourierContractPageData.php @@ -24,16 +24,7 @@ class CourierContractPageData public function page(bool $includeRows = true, string $partnerType = 'courier'): array { - $search = mb_substr(trim((string) request()->query('contract_search', '')), 0, 255); - $status = (string) request()->query('contract_status', ''); - $status = array_key_exists($status, self::STATUSES) ? $status : ''; - $from = $this->date(request()->query('contract_end_from')); - $to = $this->date(request()->query('contract_end_to')); - if ($from && $to && $from > $to) { - [$from, $to] = [$to, $from]; - } - $filters = ['contract_search' => $search, 'contract_status' => $status, - 'contract_end_from' => $from ?? '', 'contract_end_to' => $to ?? '']; + $filters = $this->filters(); $limit = min(max((int) setting('default.list_limit', 25), 1), 100); $page = max((int) request()->query('page', 1), 1); $options = ['path' => request()->url(), 'query' => request()->except('page')]; @@ -52,39 +43,14 @@ class CourierContractPageData } $matched = []; - $today = Carbon::today(config('app.timezone')); - // Dates and partner types are encrypted. Stream bounded chunks instead of SQL JSON queries - // or persisting a second, potentially stale copy of the signed business snapshot. - $signatures = CourierAgreementSignature::query()->where('company_id', (int) company_id()) - ->where('document_type', 'cooperation-contract')->where('status', CourierAgreementSignature::STATUS_ARCHIVED) - ->select(['id', 'company_id', 'business_no', 'courier_name', 'payload', 'document_type', 'status', - 'signed_pdf_path', 'certificate_path', 'archived_at', 'created_by', 'created_at']) - ->lazyById(250); - foreach ($signatures as $signature) { - $payload = $signature->archivedContractPayload($partnerType); - if ($payload === null) { - try { - $signature->payload; - } catch (Throwable) { - $data['courierContractUnreadable']++; - } - continue; - } - $row = $this->snapshot($signature, $payload, $today); + $recordUnreadable = static function () use (&$data): void { + $data['courierContractUnreadable']++; + }; + foreach ($this->contractRows($partnerType, $recordUnreadable) as $row) { $data['courierContractCounts'][$row['status']]++; - if (! $includeRows || ($status !== '' && $status !== $row['status'])) { - continue; + if ($includeRows && $this->matchesFilters($row, $filters)) { + $matched[] = $row; } - if (($from && (! $row['end_date'] || $row['end_date'] < $from)) - || ($to && (! $row['end_date'] || $row['end_date'] > $to))) { - continue; - } - if ($search !== '' && ! str_contains(mb_strtolower(implode(' ', [ - $row['courier_name'], $row['number'], $row['business_no'], $row['party_a'], $row['party_b'], - ])), mb_strtolower($search))) { - continue; - } - $matched[] = $row; } if ($includeRows) { $priority = ['due' => 0, 'expired' => 1, 'unknown' => 2, 'active' => 3, 'upcoming' => 4]; @@ -113,6 +79,71 @@ class CourierContractPageData return $data; } + public function supplierExportRows(): iterable + { + $filters = $this->filters(); + foreach ($this->contractRows('supplier') as $row) { + if ($this->matchesFilters($row, $filters)) { + yield $row; + } + } + } + + private function filters(): array + { + $search = request()->query('contract_search'); + $search = is_string($search) ? mb_substr(trim($search), 0, 255) : ''; + $status = request()->query('contract_status'); + $status = is_string($status) && array_key_exists($status, self::STATUSES) ? $status : ''; + $from = $this->date(request()->query('contract_end_from')); + $to = $this->date(request()->query('contract_end_to')); + if ($from && $to && $from > $to) { + [$from, $to] = [$to, $from]; + } + return ['contract_search' => $search, 'contract_status' => $status, + 'contract_end_from' => $from ?? '', 'contract_end_to' => $to ?? '']; + } + + private function matchesFilters(array $row, array $filters): bool + { + $status = $filters['contract_status']; + $from = $filters['contract_end_from']; + $to = $filters['contract_end_to']; + if (($status !== '' && $status !== $row['status']) + || ($from !== '' && (! $row['end_date'] || $row['end_date'] < $from)) + || ($to !== '' && (! $row['end_date'] || $row['end_date'] > $to))) { + return false; + } + + $search = $filters['contract_search']; + return $search === '' || str_contains(mb_strtolower(implode(' ', [ + $row['courier_name'], $row['number'], $row['business_no'], $row['party_a'], $row['party_b'], + ])), mb_strtolower($search)); + } + + private function contractRows(string $partnerType, ?callable $recordUnreadable = null): iterable + { + $today = Carbon::today(config('app.timezone')); + // The dates and partner type live in encrypted snapshots. + $signatures = CourierAgreementSignature::query()->where('company_id', (int) company_id()) + ->where('document_type', 'cooperation-contract')->where('status', CourierAgreementSignature::STATUS_ARCHIVED) + ->select(['id', 'company_id', 'business_no', 'courier_name', 'payload', 'document_type', 'status', + 'signed_pdf_path', 'certificate_path', 'archived_at', 'created_by', 'created_at']) + ->lazyById(250); + foreach ($signatures as $signature) { + $payload = $signature->archivedContractPayload($partnerType); + if ($payload !== null) { + yield $this->snapshot($signature, $payload, $today); + } elseif ($recordUnreadable !== null) { + try { + $signature->payload; + } catch (Throwable) { + $recordUnreadable(); + } + } + } + } + private function snapshot(CourierAgreementSignature $signature, array $payload, Carbon $today): array { $start = $this->date($payload['cooperation_start_date'] ?? null); diff --git a/app/Http/Controllers/Business/SupplierContractExport.php b/app/Http/Controllers/Business/SupplierContractExport.php new file mode 100644 index 00000000..df6a0fa8 --- /dev/null +++ b/app/Http/Controllers/Business/SupplierContractExport.php @@ -0,0 +1,65 @@ +streamDownload(static function () use ($contracts, $manifest): void { + $zip = new ZipStream(defaultCompressionMethod: CompressionMethod::STORE, sendHttpHeaders: false); + $disk = Storage::disk('uploads'); + try { + fwrite($manifest, "\xEF\xBB\xBF"); + fputcsv($manifest, ['供应商', '期限状态', '合同编号', '开始日期', '结束日期', '距到期天数', + '甲方签署主体', '乙方签署主体', '签署单号', '签署原件'], ',', '"', ''); + foreach ($contracts->supplierExportRows() as $row) { + $path = $row['signed_path']; + $attachment = '归档附件缺失'; + if (filled($path) && $disk->exists($path)) { + $pdf = $disk->readStream($path); + if (is_resource($pdf)) { + $attachment = '签署原件/合同-' . $row['id'] . '.pdf'; + try { + $zip->addFileFromStream($attachment, $pdf); + } finally { + fclose($pdf); + } + } else { + $attachment = '归档附件读取失败'; + } + } + + $values = array_map(static function ($value): string|int { + if (is_int($value)) { + return $value; + } + $value = (string) $value; + return preg_match('/^[\s\x00-\x1f]*[=+@-]|^[\t\r\n]/u', $value) ? "'" . $value : $value; + }, [ + $row['courier_name'], CourierContractPageData::STATUSES[$row['status']], $row['number'], + $row['start_date'], $row['end_date'], $row['days'], $row['party_a'], $row['party_b'], $row['business_no'], $attachment, + ]); + fputcsv($manifest, $values, ',', '"', ''); + } + + rewind($manifest); + $zip->addFileFromStream('供应商合同清单.csv', $manifest); + $zip->finish(); + } finally { + fclose($manifest); + } + }, '供应商合作合同-' . now()->format('Ymd-His') . '.zip', [ + 'Content-Type' => 'application/zip', 'Cache-Control' => 'private, no-store', + ]); + } +} diff --git a/resources/views/business/pages/supplier-payables/partials/supplier-contracts.blade.php b/resources/views/business/pages/supplier-payables/partials/supplier-contracts.blade.php index d7fcc751..49bcfacc 100644 --- a/resources/views/business/pages/supplier-payables/partials/supplier-contracts.blade.php +++ b/resources/views/business/pages/supplier-payables/partials/supplier-contracts.blade.php @@ -22,6 +22,8 @@