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

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];
$zcomp = $_SESSION["comp_" . $dbname];

$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
$zyear = $myyear;

$vouch = $_GET['vouchno'];

$cond = '';
$lcond = '';
$formname = "Tax Invoice";

$maindb = 'gstmain';
$trandb = 'gsttran';

$getqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as vouchdt   FROM `$maindb` WHERE vouchno='$vouch' ORDER BY `vouchno`")or die(mysqli_error($conn));
$getdata = mysqli_fetch_array($getqry);


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

$getparty = mysqli_query($conn, "SELECT * From accmast WHERE `accode` = '$getdata[party]'")or die(mysqli_error($conn));
$showparty = mysqli_fetch_array($getparty);

$getsalesm = mysqli_query($conn, "SELECT `accode`,`name` From accmast WHERE `accode` = '$getdata[salesman]'")or die(mysqli_error($conn));
$showsalesm = mysqli_fetch_array($getsalesm);

$oms = $getdata['oms'];

require_once("library/TCPDF/tcpdf.php");


$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);

$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

$pdf->AddPage();


$fs = 11;
$ht = 5;
$hln = 8;

$pdf->SetFont('aefurat', 'BU', 10);
$pdf->Cell(63.3, 7, "", 'TL', 0, 'C');
$pdf->Cell(63.3, 7, "Subject To Mumbai Jurisdictiion", 'T', 0, 'C');
$pdf->Cell(63.3, 7, "", 'TR', 0, 'C');
$pdf->Ln(5);

$pdf->SetFont('aefurat', 'B', 16);
$pdf->Cell(190, 8, "$init", 'RL', 0, 'C');
$pdf->Ln(5);

$pdf->SetFont('aefurat', 'B', 9);
$pdf->Cell(190, 7, "$getcmpname[add1] $getcmpname[add2] $getcmpname[add3] $getcmpname[city] - $getcmpname[pin]", 'RL', 0, 'C');
$pdf->Ln(4);

$pdf->SetFont('aefurat', 'B', 9);
$pdf->Cell(190, 7, "Email : $getcmpname[email]  Tel :$getcmpname[tel]  Mobile :$getcmpname[mobile]  Website :$getcmpname[website]", 'RL', 0, 'C');
$pdf->Ln(4);

$pdf->SetFont('aefurat', 'B', 14);
$pdf->Cell(190, 7, "Tax Invoice", 'BRL', 0, 'C');
$pdf->Ln(8);

$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(90, 5, "Bill To Party", 1, 0, 'C');
$pdf->Cell(1, 6, "", 0, 0, 'L');
$pdf->Cell(99, 5, "Ship To Party", 1, 0, 'C');
$pdf->Ln(6);

