商品月加权平均单价工具区与进销存总表统一:全部筛选结果/当前显示列及顺序+导出,新增本页导出接口;月份显示改为 Y 年 m 月

This commit is contained in:
chenxi 2026-09-29 16:58:12 +08:00
parent ea7dcff178
commit 2d91935304
3 changed files with 95 additions and 3 deletions

View File

@ -460,6 +460,88 @@ class Pages extends Controller
]);
}
/**
* 商品月加权平均单价导出:按当前月份与筛选条件导出 CSV,列序跟随页面显示列。
*/
public function exportMonthlyWeightedAveragePrice(Request $request): StreamedResponse
{
$connection = (string) config('upstream_mirrors.connection', 'business_mirror');
$map = [
'month' => ['report_month', '月份'],
'barcode_warehouse' => ['barcode_warehouse', '条码+仓库'],
'warehouse_short_name' => ['warehouse_short_name', '仓库简称'],
'supplier' => ['supplier', '供应商'],
'product_code' => ['product_code', '商品编码'],
'product_name' => ['product_name', '商品名称'],
'opening_quantity' => ['opening_quantity', '原始数量'],
'opening_unit_price' => ['opening_unit_price', '原始价格'],
'opening_original_amount' => ['opening_original_amount', '原始期初金额'],
'opening_adjusted_price' => ['adjusted_unit_price', '更改价格'],
'opening_amount' => ['opening_amount', '期初金额'],
'purchase_quantity' => ['purchase_quantity', '采购数量'],
'purchase_unit_price' => ['purchase_unit_price', '采购单价'],
'purchase_amount' => ['purchase_amount', '入库金额'],
'weighted_average_unit_price' => ['weighted_average_unit_price', '加权平均单价'],
'pricing_status' => ['pricing_status', '定价状态'],
];
$months = DB::connection($connection)->table('product_monthly_weighted_average_states')
->where('status', 'computed')->orderByDesc('report_month')->pluck('report_month')
->map(static fn ($month): string => substr((string) $month, 0, 10))->all();
$requested = substr(trim((string) $request->query('month', '')), 0, 10);
$month = in_array($requested, $months, true) ? $requested : ($months[0] ?? null);
$query = DB::connection($connection)->table('product_monthly_weighted_average_rows');
if ($month !== null) {
$query->where('report_month', $month);
}
$code = trim((string) $request->query('product_code', ''));
if ($code !== '') {
$query->where('product_code', $code);
}
$provider = trim((string) $request->query('warehouse_provider', ''));
if ($provider !== '') {
$query->where('warehouse_short_name', 'like', '%' . addcslashes($provider, '%_\\') . '%');
}
$category = trim((string) $request->query('category', ''));
if ($category !== '') {
$query->where('product_name', 'like', '%' . addcslashes($category, '%_\\') . '%');
}
$selected = array_values(array_intersect(
array_filter(explode(',', (string) $request->query('columns', ''))),
array_keys($map),
));
if ($selected === []) {
$selected = array_keys($map);
}
$size = min(max((int) setting('default.list_limit', 25), 1), 50);
if ($request->query('scope') === 'page') {
$query->forPage(max((int) $request->query('page', 1), 1), $size);
}
$filename = '商品月加权平均单价_' . ($month ?? 'all') . '.csv';
return response()->streamDownload(function () use ($query, $selected, $map): void {
$out = fopen('php://output', 'w');
fwrite($out, "\xEF\xBB\xBF");
fputcsv($out, array_map(static fn (string $key): string => $map[$key][1], $selected));
$query->orderBy('warehouse_short_name')->orderBy('product_code')
->chunk(1000, function ($rows) use ($out, $selected, $map): void {
foreach ($rows as $row) {
$line = [];
foreach ($selected as $key) {
$value = $row->{$map[$key][0]} ?? '';
$line[] = is_string($value) ? $value : (string) $value;
}
fputcsv($out, $line);
}
});
fclose($out);
}, $filename, ['Content-Type' => 'text/csv; charset=UTF-8']);
}
protected function viewData(
array $section,
?array $page = null,

View File

@ -1322,7 +1322,7 @@
<select id="directory-month-{{ $page['slug'] ?? 'page' }}" name="month" onchange="this.form.submit()">
@foreach ($directoryMonthOptions as $directoryMonthOption)
<option value="{{ $directoryMonthOption }}" @selected($directoryMonthOption === $directoryActiveMonth)>
{{ \Illuminate\Support\Carbon::parse($directoryMonthOption)->format('Y年n月') }}
{{ \Illuminate\Support\Carbon::parse($directoryMonthOption)->format('Y 年 m 月') }}
</option>
@endforeach
</select>
@ -1342,7 +1342,7 @@
@endforeach
</div>
</div>
@if (($page['slug'] ?? '') === 'inventory-report')
@if (in_array(($page['slug'] ?? ''), ['inventory-report', 'monthly-weighted-average-price'], true))
<select data-inventory-export-scope aria-label="导出范围">
<option value="all">全部筛选结果</option>
<option value="page">当前页(第 {{ $directoryCurrentPage }} 页)</option>
@ -1351,7 +1351,9 @@
<option value="visible">当前显示列及顺序</option>
<option value="all">全部可导出字段</option>
</select>
<button type="button" data-inventory-export data-export-url="{{ route('business.product-inventory.inventory-report.export') }}">导出</button>
<button type="button" data-inventory-export data-export-url="{{ ($page['slug'] ?? '') === 'inventory-report'
? route('business.product-inventory.inventory-report.export')
: route('business.product-inventory.monthly-weighted-average-price.export') }}">导出</button>
@else
<div class="directory-export-group" aria-label="导出范围">
<button type="button" data-directory-export="visible">当前页·显示列</button>

View File

@ -156,10 +156,18 @@ Route::get('workspace/product-inventory/online-daifa-price/export', 'Business\Pa
->middleware('business.page:product-inventory,online-daifa-price')
->name('business.product-inventory.online-daifa-price.export');
Route::get('workspace/product-inventory/monthly-weighted-average-price/export', 'Business\Pages@exportMonthlyWeightedAveragePrice')
->middleware('permission:read-business-pages')
->name('business.product-inventory.monthly-weighted-average-price.export');
Route::get('workspace/product-inventory/inventory-report/export', 'Business\Pages@exportInventoryReport')
->middleware('business.page:product-inventory,inventory-report')
->name('business.product-inventory.inventory-report.export');
Route::get('workspace/product-inventory/monthly-weighted-average-price/export', 'Business\Pages@exportMonthlyWeightedAveragePrice')
->middleware('business.page:product-inventory,monthly-weighted-average-price')
->name('business.product-inventory.monthly-weighted-average-price.export');
Route::prefix('workspace/product-inventory/inventory-report/warehouse-mappings')
->middleware('business.page:product-inventory,inventory-report')
->name('business.product-inventory.inventory-report.warehouse-mappings.')