74 lines
3.6 KiB
PHP
74 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\View;
|
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Modules\EcommerceFinance\Repositories\SupplierManagementMirrorRepository;
|
|
use Throwable;
|
|
|
|
class SupplierManagementPageData
|
|
{
|
|
public const TABS = ['contracts' => '合同信息', 'contacts' => '联系信息', 'payments' => '打款信息', 'logs' => '操作日志'];
|
|
|
|
public function __construct(private readonly SupplierManagementMirrorRepository $repository)
|
|
{
|
|
}
|
|
|
|
public function filters(array $input): array
|
|
{
|
|
$text = static fn ($key): string => mb_substr(trim(is_scalar($input[$key] ?? '') ? (string) ($input[$key] ?? '') : ''), 0, 191);
|
|
return ['supplier_name' => $text('supplier_name'), 'buyer' => $text('buyer'),
|
|
'cooperation_status' => in_array($text('cooperation_status'), ['1', '2'], true) ? $text('cooperation_status') : ''];
|
|
}
|
|
|
|
public function page(): array
|
|
{
|
|
$filters = $this->filters(request()->query());
|
|
$page = max((int) request('page', 1), 1);
|
|
$limit = min(max((int) request('limit', 20), 1), 100);
|
|
$requestedTab = request('detail_tab', 'contracts');
|
|
$tab = is_string($requestedTab) && array_key_exists($requestedTab, self::TABS) ? $requestedTab : 'contracts';
|
|
$result = ['rows' => [], 'total' => 0];
|
|
$detail = ['rows' => [], 'total' => 0, 'message' => '请选择上方供应商查看详情。', 'ready' => false];
|
|
$state = null;
|
|
$ready = false;
|
|
$error = null;
|
|
$selected = null;
|
|
try {
|
|
$state = $this->repository->state();
|
|
$ready = $this->repository->ready();
|
|
if ($ready) {
|
|
$result = $this->repository->paginate($filters, $page, $limit);
|
|
if ((int) request('supplier_id', 0) > 0) {
|
|
// 选择必须仍在当前列表中;筛选和翻页后清除旧选择。
|
|
$selected = collect($result['rows'])->firstWhere('Id', (int) request('supplier_id'));
|
|
if ($selected) {
|
|
try {
|
|
$detail = $this->repository->details($selected, $tab, max((int) request('detail_page', 1), 1), $limit);
|
|
} catch (Throwable $exception) {
|
|
report($exception);
|
|
$detail['message'] = '该详情资料暂时无法读取,请稍后重试。';
|
|
}
|
|
} else {
|
|
$detail['message'] = '该供应商不在当前列表,请重新选择。';
|
|
}
|
|
}
|
|
}
|
|
} catch (Throwable $exception) {
|
|
report($exception);
|
|
$error = '供应商资料暂时无法读取,请稍后重试。';
|
|
}
|
|
return [
|
|
'view' => 'business.pages.supplier-payables.supplier-management',
|
|
'supplierFilters' => $filters, 'supplierState' => $state, 'supplierReady' => $ready,
|
|
'supplierError' => $error, 'supplierColumns' => SupplierManagementMirrorRepository::COLUMNS,
|
|
'suppliers' => new LengthAwarePaginator($result['rows'], $result['total'], $limit, $page,
|
|
['path' => request()->url(), 'query' => request()->except('page', 'supplier_id', 'detail_page')]),
|
|
'selectedSupplier' => $selected, 'supplierDetailTab' => $tab,
|
|
'supplierDetail' => $detail,
|
|
'supplierDetails' => new LengthAwarePaginator($detail['rows'], $detail['total'], $limit, max((int) request('detail_page', 1), 1),
|
|
['path' => request()->url(), 'pageName' => 'detail_page', 'query' => request()->except('detail_page')]),
|
|
];
|
|
}
|
|
}
|