88 lines
3.0 KiB
PHP
88 lines
3.0 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)
|
|
{
|
|
}
|
|
|
|
/** 账单冲减复用原始数据的订单售后费用明细,不重新推断科目。 */
|
|
public function page(): array
|
|
{
|
|
return ['view' => 'business.pages.order-data.bill-offset'] + $this->data();
|
|
}
|
|
|
|
/** @param array<string,mixed>|null $input */
|
|
public function data(?array $input = null, ?int $page = null, ?int $limit = null): array
|
|
{
|
|
$filters = $this->orderData->afterSalesFilters($input);
|
|
|
|
return $this->dataForNormalizedFilters(
|
|
$filters,
|
|
$page ?? (int) request('page', 1),
|
|
$limit ?? (int) setting('default.list_limit', 25),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Reuse an already normalized filter set for subsequent export pages.
|
|
*
|
|
* @param array{from:string,to:string,order_no:string,shop_name:string,sku_id:string} $filters
|
|
* @return array<string,mixed>
|
|
*/
|
|
public function dataForNormalizedFilters(array $filters, int $page, int $limit): array
|
|
{
|
|
$page = max($page, 1);
|
|
$limit = min(max($limit, 1), 100);
|
|
$rows = $this->emptyPaginator($page, $limit);
|
|
$status = 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');
|
|
} catch (Throwable $exception) {
|
|
report($exception);
|
|
$error = '订单售后费用数据暂时无法读取,请稍后重试。';
|
|
}
|
|
|
|
$amounts = Collection::make($rows->items())->pluck('account_500112');
|
|
// 500112 未接入时保留为空;本页有任何未知金额时不生成误导性合计。
|
|
$amount = $amounts->isNotEmpty() && $amounts->every(static fn ($value): bool => is_numeric($value))
|
|
? round((float) $amounts->sum(), 2)
|
|
: null;
|
|
|
|
return [
|
|
'billOffsetFilters' => $filters,
|
|
'billOffsetRows' => $rows,
|
|
'billOffsetSummary' => ['count' => $rows->count(), 'amount' => $amount],
|
|
'billOffsetStatus' => $status,
|
|
'billOffsetError' => $error,
|
|
];
|
|
}
|
|
|
|
private function emptyPaginator(int $page, int $limit): Paginator
|
|
{
|
|
return new Paginator([], $limit, $page, [
|
|
'path' => request()->url(),
|
|
'query' => request()->except('page'),
|
|
]);
|
|
}
|
|
}
|