<?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);
    }
}

//browse('udar');

$pdf = new pdf('P', 'mm', 'A4');

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

$bfont = 16; $ht = 4; $h = 8; $ln = 8; $r = 5; $t = 10;// HEIGHT ADJUSTMENT HERE 
$imageFile = 'images/fls1.jpg';
$pdf->Ln(5);
$pdf->Image($imageFile, 35, 6, 25, 15);
$pdf->SetFont('Helvetica', 'B', $bfont);
$pdf->Cell(190, 6, "$getcmpname[compname]",'B', 0, 'C');
$pdf->Ln(6);

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

$pdf->SetFont('Arial', '', $h);
$pdf->Cell(190, $ht, "Operating Office : $getcmpname[opadd1]" . " $getcmpname[opadd2]" . " $getcmpname[opadd3]. Phone : $getcmpname[opmob] / $getcmpname[optel]", 0, 0, 'C');
$pdf->Ln();

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

$pdf->SetFont('Arial', '', $h);
$pdf->Cell(190, $ht, "Email : $getcmpname[email]", 0, 0, 'C');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', $h);
$pdf->Cell(190, $ht, 'INVOICE OF SUPPLY', 1, 0, 'C');
$pdf->Ln();

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

$pdf->SetFont('Arial', 'B', $h);
$pdf->Cell(18, $ht, "Invoice No:", "LBT", 0, 'L');
$pdf->SetFont('Arial', '', $h);
$pdf->Cell(77, $ht, "$get[vouchno]", "RBT", 0, 'L');
$pdf->SetFont('Arial', 'B', $h);
$pdf->Cell(95, $ht, 'To, ', '1', '0', '');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', $h);
$pdf->Cell(20, $ht, "Invoice date:", 'LBT', '0', '');
$pdf->SetFont('Arial', '', $h);
$pdf->Cell(75, $ht, "$get[mydt]", 'RBT', '0', '');
$pdf->SetFont('Arial', 'B', $h);
$pdf->Cell(10, $ht, "Name:", 'LBT', '0', '');
$pdf->SetFont('Arial', '', $h);
$pdf->Cell(85, $ht, "$cust[name]", 'RBT', '0', '');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', $h);
$pdf->Cell(20, $ht, "Vessel Name:", 'LBT', '0', '');
$pdf->SetFont('Arial', '', $h);
$pdf->Cell(75, $ht, "$fvessel[name]", 'RBT', '0', '');
$pdf->SetFont('Arial', 'B', $h);
$pdf->Cell(17, $ht, "GSTIN No.:", 'LBT', '0', '');
$pdf->SetFont('Arial', '', $h);
$pdf->Cell(78, $ht, "$cust[gstno]", 'RBT', '0', '');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', $h);
$pdf->Cell(20, $ht, "JOB No.:", 'LBT', '0', '');
$pdf->SetFont('Arial', '', $h);
$pdf->Cell(75, $ht, "", 'RBT', '0', '');
$pdf->SetFont('Arial', 'B', $h);
$pdf->Cell(15, $ht, "Address:", 'LT', '0', '');
$pdf->SetFont('Arial', '', $h);
$pdf->Cell(80, $ht, "$cust[add1]", 'RT', '0', '');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', $h);
$pdf->Cell(25, $ht, "Reverse Charge :", 'LBT', '0', '');
$pdf->SetFont('Arial', '', $h);
$pdf->Cell(70, $ht, "No", 'RBT', '0', '');
$pdf->SetFont('Arial', '', $h);
$pdf->Cell(95, $ht, "$cust[add2], $cust[add3]", 'R', '0', '');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', $h);
$pdf->Cell(20, $ht, "State Code :", 'LBT', '0', '');
$pdf->SetFont('Arial', '', $h);
$pdf->Cell(75, $ht, "$getcmpname[statecode]", 'RBT', '0', '');
$pdf->SetFont('Arial', '', $h);
$pdf->Cell(95, $ht, "$cust[city] - $cust[pin]", 'RB', '0', '');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', $h);
$pdf->Cell(10, $ht, "State :", 'LBT', '0', '');
$pdf->SetFont('Arial', '', $h);
$pdf->Cell(85, $ht, "$getcmpname[state]", 'RBT', '0', '');
$pdf->SetFont('Arial', 'B', $h);
$pdf->Cell(20, $ht, "Contact No:", 'LBT', '0', '');
$pdf->SetFont('Arial', '', $h);
$pdf->Cell(75, $ht, "$cust[city] / $cust[pin]", 'RB', '0', '');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', $h);
$pdf->Cell(10, $ht, "", 'LBT', '0', '');
$pdf->SetFont('Arial', '', $h);
$pdf->Cell(85, $ht, "", 'RBT', '0', '');
$pdf->SetFont('Arial', 'B', $h);
$pdf->Cell(20, $ht, "Email:", 'LBT', '0', '');
$pdf->SetFont('Arial', '', $h);
$pdf->Cell(75, $ht, "$cust[email]", 'RB', '0', '');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', $h);
$pdf->Cell(20, $ht, 'Sr.No', 'RTL', '0', 'C');
$pdf->Cell(75, $ht, 'Description of service', '1', '0', 'C');
$pdf->Cell(95, $ht, 'AMOUNT', '1', '0', 'C');
$pdf->Ln();

