CaiWuXiTong/modules/EcommerceFinance/View/SupplierPayableForecastPageData.php
苏木 73679c55e3 feat(货款预估): 支持秒级筛选和筛选结果导出 (#1468)
支持入库时间精确到秒筛选,并导出当前筛选全集。
Co-authored-by: 苏木 <594326199@qq.com>
Co-committed-by: 苏木 <594326199@qq.com>
2026-11-26 16:42:28 +08:00

132 lines
4.6 KiB
PHP

<?php
namespace Modules\EcommerceFinance\View;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Carbon;
use Modules\EcommerceFinance\Repositories\SupplierPayableForecastRepository;
use Modules\EcommerceFinance\Services\PayablesFinalRegistry;
class SupplierPayableForecastPageData
{
public function __construct(
private readonly SupplierPayableForecastRepository $repository,
private readonly PayablesFinalRegistry $registry,
) {
}
/** @return array<string,mixed> */
public function page(Request $request): array
{
$activeView = in_array($request->query('forecast_view'), ['goods', 'returns', 'summary'], true)
? (string) $request->query('forecast_view')
: 'goods';
$filters = $this->goodsFilters($request);
$syncStatus = $this->repository->syncStatus();
$result = [
'total' => 0,
'quantity' => 0.0,
'supplier_count' => 0,
'rows' => [],
'page' => max((int) $request->query('goods_page', 1), 1),
'per_page' => 20,
];
if ($syncStatus['ready']) {
$result = $this->repository->goodsDetails(
$filters,
max((int) $request->query('goods_page', 1), 1),
20,
);
}
$paginationQuery = array_filter([
'goods_date_from' => $filters['date_from'] === SupplierPayableForecastRepository::DEFAULT_FROM_TIME
? null
: $filters['date_from'],
'goods_date_to' => $filters['date_to'] !== '' ? $filters['date_to'] : null,
'goods_keyword' => $filters['keyword'] !== '' ? $filters['keyword'] : null,
], static fn (mixed $value): bool => $value !== null && $value !== '');
$rows = new LengthAwarePaginator(
$result['rows'],
$result['total'],
$result['per_page'],
$result['page'],
[
'path' => $request->url(),
'query' => $paginationQuery,
],
);
return [
'activeForecastView' => $activeView,
'goodsForecastFields' => $this->registry->fields(
PayablesFinalRegistry::SECTION_SUPPLIER,
'payment-reconciliation',
),
'goodsForecastFilters' => $filters,
'goodsForecastRows' => $rows,
'goodsForecastSummary' => [
'quantity' => $result['quantity'],
'supplier_count' => $result['supplier_count'],
'matched_price_rows' => collect($result['rows'])->where('priceMatched', true)->count(),
'missing_price_rows' => collect($result['rows'])->where('priceMatched', false)->count(),
],
'goodsForecastSyncStatus' => $syncStatus,
];
}
/** @return array{date_from:string,date_to:string,keyword:string} */
public function goodsFilters(Request $request): array
{
$minimum = Carbon::createFromFormat('!Y-m-d H:i:s', SupplierPayableForecastRepository::DEFAULT_FROM_TIME);
$from = $this->validDateTime($request->query('goods_date_from'));
if ($from === null || $from->lt($minimum)) {
$from = $minimum->copy();
}
$to = $this->validDateTime($request->query('goods_date_to'), true);
if ($to !== null && $to->lt($from)) {
$to = $from->copy();
}
return [
'date_from' => $from->format('Y-m-d H:i:s'),
'date_to' => $to?->format('Y-m-d H:i:s') ?? '',
'keyword' => mb_substr(trim((string) $request->query('goods_keyword', '')), 0, 120),
];
}
private function validDateTime(mixed $value, bool $endOfDay = false): ?Carbon
{
if (! is_string($value)) {
return null;
}
$value = trim($value);
$dateOnly = preg_match('/^\d{4}-\d{2}-\d{2}$/D', $value) === 1;
$withTime = preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/D', $value) === 1;
if (! $dateOnly && ! $withTime) {
return null;
}
$format = $dateOnly ? '!Y-m-d' : '!Y-m-d H:i:s';
$date = Carbon::createFromFormat($format, $value);
$errors = Carbon::getLastErrors();
if (! $date
|| ! ($errors === false || ($errors['warning_count'] === 0 && $errors['error_count'] === 0))
|| $date->format($dateOnly ? 'Y-m-d' : 'Y-m-d H:i:s') !== $value) {
return null;
}
if ($dateOnly) {
return $endOfDay ? $date->endOfDay() : $date->startOfDay();
}
return $date;
}
}