117 lines
5.8 KiB
PHP
117 lines
5.8 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Business;
|
||
|
||
use App\Support\CourierBills\PayableSummary;
|
||
use Illuminate\Database\QueryException;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Routing\Controller;
|
||
use Illuminate\Validation\Rule;
|
||
use Illuminate\Validation\ValidationException;
|
||
use RuntimeException;
|
||
|
||
/** 快递应付汇总:所有承运商的月度应付,格式同手工快递汇总表。 */
|
||
final class CourierPayableSummaryController extends Controller
|
||
{
|
||
public const STATUSES = ['已确认', '待处理', '本月无账单', '旧账挂账'];
|
||
|
||
public function __construct(private readonly PayableSummary $summary) {}
|
||
|
||
public function page(Request $request): array
|
||
{
|
||
$data = ['view' => 'business.pages.courier-payables.courier-payable-summary', 'psSection' => CourierBillController::section($request),
|
||
'psUnavailable' => false, 'psRows' => [], 'psMonths' => [], 'psMonth' => null];
|
||
try {
|
||
$months = $this->summary->months();
|
||
$month = is_string($request->query('month')) && in_array($request->query('month'), $months, true) ? $request->query('month') : ($months[0] ?? null);
|
||
$rows = $month === null ? [] : $this->summary->rows($month);
|
||
$status = in_array($request->query('status'), self::STATUSES, true) ? $request->query('status') : null;
|
||
$keyword = trim((string) $request->query('q'));
|
||
$filtered = array_values(array_filter($rows, static function (array $row) use ($status, $keyword, $request): bool {
|
||
$c = $row['cells'];
|
||
foreach (['company' => 'C', 'owner' => 'D', 'state' => 'E'] as $param => $letter) {
|
||
if (filled($request->query($param)) && (string) ($c[$letter] ?? '') !== (string) $request->query($param)) {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
return ($status === null || $row['status'] === $status)
|
||
&& ($keyword === '' || str_contains((string) ($c['H'] ?? ''), $keyword) || str_contains((string) ($c['G'] ?? ''), $keyword));
|
||
}));
|
||
$counts = array_count_values(array_column($rows, 'status'));
|
||
$options = static fn (string $letter): array => collect($rows)->pluck('cells.' . $letter)->filter()->unique()->sort()->values()->all();
|
||
$data = array_merge($data, ['psMonths' => $months, 'psMonth' => $month, 'psRows' => $filtered, 'psAll' => count($rows),
|
||
'psCounts' => $counts, 'psStatus' => $status, 'psCompanies' => $options('C'), 'psOwners' => $options('D'),
|
||
'psTotals' => collect(PayableSummary::MONEY)->mapWithKeys(fn ($k) => [$k => array_sum(array_map(fn ($r) => (float) ($r['cells'][$k] ?? 0), $filtered))])->all()]);
|
||
} catch (QueryException $error) {
|
||
report($error);
|
||
$data['psUnavailable'] = true;
|
||
}
|
||
|
||
return $data;
|
||
}
|
||
|
||
public function import(Request $request)
|
||
{
|
||
$data = $request->validate(['month' => ['required', 'date_format:Y-m'], 'summary' => ['required', 'file', 'max:51200']],
|
||
[], ['summary' => '快递汇总表', 'month' => '月份']);
|
||
if (strtolower($data['summary']->getClientOriginalExtension()) !== 'xlsx') {
|
||
throw ValidationException::withMessages(['summary' => '请上传 xlsx 格式的快递汇总表。']);
|
||
}
|
||
$result = $this->guard(fn () => $this->summary->import($data['summary']->getRealPath(), $data['month']));
|
||
|
||
return $this->back($request, $data['month'], '已导入 ' . $data['month'] . ' 快递汇总 ' . $result['rows'] . ' 个承运商。');
|
||
}
|
||
|
||
public function createMonth(Request $request)
|
||
{
|
||
$month = $request->validate(['month' => ['required', 'date_format:Y-m']])['month'];
|
||
$count = $this->guard(fn () => $this->summary->createMonth($month));
|
||
|
||
return $this->back($request, $month, '已生成 ' . $month . ':' . $count . ' 个承运商,月初结余按上月「本月应付 − 本次实付」结转。');
|
||
}
|
||
|
||
public function update(Request $request, int $row)
|
||
{
|
||
$data = $request->validate(['column' => ['required', Rule::in(array_keys(PayableSummary::EDITABLE))], 'value' => ['nullable', 'string', 'max:500']]);
|
||
$this->guard(fn () => $this->summary->updateCell($row, $data['column'], $data['value'] ?? null));
|
||
if ($request->expectsJson()) {
|
||
return response()->json(['ok' => true]);
|
||
}
|
||
|
||
return redirect()->to(url()->previous())->with('courier_summary_message', '已保存。');
|
||
}
|
||
|
||
public function addCarrier(Request $request)
|
||
{
|
||
$data = $request->validate(['month' => ['required', 'date_format:Y-m'], 'carrier' => ['required', 'string', 'max:128']], [], ['carrier' => '承运商']);
|
||
$this->guard(fn () => $this->summary->addCarrier($data['month'], $data['carrier']));
|
||
|
||
return $this->back($request, $data['month'], '已新增承运商。');
|
||
}
|
||
|
||
public function export(Request $request)
|
||
{
|
||
$month = $request->validate(['month' => ['required', 'date_format:Y-m']])['month'];
|
||
|
||
return response()->streamDownload(function () use ($month): void {
|
||
$this->summary->export($month, fopen('php://output', 'wb'));
|
||
}, '快递应付汇总_' . $month . '.xlsx');
|
||
}
|
||
|
||
private function guard(callable $action): mixed
|
||
{
|
||
try {
|
||
return $action();
|
||
} catch (RuntimeException $error) {
|
||
throw ValidationException::withMessages(['courier_summary' => $error->getMessage()]);
|
||
}
|
||
}
|
||
|
||
private function back(Request $request, string $month, string $message)
|
||
{
|
||
return redirect()->route('business.pages.show', ['section' => CourierBillController::section($request),
|
||
'page' => 'courier-payable-summary', 'month' => $month])->with('courier_summary_message', $message);
|
||
}
|
||
}
|