289 lines
12 KiB
PHP
289 lines
12 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\Repositories\SupplierPayableForecastSummaryRepository;
|
|
use Modules\EcommerceFinance\Services\PayablesFinalRegistry;
|
|
use Modules\EcommerceFinance\Services\SupplierPayableForecastSyncRunStore;
|
|
|
|
class SupplierPayableForecastPageData
|
|
{
|
|
public function __construct(
|
|
private readonly SupplierPayableForecastRepository $repository,
|
|
private readonly SupplierPayableForecastSummaryRepository $summaryRepository,
|
|
private readonly PayablesFinalRegistry $registry,
|
|
private readonly SupplierPayableForecastSyncRunStore $syncRuns,
|
|
) {
|
|
}
|
|
|
|
/** @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();
|
|
$returnFilters = $this->returnFilters($request);
|
|
$returnSyncStatus = $this->repository->returnSyncStatus();
|
|
$result = [
|
|
'total' => 0,
|
|
'quantity' => 0.0,
|
|
'amount' => 0.0,
|
|
'estimated_amount_after_discount' => 0.0,
|
|
'supplier_count' => 0,
|
|
'sources' => [
|
|
SupplierPayableForecastRepository::SOURCE_PURCHASE => [
|
|
'row_count' => 0,
|
|
'quantity' => 0.0,
|
|
'latest_at' => null,
|
|
],
|
|
SupplierPayableForecastRepository::SOURCE_SHIPPING => [
|
|
'row_count' => 0,
|
|
'quantity' => 0.0,
|
|
'latest_at' => null,
|
|
],
|
|
],
|
|
'rows' => [],
|
|
'page' => max((int) $request->query('goods_page', 1), 1),
|
|
'per_page' => 20,
|
|
];
|
|
$returnResult = [
|
|
'total' => 0,
|
|
'quantity' => 0.0,
|
|
'amount' => 0.0,
|
|
'estimated_amount_after_discount' => 0.0,
|
|
'rows' => [],
|
|
'page' => max((int) $request->query('returns_page', 1), 1),
|
|
'per_page' => 20,
|
|
];
|
|
$summaryResult = [
|
|
'ready' => false,
|
|
'sync_status' => [
|
|
'ready' => false,
|
|
'code' => 'unavailable',
|
|
'status' => 'unavailable',
|
|
'last_succeeded_at' => null,
|
|
],
|
|
'months' => [],
|
|
'filters' => ['month' => '', 'keyword' => ''],
|
|
'total' => 0,
|
|
'rows' => [],
|
|
'page' => max((int) $request->query('summary_page', 1), 1),
|
|
'per_page' => 20,
|
|
'totals' => ['subtotal' => 0.0, 'supplier_balance' => 0.0],
|
|
];
|
|
|
|
if ($syncStatus['ready']) {
|
|
$result = $this->repository->goodsDetails(
|
|
$filters,
|
|
max((int) $request->query('goods_page', 1), 1),
|
|
20,
|
|
);
|
|
}
|
|
if ($returnSyncStatus['ready']) {
|
|
$returnResult = $this->repository->returnDetails(
|
|
$returnFilters,
|
|
max((int) $request->query('returns_page', 1), 1),
|
|
20,
|
|
);
|
|
}
|
|
$summaryResult = $this->summaryRepository->paginate([
|
|
'month' => (string) $request->query('summary_month', ''),
|
|
'keyword' => (string) $request->query('summary_keyword', ''),
|
|
], max((int) $request->query('summary_page', 1), 1));
|
|
|
|
$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,
|
|
'goods_source' => $filters['source'] !== SupplierPayableForecastRepository::SOURCE_ALL
|
|
? $filters['source']
|
|
: 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,
|
|
],
|
|
);
|
|
$returnPaginationQuery = array_filter([
|
|
'forecast_view' => 'returns',
|
|
'returns_date_from' => $returnFilters['date_from'] === SupplierPayableForecastRepository::RETURN_DEFAULT_FROM_TIME
|
|
? null
|
|
: $returnFilters['date_from'],
|
|
'returns_date_to' => $returnFilters['date_to'] !== '' ? $returnFilters['date_to'] : null,
|
|
'returns_keyword' => $returnFilters['keyword'] !== '' ? $returnFilters['keyword'] : null,
|
|
], static fn (mixed $value): bool => $value !== null && $value !== '');
|
|
$returnRows = new LengthAwarePaginator(
|
|
$returnResult['rows'],
|
|
$returnResult['total'],
|
|
$returnResult['per_page'],
|
|
$returnResult['page'],
|
|
[
|
|
'path' => $request->url(),
|
|
'query' => $returnPaginationQuery,
|
|
],
|
|
);
|
|
$summaryPaginationQuery = array_filter([
|
|
'forecast_view' => 'summary',
|
|
'summary_month' => $summaryResult['filters']['month'],
|
|
'summary_keyword' => $summaryResult['filters']['keyword'] !== ''
|
|
? $summaryResult['filters']['keyword']
|
|
: null,
|
|
], static fn (mixed $value): bool => $value !== null && $value !== '');
|
|
$summaryRows = new LengthAwarePaginator(
|
|
$summaryResult['rows'],
|
|
$summaryResult['total'],
|
|
$summaryResult['per_page'],
|
|
$summaryResult['page'],
|
|
[
|
|
'path' => $request->url(),
|
|
'query' => $summaryPaginationQuery,
|
|
],
|
|
);
|
|
|
|
return [
|
|
'activeForecastView' => $activeView,
|
|
'goodsForecastFields' => $this->registry->fields(
|
|
PayablesFinalRegistry::SECTION_SUPPLIER,
|
|
'payment-reconciliation',
|
|
),
|
|
'goodsForecastFilters' => $filters,
|
|
'goodsForecastRows' => $rows,
|
|
'goodsForecastSummary' => [
|
|
'quantity' => $result['quantity'],
|
|
'amount' => $result['amount'],
|
|
'estimated_amount_after_discount' => $result['estimated_amount_after_discount'],
|
|
'supplier_count' => $result['supplier_count'],
|
|
'sources' => $result['sources'],
|
|
'matched_price_rows' => collect($result['rows'])->where('priceMatched', true)->count(),
|
|
'missing_price_rows' => collect($result['rows'])->where('priceMatched', false)->count(),
|
|
],
|
|
'goodsForecastSyncStatus' => $syncStatus,
|
|
'returnForecastFields' => $this->registry->fields(
|
|
PayablesFinalRegistry::SECTION_SUPPLIER,
|
|
'return-reconciliation',
|
|
),
|
|
'returnForecastFilters' => $returnFilters,
|
|
'returnForecastRows' => $returnRows,
|
|
'returnForecastSummary' => [
|
|
'quantity' => $returnResult['quantity'],
|
|
'amount' => $returnResult['amount'],
|
|
'estimated_amount_after_discount' => $returnResult['estimated_amount_after_discount'],
|
|
],
|
|
'returnForecastSyncStatus' => $returnSyncStatus,
|
|
'summaryForecastFilters' => $summaryResult['filters'],
|
|
'summaryForecastMonths' => $summaryResult['months'],
|
|
'summaryForecastRows' => $summaryRows,
|
|
'summaryForecastTotals' => $summaryResult['totals'],
|
|
'summaryForecastSyncStatus' => $summaryResult['sync_status'],
|
|
'forecastSyncRuns' => $this->syncRuns->latestSummary(),
|
|
];
|
|
}
|
|
|
|
/** @return array{date_from:string,date_to:string,keyword:string,source: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);
|
|
$closedThrough = now('Asia/Shanghai')->subDay()->endOfDay();
|
|
if ($to === null || $to->gt($closedThrough)) {
|
|
$to = $closedThrough->copy();
|
|
}
|
|
if ($to->lt($from)) {
|
|
$to = $from->copy();
|
|
}
|
|
|
|
$source = (string) $request->query('goods_source', SupplierPayableForecastRepository::SOURCE_ALL);
|
|
if (! in_array($source, [
|
|
SupplierPayableForecastRepository::SOURCE_ALL,
|
|
SupplierPayableForecastRepository::SOURCE_PURCHASE,
|
|
SupplierPayableForecastRepository::SOURCE_SHIPPING,
|
|
], true)) {
|
|
$source = SupplierPayableForecastRepository::SOURCE_ALL;
|
|
}
|
|
|
|
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),
|
|
'source' => $source,
|
|
];
|
|
}
|
|
|
|
/** @return array{date_from:string,date_to:string,keyword:string} */
|
|
public function returnFilters(Request $request): array
|
|
{
|
|
$minimum = Carbon::createFromFormat(
|
|
'!Y-m-d H:i:s',
|
|
SupplierPayableForecastRepository::RETURN_DEFAULT_FROM_TIME,
|
|
);
|
|
$from = $this->validDateTime($request->query('returns_date_from'));
|
|
if ($from === null || $from->lt($minimum)) {
|
|
$from = $minimum->copy();
|
|
}
|
|
|
|
$to = $this->validDateTime($request->query('returns_date_to'), true);
|
|
$closedThrough = now('Asia/Shanghai')->subDay()->endOfDay();
|
|
if ($to === null || $to->gt($closedThrough)) {
|
|
$to = $closedThrough->copy();
|
|
}
|
|
if ($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('returns_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;
|
|
}
|
|
}
|