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

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

$vouch = $_GET['vouchno'];
$prnsuit = $_GET['prnsuit'];
$prnbrand = $_GET['prnbrand'];

$cond = '';
$lcond = '';
$formname = "Invoice";
$maindb = 'salemain';
$trandb = 'saletran';

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

if (mysqli_num_rows($getqry) <= 0) {
    browse("Record Not Found");
}
include("includes/linkshead.php");
?>
<link href="MyStyle/rep_format.css" rel="stylesheet">
<style>
    body{
        font-size: 15px;
    }
    .table > tbody > tr > td{
        padding: 0px;
    }

</style>
</head>
<body>
    <?php
    $tdhg = "24px";
    $qry = mysqli_fetch_array($getqry);
    ?>
    <div class="page">
        <div class="subpage" style="margin-top: 30px">
            <div class="row">
                <div class="col-sm-12 " style="height: 120px"></div>
            </div>
            <div class="row">
                <div class="col-sm-12 text-center">
                    <label class="label-control"><? Echo $formname; ?></label>
                </div>
            </div>
            <div class="border">
                <div class="row" style="margin-top: 10px">
                    <table style="width: 100%">
                        <tr>
                            <td width="25%"></td>
                            <td width="25%"><label class="label-control">No  : <? Echo $myyear . '/' . $qry[invno]; ?></label></td>
                            <td width="25%" class="text-right"><label class="label-control">Date: <? Echo $qry[mydt]; ?></label></td>
                            <td width="25%"></td>
                        </tr>
                    </table>
                </div>
                <div class="row">
                    <?
                    $getcust = mysqli_query($conn, "SELECT * FROM `accmast` WHERE  `accode`='$qry[party]' AND `grpcode`='010'")or die(mysqli_error($conn));
                    $cust = mysqli_fetch_array($getcust);
                    ?>
                    <table width="50%" class="table-hover pad">
                        <tr>
                            <td width="20%" valign="top" class="numeric">
                                <label class="smfont pad">M/s.</label>
                            </td>
                            <td width="80%" class="pad">
                                <label class="smfont"><? Echo $cust[name]; ?></label><br>
                                <label class="smfont"><? Echo $cust[add1]; ?></label><br>
                                <label class="smfont"><? Echo $cust[add2]; ?></label><br>
                                <?php if (!empty($cust[add3])) { ?>
                                    <label class="smfont"><? Echo $cust[add3]; ?></label><br>
                                <?php } ?>   
                                <label class="smfont"><? Echo $cust[city] . '   Pin :' . $cust[pin]; ?></label>
                                <label class="smfont">Tel : <? Echo $cust[tel]; ?></label><br>
                                <label class="smfont">TIN No. : <? Echo $cust[vattin]; ?></label><br>
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="row">
                    <?
                    $getbrok = mysqli_query($conn, "SELECT * FROM `accmast` WHERE  `accode`='$qry[broker]'")or die(mysqli_error($conn));
                    $brok = mysqli_fetch_array($getbrok);
                    $gettrsp = mysqli_query($conn, "SELECT * FROM `transport` WHERE  `code`='$qry[transporter]'")or die(mysqli_error($conn));
                    $trsp = mysqli_fetch_array($gettrsp);
                    ?>
                    <div class="col-sm-12">
                        <table style="width: 100%">
                            <tr>
                                <td width="50%" class="table-hover">
                                    <table width="100%">
                                        <tr>
                                            <td width="20%" class="numeric"><label class="smfont pad">Broker :</label></td>
                                            <td width="80%"><label class="smfont pad"><? Echo $brok[name]; ?></label></td>
                                        </tr>
                                    </table>
                                </td>
                                <td width="50%">
                                    <table width="100%">
                                        <tr>
                                            <td width="40%" class="numeric"><label class="smfont">Transporter :</label></td>
                                            <td width="60%"><label class="smfont"> <? Echo $trsp[name]; ?></label></td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-12">
                        <table style="width: 100%">
                            <tr>
                                <td width="50%" class="table-hover">
                                    <table width="100%">
                                        <tr>
                                            <td width="20%" class="numeric"><label class="smfont pad">L.R. No. :</label></td>
                                            <td width="80%"><label class="smfont pad"><? Echo $qry[lrno]; ?></label></td>
                                        </tr>
                                    </table>
                                </td>                                
                                <td width="50%">
                                    <table width="100%">
                                        <tr>
                                            <td width="40%" class="numeric"><label class="smfont">L.R.Date : </label></td>
                                            <td width="60%"><label class="smfont"><? Echo $qry[mylrdt]; ?></label></td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
            <div style="border-right: 1px solid black;border-left: 1px solid black;">
                <div class="row">
                    <div class="col-sm-12 pad">
                        <table class="table-condensed table-hover" style="width: 100%">
                            <tr style="background-color:#cccccc;border: 1px solid black; height: <?= $tdhg; ?>">
                                <th width="8%">Type</th>
                                <th width="43%">Description</th>
                                <th class="numeric" width="4%">    </th>
                                <th class="numeric" width="6%">Pcs.</th>
                                <th class="numeric" width="8%">Mtrs</th>
                                <th class="numeric" width="8%">Net.Mtrs</th>
                                <th class="numeric" width="11%">Rate</th>
                                <th class="numeric" width="12%">Amount</th>
                            </tr>
                            <?
                            $ctab = 0;
                            $tbrand = '';
                            $gettran = mysqli_query($conn, "SELECT * FROM `saletran` WHERE `vouchno`='$qry[vouchno]' ORDER BY `brand`, itemcode")or die(mysqli_error($conn));
                            while ($tran = mysqli_fetch_array($gettran)) {
                                $mbrand = $tran[brand];

                                $getbrand = mysqli_query($conn, "SELECT * FROM `brand` WHERE `code`='$tran[brand]'")or die(mysqli_error($conn));
                                $brand = mysqli_fetch_array($getbrand);

                                $getitem = mysqli_query($conn, "SELECT * FROM `item` WHERE `code`='$tran[itemcode]'")or die(mysqli_error($conn));
                                $item = mysqli_fetch_array($getitem);

                                $gettype = mysqli_query($conn, "SELECT * FROM `type` WHERE `code`='$qry[type]'")or die(mysqli_error($conn));
                                $type = mysqli_fetch_array($gettype);
                                ?>
                                <?php
                                if ($mbrand != $tbrand) {
                                    if ($prnbrand == 'N') {
                                        ?>
                                        <tr>
                                        </tr>
                                        <?php
                                    } else {
                                        ?>
                                        <tr style="height: <?= $tdhg; ?>">
                                            <td></td>
                                            <td><b><? Echo $brand[name]; ?></b></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                        </tr>
                                        <?php
                                    }
                                    $ctab = $ctab + 1;
                                    $tbrand = $mbrand;
                                }
                                ?>
                                <tr style="height: <?= $tdhg; ?>">
                                    <td>
                                        <?php
                                        if ($prnsuit == 'N') {
                                            
                                        } else {
                                            Echo $type[name];
                                        }
                                        ?>
                                    </td>
                                    <td><? Echo $item[name]; ?></td>
                                    <?php if ($tran[flag] == 0) { ?>
                                        <td></td>
                                    <?php } else { ?>
                                        <td class=""><? Echo '(' . $tran[flag] . 'F)'; ?></td>
                                    <?php } ?>
                                    <td class="numeric"><? Echo $tran[taka]; ?></td>
                                    <td class="numeric"><? Echo $tran[qty]; ?></td>
                                    <td class="numeric"><? Echo $tran[invqty]; ?></td>
                                    <td class="numeric"><? Echo $tran[rate]; ?></td>
                                    <td class="numeric"><? Echo $tran[amount]; ?></td>
                                </tr>
                                <?
                                $tottaka = $tran[taka] + $tottaka;
                                $totqty = $tran[invqty] + $totqty;
                                $ctab = $ctab + 1;
                            }
                            if ($ctab < 19) {
                                for ($i = $ctab; $i < 19; $i++) {
                                    ?>
                                    <tr style="height: <?= $tdhg; ?>">
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                    </tr>
                                    <?php
                                }
                            }
                            ?> 
                        </table>
                    </div>
                </div>
            </div>
            <div class="border">
                <div class="row">
                    <div class="col-sm-12" style="padding: 0px">
                        <table class="table-hover " style="float: left;width:65%">
                            <tr>
                                <td><label class="smfont "><? Echo $qry[remark1]; ?></label></td>
                            </tr>
                            <tr>
                                <td><label class="smfont "><? Echo $qry[remark2]; ?></label></td>
                            </tr>
                            <tr>
                                <td><label class="smfont "><? Echo $qry[remark3]; ?></label></td>
                            </tr>
                            <tr>
                                <td><label class="smfont "><? Echo $qry[remark4]; ?></label></td>
                            </tr>
                            <tr>
                                <td><label class="smfont"></label></td>
                            </tr>
                        </table>
                        <table class="table-hover" style="float: left;border-left:1px solid black;width:35%">
                            <tr>
                                <td><label class="smfont ">Sub Total</label></td>
                                <td><label class="smfont "></label></td>
                                <td class="numeric"><label class="smfont "><? Echo $qry[subtotal]; ?></label></td>
                            </tr>
                            <tr>
                                <?php // if ($qry[discount] > 0) { ?>
                                    <td><label class="smfont ">Discount </label></td>
                                    <td class="numeric"><label class="smfont "><? Echo $qry[discperc] . "%"; ?></label></td>
                                    <td class="numeric"><label class="smfont "><? Echo $qry[discount]; ?></label></td>
                                    <?php
