Files
admin-erahn/app/Exports/BillplzTransactionSummarySheet.php
2026-06-24 15:34:26 +08:00

181 lines
5.8 KiB
PHP

<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class BillplzTransactionSummarySheet extends DefaultValueBinder implements FromArray, WithColumnFormatting, WithColumnWidths, WithCustomValueBinder, WithEvents, WithStrictNullComparison, WithStyles, WithTitle
{
protected $summary_data;
protected $sheet_title;
protected $date;
public function __construct($summary_data, $sheet_title, $date = null)
{
$this->summary_data = $summary_data;
$this->sheet_title = $sheet_title;
$this->date = $date;
}
public function title(): string
{
return $this->sheet_title;
}
public function bindValue(Cell $cell, $value)
{
if ($cell->getColumn() === 'J' && $value !== '' && $value !== null) {
$cell->setValueExplicit((string) $value, DataType::TYPE_STRING);
return true;
}
return parent::bindValue($cell, $value);
}
public function columnFormats(): array
{
return [
'J' => NumberFormat::FORMAT_TEXT,
];
}
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
$sheet = $event->sheet->getDelegate();
$highest_column = $sheet->getHighestColumn(8);
$sheet->mergeCells('A1:' . $highest_column . '1');
$sheet->getStyle('A1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$sheet->getStyle('B6')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT);
},
];
}
public function columnWidths(): array
{
return [
'A' => 25,
'B' => 22,
'C' => 15,
'D' => 22,
'E' => 15,
'F' => 22,
'G' => 20,
'H' => 15,
'I' => 30,
'J' => 16,
'K' => 20,
];
}
public function styles(Worksheet $sheet)
{
return [
1 => ['alignment' => ['wrapText' => true, 'horizontal' => Alignment::HORIZONTAL_CENTER]],
3 => ['alignment' => ['wrapText' => true]],
4 => ['alignment' => ['wrapText' => true]],
5 => ['alignment' => ['wrapText' => true]],
6 => ['alignment' => ['wrapText' => true]],
8 => ['alignment' => ['wrapText' => true]],
];
}
public function array(): array
{
$title = 'Billplz Transaction Summary' . ($this->date ? ' (' . $this->date . ')' : '');
$rows = [];
$rows[] = [$title];
$rows[] = ['', '', '', '', '', '', '', '', '', '', ''];
$rows[] = ['Sum of Billplz Transactions', $this->summary_data['sumOfBillplzTransactions'] ?? ''];
$rows[] = ['Sum of Arrahn Transactions', $this->summary_data['sumOfArrahnTransactions'] ?? ''];
$rows[] = ['Payment Gateway Cost', $this->summary_data['paymentGatewayCost'] ?? ''];
$rows[] = ['No of Arrahn Transactions', $this->summary_data['numberOfArrahnTransactions'] ?? ''];
$rows[] = ['', '', '', '', '', '', '', '', '', '', ''];
$rows[] = [
'Bil',
'Billplz ID',
'Billplz Status',
'Billplz Paid Amount (RM)',
'Arrahn Status',
'Arrahn Total Payment',
'Arrahn Norujukan',
'Arrahn Kodcaw',
'Arrahn Customer Name',
'Arrahn No. KP',
'Arrahn Paid At',
];
$bil = 1;
$details = $this->summary_data['billPlzTransactionSummary'] ?? [];
foreach ($details as $data) {
$billplz_bill = $data['billplz_bill'] ?? null;
$arrahn_transactions = $data['arrahn_transactions'] ?? [];
$billplz_id = $billplz_bill['id'] ?? '';
$billplz_status = $billplz_bill['state'] ?? '';
$billplz_paid_amount = '';
if (isset($billplz_bill['paid_amount']['amount'])) {
$billplz_paid_amount = floatval($billplz_bill['paid_amount']['amount']) / 100;
}
if (empty($arrahn_transactions)) {
$rows[] = [
$bil++,
$billplz_id,
$billplz_status,
$billplz_paid_amount,
'',
'',
'',
'',
'',
'',
'',
];
continue;
}
foreach ($arrahn_transactions as $transaction) {
$rows[] = [
$bil++,
$billplz_id,
$billplz_status,
$billplz_paid_amount,
$transaction['status'] ?? '',
$transaction['total_payment'] ?? '',
$transaction['norujukan'] ?? '',
$transaction['kodcaw'] ?? '',
$transaction['customer_name'] ?? '',
$transaction['nokp'] ?? '',
$transaction['paid_at'] ?? '',
];
}
}
return $rows;
}
}