connection = $databases->connection( (string) config('upstream_mirrors.connection', 'business_mirror'), ); } public function ready(): bool { return Schema::connection($this->connection->getName())->hasTable(self::TABLE); } /** * @return array{ * rows:LengthAwarePaginator, * filters:array, * options:array>, * summary:array, * imported_at:mixed, * source_file:?string, * ready:bool * } */ public function page(Request $request): array { $filters = $this->filters($request); if (! $this->ready()) { return [ 'rows' => new Paginator([], 0, self::PER_PAGE, 1, [ 'path' => $request->url(), 'query' => $request->query(), ]), 'filters' => $filters, 'options' => $this->emptyOptions(), 'summary' => $this->emptySummary(), 'imported_at' => null, 'source_file' => null, 'ready' => false, ]; } $query = $this->filteredQuery($filters); $summary = $this->summary($query); $sortable = array_values(array_map( static fn (array $column): string => (string) $column['field'], array_filter( (array) config('actual_tax.columns', []), static fn (array $column): bool => ($column['sortable'] ?? true) !== false, ), )); $sort = (string) $request->query('sort', ''); $sort = in_array($sort, $sortable, true) ? $sort : ''; $direction = strtolower((string) $request->query('direction', 'asc')) === 'desc' ? 'desc' : 'asc'; if ($sort !== '') { $query->orderBy($sort, $direction); } else { $query->orderByDesc('period')->orderBy('sort_order'); } $query->orderBy('id'); $rows = $query->paginate( self::PER_PAGE, ['*'], 'page', max((int) $request->query('page', 1), 1), )->withQueryString(); $latest = $this->connection->table(self::TABLE) ->orderByDesc('imported_at') ->first(['imported_at', 'source_file']); return [ 'rows' => $rows, 'filters' => $filters, 'options' => $this->filterOptions(), 'summary' => $summary, 'imported_at' => $latest?->imported_at, 'source_file' => $latest?->source_file, 'ready' => true, ]; } /** * @return array */ private function filters(Request $request): array { $period = trim((string) $request->query('period', '')); if (preg_match('/^\d{6}$/', $period) !== 1) { $period = ''; } return [ 'period' => $period, 'type' => $this->filterText($request->query('type'), 16), 'region' => $this->filterText($request->query('region'), 16), 'taxpayer_type' => $this->filterText($request->query('taxpayer_type'), 32), 'accounting_company' => $this->filterText($request->query('accounting_company'), 191), 'contact_person' => $this->filterText($request->query('contact_person'), 64), 'company_name' => $this->filterText($request->query('company_name'), 191), ]; } /** * @param array $filters */ private function filteredQuery(array $filters): Builder { $query = $this->connection->table(self::TABLE); foreach (['period', 'type', 'region', 'taxpayer_type', 'accounting_company', 'contact_person'] as $field) { if (($filters[$field] ?? '') !== '') { $query->where($field, $filters[$field]); } } if (($filters['company_name'] ?? '') !== '') { $query->where( 'company_name', 'like', '%' . addcslashes($filters['company_name'], '%_\\') . '%', ); } return $query; } /** * @return array */ private function summary(Builder $query): array { $aggregate = (clone $query) ->selectRaw('COUNT(*) AS records') ->selectRaw('COUNT(DISTINCT company_name) AS companies') ->selectRaw('COALESCE(SUM(total_revenue), 0) AS total_revenue') ->selectRaw('COALESCE(SUM(total_tax_paid), 0) AS total_tax_paid') ->first(); $revenue = round((float) ($aggregate->total_revenue ?? 0), 2); $tax = round((float) ($aggregate->total_tax_paid ?? 0), 2); return [ 'records' => (int) ($aggregate->records ?? 0), 'companies' => (int) ($aggregate->companies ?? 0), 'total_revenue' => $revenue, 'total_tax_paid' => $tax, 'tax_burden_rate' => $revenue > 0 ? $tax / $revenue : null, ]; } /** * @return array> */ private function filterOptions(): array { $options = []; foreach ([ 'period', 'type', 'region', 'taxpayer_type', 'accounting_company', 'contact_person', ] as $field) { $direction = $field === 'period' ? 'desc' : 'asc'; $options[$field] = $this->connection->table(self::TABLE) ->whereNotNull($field) ->where($field, '<>', '') ->distinct() ->orderBy($field, $direction) ->pluck($field) ->all(); } return $options; } /** * @return array> */ private function emptyOptions(): array { return [ 'period' => [], 'type' => [], 'region' => [], 'taxpayer_type' => [], 'accounting_company' => [], 'contact_person' => [], ]; } /** * @return array */ private function emptySummary(): array { return [ 'records' => 0, 'companies' => 0, 'total_revenue' => 0.0, 'total_tax_paid' => 0.0, 'tax_burden_rate' => null, ]; } private function filterText(mixed $value, int $limit): string { return mb_substr(trim((string) $value), 0, $limit); } }