161 lines
5.5 KiB
PHP
161 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\View;
|
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
final class BankBillPageData
|
|
{
|
|
private const TABLE = 'fin_bank_direct_bills';
|
|
|
|
private const PER_PAGE = 100;
|
|
|
|
public function page(): array
|
|
{
|
|
$filters = $this->filters();
|
|
$query = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror'))
|
|
->table(self::TABLE);
|
|
|
|
if ($filters['from'] !== '') {
|
|
$query->where('txn_date', '>=', $filters['from']);
|
|
}
|
|
if ($filters['to'] !== '') {
|
|
$query->where('txn_date', '<=', $filters['to']);
|
|
}
|
|
if ($filters['bank'] !== 'all') {
|
|
$query->where('bank_name', $filters['bank']);
|
|
}
|
|
if ($filters['account_no'] !== '') {
|
|
$query->where('account_no', $filters['account_no']);
|
|
}
|
|
if ($filters['search'] !== '') {
|
|
$search = $filters['search'];
|
|
$query->where(function ($builder) use ($search): void {
|
|
foreach ([
|
|
'company_name',
|
|
'account_short',
|
|
'account_no',
|
|
'counterparty_name',
|
|
'counterparty_account',
|
|
'counterparty_bank',
|
|
'detail',
|
|
'payment_remark',
|
|
'record_code',
|
|
] as $column) {
|
|
$builder->orWhere($column, 'like', '%' . $search . '%');
|
|
}
|
|
});
|
|
}
|
|
|
|
$summaryQuery = clone $query;
|
|
$summary = [
|
|
'count' => (int) (clone $summaryQuery)->count(),
|
|
'income' => round((float) (clone $summaryQuery)->sum('income'), 2),
|
|
'expense' => round((float) (clone $summaryQuery)->sum('expense'), 2),
|
|
'latest_imported_at' => (clone $summaryQuery)->max('imported_at'),
|
|
];
|
|
|
|
$page = min(
|
|
max(1, (int) request('bills_page', 1)),
|
|
max(1, (int) ceil($summary['count'] / self::PER_PAGE)),
|
|
);
|
|
$rows = (clone $query)
|
|
->orderByDesc('txn_date')
|
|
->orderByDesc('txn_at')
|
|
->orderByDesc('id')
|
|
->forPage($page, self::PER_PAGE)
|
|
->get();
|
|
$paginator = new LengthAwarePaginator(
|
|
$rows->values()->all(),
|
|
$summary['count'],
|
|
self::PER_PAGE,
|
|
$page,
|
|
['path' => request()->url(), 'query' => request()->query()],
|
|
);
|
|
$paginator->setPageName('bills_page');
|
|
|
|
return [
|
|
'filters' => $filters,
|
|
'period' => ['from' => $filters['from'], 'to' => $filters['to']],
|
|
'summary' => $summary,
|
|
'page' => $paginator,
|
|
'bankOptions' => $this->bankOptions(),
|
|
'accountOptions' => $this->accountOptions($filters['bank']),
|
|
];
|
|
}
|
|
|
|
private function filters(): array
|
|
{
|
|
$defaultFrom = now()->startOfMonth()->toDateString();
|
|
$defaultTo = now()->endOfMonth()->toDateString();
|
|
$from = $this->date((string) request('bills_from', '')) ?? $defaultFrom;
|
|
$to = $this->date((string) request('bills_to', '')) ?? $defaultTo;
|
|
if ($from > $to) {
|
|
[$from, $to] = [$to, $from];
|
|
}
|
|
|
|
return [
|
|
'from' => $from,
|
|
'to' => $to,
|
|
'bank' => trim((string) request('bills_bank', 'all')) ?: 'all',
|
|
'account_no' => preg_replace('/\D+/', '', (string) request('bills_account_no', '')) ?? '',
|
|
'search' => trim((string) request('bills_search', '')),
|
|
];
|
|
}
|
|
|
|
private function date(string $value): ?string
|
|
{
|
|
if ($value === '') {
|
|
return null;
|
|
}
|
|
$date = \DateTimeImmutable::createFromFormat('!Y-m-d', $value);
|
|
|
|
return $date !== false && $date->format('Y-m-d') === $value ? $value : null;
|
|
}
|
|
|
|
private function bankOptions(): array
|
|
{
|
|
return DB::connection((string) config('upstream_mirrors.connection', 'business_mirror'))
|
|
->table(self::TABLE)
|
|
->whereNotNull('bank_name')
|
|
->where('bank_name', '<>', '')
|
|
->distinct()
|
|
->orderBy('bank_name')
|
|
->pluck('bank_name')
|
|
->map(static fn (mixed $value): string => trim((string) $value))
|
|
->filter()
|
|
->values()
|
|
->all();
|
|
}
|
|
|
|
private function accountOptions(string $bank): array
|
|
{
|
|
$query = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror'))
|
|
->table(self::TABLE)
|
|
->select(['bank_name', 'company_name', 'account_short', 'account_no'])
|
|
->distinct();
|
|
if ($bank !== 'all') {
|
|
$query->where('bank_name', $bank);
|
|
}
|
|
|
|
return $query
|
|
->orderBy('bank_name')
|
|
->orderBy('account_short')
|
|
->get()
|
|
->map(static fn (object $row): array => [
|
|
'bank_name' => (string) $row->bank_name,
|
|
'company_name' => (string) ($row->company_name ?? ''),
|
|
'account_short' => (string) $row->account_short,
|
|
'account_no' => (string) $row->account_no,
|
|
'label' => trim(implode(' · ', array_filter([
|
|
(string) $row->account_short,
|
|
(string) $row->account_no,
|
|
(string) ($row->company_name ?? ''),
|
|
]))),
|
|
])
|
|
->values()
|
|
->all();
|
|
}
|
|
}
|