Update: 2026-05-15 04:45:18
This commit is contained in:
@@ -170,8 +170,8 @@ foreach ($invoices as $invIdx => $inv) {
|
||||
$sheet->mergeCells("A{$row}:I{$row}");
|
||||
$sheet->setCellValue("A{$row}", 'مُـصَـادَق — تقرير فاتورة مشتريات');
|
||||
$sheet->getStyle("A{$row}")->applyFromArray([
|
||||
'font' => ['bold' => true, 'size' => 16, 'color' => new Color($headerFont)],
|
||||
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => new Color($headerBg)],
|
||||
'font' => ['bold' => true, 'size' => 16, 'color' => ['argb' => 'FF' . $headerFont]],
|
||||
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['argb' => 'FF' . $headerBg]],
|
||||
'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER],
|
||||
]);
|
||||
$sheet->getRowDimension($row)->setRowHeight(40);
|
||||
@@ -200,13 +200,13 @@ foreach ($invoices as $invIdx => $inv) {
|
||||
$sheet->setCellValue("F{$row}", $meta[3]);
|
||||
|
||||
// Style labels
|
||||
$sheet->getStyle("A{$row}")->applyFromArray([
|
||||
'font' => ['bold' => true, 'size' => 11, 'color' => new Color($subHeaderFont)],
|
||||
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => new Color($subHeaderBg)],
|
||||
$sheet->getStyle("A{$row}:C{$row}")->applyFromArray([
|
||||
'font' => ['bold' => true, 'size' => 11, 'color' => ['argb' => 'FF' . $subHeaderFont]],
|
||||
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['argb' => 'FF' . $subHeaderBg]],
|
||||
]);
|
||||
$sheet->getStyle("E{$row}")->applyFromArray([
|
||||
'font' => ['bold' => true, 'size' => 11, 'color' => new Color($subHeaderFont)],
|
||||
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => new Color($subHeaderBg)],
|
||||
'font' => ['bold' => true, 'size' => 11, 'color' => ['argb' => 'FF' . $subHeaderFont]],
|
||||
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['argb' => 'FF' . $subHeaderBg]],
|
||||
]);
|
||||
$sheet->getRowDimension($row)->setRowHeight(24);
|
||||
$row++;
|
||||
@@ -214,20 +214,21 @@ foreach ($invoices as $invIdx => $inv) {
|
||||
$row++; // Empty spacer row
|
||||
|
||||
// ── LINE ITEMS TABLE HEADER ─────────────────
|
||||
$headers = ['#', 'وصف البند', 'الكمية', 'سعر الوحدة', 'المجموع الجزئي', 'نسبة الضريبة', 'قيمة الضريبة', 'قيمة الخصم', 'الصافي'];
|
||||
$headers = ['#', 'وصف البند', 'الكمية', 'سعر الوحدة', 'المجموع الجزئي', 'نسبة الضريبة', 'قيمة الضريبة', 'الخصم', 'الصافي'];
|
||||
$cols = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'];
|
||||
$headerRow = $row;
|
||||
$itemsStartRow = $row + 1;
|
||||
|
||||
foreach ($headers as $i => $header) {
|
||||
$sheet->setCellValue($cols[$i] . $row, $header);
|
||||
}
|
||||
|
||||
$sheet->getStyle("A{$row}:I{$row}")->applyFromArray([
|
||||
'font' => ['bold' => true, 'size' => 12, 'color' => new Color($headerFont)],
|
||||
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => new Color($headerBg)],
|
||||
'font' => ['bold' => true, 'size' => 12, 'color' => ['argb' => 'FF' . $headerFont]],
|
||||
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['argb' => 'FF' . $headerBg]],
|
||||
'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER],
|
||||
'borders' => [
|
||||
'allBorders' => ['borderStyle' => Border::BORDER_THIN, 'color' => new Color($headerBg)],
|
||||
'allBorders' => ['borderStyle' => Border::BORDER_THIN, 'color' => ['argb' => 'FF' . $headerBg]],
|
||||
],
|
||||
]);
|
||||
$sheet->getRowDimension($row)->setRowHeight(32);
|
||||
@@ -244,56 +245,33 @@ foreach ($invoices as $invIdx => $inv) {
|
||||
$taxRate = is_numeric($line['tax_rate'] ?? null) ? (float)$line['tax_rate'] : 0.16;
|
||||
$discount = is_numeric($line['discount_amount'] ?? null) ? (float)$line['discount_amount'] : 0;
|
||||
|
||||
// A: Line number
|
||||
$sheet->setCellValue("A{$row}", $lineNum);
|
||||
|
||||
// B: Description
|
||||
$sheet->setCellValue("B{$row}", $line['description'] ?? 'بدون وصف');
|
||||
|
||||
// C: Quantity (value)
|
||||
$sheet->setCellValue("C{$row}", $quantity);
|
||||
|
||||
// D: Unit Price (value)
|
||||
$sheet->setCellValue("D{$row}", $unitPrice);
|
||||
|
||||
// E: Subtotal = Quantity × Unit Price (FORMULA)
|
||||
$sheet->setCellValue("E{$row}", "=C{$row}*D{$row}");
|
||||
|
||||
// F: Tax Rate (as percentage)
|
||||
$sheet->setCellValue("F{$row}", $taxRate);
|
||||
$sheet->getStyle("F{$row}")->getNumberFormat()->setFormatCode('0%');
|
||||
|
||||
// G: Tax Amount = Subtotal × Tax Rate (FORMULA)
|
||||
$sheet->setCellValue("G{$row}", "=E{$row}*F{$row}");
|
||||
|
||||
// H: Discount amount (value)
|
||||
$sheet->setCellValue("H{$row}", $discount);
|
||||
|
||||
// I: Net Total = Subtotal + Tax - Discount (FORMULA)
|
||||
$sheet->setCellValue("I{$row}", "=E{$row}+G{$row}-H{$row}");
|
||||
|
||||
// Alternating row colors
|
||||
if ($lineIdx % 2 === 1) {
|
||||
$sheet->getStyle("A{$row}:I{$row}")->applyFromArray([
|
||||
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => new Color($altRowBg)],
|
||||
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['argb' => 'FF' . $altRowBg]],
|
||||
]);
|
||||
}
|
||||
|
||||
// Number formatting for currency columns
|
||||
foreach (['D', 'E', 'G', 'H', 'I'] as $col) {
|
||||
$sheet->getStyle("{$col}{$row}")->getNumberFormat()->setFormatCode('#,##0.000');
|
||||
}
|
||||
$sheet->getStyle("C{$row}")->getNumberFormat()->setFormatCode('#,##0');
|
||||
|
||||
// Center align numbers
|
||||
$sheet->getStyle("A{$row}:I{$row}")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
|
||||
$sheet->getStyle("B{$row}")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
|
||||
|
||||
$sheet->getRowDimension($row)->setRowHeight(26);
|
||||
$row++;
|
||||
}
|
||||
} else {
|
||||
// If no line items, create a single row from invoice totals
|
||||
$sheet->setCellValue("A{$row}", 1);
|
||||
$sheet->setCellValue("B{$row}", 'إجمالي الفاتورة (بدون تفاصيل بنود)');
|
||||
$sheet->setCellValue("C{$row}", 1);
|
||||
@@ -304,7 +282,6 @@ foreach ($invoices as $invIdx => $inv) {
|
||||
$sheet->setCellValue("G{$row}", "=E{$row}*F{$row}");
|
||||
$sheet->setCellValue("H{$row}", (float)($inv['discount_total'] ?? 0));
|
||||
$sheet->setCellValue("I{$row}", "=E{$row}+G{$row}-H{$row}");
|
||||
|
||||
foreach (['D', 'E', 'G', 'H', 'I'] as $col) {
|
||||
$sheet->getStyle("{$col}{$row}")->getNumberFormat()->setFormatCode('#,##0.000');
|
||||
}
|
||||
@@ -316,45 +293,30 @@ foreach ($invoices as $invIdx => $inv) {
|
||||
$lastDataRow = $row - 1;
|
||||
|
||||
// ── DATA AREA BORDERS ──
|
||||
$sheet->getStyle("A{$headerRow}:I{$lastDataRow}")->applyFromArray([
|
||||
$sheet->getStyle("A{$itemsStartRow}:I" . ($row - 1))->applyFromArray([
|
||||
'borders' => [
|
||||
'allBorders' => ['borderStyle' => Border::BORDER_THIN, 'color' => new Color($borderColor)],
|
||||
],
|
||||
'allBorders' => ['borderStyle' => Border::BORDER_THIN, 'color' => ['argb' => 'FF' . $borderColor]],
|
||||
]
|
||||
]);
|
||||
|
||||
// ── TOTALS ROW WITH SUM FORMULAS ────────────
|
||||
$sheet->mergeCells("A{$row}:B{$row}");
|
||||
$sheet->setCellValue("A{$row}", 'المجموع الكلي');
|
||||
|
||||
// C: Sum of quantities
|
||||
$sheet->setCellValue("C{$row}", "=SUM(C{$firstDataRow}:C{$lastDataRow})");
|
||||
|
||||
// D: (empty — unit price sum doesn't make sense)
|
||||
$sheet->setCellValue("D{$row}", '');
|
||||
|
||||
// E: Sum of subtotals
|
||||
$sheet->setCellValue("E{$row}", "=SUM(E{$firstDataRow}:E{$lastDataRow})");
|
||||
|
||||
// F: (empty — average tax rate doesn't belong here)
|
||||
$sheet->setCellValue("F{$row}", '');
|
||||
|
||||
// G: Sum of tax amounts
|
||||
$sheet->setCellValue("G{$row}", "=SUM(G{$firstDataRow}:G{$lastDataRow})");
|
||||
|
||||
// H: Sum of discounts
|
||||
$sheet->setCellValue("H{$row}", "=SUM(H{$firstDataRow}:H{$lastDataRow})");
|
||||
|
||||
// I: Sum of net totals
|
||||
$sheet->setCellValue("I{$row}", "=SUM(I{$firstDataRow}:I{$lastDataRow})");
|
||||
|
||||
// Style totals row
|
||||
$sheet->getStyle("A{$row}:I{$row}")->applyFromArray([
|
||||
'font' => ['bold' => true, 'size' => 13, 'color' => new Color($totalFont)],
|
||||
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => new Color($totalBg)],
|
||||
'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER],
|
||||
$sheet->getStyle("G{$row}:I{$row}")->applyFromArray([
|
||||
'font' => ['bold' => true, 'size' => 13, 'color' => ['argb' => 'FF' . $totalFont]],
|
||||
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['argb' => 'FF' . $totalBg]],
|
||||
'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER],
|
||||
'borders' => [
|
||||
'allBorders' => ['borderStyle' => Border::BORDER_MEDIUM, 'color' => new Color('059669')],
|
||||
],
|
||||
'allBorders' => ['borderStyle' => Border::BORDER_MEDIUM, 'color' => ['argb' => 'FF059669']],
|
||||
]
|
||||
]);
|
||||
$sheet->getStyle("A{$row}")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
|
||||
foreach (['C', 'E', 'G', 'H', 'I'] as $col) {
|
||||
@@ -367,9 +329,9 @@ foreach ($invoices as $invIdx => $inv) {
|
||||
// ── FOOTER ──
|
||||
$sheet->mergeCells("A{$row}:I{$row}");
|
||||
$sheet->setCellValue("A{$row}", 'تم إنشاء هذا التقرير تلقائياً من منصة مُصادَق — ' . date('Y-m-d H:i'));
|
||||
$sheet->getStyle("A{$row}")->applyFromArray([
|
||||
'font' => ['italic' => true, 'size' => 9, 'color' => new Color('8B82B0')],
|
||||
'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER],
|
||||
$sheet->getStyle("A{$row}:I{$row}")->applyFromArray([
|
||||
'font' => ['italic' => true, 'size' => 9, 'color' => ['argb' => 'FF8B82B0']],
|
||||
'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER]
|
||||
]);
|
||||
|
||||
// ── Print settings ──
|
||||
|
||||
Reference in New Issue
Block a user