CasperSecurity
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use Carbon\Carbon;
class LedgerReportExport implements FromCollection, WithHeadings, WithEvents
{
protected $ledgerData, $openingBalance, $startDate, $endDate, $ledgerHeadName;
public function __construct($ledgerData, $openingBalance, $startDate, $endDate, $ledgerHeadName)
{
$this->ledgerData = $ledgerData;
$this->openingBalance = $openingBalance;
$this->startDate = $startDate;
$this->endDate = $endDate;
$this->ledgerHeadName = $ledgerHeadName;
}
public function collection()
{
$rows = collect();
$balance = $this->openingBalance;
$slNo = 1;
$debitTotal = 0;
$creditTotal = 0;
// Opening Balance row
$rows->push([
'Sl No.' => $slNo++,
'Date' => Carbon::parse($this->startDate)->format('d-m-Y'),
'Voucher No.' => 'Opening Balance',
'Ledger Head' => $this->ledgerHeadName ?? 'Opening Balance',
'Party' => '',
'Particular' => '',
'Debit' => $this->openingBalance < 0 ? abs($this->openingBalance) : 0,
'Credit' => $this->openingBalance > 0 ? abs($this->openingBalance) : 0,
'Balance' => $balance,
]);
// Calculate debit and credit totals for opening balance
if ($this->openingBalance < 0) {
$debitTotal += abs($this->openingBalance);
} else {
$creditTotal += abs($this->openingBalance);
}
// Ledger Data rows
foreach ($this->ledgerData as $ld) {
$debit = $ld->amount < 0 ? abs($ld->amount) : 0;
$credit = $ld->amount > 0 ? abs($ld->amount) : 0;
$balance += $ld->amount;
// Update the debit and credit totals
$debitTotal += $debit;
$creditTotal += $credit;
// Map Voucher No. and Particulars
$rows->push([
'Sl No.' => $slNo++,
'Date' => isset($ld->created_at) ? Carbon::parse($ld->created_at)->format('d-m-Y') : 'N/A', // Use created_at here
'Voucher No.' => $ld->voucher_no ?? 'N/A',
'Ledger Head' => $ld->ledger_head_name ?? 'N/A',
'Party' => $ld->paid_to_received_from ?? 'N/A',
'Particular' => $ld->voucher_particulars ?? 'N/A',
'Debit' => $debit,
'Credit' => $credit,
'Balance' => $balance,
]);
}
// Total row
$rows->push([
'Sl No.' => '',
'Date' => '',
'Voucher No.' => '',
'Ledger Head' => '',
'Party' => '',
'Particular' => 'Total',
'Debit' => $debitTotal,
'Credit' => $creditTotal,
'Balance' => '',
]);
return $rows;
}
public function headings(): array
{
return ['Sl No.', 'Date', 'Voucher No.', 'Ledger Head', 'Party', 'Particular', 'Debit', 'Credit', 'Balance'];
}
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
$sheet = $event->sheet->getDelegate();
$highestRow = $sheet->getHighestRow();
// Bold headers
$sheet->getStyle('A1:I1')->getFont()->setBold(true);
$sheet->getStyle('A1:I1')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
foreach (range('A', 'I') as $col) {
$sheet->getColumnDimension($col)->setAutoSize(true);
}
$sheet->getStyle("G2:I{$highestRow}")
->getNumberFormat()
->setFormatCode('#,##0.00');
$sheet->getStyle("A2:D{$highestRow}")
->getAlignment()
->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$sheet->getStyle("G2:I{$highestRow}")
->getAlignment()
->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT);
$sheet->getStyle("F{$highestRow}:H{$highestRow}")->getFont()->setBold(true);
},
];
}
}