Spaces:
Running
Running
File size: 12,526 Bytes
e31284f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | <?php
class ReceiptGenerator
{
private $fontPath;
private $logoPath;
public function __construct()
{
if (!extension_loaded('gd')) {
throw new Exception("The PHP GD extension is not enabled. Please enable it in your php.ini or install php-gd.");
}
// Try to find a suitable font
$fonts = [
'C:/Windows/Fonts/arial.ttf',
'/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',
'/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf',
'/usr/share/fonts/TTF/arial.ttf'
];
$this->fontPath = null;
foreach ($fonts as $font) {
if (file_exists($font)) {
$this->fontPath = $font;
break;
}
}
$this->logoPath = __DIR__ . '/../assets/logo.png';
}
public function generate($data)
{
$width = 900; // Increased width to accommodate 6 columns comfortably
// Calculate dynamic height
$baseHeight = 450;
$rowHeight = 40;
$numRows = count($data['allocations'] ?? []);
$height = $baseHeight + ($numRows * $rowHeight) + 150;
$image = imagecreatetruecolor($width, $height);
// Colors
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$grey = imagecolorallocate($image, 100, 100, 100);
$lightGrey = imagecolorallocate($image, 240, 240, 240);
imagefilledrectangle($image, 0, 0, $width, $height, $white);
// Logo
if (file_exists($this->logoPath)) {
$logo = @imagecreatefrompng($this->logoPath);
if ($logo) {
$logoW = imagesx($logo);
$logoH = imagesy($logo);
$targetW = 100;
$targetH = ($targetW / $logoW) * $logoH;
imagecopyresampled($image, $logo, 40, 30, 0, 0, (int) $targetW, (int) $targetH, (int) $logoW, (int) $logoH);
}
}
// School Name Logic
$schoolName = "ACE JUNIOR COLLEGE";
$levelName = isset($data['level_name']) ? strtoupper($data['level_name']) : '';
// Check for Senior classes (SSS 1, SSS 2, SSS 3, or Senior)
if (strpos($levelName, 'SSS') !== false || strpos($levelName, 'SENIOR') !== false) {
$schoolName = "ACE SENIOR COLLEGE";
}
// Header
$this->centerText($image, 18, 50, $schoolName, $black);
$this->centerText($image, 10, 75, "...education for excellence", $grey);
$this->centerText($image, 10, 100, "1, Ebute - Igbogbo Road, Ipakodo, Ikorodu, Lagos, Nigeria", $black);
$this->centerText($image, 10, 120, "Phone No(s): 08027449739, 08077275777", $black);
$this->centerText($image, 10, 140, "mailto:info@acecollege.com.ng https://www.acecollege.com.ng", $black);
// Receipt Title
$titleY = 180;
$receiptNo = $data['receipt_no'] ?? 'N/A';
$this->centerText($image, 12, $titleY, "SCHOOL FEES RECEIPT - REFERENCE NO: $receiptNo", $black);
// Session Info
$session = $data['allocations'][0]['academic_session'] ?? '----';
$term = $data['allocations'][0]['term_of_session'] ?? '-';
$nextSession = is_numeric($session) ? ($session + 1) : '';
$levelPart = isset($data['level_name']) ? ' - ' . strtoupper($data['level_name']) : '';
$sessionStr = "$session/$nextSession ACADEMIC SESSION, TERM $term" . $levelPart;
$this->centerText($image, 10, $titleY + 25, $sessionStr, $grey);
// Student Box
$boxY = 230;
imagerectangle($image, 40, $boxY, $width - 40, $boxY + 40, $grey);
// We expect student_name to be passed in $data. If not, fallback.
$studentName = isset($data['student_name']) ? strtoupper($data['student_name']) : (isset($data['student_code']) ? "STUDENT CODE: " . $data['student_code'] : 'UNKNOWN STUDENT');
$this->centerText($image, 14, $boxY + 26, $studentName, $grey);
// Table Header
$tableY = 300;
// 6 Columns: Description, Term/Session, Amount Billed, Amount Paid, Paid To Date, To Balance
// Width 900. Margins 40. Active 820.
// Approx X: 40, 320, 440, 560, 680, 800 (Right Boundary for last col is width-40 = 860)
// Let's adjust spacing.
$cols = [40, 300, 440, 550, 660, 780];
imageline($image, 40, $tableY, $width - 40, $tableY, $black);
imageline($image, 40, $tableY + 30, $width - 40, $tableY + 30, $black);
if ($this->fontPath) {
imagettftext($image, 8, 0, $cols[0], $tableY + 20, $black, $this->fontPath, "FEE DESCRIPTION");
imagettftext($image, 8, 0, $cols[1], $tableY + 20, $black, $this->fontPath, "TERM/SESSION");
imagettftext($image, 8, 0, $cols[2], $tableY + 20, $black, $this->fontPath, "AMOUNT BILLED");
imagettftext($image, 8, 0, $cols[3], $tableY + 20, $black, $this->fontPath, "AMOUNT PAID");
imagettftext($image, 8, 0, $cols[4], $tableY + 20, $black, $this->fontPath, "PAID TO DATE");
imagettftext($image, 8, 0, $cols[5], $tableY + 20, $black, $this->fontPath, "TO BALANCE");
} else {
// Fallback font
imagestring($image, 3, $cols[0], $tableY + 5, "FEE DESCRIPTION", $black);
imagestring($image, 3, $cols[1], $tableY + 5, "TERM/SESSION", $black);
imagestring($image, 3, $cols[2], $tableY + 5, "BILLED", $black);
imagestring($image, 3, $cols[3], $tableY + 5, "PAID", $black);
imagestring($image, 3, $cols[4], $tableY + 5, "PAID TO DATE", $black);
imagestring($image, 3, $cols[5], $tableY + 5, "TO BALANCE", $black);
}
// Rows
$currY = $tableY + 30;
$totalBilled = 0;
$totalPaidToDate = 0;
$totalBalance = 0;
// Total Paid is passed from outside
$totalAmountPaid = 0;
if (isset($data['allocations']) && is_array($data['allocations'])) {
foreach ($data['allocations'] as $alloc) {
$desc = $alloc['description'];
$session = $alloc['academic_session'] ?? '';
$term = $alloc['term_of_session'] ?? '';
// Format: Term/Session e.g. "1/2025" or "1st/2025"
// Assuming simpler "1/2025" format for space
$termSessionStr = "$term/$session";
$billedVal = $alloc['amount_billed'] ?? 0;
$paidVal = $alloc['amount'] ?? 0;
$paidToDateVal = $alloc['total_paid_to_date'] ?? 0;
$balanceVal = $alloc['balance'] ?? 0;
$totalBilled += $billedVal;
$totalAmountPaid += $paidVal;
$totalPaidToDate += $paidToDateVal;
$totalBalance += $balanceVal;
$billed = number_format($billedVal, 2);
$paid = number_format($paidVal, 2);
$paidToDate = number_format($paidToDateVal, 2);
$balance = number_format($balanceVal, 2);
$currY += 30;
if ($this->fontPath) {
imagettftext($image, 8, 0, $cols[0], $currY, $black, $this->fontPath, substr($desc, 0, 35));
imagettftext($image, 8, 0, $cols[1], $currY, $black, $this->fontPath, $termSessionStr); // Term/Session
} else {
imagestring($image, 3, $cols[0], $currY - 10, substr($desc, 0, 35), $black);
imagestring($image, 3, $cols[1], $currY - 10, $termSessionStr, $black);
}
// Right Alignment using approx right boundaries
// Col 2 (Billed): starts 440. Right align around 530?
// Col 3 (Paid): starts 550.
// Col 4 (PTD): starts 660.
// Col 5 (Bal): starts 780.
$rbBilled = 530;
$rbPaid = 640;
$rbPTD = 760;
$rbBal = $width - 40;
$this->rightAlignText($image, 8, $rbBilled, $currY, $billed, $black);
$this->rightAlignText($image, 8, $rbPaid, $currY, $paid, $black);
$this->rightAlignText($image, 8, $rbPTD, $currY, $paidToDate, $black);
$this->rightAlignText($image, 8, $rbBal, $currY, $balance, $black);
}
}
// Total Row
$currY += 20;
imageline($image, 40, $currY, $width - 40, $currY, $black);
$currY += 26;
$totalPaidStr = number_format($data['total_paid'] ?? 0, 2);
$totalBilledStr = number_format($totalBilled, 2);
$totalPaidToDateStr = number_format($totalPaidToDate, 2);
$totalBalanceStr = number_format($totalBalance, 2);
// Columns for totals
$rbBilled = 530;
$rbPaid = 640;
$rbPTD = 760;
$rbBal = $width - 40;
$this->rightAlignText($image, 9, $rbBilled, $currY, $totalBilledStr, $grey);
$this->rightAlignText($image, 9, $rbPaid, $currY, $totalPaidStr, $grey);
$this->rightAlignText($image, 9, $rbPTD, $currY, $totalPaidToDateStr, $grey);
$this->rightAlignText($image, 9, $rbBal, $currY, $totalBalanceStr, $grey);
imageline($image, 40, $currY + 14, $width - 40, $currY + 14, $black);
// Footer
$footerY = $currY + 50;
if ($this->fontPath) {
imagettftext($image, 9, 0, 40, $footerY, $black, $this->fontPath, "RECEIPT DULY VERIFIED BY");
imagettftext($image, 9, 0, 40, $footerY + 20, $black, $this->fontPath, "BURSAR'S OFFICE");
} else {
imagestring($image, 3, 40, $footerY - 10, "RECEIPT DULY VERIFIED BY", $black);
imagestring($image, 3, 40, $footerY + 10, "BURSAR'S OFFICE", $black);
}
$receivedY = $footerY;
$this->rightAlignText($image, 9, $width - 40, $receivedY, "RECEIVED IN", $black);
$this->rightAlignText($image, 9, $width - 40, $receivedY + 15, "BANK", $black);
$this->rightAlignText($image, 9, $width - 40, $receivedY + 40, "RECEIVED ON", $black);
$dateStr = isset($data['payment_date']) ? strtoupper(date("F d, Y", strtotime($data['payment_date']))) : date("F d, Y");
$this->rightAlignText($image, 9, $width - 40, $receivedY + 55, $dateStr, $black);
ob_start();
imagepng($image);
$content = ob_get_clean();
imagedestroy($image);
return $content;
}
public function generateBase64($data)
{
$content = $this->generate($data);
return base64_encode($content);
}
private function centerText($image, $size, $y, $text, $color)
{
if ($this->fontPath) {
try {
$box = imagettfbbox($size, 0, $this->fontPath, $text);
$textW = $box[2] - $box[0];
$x = (int) ((imagesx($image) - $textW) / 2);
imagettftext($image, $size, 0, $x, (int) $y, $color, $this->fontPath, $text);
return;
} catch (Exception $e) {
// Fallthrough to built-in font
}
}
// Fallback or if fontPath is missing
$gdSize = (int) min(5, max(1, floor($size / 3)));
$charWidth = imagefontwidth($gdSize);
$textW = strlen($text) * $charWidth;
$x = (int) ((imagesx($image) - $textW) / 2);
imagestring($image, $gdSize, $x, (int) $y, $text, $color);
}
private function rightAlignText($image, $size, $rightX, $y, $text, $color)
{
if ($this->fontPath) {
try {
$box = imagettfbbox($size, 0, $this->fontPath, $text);
$textW = $box[2] - $box[0];
$x = (int) ($rightX - $textW);
imagettftext($image, $size, 0, $x, (int) $y, $color, $this->fontPath, $text);
return;
} catch (Exception $e) {
// Fallthrough
}
}
$gdSize = (int) min(5, max(1, floor($size / 3)));
$charWidth = imagefontwidth($gdSize);
$x = (int) ($rightX - (strlen($text) * $charWidth));
imagestring($image, $gdSize, $x, (int) $y, $text, $color);
}
}
|