CaiWuXiTong/modules/EcommerceFinance/View/SupplierPayableForecastPageData.php
苏木 205cdfe50e feat(供应商货款预估): 接入采购入库与变价价格 (#1347)
接入聚水潭采购入库明细、固定剔除规则和工作平台最新变价后采购价。
Co-authored-by: 苏木 <594326199@qq.com>
Co-committed-by: 苏木 <594326199@qq.com>
2026-11-21 15:59:27 +08:00

118 lines
4.1 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
? 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} */
private function goodsFilters(Request $request): array
{
$minimum = Carbon::createFromFormat('!Y-m-d', SupplierPayableForecastRepository::DEFAULT_FROM);
$from = $this->validDate($request->query('goods_date_from'));
if ($from === null || $from->lt($minimum)) {
$from = $minimum;
}
$to = $this->validDate($request->query('goods_date_to'));
if ($to !== null && $to->lt($from)) {
$to = $from->copy();
}
return [
'date_from' => $from->format('Y-m-d'),
'date_to' => $to?->format('Y-m-d') ?? '',
'keyword' => mb_substr(trim((string) $request->query('goods_keyword', '')), 0, 120),
];
}
private function validDate(mixed $value): ?Carbon
{
if (! is_string($value) || preg_match('/^\d{4}-\d{2}-\d{2}$/D', $value) !== 1) {
return null;
}
$date = Carbon::createFromFormat('!Y-m-d', $value);
$errors = Carbon::getLastErrors();
return $date !== false
&& ($errors === false || ($errors['warning_count'] === 0 && $errors['error_count'] === 0))
&& $date->format('Y-m-d') === $value
? $date
: null;
}
}