CaiWuXiTong/app/Http/Controllers/Business/InternalFlowReportActions.php
DoDo 27d0f6d9fe feat: 内部流转开票按标签顺延分配
内部开票额度按标签 1→2→3→4 顺延,下一标签按剩余进项需求占比接收,票不足时计入缺口。
2026-11-09 14:54:21 +08:00

74 lines
3.0 KiB
PHP

<?php
namespace App\Http\Controllers\Business;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Modules\EcommerceFinance\Repositories\InternalFlowReportRepository;
use Modules\EcommerceFinance\Services\InternalFlowProjection;
use Throwable;
class InternalFlowReportActions
{
public function updateSupplierReceipt(
Request $request,
InternalFlowReportRepository $repository,
InternalFlowProjection $projection,
int $record,
): JsonResponse|RedirectResponse {
$data = $request->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 ?? '收票-供应商已保存。',
);
}
}