80 lines
2.4 KiB
PHP
80 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\View;
|
|
|
|
use Illuminate\Pagination\Paginator;
|
|
use Illuminate\Support\Collection;
|
|
use Throwable;
|
|
|
|
class BillOffsetPageData
|
|
{
|
|
public function __construct(private readonly OrderDataPageData $orderData)
|
|
{
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
public function page(): array
|
|
{
|
|
$filters = $this->orderData->afterSalesFilters();
|
|
$page = max((int) request('page', 1), 1);
|
|
$limit = min(max((int) setting('default.list_limit', 25), 1), 100);
|
|
$rows = $this->emptyPaginator($page, $limit);
|
|
$status = null;
|
|
$pageAmount = null;
|
|
$error = null;
|
|
|
|
try {
|
|
$result = $this->orderData->afterSalesRows($filters, $page, $limit);
|
|
$rows = new Paginator(
|
|
Collection::make($result['rows'] ?? [])->map(static fn (array $row): object => (object) $row),
|
|
$limit,
|
|
$page,
|
|
[
|
|
'path' => request()->url(),
|
|
'query' => request()->except('page'),
|
|
],
|
|
);
|
|
$rows->hasMorePagesWhen((bool) ($result['has_more'] ?? false));
|
|
$status = data_get($result, 'status.X-Caiwu-Data-Status');
|
|
$pageAmount = $this->pageAmount($rows->items());
|
|
} catch (Throwable $exception) {
|
|
report($exception);
|
|
$error = '订单售后费用数据暂时无法读取,请稍后重试。';
|
|
}
|
|
|
|
return [
|
|
'view' => 'business.pages.order-data.bill-offset',
|
|
'billOffsetRows' => $rows,
|
|
'billOffsetFilters' => $filters,
|
|
'billOffsetStatus' => $status,
|
|
'billOffsetPageAmount' => $pageAmount,
|
|
'billOffsetError' => $error,
|
|
];
|
|
}
|
|
|
|
/** @param array<int,mixed> $items */
|
|
private function pageAmount(array $items): ?float
|
|
{
|
|
$amount = null;
|
|
|
|
foreach ($items as $item) {
|
|
$value = data_get($item, 'account_500112');
|
|
if ($value === null || $value === '') {
|
|
continue;
|
|
}
|
|
|
|
$amount = ($amount ?? 0.0) + (float) $value;
|
|
}
|
|
|
|
return $amount === null ? null : round($amount, 2);
|
|
}
|
|
|
|
private function emptyPaginator(int $page, int $limit): Paginator
|
|
{
|
|
return new Paginator([], $limit, $page, [
|
|
'path' => request()->url(),
|
|
'query' => request()->except('page'),
|
|
]);
|
|
}
|
|
}
|