30 lines
2.1 KiB
PHP
30 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Business;
|
|
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\EcommerceFinance\View\AccessoryReshipPageData;
|
|
|
|
class AccessoryReshipDeductions extends Controller
|
|
{
|
|
public function export(AccessoryReshipPageData $data)
|
|
{
|
|
$filters = $data->filters();
|
|
// 先完成读取,失败时返回正常错误,避免下载半份扣款清单。
|
|
$file = tmpfile();
|
|
if ($file === false) abort(500, '无法创建导出文件。');
|
|
fwrite($file, "\xEF\xBB\xBF");
|
|
fputcsv($file, ['交易单号','补发快递单号','补发快递','补发时间','原始条形码','处理后条形码','补发运费','匹配状态','首发快递公司(候选)','首发快递单号(候选)','首发所属月份(候选)','扣款金额','说明']);
|
|
foreach ($data->rows($filters) as $row) {
|
|
if ($filters['match_status'] !== 'all' && $row['status'] !== $filters['match_status']) continue;
|
|
$values = [$row['trade_no'], $row['express_no'], ($row['express_name1'] ?: ($row['express_name2'] ?? '')), $row['ship_time'], $row['barcode'], $row['normalized_barcode'], $row['freight'], $row['status'] === 'matched' ? '可扣款' : '未匹配不扣款', implode(' / ', array_column($row['matches'], 'courier')), implode(' / ', array_column($row['matches'], 'express_no')), implode(' / ', array_column($row['matches'], 'month')), $row['deduction_cents'] === null ? '' : number_format($row['deduction_cents'] / 100, 2, '.', ''), $row['freight_cents'] === null ? '运费缺失或异常,待核实' : (count($row['matches']) > 1 ? '多候选待确认归属;运费只计一次' : '')];
|
|
fputcsv($file, array_map(static function ($value) {
|
|
$value = (string) $value;
|
|
return preg_match('/^[=+\-@\t\r\n]/u', $value) ? "'" . $value : $value;
|
|
}, $values));
|
|
}
|
|
rewind($file);
|
|
return response()->streamDownload(function () use ($file) { fpassthru($file); fclose($file); }, '配件补发扣款-' . $filters['month'] . '.csv', ['Content-Type' => 'text/csv; charset=UTF-8']);
|
|
}
|
|
}
|