CaiWuXiTong/modules/EcommerceFinance/View/SupplierManagementPageData.php
renshen 176357871b feat: 供应商管理新增是否包邮厂家
在供应商名称后新增「是否包邮厂家」,支持行内编辑、模板导入和导出;空白按不是包邮厂家处理。
2026-10-21 11:20:58 +08:00

99 lines
4.5 KiB
PHP

<?php
namespace Modules\EcommerceFinance\View;
use Illuminate\Pagination\LengthAwarePaginator;
use Modules\EcommerceFinance\Repositories\SupplierManagementMirrorRepository;
use Modules\EcommerceFinance\Services\SupplierFreeShippingStore;
use Throwable;
class SupplierManagementPageData
{
public const TABS = ['contracts' => '合同信息', 'contacts' => '联系信息', 'payments' => '打款信息', 'logs' => '操作日志'];
public const FREE_SHIPPING_COLUMN = SupplierFreeShippingStore::FIELD;
public const FREE_SHIPPING_LABEL = '是否包邮厂家';
public function __construct(
private readonly SupplierManagementMirrorRepository $repository,
private readonly SupplierFreeShippingStore $freeShipping,
) {
}
/** 是否包邮厂家紧跟在供应商名称后展示。 */
public static function columns(): array
{
$columns = [];
foreach (SupplierManagementMirrorRepository::COLUMNS as $key => $label) {
$columns[$key] = $label;
if ($key === 'scmname') {
$columns[self::FREE_SHIPPING_COLUMN] = self::FREE_SHIPPING_LABEL;
}
}
return $columns;
}
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);
$result['rows'] = $this->freeShipping->decorateRows(
$result['rows'],
(int) (company_id() ?: 1),
);
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' => self::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')]),
];
}
}