*/ 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; } }