<?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];

ini_set('display_errors', 1);
error_reporting(E_ERROR);

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

$vouch = $_GET['vouchno'];

$cond = '';
$lcond = '';
$formname = "Material Given To Karigar Print";
$formname = "";

$maindb = 'rawmain';
$trandb = 'rawtran';

$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);

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

<!DOCTYPE HTML>
<html lang="en-US">

<style>
    body {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
        font: 12pt "Tahoma";
    }

    @media print {
        .noprint {
            display: none;
        }
    }

    * {
        box-sizing: border-box;
        -moz-box-sizing: border-box;
    }

    .page {
        width: 210mm;
        min-height: 297mm;
        padding: 4mm;
        margin: 10mm auto;
        border: 1px #D3D3D3 solid;
        border-radius: 5px;
    }

    .subpage {
        padding: 0cm;
        height: 288mm;
        outline: 2cm #ffffff solid;
    }

    @page {
        size: A4;
        margin: 0;
    }

    @media print {

        html,
        body {
            width: 210mm;
            height: 297mm;
        }

        .page {
            margin: 0;
            border: initial;
            border-radius: initial;
            width: initial;
            min-height: initial;
            box-shadow: initial;
            background: initial;
            page-break-after: always;
        }
    }

    .numeric {
        text-align: right;
    }

    .rnone {
        border-right: none;
    }

    .lnone {
        border-left: none;
    }

    /* ── Watermark ─────────────────────────────────────────────── */
    .page {
        position: relative;
    }

    .watermark {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 550px;
        /* adjust size to taste */
        height: 300px;
        opacity: 0.08;
        /* very faint — increase to make darker */
        pointer-events: none;
        z-index: 9999;
        user-select: none;
    }

    .watermark img {
        width: 100%;
        height: 100%;
        object-fit: contain;
    }

    @media print {
        .watermark {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            opacity: 0.06;
            -webkit-print-color-adjust: exact;
            print-color-adjust: exact;
        }
    }
    .sktable td,
    .sktable th {
        border: 1px solid black;
    }
</style>
</head>

<!--<link href="MyStyle/rep_format.css" rel="stylesheet">-->

