121 lines
4.7 KiB
PHP
121 lines
4.7 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 {
|
|
return $this->updateReceipt(
|
|
$request,
|
|
$repository,
|
|
$projection,
|
|
$record,
|
|
'receipt_from_supplier',
|
|
'收票-供应商已保存。',
|
|
);
|
|
}
|
|
|
|
public function updateSuofeiyaReceipt(
|
|
Request $request,
|
|
InternalFlowReportRepository $repository,
|
|
InternalFlowProjection $projection,
|
|
int $record,
|
|
): JsonResponse|RedirectResponse {
|
|
$companyName = $repository->companyNameForRecord($record);
|
|
abort_if($companyName === null, 404);
|
|
$rule = config('internal_flow_report.cost_to_income.' . $companyName);
|
|
abort_unless(
|
|
is_array($rule) && ($rule['receipt_field'] ?? null) === 'receipt_from_suofeiya',
|
|
403,
|
|
);
|
|
|
|
return $this->updateReceipt(
|
|
$request,
|
|
$repository,
|
|
$projection,
|
|
$record,
|
|
'receipt_from_suofeiya',
|
|
'收票-索菲亚已保存,并已按 90% 成本率倒推收入。',
|
|
);
|
|
}
|
|
|
|
private function updateReceipt(
|
|
Request $request,
|
|
InternalFlowReportRepository $repository,
|
|
InternalFlowProjection $projection,
|
|
int $record,
|
|
string $field,
|
|
string $successMessage,
|
|
): JsonResponse|RedirectResponse {
|
|
$data = $request->validate([
|
|
$field => ['present', 'nullable', 'numeric', 'min:0', 'max:99999999999999'],
|
|
]);
|
|
$amount = $data[$field] === null ? null : round((float) $data[$field], 2);
|
|
$companyName = $repository->updateReceiptOverride($record, $field, $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_field' => $field,
|
|
'receipt_amount' => $saved[$field] ?? null,
|
|
'projected_sales' => $saved['projected_sales'] ?? 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'],
|
|
'projected_sales' => $row['projected_sales'] ?? null,
|
|
'invoiced_to_buyer' => $row['invoiced_to_buyer'] ?? null,
|
|
'internal_invoiced' => $row['internal_invoiced'] ?? null,
|
|
'required_input_cost' => $row['required_input_cost'] ?? null,
|
|
'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,
|
|
'invoicing_ratio' => $row['invoicing_ratio'] ?? null,
|
|
])->values()->all(),
|
|
'summary' => $payload['summary'] ?? [],
|
|
'warning' => $projectionError,
|
|
]);
|
|
}
|
|
|
|
return back()->with(
|
|
$projectionError === null ? 'success' : 'warning',
|
|
$projectionError ?? $successMessage,
|
|
);
|
|
}
|
|
}
|