Contract reference text format inside exported excel

This commit is contained in:
Simon Pocrnjič 2025-12-10 21:15:53 +01:00
parent 53941c054e
commit 79de54eef0
2 changed files with 14 additions and 2 deletions

View File

@ -11,11 +11,14 @@
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
class SegmentContractsExport implements FromQuery, ShouldAutoSize, WithColumnFormatting, WithHeadings, WithMapping
{
public const DATE_EXCEL_FORMAT = 'dd"."mm"."yyyy';
public const TEXT_EXCEL_FORMAT = NumberFormat::FORMAT_TEXT;
/**
* @var array<string, array{label: string}>
*/
@ -76,8 +79,16 @@ public function columnFormats(): array
$formats = [];
foreach ($this->columns as $index => $column) {
$letter = $this->columnLetter($index);
if ($column === 'reference') {
$formats[$letter] = self::TEXT_EXCEL_FORMAT;
continue;
}
if (in_array($column, ['start_date', 'end_date'], true)) {
$formats[$this->columnLetter($index)] = self::DATE_EXCEL_FORMAT;
$formats[$letter] = self::DATE_EXCEL_FORMAT;
}
}

View File

@ -141,7 +141,7 @@ public function test_export_filename_includes_client_name_when_filtered(): void
});
}
public function test_column_formats_apply_to_date_columns(): void
public function test_column_formats_apply_to_reference_and_date_columns(): void
{
$export = new SegmentContractsExport(
Contract::query(),
@ -150,6 +150,7 @@ public function test_column_formats_apply_to_date_columns(): void
$this->assertSame(
[
'A' => SegmentContractsExport::TEXT_EXCEL_FORMAT,
'B' => SegmentContractsExport::DATE_EXCEL_FORMAT,
'D' => SegmentContractsExport::DATE_EXCEL_FORMAT,
],