<?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 = "Purchase Voucher";
$maindb = 'purmain';
$trandb = 'purtran';

$getqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as mydt 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 {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
        /*background-color: #FAFAFA;*/
        font: 15pt "Arial";
    }
    .page {
        width: 210mm;
        min-height: 297mm;
        padding: 0mm;
        margin: 0mm auto;
        border: 1px #D3D3D3 solid;
        border-radius: 5px;
        /*background: white;*/

    }
    .subpage {
        padding: 0cm;
        /*        border: 1px solid;*/
        height: 288mm;
        outline: 2cm #ffffff solid;
    }

    .table > tbody > tr > td{
        padding: 0px;
    }

</style>
</head>
<body>
    <?php
    $tdhg = "24px";
    $qry = mysqli_fetch_array($getqry);

    $day = $qry['invdt'];
    $timestamp = new DateTime($day);
    $mon = date_format($timestamp, 'F');
    $din = date_format($timestamp, 'd');
    $year2 = date_format($timestamp, 'Y');
    ?>
    <div class="page">
        <div class="" style="margin-top: 1px">
            <div class="row">
                <?php
                $zroot = $_SESSION["zdir"];
                $zcomp = $_SESSION["comp_" . $dbname];
                $getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company`")or die(mysqli_error($conn));
                $comp = mysqli_fetch_array($getcomp);
                ?>
                <style>
                    @media print {
                        .noPrint {
                            display:none;
                        }
                    }
                </style>
<!--                <table class="" width="100%" id="head">
                    <tr style="text-align:center">
                        <td style="font-size: 18px">
                            <label style='font-size:20px;'><? Echo $comp['name']; ?></label>
                        </td>
                    </tr>
                    <tr  style="text-align:center">
                        <td style="font-size: 12px">
                <?php echo "<label style='font-size:15px;'>$comp[add1] $comp[add2] $comp[add3] $comp[city] $comp[pin]</label>" ?>
                        </td>
                    </tr>
                </table>-->
            </div>
            <div class="row">
                <div class="col-sm-12 text-center">
                    <label class="label-control"><h3><? Echo $formname; ?></h3></label>
                </div>
            </div>
            <?php
            $getcust = mysqli_query($conn, "SELECT * FROM `accmast` WHERE  `accode`='$qry[party]' AND `grpcode`='009'")or die(mysqli_error($conn));
            $cust = mysqli_fetch_array($getcust);
            ?>
            <div class="border">
                <div class="row" style="margin-top: 10px">
                    <table style="width: 100%;">
                        <tr>
                            <td width="2%"></td>
                            <td width="25%"><label class="label-control">Name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <? Echo $cust['name']; ?></label></td>
                            <td width="8%"></td>
                            <td width="28%" class="text-right"><label class="label-control">Invoice No. &nbsp; :&nbsp; <? Echo $myyear . ' / ' . $qry['invno']; ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </label></td>
                            <td width="4%"></td>
                        </tr>
                        <tr>
                            <td width="2%"></td>
                            <td width="25%" class=""><label class="label-control">Address &nbsp;&nbsp;&nbsp;: <? Echo $cust['add1']; ?></label></td>
                            <td width="8%"></td>
                            <td width="28%" class="text-right"><label class="label-control">Invoice Date &nbsp;:&nbsp; <? Echo $mon . ' ' . $din . ' , ' . $year2; ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label></td>
                            <td width="4%"></td>
                        </tr>
                        <tr>
                            <td width="2%"></td>
                            <td width="25%" class=""><label class="label-control">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  <? Echo $cust['add2']; ?></label></td>
                            <td width="8%"></td>
                            <td width="28%" class="text-right"><label class="label-control">Container No. &nbsp;: &nbsp;<? Echo $qry['containerno']; ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label></td>
                            <td width="4"></td>
                        </tr>
                        <tr>
                            <td width="2%"></td>
                            <td width="25%" class=""><label class="label-control">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <? Echo $cust['add3']; ?></label></td>
                            <td width="8%"></td>
                            <td width="28%" class="text-right"><label class="label-control">Seal No. &nbsp;:&nbsp; <? Echo $qry['sealno']; ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label></td>
                            <td width="4%"></td>
                        </tr>
                    </table>
                </div>
                <div class="row">

<!--                    <table width="50%" class="table-hover pad">
    <tr>
        <td width="3%"></td>
        <td width="17%" valign="top" class="numeric">
            <label class="smfont pad">M/s.</label>
            <label class="smfont pad">Address</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">City :<? Echo $cust['city']; ?></label><br>
            <label class="smfont">Pin :  <? Echo $cust['pin']; ?></label><br>
            <label class="smfont">Tel : <? Echo $cust['tel']; ?></label><br>
        </td>
    </tr>
</table>-->
                </div>
            </div>
            <div style="border-right: 1px solid black;border-left: 1px solid black;">
                <div class="row">
                    <div class="row pad">
                        <table class="table-condensed table-bordered table-hover" style="width: 100%">
                            <tr style="background-color:#1ABC9C;border: 1px solid black; height: <?= $tdhg; ?>;font-size:14px">
                                <th width="3%" class='text-left'>Sr.</th>
                                <th width="7%" class='text-left'>Logo</th>
                                <th width="5%">Code</th>
                                <th width="8%">Description</th>
                                <th width="6%" class='text-right'>Qty.</th>
                                <th width="6%" class='text-right'>Bale Qty.(Kgs)</th>
                                <th width="6%" class='text-right'>Tot. Qty.(Kgs)</th>
                                <th width="6%" class='text-right'>Rate</th>
                                <th width="6%" class='text-right'>Amount</th>
                            </tr>
                            <?
                            $ctr = 0;
                            $ctab = 0;
                            $tbrand = '';
                            $gettran = mysqli_query($conn, "SELECT * FROM `purtran` WHERE `vouchno`='$qry[vouchno]' ORDER BY `srno`")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 `barcode`='$tran[itemcode]'")or die(mysqli_error($conn));
                                $item = mysqli_fetch_array($getitem);
                                $ctr++;
                                ?>

                                <tr style="height: <?= $tdhg; ?>">
                                    <td><?= $ctr; ?></td>
                                    <td><?= $brand['name']; ?></td>
                                    <td><?= $item['code']; ?></td>
                                    <td><?= $item['name']; ?></td>
                                    <td class='text-right'><?= $tran['qty']; ?></td>
                                    <td class='text-right'><?= $tran['baleqty']; ?></td>
                                    <td class='text-right'><?= $tran['totqty']; ?></td>
                                    <td class='text-right'><?= $tran['rate']; ?></td>
                                    <td class='text-right'><?= $tran['amount']; ?></td>

                                </tr>
                                <?
                                $tottaka = $tran['taka'] + $tottaka;
                                $totqty = $tran['invqty'] + $totqty;
                                $ctab = $ctab + 1;
                            }
                            if ($ctab < 15) {
                                for ($i = $ctab; $i < 15; $i++) {
                                    ?>
                                    <tr style="height: <?= $tdhg; ?>">
                                        <td></td>
                                        <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 rborder pad" style="float: left;width:65%">
                            <tr>
                                <td><label class="smfont pad"><? Echo $qry['rem1']; ?></label></td>
                            </tr>
                            <tr>
                                <td><label class="smfont pad"><? Echo $qry['rem2']; ?></label></td>
                            </tr>
                            <tr>
                                <td><label class="smfont pad"></label></td>
                            </tr>
                            <tr>
                                <td><label class="smfont pad"></label></td>
                            </tr>
                            
                        </table>
                        <table class="table-hover" style="float: left;width:35%">
                            <tr>
                                <td><label class="smfont pad">Sub Total</label></td>
                                <td><label class="smfont pad"></label></td>
                                <td class="numeric"><label class="smfont pad"><? Echo $qry['subtotal']; ?></label></td>
                            </tr>
                            <?php if ($qry['discount'] > 0) { ?>
                                <tr>
                                    <td><label class="smfont pad">Discount </label></td>
                                    <td class="numeric"><label class="smfont pad"><? Echo $qry['discperc'] . "%"; ?></label></td>
                                    <td class="numeric"><label class="smfont pad"><? Echo $qry['discount']; ?></label></td>
                                </tr>
                            <?php } ?>
                            <?php if ($qry['freight'] > 0) { ?>
                                <tr>

                                    <td><label class="smfont pad">Freight </label></td>
                                    <td><label class="smfont pad"></label></td>
                                    <td class="numeric"><label class="smfont pad"><? Echo $qry['freight']; ?></label></td>

                                </tr>
                            <?php } ?>
                            <?php if ($qry['labour'] > 0) { ?>
                                <tr>

                                    <td><label class="smfont pad">Labour</label></td>
                                    <td><label class="smfont pad"></label></td>
                                    <td class="numeric"><label class="smfont pad"><? Echo $qry['labour']; ?></label></td>

                                </tr>
                            <?php } ?>
                            <?php if ($qry['tempo'] > 0) { ?>
                                <tr>

                                    <td><label class="smfont pad">Tempo</label></td>
                                    <td><label class="smfont pad"></label></td>
                                    <td class="numeric"><label class="smfont pad"><? Echo $qry['tempo']; ?></label></td>

                                </tr>
                            <?php } ?>
                            <?php if ($qry['packing'] > 0) { ?>
                                <tr>

                                    <td><label class="smfont pad">Packing</label></td>
                                    <td><label class="smfont pad"></label></td>
                                    <td class="numeric"><label class="smfont pad"><? Echo $qry['packing']; ?></label></td>

                                </tr>
                            <?php } ?>
                            <?php if ($qry['othchgs'] > 0) { ?>
                                <tr>

                                    <td><label class="smfont pad">Other Charges</label></td>
                                    <td><label class="smfont pad"></label></td>
                                    <td class="numeric"><label class="smfont pad"><? Echo $qry['othchgs']; ?></label></td>

                                </tr>
                            <?php } ?>
                        </table>
                    </div>
                </div>
                <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 pad"></label></td>
                                <td><label class="smfont pad"></label></td>
                                <td><label class="smfont pad"></label></td>
                            </tr>
                        </table>
                        <table class="table-hover tborder lborder" 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 $zroot.`company`")or die(mysqli_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'); ?>
