<?php
if (!$isInclude) {
    require("includes/check_auth_user.php");
    require("includes/dbconnect.php");
    include('includes/function.php');
}
mysqli_query($conn, "SET NAMES 'utf8'");
mysqli_query($conn, 'SET CHARACTER SET utf8');

$formname = 'Purchase Voucher';
$maindb = 'purmain';
$trandb = 'purtran';
$file = '';
$source = "PU";
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];
mysqli_select_db($conn, $zdb);
$mht = 'height:20px;';

$admin = $_SESSION["admin_" . $dbname];
$vouchno = $_REQUEST["vouchno"];


$fseries = $_SESSION["series"];
$keycond = "AND series = '$fseries'";
$admin = $_SESSION["admin_" . $dbname];

$vouchno = ($_REQUEST["vouchno"]) ? $_REQUEST["vouchno"] : $mvouchno;

$mainqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS mydt FROM `$maindb` WHERE vouchno='$vouchno' $keycond") or die(mysqli_error($conn));
$getmdata = mysqli_fetch_array($mainqry);
$vouchdt = $getmdata['vouchdt'];

$prnnum = $getmdata['prnnum'];
$newprnnum = $prnnum + 1;

// mysqli_query($conn, "Update `$maindb` SET `prnnum`= '$newprnnum' where vouchno = '$vouchno'") or die(mysqli_error($conn));