//                                }
                                ?>
                            </tr>
                            <tr>
                                <td><label class="smfont ">Freight </label></td>
                                <td><label class="smfont "></label></td>
                                <td class="numeric"><label class="smfont "><? Echo $qry[freight]; ?></label></td>
                            </tr>
                            <tr>
                                <td><label class="smfont ">Taxable Value </label></td>
                                <td><label class="smfont "></label></td>
                                <td class="numeric"><label class="smfont "><? Echo $qry[taxamt]; ?></label></td>
                            </tr>
                             <tr>
                                <?php // if ($qry[cgstperc] > 0) { ?>
                                    <td><label class="smfont ">Add CGST</label></td>
                                    <td class="numeric"><label class="smfont "><? Echo $qry[cgstperc] . "%"; ?></label></td>
                                    <td class="numeric"><label class="smfont "><? Echo $qry[totcgst]; ?></label></td>
                                 <?php // } ?>
                            </tr>
                             <tr>
                                <?php // if ($qry[sgstperc] > 0) { ?>
                                    <td><label class="smfont ">Add SGST</label></td>
                                    <td class="numeric"><label class="smfont "><? Echo $qry[sgstperc] . "%"; ?></label></td>
                                    <td class="numeric"><label class="smfont "><? Echo $qry[totsgst]; ?></label></td>
                                <?php // } ?>
                            </tr>
                             <tr>
                                <?php // if ( $qry[igstperc] > 0 ){ ?>
                                    <td><label class="smfont ">Add IGST</label></td>
                                    <td class="numeric"><label class="smfont "><? Echo $qry[igstperc] . "%"; ?></label></td>
                                    <td class="numeric"><label class="smfont "><? Echo $qry[totigst]; ?></label></td>
                                <?php // } ?>
                            </tr>
                        </table>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-12" style="padding: 0px">
                        <table class="table-hover rborder" style="float: left;width:65%">
                            <tr>
                                <td><label class="smfont pad">Total : </label> <? Echo $tottaka . " Pcs."; ?></td>
