Files
basicdata/app/Exports/BranchExport.php
Daeng Deni Mardaeni 4c4a4a33a9 fix(export): tambahkan properti parent_id pada BranchExport
- Menambahkan properti `protected $parent_id` pada kelas **BranchExport**.
- Properti ini digunakan untuk menyimpan ID cabang induk yang akan dipakai sebagai filter saat melakukan ekspor data.
- Memastikan nilai `parent_id` diinisialisasi melalui constructor dan dideklarasikan sebagai properti kelas.
- Perbaikan ini memastikan filter berdasarkan cabang induk berfungsi dengan benar pada proses ekspor.
- Mendukung konsistensi data pada ekspor **Excel/CSV** dengan filter yang akurat.
- Meningkatkan keandalan fitur ekspor cabang dalam modul laporan.
- Mengoptimalkan integrasi antara fitur filter dan proses ekspor.
- Memperbaiki potensi bug yang muncul akibat properti tidak dideklarasikan.
- Menjamin proses ekspor lebih stabil dan sesuai dengan parameter yang diberikan.
2025-09-08 15:12:27 +07:00

83 lines
2.4 KiB
PHP

<?php
namespace Modules\Basicdata\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Modules\Basicdata\Models\Branch;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
class BranchExport implements WithColumnFormatting, WithHeadings, FromCollection, WithMapping
{
protected $search;
protected $parent_id;
public function __construct($search = null, $parent_id = null)
{
$this->search = $search;
$this->parent_id = $parent_id;
}
public function collection()
{
$query = Branch::query();
if (!empty($this->search)) {
$search = strtolower($this->search);
$query->where(function ($q) use ($search) {
$q->whereRaw('LOWER(code) LIKE ?', ['%' . $search . '%'])
->orWhereRaw('LOWER(name) LIKE ?', ['%' . $search . '%'])
->orWhereRaw('LOWER(address) LIKE ?', ['%' . $search . '%'])
->orWhereHas('parent', function ($q) use ($search) {
$q->whereRaw('LOWER(name) LIKE ?', ['%' . strtolower($search) . '%']);
});
});
}
// Apply parent filter if provided
if (isset($this->parent_id) && !empty($this->parent_id)) {
$parentId = $this->parent_id;
$query->where('parent_id', $parentId);
}
return $query->get();
}
public function map($row)
: array
{
return [
$row->id,
$row->code,
$row->name,
$row->parent ? $row->parent->name : '',
$row->address,
$row->created_at
];
}
public function headings()
: array
{
return [
'ID',
'Code',
'Name',
'Parent Branch',
'Address',
'Created At'
];
}
public function columnFormats()
: array
{
return [
'A' => NumberFormat::FORMAT_NUMBER,
'E' => NumberFormat::FORMAT_DATE_DATETIME
];
}
}