From 73679c55e3e11695aa7be99e0579f5fb7d43bebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=9C=A8?= <594326199@qq.com> Date: Thu, 26 Nov 2026 16:42:28 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E8=B4=A7=E6=AC=BE=E9=A2=84=E4=BC=B0):=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=A7=92=E7=BA=A7=E7=AD=9B=E9=80=89=E5=92=8C?= =?UTF-8?q?=E7=AD=9B=E9=80=89=E7=BB=93=E6=9E=9C=E5=AF=BC=E5=87=BA=20(#1468?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 支持入库时间精确到秒筛选,并导出当前筛选全集。 Co-authored-by: 苏木 <594326199@qq.com> Co-committed-by: 苏木 <594326199@qq.com> --- .../SupplierPayableForecastExport.php | 86 +++++++++ .../SupplierPayableForecastRepository.php | 92 ++++++--- .../View/SupplierPayableForecastPageData.php | 46 +++-- .../supplier-payable-forecast.blade.php | 35 ++-- .../partials/date-range-picker.blade.php | 179 +++++++++++++++++- routes/admin.php | 4 + .../Business/NegativeAlertPagesTest.php | 96 ++++++++++ .../SupplierPayableForecastRepositoryTest.php | 39 ++++ 8 files changed, 523 insertions(+), 54 deletions(-) create mode 100644 app/Http/Controllers/Business/SupplierPayableForecastExport.php diff --git a/app/Http/Controllers/Business/SupplierPayableForecastExport.php b/app/Http/Controllers/Business/SupplierPayableForecastExport.php new file mode 100644 index 00000000..f490432a --- /dev/null +++ b/app/Http/Controllers/Business/SupplierPayableForecastExport.php @@ -0,0 +1,86 @@ +goodsFilters($request); + abort_unless( + $repository->syncStatus()['ready'], + 503, + '采购入库共享镜像尚未就绪,暂不能导出。', + ); + + $fields = $registry->fields( + PayablesFinalRegistry::SECTION_SUPPLIER, + 'payment-reconciliation', + ); + + return response()->streamDownload(function () use ($fields, $filters, $repository): void { + $stream = fopen('php://output', 'w'); + if ($stream === false) { + throw new \RuntimeException('无法创建供应商货款明细导出文件。'); + } + + fwrite($stream, "\xEF\xBB\xBF"); + fputcsv($stream, array_map( + static fn (array $field): string => (string) ($field['label'] ?? ''), + $fields, + )); + + foreach ($repository->goodsRowsForExport($filters) as $row) { + $values = []; + foreach ($fields as $key => $field) { + $value = $row[$key] ?? ''; + $values[] = ($field['type'] ?? '') === 'number' + ? number_format((float) $value, 2, '.', '') + : $this->textCell($value); + } + fputcsv($stream, $values); + } + + fclose($stream); + }, $this->filename($filters), [ + 'Content-Type' => 'text/csv; charset=UTF-8', + 'Cache-Control' => 'private, no-store', + ]); + } + + /** @param array{date_from:string,date_to:string,keyword:string} $filters */ + private function filename(array $filters): string + { + $from = Carbon::parse($filters['date_from'])->format('Ymd_His'); + $to = $filters['date_to'] !== '' + ? Carbon::parse($filters['date_to'])->format('Ymd_His') + : '不限'; + + return '供应商货款明细_' . $from . '_' . $to . '_' . now()->format('Ymd_His') . '.csv'; + } + + private function textCell(mixed $value): string + { + $text = trim((string) $value); + if ($text === '') { + return ''; + } + + return preg_match('/^[=+\-@\t\r\n]/u', $text) === 1 + || preg_match('/^\d{12,}$/D', $text) === 1 + ? "'" . $text + : $text; + } +} diff --git a/modules/EcommerceFinance/Repositories/SupplierPayableForecastRepository.php b/modules/EcommerceFinance/Repositories/SupplierPayableForecastRepository.php index a11aeb89..044e55e8 100644 --- a/modules/EcommerceFinance/Repositories/SupplierPayableForecastRepository.php +++ b/modules/EcommerceFinance/Repositories/SupplierPayableForecastRepository.php @@ -24,6 +24,28 @@ class SupplierPayableForecastRepository public const DEFAULT_FROM = '2026-10-01'; + public const DEFAULT_FROM_TIME = self::DEFAULT_FROM . ' 00:00:00'; + + private const GOODS_SOURCE_COLUMNS = [ + 'h.Id as receiving_id', + 'h.io_id', + 'h.po_id', + 'h.io_date', + 'h.remark as head_remark', + 'h.warehouse', + 'h.logistics_company', + 'h.status', + 'h.supplier_name', + 'i.Id as item_id', + 'i.sku_id', + 'i.i_id', + 'i.name', + 'i.qty', + 'i.cost_price', + 'i.cost_amount', + 'i.remark as item_remark', + ]; + private readonly ConnectionInterface $connection; private readonly string $connectionName; @@ -125,25 +147,7 @@ class SupplierPayableForecastRepository ->orderByDesc('h.Id') ->orderByDesc('i.Id') ->forPage($page, $perPage) - ->get([ - 'h.Id as receiving_id', - 'h.io_id', - 'h.po_id', - 'h.io_date', - 'h.remark as head_remark', - 'h.warehouse', - 'h.logistics_company', - 'h.status', - 'h.supplier_name', - 'i.Id as item_id', - 'i.sku_id', - 'i.i_id', - 'i.name', - 'i.qty', - 'i.cost_price', - 'i.cost_amount', - 'i.remark as item_remark', - ]); + ->get(self::GOODS_SOURCE_COLUMNS); $priceMap = $this->latestChangePrices( $sourceRows->pluck('sku_id')->filter()->map(static fn (mixed $value): string => trim((string) $value))->unique()->values()->all() @@ -163,12 +167,52 @@ class SupplierPayableForecastRepository ]; } + /** + * 流式读取当前筛选全集,按小批次匹配变价,避免导出把整表装入内存。 + * + * @param array{date_from:string,date_to:string,keyword:string} $filters + * @return \Generator> + */ + public function goodsRowsForExport(array $filters, int $batchSize = 500): \Generator + { + $batchSize = min(max($batchSize, 1), 1000); + $query = $this->filteredGoodsQuery($filters); + $page = 1; + + do { + $sourceRows = (clone $query) + ->orderByDesc('h.io_date') + ->orderByDesc('h.Id') + ->orderByDesc('i.Id') + ->forPage($page, $batchSize) + ->get(self::GOODS_SOURCE_COLUMNS); + + $priceMap = $this->latestChangePrices( + $sourceRows->pluck('sku_id') + ->filter() + ->map(static fn (mixed $value): string => trim((string) $value)) + ->unique() + ->values() + ->all() + ); + + foreach ($sourceRows as $row) { + yield $this->formatGoodsRow( + $row, + $priceMap->get($this->normalizeSku($row->sku_id)), + ); + } + + $page++; + } while ($sourceRows->count() === $batchSize); + } + /** * @param array{date_from:string,date_to:string,keyword:string} $filters */ private function filteredGoodsQuery(array $filters): \Illuminate\Database\Query\Builder { - $from = Carbon::parse($filters['date_from'] ?: self::DEFAULT_FROM)->startOfDay(); + $from = Carbon::parse($filters['date_from'] ?: self::DEFAULT_FROM_TIME); $to = trim((string) ($filters['date_to'] ?? '')); $keyword = trim((string) ($filters['keyword'] ?? '')); @@ -181,7 +225,11 @@ class SupplierPayableForecastRepository ->whereRaw("TRIM(COALESCE(sku_id, '')) NOT LIKE ?", ['DF-%']); if ($to !== '') { - $query->where('h.io_date', '<', Carbon::parse($to)->addDay()->startOfDay()->format('Y-m-d H:i:s')); + $toDate = Carbon::parse($to); + if (preg_match('/^\d{4}-\d{2}-\d{2}$/D', $to) === 1) { + $toDate->endOfDay(); + } + $query->where('h.io_date', '<=', $toDate->format('Y-m-d H:i:s')); } if ($keyword !== '') { @@ -245,7 +293,7 @@ class SupplierPayableForecastRepository 'month' => $occurredAt->format('Y-m'), 'stockNo' => (string) ($row->io_id ?? ''), 'purchaseNo' => (string) ($row->po_id ?? ''), - 'date' => $occurredAt->format('Y-m-d'), + 'date' => $occurredAt->format('Y-m-d H:i:s'), 'supplier' => (string) ($row->supplier_name ?? ''), 'skuCode' => (string) ($row->i_id ?? ''), 'productCode' => (string) ($row->sku_id ?? ''), diff --git a/modules/EcommerceFinance/View/SupplierPayableForecastPageData.php b/modules/EcommerceFinance/View/SupplierPayableForecastPageData.php index f75eda4d..349d01d5 100644 --- a/modules/EcommerceFinance/View/SupplierPayableForecastPageData.php +++ b/modules/EcommerceFinance/View/SupplierPayableForecastPageData.php @@ -42,7 +42,7 @@ class SupplierPayableForecastPageData } $paginationQuery = array_filter([ - 'goods_date_from' => $filters['date_from'] === SupplierPayableForecastRepository::DEFAULT_FROM + 'goods_date_from' => $filters['date_from'] === SupplierPayableForecastRepository::DEFAULT_FROM_TIME ? null : $filters['date_from'], 'goods_date_to' => $filters['date_to'] !== '' ? $filters['date_to'] : null, @@ -79,39 +79,53 @@ class SupplierPayableForecastPageData } /** @return array{date_from:string,date_to:string,keyword:string} */ - private function goodsFilters(Request $request): array + public function goodsFilters(Request $request): array { - $minimum = Carbon::createFromFormat('!Y-m-d', SupplierPayableForecastRepository::DEFAULT_FROM); - $from = $this->validDate($request->query('goods_date_from')); + $minimum = Carbon::createFromFormat('!Y-m-d H:i:s', SupplierPayableForecastRepository::DEFAULT_FROM_TIME); + $from = $this->validDateTime($request->query('goods_date_from')); if ($from === null || $from->lt($minimum)) { - $from = $minimum; + $from = $minimum->copy(); } - $to = $this->validDate($request->query('goods_date_to')); + $to = $this->validDateTime($request->query('goods_date_to'), true); if ($to !== null && $to->lt($from)) { $to = $from->copy(); } return [ - 'date_from' => $from->format('Y-m-d'), - 'date_to' => $to?->format('Y-m-d') ?? '', + 'date_from' => $from->format('Y-m-d H:i:s'), + 'date_to' => $to?->format('Y-m-d H:i:s') ?? '', 'keyword' => mb_substr(trim((string) $request->query('goods_keyword', '')), 0, 120), ]; } - private function validDate(mixed $value): ?Carbon + private function validDateTime(mixed $value, bool $endOfDay = false): ?Carbon { - if (! is_string($value) || preg_match('/^\d{4}-\d{2}-\d{2}$/D', $value) !== 1) { + if (! is_string($value)) { return null; } - $date = Carbon::createFromFormat('!Y-m-d', $value); + $value = trim($value); + $dateOnly = preg_match('/^\d{4}-\d{2}-\d{2}$/D', $value) === 1; + $withTime = preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/D', $value) === 1; + if (! $dateOnly && ! $withTime) { + return null; + } + + $format = $dateOnly ? '!Y-m-d' : '!Y-m-d H:i:s'; + $date = Carbon::createFromFormat($format, $value); $errors = Carbon::getLastErrors(); - return $date !== false - && ($errors === false || ($errors['warning_count'] === 0 && $errors['error_count'] === 0)) - && $date->format('Y-m-d') === $value - ? $date - : null; + if (! $date + || ! ($errors === false || ($errors['warning_count'] === 0 && $errors['error_count'] === 0)) + || $date->format($dateOnly ? 'Y-m-d' : 'Y-m-d H:i:s') !== $value) { + return null; + } + + if ($dateOnly) { + return $endOfDay ? $date->endOfDay() : $date->startOfDay(); + } + + return $date; } } diff --git a/resources/views/business/pages/negative-alerts/supplier-payable-forecast.blade.php b/resources/views/business/pages/negative-alerts/supplier-payable-forecast.blade.php index db35a125..f4706c36 100644 --- a/resources/views/business/pages/negative-alerts/supplier-payable-forecast.blade.php +++ b/resources/views/business/pages/negative-alerts/supplier-payable-forecast.blade.php @@ -244,7 +244,7 @@ .supplier-payable-forecast .npf-filter { display: grid; - grid-template-columns: minmax(140px, .8fr) minmax(140px, .8fr) minmax(240px, 1.8fr) auto; + grid-template-columns: minmax(190px, .9fr) minmax(190px, .9fr) minmax(240px, 1.8fr) auto; align-items: end; gap: 10px; padding: 12px; @@ -313,8 +313,10 @@ .supplier-payable-forecast .npf-button:hover { border-color: var(--npf-gold); background: #FCFAF5; } .supplier-payable-forecast .npf-button:focus-visible { outline: 0; box-shadow: 0 0 0 3px rgba(133, 102, 50, 0.20); } + .supplier-payable-forecast .npf-button:disabled { opacity: .48; cursor: not-allowed; } .supplier-payable-forecast .npf-button.is-primary { border-color: var(--npf-gold); background: var(--npf-gold); color: #FFFFFF; } .supplier-payable-forecast .npf-button.is-primary:hover { border-color: #6A5129; background: #6A5129; } + .supplier-payable-forecast .npf-button .material-icons-outlined { margin-right: 3px; font-size: 16px; } .supplier-payable-forecast .npf-table-shell { max-width: 100%; @@ -482,26 +484,26 @@
- 入库日期起 -
- - -
- 入库日期止 -
- - - @@ -513,6 +515,17 @@
+ 重置
diff --git a/resources/views/business/pages/partials/date-range-picker.blade.php b/resources/views/business/pages/partials/date-range-picker.blade.php index 094d3bc8..b8f3d2ba 100644 --- a/resources/views/business/pages/partials/date-range-picker.blade.php +++ b/resources/views/business/pages/partials/date-range-picker.blade.php @@ -158,6 +158,62 @@ background: var(--vip-content-accent-dark, #856632); color: #ffffff; } + .upstream-data-calendar.has-time { + width: min(18.5rem, calc(100vw - 2rem)); + } + .upstream-data-calendar__time { + display: grid; + grid-template-columns: repeat(3, minmax(0, 1fr)); + gap: 0.375rem; + margin-top: 0.5rem; + padding-top: 0.5rem; + border-top: 1px solid var(--vip-border, #d8d2c7); + } + .upstream-data-calendar__time label { + display: grid; + gap: 0.25rem; + min-width: 0; + color: var(--vip-content-muted, #595959); + font-size: 0.6875rem; + font-weight: 500; + } + .upstream-data-calendar__time input { + width: 100%; + min-width: 0; + height: 2rem; + padding: 0 0.375rem; + border: 1px solid var(--vip-border, #d8d2c7); + border-radius: 0.375rem; + background: var(--vip-surface, #ffffff); + color: var(--vip-content-ink, #171717); + font: inherit; + font-size: 0.75rem; + font-variant-numeric: tabular-nums; + text-align: center; + } + .upstream-data-calendar__time input:focus { + border-color: var(--vip-content-accent-dark, #856632); + outline: none; + box-shadow: 0 0 0 2px rgba(133, 102, 50, 0.18); + } + .upstream-data-calendar__actions { + display: flex; + justify-content: flex-end; + margin-top: 0.5rem; + } + .upstream-data-calendar__apply { + min-height: 1.875rem; + padding: 0 0.75rem; + border: 1px solid var(--vip-content-accent-dark, #856632); + border-radius: 0.375rem; + background: var(--vip-content-accent-dark, #856632); + color: #ffffff; + cursor: pointer; + font: inherit; + font-size: 0.75rem; + font-weight: 600; + } + .upstream-data-calendar__apply:hover { background: #6a5129; } /* 结束日期的日历向右展开会顶出工具栏,改成向左 */ .upstream-data-date:last-of-type .upstream-data-calendar { right: 0; @@ -197,6 +253,36 @@ && left.getDate() === right.getDate(); } + function parseTimeParts(text, endOfDay) { + var match = /^\d{4}-\d{2}-\d{2}[ T](\d{2}):(\d{2}):(\d{2})$/.exec(String(text || '').trim()); + if (!match) { + return endOfDay ? { hour: 23, minute: 59, second: 59 } : { hour: 0, minute: 0, second: 0 }; + } + + return { + hour: Number(match[1]), + minute: Number(match[2]), + second: Number(match[3]) + }; + } + + function clampPart(value, max) { + var number = Number.parseInt(value, 10); + if (Number.isNaN(number)) { return 0; } + + return Math.min(Math.max(number, 0), max); + } + + function dateTimeText(dateText, timeParts) { + return dateText + ' ' + pad2(timeParts.hour) + ':' + pad2(timeParts.minute) + ':' + pad2(timeParts.second); + } + + function timeField(part, label, max, value) { + return ''; + } + function setupDatePicker(picker) { if (picker.dataset.datePickerReady === 'true') { return; } picker.dataset.datePickerReady = 'true'; @@ -206,11 +292,15 @@ var clear = picker.querySelector('[data-date-clear]'); if (!input || !panel || !toggle) { return; } + var withTime = picker.hasAttribute('data-date-time') || input.hasAttribute('data-date-time'); + var endOfDay = picker.hasAttribute('data-date-end-of-day'); var today = new Date(); var selected = parseIsoDate(input.value); + var timeParts = parseTimeParts(input.value, endOfDay); var anchor = selected || today; var viewYear = anchor.getFullYear(); var viewMonth = anchor.getMonth(); + panel.classList.toggle('has-time', withTime); function isOpen() { return !panel.hidden; } @@ -218,6 +308,35 @@ if (clear) { clear.hidden = input.value === ''; } } + function updateInputValue() { + if (!selected) { + input.value = ''; + return; + } + + var dateText = isoDate(selected); + input.value = withTime ? dateTimeText(dateText, timeParts) : dateText; + } + + function readTimeParts() { + if (!withTime) { return; } + var next = {}; + var limits = { hour: 23, minute: 59, second: 59 }; + panel.querySelectorAll('[data-time-part]').forEach(function (node) { + var part = node.getAttribute('data-time-part'); + if (!(part in limits)) { return; } + next[part] = clampPart(node.value, limits[part]); + node.value = pad2(next[part]); + }); + if ('hour' in next && 'minute' in next && 'second' in next) { + timeParts = next; + } + } + + function dispatchChange() { + input.dispatchEvent(new Event('change', { bubbles: true })); + } + function render() { var monthStart = new Date(viewYear, viewMonth, 1); var gridStart = new Date(viewYear, viewMonth, 1 - monthStart.getDay()); @@ -242,12 +361,27 @@ html += ''; } + html += '
'; - panel.innerHTML = html + '
'; + if (withTime) { + html += '
' + + timeField('hour', '时', 23, timeParts.hour) + + timeField('minute', '分', 59, timeParts.minute) + + timeField('second', '秒', 59, timeParts.second) + + '
' + + '
' + + '' + + '
'; + } + + panel.innerHTML = html; } function close() { if (!isOpen()) { return; } + readTimeParts(); + updateInputValue(); + syncClear(); panel.hidden = true; toggle.setAttribute('aria-expanded', 'false'); } @@ -256,6 +390,7 @@ closers.forEach(function (otherClose) { if (otherClose !== close) { otherClose(); } }); if (isOpen()) { return; } selected = parseIsoDate(input.value); + timeParts = parseTimeParts(input.value, endOfDay); anchor = selected || new Date(); viewYear = anchor.getFullYear(); viewMonth = anchor.getMonth(); @@ -285,10 +420,43 @@ } var day = event.target.closest('[data-calendar-day]'); if (day && panel.contains(day)) { - input.value = day.getAttribute('data-calendar-day'); - selected = parseIsoDate(input.value); + selected = parseIsoDate(day.getAttribute('data-calendar-day')); + updateInputValue(); syncClear(); - input.dispatchEvent(new Event('change', { bubbles: true })); + dispatchChange(); + if (withTime) { + render(); + } else { + close(); + } + return; + } + var apply = event.target.closest('[data-calendar-apply]'); + if (apply && panel.contains(apply)) { + readTimeParts(); + updateInputValue(); + syncClear(); + dispatchChange(); + close(); + } + }); + + panel.addEventListener('change', function (event) { + var timeInput = event.target.closest('[data-time-part]'); + if (!withTime || !timeInput || !panel.contains(timeInput)) { return; } + readTimeParts(); + updateInputValue(); + syncClear(); + dispatchChange(); + }); + + panel.addEventListener('keydown', function (event) { + if (withTime && event.key === 'Enter' && event.target.closest('[data-time-part]')) { + event.preventDefault(); + readTimeParts(); + updateInputValue(); + syncClear(); + dispatchChange(); close(); } }); @@ -311,8 +479,9 @@ clear.addEventListener('click', function () { input.value = ''; selected = null; + timeParts = endOfDay ? { hour: 23, minute: 59, second: 59 } : { hour: 0, minute: 0, second: 0 }; syncClear(); - input.dispatchEvent(new Event('change', { bubbles: true })); + dispatchChange(); close(); }); } diff --git a/routes/admin.php b/routes/admin.php index 8ccdfe8a..b7d572e3 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -263,6 +263,10 @@ Route::get('workspace/supplier-payables/supplier-management/contracts/export', [ ->middleware('business.page:supplier-payables,supplier-management') ->name('business.supplier-contracts.export'); +Route::get('workspace/negative-alerts/supplier-payable-forecast/goods/export', [\App\Http\Controllers\Business\SupplierPayableForecastExport::class, 'goods']) + ->middleware('business.page:negative-alerts,supplier-negative-alert') + ->name('business.supplier-payable-forecast.goods.export'); + Route::get('workspace/product-inventory/normal-products/export', 'Business\Pages@exportNormalProducts') ->middleware('business.page:product-inventory,normal-products') ->name('business.product-inventory.normal-products.export'); diff --git a/tests/Feature/Business/NegativeAlertPagesTest.php b/tests/Feature/Business/NegativeAlertPagesTest.php index 409b3e16..23dc1a0f 100644 --- a/tests/Feature/Business/NegativeAlertPagesTest.php +++ b/tests/Feature/Business/NegativeAlertPagesTest.php @@ -421,6 +421,14 @@ class NegativeAlertPagesTest extends FeatureTestCase $html = (string) $response->getContent(); $this->assertStringContainsString('data-date-picker', $html); $this->assertStringContainsString('data-date-input', $html); + $this->assertStringContainsString('data-date-time', $html); + $this->assertStringContainsString('data-date-end-of-day', $html); + $this->assertStringContainsString("timeField('hour', '时', 23", $html); + $this->assertStringContainsString("timeField('second', '秒', 59", $html); + $this->assertStringContainsString('入库时间起', $html); + $this->assertStringContainsString('2026-10-01 00:00:00', $html); + $this->assertStringContainsString(route('business.supplier-payable-forecast.goods.export'), $html); + $this->assertStringContainsString('导出当前筛选', $html); $this->assertStringContainsString('CaiWuDatePicker.setup(filterForm)', $html); $this->assertStringNotContainsString('name="goods_date_from" min=', $html); $this->assertStringNotContainsString('name="goods_date_to" min=', $html); @@ -439,6 +447,94 @@ class NegativeAlertPagesTest extends FeatureTestCase '数量', '单价', '金额', '预估单价', '预估金额', '供应商商品编码', '备注', '仓库名称', '快递名称', '快递单号', '未发出', '对账人', ], $labels); + + $this->loginAs()->get(route('business.pages.show', [ + 'section' => 'negative-alerts', + 'page' => 'supplier-negative-alert', + 'tab' => 'payable-forecast', + 'goods_date_from' => '2026-10-01 10:00:00', + 'goods_date_to' => '2026-10-01 10:00:00', + ]))->assertOk() + ->assertSeeText('IN-20261001') + ->assertSeeText('2026-10-01 10:00:00'); + + $this->loginAs()->get(route('business.pages.show', [ + 'section' => 'negative-alerts', + 'page' => 'supplier-negative-alert', + 'tab' => 'payable-forecast', + 'goods_date_from' => '2026-10-01 10:00:01', + 'goods_date_to' => '2026-10-01 10:00:02', + ]))->assertOk() + ->assertSeeText('没有符合当前条件的货款明细') + ->assertDontSeeText('IN-20261001'); + + $this->loginAs()->get(route('business.pages.show', [ + 'section' => 'negative-alerts', + 'page' => 'supplier-negative-alert', + 'tab' => 'payable-forecast', + 'goods_date_from' => '2026-10-01', + 'goods_date_to' => '2026-10-01', + ]))->assertOk() + ->assertSeeText('IN-20261001'); + } + + public function test_supplier_payable_forecast_export_streams_all_rows_matching_second_precision(): void + { + $this->createSupplierPayableForecastTables(); + $now = now(); + DB::connection('business_mirror')->table('business_mirror_table_states')->insert([ + ['dataset_key' => SupplierPayableForecastRepository::HEAD_DATASET, 'status' => 'succeeded', 'initial_completed_at' => $now, 'last_succeeded_at' => $now], + ['dataset_key' => SupplierPayableForecastRepository::ITEM_DATASET, 'status' => 'succeeded', 'initial_completed_at' => $now, 'last_succeeded_at' => $now], + ['dataset_key' => SupplierPayableForecastRepository::PRICE_DATASET, 'status' => 'succeeded', 'initial_completed_at' => $now, 'last_succeeded_at' => $now], + ]); + + for ($index = 1; $index <= 22; $index++) { + $ioId = $index === 21 ? '123456789012345678' : 'IN-EXPORT-' . $index; + DB::connection('business_mirror')->table(SupplierPayableForecastRepository::HEAD_TABLE)->insert([ + 'Id' => $index, + 'io_id' => $ioId, + 'po_id' => 800000 + $index, + 'io_date' => $index === 22 ? '2026-11-01 10:00:01' : '2026-11-01 10:00:00', + 'warehouse' => '正常仓', + 'supplier_name' => '导出供应商', + ]); + DB::connection('business_mirror')->table(SupplierPayableForecastRepository::ITEM_TABLE)->insert([ + 'Id' => $index, + 'io_id' => $ioId, + 'ioi_id' => 'ITEM-EXPORT-' . $index, + 'sku_id' => 'SKU-EXPORT-' . $index, + 'i_id' => 'STYLE-EXPORT-' . $index, + 'name' => $index === 21 ? '=危险商品' : '导出商品 ' . $index, + 'qty' => 1, + 'cost_price' => 3, + 'cost_amount' => 3, + ]); + } + + $response = $this->loginAs()->get(route('business.supplier-payable-forecast.goods.export', [ + 'goods_date_from' => '2026-11-01 00:00:00', + 'goods_date_to' => '2026-11-01 10:00:00', + ]))->assertOk() + ->assertHeader('content-type', 'text/csv; charset=UTF-8') + ->assertDownload(); + + $csv = $response->streamedContent(); + $this->assertStringStartsWith("\xEF\xBB\xBF", $csv); + $this->assertSame(22, substr_count($csv, "\n")); + $this->assertStringContainsString('IN-EXPORT-1', $csv); + $this->assertStringContainsString('IN-EXPORT-20', $csv); + $this->assertStringContainsString('STYLE-EXPORT-21', $csv); + $this->assertStringNotContainsString('IN-EXPORT-22', $csv); + $this->assertStringContainsString("'123456789012345678", $csv); + $this->assertStringContainsString("'=危险商品", $csv); + + $lines = preg_split('/\r\n|\n|\r/', trim(substr($csv, 3))) ?: []; + $this->assertSame([ + '月份', '入库单号', '采购单号', '日期', '供应商', '货号', '商品编码', '规格名称', + '数量', '单价', '金额', '预估单价', '预估金额', '供应商商品编码', '备注', + '仓库名称', '快递名称', '快递单号', '未发出', '对账人', + ], str_getcsv($lines[0])); + $this->assertSame('2026-11-01 10:00:00', str_getcsv($lines[1])[3]); } private function createSupplierPayableForecastTables(): void diff --git a/tests/Unit/EcommerceFinance/SupplierPayableForecastRepositoryTest.php b/tests/Unit/EcommerceFinance/SupplierPayableForecastRepositoryTest.php index b28ca42c..baed27c7 100644 --- a/tests/Unit/EcommerceFinance/SupplierPayableForecastRepositoryTest.php +++ b/tests/Unit/EcommerceFinance/SupplierPayableForecastRepositoryTest.php @@ -49,6 +49,45 @@ class SupplierPayableForecastRepositoryTest extends TestCase ], 1); $this->assertSame(1, $keyword['total']); $this->assertSame('SKU-A', $keyword['rows'][0]['productCode']); + + $exportRows = iterator_to_array(app(SupplierPayableForecastRepository::class)->goodsRowsForExport([ + 'date_from' => '2026-10-01 00:00:00', + 'date_to' => '', + 'keyword' => '', + ], 1)); + $this->assertCount(2, $exportRows); + $this->assertSame(['SKU-B', 'SKU-A'], array_column($exportRows, 'productCode')); + } + + public function test_goods_date_filters_are_inclusive_to_the_second_and_keep_legacy_date_ranges(): void + { + $this->createTables(); + $this->seedRows(); + $repository = app(SupplierPayableForecastRepository::class); + + $exact = $repository->goodsDetails([ + 'date_from' => '2026-10-02 09:00:00', + 'date_to' => '2026-10-02 09:00:00', + 'keyword' => '', + ], 1); + $this->assertSame(1, $exact['total']); + $this->assertSame('IN-1', $exact['rows'][0]['stockNo']); + $this->assertSame('2026-10-02 09:00:00', $exact['rows'][0]['date']); + + $afterBoundary = $repository->goodsDetails([ + 'date_from' => '2026-10-02 09:00:01', + 'date_to' => '2026-10-02 09:00:02', + 'keyword' => '', + ], 1); + $this->assertSame(0, $afterBoundary['total']); + + $legacy = $repository->goodsDetails([ + 'date_from' => '2026-10-02', + 'date_to' => '2026-10-02', + 'keyword' => '', + ], 1); + $this->assertSame(1, $legacy['total']); + $this->assertSame('IN-1', $legacy['rows'][0]['stockNo']); } private function createTables(): void