validate([ 'receipt_from_supplier' => ['present', 'nullable', 'numeric', 'min:0', 'max:99999999999999'], ]); $amount = $data['receipt_from_supplier'] === null ? null : round((float) $data['receipt_from_supplier'], 2); $companyName = $repository->updateSupplierReceipt($record, $amount, user_id()); abort_if($companyName === null, 404); $projectionPayload = null; $projectionError = null; try { $projectionPayload = $projection->forMonth(); } catch (Throwable $exception) { report($exception); $projectionError = '主体维度报表上月业绩暂时无法读取,已保存供应商收票,其他自动计算暂时保留原值。'; } $filters = [ 'company_name' => (string) $request->input('company_name', ''), 'tag' => (string) $request->input('tag', ''), ]; $payload = $repository->paginate(1, 100, $filters, null, 'asc', $projectionPayload); $rows = collect($payload['data'] ?? []); $saved = $rows->firstWhere('id', $record) ?? $repository->find($record, $projectionPayload); if ($request->expectsJson()) { return response()->json([ 'ok' => true, 'record' => [ 'id' => (int) ($saved['id'] ?? $record), 'receipt_from_supplier' => $saved['receipt_from_supplier'] ?? null, 'receipt_total' => $saved['receipt_total'] ?? null, 'remaining_gap' => $saved['remaining_gap'] ?? null, ], 'rows' => $rows->map(static fn (array $row): array => [ 'id' => (int) $row['id'], 'internal_transfer_1' => $row['internal_transfer_1'] ?? null, 'internal_transfer_2' => $row['internal_transfer_2'] ?? null, 'internal_transfer_3' => $row['internal_transfer_3'] ?? null, 'receipt_total' => $row['receipt_total'] ?? null, 'remaining_gap' => $row['remaining_gap'] ?? null, ])->values()->all(), 'summary' => $payload['summary'] ?? [], 'warning' => $projectionError, ]); } return back()->with( $projectionError === null ? 'success' : 'warning', $projectionError ?? '收票-供应商已保存。', ); } }