if ($getdata["nameprint"] != '') {
    $printname = $getdata["nameprint"];
    $printmobile = $getdata["printmobile"];
} else {
    $printname = $showparty['name'];
    $printmobile = $showparty['mobile'];
}
$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(19, 4, "M/s. :", 'LT', 0, 'L');
$pdf->SetFont('aefurat', '', 9);
$pdf->Cell(71, 4, "$printname", 'TR', 0, 'L');
$pdf->Cell(1, 4, "", R, 0, 'L');
$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(19, 4, "M/s. :", 'TL', 0, 'L');
$pdf->SetFont('aefurat', '', 9);
$pdf->Cell(80, 4, "$printname", 'TR', 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(19, 4, "Address :", 'L', 0, 'L');
$pdf->SetFont('aefurat', 'B', 8);
$pdf->Cell(71, 4, "$showparty[add1]", 'R', 0, 'L');
$pdf->Cell(1, 4, "", 'R', 0, 'L');
$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(19, 4, "Address :", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 8);
$pdf->Cell(80, 4, "$showparty[add1]", 'R', 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(19, 4, "", 'L', 0, 'L');
$pdf->SetFont('aefurat', 'B', 8);
$pdf->Cell(71, 4, "$showparty[add2]", 'R', 0, 'L');
$pdf->Cell(1, 4, "", 'R', 0, 'L');
$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(19, 4, "", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 8);
$pdf->Cell(80, 4, "$showparty[add2]", 'R', 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(19, 4, "", 'L', 0, 'L');
$pdf->SetFont('aefurat', 'B', 8);
$pdf->Cell(71, 4, "$showparty[add3] $showparty[city] $showparty[pin]", 'R', 0, 'L');
$pdf->Cell(1, 4, "", 'R', 0, 'L');
$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(19, 4, "", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 8);
$pdf->Cell(80, 4, "$showparty[add3] $showparty[city] $showparty[pin]", 'R', 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(19, 4, "Mobile :", 'L', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(71, 4, "$printmobile", 'R', 0, 'L');
$pdf->Cell(1, 4, "", 'R', 0, 'L');
$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(19, 4, "Mobile :", 'L', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(80, 4, "$printmobile", 'R', 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(19, 4, "GST No. :", 'L', 0, 'L');
$pdf->SetFont('aefurat', '', 8);
$pdf->Cell(71, 4, "$showparty[gstno]", 'R', 0, 'L');
$pdf->Cell(1, 4, "", 'R', 0, 'L');
$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(19, 4, "GST No. :", 'L', 0, 'L');
$pdf->SetFont('aefurat', '', 8);
$pdf->Cell(80, 4, "$showparty[gstno]", 'R', 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(19, 4, "State :", 'L', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(71, 4, "$showparty[state]", 'R', 0, 'L');
$pdf->Cell(1, 4, "", 'R', 0, 'L');
$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(19, 4, "State :", 'L', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(80, 4, "$showparty[state]", 'R', 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('aefurat', 'B', 9);
$pdf->Cell(19, 4, "State Code :", 'LB', 0, 'L');
$pdf->SetFont('aefurat', '', 8);
$pdf->Cell(71, 4, "$showparty[statecode]", 'RB', 0, 'L');
$pdf->Cell(1, 4, "", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 9);
$pdf->Cell(19, 4, "State Code :", 'LB', 0, 'L');
$pdf->SetFont('aefurat', '', 8);
$pdf->Cell(80, 4, "$showparty[statecode]", 'RB', 0, 'L');
$pdf->Ln(5);

$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(19, 5, "Invoice No :", 'LT', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(71, 5, "BN/$zyear/$getdata[vouchno]", 'RT', 0, 'L');
$pdf->Cell(1, 5, "", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(23, 5, "Team Member", 'TL', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(76, 5, "$showsalesm[name]", 'TR', 0, 'L');
$pdf->Ln(5);

if ($getdata['cashamt'] > 0) {
    $paytype = "Cash";
    If ($getdata['ccamt'] > 0) {
        $paytype = "$paytype" . "/CC";
    }

    If ($getdata['credamt'] > 0) {
        $paytype = "$paytype" . "/Credit";
    }
} else {
    if ($getdata['ccamt'] > 0) {
        $paytype = "CC";

        If ($getdata['credamt'] > 0) {
            $paytype = "$paytype" . "/Credit";
        }
    } else {
        if ($getdata['credamt'] > 0) {
            $paytype = "Credit";
        }
    }
}

$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(19, 5, "Date:", 'LB', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(71, 5, "$getdata[vouchdt]", 'RB', 0, 'L');
$pdf->Cell(1, 5, "", 'B', 0, 'L');
$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(19, 5, "Paid By", 'LB', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(80, 5, "$paytype", 'BR', 0, 'L');
$pdf->Ln(6);

$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(5, 5, "Sr.", 'BLT', 0, 'C');
$pdf->Cell(18, 5, "Barcode", 'LBT', 0, 'C');
$pdf->Cell(45, 5, "Item", 'LBT', 0, 'C');
$pdf->Cell(26, 5, "Color", 'BLT', 0, 'C');
$pdf->Cell(18, 5, "HSN Code", 'BLRT', 0, 'C');
$pdf->Cell(16, 5, "GST%", 'BRT', 0, 'R');
$pdf->Cell(17, 5, "Qty.", 'BRT', 0, 'R');
$pdf->Cell(21, 5, "Rate", 'BRT', 0, 'R');
$pdf->Cell(24, 5, "Amount", 'BRT', 0, 'R');
$pdf->Ln(5);

$ctr = 0;
$ctab = 0;
$totqty = 0;
$gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$vouch' order by srno")or die(mysqli_error($conn));
while ($showtran = mysqli_fetch_array($gettran)) {
    $getitem = mysqli_query($conn, "SELECT `name`,`code`,`nameprint`  From `item` WHERE `code` = $showtran[item] ")or die(mysqli_error($conn));
    $showitem = mysqli_fetch_array($getitem);


    $getuint = mysqli_query($conn, "SELECT `name`,`code`  From `color` WHERE `code` = '$showtran[color]' ")or die(mysqli_error($conn));
    $showcolor = mysqli_fetch_array($getuint);
    $gstperc = $showtran['cgstrate'] * 2;
    $gstamt = $showtran['cgstamt'] * 2;

    $nameprint = $showitem["nameprint"];
    $name = $showitem["name"];

    if ($nameprint != '') {
        $itemname = $nameprint;
    } else {
        $itemname = $name;
    }
    if ($oms == '') {
        ?>
         <? $gst=$gstperc ?>
    <?php } else { ?>
        <? $gst=$showtran['igstrate']; ?>
        <?php
    }
    $totqty = $totqty + $showtran['qty'];
    $ctr++;
    $ctab = $ctab + 1;
    $pdf->Cell(5, 4.2, "$showtran[srno]", 'BLT', 0, 'C');
    $pdf->Cell(18, 4.2, "$showtran[item]", 'LBT', 0, 'L');
    $pdf->Cell(45, 4.2, "$itemname", 'LBT', 0, 'L');
    $pdf->Cell(26, 4.2, "$showcolor[name]", 'BLT', 0, 'L');
    $pdf->Cell(18, 4.2, "$showtran[hsncode]", 'BLRT', 0, 'L');
    $pdf->Cell(16, 4.2, "$gst", 'BRT', 0, 'R');
    $pdf->Cell(17, 4.2, "$showtran[qty]", 'BRT', 0, 'R');
    $pdf->Cell(21, 4.2, "$showtran[rate]", 'BRT', 0, 'R');
    $pdf->Cell(24, 4.2, "$showtran[amount]", 'BRT', 0, 'R');
    $pdf->Ln(4.5);
}
$newctr = 21 - $a;
for ($i = 1; $i <= $newctr; $i++) {
    $pdf->Cell(5, 4.2, "", 'BLT', 0, 'C');
    $pdf->Cell(18, 4.2, "", 'LBT', 0, 'C');
    $pdf->Cell(45, 4.2, "", 'LBT', 0, 'C');
    $pdf->Cell(26, 4.2, "", 'BLT', 0, 'C');
    $pdf->Cell(18, 4.2, "", 'BLRT', 0, 'C');
    $pdf->Cell(16, 4.2, "", 'BRT', 0, 'R');
    $pdf->Cell(17, 4.2, "", 'BRT', 0, 'R');
    $pdf->Cell(21, 4.2, "", 'BRT', 0, 'R');
    $pdf->Cell(24, 4.2, "", 'BRT', 0, 'R');
    $pdf->Ln(4.5);
}

$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(122, 4, "Total", 'BL', 0, 'R');
$pdf->Cell(22, 4, "$totqty", 'B', 0, 'R');
$pdf->Cell(46, 4, " $getdata[subtotal]", 'BR', 0, 'R');
$pdf->Ln(4);

$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(135, 5, "Taxable Value", 'BL', 0, 'R');
$pdf->Cell(55, 5, "$getdata[tottaxamt]", 'BR', 0, 'R');
$pdf->Ln(5);

if ($getdata["packing"] > 0){
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(135, 5, "Packing", 'BL', 0, 'R');
$pdf->Cell(55, 5, "$getdata[packing]", 'BR', 0, 'R');
$pdf->Ln(5);
}

$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(40, 4, "Bank Name", 'BLRT', 0, 'L');
$pdf->Cell(79, 4, "$getcmpname[bankname]", 'BLRT', 0, 'L');
$pdf->Cell(1, 4, "", 'LR', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(14, 4, "%", 'BLRT', 0, 'L');
$pdf->Cell(14, 4, "CGST", 'BLRT', 0, 'L');
$pdf->Cell(14, 4, "SGST", 'BLRT', 0, 'L');
$pdf->Cell(14, 4, "IGST", 'BLRT', 0, 'L');
$pdf->Cell(14, 4, "", 'BLRT', 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(40, 4, "A/c Name", 'BLRT', 0, 'L');
$pdf->Cell(79, 4, "$getcmpname[acname]", 'BLRT', 0, 'L');
$pdf->Cell(1, 4, "", 'LR', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(14, 4, "5%", 'LRT', 0, 'L');
$pdf->Cell(14, 4, "$getdata[cgst5]", 'LRT', 0, 'R');
$pdf->Cell(14, 4, "$getdata[sgst5]", 'LRT', 0, 'R');
$pdf->Cell(14, 4, "$getdata[igst5]", 'LTR', 0, 'R');
$pdf->Cell(14, 4, "$getdata[totgst5]", 'LRT', 0, 'R');
$pdf->Ln(4);

$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(40, 4, "Branch", 'BLRT', 0, 'L');
$pdf->Cell(79, 4, "$getcmpname[branchname]", 'BLRT', 0, 'L');
$pdf->Cell(1, 4, "", 'LR', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(14, 4, "12%", 'LRT', 0, 'L');
$pdf->Cell(14, 4, "$getdata[cgst12]", 'LRT', 0, 'R');
$pdf->Cell(14, 4, "$getdata[sgst12]", 'LRT', 0, 'R');
$pdf->Cell(14, 4, "$getdata[igst12]", 'LRT', 0, 'R');
$pdf->Cell(14, 4, "$getdata[totgst12]", 'LRT', 0, 'R');
$pdf->Ln(4);

$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(40, 4, "Account No.", 'BLRT', 0, 'L');
$pdf->Cell(79, 4, "$getcmpname[acno]", 'BLRT', 0, 'L');
$pdf->Cell(1, 4, "", 'LR', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(14, 4, "18%", 'BLRT', 0, 'L');
$pdf->Cell(14, 4, "$getdata[cgst18]", 'LRTB', 0, 'R');
$pdf->Cell(14, 4, "$getdata[sgst18]", 'LRTB', 0, 'R');
$pdf->Cell(14, 4, "$getdata[igst18]", 'LRTB', 0, 'R');
$pdf->Cell(14, 4, "$getdata[totgst18]", 'LRTB', 0, 'R');
$pdf->Ln(4);

$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(40, 4, "IFSC Code", 'BLRT', 0, 'L');
$pdf->Cell(79, 4, "$getcmpname[bankcode]", 'BLRT', 0, 'L');
$pdf->Cell(1, 4, "", 'LR', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(14, 4, "28%", 'BLRT', 0, 'L');
$pdf->Cell(14, 4, "$getdata[cgst28]", 'BLRT', 0, 'R');
$pdf->Cell(14, 4, "$getdata[sgst28]", 'BLRT', 0, 'R');
$pdf->Cell(14, 4, "$getdata[igst28]", 'BLRT', 0, 'R');
$pdf->Cell(14, 4, "$getdata[totgst28]", 'BLRT', 0, 'R');
$pdf->Ln(6);


$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(120, 4, "GST No:27AAKFN9380L1ZH", 'TBLR', 0, 'L');
$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(40, 4, "Total Amount", 'BT', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(30, 4, " $getdata[netamt]", 'BRT', 0, 'R');
$pdf->Ln(4);

//$mwords = round($netamt);
//$words = rsword($mwords);
$amountinwords = rsword(round($getdata['netamt']));
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(33, 4, "Amount in Words:", 'BL', 0, 'L');
$pdf->SetFont('aefurat', '', 10);
$pdf->Cell(157, 4, $amountinwords, 'BR', 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('aefurat', 'B', 15);
$pdf->Cell(190, 20, "For $init", 0, 0, 'R');
$pdf->Ln(5);


$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(30, 6, "Terms & Conditions:", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 9);
$pdf->Cell(70, 6.5, "Goods return without previous coonsent will not be accepted. No liability accepted", 0, 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('aefurat', '', 8);
$pdf->Cell(120, 6.5, "any breakage or shortage in transit which is entirely at your risk . All claims to be settled to Mumbai.", 0, 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('aefurat', '', 8);
$pdf->Cell(120, 6.5, "Interest at 18% per annum will be charged if payment is not made within 8 days. Our responsibility cases", 0, 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('aefurat', '', 8);
$pdf->Cell(120, 6.5, "when good leave our godown.", 0, 0, 'L');
$pdf->Ln(5);

$pdf->SetFont('aefurat', 'B', 10);
$pdf->Cell(190, 20, "AUTHORISED SIGNATORY", 0, 0, 'R');
$pdf->Ln(3);


$pdf->SetFont('aefurat', 'B', 12);
$pdf->Cell(30, 6, "Subject To Mumbai Jurisdiction", 0, 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('aefurat', '', 8);
$pdf->Cell(120, 6.5, "You are required to provide GSTIN No.ones & ledger accounts periodically for enabling statuary", 0, 0, 'L');
$pdf->Ln(4);

$pdf->SetFont('aefurat', '', 8);
$pdf->Cell(120, 6.5, "compliance payment will be accepted by payee's by payee's a/c check only ", 0, 0, 'L');
$pdf->Ln(4);

ob_end_clean();
$pdf->Output("Annexure I Normal-" . $getmdata['vouchno'] . ".pdf", "I");
?>
         
