From ff0618774d28ad6ad08c827062f55f8634de492e Mon Sep 17 00:00:00 2001 From: limeng <1131177064@qq.com> Date: Mon, 12 Oct 2026 14:45:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BF=AB=E9=80=92=E5=BA=94=E4=BB=98=EF=BC=9A?= =?UTF-8?q?=E4=BB=93=E5=82=A8=E6=A0=B8=E7=AE=97=E7=A7=BB=E4=B8=BA=E7=8B=AC?= =?UTF-8?q?=E7=AB=8B=E4=BB=93=E5=82=A8=E8=BF=90=E8=B4=B9=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Business/Pages.php | 1 + .../Business/ThirdPartyWarehouseQuotes.php | 288 ---------------- .../Livewire/Business/WarehouseFreight.php | 323 ++++++++++++++++++ config/business_navigation.php | 1 + .../third-party-warehouse-quotes.blade.php | 235 +------------ .../partials/warehouse-freight.blade.php | 158 +++++++++ .../partials/warehouse-styles.blade.php | 99 ++++++ .../warehouse-freight.blade.php | 1 + .../ThirdPartyWarehouseQuotePageTest.php | 144 +------- .../Business/WarehouseFreightPageTest.php | 289 ++++++++++++++++ 10 files changed, 876 insertions(+), 663 deletions(-) create mode 100644 app/Http/Livewire/Business/WarehouseFreight.php create mode 100644 resources/views/business/pages/courier-payables/partials/warehouse-freight.blade.php create mode 100644 resources/views/business/pages/courier-payables/partials/warehouse-styles.blade.php create mode 100644 resources/views/business/pages/courier-payables/warehouse-freight.blade.php create mode 100644 tests/Feature/Business/WarehouseFreightPageTest.php diff --git a/app/Http/Controllers/Business/Pages.php b/app/Http/Controllers/Business/Pages.php index aa11cafe..be2b0f74 100644 --- a/app/Http/Controllers/Business/Pages.php +++ b/app/Http/Controllers/Business/Pages.php @@ -2668,6 +2668,7 @@ class Pages extends Controller 'courier-information' => $this->contentForCourierInformation(), 'freight-reconciliation' => $this->contentForFreightReconciliation(), 'third-party-warehouse-storage' => $this->contentForThirdPartyWarehouseStorage(), + 'warehouse-freight' => ['view' => 'business.pages.courier-payables.warehouse-freight'], 'board-deductions' => $this->contentForBoardDeductions(), 'supplier-payables-report' => $this->contentForSupplierPayablesReport(), 'supplier-management' => app(\Modules\EcommerceFinance\View\SupplierManagementPageData::class)->page(), diff --git a/app/Http/Livewire/Business/ThirdPartyWarehouseQuotes.php b/app/Http/Livewire/Business/ThirdPartyWarehouseQuotes.php index 3427a468..efb2183b 100644 --- a/app/Http/Livewire/Business/ThirdPartyWarehouseQuotes.php +++ b/app/Http/Livewire/Business/ThirdPartyWarehouseQuotes.php @@ -3,21 +3,12 @@ namespace App\Http\Livewire\Business; use App\Business\ThirdPartyWarehouseQuoteRepository; -use App\Business\ThirdPartyWarehouseStorageFeeCalculator; -use App\Exports\ThirdPartyWarehouseStorageDetailExport; -use App\Exports\ThirdPartyWarehouseStorageSettlementExport; use App\Models\Business\ThirdPartyWarehouseQuote; -use App\Models\Business\ThirdPartyWarehouseStorageSettlement; use Illuminate\Contracts\View\View; use Livewire\Component; -use Maatwebsite\Excel\Facades\Excel; -use Symfony\Component\HttpFoundation\BinaryFileResponse; class ThirdPartyWarehouseQuotes extends Component { - /** 固定核算「三方-南昌樵舍仓」,与镜像仓库一致。 */ - public const WAREHOUSE_ID = 15045364; - public string $warehouseName = '南昌樵舍仓'; public string $effectiveAt = ''; @@ -36,29 +27,9 @@ class ThirdPartyWarehouseQuotes extends Component public ?string $message = null; - /** 仓储费核算 */ - public string $settlementPeriod = ''; - - /** @var array|null */ - public ?array $settlementSummary = null; - - public ?string $settlementMessage = null; - - public ?string $settlementError = null; - - public ?string $settlementDetailPeriod = null; - - public string $settlementDetailDate = ''; - - /** @var list */ - public array $settlementDetailDates = []; - - public ?string $settlementDetailMessage = null; - public function mount(): void { $this->effectiveAt = now()->toDateString(); - $this->settlementPeriod = now()->startOfMonth()->subMonth()->format('Y-m'); } public function save(): void @@ -94,276 +65,17 @@ class ThirdPartyWarehouseQuotes extends Component $this->message = '仓储报价状态已更新。'; } - public function calculateStorageFee(): void - { - $this->ensureAuthenticated(); - $this->settlementMessage = null; - $this->settlementError = null; - - $this->validate([ - 'settlementPeriod' => ['required', 'regex:/^\d{4}-(0[1-9]|1[0-2])$/'], - ], [ - 'settlementPeriod.regex' => '结算月份格式应为 YYYY-MM。', - ]); - - try { - $result = app(ThirdPartyWarehouseStorageFeeCalculator::class) - ->calculate(self::WAREHOUSE_ID, $this->warehouseName, $this->settlementPeriod); - } catch (\Symfony\Component\HttpKernel\Exception\HttpException $e) { - $this->settlementError = $e->getMessage(); - - return; - } - - $summary = $result['summary']; - ThirdPartyWarehouseStorageSettlement::query()->updateOrCreate( - [ - 'company_id' => (int) company_id(), - 'warehouse_id' => self::WAREHOUSE_ID, - 'period' => $this->settlementPeriod, - ], - [ - 'warehouse_name' => $this->warehouseName, - 'quote_id' => $summary['quote_id'] ?? null, - 'inbound_rate_per_ton' => $summary['inbound_rate_per_ton'], - 'outbound_rate_per_order' => $summary['outbound_rate_per_order'], - 'pieces_per_pallet' => $summary['pieces_per_pallet'], - 'pallet_area_sqm' => $summary['pallet_area_sqm'], - 'storage_rate_per_sqm_month' => $summary['storage_rate_per_sqm_month'], - 'days_in_month' => $summary['days_in_month'], - 'inbound_tons' => $summary['inbound_tons'], - 'outbound_qty' => $summary['outbound_qty'], - 'inbound_fee' => $summary['inbound_fee'], - 'outbound_fee' => $summary['outbound_fee'], - 'storage_fee' => $summary['storage_fee'], - 'memo_fee' => $summary['memo_fee'], - 'loss_fee' => $summary['loss_fee'], - 'compensation_fee' => $summary['compensation_fee'], - 'total_fee' => $summary['total_fee'], - 'detail_json' => [ - 'daily' => $result['daily'], - 'per_sku' => $result['per_sku'], - 'daily_sku' => $result['daily_sku'], - ], - 'calculated_by' => (int) user_id(), - ], - ); - - $this->settlementSummary = $summary; - $this->settlementMessage = $this->settlementPeriod . ' 仓储费核算完成,已存档。'; - } - - public function exportSettlement(string $period): ?BinaryFileResponse - { - $this->ensureAuthenticated(); - - $settlement = ThirdPartyWarehouseStorageSettlement::query() - ->where('company_id', (int) company_id()) - ->where('warehouse_id', self::WAREHOUSE_ID) - ->where('period', $period) - ->first(); - - if ($settlement === null) { - $this->settlementError = '未找到 ' . $period . ' 的核算存档,请先核算。'; - - return null; - } - - $detail = $settlement->detail_json ?: ['daily' => [], 'per_sku' => [], 'daily_sku' => []]; - $result = [ - 'summary' => $this->summaryFromSettlement($settlement), - 'daily' => $detail['daily'] ?? [], - 'per_sku' => $detail['per_sku'] ?? [], - 'daily_sku' => $detail['daily_sku'] ?? [], - ]; - - $fileName = $settlement->warehouse_name . '仓储费用_' . $period . '.xlsx'; - - return Excel::download(new ThirdPartyWarehouseStorageSettlementExport($result), $fileName); - } - - public function exportSettlementDetails(string $period): ?BinaryFileResponse - { - $this->ensureAuthenticated(); - abort_unless(preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $period) === 1, 422); - - $settlement = $this->settlementForPeriod($period); - if ($settlement === null) { - $this->settlementError = '未找到 ' . $period . ' 的核算存档,请先核算。'; - - return null; - } - - $dailySku = ($settlement->detail_json ?: [])['daily_sku'] ?? []; - if ($dailySku === []) { - $this->showMissingSettlementDetails($period); - - return null; - } - - $fileName = $settlement->warehouse_name . '每日商品仓储费明细_' . $period . '.xlsx'; - - return Excel::download(new ThirdPartyWarehouseStorageDetailExport($dailySku), $fileName); - } - - public function viewSettlementDetails(string $period): void - { - $this->ensureAuthenticated(); - abort_unless(preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $period) === 1, 422); - - $this->settlementDetailPeriod = $period; - $this->settlementDetailDate = ''; - $this->settlementDetailDates = []; - $this->settlementDetailMessage = null; - - $settlement = $this->settlementForPeriod($period); - if ($settlement === null) { - $this->settlementDetailMessage = '未找到 ' . $period . ' 的核算存档。'; - $this->dispatch('warehouse-settlement-details-opened'); - - return; - } - - $detail = $settlement->detail_json ?: []; - $dailySku = $detail['daily_sku'] ?? []; - if ($dailySku === []) { - $this->showMissingSettlementDetails($period); - - return; - } - - $dates = array_values(array_unique(array_column($dailySku, 'date'))); - sort($dates); - $this->settlementDetailDates = $dates; - $this->settlementDetailDate = $dates[0] ?? ''; - $this->dispatch('warehouse-settlement-details-opened'); - } - - public function closeSettlementDetails(): void - { - $this->settlementDetailPeriod = null; - $this->settlementDetailDate = ''; - $this->settlementDetailDates = []; - $this->settlementDetailMessage = null; - } - public function render(): View { $data = $this->repository()->pageData(); - $history = ThirdPartyWarehouseStorageSettlement::query() - ->where('company_id', (int) company_id()) - ->where('warehouse_id', self::WAREHOUSE_ID) - ->orderByDesc('period') - ->limit(24) - ->get() - ->map(fn (ThirdPartyWarehouseStorageSettlement $row): array => [ - 'period' => $row->period, - 'total_fee' => (float) $row->total_fee, - 'inbound_fee' => (float) $row->inbound_fee, - 'outbound_fee' => (float) $row->outbound_fee, - 'storage_fee' => (float) $row->storage_fee, - 'calculated_at' => optional($row->updated_at)->format('Y-m-d H:i'), - ]) - ->all(); - $settlementDetail = $this->settlementDetailData(); - return view('business.pages.courier-payables.partials.third-party-warehouse-quotes', [ 'warehouseQuotes' => $data['rows'], 'warehouseQuoteSummary' => $data['summary'], 'warehouseQuotesReady' => $data['ready'], - 'settlementHistory' => $history, - 'settlementDetailRows' => $settlementDetail['rows'], - 'settlementDetailSummary' => $settlementDetail['summary'], ]); } - /** @return array */ - private function summaryFromSettlement(ThirdPartyWarehouseStorageSettlement $s): array - { - return [ - 'period' => $s->period, - 'warehouse_id' => (int) $s->warehouse_id, - 'warehouse_name' => $s->warehouse_name, - 'quote_id' => $s->quote_id, - 'days_in_month' => (int) $s->days_in_month, - 'inbound_rate_per_ton' => (float) $s->inbound_rate_per_ton, - 'outbound_rate_per_order' => (float) $s->outbound_rate_per_order, - 'pieces_per_pallet' => (float) $s->pieces_per_pallet, - 'pallet_area_sqm' => (float) $s->pallet_area_sqm, - 'storage_rate_per_sqm_month' => (float) $s->storage_rate_per_sqm_month, - 'inbound_tons' => (float) $s->inbound_tons, - 'inbound_fee' => (float) $s->inbound_fee, - 'outbound_qty' => (float) $s->outbound_qty, - 'outbound_fee' => (float) $s->outbound_fee, - 'storage_fee' => (float) $s->storage_fee, - 'memo_fee' => (float) $s->memo_fee, - 'loss_fee' => (float) $s->loss_fee, - 'compensation_fee' => (float) $s->compensation_fee, - 'total_fee' => (float) $s->total_fee, - ]; - } - - /** - * 明细行只参与当前服务端渲染,不进入 Livewire 公共快照,避免日期切换时回传整张表。 - * - * @return array{rows:list>,summary:array|null} - */ - private function settlementDetailData(): array - { - $empty = ['rows' => [], 'summary' => null]; - - if ($this->settlementDetailPeriod === null - || ! in_array($this->settlementDetailDate, $this->settlementDetailDates, true)) { - return $empty; - } - - $settlement = $this->settlementForPeriod($this->settlementDetailPeriod); - if ($settlement === null) { - return $empty; - } - - $detail = $settlement->detail_json ?: []; - $rows = array_values(array_filter( - $detail['daily_sku'] ?? [], - fn (array $row): bool => ($row['date'] ?? null) === $this->settlementDetailDate, - )); - usort($rows, static fn (array $left, array $right): int => strcmp( - (string) ($left['sku_code'] ?? ''), - (string) ($right['sku_code'] ?? ''), - )); - - return [ - 'rows' => $rows, - 'summary' => [ - 'sku_count' => count($rows), - 'opening' => array_sum(array_column($rows, 'opening')), - 'net_movement' => array_sum(array_column($rows, 'net_movement')), - 'closing' => array_sum(array_column($rows, 'closing')), - 'storage_fee' => array_sum(array_column($rows, 'storage_fee')), - ], - ]; - } - - private function settlementForPeriod(string $period): ?ThirdPartyWarehouseStorageSettlement - { - return ThirdPartyWarehouseStorageSettlement::query() - ->where('company_id', (int) company_id()) - ->where('warehouse_id', self::WAREHOUSE_ID) - ->where('period', $period) - ->first(); - } - - private function showMissingSettlementDetails(string $period): void - { - $this->settlementPeriod = $period; - $this->settlementDetailPeriod = $period; - $this->settlementDetailDate = ''; - $this->settlementDetailDates = []; - $this->settlementDetailMessage = $period . ' 的原存档没有每日商品明细,请重新核算该月份后查看和导出。'; - $this->dispatch('warehouse-settlement-details-opened'); - } - /** @return array> */ protected function rules(): array { diff --git a/app/Http/Livewire/Business/WarehouseFreight.php b/app/Http/Livewire/Business/WarehouseFreight.php new file mode 100644 index 00000000..df085781 --- /dev/null +++ b/app/Http/Livewire/Business/WarehouseFreight.php @@ -0,0 +1,323 @@ +|null */ + public ?array $settlementSummary = null; + + public ?string $settlementMessage = null; + + public ?string $settlementError = null; + + public ?string $settlementDetailPeriod = null; + + public string $settlementDetailDate = ''; + + /** @var list */ + public array $settlementDetailDates = []; + + public ?string $settlementDetailMessage = null; + + public function mount(): void + { + $this->ensureAuthenticated(); + $this->settlementPeriod = now()->startOfMonth()->subMonth()->format('Y-m'); + } + + public function calculateStorageFee(): void + { + $this->ensureAuthenticated(); + $this->settlementMessage = null; + $this->settlementError = null; + + $this->validate([ + 'settlementPeriod' => ['required', 'regex:/^\d{4}-(0[1-9]|1[0-2])$/'], + ], [ + 'settlementPeriod.regex' => '结算月份格式应为 YYYY-MM。', + ]); + + try { + $result = app(ThirdPartyWarehouseStorageFeeCalculator::class) + ->calculate(self::WAREHOUSE_ID, $this->warehouseName, $this->settlementPeriod); + } catch (\Symfony\Component\HttpKernel\Exception\HttpException $e) { + $this->settlementError = $e->getMessage(); + + return; + } + + $summary = $result['summary']; + ThirdPartyWarehouseStorageSettlement::query()->updateOrCreate( + [ + 'company_id' => (int) company_id(), + 'warehouse_id' => self::WAREHOUSE_ID, + 'period' => $this->settlementPeriod, + ], + [ + 'warehouse_name' => $this->warehouseName, + 'quote_id' => $summary['quote_id'] ?? null, + 'inbound_rate_per_ton' => $summary['inbound_rate_per_ton'], + 'outbound_rate_per_order' => $summary['outbound_rate_per_order'], + 'pieces_per_pallet' => $summary['pieces_per_pallet'], + 'pallet_area_sqm' => $summary['pallet_area_sqm'], + 'storage_rate_per_sqm_month' => $summary['storage_rate_per_sqm_month'], + 'days_in_month' => $summary['days_in_month'], + 'inbound_tons' => $summary['inbound_tons'], + 'outbound_qty' => $summary['outbound_qty'], + 'inbound_fee' => $summary['inbound_fee'], + 'outbound_fee' => $summary['outbound_fee'], + 'storage_fee' => $summary['storage_fee'], + 'memo_fee' => $summary['memo_fee'], + 'loss_fee' => $summary['loss_fee'], + 'compensation_fee' => $summary['compensation_fee'], + 'total_fee' => $summary['total_fee'], + 'detail_json' => [ + 'daily' => $result['daily'], + 'per_sku' => $result['per_sku'], + 'daily_sku' => $result['daily_sku'], + ], + 'calculated_by' => (int) user_id(), + ], + ); + + $this->settlementSummary = $summary; + $this->settlementMessage = $this->settlementPeriod . ' 仓储费核算完成,已存档。'; + } + + public function exportSettlement(string $period): ?BinaryFileResponse + { + $this->ensureAuthenticated(); + + $settlement = ThirdPartyWarehouseStorageSettlement::query() + ->where('company_id', (int) company_id()) + ->where('warehouse_id', self::WAREHOUSE_ID) + ->where('period', $period) + ->first(); + + if ($settlement === null) { + $this->settlementError = '未找到 ' . $period . ' 的核算存档,请先核算。'; + + return null; + } + + $detail = $settlement->detail_json ?: ['daily' => [], 'per_sku' => [], 'daily_sku' => []]; + $result = [ + 'summary' => $this->summaryFromSettlement($settlement), + 'daily' => $detail['daily'] ?? [], + 'per_sku' => $detail['per_sku'] ?? [], + 'daily_sku' => $detail['daily_sku'] ?? [], + ]; + + $fileName = $settlement->warehouse_name . '仓储费用_' . $period . '.xlsx'; + + return Excel::download(new ThirdPartyWarehouseStorageSettlementExport($result), $fileName); + } + + public function exportSettlementDetails(string $period): ?BinaryFileResponse + { + $this->ensureAuthenticated(); + abort_unless(preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $period) === 1, 422); + + $settlement = $this->settlementForPeriod($period); + if ($settlement === null) { + $this->settlementError = '未找到 ' . $period . ' 的核算存档,请先核算。'; + + return null; + } + + $dailySku = ($settlement->detail_json ?: [])['daily_sku'] ?? []; + if ($dailySku === []) { + $this->showMissingSettlementDetails($period); + + return null; + } + + $fileName = $settlement->warehouse_name . '每日商品仓储费明细_' . $period . '.xlsx'; + + return Excel::download(new ThirdPartyWarehouseStorageDetailExport($dailySku), $fileName); + } + + public function viewSettlementDetails(string $period): void + { + $this->ensureAuthenticated(); + abort_unless(preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $period) === 1, 422); + + $this->settlementDetailPeriod = $period; + $this->settlementDetailDate = ''; + $this->settlementDetailDates = []; + $this->settlementDetailMessage = null; + + $settlement = $this->settlementForPeriod($period); + if ($settlement === null) { + $this->settlementDetailMessage = '未找到 ' . $period . ' 的核算存档。'; + $this->dispatch('warehouse-settlement-details-opened'); + + return; + } + + $detail = $settlement->detail_json ?: []; + $dailySku = $detail['daily_sku'] ?? []; + if ($dailySku === []) { + $this->showMissingSettlementDetails($period); + + return; + } + + $dates = array_values(array_unique(array_column($dailySku, 'date'))); + sort($dates); + $this->settlementDetailDates = $dates; + $this->settlementDetailDate = $dates[0] ?? ''; + $this->dispatch('warehouse-settlement-details-opened'); + } + + public function closeSettlementDetails(): void + { + $this->settlementDetailPeriod = null; + $this->settlementDetailDate = ''; + $this->settlementDetailDates = []; + $this->settlementDetailMessage = null; + } + + public function render(): View + { + $this->ensureAuthenticated(); + + $history = ThirdPartyWarehouseStorageSettlement::query() + ->where('company_id', (int) company_id()) + ->where('warehouse_id', self::WAREHOUSE_ID) + ->orderByDesc('period') + ->limit(24) + ->get() + ->map(fn (ThirdPartyWarehouseStorageSettlement $row): array => [ + 'period' => $row->period, + 'total_fee' => (float) $row->total_fee, + 'inbound_fee' => (float) $row->inbound_fee, + 'outbound_fee' => (float) $row->outbound_fee, + 'storage_fee' => (float) $row->storage_fee, + 'calculated_at' => optional($row->updated_at)->format('Y-m-d H:i'), + ]) + ->all(); + $settlementDetail = $this->settlementDetailData(); + + return view('business.pages.courier-payables.partials.warehouse-freight', [ + 'settlementHistory' => $history, + 'settlementDetailRows' => $settlementDetail['rows'], + 'settlementDetailSummary' => $settlementDetail['summary'], + ]); + } + + /** @return array */ + private function summaryFromSettlement(ThirdPartyWarehouseStorageSettlement $s): array + { + return [ + 'period' => $s->period, + 'warehouse_id' => (int) $s->warehouse_id, + 'warehouse_name' => $s->warehouse_name, + 'quote_id' => $s->quote_id, + 'days_in_month' => (int) $s->days_in_month, + 'inbound_rate_per_ton' => (float) $s->inbound_rate_per_ton, + 'outbound_rate_per_order' => (float) $s->outbound_rate_per_order, + 'pieces_per_pallet' => (float) $s->pieces_per_pallet, + 'pallet_area_sqm' => (float) $s->pallet_area_sqm, + 'storage_rate_per_sqm_month' => (float) $s->storage_rate_per_sqm_month, + 'inbound_tons' => (float) $s->inbound_tons, + 'inbound_fee' => (float) $s->inbound_fee, + 'outbound_qty' => (float) $s->outbound_qty, + 'outbound_fee' => (float) $s->outbound_fee, + 'storage_fee' => (float) $s->storage_fee, + 'memo_fee' => (float) $s->memo_fee, + 'loss_fee' => (float) $s->loss_fee, + 'compensation_fee' => (float) $s->compensation_fee, + 'total_fee' => (float) $s->total_fee, + ]; + } + + /** + * 明细行只参与当前服务端渲染,不进入 Livewire 公共快照,避免日期切换时回传整张表。 + * + * @return array{rows:list>,summary:array|null} + */ + private function settlementDetailData(): array + { + $empty = ['rows' => [], 'summary' => null]; + + if ($this->settlementDetailPeriod === null + || ! in_array($this->settlementDetailDate, $this->settlementDetailDates, true)) { + return $empty; + } + + $settlement = $this->settlementForPeriod($this->settlementDetailPeriod); + if ($settlement === null) { + return $empty; + } + + $detail = $settlement->detail_json ?: []; + $rows = array_values(array_filter( + $detail['daily_sku'] ?? [], + fn (array $row): bool => ($row['date'] ?? null) === $this->settlementDetailDate, + )); + usort($rows, static fn (array $left, array $right): int => strcmp( + (string) ($left['sku_code'] ?? ''), + (string) ($right['sku_code'] ?? ''), + )); + + return [ + 'rows' => $rows, + 'summary' => [ + 'sku_count' => count($rows), + 'opening' => array_sum(array_column($rows, 'opening')), + 'net_movement' => array_sum(array_column($rows, 'net_movement')), + 'closing' => array_sum(array_column($rows, 'closing')), + 'storage_fee' => array_sum(array_column($rows, 'storage_fee')), + ], + ]; + } + + private function settlementForPeriod(string $period): ?ThirdPartyWarehouseStorageSettlement + { + return ThirdPartyWarehouseStorageSettlement::query() + ->where('company_id', (int) company_id()) + ->where('warehouse_id', self::WAREHOUSE_ID) + ->where('period', $period) + ->first(); + } + + private function showMissingSettlementDetails(string $period): void + { + $this->settlementPeriod = $period; + $this->settlementDetailPeriod = $period; + $this->settlementDetailDate = ''; + $this->settlementDetailDates = []; + $this->settlementDetailMessage = $period . ' 的原存档没有每日商品明细,请重新核算该月份后查看和导出。'; + $this->dispatch('warehouse-settlement-details-opened'); + } + + private function ensureAuthenticated(): void + { + abort_unless(auth()->check(), 401); + + $section = collect(SidebarPermissions::navigationFor(user()))->firstWhere('slug', 'courier-payables'); + abort_unless(collect($section['pages'] ?? [])->contains('slug', 'warehouse-freight'), 403, '当前账号没有该页面权限。'); + } +} diff --git a/config/business_navigation.php b/config/business_navigation.php index 3a0d7ef3..e8cfc8bf 100644 --- a/config/business_navigation.php +++ b/config/business_navigation.php @@ -722,6 +722,7 @@ $navigation = [ ['slug' => 'shipping-data', 'title' => '发货数据'], ['slug' => 'return-pickup-management', 'title' => '退货取件管理'], ['slug' => 'third-party-warehouse-storage', 'title' => '三方仓进销存'], + ['slug' => 'warehouse-freight', 'title' => '仓储运费'], ['slug' => 'bookkeeping-details', 'title' => '记账明细'], ['slug' => 'compensation-details', 'title' => '赔偿明细'], ['slug' => 'advance-details', 'title' => '借支明细'], diff --git a/resources/views/business/pages/courier-payables/partials/third-party-warehouse-quotes.blade.php b/resources/views/business/pages/courier-payables/partials/third-party-warehouse-quotes.blade.php index 750cbd30..78c53714 100644 --- a/resources/views/business/pages/courier-payables/partials/third-party-warehouse-quotes.blade.php +++ b/resources/views/business/pages/courier-payables/partials/third-party-warehouse-quotes.blade.php @@ -7,99 +7,8 @@
- + @include('business.pages.courier-payables.partials.warehouse-styles')
@@ -197,148 +106,6 @@
-
-
-
-

仓储费核算

-
选择结算月份,一键按当期生效报价核算并存档,可查看和导出每日商品仓储费
-
-
- @if ($settlementError) - - @endif - @if ($settlementMessage) -
{{ $settlementMessage }}
- @endif -
-
- - -
- - @if ($settlementSummary) - - - @endif -
- @if ($settlementSummary) -
-
入库管理费
{{ number_format($settlementSummary['inbound_fee'], 2) }}
-
出库费用
{{ number_format($settlementSummary['outbound_fee'], 2) }}
-
当天仓储费用
{{ number_format($settlementSummary['storage_fee'], 2) }}
-
合计
{{ number_format($settlementSummary['total_fee'], 2) }}
-
- @endif -
- - - - - - - - - @forelse ($settlementHistory as $item) - - - - - - - - - - @empty - - @endforelse - -
结算月份入库管理费出库费用当天仓储费用合计核算时间操作
{{ $item['period'] }}{{ number_format($item['inbound_fee'], 2) }}{{ number_format($item['outbound_fee'], 2) }}{{ number_format($item['storage_fee'], 2) }}{{ number_format($item['total_fee'], 2) }}{{ $item['calculated_at'] }} -
- - -
-
尚未核算任何月份。
-
- @if ($settlementDetailPeriod) -
-
-
- -
-

每日商品仓储费明细

-
{{ $settlementDetailPeriod }} · 上月结转库存按商品编码逐日延续
-
-
-
- @if ($settlementDetailDates !== []) - - @endif - @if ($settlementDetailDates !== []) -
- - -
- @endif - -
-
- @if ($settlementDetailMessage) -

{{ $settlementDetailMessage }}

- @elseif ($settlementDetailSummary) -
-
商品编码数
{{ number_format($settlementDetailSummary['sku_count']) }}
-
上日结余
{{ number_format($settlementDetailSummary['opening'], 2) }}
-
当日进出净额
{{ number_format($settlementDetailSummary['net_movement'], 2) }}
-
当日库存结余
{{ number_format($settlementDetailSummary['closing'], 2) }}
-
当天仓储费
{{ number_format($settlementDetailSummary['storage_fee'], 4) }}
-
-
- - - - - - - - - - @forelse ($settlementDetailRows as $row) - - - - - - - - - - - - @empty - - @endforelse - -
商品编码商品名称供应商分类单位上日结余当日进出净额当日库存结余当天仓储费
{{ $row['sku_code'] }}{{ $row['product_name'] }}{{ $row['supplier'] }}{{ $row['category'] }}{{ $row['unit'] }}{{ number_format($row['opening'], 2) }}{{ number_format($row['net_movement'], 2) }}{{ number_format($row['closing'], 2) }}{{ number_format($row['storage_fee'], 6) }}
该日期没有商品库存明细。
-
- @endif -
- @endif -
-
diff --git a/resources/views/business/pages/courier-payables/partials/warehouse-freight.blade.php b/resources/views/business/pages/courier-payables/partials/warehouse-freight.blade.php new file mode 100644 index 00000000..4f69a773 --- /dev/null +++ b/resources/views/business/pages/courier-payables/partials/warehouse-freight.blade.php @@ -0,0 +1,158 @@ +
+ @include('business.pages.courier-payables.partials.warehouse-styles') + + +
+
+
+

仓储运费

+
选择结算月份,一键按当期生效报价核算并存档,可查看和导出每日商品仓储费
+
+
+ @if ($settlementError) + + @endif + @if ($settlementMessage) +
{{ $settlementMessage }}
+ @endif +
+
+ + + @error('settlementPeriod') {{ $message }} @enderror +
+ + @if ($settlementSummary) + + + @endif +
+ @if ($settlementSummary) +
+
入库管理费
{{ number_format($settlementSummary['inbound_fee'], 2) }}
+
出库费用
{{ number_format($settlementSummary['outbound_fee'], 2) }}
+
当天仓储费用
{{ number_format($settlementSummary['storage_fee'], 2) }}
+
合计
{{ number_format($settlementSummary['total_fee'], 2) }}
+
+ @endif +
+ + + + + + + + + @forelse ($settlementHistory as $item) + + + + + + + + + + @empty + + @endforelse + +
结算月份入库管理费出库费用当天仓储费用合计核算时间操作
{{ $item['period'] }}{{ number_format($item['inbound_fee'], 2) }}{{ number_format($item['outbound_fee'], 2) }}{{ number_format($item['storage_fee'], 2) }}{{ number_format($item['total_fee'], 2) }}{{ $item['calculated_at'] }} +
+ + +
+
尚未核算任何月份,请选择结算月份后核算仓储费。
+
+ @if ($settlementDetailPeriod) +
+
+
+ +
+

每日商品仓储费明细

+
{{ $settlementDetailPeriod }} · 上月结转库存按商品编码逐日延续
+
+
+
+ @if ($settlementDetailDates !== []) + + @endif + @if ($settlementDetailDates !== []) +
+ + +
+ @endif + +
+
+ @if ($settlementDetailMessage) +

{{ $settlementDetailMessage }}

+ @elseif ($settlementDetailSummary) +
+
商品编码数
{{ number_format($settlementDetailSummary['sku_count']) }}
+
上日结余
{{ number_format($settlementDetailSummary['opening'], 2) }}
+
当日进出净额
{{ number_format($settlementDetailSummary['net_movement'], 2) }}
+
当日库存结余
{{ number_format($settlementDetailSummary['closing'], 2) }}
+
当天仓储费
{{ number_format($settlementDetailSummary['storage_fee'], 4) }}
+
+
+ + + + + + + + + + @forelse ($settlementDetailRows as $row) + + + + + + + + + + + + @empty + + @endforelse + +
商品编码商品名称供应商分类单位上日结余当日进出净额当日库存结余当天仓储费
{{ $row['sku_code'] }}{{ $row['product_name'] }}{{ $row['supplier'] }}{{ $row['category'] }}{{ $row['unit'] }}{{ number_format($row['opening'], 2) }}{{ number_format($row['net_movement'], 2) }}{{ number_format($row['closing'], 2) }}{{ number_format($row['storage_fee'], 6) }}
该日期没有商品库存明细。
+
+ @endif +
+ @endif +
+ +
diff --git a/resources/views/business/pages/courier-payables/partials/warehouse-styles.blade.php b/resources/views/business/pages/courier-payables/partials/warehouse-styles.blade.php new file mode 100644 index 00000000..cb133f2e --- /dev/null +++ b/resources/views/business/pages/courier-payables/partials/warehouse-styles.blade.php @@ -0,0 +1,99 @@ + diff --git a/resources/views/business/pages/courier-payables/warehouse-freight.blade.php b/resources/views/business/pages/courier-payables/warehouse-freight.blade.php new file mode 100644 index 00000000..3338ea2b --- /dev/null +++ b/resources/views/business/pages/courier-payables/warehouse-freight.blade.php @@ -0,0 +1 @@ + diff --git a/tests/Feature/Business/ThirdPartyWarehouseQuotePageTest.php b/tests/Feature/Business/ThirdPartyWarehouseQuotePageTest.php index bb588828..78eebbb1 100644 --- a/tests/Feature/Business/ThirdPartyWarehouseQuotePageTest.php +++ b/tests/Feature/Business/ThirdPartyWarehouseQuotePageTest.php @@ -5,10 +5,8 @@ namespace Tests\Feature\Business; use App\Http\Livewire\Business\ThirdPartyWarehouseQuotes as WarehouseQuoteComponent; use App\Models\Auth\Permission; use App\Models\Business\ThirdPartyWarehouseQuote; -use App\Models\Business\ThirdPartyWarehouseStorageSettlement; use Illuminate\Database\Eloquent\ModelNotFoundException; use Livewire\Livewire; -use Maatwebsite\Excel\Facades\Excel; use Tests\Feature\FeatureTestCase; class ThirdPartyWarehouseQuotePageTest extends FeatureTestCase @@ -38,7 +36,9 @@ class ThirdPartyWarehouseQuotePageTest extends FeatureTestCase ->assertSeeText('Σ(销售出仓 + 其它出库 + 调拨出)× 出库费单价') ->assertSeeText('系统数据为唯一核算来源,不读取仓库提供、核对或差异列') ->assertSeeText($this->user->name) - ->assertSee('data-quote-panel="warehouse"', false); + ->assertSee('data-quote-panel="warehouse"', false) + ->assertDontSee('wire:click="calculateStorageFee"', false) + ->assertDontSee('warehouse-settle-period', false); } public function test_warehouse_quote_store_creates_a_new_effective_version(): void @@ -122,101 +122,6 @@ class ThirdPartyWarehouseQuotePageTest extends FeatureTestCase ->call('setStatus', $quote->id, ThirdPartyWarehouseQuote::STATUS_INACTIVE); } - public function test_daily_sku_storage_detail_can_be_opened_and_switched_by_date(): void - { - $this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail()); - $this->loginAs(); - $quote = $this->createQuote(); - $this->createSettlement($quote, [ - ['date' => '2026-08-01', 'owner' => '甲', 'sku_code' => 'SKU-A', 'product_name' => '商品A', 'supplier' => '供应商甲', 'category' => '桌类', 'unit' => '件', 'opening' => 100, 'net_movement' => 0, 'closing' => 100, 'storage_fee' => 4.417952], - ['date' => '2026-08-01', 'owner' => '甲', 'sku_code' => 'SKU-B', 'product_name' => '商品B', 'supplier' => '供应商乙', 'category' => '柜类', 'unit' => '件', 'opening' => 50, 'net_movement' => 10, 'closing' => 60, 'storage_fee' => 2.650771], - ['date' => '2026-08-02', 'owner' => '甲', 'sku_code' => 'SKU-A', 'product_name' => '商品A', 'supplier' => '供应商甲', 'category' => '桌类', 'unit' => '件', 'opening' => 100, 'net_movement' => -10, 'closing' => 90, 'storage_fee' => 3.976157], - ]); - - Livewire::test(WarehouseQuoteComponent::class) - ->call('viewSettlementDetails', '2026-08') - ->assertSet('settlementDetailDate', '2026-08-01') - ->assertDispatched('warehouse-settlement-details-opened') - ->assertSee('每日商品仓储费明细') - ->assertSee('导出整月商品明细') - ->assertSee('160.00') - ->assertSee('SKU-A') - ->assertSee('SKU-B') - ->set('settlementDetailDate', '2026-08-02') - ->assertSee('90.00') - ->assertSee('3.976157') - ->assertDontSee('SKU-B'); - } - - public function test_legacy_settlement_explains_that_recalculation_is_required(): void - { - $this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail()); - $this->loginAs(); - $quote = $this->createQuote(); - $this->createSettlement($quote, []); - - Livewire::test(WarehouseQuoteComponent::class) - ->call('viewSettlementDetails', '2026-08') - ->assertDispatched('warehouse-settlement-details-opened') - ->assertSee('原存档没有每日商品明细,请重新核算该月份后查看和导出'); - } - - public function test_daily_sku_storage_details_can_be_exported_for_the_whole_month(): void - { - $this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail()); - $this->loginAs(); - $quote = $this->createQuote(); - $dailySku = [ - ['date' => '2026-08-01', 'owner' => '甲', 'sku_code' => 'SKU-A', 'product_name' => '商品A', 'supplier' => '供应商甲', 'category' => '桌类', 'unit' => '件', 'opening' => 100, 'net_movement' => 0, 'closing' => 100, 'storage_fee' => 4.417952], - ['date' => '2026-08-02', 'owner' => '甲', 'sku_code' => 'SKU-A', 'product_name' => '商品A', 'supplier' => '供应商甲', 'category' => '桌类', 'unit' => '件', 'opening' => 100, 'net_movement' => -10, 'closing' => 90, 'storage_fee' => 3.976157], - ]; - $this->createSettlement($quote, $dailySku); - Excel::fake(); - - Livewire::test(WarehouseQuoteComponent::class) - ->call('exportSettlementDetails', '2026-08'); - - Excel::assertDownloaded('南昌樵舍仓每日商品仓储费明细_2026-08.xlsx', function ($export) use ($dailySku): bool { - return $export->title() === '每日商品仓储费' - && count($export->array()) === count($dailySku) - && $export->array()[1][0] === '2026-08-02'; - }); - } - - public function test_legacy_settlement_does_not_export_an_empty_detail_file(): void - { - $this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail()); - $this->loginAs(); - $quote = $this->createQuote(); - $this->createSettlement($quote, []); - Excel::fake(); - - Livewire::test(WarehouseQuoteComponent::class) - ->call('exportSettlementDetails', '2026-08') - ->assertSet('settlementDetailPeriod', '2026-08') - ->assertDispatched('warehouse-settlement-details-opened') - ->assertSee('原存档没有每日商品明细,请重新核算该月份后查看和导出'); - - $this->assertSame([], $this->readExcelFakeDownloads()); - } - - public function test_settlement_history_detail_and_export_are_company_scoped(): void - { - $this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail()); - $this->loginAs(); - $quote = $this->createQuote(); - $this->createSettlement($quote, [], company_id() + 999, '2026-07'); - - Livewire::test(WarehouseQuoteComponent::class) - ->assertDontSee('2026-07') - ->call('viewSettlementDetails', '2026-07') - ->assertSet('settlementDetailMessage', '未找到 2026-07 的核算存档。') - ->call('exportSettlement', '2026-07') - ->assertSet('settlementError', '未找到 2026-07 的核算存档,请先核算。') - ->call('exportSettlementDetails', '2026-07') - ->assertSet('settlementError', '未找到 2026-07 的核算存档,请先核算。'); - } - private function createQuote(): ThirdPartyWarehouseQuote { return ThirdPartyWarehouseQuote::query()->create([ @@ -235,47 +140,4 @@ class ThirdPartyWarehouseQuotePageTest extends FeatureTestCase ]); } - /** @return array */ - private function readExcelFakeDownloads(): array - { - $fake = Excel::getFacadeRoot(); - $property = new \ReflectionProperty($fake, 'downloads'); - $property->setAccessible(true); - - return $property->getValue($fake); - } - - /** @param list> $dailySku */ - private function createSettlement( - ThirdPartyWarehouseQuote $quote, - array $dailySku, - ?int $companyId = null, - string $period = '2026-08', - ): void - { - ThirdPartyWarehouseStorageSettlement::query()->create([ - 'company_id' => $companyId ?? company_id(), - 'warehouse_id' => WarehouseQuoteComponent::WAREHOUSE_ID, - 'warehouse_name' => '南昌樵舍仓', - 'period' => $period, - 'quote_id' => $quote->id, - 'inbound_rate_per_ton' => 20, - 'outbound_rate_per_order' => 0.85, - 'pieces_per_pallet' => 23, - 'pallet_area_sqm' => 3, - 'storage_rate_per_sqm_month' => 10.5, - 'days_in_month' => 31, - 'inbound_tons' => 0, - 'outbound_qty' => 0, - 'inbound_fee' => 0, - 'outbound_fee' => 0, - 'storage_fee' => array_sum(array_column($dailySku, 'storage_fee')), - 'memo_fee' => 0, - 'loss_fee' => 0, - 'compensation_fee' => 0, - 'total_fee' => array_sum(array_column($dailySku, 'storage_fee')), - 'detail_json' => ['daily' => [], 'per_sku' => [], 'daily_sku' => $dailySku], - 'calculated_by' => user_id(), - ]); - } } diff --git a/tests/Feature/Business/WarehouseFreightPageTest.php b/tests/Feature/Business/WarehouseFreightPageTest.php new file mode 100644 index 00000000..a0ec7790 --- /dev/null +++ b/tests/Feature/Business/WarehouseFreightPageTest.php @@ -0,0 +1,289 @@ +user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail()); + $quote = $this->createQuote(); + $this->createSettlement($quote, []); + + $this->loginAs()->get(route('business.pages.show', ['section' => 'courier-payables'])) + ->assertOk() + ->assertSee('data-courier-tab="warehouse-freight"', false); + + $this->get(route('business.pages.show', ['section' => 'courier-payables', 'page' => 'courier-quote'])) + ->assertOk() + ->assertDontSee('data-courier-tab="warehouse-freight"', false); + + $this->get(route('business.pages.show', ['section' => 'courier-payables', 'page' => 'warehouse-freight'])) + ->assertOk() + ->assertSeeText('仓储运费') + ->assertSeeText('2026-08') + ->assertSeeText('查看明细') + ->assertSeeText('导出核算') + ->assertSee('data-warehouse-freight-page', false) + ->assertDontSeeText('录入新报价'); + } + + public function test_restricted_quote_permission_does_not_grant_warehouse_freight_access(): void + { + $this->user->roles()->sync([\App\Models\Auth\Role::where('name', 'admin')->firstOrFail()->id]); + $this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail()); + $this->setPagePermissions(['courier-payables/quote-management']); + $this->withExceptionHandling()->loginAs(); + + $this->getJson(route('business.pages.show', ['section' => 'courier-payables', 'page' => 'warehouse-freight']), [ + 'X-Requested-With' => 'XMLHttpRequest', + ])->assertNotFound(); + Livewire::test(WarehouseFreightComponent::class)->assertForbidden(); + } + + public function test_component_actions_recheck_warehouse_freight_permission(): void + { + $this->user->roles()->sync([\App\Models\Auth\Role::where('name', 'admin')->firstOrFail()->id]); + $this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail()); + $this->setPagePermissions(['courier-payables/warehouse-freight']); + $this->withExceptionHandling()->loginAs(); + $component = Livewire::test(WarehouseFreightComponent::class); + + $this->setPagePermissions(['courier-payables/quote-management']); + $component->call('calculateStorageFee')->assertForbidden(); + $this->assertDatabaseCount('business_third_party_warehouse_storage_settlements', 0); + } + + public function test_calculation_saves_a_month_without_overwriting_other_history(): void + { + $this->loginAs(); + $quote = $this->createQuote(); + $this->createSettlement($quote, []); + $summary = ThirdPartyWarehouseStorageSettlement::query()->firstOrFail()->attributesToArray(); + $summary['period'] = '2026-09'; + $summary['days_in_month'] = 30; + $summary['inbound_fee'] = 46.0; + $summary['outbound_fee'] = 8.5; + $summary['storage_fee'] = 10.25; + $summary['total_fee'] = 64.75; + $calculator = \Mockery::mock(\App\Business\ThirdPartyWarehouseStorageFeeCalculator::class); + $calculator->shouldReceive('calculate')->once() + ->with(WarehouseFreightComponent::WAREHOUSE_ID, '南昌樵舍仓', '2026-09') + ->andReturn(['summary' => $summary, 'daily' => [], 'per_sku' => [], 'daily_sku' => []]); + $this->instance(\App\Business\ThirdPartyWarehouseStorageFeeCalculator::class, $calculator); + + Livewire::test(WarehouseFreightComponent::class) + ->set('settlementPeriod', '2026-09') + ->call('calculateStorageFee') + ->assertHasNoErrors() + ->assertSet('settlementSummary.total_fee', 64.75) + ->assertSee('64.75'); + + $this->assertDatabaseCount('business_third_party_warehouse_storage_settlements', 2); + $this->assertDatabaseHas('business_third_party_warehouse_storage_settlements', [ + 'company_id' => company_id(), 'period' => '2026-09', 'total_fee' => 64.75, + ]); + } + + public function test_invalid_month_and_missing_quote_show_errors_without_saving(): void + { + $this->loginAs(); + Livewire::test(WarehouseFreightComponent::class) + ->set('settlementPeriod', '2026-13') + ->call('calculateStorageFee') + ->assertHasErrors(['settlementPeriod' => 'regex']) + ->assertSee('结算月份格式应为 YYYY-MM。'); + + Livewire::test(WarehouseFreightComponent::class) + ->set('settlementPeriod', '2026-08') + ->call('calculateStorageFee') + ->assertSet('settlementSummary', null) + ->assertSee('role="alert"', false); + $this->assertDatabaseCount('business_third_party_warehouse_storage_settlements', 0); + } + + public function test_saved_monthly_calculation_can_be_exported(): void + { + $this->loginAs(); + $quote = $this->createQuote(); + $this->createSettlement($quote, []); + Excel::fake(); + + Livewire::test(WarehouseFreightComponent::class)->call('exportSettlement', '2026-08'); + Excel::assertDownloaded('南昌樵舍仓仓储费用_2026-08.xlsx'); + } + + public function test_daily_sku_storage_detail_can_be_opened_and_switched_by_date(): void + { + $this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail()); + $this->loginAs(); + $quote = $this->createQuote(); + $this->createSettlement($quote, [ + ['date' => '2026-08-01', 'owner' => '甲', 'sku_code' => 'SKU-A', 'product_name' => '商品A', 'supplier' => '供应商甲', 'category' => '桌类', 'unit' => '件', 'opening' => 100, 'net_movement' => 0, 'closing' => 100, 'storage_fee' => 4.417952], + ['date' => '2026-08-01', 'owner' => '甲', 'sku_code' => 'SKU-B', 'product_name' => '商品B', 'supplier' => '供应商乙', 'category' => '柜类', 'unit' => '件', 'opening' => 50, 'net_movement' => 10, 'closing' => 60, 'storage_fee' => 2.650771], + ['date' => '2026-08-02', 'owner' => '甲', 'sku_code' => 'SKU-A', 'product_name' => '商品A', 'supplier' => '供应商甲', 'category' => '桌类', 'unit' => '件', 'opening' => 100, 'net_movement' => -10, 'closing' => 90, 'storage_fee' => 3.976157], + ]); + + Livewire::test(WarehouseFreightComponent::class) + ->call('viewSettlementDetails', '2026-08') + ->assertSet('settlementDetailDate', '2026-08-01') + ->assertDispatched('warehouse-settlement-details-opened') + ->assertSee('每日商品仓储费明细') + ->assertSee('导出整月商品明细') + ->assertSee('160.00') + ->assertSee('SKU-A') + ->assertSee('SKU-B') + ->set('settlementDetailDate', '2026-08-02') + ->assertSee('90.00') + ->assertSee('3.976157') + ->assertDontSee('SKU-B'); + } + + public function test_legacy_settlement_explains_that_recalculation_is_required(): void + { + $this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail()); + $this->loginAs(); + $quote = $this->createQuote(); + $this->createSettlement($quote, []); + + Livewire::test(WarehouseFreightComponent::class) + ->call('viewSettlementDetails', '2026-08') + ->assertDispatched('warehouse-settlement-details-opened') + ->assertSee('原存档没有每日商品明细,请重新核算该月份后查看和导出'); + } + + public function test_daily_sku_storage_details_can_be_exported_for_the_whole_month(): void + { + $this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail()); + $this->loginAs(); + $quote = $this->createQuote(); + $dailySku = [ + ['date' => '2026-08-01', 'owner' => '甲', 'sku_code' => 'SKU-A', 'product_name' => '商品A', 'supplier' => '供应商甲', 'category' => '桌类', 'unit' => '件', 'opening' => 100, 'net_movement' => 0, 'closing' => 100, 'storage_fee' => 4.417952], + ['date' => '2026-08-02', 'owner' => '甲', 'sku_code' => 'SKU-A', 'product_name' => '商品A', 'supplier' => '供应商甲', 'category' => '桌类', 'unit' => '件', 'opening' => 100, 'net_movement' => -10, 'closing' => 90, 'storage_fee' => 3.976157], + ]; + $this->createSettlement($quote, $dailySku); + Excel::fake(); + + Livewire::test(WarehouseFreightComponent::class) + ->call('exportSettlementDetails', '2026-08'); + + Excel::assertDownloaded('南昌樵舍仓每日商品仓储费明细_2026-08.xlsx', function ($export) use ($dailySku): bool { + return $export->title() === '每日商品仓储费' + && count($export->array()) === count($dailySku) + && $export->array()[1][0] === '2026-08-02'; + }); + } + + public function test_legacy_settlement_does_not_export_an_empty_detail_file(): void + { + $this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail()); + $this->loginAs(); + $quote = $this->createQuote(); + $this->createSettlement($quote, []); + Excel::fake(); + + Livewire::test(WarehouseFreightComponent::class) + ->call('exportSettlementDetails', '2026-08') + ->assertSet('settlementDetailPeriod', '2026-08') + ->assertDispatched('warehouse-settlement-details-opened') + ->assertSee('原存档没有每日商品明细,请重新核算该月份后查看和导出'); + + $this->assertSame([], $this->readExcelFakeDownloads()); + } + + public function test_settlement_history_detail_and_export_are_company_scoped(): void + { + $this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail()); + $this->loginAs(); + $quote = $this->createQuote(); + $this->createSettlement($quote, [], company_id() + 999, '2026-07'); + + Livewire::test(WarehouseFreightComponent::class) + ->assertDontSee('2026-07') + ->call('viewSettlementDetails', '2026-07') + ->assertSet('settlementDetailMessage', '未找到 2026-07 的核算存档。') + ->call('exportSettlement', '2026-07') + ->assertSet('settlementError', '未找到 2026-07 的核算存档,请先核算。') + ->call('exportSettlementDetails', '2026-07') + ->assertSet('settlementError', '未找到 2026-07 的核算存档,请先核算。'); + } + + private function createQuote(): ThirdPartyWarehouseQuote + { + return ThirdPartyWarehouseQuote::query()->create([ + 'company_id' => company_id(), + 'warehouse_name' => '南昌樵舍仓', + 'inbound_rate_per_ton' => 20, + 'outbound_rate_per_order' => 0.85, + 'pieces_per_pallet' => 23, + 'pallet_area_sqm' => 3, + 'storage_rate_per_sqm_month' => 10.5, + 'effective_at' => '2026-08-01', + 'status' => ThirdPartyWarehouseQuote::STATUS_ACTIVE, + 'remark' => '八月合同报价', + 'created_by' => $this->user->id, + 'updated_by' => $this->user->id, + ]); + } + + private function setPagePermissions(array $pages): void + { + $subject = 'user_' . $this->user->id; + setting()->set('system.sidebar_permissions', [ + 'configured' => [$subject], 'subjects' => [$subject => $pages], + ]); + setting()->save(); + } + + /** @return array */ + private function readExcelFakeDownloads(): array + { + $fake = Excel::getFacadeRoot(); + $property = new \ReflectionProperty($fake, 'downloads'); + $property->setAccessible(true); + + return $property->getValue($fake); + } + + /** @param list> $dailySku */ + private function createSettlement( + ThirdPartyWarehouseQuote $quote, + array $dailySku, + ?int $companyId = null, + string $period = '2026-08', + ): void + { + ThirdPartyWarehouseStorageSettlement::query()->create([ + 'company_id' => $companyId ?? company_id(), + 'warehouse_id' => WarehouseFreightComponent::WAREHOUSE_ID, + 'warehouse_name' => '南昌樵舍仓', + 'period' => $period, + 'quote_id' => $quote->id, + 'inbound_rate_per_ton' => 20, + 'outbound_rate_per_order' => 0.85, + 'pieces_per_pallet' => 23, + 'pallet_area_sqm' => 3, + 'storage_rate_per_sqm_month' => 10.5, + 'days_in_month' => 31, + 'inbound_tons' => 0, + 'outbound_qty' => 0, + 'inbound_fee' => 0, + 'outbound_fee' => 0, + 'storage_fee' => array_sum(array_column($dailySku, 'storage_fee')), + 'memo_fee' => 0, + 'loss_fee' => 0, + 'compensation_fee' => 0, + 'total_fee' => array_sum(array_column($dailySku, 'storage_fee')), + 'detail_json' => ['daily' => [], 'per_sku' => [], 'daily_sku' => $dailySku], + 'calculated_by' => user_id(), + ]); + } +} -- 2.47.2