<?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/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"];

$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 trandt") 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();

$result2 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE vouchno='$vouchno'") or die(mysqli_error($conn));
$get2 = mysqli_fetch_array($result2);

$gettran2 = mysqli_query($conn, "SELECT chalno FROM $trandb WHERE vouchno = $vouchno GROUP BY chalno") or die(mysqli_error($conn));
while ($tran2 = mysqli_fetch_array($gettran2)) {

    $getloop = mysqli_query($conn, "SELECT a.*,  DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt, b.frmtime, b.totime
FROM `chalmain` a 
LEFT JOIN `chaltran` b ON b.vouchno = a.vouchno 
WHERE a.vouchno = '$tran2[chalno]'") or die(mysqli_error($conn));

    while ($fetloop = mysqli_fetch_array($getloop)) {
        $pdf->AddPage();

        $getparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$fetloop[party]'") or die(mysqli_error($conn));
        $lparty = mysqli_fetch_array($getparty);

        $getanch = mysqli_query($conn, "SELECT `name` FROM `port` WHERE `code`= '$fetloop[anchor]'") or die(mysqli_error($conn));
        $fanch = mysqli_fetch_array($getanch);


        $ht = 6;
        $ln = 6;
        $ln = 8;

        $pdf->Ln(30);

        $imageFile = 'images/fls1_new.jpg';
        $pdf->Image($imageFile, 4, 35, 33, 22);

        $pdf->SetFont('Times', 'B', 25);
        $pdf->Cell(190, $ht, "$getcmpname[compname]", 0, 0, 'C');
        $pdf->Ln();

        $pdf->SetFont('Times', 'B', 12);
        $pdf->Cell(190, $ht, "LAUNCH, TUG, BARGE OWNER & OPERATOR", 0, 0, 'C');
        $pdf->Ln();

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

        $pdf->SetFont('Times', 'B', 10);
        $pdf->Cell(190, $ht, "Email : $getcmpname[email]" . " Office : $getcmpname[htel]" . " Phone : $getcmpname[hmob]", 0, 0, 'C');
        $pdf->Ln();

        $pdf->SetFont('Arial', '', 10);
        $pdf->Cell(10, $ln, "No : ", 0, 0, 'L');
        $pdf->SetFont('Arial', 'B', 12);
        $pdf->Cell(15, $ln, "$fetloop[manualno]", "B", 0, 'L');
        $pdf->Cell(15, $ln, "", 0, 0, 'L');
        $pdf->SetFont('Arial', '', 10);
        $pdf->Cell(125, $ln, "Date : ", 0, 0, 'R');
        $pdf->SetFont('Arial', 'B', 12);
        $pdf->Cell(25, $ln, "$fetloop[mydt]", 'B', 0, 'R');
        $pdf->Ln();

        $pdf->SetFont('Arial', '', 10);
        $pdf->Cell(42, $ln, "Service Rendered to M/s : ", 0, 0, 'L');
        $pdf->SetFont('Arial', 'B', 11);
        $pdf->Cell(148, $ln, "$lparty[name]", 'B', 0, 'L');
        $pdf->Ln();

        $pdf->SetFont('Arial', '', 10);
        $pdf->Cell(18, $ln, "Order By. ", 0, 0, 'L');
        $pdf->SetFont('Arial', 'B', 12);
        $pdf->Cell(172, $ln, "$fetloop[ordby]", 'B', 0, 'L');
        $pdf->Ln();

        $pdf->SetFont('Arial', '', 11);
        $pdf->Cell(18, $ln, "Vessel", 0, 0, 'L');
        $pdf->SetFont('Arial', 'B', 12);
        $pdf->Cell(100, $ln, "$fetloop[vessel]", 'B', 0, 'L');
        $pdf->SetFont('Arial', '', 10);
        $pdf->Cell(12, $ln, "Anch. ", 0, 0, 'L');
        $pdf->SetFont('Arial', 'B', 12);
        $pdf->Cell(60, $ln, "$fanch[name]", 'B', 0, 'L');
        $pdf->Ln($ln);


        $pdf->SetFont('Arial', '', 10);
        $pdf->Cell(18, $ln, "Purpose", 0, 0, 'L');
        $pdf->SetFont('Arial', 'B', 12);
        $pdf->Cell(172, $ln, "$fetloop[purpose] ", 'B', 0, 'L');
        $pdf->Ln($ln);

        $pdf->SetFont('Arial', 'B', 12);
        $pdf->Cell(190, $ln, "$fetloop[purpose2] ", 'B', 0, 'L');
        $pdf->Ln($ln);
        $pdf->SetFont('Arial', 'B', 12);
        $pdf->Cell(190, $ln, "$fetloop[purpose3] ", 'B', 0, 'L');
        $pdf->Ln($ln);
        $pdf->SetFont('Arial', 'B', 12);
        $pdf->Cell(190, $ln, "$fetloop[purpose4]", 'B', 0, 'L');
        $pdf->Ln($ln);

        $pdf->Cell(190, 4, "", 'B', 0, 'L');
        $pdf->Ln();

        $pdf->SetFont('Arial', '', 10);
        $pdf->Cell(7, $ln, "Sr.", 1, 0, 'L');
        $pdf->Cell(40, $ln, "Launch", 1, 0, 'C');
        $pdf->Cell(14, $ln, "Type", 1, 0, 'C');
        $pdf->Cell(13, $ln, "From", 1, 0, 'C');
        $pdf->Cell(13, $ln, "To", 1, 0, 'C');
        $pdf->Cell(14, $ln, "Hours", 1, 0, 'C');
        $pdf->Cell(18, $ln, "Rs. Per Hr", 1, 0, 'R');
        $pdf->Cell(20, $ln, "Amount", 1, 0, 'R');
        $pdf->Cell(22, $ln, "Crew O.T.", 1, 0, 'R');
        $pdf->Cell(29, $ln, "Total", 1, 0, 'R');
        $pdf->ln();

        $ctr = 0;
        $ctab = 0;
        $amt = 0;
        $getlooptran = mysqli_query($conn, "SELECT a.*, b.name as launchname FROM `chaltran` a LEFT JOIN `launch` b ON a.launch=b.code WHERE a.vouchno='$fetloop[vouchno]' order by a.srno") or die(mysqli_error($conn));
        while ($fetlooptran = mysqli_fetch_array($getlooptran)) {
            if ($fetlooptran['type'] == 'H') {
                $looptype = "Hours";
            } else {
                $looptype = "Lumpsum";
            }
            $hrs = $fetlooptran["nohrs"] .  "." . $fetlooptran["nomin"];
            $amt =  $hrs * $fetlooptran["rate"];
            $ctr++;
            $ctab = $ctab + 1;
            $pdf->SetFont('Arial', 'B', 10);
            $pdf->Cell(7, $ln, "$ctr", 1, 0, 'L');
            $pdf->Cell(40, $ln, "$fetlooptran[launchname]", 1, 0, 'C');
            $pdf->SetFont('Arial', 'B', 8);
            $pdf->Cell(14, $ln, "$looptype", 1, 0, 'C');
            $pdf->SetFont('Arial', 'B', 10);
            // if ($fetlooptran['frmtime'] > 0) {
            $pdf->Cell(13, $ln, "$fetlooptran[frmtime]", 1, 0, 'R');
            // } else {
            // $pdf->Cell(13, $ln, "", 1, 0, 'R');
            // }
            // if ($fetlooptran['totime'] > 0) {
            $pdf->Cell(13, $ln, "$fetlooptran[totime]", 1, 0, 'R');
            // } else {
            // $pdf->Cell(13, $ln, "", 1, 0, 'R');
            // }
            if ($fetlooptran['nohrs'] > 0) {
                $pdf->Cell(14, $ln, "$fetlooptran[nohrs] . $fetlooptran[nomin]", 1, 0, 'R');
            } else {
                $pdf->Cell(14, $ln, "", 1, 0, 'R');
            }
            if ($fetlooptran['rate'] > 0) {
                $pdf->Cell(18, $ln, "$fetlooptran[rate]", 1, 0, 'R');
            } else {
                $pdf->Cell(18, $ln, "", 1, 0, 'R');
            }
            if ($amt > 0) {
                $pdf->Cell(20, $ln, "$fetlooptran[calcamt]", 1, 0, 'R');
            } else {
                $pdf->Cell(20, $ln, "", 1, 0, 'R');
            }
            if ($fetlooptran['otamt'] > 0) {
                $pdf->Cell(22, $ln, "$fetlooptran[otamt]", 1, 0, 'R');
            } else {
                $pdf->Cell(22, $ln, "", 1, 0, 'R');
            }
            if ($fetlooptran['amount'] > 0) {
                $pdf->Cell(29, $ln, "$fetlooptran[amount]", 1, 0, 'R');
            } else {
                $pdf->Cell(29, $ln, "", 1, 0, 'R');
            }
            $pdf->ln();

            $i = 0;
            $totqty = $totqty + $showfetlooptran['qty'];
        }

        $pdf->Cell(161, $ln, "Total Rs", 1, 0, 'R');
        $pdf->Cell(29, $ln, "$fetloop[amount]", 1, 0, 'R');
        $pdf->ln(9);

        $pdf->Cell(50, 4, "E.& O.E", 0, 0, 'L');
        $pdf->Cell(140, 4, "For $getcmpname[compname]", 0, 0, 'R');
        $pdf->ln(10);
        $image   = "images/sign.jpg";
        $pdf->Cell(140, 4, "", 0, 0, 'R');
        if (file_exists($image)) {
            $pdf->Cell(40, 10, '', '', 0, 'C'); // Create a cell with fill color
            $pdf->Image($image, $pdf->GetX() - 30, $pdf->GetY() - 6, 41, 23); // Adjust image position inside the cell
        }
        $pdf->ln();
        $pdf->Cell(150, $ln, "", '', 0, 'R');
        $pdf->Cell(40, $ln, "", 'B', 0, 'R');

        $y_aqui = $pdf->Gety();
        $y_aqui2 = $pdf->Gety();
    }
}
ob_end_clean();

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