129 lines
5.2 KiB
PHP
129 lines
5.2 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Business;
|
||
|
||
use App\Business\KukaPostingBase\KukaPostingAmountPoster;
|
||
use Illuminate\Http\RedirectResponse;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Routing\Controller;
|
||
use InvalidArgumentException;
|
||
use RuntimeException;
|
||
use Throwable;
|
||
|
||
class KukaPostingAmountPosting extends Controller
|
||
{
|
||
public function preview(
|
||
Request $request,
|
||
KukaPostingAmountPoster $poster,
|
||
): RedirectResponse {
|
||
$data = $this->validatePlan($request);
|
||
$companyId = (int) (company_id() ?: 1);
|
||
|
||
try {
|
||
$plan = $poster->plan($companyId, $data['period'], $data['target_amount']);
|
||
} catch (InvalidArgumentException | RuntimeException $exception) {
|
||
return $this->redirectToPage($request, $data['period'])
|
||
->withInput()
|
||
->withErrors(['posting_amount' => $exception->getMessage()]);
|
||
} catch (Throwable $exception) {
|
||
report($exception);
|
||
|
||
return $this->redirectToPage($request, $data['period'])
|
||
->withInput()
|
||
->withErrors(['posting_amount' => '过账金额暂时无法计算,请稍后重试。']);
|
||
}
|
||
|
||
return $this->redirectToPage($request, $data['period'])
|
||
->withInput()
|
||
->with('kuka_posting_amount_preview', $poster->previewSummary($plan));
|
||
}
|
||
|
||
public function apply(
|
||
Request $request,
|
||
KukaPostingAmountPoster $poster,
|
||
): RedirectResponse {
|
||
$data = $request->validate([
|
||
'period' => ['required', 'string', 'regex:/^\d{4}-(0[1-9]|1[0-2])$/'],
|
||
'target_amount' => ['required', 'string', 'regex:/^\d{1,12}(?:\.\d{1,2})?$/', 'not_in:0,0.0,0.00'],
|
||
'preview_hash' => ['required', 'string', 'size:64'],
|
||
], $this->messages());
|
||
$companyId = (int) (company_id() ?: 1);
|
||
|
||
try {
|
||
$plan = $poster->plan($companyId, $data['period'], $data['target_amount']);
|
||
if (! hash_equals((string) $plan['preview_hash'], (string) $data['preview_hash'])) {
|
||
throw new InvalidArgumentException('发货明细或物料金额已经变化,请重新计算过账方案。');
|
||
}
|
||
if (($plan['is_exact'] ?? false) !== true) {
|
||
throw new InvalidArgumentException(sprintf(
|
||
'过账金额与选中发货金额仍有差额 %s,未写入过账标签。',
|
||
number_format((int) $plan['difference_cents'] / 100, 2, '.', ','),
|
||
));
|
||
}
|
||
|
||
$result = $poster->apply($plan, auth()->id());
|
||
} catch (InvalidArgumentException | RuntimeException $exception) {
|
||
return $this->redirectToPage($request, $data['period'])
|
||
->withInput()
|
||
->withErrors(['posting_amount' => $exception->getMessage()]);
|
||
} catch (Throwable $exception) {
|
||
report($exception);
|
||
|
||
return $this->redirectToPage($request, $data['period'])
|
||
->withInput()
|
||
->withErrors(['posting_amount' => '过账标签写入失败,原有标签已保留。']);
|
||
}
|
||
|
||
return $this->redirectToPage($request, $data['period'])->with(
|
||
'success',
|
||
sprintf(
|
||
'整月过账标签已更新:%s 行标记为“是”,其余标记为“补单(订单四组)”;本次变更 %s 行。',
|
||
number_format((int) $plan['selected_rows']),
|
||
number_format((int) $result['updated']),
|
||
),
|
||
);
|
||
}
|
||
|
||
/**
|
||
* @return array{period:string,target_amount:string}
|
||
*/
|
||
private function validatePlan(Request $request): array
|
||
{
|
||
return $request->validate([
|
||
'period' => ['required', 'string', 'regex:/^\d{4}-(0[1-9]|1[0-2])$/'],
|
||
'target_amount' => ['required', 'string', 'regex:/^\d{1,12}(?:\.\d{1,2})?$/', 'not_in:0,0.0,0.00'],
|
||
], $this->messages());
|
||
}
|
||
|
||
/**
|
||
* @return array<string,string>
|
||
*/
|
||
private function messages(): array
|
||
{
|
||
return [
|
||
'period.required' => '请选择需要过账的账期。',
|
||
'period.regex' => '账期格式必须为 YYYY-MM。',
|
||
'target_amount.required' => '请填写整月过账金额。',
|
||
'target_amount.regex' => '整月过账金额最多保留两位小数。',
|
||
'target_amount.not_in' => '整月过账金额必须大于 0。',
|
||
'preview_hash.required' => '请先计算过账方案。',
|
||
'preview_hash.size' => '过账方案已失效,请重新计算。',
|
||
];
|
||
}
|
||
|
||
private function redirectToPage(Request $request, string $period): RedirectResponse
|
||
{
|
||
return redirect()->route('business.pages.show', [
|
||
'company_id' => company_id() ?: 1,
|
||
'section' => 'posting-data',
|
||
'page' => 'kuka-basic-data',
|
||
'submenu' => 'shipments',
|
||
'period' => $period,
|
||
'store_name' => mb_substr(trim((string) $request->input('store_name', '')), 0, 191),
|
||
'status' => mb_substr(trim((string) $request->input('status', '')), 0, 64),
|
||
'keyword' => mb_substr(trim((string) $request->input('keyword', '')), 0, 120),
|
||
'per_page' => max(1, min(200, (int) $request->input('per_page', 50))),
|
||
]);
|
||
}
|
||
}
|