<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$formname = 'CHALLAN / BILL';
$maindb = 'invmain';
$trandb = 'invtran';
$file = 'invoice_pdf.php';
$source = "";
$zcomp = $_SESSION["comp_" . $dbname];

$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];
mysqli_select_db($conn, $zdb);

$mht = 'height:20px;';
$admin = $_SESSION["admin_" . $dbname];
$vouchno = $_REQUEST["vouchno"];

$result = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE vouchno='$vouchno'") or die(mysqli_error($conn));
$get = mysqli_fetch_array($result);
$getanch = mysqli_query($conn, "SELECT `name` FROM `port` WHERE `code`= '$get[anchor]'") or die(mysqli_error($conn));
$fanch = mysqli_fetch_array($getanch);

$getlaunch = mysqli_query($conn, "SELECT `name` FROM `launch` WHERE `code`= '$get[launch]'") or die(mysqli_error($conn));
$flaunch = mysqli_fetch_array($getlaunch);

$getvessel = mysqli_query($conn, "SELECT `name` FROM `vessel` WHERE `code`= '$get[vessel]'") or die(mysqli_error($conn));
$fvessel = mysqli_fetch_array($getvessel);

$cmpname = mysqli_query($conn, "SELECT * From $zroot.`company` where code ='$zcomp'") or die(mysqli_error($conn));
$getcmpname = mysqli_fetch_array($cmpname);

$gcust = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$get[party]'") or die(mysqli_error($conn));
$cust = mysqli_fetch_array($gcust);

require("../library_all/fpdf/fpdf.php");

class pdf extends fpdf
{

    function Footer()
    {
        $this->SetY(-15);
        $this->SetFont('Arial', 'I', 8);
    }
}

$pdf = new pdf('P', 'mm', 'A4');
$para = $_REQUEST["para"];