$pdf->Cell(20, $ht, '', 'RBL', '0', 'C');
$pdf->Cell(37, $ht, 'Date', '1', '0', 'C');
$pdf->Cell(38, $ht, 'No.', '1', '0', 'C');
$pdf->Cell(49, $ht, 'Rs', '1', '0', 'R');
$pdf->Cell(46, $ht, 'P.', '1', '0', 'L');
$pdf->Ln();

$gettran = mysqli_query($conn, "SELECT *, DATE_FORMAT(`chaldate`, '%d-%m-%Y') as trandt FROM $trandb WHERE vouchno=$vouchno") or die(mysqli_error($conn));
$ctr = $tamt = 0;
$pdf->SetFont('Arial', '', $h);
while ($tran = mysqli_fetch_array($gettran)) {
    $amount = $tran['amount'];
    list($integerPart, $decimalPart) = explode('.', $amount);
    
    $ctr++;
    $pdf->Cell(20, $ht, "$ctr.", '1', '0', 'C');
    $pdf->Cell(37, $ht, "$tran[trandt]", '1', '0', 'C');
    $pdf->Cell(38, $ht, "$tran[chalno]", '1', '0', 'C');
    $pdf->Cell(49, $ht, "$integerPart", '1', '0', 'R');
    $pdf->Cell(46, $ht, "$decimalPart", '1', '0', 'L');
    $pdf->Ln();

    $tamt += $tran['amount'];
}

list($intpart, $decPart) = explode('.', $tamt);

$pdf->SetFont('Arial', 'B', $h);
$pdf->Cell(20, $ht, '', '1', '0', 'C');
$pdf->Cell(75, $ht, 'TOTAL', '1', '0', 'R');
$pdf->Cell(49, $ht, "$intpart", '1', '0', 'R');
$pdf->Cell(46, $ht, "$decPart", '1', '0', 'L');
$pdf->Ln();

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

$pdf->SetFont('Arial', 'B', $h);
$pdf->Cell(95, $ht, 'GRAND TOTAL', '1', '0', 'R');
$pdf->SetFont('Arial', 'B', $h);

$pdf->Cell(49, $ht, "$intpart", '1', '0', 'R');
$pdf->Cell(46, $ht, "$decPart", '1', '0', 'L');
$pdf->Ln();

$pdf->SetFont('Arial', 'B', $h);
$pdf->Cell(95, $ht, 'Bank Details :-', 'TLR', '0', 'L');
$pdf->Cell(95, $ht, "For $getcmpname[compname]", 'TLR', '0', 'R');
$pdf->Ln();

$pdf->Cell(95, $ht, '', 'LR', '0', 'L');
$pdf->Cell(95, $ht, '', 'LR', '0', '');
$pdf->Ln();

$pdf->Cell(25, $ht, 'Bank Name ', 'L', '0', 'L');
$pdf->Cell(70, $ht, ": $getcmpname[bankname]", 'R', '0', 'L');
$pdf->Cell(95, $ht, '', 'LR', '0', '');
$pdf->Ln();

$pdf->Cell(25, $ht, 'Bank A/c No. ', 'L', '0', 'L');
$pdf->Cell(70, $ht, ": $getcmpname[acno]", 'R', '0', 'L');
$pdf->Cell(95, $ht, '', 'LR', '0', '');
$pdf->Ln();

$pdf->Cell(25, $ht, 'Bank IFSC Code ', 'L', '0', 'L');
$pdf->Cell(70, $ht, ": $getcmpname[ifccode]", 'R', '0', 'L');
$pdf->Cell(95, $ht, '', 'LR', '0', '');
$pdf->Ln();

$pdf->Cell(25, $ht, 'Branch Name ', 'LB', '0', 'L');
$pdf->Cell(70, $ht, ": $getcmpname[branchname]", 'BR', '0', 'L');
$pdf->Cell(95, $ht, '', 'BLR', '0', '');
$pdf->Ln();

$pdf->Cell(190, $ht, 'Note:', 'LR', '0', 'L');
$pdf->Ln();
$pdf->Cell(190, $ht, '1) THIS IS AN ORIGINAL INVOICE COPY FOR THE SERVICES RENDERED.', 'LR', '0', 'L');
$pdf->Ln();
$pdf->Cell(190, $ht, '2) PAYMENT SHOULD BE DONE AGAINST "INVOICE OR SUPPLY" ONLY .', 'LRB', '0', 'L');
$pdf->Ln();


$pdf->Output();
