<?php
require('fpdf/fpdf.php'); // Path ke file FPDF

class PDF extends FPDF
{   
    private $maxWidth = 190;
    private $nama;
    private $alamat;
    private $telepon;
    private $logo;

    public function setHeaderData($nama, $alamat, $telepon, $logo='')
    {
        $this->nama = $nama;
        $this->alamat = $alamat;
        $this->telepon = $telepon;
        $this->logo = $logo;
    }

    function Header()
    {
        // Logo
        $this->Image('/www/wwwroot/bilspro/img/user/'.$this->logo, 10, 8, 22, 22); // Path ke logo Anda, x, y, width
        $this->SetXY(35, 11);
        $this->SetFont('Arial', 'B', 14);
        $this->Cell(0, 6, $this->nama, 0, 1, 'L');
        $this->SetFont('Arial', 'I', 11);
        $this->SetXY(35, 17);
        $this->Cell(0, 6, $this->alamat, 0, 1, 'L');
        $this->SetXY(35, 23);
        $this->Cell(0, 6, 'Phone: '.$this->telepon, 0, 1, 'L');
        $this->Ln(14);

        // Garis Horizontal
        $this->SetLineWidth(0.8); // Ketebalan garis
        $this->Line(10, 33, 200, 33); // x1, y1, x2, y2 (x1, y1 = start; x2, y2 = end)
        $this->SetLineWidth(0.4);
        $this->Line(10, 34, 200, 34);

        $this->SetTextColor(255, 140, 0);
        $this->SetFont('Arial', 'B', 20);
        $this->Cell(0, -56, 'INVOICE', 0, 1, 'R');
        $this->Ln(50);
    }

    function Footer()
    {
        $this->SetY(-15);
        $this->SetFont('Arial', 'I', 8);
        $this->Cell(0, 10, 'Page ' . $this->PageNo(), 0, 0, 'C');
    }

    function CustomerDetails($customerName, $customerAddress, $invoiceNumber, $invoiceDate, $customerNumber, $invoiceLimit)
    {
        $this->SetFont('Arial', 'B', 11);
        $this->Cell(0, 7, 'Bill To:', 0, 1);
        $this->SetFont('Arial', '', 11);
        $this->Cell(0, 7, $customerName, 0, 1);
        $this->MultiCell(0, 7, $customerAddress);
        $this->Cell(0, 7, $customerNumber, 0, 1);
        $this->Ln(10);

        $this->SetFont('Arial', '', 11);
        $this->SetXY(150, 37);
        $this->Cell(0, 6, "No Invoice    : $invoiceNumber", 0, 1);
        $this->SetXY(150, 44);
        $this->Cell(0, 6, "Invoice Date : $invoiceDate", 0, 1);
        $this->SetXY(150, 51);
        $this->Cell(0, 6, "Limit Invoice : $invoiceLimit", 0, 1);
        $this->Ln(15);
    }
    
    
    
    // function InvoiceTable($header, $data)
    // {
    //     // Menentukan lebar kolom
    //     $columnWidths = array(10, 25, 100, 22, 33);
    //     $totalWidth = array_sum($columnWidths);

    //     // Menghitung skala untuk menyesuaikan tabel dengan lebar maksimum
    //     $scalingFactor = $this->maxWidth / $totalWidth;
    //     //Atur Warna
    //     $this->SetFillColor(255, 140, 0);
    //     // Mengatur font untuk header
    //     $this->SetFont('Arial', 'B', 11);
    //     $this->Cell($columnWidths[0] * $scalingFactor, 9, $header[0], 1, 0, 'C', true); // Item
    //     $this->Cell($columnWidths[1] * $scalingFactor, 9, $header[1], 1, 0, 'C', true); // Description
    //     $this->Cell($columnWidths[2] * $scalingFactor, 9, $header[2], 1, 0, 'C', true); // Description
    //     $this->Cell($columnWidths[3] * $scalingFactor, 9, $header[3], 1, 0, 'C', true); // Quantity
    //     $this->Cell($columnWidths[4] * $scalingFactor, 9, $header[4], 1, 0, 'C', true); // Price
    //     $this->Ln();