if ($para == 'img'){
    
$pdf->AddPage();
$pdf->setTitle("$getcmpname[compname]");
$pdf->SetFont('Arial', 'B', 10);

$bfont = 17; $ht = 6; $h = 8; $ln = 8; $r = 5; // HEIGHT ADJUSTMENT HERE 
$imageFile = 'images/fls1.jpg';
$pdf->Ln(5);
// $pdf->Image($imageFile, 25, 5, 25, 15);
$pdf->SetTextColor(12, 77, 52);
// $pdf->SetFillColor(12, 77, 52);
// $pdf->SetFont('Helvetica', 'B', $bfont);
// $pdf->Cell(190, $ht, "$getcmpname[compname]", 'B', 0, 'C');
$pdf->Ln(8);
$pdf->setFillColor(255, 255, 255);
$pdf->setTextColor(0, 0, 0);

$pdf->SetFont('Arial', 'B', 11);
// $pdf->Cell(190, $ht, "LAUNCHES, TUG's OWNERS & OPERATORS", 'B', 0, 'C');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
// $pdf->Cell(190, $ht, "$getcmpname[hoadd1]" . " $getcmpname[hoadd2]" . " $getcmpname[hoadd3]", 0, 0, 'C');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
// $pdf->Cell(190, $ht, "$getcmpname[hmob]" . " / " . " $getcmpname[htel], " . " Email : $getcmpname[email]", 0, 0, 'C');
$pdf->Ln(35);


$pdf->SetFillColor(159, 166, 133);
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(180, $ht, 'INVOICE', 1, 0, 'C', 1);
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(180, $ht, "OUR GSTIN No. $getcmpname[gstno]", 1, 0, 'C');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(95, $ht, "Invoice No: $get[maninvno]", 1, 0, 'L');
$pdf->Cell(85, $ht, 'To, ', '1', '0', '');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(95, $ht, "Invoice date: $get[mydt]", 1, '0', '');
$pdf->Cell(85, $ht, "Name: $cust[name]", 1, '0', '');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(95, $ht, "Vessel Name: $fvessel[name]", 1, '0', '');
$pdf->Cell(85, $ht, "GSTIN No.:  $cust[gstno]", 1, '0', '');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(95, $ht, "JOB No / PO: $get[jobno]", 1, '0', '');
$pdf->Cell(17, $ht, "Address:", 'T', '0', '');
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(68, $ht, "$cust[add1]", 'R', '0', '');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(95, $ht, "Reverse Charge: NO", '1', '0', '');
$pdf->SetFont('Arial', 'B', 9);
if ($cust['add2'] != '') {
    $pdf->Cell(85, $ht, "$cust[add2], $cust[add3]", 'R', '0', '');
} else {
    $pdf->Cell(85, $ht, "", 'R', '0', '');
}
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(95, $ht, "State Code : $getcmpname[statecode]", '1', '0', '');
$pdf->SetFont('Arial', 'B', 9);
if ($cust['city'] != '') {
    $pdf->Cell(85, $ht, "$cust[city] - $cust[pin]", 'R', '0', '');
} else {
    $pdf->Cell(85, $ht, "", 'R', '0', '');
}
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(95, 6, "State : $getcmpname[state]", '1', '0', '');
$pdf->Cell(85, 6, "Contact : $cust[contpers]", '1', '0', '');
$pdf->Ln();

$pdf->Cell(5, 6, "", 0, '0', '');
$pdf->Cell(95, 6, "", '1', '0', '');
$pdf->Cell(85, 6, "Email: $cust[email]", '1', '0', 'L');
$pdf->Ln();

$pdf->SetFillColor(159, 166, 133);
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(20, 12, 'Sr .No', '1', '0', 'C', 1);
$pdf->Cell(75, 6, 'DESCRIPTION OF SERVICE', '1', '0', 'C', 1);
$pdf->Cell(85, 6, 'AMOUNT', '1', '0', 'C', 1);
$pdf->Ln();

$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->SetX(35);
$pdf->Cell(43, 6, 'Date', '1', '0', 'C', 1);
$pdf->Cell(32, 6, 'No.', '1', '0', 'C', 1);
$pdf->Cell(49, 6, 'Rs', '1', '0', 'R', 1);
$pdf->Cell(36, 6, 'P.', '1', '0', 'L', 1);
$pdf->Ln();

$gettran = mysqli_query($conn, "SELECT chalno, DATE_FORMAT(MIN(chaldate), '%d-%m-%Y') AS trandt, SUM(amount) AS amount
    FROM $trandb WHERE vouchno = $vouchno GROUP BY chalno ORDER BY chalno ASC") or die(mysqli_error($conn));
$ctr = $tamt = 0;
while ($tran = mysqli_fetch_array($gettran)) {
    $amount = $tran['amount'];
    list($integerPart, $decimalPart) = explode('.', number_format($amount, 2, '.', ''));

    $ctr++;
    $pdf->Cell(5, $ht, '', 0, 0, 'C');
    $pdf->Cell(20, 6, "$ctr.", '1', '0', 'C');
    $pdf->Cell(43, 6, "$tran[trandt]", '1', '0', 'C');
    $pdf->Cell(32, 6, "$tran[chalno]", '1', '0', 'C');
    $pdf->Cell(49, 6, "$integerPart", '1', '0', 'R');
    $pdf->Cell(36, 6, "$decimalPart", '1', '0', 'L');
    $pdf->Ln();

    $tamt += $amount;
}

list($intpart, $decPart) = explode('.', $tamt);
if ($intpart == "") {
    $int = "00";
} else {
    $int = $intpart;
}
if ($decPart == "") {
    $dec = "00";
} else {
    $dec = $decPart;
}
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(20, 6, '', '1', '0', 'C');
$pdf->Cell(75, 6, 'TOTAL', '1', '0', 'R');
// $pdf->Cell(32, 6, '', '1', '0', 'C');  
$pdf->Cell(49, 6, "$int", '1', '0', 'R');
$pdf->Cell(36, 6, "$dec", '1', '0', 'L');
$pdf->Ln();

$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(20, 6, '', '1', '0', 'C');
$pdf->Cell(75, 6, 'GST', '1', '0', 'R');
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(85, 6, "Exempted Under SAC CODE 996522", '1', '0', 'C');
$pdf->Ln();

$pdf->SetFillColor(159, 166, 133);
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(50, 6, '', '1', '0', 'C');
$pdf->Cell(45, 6, 'GRAND TOTAL', '1', '0', 'R', 1);

$pdf->Cell(49, 6, "$int", '1', '0', 'R', 1);
$pdf->Cell(36, 6, "$dec", '1', '0', 'L', 1);
$pdf->Ln();

$ht2 = 5;
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(30, $ht2, 'Bank Name ', 'L', '0', 'L');
$pdf->Cell(65, $ht2, ": $getcmpname[bankname]", 'R', '0', 'L');
$pdf->Cell(85, $ht2, "For $getcmpname[compname]", 'LR', '0', '');
$pdf->Ln();

$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(30, $ht2, 'Bank A/c No. ', 'L', '0', 'L');
$pdf->Cell(65, $ht2, ": $getcmpname[acno]", 'R', '0', 'L');
$pdf->Cell(85, $ht2, '', 'LR', '0', '');
$pdf->Ln();

$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(30, $ht2, 'Bank IFSC Code ', 'L', '0', 'L');
$pdf->Cell(65, $ht2, ": $getcmpname[ifccode]", 'R', '0', 'L');
$pdf->Cell(85, $ht2, '', 'LR', '0', '');
$pdf->Ln();

$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(30, $ht2, 'Branch Name ', 'LB', '0', 'L');
$pdf->Cell(65, $ht2, ": $getcmpname[branchname]", 'BR', '0', 'L');
$pdf->Cell(85, $ht2, '', 'BLR', '0', '');
$pdf->Ln();

$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(180, 5, 'Note : 1) THIS IS AN ORIGINAL INVOICE COPY FOR THE SERVICES RENDERED.', 'LR', '0', 'L');
$pdf->Ln();

$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(180, 5, '2) PAYMENT SHOULD BE DONE AGAINST "THIS INVOICE" ONLY .', 'LRB', '0', 'L');
$pdf->Ln();

$getimgloop = mysqli_query($conn, "SELECT a.imgpath FROM chalmain a 
LEFT JOIN chaltran b on a.vouchno=b.vouchno WHERE b.invno = $vouchno")or die(mysqli_error($conn));

while ($fetloop = mysqli_fetch_array($getimgloop)) {
    $img   = "$fetloop[imgpath]";
    if (file_exists($img)) {
        $pdf->AddPage();
        $pdf->Cell(30, 10, '','', 0, 'C'); // Create a cell with fill color
        $pdf->Image($img, $pdf->GetX(), $pdf->GetY(), 130, 160); // Adjust image position inside the cell
    }
}



} else {
    $pdf->AddPage();
$pdf->setTitle("$getcmpname[compname]");
$pdf->SetFont('Arial', 'B', 10);

$bfont = 17; $ht = 6; $h = 8; $ln = 8; $r = 5; // HEIGHT ADJUSTMENT HERE 
$imageFile = 'images/fls1.jpg';
$pdf->Ln(5);
// $pdf->Image($imageFile, 25, 5, 25, 15);
$pdf->SetTextColor(12, 77, 52);
// $pdf->SetFillColor(12, 77, 52);
// $pdf->SetFont('Helvetica', 'B', $bfont);
// $pdf->Cell(190, $ht, "$getcmpname[compname]", 'B', 0, 'C');
$pdf->Ln(8);
$pdf->setFillColor(255, 255, 255);
$pdf->setTextColor(0, 0, 0);

$pdf->SetFont('Arial', 'B', 11);
// $pdf->Cell(190, $ht, "LAUNCHES, TUG's OWNERS & OPERATORS", 'B', 0, 'C');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
// $pdf->Cell(190, $ht, "$getcmpname[hoadd1]" . " $getcmpname[hoadd2]" . " $getcmpname[hoadd3]", 0, 0, 'C');
$pdf->Ln();

$pdf->SetFont('Arial', '', 10);
// $pdf->Cell(190, $ht, "$getcmpname[hmob]" . " / " . " $getcmpname[htel], " . " Email : $getcmpname[email]", 0, 0, 'C');
$pdf->Ln(35);


$pdf->SetFillColor(159, 166, 133);
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(180, $ht, 'INVOICE', 1, 0, 'C', 1);
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(180, $ht, "OUR GSTIN No. $getcmpname[gstno]", 1, 0, 'C');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(95, $ht, "Invoice No: $get[maninvno]", 1, 0, 'L');
$pdf->Cell(85, $ht, 'To, ', '1', '0', '');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(95, $ht, "Invoice date: $get[mydt]", 1, '0', '');
$pdf->Cell(85, $ht, "Name: $cust[name]", 1, '0', '');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(95, $ht, "Vessel Name: $fvessel[name]", 1, '0', '');
$pdf->Cell(85, $ht, "GSTIN No.:  $cust[gstno]", 1, '0', '');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(95, $ht, "JOB No / PO: $get[jobno]", 1, '0', '');
$pdf->Cell(17, $ht, "Address:", 'T', '0', '');
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(68, $ht, "$cust[add1]", 'R', '0', '');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(95, $ht, "Reverse Charge: NO", '1', '0', '');
$pdf->SetFont('Arial', 'B', 9);
if ($cust['add2'] != '') {
    $pdf->Cell(85, $ht, "$cust[add2], $cust[add3]", 'R', '0', '');
} else {
    $pdf->Cell(85, $ht, "", 'R', '0', '');
}
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(95, $ht, "State Code : $getcmpname[statecode]", '1', '0', '');
$pdf->SetFont('Arial', 'B', 9);
if ($cust['city'] != '') {
    $pdf->Cell(85, $ht, "$cust[city] - $cust[pin]", 'R', '0', '');
} else {
    $pdf->Cell(85, $ht, "", 'R', '0', '');
}
$pdf->Ln();

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(95, 6, "State : $getcmpname[state]", '1', '0', '');
$pdf->Cell(85, 6, "Contact : $cust[contpers]", '1', '0', '');
$pdf->Ln();

$pdf->Cell(5, 6, "", 0, '0', '');
$pdf->Cell(95, 6, "", '1', '0', '');
$pdf->Cell(85, 6, "Email: $cust[email]", '1', '0', 'L');
$pdf->Ln();

$pdf->SetFillColor(159, 166, 133);
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(20, 12, 'Sr .No', '1', '0', 'C', 1);
$pdf->Cell(75, 6, 'DESCRIPTION OF SERVICE', '1', '0', 'C', 1);
$pdf->Cell(85, 6, 'AMOUNT', '1', '0', 'C', 1);
$pdf->Ln();

$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->SetX(35);
$pdf->Cell(43, 6, 'Date', '1', '0', 'C', 1);
$pdf->Cell(32, 6, 'No.', '1', '0', 'C', 1);
$pdf->Cell(49, 6, 'Rs', '1', '0', 'R', 1);
$pdf->Cell(36, 6, 'P.', '1', '0', 'L', 1);
$pdf->Ln();

$gettran = mysqli_query($conn, "SELECT chalno,chaldate as sortdate, DATE_FORMAT(MIN(chaldate), '%d-%m-%Y') as trandt, SUM(amount) as amount FROM $trandb WHERE vouchno = $vouchno GROUP BY chalno ORDER BY sortdate,chalno ASC") or die(mysqli_error($conn));
$ctr = $tamt = 0;
while ($tran = mysqli_fetch_array($gettran)) {
    $amount = $tran['amount'];
    list($integerPart, $decimalPart) = explode('.', number_format($amount, 2, '.', ''));

    $ctr++;
    $pdf->Cell(5, $ht, '', 0, 0, 'C');
    $pdf->Cell(20, 6, "$ctr.", '1', '0', 'C');
    $pdf->Cell(43, 6, "$tran[trandt]", '1', '0', 'C');
    $pdf->Cell(32, 6, "$tran[chalno]", '1', '0', 'C');
    $pdf->Cell(49, 6, "$integerPart", '1', '0', 'R');
    $pdf->Cell(36, 6, "$decimalPart", '1', '0', 'L');
    $pdf->Ln();

    $tamt += $amount;
}

list($intpart, $decPart) = explode('.', $tamt);
if ($intpart == "") {
    $int = "00";
} else {
    $int = $intpart;
}
if ($decPart == "") {
    $dec = "00";
} else {
    $dec = $decPart;
}
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(20, 6, '', '1', '0', 'C');
$pdf->Cell(75, 6, 'TOTAL', '1', '0', 'R');
// $pdf->Cell(32, 6, '', '1', '0', 'C');  
$pdf->Cell(49, 6, "$int", '1', '0', 'R');
$pdf->Cell(36, 6, "$dec", '1', '0', 'L');
$pdf->Ln();

$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(20, 6, '', '1', '0', 'C');
$pdf->Cell(75, 6, 'GST', '1', '0', 'R');
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(85, 6, "Exempted Under SAC CODE 996522", '1', '0', 'C');
$pdf->Ln();

$pdf->SetFillColor(159, 166, 133);
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(50, 6, '', '1', '0', 'C');
$pdf->Cell(45, 6, 'GRAND TOTAL', '1', '0', 'R', 1);

$pdf->Cell(49, 6, "$int", '1', '0', 'R', 1);
$pdf->Cell(36, 6, "$dec", '1', '0', 'L', 1);
$pdf->Ln();

$ht2 = 5;
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(30, $ht2, 'Bank Name ', 'L', '0', 'L');
$pdf->Cell(65, $ht2, ": $getcmpname[bankname]", 'R', '0', 'L');
$pdf->Cell(85, $ht2, "For $getcmpname[compname]", 'LR', '0', 'C');
$pdf->Ln();

$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(30, $ht2, 'Bank A/c No. ', 'L', '0', 'L');
$pdf->Cell(65, $ht2, ": $getcmpname[acno]", 'R', '0', 'L');
$pdf->Cell(85, $ht2, '', 'LR', '0', '');
$pdf->Ln();

$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(30, $ht2, 'Bank IFSC Code ', 'L', '0', 'L');
$pdf->Cell(65, $ht2, ": $getcmpname[ifccode]", 'R', '0', 'L');
$pdf->Cell(85, $ht2, '', 'LR', '0', '');
$pdf->Ln();

$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(30, $ht2, 'Branch Name ', 'LB', '0', 'L');
$pdf->Cell(65, $ht2, ": $getcmpname[branchname]", 'BR', '0', 'L');
$pdf->Cell(85, $ht2, '', 'BLR', '0', '');
$pdf->Ln();

$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(180, 5, 'Note : 1) THIS IS AN ORIGINAL INVOICE COPY FOR THE SERVICES RENDERED.', 'LR', '0', 'L');
$pdf->Ln();

$pdf->Cell(5, $ht, '', 0, 0, 'C');
$pdf->Cell(180, 5, '2) PAYMENT SHOULD BE DONE AGAINST "THIS INVOICE" ONLY .', 'LRB', '0', 'L');
$pdf->Ln();
}

$pdf->Output($get['maninvno'] . " - " . $fvessel['name'] . ".pdf", "I");