$getpaccode = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`= '$getmdata[party]'") or die(mysqli_error($conn));
$cust = mysqli_fetch_array($getpaccode);
$add1 = $cust['add1'];
$add2 = $cust['add2'];
$add3 = $cust['add3'];
$city = $cust['city'];
$pin = $cust['pin'];
$paytype = $getmdata['paytype'];

if ($getmdata['cashparty'] != ""){
    $party = $getmdata['cashparty'];
}else{
    $party = $cust['name'];
}

?>

<!DOCTYPE HTML>
<html lang="en-US">
    <?php
    include("includes/linkshead.php");
    ?>
</head>
<style>
    body {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
        padding-right: 5px;
        padding-left: 5px;
        padding-top: 5px;
        padding-bottom: 0px;
        font: 12pt "Tahoma";
    }

    .f12 {
        font-size: 12px;
    }

    .f11 {
        font-size: 11px;
    }

    .f13 {
        font-size: 13px;
        font-weight: bold;
        padding: 3px;
    }

    .f14 {
        font-size: 14px;
    }

    .bold {
        font-weight: bold;
    }

    .page {
        width: 140mm;
        min-height: 207mm;
        padding: 2mm;
        margin: 2mm auto;
        page-break-after: always;
    }

    .subpage {
        border: 1px solid black;
        padding: 1%;
    }

    .border {
        border: 1px solid black;
        color: black;
        padding-right: 2px;
        padding-left: 3px;
    }

    th {
        font-weight: bold;
        font-size: 13px;
    }

    @media print {
        @page {
            size: a5;
            margin: 0;
        }


        .page {
            width: 90%;
            /* width: 144mm; */
            min-height: 180mm;
            padding: 2mm;
            margin: 10mm auto;
            page-break-after: always;
        }

        .border {
            border: 1px solid black;
            color: black;
            padding-right: 2px;
            padding-left: 3px;
        }

        th {
            font-weight: bold;
            font-size: 13px;
        }

        .table2 {
            border: 2px solid black;
        }
    }
</style>
<?php
//require("includes/dbconnect.php");
$comp = mysqli_query($conn, "SELECT * FROM $zroot.`company`") or die(mysqli_errno($conn));
$zcomp = mysqli_fetch_assoc($comp);
header('Content-Type: text/html;charset=UTF-8');
ini_set('default_charset', 'utf-8');
?>

<body id="main_container" onload="window.print()">
    <div class="page">
        <div class="subpage">
            <table class="" style="width:100%;">
                <thead>
                    <tr>
                        <td colspan="4" style="text-align: center !important;"><b style="width: 100%;font-size:15px"><?= $formname; ?></b></td>
                    </tr>
                </thead>
                <tr>
                    <td colspan="4" style="text-align: left; width:100%;font-size:15px"><?php echo "$party"; ?></td>
                </tr>
                <tr>
                    <td style="text-align: left; width:55%;font-size:15px"><b>Voucher.</b> : <?= $getmdata['vouchno'] ?> </td>
                    <td style="text-align: right ; width:20%;font-size:15px"><b>Bill Date. &nbsp;</b></td>
                    <td style="width:1%">:</td>
                    <td style="text-align: left; width:24%;font-size:15px">&nbsp;<?php echo " $getmdata[mydt]"; ?></td>
                </tr>
                <tr>
                    <td style="text-align: left; width:55%;font-size:15px"><b>Bill No.</b> : <?= $getmdata['invno'] ?> </td>
                    <td style="text-align: right ; width:20%;font-size:15px"><b>&nbsp;</b></td>
                    <td style="width:1%"></td>
                    <td style="text-align: left; width:24%;font-size:15px">&nbsp;</td>
                </tr>
            </table>

            <br>
            <table style="width:100%" class="">

                <tr class="" style="height:24px;">
                    <th class="border" style="font-size:13px;width:30%; text-align: center;">Item</th>
                    <th class="border" style="font-size:13px;width:8%; text-align: center;">Bag</th>
                    <th class="border" style="font-size:13px;width:10%; text-align: center;">Qty</th>
                    <th class="border" style="font-size:13px;width:16%; text-align: center;">Cost/10Kg</th>
                    <th class="border" style="font-size:13px;width:14%; text-align: center;">Amount</th>
                </tr>


                <?php
                $tranpart = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$getmdata[vouchno]' order by srno") or die(mysqli_error($conn));
                $mht = '20px';
                $ctab = 0;
                $grqty = 0;
                while ($row = mysqli_fetch_array($tranpart)) {
                    $getbrand = mysqli_query($conn, "SELECT * FROM `product` WHERE code = '$row[itemcode]'") or die(mysqli_error($conn));
                    $getb = mysqli_fetch_array($getbrand);
                    ?>
                    <tr class="border" style="height:24px;">
                        <td class="border " style="font-size:15px;"><?php echo "$getb[name]"; ?></td>
                        <td class="border " style="font-size:13px;text-align: right;"><?php echo "$row[cartqty]"; ?></td>
                        <td class="border " style="font-size:13px;text-align: right;"><?php echo "$row[qty]"; ?></td>
                        <td class="border " style="font-size:13px;text-align: right"><?php echo "$row[rate]"; ?></td>
                        <td class="border " style="font-size:13px;text-align: right"><?php echo "$row[amount]"; ?></td>
                    </tr>
                    <?php
                    $ctab = $ctab + 1;
                    $grqty = $grqty + $row['qty'];
                    $totamt = $totamt + $row['amount'];
                }
                if ($ctab < 6) {
                    for ($i = $ctab; $i <= 6; $i++) {
                        $ctab = $ctab + 1;
                        ?>
                        <tr style="height:24px;">
                            <td class="border"></td>
                            <td class="border"></td>
                            <td class="border"></td>
                            <td class="border"></td>
                            <td class="border"></td>
                        </tr>
                        <?php
                    }
                }
                ?>

                <tr>
                    
                    <td colspan="5" style="border:none;border-collapse: collapse; ">
                        <table border="solid 1px" style="border-collapse: collapse; width:100%">
                            <tbody style="border-collapse: collapse;font-size: 12px;">

                                <tr style="height:24px;">
                                    <td class="border text-right  bold" colspan="3">Total</td>
                                    <td class="border text-right  bold"><?php echo number_format(round($totamt), 2) ?></td>
                                </tr>
                                <?php if ($getmdata['marketfee'] > 0) { ?>
                                    <tr style="height:24px;">
                                    <td class="border text-right  bold" colspan="3">Market Fees</td>
                                        <td class="border text-right  bold"><?php echo number_format(round($getmdata['marketfee']), 2) ?></td>
                                    </tr>

                                <?php } else { ?>
                                    <tr style="height:24px;">
                                        <td class="border text-right  bold" colspan="3"></td>
                                        <td class="border text-right  bold"></td>
                                    </tr>
                                <?php } ?>
                                <?php if ($getmdata['commamt'] > 0) { ?>
                                    <tr style="height:24px;">
                                        <td class="border text-right  bold" colspan="3">Commission &nbsp;&nbsp;&nbsp;<?= $getmdata['commperc']; ?>% &nbsp;</td>
                                        <td class="border text-right  bold"><?php echo number_format(round($getmdata['commamt']), 2) ?></td>
                                    </tr>

                                <?php } else { ?>
                                    <tr style="height:24px;">
                                        <td class="border text-right  bold" colspan="3"></td>
                                        <td class="border text-right  bold"></td>
                                    </tr>
                                <?php } ?>
                              
                                <tr style="height:24px;">
                                    <td class="border text-right  bold" colspan="3">Bill Amount</td>
                                    <td class="border text-right  bold"><?php echo rajnumformat(round($getmdata['billamt'])) ?></td>
                                </tr>
                            </tbody>
                        </table>
                    </td>
                </tr>

            </table>
        </div>
    </div>
    <!--</div>-->
    <!--</div>-->
    <?php
    include("includes/linksfoot.php");
    ?>

</body>

</html>