174 lines
7.6 KiB
PHP
174 lines
7.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\View;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Modules\EcommerceFinance\Repositories\DingInvoiceDataMirrorRepository;
|
|
use Throwable;
|
|
|
|
class InvoiceDataPageData
|
|
{
|
|
public function __construct(
|
|
private readonly DingInvoiceDataMirrorRepository $repository,
|
|
) {}
|
|
|
|
public static function normalizePerPage(mixed $value): int
|
|
{
|
|
$perPage = (int) $value;
|
|
|
|
return in_array($perPage, DingInvoiceDataMirrorRepository::PER_PAGE_OPTIONS, true)
|
|
? $perPage
|
|
: 50;
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
public function page(Request $request): array
|
|
{
|
|
try {
|
|
$months = $this->repository->months();
|
|
} catch (Throwable $exception) {
|
|
report($exception);
|
|
$months = [];
|
|
}
|
|
$requestedMonth = trim((string) $request->query('month', ''));
|
|
$month = preg_match('/^\d{4}-\d{2}$/', $requestedMonth) === 1
|
|
? $requestedMonth
|
|
: (string) ($months[0] ?? '');
|
|
$view = $request->query('invoice_view') === DingInvoiceDataMirrorRepository::VIEW_FLOWS
|
|
? DingInvoiceDataMirrorRepository::VIEW_FLOWS
|
|
: DingInvoiceDataMirrorRepository::VIEW_DETAIL;
|
|
$invoiceState = array_key_exists(
|
|
(string) $request->query('invoice_state', 'all'),
|
|
DingInvoiceDataMirrorRepository::INVOICE_STATES,
|
|
) ? (string) $request->query('invoice_state', 'all') : 'all';
|
|
$filters = [
|
|
'month' => $month,
|
|
'invoice_state' => $invoiceState,
|
|
'invoice_type' => mb_substr(trim((string) $request->query('invoice_type', '')), 0, 100),
|
|
'keyword' => mb_substr(trim((string) $request->query('keyword', '')), 0, 100),
|
|
];
|
|
$detailPage = max((int) $request->query('page', 1), 1);
|
|
$flowPage = max((int) $request->query('flow_page', 1), 1);
|
|
$perPage = self::normalizePerPage($request->query('per_page'));
|
|
|
|
try {
|
|
$options = $this->repository->filterOptions($filters);
|
|
$detailFilters = [...$filters, 'view' => DingInvoiceDataMirrorRepository::VIEW_DETAIL];
|
|
$flowFilters = [...$filters, 'view' => DingInvoiceDataMirrorRepository::VIEW_FLOWS];
|
|
$detailResult = $this->repository->paginate($detailFilters, $detailPage, $perPage);
|
|
$flowResult = $this->repository->paginate($flowFilters, $flowPage, $perPage);
|
|
$detailPaginator = new LengthAwarePaginator(
|
|
$detailResult['data'],
|
|
$detailResult['total'],
|
|
$perPage,
|
|
$detailPage,
|
|
[
|
|
'path' => $request->url(),
|
|
'query' => array_filter(
|
|
[...$filters, 'invoice_view' => 'detail', 'per_page' => $perPage],
|
|
static fn (mixed $value): bool => $value !== '' && $value !== null,
|
|
),
|
|
],
|
|
);
|
|
$flowPaginator = new LengthAwarePaginator(
|
|
$flowResult['data'],
|
|
$flowResult['total'],
|
|
$perPage,
|
|
$flowPage,
|
|
[
|
|
'path' => $request->url(),
|
|
'query' => array_filter(
|
|
[...$filters, 'invoice_view' => 'flows', 'per_page' => $perPage],
|
|
static fn (mixed $value): bool => $value !== '' && $value !== null,
|
|
),
|
|
'pageName' => 'flow_page',
|
|
],
|
|
);
|
|
$state = $detailResult['state'];
|
|
$mirrorStatus = (string) (
|
|
$state['status']
|
|
?? ((int) ($detailResult['summary']['flows'] ?? 0) > 0 ? 'imported' : 'unavailable')
|
|
);
|
|
|
|
return [
|
|
'view' => 'business.pages.financial-operations.invoice-data',
|
|
'invoiceDetailRows' => $detailPaginator,
|
|
'invoiceFlowRows' => $flowPaginator,
|
|
'invoiceActiveView' => $view,
|
|
'invoiceFilters' => $filters,
|
|
'invoiceMonths' => $months,
|
|
'invoiceTypes' => $options['invoice_types'],
|
|
'invoiceStates' => DingInvoiceDataMirrorRepository::INVOICE_STATES,
|
|
'invoicePerPageOptions' => DingInvoiceDataMirrorRepository::PER_PAGE_OPTIONS,
|
|
'invoicePerPage' => $perPage,
|
|
'invoiceSummary' => $detailResult['summary'],
|
|
'invoiceMirrorStatus' => $mirrorStatus,
|
|
'invoiceMirrorSyncedAt' => $state['last_succeeded_at'] ?? null,
|
|
'invoiceMirrorError' => null,
|
|
'invoicePageUrl' => route('business.pages.show', [
|
|
'section' => 'process-data',
|
|
'page' => 'invoice-data',
|
|
]),
|
|
'invoiceExportUrl' => route('business.invoice-data.export', array_filter([
|
|
'month' => $filters['month'],
|
|
'invoice_view' => $view,
|
|
'invoice_state' => $filters['invoice_state'],
|
|
'invoice_type' => $filters['invoice_type'],
|
|
'keyword' => $filters['keyword'],
|
|
], static fn (mixed $value): bool => $value !== '' && $value !== null)),
|
|
'invoiceRefreshUrl' => route('business.invoice-data.refresh'),
|
|
'invoiceOcrConfigured' => trim((string) config(
|
|
'ding_invoice_data.ocr.api_key',
|
|
'',
|
|
)) !== '',
|
|
'invoiceScope' => $this->repository->scope(),
|
|
];
|
|
} catch (Throwable $exception) {
|
|
report($exception);
|
|
|
|
return [
|
|
'view' => 'business.pages.financial-operations.invoice-data',
|
|
'invoiceDetailRows' => new LengthAwarePaginator([], 0, $perPage, 1, [
|
|
'path' => $request->url(),
|
|
'query' => $request->query(),
|
|
]),
|
|
'invoiceFlowRows' => new LengthAwarePaginator([], 0, $perPage, 1, [
|
|
'path' => $request->url(),
|
|
'query' => $request->query(),
|
|
]),
|
|
'invoiceActiveView' => $view,
|
|
'invoiceFilters' => $filters,
|
|
'invoiceMonths' => $months,
|
|
'invoiceTypes' => [],
|
|
'invoiceStates' => DingInvoiceDataMirrorRepository::INVOICE_STATES,
|
|
'invoicePerPageOptions' => DingInvoiceDataMirrorRepository::PER_PAGE_OPTIONS,
|
|
'invoicePerPage' => $perPage,
|
|
'invoiceSummary' => [
|
|
'flows' => 0,
|
|
'payment_amount' => 0.0,
|
|
'invoice_count' => 0,
|
|
'invoice_total' => 0.0,
|
|
'no_invoice_flows' => 0,
|
|
'needs_review_flows' => 0,
|
|
'invoice_rate' => 0.0,
|
|
],
|
|
'invoiceMirrorStatus' => 'unavailable',
|
|
'invoiceMirrorSyncedAt' => null,
|
|
'invoiceMirrorError' => '发票数据镜像尚未完成导入,请稍后重试。',
|
|
'invoicePageUrl' => route('business.pages.show', [
|
|
'section' => 'process-data',
|
|
'page' => 'invoice-data',
|
|
]),
|
|
'invoiceExportUrl' => '#',
|
|
'invoiceRefreshUrl' => route('business.invoice-data.refresh'),
|
|
'invoiceOcrConfigured' => trim((string) config(
|
|
'ding_invoice_data.ocr.api_key',
|
|
'',
|
|
)) !== '',
|
|
'invoiceScope' => $this->repository->scope(),
|
|
];
|
|
}
|
|
}
|
|
}
|