CaiWuXiTong/app/Jobs/Common/InventoryReportExport.php
chenxi 26556a9169 Add warehouse short names to inventory report
Restore warehouse name mapping ledger and inventory short-name column.
2026-09-28 16:33:38 +08:00

84 lines
2.8 KiB
PHP

<?php
namespace App\Jobs\Common;
use App\Abstracts\JobShouldQueue;
use App\Business\InventoryWarehouseShortNames;
use App\Notifications\Common\ExportFailed;
use Illuminate\Support\Facades\File;
use Modules\EcommerceFinance\Repositories\ThirdPartyWarehouseStorageMirrorRepository;
use Throwable;
class InventoryReportExport extends JobShouldQueue
{
/** A complete month can exceed the web request and default queue timeout. */
public int $timeout = 600;
public int $tries = 1;
protected int $company_id = 1;
public function __construct(
protected $user,
protected array $filters,
protected array $columns,
protected string $file_name,
?int $companyId = null,
) {
$this->company_id = $companyId ?? 1;
$this->onQueue('exports');
}
public function handle(ThirdPartyWarehouseStorageMirrorRepository $repository, InventoryWarehouseShortNames $shortNames): void
{
$path = storage_path('app/temp/' . $this->file_name);
File::ensureDirectoryExists(dirname($path));
try {
$csv = fopen($path, 'wb');
if ($csv === false) {
throw new \RuntimeException('无法创建进销存报表导出文件。');
}
fputs($csv, chr(0xEF) . chr(0xBB) . chr(0xBF));
fputcsv($csv, array_column($this->columns, 'label'));
$names = $shortNames->forCompany($this->company_id);
foreach ($repository->exportMonthlyInventoryReportRows($this->filters) as $row) {
$row = $shortNames->fill($row, $names);
fputcsv($csv, array_map(function (array $column) use ($row): mixed {
$value = data_get($row, $column['name']);
return $column['name'] === 'product_code'
? $this->csvIdentifier($value)
: (($column['kind'] ?? null) === 'integer'
? ($value === null || $value === '' ? '' : $value)
: $this->csvText($value));
}, $this->columns));
}
fclose($csv);
dispatch(new CreateMediableForExport($this->user, $this->file_name, '进销存报表'));
} catch (Throwable $exception) {
if (isset($csv) && is_resource($csv)) {
fclose($csv);
}
@unlink($path);
report($exception);
$this->user->notify(new ExportFailed($exception->getMessage()));
throw $exception;
}
}
private function csvIdentifier(mixed $value): string
{
$text = trim((string) ($value ?? ''));
return $text === '' ? '' : "'" . $text;
}
private function csvText(mixed $value): string
{
return trim((string) ($value ?? ''));
}
}