<body id="main_container">
    <div class="book">
        <button class="btn btn-primary goBack noprint" onclick="window.close()"
            style="float: right;width: 150px;height:35px;background-color: white;color:#A0B1BA;">Go Back</button>
        <button class="btn btn-primary goBack noprint" onclick="window.print()"
            style="float: right;width: 150px;height:35px;background-color: white;color:#A0B1BA;">Print</button>
        <div class="page">
            <!-- ── WATERMARK ── -->
            <div class="watermark">
                <img src="images/mwatermark.png" alt="">
            </div>
            <!--   ************Header************  -->
            <div style="border:solid 1px;" class="col-md-12">
                <center>
                    <p style="margin-top:2px;margin-bottom: 2px;font-size:11px;"><u>Subject To Mumbai Jurisdiction</u></p>
                </center>
                <?php include("includes/head_print.php"); ?>
            </div>

            <div style="border:solid 1px;" class="col-md-12" style="padding-left: 1px ; padding-right: 1px">

                <table class="table table-border table-hover" style="width:100%">
                    <tr>
                        <td style="text-align: left; width:5%"><b>Voucher</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:40%"><?php echo "$getdata[vouchno]"; ?></td>
                        <td style="text-align: right ; width:30%"><b>Date</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:20%"><?php echo " $getdata[vouchdt]"; ?></td>
                    </tr>
                    <tr>
                        <td style="text-align: left; width:5%"><b>Customer</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:40%"><?php echo "$showparty[name]"; ?></td>
                    </tr>
                </table>
            </div>
            <div>
                <table class="sktable" style="border-collapse: collapse; width:100%">
                    <tr>
                        <th style="width:5%; color:black;"><label>Sr.</label></th>
                        <th style="width:25%; color:black;"><label>Material.</label></th>
                        <th style="width:15%; color:black;"><label>Color</label></th>
                        <th style="width:15%; color:black;"><label>Lot</label></th>
                        <!-- <th style="width:9%; color:black; text-align: right;"><label>Rolls</label></th> -->
                        <!-- <th style="width:9%; color:black; text-align: right;"><label>Yard</label></th> -->
                        <th style="width:10%; color:black; text-align: right;"><label>Mtrs./Qty</label></th>
                        <!-- <th style="width:6%; color:black; text-align: right;"><label>Unit</label></th> -->
                        <th style="width:10%; color:black; text-align: right;"><label>Rate</label></th>
                        <th style="width:10%; color:black; text-align: right;"><label>Amount</label></th>
                    </tr>

                    <?php
                    $ctr = 0;
                    $ctab = 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)) {
                        $finditem = mysqli_query($conn, "SELECT `code`,`name` FROM `rawmat` WHERE `code`='$showtran[item]'") or die(mysqli_error($conn));
                        $fitem = mysqli_fetch_array($finditem);

                        $getcolor = mysqli_query($conn, "SELECT `code`, `name` FROM `color` WHERE `code` = '$showtran[color]'") or die(mysqli_error($conn));
                        $showcolor = mysqli_fetch_array($getcolor);

                        echo "<tr>";
                        echo "<td style=''>$showtran[srno]</td>";
                        echo "<td style=''>$fitem[name]</td>";
                        echo "<td style=''>$showcolor[name]</td>";
                        echo "<td style='text-align: right;'>$showtran[lot]</td>";
                        // echo "<td style='text-align: right;'>$showtran[yard]</td>";
                        echo "<td style='text-align: right;'>$showtran[qty]</td>";
                        echo "<td style='text-align: right;'>$showtran[rate]</td>";
                        echo "<td style='text-align: right;'>$showtran[amount]</td>";
                        echo "</tr>";

                        $ctr++;
                        $ctab = $ctab + 1;
                        $totqty = $totqty + $showtran['qty'];
                        $totlot += (int) $showtran['lot'];
                        $totyard = $totyard + $showtran['yard'];
                        $totmeters = $totmeters + $showtran['meters'];
                    }
                    if ($ctab < 23) {
                        for ($i = $ctab; $i <= 23; $i++) {
                            $ctab = $ctab + 1;
                            ?>
                            <tr style="height:26px;">
                                <td class="border"></td>
                                <td class="border"></td>
                                <td class="border"></td>
                                <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="2"></td>
                        <td colspan="" style="text-align: right;"> Total </td>
                        <td style="text-align: right;"> <b><?= $totlot; ?> </b></td>
                        <!-- <td style="text-align: right;"> <b><?= $totyard; ?> </b></td> -->
                        <td style="text-align: right;"><b><?= $totqty; ?></b></td>
                    </tr>
                    <tr>
                        <td colspan="4"><?= "$getdata[remark1]" . " &nbsp;" . "$getdata[remark2]"; ?></td>
                        <td colspan="" style="text-align: right;"> Subtotal </td>
                        <td style="border-right: 1px;border-left: 1px; "></td>
                        <td style="text-align: right;border-left: none;"><b><?= $getdata['subtotal']; ?></b></td>

                    </tr>

                    <?php
                    if ($getdata['lesother'] > 0) {
                        ?>
                        <tr>
                            <td style="text-align: right; " class="lnone rnone"> </td>
                            <td colspan="4" style="text-align: right;" class="rnone"> Less Other</td>
                            <td class="lnone rnone"></td>
                            <td style="text-align: right;" class="lnone "><b> <?= $getdata['lesother']; ?> </b> </td>
                        </tr>
                    <?php } ?>
                    <?php
                    if ($getdata['totalamount'] > 0) {
                        ?>
                        <!-- <tr>
                            <td colspan="7" style="text-align: right;" class="rnone">Total Amount</td>
                            <td class="lnone rnone"></td>
                            <td style="text-align: right;" class="lnone "><b><?= $getdata['totalamount']; ?>  </b> </td>
                        </tr> -->

                    <?php } ?>
                    <?php
                    if ($getdata['roundoff'] > 0) {
                        ?>
                        <tr>
                            <td colspan="6" style="text-align: right;" class="rnone">Round Off</td>
                            <td class="lnone rnone"></td>
                            <td style="text-align: right;" class="lnone "><b> <?= $getdata['roundoff']; ?> </b></td>
                        </tr>

                    <?php } ?>

                    <?php
                    if ($getdata['netamt'] > 0) {
                        ?>
                        <tr>
                            <td colspan="5" style="text-align: right;" class="rnone">Bill Amount</td>
                            <td class="lnone rnone"></td>
                            <td style="text-align: right;" class="lnone "><b> <?= $getdata['netamt']; ?> </b></td>
                        </tr>

                    <?php } ?>
                    </tr>
                </table>
            </div>

            <div>
                <table class="border">
                    <tr>
                        <td class="border" style="width: 541px; font-size: 14px;">
                            <!--<img src="images/taximg.png">-->
                        </td>
                        <td class="border" style="width: 216px">
                            <?php
                            require("includes/dbconnect.php");
                            ?>
                            <div style=" text-align:right; font-size: 14px; font-weight: 900">
                                <?= "For $getcmpname[name]"; ?></div>
                            <br>
                            <br>
                            <br>
                            <div style=" margin-left: 51px; font-size: 11px">AUTHORISED SIGNATORY</div>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <!--</div>-->
    </div>
    <?php
    include("includes/linksfoot.php");
    ?>
    <script>
        $(function () {
            formGeneral();
        });
    </script>
</body>

</html>