    //     // Mengatur font untuk data
    //     $this->SetFont('Arial', '', 11);
    //     foreach ($data as $row) {
    //         $this->Cell($columnWidths[0] * $scalingFactor, 9, $row[0], 1, 0, 'C'); // Item
    //         $this->Cell($columnWidths[1] * $scalingFactor, 9, $row[1], 1, 0, 'C'); // Description
    //         $this->Cell($columnWidths[2] * $scalingFactor, 9, $row[2], 1, 0, 'L'); // Description
    //         $this->Cell($columnWidths[3] * $scalingFactor, 9, $row[3], 1, 0, 'C'); // Quantity
    //         $this->Cell($columnWidths[4] * $scalingFactor, 9, $row[4], 1, 0, 'R'); // Price (align right)
    //         $this->Ln();
    //     }
    // }
    
function InvoiceTable($header, $data)
{
    // Menentukan lebar kolom
    $columnWidths = array(10, 25, 100, 22, 33);
    $totalWidth = array_sum($columnWidths);

    // Menghitung skala untuk menyesuaikan tabel dengan lebar maksimum
    $scalingFactor = $this->maxWidth / $totalWidth;

    // Atur warna header
    $this->SetFillColor(255, 140, 0);

    // Mengatur font untuk header
    $this->SetFont('Arial', 'B', 11);
    $this->Cell($columnWidths[0] * $scalingFactor, 9, $header[0], 1, 0, 'C', true); // Item
    $this->Cell($columnWidths[1] * $scalingFactor, 9, $header[1], 1, 0, 'C', true); // Description
    $this->Cell($columnWidths[2] * $scalingFactor, 9, $header[2], 1, 0, 'C', true); // Description
    $this->Cell($columnWidths[3] * $scalingFactor, 9, $header[3], 1, 0, 'C', true); // Quantity
    $this->Cell($columnWidths[4] * $scalingFactor, 9, $header[4], 1, 0, 'C', true); // Price
    $this->Ln();

    // Mengatur font untuk data
    $this->SetFont('Arial', '', 11);
    foreach ($data as $row) {
        $maxHeight = 0;

        // Tentukan tinggi cell maksimal untuk kolom Description dan Quantity
        $nbDescription = $this->NbLines($columnWidths[2] * $scalingFactor, $row[2]); // Baris untuk kolom Description
        $nbQuantity = $this->NbLines($columnWidths[3] * $scalingFactor, $row[3]);    // Baris untuk kolom Quantity

        // Tinggi baris maksimal dihitung dari jumlah baris terbesar
        $maxHeight = 9 * max($nbDescription, $nbQuantity);

        // Cek apakah perlu pindah halaman (pagination)
        if ($this->GetY() + $maxHeight > $this->PageBreakTrigger) {
            $this->AddPage();
            // Cetak header ulang di halaman baru
            $this->SetFillColor(255, 140, 0);
            $this->SetFont('Arial', 'B', 11);
            $this->Cell($columnWidths[0] * $scalingFactor, 9, $header[0], 1, 0, 'C', true); // Item
            $this->Cell($columnWidths[1] * $scalingFactor, 9, $header[1], 1, 0, 'C', true); // Description
            $this->Cell($columnWidths[2] * $scalingFactor, 9, $header[2], 1, 0, 'C', true); // Description
            $this->Cell($columnWidths[3] * $scalingFactor, 9, $header[3], 1, 0, 'C', true); // Quantity
            $this->Cell($columnWidths[4] * $scalingFactor, 9, $header[4], 1, 0, 'C', true); // Price
            $this->Ln();
            $this->SetFont('Arial', '', 11); // Kembali ke font data
        }

        $x = $this->GetX(); // Simpan posisi X awal
        $y = $this->GetY(); // Simpan posisi Y awal

        // Cetak kolom pertama (Item)
        $this->Cell($columnWidths[0] * $scalingFactor, $maxHeight, $row[0], 1, 0, 'C');

        // Cetak kolom kedua (Short Description)
        $this->Cell($columnWidths[1] * $scalingFactor, $maxHeight, $row[1], 1, 0, 'C');

        // Cetak kolom ketiga (Description) menggunakan MultiCell
        $this->SetXY($x + ($columnWidths[0] + $columnWidths[1]) * $scalingFactor, $y);
        $this->MultiCell($columnWidths[2] * $scalingFactor, $maxHeight / max(1, $nbDescription), $row[2], 1, 'L');

        // Kembalikan posisi untuk kolom keempat (Quantity)
        $this->SetXY($x + ($columnWidths[0] + $columnWidths[1] + $columnWidths[2]) * $scalingFactor, $y);
        $this->MultiCell($columnWidths[3] * $scalingFactor, $maxHeight / max(1, $nbQuantity), $row[3], 1, 'C');

        // Kembalikan posisi untuk kolom kelima (Price)
        $this->SetXY($x + ($columnWidths[0] + $columnWidths[1] + $columnWidths[2] + $columnWidths[3]) * $scalingFactor, $y);
        $this->Cell($columnWidths[4] * $scalingFactor, $maxHeight, $row[4], 1, 0, 'R');

        // Pindah ke baris berikutnya
        $this->Ln($maxHeight);
    }
}

// Fungsi tambahan untuk menghitung jumlah baris Multicell
function NbLines($w, $txt)
{
    // Hitung jumlah baris dalam MultiCell
    $cw = &$this->CurrentFont['cw'];
    if ($w == 0) {
        $w = $this->w - $this->rMargin - $this->x;
    }
    $wmax = ($w - 2 * $this->cMargin) * 1000 / $this->FontSize;
    $s = str_replace("\r", '', $txt);
    $nb = strlen($s);
    if ($nb > 0 && $s[$nb - 1] == "\n") {
        $nb--;
    }
    $sep = -1;
    $i = 0;
    $j = 0;
    $l = 0;
    $nl = 1;
    while ($i < $nb) {
        $c = $s[$i];
        if ($c == "\n") {
            $i++;
            $sep = -1;
            $j = $i;
            $l = 0;
            $nl++;
            continue;
        }
        if ($c == ' ') {
            $sep = $i;
        }
        $l += $cw[$c];
        if ($l > $wmax) {
            if ($sep == -1) {
                if ($i == $j) {
                    $i++;
                }
            } else {
                $i = $sep + 1;
            }
            $sep = -1;
            $j = $i;
            $l = 0;
            $nl++;
        } else {
            $i++;
        }
    }
    return $nl;
}



