HEX
Server: nginx/1.24.0
System: Linux localhost 5.15.0-46-generic #49-Ubuntu SMP Thu Aug 4 18:03:25 UTC 2022 x86_64
User: www (1000)
PHP: 8.3.27
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot/erp.nhatnamsst.com/domains/Debt/Exports/DebtExport.php
<?php

namespace Domains\Debt\Exports;

use Domains\Core\Enums\AdminRole;
use Domains\Core\Enums\DebtStatus;
use Domains\Debt\Models\Debt;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Fill;

class DebtExport implements FromCollection, WithHeadings, WithMapping, WithStyles, WithColumnWidths
{
    protected $query;

    public function __construct($query = null)
    {
        $this->query = $query;
    }

    public function collection()
    {
        if ($this->query) {
            return $this->query->get();
        }

        $query = Debt::with(['sale', 'saleLeader', 'customer', 'details', 'payments']);
        
        $currentAdmin = get_auth_admin();
        if ($currentAdmin && !$currentAdmin->checkIsSuperAdmin()) {
            if ($currentAdmin->role === AdminRole::SALE) {
                $query->where('sale_id', $currentAdmin->id);
            } elseif ($currentAdmin->role === AdminRole::LEADER_SALE) {
                $teamMemberIds = $currentAdmin->getTeamMemberIds();
                $query->whereIn('sale_id', $teamMemberIds);
            }
        }
        
        return $query->get();
    }

    public function headings(): array
    {
        return [
            'Số hóa đơn',
            'Ngày xuất HĐ',
            'Ngày công nợ',
            'Sale',
            'Sale Leader',
            'Khách hàng',
            'Tổng giá PO',
            'Tổng thanh toán',
            'Còn lại',
            'Trạng thái',
        ];
    }

    public function map($debt): array
    {
        $invoiceDate = $debt->invoice_date 
            ? $debt->invoice_date->format('d/m/Y') 
            : '-';
        
        $debtAgingDate = $debt->debt_aging_date 
            ? $debt->debt_aging_date->format('d/m/Y') 
            : '-';
        
        $saleName = $debt->sale 
            ? ($debt->sale->code ?? '') . ($debt->sale->code ? ' - ' : '') . $debt->sale->fullname 
            : '-';
        
        $saleLeaderName = $debt->saleLeader 
            ? ($debt->saleLeader->code ?? '') . ($debt->saleLeader->code ? ' - ' : '') . $debt->saleLeader->fullname 
            : '-';
        
        $customerName = $debt->customer ? $debt->customer->name : '-';
        
        $totalPoPrice = number_format($debt->total_po_price, 0, ',', '.');
        
        $totalPaymentAmount = number_format($debt->total_payment_amount, 0, ',', '.');
        
        $remainingAmount = number_format($debt->remaining_amount, 0, ',', '.');
        
        $status = $debt->status instanceof DebtStatus 
            ? $debt->status 
            : DebtStatus::from($debt->status ?? 1);
        $statusText = $status->description();

        return [
            $debt->invoice_number,
            $invoiceDate,
            $debtAgingDate,
            $saleName,
            $saleLeaderName,
            $customerName,
            $totalPoPrice,
            $totalPaymentAmount,
            $remainingAmount,
            $statusText,
        ];
    }

    public function styles(Worksheet $sheet)
    {
        return [
            1 => [
                'font' => ['bold' => true, 'size' => 12],
                'fill' => [
                    'fillType' => Fill::FILL_SOLID,
                    'startColor' => ['rgb' => 'E2E8F0']
                ],
                'alignment' => [
                    'horizontal' => Alignment::HORIZONTAL_CENTER,
                    'vertical' => Alignment::VERTICAL_CENTER,
                ],
            ],
        ];
    }

    public function columnWidths(): array
    {
        return [
            'A' => 20, // Số hóa đơn
            'B' => 15, // Ngày xuất HĐ
            'C' => 15, // Ngày công nợ
            'D' => 25, // Sale
            'E' => 25, // Sale Leader
            'F' => 30, // Khách hàng
            'G' => 20, // Tổng giá PO
            'H' => 20, // Tổng thanh toán
            'I' => 20, // Còn lại
            'J' => 20, // Trạng thái
        ];
    }
}