<!--                                <td><label class="smfont pad"><? Echo $tottaka . "Pcs."; ?></label></td>-->
                                <td><label class="smfont pad">Total : </label> <? Echo number_format($totqty, 2) . " Mtrs."; ?></td>
                            </tr>
                        </table>
                        <table class="table-hover tborder" style="float: left;width:35%">
                            <tr>
                                <td><label class="smfont pad">Bill Amount</label></td>
                                <td class="numeric"><label class="smfont pad"><i class="fa fa-rupee"></i></label></td>
                                <td class="numeric"><label class="smfont pad"><? Echo number_format(round($qry[netamt]), 2); ?></label></td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
            <?php
            $mwords = round($qry[netamt]);
//            $words = convert_number_to_words($mwords);
            $words = rsword($mwords);
            ?>
            <div class="border">
                <div class="row">
                    <div class="col-sm-12" style="padding: 0px">
                        <label class="smfont pad"><?php Echo $words . " Only"; ?></label>
                    </div>
                </div>
            </div>
            <?php
            $getcomp = mysqli_query($conn, "SELECT * FROM cepheisy_fab_root.`company`")or die(mysql_errror());
            $comp = mysqli_fetch_array($getcomp);
            ?>
            <div class="">
                <div class="row">
                    <div class="col-sm-12" style="padding:0px">
                        <table class="table-hover" width='100%'>
                            <tr>
                                <td><label class="smfont">E.O.& E.</label></td>
                                <td class="numeric">
                                    <label class="smfont"><?php Echo "For " . $comp[name] ?></label><br>
                                    <img src="assets/img/sign.jpg" alt="sign">
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php include('includes/linksfoot.php'); ?>