    function Total($subtotal,$lainnya,$totalAmount,$terbilang)
    {
        $this->Ln(1);
        $this->SetFont('Arial', '', 11);
        $this->Cell($this->maxWidth - 33, 9, 'Sub Total', 1, 0, 'R');
        $this->Cell(33, 9, $subtotal, 1, 1, 'R');
        $this->Cell($this->maxWidth - 33, 9, 'Lainnya', 1, 0, 'R');
        $this->Cell(33, 9, $lainnya, 1, 1, 'R');
        $this->SetFont('Arial', 'B', 11);
        $this->Cell($this->maxWidth - 33, 9, 'Total', 1, 0, 'R');
        $this->Cell(33, 9, $totalAmount, 1, 1, 'R');
        $this->Ln(1);
        $this->SetFont('Arial', 'I', 11);
        $this->Cell(0, 9, 'Terbilang : ', 0, 1);
        $this->SetFont('Arial', 'I', 11);
        $this->MultiCell(190, 9, $terbilang, 1, 'L');
    }

    function Keterangan($Keterangan)
    {
        $this->Ln(10);
        $this->SetFont('Arial', '', 11);
        $this->SetFillColor(255, 140, 0);
        $this->MultiCell(100, 6, $Keterangan, 0, 'L', true);
    }

    function Pembuat($Pembuat)
    {
        $this->Ln(-25);
        $this->SetFont('Arial', '', 11);
        $this->Cell(300, 6, 'Mengetahui,', 0, 1, 'C');
        $this->Ln(18);
        $this->SetFont('Arial', 'B', 11);
        $this->Cell(300, 6, '( '.$Pembuat.' )', 0, 1, 'C');
        $this->SetFont('Arial', 'I', 11);
        $this->Cell(300, 6, 'financial department', 0, 1, 'C');
    }
}

?>
