326 lines
15 KiB
PHP
326 lines
15 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Business;
|
|
|
|
use App\Models\Business\CourierAgreementSignature;
|
|
use App\Models\Business\CourierSigningSubject;
|
|
use App\Services\Esign\EsignClient;
|
|
use App\Support\CourierAgreementSignatureService;
|
|
use App\Utilities\SystemAccess;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
|
|
class CourierAgreementSignatures extends Controller
|
|
{
|
|
public function __construct(private readonly CourierAgreementSignatureService $service)
|
|
{
|
|
}
|
|
|
|
public function store(Request $request): RedirectResponse
|
|
{
|
|
$this->ensureCanManage();
|
|
abort_unless(EsignClient::configured(), 422, 'E 签宝沙箱尚未配置 App ID 或 AppSecret。');
|
|
abort_unless(Schema::hasTable('business_courier_agreement_signatures'), 503, '协议签署数据表尚未完成迁移,请先执行数据库迁移。');
|
|
|
|
$data = $request->validate($this->rules());
|
|
$type = $data['document_type'];
|
|
if ($type === 'authorization' && count($data['authorization_items'] ?? []) === 0) {
|
|
throw ValidationException::withMessages(['authorization_items' => '授权委托书至少选择一项授权事项。']);
|
|
}
|
|
$this->validateEsignAccount($data['esign_account'], 'esign_account');
|
|
if (! empty($data['secondary_esign_account'])) {
|
|
$this->validateEsignAccount($data['secondary_esign_account'], 'secondary_esign_account');
|
|
}
|
|
|
|
$data['organization_name'] = $data['signing_subject'];
|
|
$data['operator_name'] = $data['operator_name'] ?? $data['courier_name'];
|
|
$data['operator_account'] = $data['esign_account'];
|
|
$data['payload'] = $this->payload($data);
|
|
|
|
$signature = $this->service->send($data, user_id());
|
|
|
|
return redirect()
|
|
->route('business.pages.show', ['section' => 'courier-payables', 'page' => 'agreement-signing', 'agreement_view' => 'tasks'])
|
|
->with('success', $signature->status === CourierAgreementSignature::STATUS_SIGNING
|
|
? "{$signature->documentLabel()}已推送到 E 签宝,等待对方签署。"
|
|
: "{$signature->documentLabel()}签署任务已受理。")
|
|
->with('agreement_signature_id', $signature->id);
|
|
}
|
|
|
|
/**
|
|
* Create a signing subject for the current tenant.
|
|
*
|
|
* The subject is deliberately kept separate from the global companies table:
|
|
* one tenant may need to sign agreements using more than one legal entity.
|
|
*/
|
|
public function storeSubject(Request $request): JsonResponse
|
|
{
|
|
$this->ensureCanManage();
|
|
|
|
if ($response = $this->subjectsTableUnavailable()) {
|
|
return $response;
|
|
}
|
|
|
|
$data = $this->subjectData($request);
|
|
$subject = DB::transaction(function () use ($data): CourierSigningSubject {
|
|
$this->ensureSubjectNameAvailable($data['name']);
|
|
|
|
$isFirst = ! CourierSigningSubject::query()->exists();
|
|
|
|
return CourierSigningSubject::create([
|
|
'company_id' => company_id(),
|
|
'name' => $data['name'],
|
|
'unified_social_credit_code' => $data['unified_social_credit_code'],
|
|
'is_default' => $isFirst,
|
|
'created_by' => user_id(),
|
|
'updated_by' => user_id(),
|
|
]);
|
|
});
|
|
|
|
return $this->subjectResponse($subject, '公司主体已新增。');
|
|
}
|
|
|
|
public function updateSubject(Request $request, CourierSigningSubject $subject): JsonResponse
|
|
{
|
|
$this->ensureCanManage();
|
|
$this->ensureSubjectBelongsToCurrentCompany($subject);
|
|
|
|
if ($response = $this->subjectsTableUnavailable()) {
|
|
return $response;
|
|
}
|
|
|
|
$data = $this->subjectData($request);
|
|
$this->ensureSubjectNameAvailable($data['name'], $subject);
|
|
|
|
$subject->update([
|
|
'name' => $data['name'],
|
|
'unified_social_credit_code' => $data['unified_social_credit_code'],
|
|
'updated_by' => user_id(),
|
|
]);
|
|
|
|
return $this->subjectResponse($subject->fresh(), '公司主体已更新。');
|
|
}
|
|
|
|
public function setDefaultSubject(CourierSigningSubject $subject): JsonResponse
|
|
{
|
|
$this->ensureCanManage();
|
|
$this->ensureSubjectBelongsToCurrentCompany($subject);
|
|
|
|
if ($response = $this->subjectsTableUnavailable()) {
|
|
return $response;
|
|
}
|
|
|
|
DB::transaction(function () use ($subject): void {
|
|
CourierSigningSubject::query()
|
|
->where('is_default', true)
|
|
->update([
|
|
'is_default' => false,
|
|
'updated_by' => user_id(),
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
$subject->forceFill([
|
|
'is_default' => true,
|
|
'updated_by' => user_id(),
|
|
])->save();
|
|
});
|
|
|
|
return $this->subjectResponse($subject->fresh(), '默认签署主体已切换。');
|
|
}
|
|
|
|
public function synchronize(CourierAgreementSignature $agreementSignature): RedirectResponse
|
|
{
|
|
$this->ensureCanManage();
|
|
$signature = $this->service->synchronize($agreementSignature, user_id());
|
|
|
|
return redirect()
|
|
->route('business.pages.show', ['section' => 'courier-payables', 'page' => 'agreement-signing', 'agreement_view' => 'tasks'])
|
|
->with('success', "{$signature->documentLabel()}状态已同步:{$signature->statusLabel()}。");
|
|
}
|
|
|
|
public function archive(CourierAgreementSignature $agreementSignature): RedirectResponse
|
|
{
|
|
$this->ensureCanManage();
|
|
abort_unless($agreementSignature->status === CourierAgreementSignature::STATUS_COMPLETED && $agreementSignature->remote_status === '2', 422, '只有签署完成的协议可以人工归档。');
|
|
$signature = $this->service->archive($agreementSignature, null, user_id());
|
|
|
|
return redirect()
|
|
->route('business.pages.show', ['section' => 'courier-payables', 'page' => 'agreement-signing', 'agreement_view' => 'tasks'])
|
|
->with('success', "{$signature->documentLabel()}已人工确认并归档。");
|
|
}
|
|
|
|
public function download(CourierAgreementSignature $agreementSignature, string $kind): StreamedResponse
|
|
{
|
|
$this->ensureCanManage();
|
|
[$path, $suffix] = match ($kind) {
|
|
'source' => [$agreementSignature->source_pdf_path, '签署原文'],
|
|
'signed' => [$agreementSignature->signed_pdf_path, '已签署协议'],
|
|
'certificate' => [$agreementSignature->certificate_path, '签署完成证书'],
|
|
default => abort(404),
|
|
};
|
|
|
|
abort_if(blank($path) || ! \Storage::disk('uploads')->exists($path), 404, '归档文件不存在。');
|
|
|
|
return \Storage::disk('uploads')->download($path, $agreementSignature->business_no . '-' . $suffix . '.pdf', ['Content-Type' => 'application/pdf']);
|
|
}
|
|
|
|
private function rules(): array
|
|
{
|
|
$date = ['required', 'date', 'after_or_equal:today', 'before_or_equal:' . now()->addDays(90)->toDateString()];
|
|
|
|
return [
|
|
'idempotency_key' => ['required', 'string', 'max:80', 'regex:/^[A-Za-z0-9_-]+$/'],
|
|
'courier_name' => ['required', 'string', 'max:255'],
|
|
'document_type' => ['required', 'in:' . implode(',', array_keys(CourierAgreementSignature::DOCUMENTS))],
|
|
'signing_subject' => ['required', 'string', 'min:2', 'max:255'],
|
|
'company_full_name' => ['required', 'string', 'min:2', 'max:255'],
|
|
'signing_deadline' => $date,
|
|
'esign_account' => ['required', 'string', 'max:255'],
|
|
'secondary_esign_account' => ['required_if:document_type,entity-change,cooperation-contract,authorization', 'nullable', 'string', 'max:255'],
|
|
'operator_name' => ['nullable', 'string', 'max:100'],
|
|
'archive_mode' => ['required', 'in:auto,manual'],
|
|
'account_proof_effective_date' => ['required_if:document_type,account-proof', 'date'],
|
|
'account_proof_account_name' => ['required_if:document_type,account-proof', 'string', 'max:255'],
|
|
'account_proof_bank_name' => ['required_if:document_type,account-proof', 'string', 'max:255'],
|
|
'account_proof_bank_account' => ['required_if:document_type,account-proof', 'string', 'max:64', 'regex:/^[0-9A-Za-z-]+$/'],
|
|
'account_change_date' => ['required_if:document_type,account-change', 'date'],
|
|
'old_account_name' => ['required_if:document_type,account-change', 'string', 'max:255'],
|
|
'old_account_number' => ['required_if:document_type,account-change', 'string', 'max:64', 'regex:/^[0-9A-Za-z-]+$/'],
|
|
'old_account_stop_date' => ['required_if:document_type,account-change', 'date'],
|
|
'new_account_effective_date' => ['required_if:document_type,account-change', 'date'],
|
|
'new_account_name' => ['required_if:document_type,account-change', 'string', 'max:255'],
|
|
'new_bank_name' => ['required_if:document_type,account-change', 'string', 'max:255'],
|
|
'new_bank_account' => ['required_if:document_type,account-change', 'string', 'max:64', 'regex:/^[0-9A-Za-z-]+$/'],
|
|
'former_company_name' => ['required_if:document_type,entity-change', 'string', 'max:255'],
|
|
'entity_change_date' => ['required_if:document_type,entity-change', 'date'],
|
|
'changed_company_name' => ['required_if:document_type,entity-change', 'string', 'max:255'],
|
|
'changed_company_start_date' => ['required_if:document_type,entity-change', 'date'],
|
|
'entity_proof_date' => ['required_if:document_type,entity-change', 'date'],
|
|
'contract_partner_name' => ['required_if:document_type,shared-contract', 'string', 'max:255'],
|
|
'shared_courier_name' => ['required_if:document_type,shared-contract', 'string', 'max:255'],
|
|
'shared_proof_date' => ['required_if:document_type,shared-contract', 'date'],
|
|
'cooperation_party_a_name' => ['required_if:document_type,cooperation-contract', 'string', 'max:255'],
|
|
'cooperation_party_a_phone' => ['required_if:document_type,cooperation-contract', 'string', 'max:64'],
|
|
'cooperation_party_b_name' => ['required_if:document_type,cooperation-contract', 'string', 'max:255'],
|
|
'cooperation_party_b_phone' => ['required_if:document_type,cooperation-contract', 'string', 'max:64'],
|
|
'cooperation_start_date' => ['required_if:document_type,cooperation-contract', 'date'],
|
|
'cooperation_end_date' => ['required_if:document_type,cooperation-contract', 'date', 'after_or_equal:cooperation_start_date'],
|
|
'principal_name' => ['required_if:document_type,authorization', 'string', 'max:100'],
|
|
'principal_id_number' => ['required_if:document_type,authorization', 'string', 'max:64'],
|
|
'trustee_name' => ['required_if:document_type,authorization', 'string', 'max:100'],
|
|
'trustee_id_number' => ['required_if:document_type,authorization', 'string', 'max:64'],
|
|
'authorization_items' => ['nullable', 'array'],
|
|
'authorization_items.*' => ['in:1,2,3,4'],
|
|
'bill_counterparty' => ['required_if:document_type,authorization', 'string', 'max:255'],
|
|
'loan_counterparty' => ['required_if:document_type,authorization', 'string', 'max:255'],
|
|
'payment_counterparty' => ['required_if:document_type,authorization', 'string', 'max:255'],
|
|
'authorization_start_date' => ['required_if:document_type,authorization', 'date'],
|
|
'authorization_end_date' => ['required_if:document_type,authorization', 'date', 'after_or_equal:authorization_start_date'],
|
|
];
|
|
}
|
|
|
|
private function payload(array $data): array
|
|
{
|
|
$allowed = array_keys($this->rules());
|
|
$payload = array_intersect_key($data, array_flip($allowed));
|
|
unset($payload['esign_account'], $payload['idempotency_key'], $payload['operator_name']);
|
|
$payload['authorization_items'] = array_values(array_map('strval', $data['authorization_items'] ?? []));
|
|
return $payload;
|
|
}
|
|
|
|
private function ensureCanManage(): void
|
|
{
|
|
abort_unless(
|
|
in_array(SystemAccess::roleName(user()), [SystemAccess::SUPER_ADMIN, SystemAccess::ADMIN], true),
|
|
403,
|
|
'当前账号没有协议签署管理权限。',
|
|
);
|
|
}
|
|
|
|
private function subjectData(Request $request): array
|
|
{
|
|
$data = $request->validate([
|
|
'name' => ['required', 'string', 'max:255'],
|
|
'unified_social_credit_code' => ['nullable', 'string', 'max:32', 'regex:/^[0-9A-Za-z]{18}$/'],
|
|
]);
|
|
|
|
$data['name'] = trim($data['name']);
|
|
if (mb_strlen($data['name']) < 2) {
|
|
throw ValidationException::withMessages(['name' => '公司名称至少填写 2 个字符。']);
|
|
}
|
|
|
|
$data['unified_social_credit_code'] = trim((string) ($data['unified_social_credit_code'] ?? '')) ?: null;
|
|
|
|
return $data;
|
|
}
|
|
|
|
private function ensureSubjectNameAvailable(string $name, ?CourierSigningSubject $except = null): void
|
|
{
|
|
$query = CourierSigningSubject::query()->where('name', $name);
|
|
|
|
if ($except) {
|
|
$query->where($query->getModel()->getQualifiedKeyName(), '<>', $except->getKey());
|
|
}
|
|
|
|
if ($query->exists()) {
|
|
throw ValidationException::withMessages(['name' => '当前公司下已有同名签署主体。']);
|
|
}
|
|
}
|
|
|
|
private function subjectResponse(CourierSigningSubject $subject, string $message): JsonResponse
|
|
{
|
|
return response()->json([
|
|
'message' => $message,
|
|
'subject' => $this->serializeSubject($subject),
|
|
'subjects' => CourierSigningSubject::query()
|
|
->orderByDesc('is_default')
|
|
->orderBy('name')
|
|
->get()
|
|
->map(fn (CourierSigningSubject $item): array => $this->serializeSubject($item))
|
|
->values(),
|
|
]);
|
|
}
|
|
|
|
private function serializeSubject(CourierSigningSubject $subject): array
|
|
{
|
|
return [
|
|
'id' => $subject->id,
|
|
'name' => $subject->name,
|
|
'unified_social_credit_code' => $subject->unified_social_credit_code,
|
|
'is_default' => (bool) $subject->is_default,
|
|
];
|
|
}
|
|
|
|
private function subjectsTableUnavailable(): ?JsonResponse
|
|
{
|
|
if (Schema::hasTable('business_courier_signing_subjects')) {
|
|
return null;
|
|
}
|
|
|
|
return response()->json([
|
|
'message' => '公司主体数据表尚未完成迁移,请先执行数据库迁移。',
|
|
], 503);
|
|
}
|
|
|
|
private function ensureSubjectBelongsToCurrentCompany(CourierSigningSubject $subject): void
|
|
{
|
|
abort_unless((int) $subject->company_id === (int) company_id(), 404, '签署主体不存在。');
|
|
}
|
|
|
|
private function validateEsignAccount(string $account, string $field): void
|
|
{
|
|
$valid = preg_match('/^1\d{10}$/', $account) === 1
|
|
|| filter_var($account, FILTER_VALIDATE_EMAIL) !== false;
|
|
|
|
if (! $valid) {
|
|
throw ValidationException::withMessages([$field => 'E签宝账号必须是 11 位手机号或有效邮箱。']);
|
|
}
|
|
}
|
|
}
|