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

$vouch = $_GET['vouchno'];

$cond = '';
$lcond = '';
$formname = "SALE";

$maindb = 'salemain';
$trandb = 'saletran';

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

$logo = $getcmpname['logoimg'];

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

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

$getarchitect = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$getdata[architect]'") or die(mysqli_error($conn));
$larchitect = mysqli_fetch_array($getarchitect);
?>

<!DOCTYPE HTML>
<html lang="en-US">
    <style>
        body {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            /*background-color: #FAFAFA;*/
            font: 12pt "Arial";
        }

        @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;
            /*background: white;*/

        }

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

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

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

            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;
        }

        .outer {
            width: 100%;
            border-collapse: collapse;
            border: 1px solid black;
        }

        .center {
            text-align: center;
        }

        .B {
            padding-left: 15px;
        }


        .d {
            border-collapse: inherit;
        }


        .w {
            width: 40%;
        }
    </style>
    <title>Tileweb</title>
</head>


<body id="main_container">
    <div class="book tablerep">

        <button class="btn btn-primary goBack noPrint" onclick="window.close()" style="width: 150px;float:right;height:35px;background-color: #26A69A;color:white;padding-left: 1%">Go Back</button>
        &nbsp;&nbsp;&nbsp;
        <button onclick="window.print();return false;" class="btn btn-primary noPrint" style="width: 150px;height:35px;float:right;background-color: #9E8759;color:white;">Print</button>
        <div class="page">
            <div class="">
                <div class="row" style="text-align:center;">
                    <img src="images/logo/HALOGO.png" alt="Cinque Terre" class="skcssimg" style="width:102%;height:100%;margin-top:-15px">
                </div>
                <div class="row" style="text-align:center;">
                    <b style="font-size: 20px;">
                        <?php echo $formname ?></b>
                </div>
                <br>
                <table class=" table table-borderless" style="width:100%">
                    <tbody>
                        <tr>
                            <?php if ($getdata['ntprint'] == "") { ?>
                                <td style="width:25%;">M/S. (BILL TO)</td>
                                <td style="width:40%;"><b><?php echo " : " . $lparty["name"]; ?></b></td>
                            <?php } else { ?>
                                <td style="width:25%;"></td>
                                <td style="width:40%;"><b></b></td>
                            <?php } ?>
                            <td style="width:20%;">NO. </td>
                            <td style="width:15%;"><b><?php echo " : " . $getdata['vouchno']; ?></b></td>

                        </tr>
                        <tr>
                            <td style="width:25%;">ADDRESS (SHIP TO)</td>
                            <td style="width:40%;"><b><?php echo " : " . $getdata["add1"]; ?></b></td>
                            <td style="width:20%;">MOBILE</td>
                            <td style="width:15%;"><b><?php echo " : " . $getdata["mobile"]; ?></b></td>
                        </tr>
                        <tr>
                            <?php if ($getdata['add2'] != '') { ?>

                                <td style="width:25%;"></td>
                                <td style="width:40%;"><b><?php echo "&nbsp;&nbsp;" . $getdata["add2"]; ?></b></td>
                            <?php } else { ?>
                                <td style="width:25%;"></td>
                                <td style="width:40%;"></td>
                            <?php }
                            ?>
                            <td style="width:20%;">DATE</td>
                            <td style="width:15%;"><b><?php echo " : " . $getdata["mydt"]; ?></b></td>
                        </tr>
                        <tr>
                            <?php if ($getdata['add3'] != '') { ?>

                                <td style="width:25%;"></td>
                                <td style="width:40%;"><b><?php echo "&nbsp;&nbsp;" . $getdata["add3"]; ?></b></td>
                            <?php } else { ?>
                                <td style="width:25%;"></td>
                                <td style="width:40%;"></td>
                            <?php }
                            ?>
                            <td style="width:20%;"></td>
                            <td style="width:15%;"></td>
                        </tr>
                        <?php if ($getdata['add4'] != '') { ?>
                            <tr>

                                <td style="width:25%;"></td>
                                <td style="width:40%;"><b><?php echo "&nbsp;&nbsp;" . $getdata["add4"]; ?></b></td>

                                <td style="width:20%;"></td>
                                <td style="width:15%;"></td>
                            </tr>
                            <?php
                        }
                        if ($larchitect['name'] != '') {
                            ?>
                            <tr>
                                <td style="width:25%;">ARCHITECT</td>
                                <td style="width:40%;"><b><?php echo " : " . $larchitect['name']; ?> </b></td>

                                <td style="width:20%;">ARCHITECT NO.</td>
                                <td style="width:15%;"><b><?= " : " . $getdata['archno'] ?></b></td>
                            </tr>
                        <?php } ?>

                        <tr>
                            <td style="width:25%;">SALES EXECUTIVE</td>
                            <td colspan="" style="width:40%;"><b><?php echo " : " . $lsale['name']; ?></b></td>
                            <td colspan="" style="width:20%;"></td>
                            <td colspan="" style="width:15%;"></td>
                        </tr>

                    </tbody>
                </table>
                <div>
                    <table border="solid 1px" style="border-collapse: collapse;  width:100% !important;">
                        <tr style="background-color:#8F4131 !important; -webkit-print-color-adjust: exact;font-size:14px">
                            <th style="width:5% ;">SR. No.</th>
                            <th style="width:5%;text-align:right" class="text-right">QTY.</th>
                            <th style="width:10%;" class="text-center">UNIT</th>
                            <th style="width:30%; text-align:left" class="text-left">DESCRIPTION</th>
                            <th style="width:20%;" class="text-center">SIZE</th>
                            <th style="width:10%;" class="text-center">SQ.FT.</th>
                            <th style="width:10%;" class="text-center">CAL BY</th>
                            <th style="width:20%;text-align:right" class="text-right">RATE</th>
                            <th style="width:20%;text-align:right" class="text-right">AMOUNT</th>
                        </tr>

                        <?php
                        $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 ($row = mysqli_fetch_array($gettran)) {
                            if ($row['qty'] > 0) {
                                $qty = $row['qty'];
                            } else {
                                $qty = "";
                            }
                            if ($row['rate'] > 0) {
                                $rate = $row['rate'];
                            } else {
                                $rate = "";
                            }
                            if ($row['amount'] > 0) {
                                $amount = $row['amount'];
                            } else {
                                $amount = "";
                            }
                            if ($row['calby'] == "P") {
                                $calname = "Pc";
                            } elseif ($row['calby'] == "S") {
                                $calname = "Sq.Ft";
                            } elseif ($row['calby'] == "S") {

                                $calname = "Box";
                            } else {
                                $calname = "";
                            }
                            $ctr++;

                            echo "<tr>";

                            echo "<td style='text-align:center'>$ctr</td>";
                            echo "<td style='text-align:right'>$qty</td>";
                            echo "<td style='text-align:center' >$row[unit]</td>";
                            echo "<td style='text-align:left'>$row[itemname]</td>";
                            echo "<td style='text-align:center'>$row[size]</td>";
                            echo "<td style='text-align:center'>$row[sqft]</td>";
                            echo "<td style='text-align:center'>$calname</td>";
                            echo "<td style='text-align:right'>$rate</td>";
                            echo "<td style='text-align:right'>$amount</td>";
                            echo "</tr>";

                            $ctab = $ctab + 1;
                            $totqty = $showtran['qty'] + $totqty;
                            $totamt = $row['amount'] + $totamt;
                            $totalsqft = $row['sqft'] + $totalsqft;
                            $totsqft = number_format($totalsqft, 2);
                        }
                        if ($ctab < 12) {
                            for ($i = $ctab; $i <= 12; $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>
                                    <td class="border"></td>
                                </tr>
                                <?php
                            }
                        }
                        ?>

                        <tr>
                            <td style="text-align: left;"> <b></b></td>
                            <td style="text-align: right;"> <b><?= $getdata['totalqty'] ?> </b></td>
                            <td style="text-align: left;"> <b></b></td>
                            <td style="text-align: left;"> <b></b></td>
                            <td style="text-align: left;"> <b></b></td>
                            <td style="text-align: right;"> <b><?= $totsqft ?></b></td>
                            <td style="text-align: left;"> <b></b></td>
                            <td style="text-align: left;"> <b></b></td>
                            <td style="text-align: right;"> <b><?= $getdata['grossamt'] ?> </b></td>
                        </tr>
                    </table>
                    <table border="solid 1px" style="border-collapse: collapse;  width:100% !important;">
                        <tr>
                            <td colspan="4">
                                <table border="solid 1px" style="border-collapse: collapse; width:100%">
                                    <tbody style="border-collapse: collapse;">

                                        <tr>
                                            <td style="width:25%;">BANK NAME</td>
                                            <td style="width:75%;"><?= $getcmpname['bankname'] ?></td>
                                        </tr>

                                        <tr>
                                            <td style="width:25%;">A/C. NAME</td>
                                            <td style="width:75%;"><?= $getcmpname['compname'] ?></td>
                                        </tr>
                                        <tr>
                                            <td style="width:25%;">BRANCH</td>
                                            <td style="width:75%;"><?= $getcmpname['branchname'] ?></td>
                                        </tr>
                                        <tr>
                                            <td style="width:25%;">ACCOUNT NO.</td>
                                            <td style="width:75%;"><?= $getcmpname['acno'] ?></td>
                                        </tr>
                                        <tr>
                                            <td style="width:25%;">IFSC CODE</td>
                                            <td style="width:75%;"><?= $getcmpname['ifccode'] ?></td>
                                        </tr>
                                    </tbody>
                                </table>
                            </td>
                            <td colspan="5" style="border:none;border-collapse: collapse; ">
                                <table border="solid 1px" style="border-collapse: collapse; width:100%">
                                    <tbody>

                                        <?php if ($getdata['totdisc'] > 0) { ?>
                                            <tr>
                                                <td style="width:50%;text-align: right">DISCOUNT</td>
                                                <td style="width:50%;text-align: right"><?= $getdata['totdisc'] ?></td>
                                            </tr>
                                        <?php } ?>
                                        <?php if ($getdata['addpack'] > 0) { ?>

                                            <tr>
                                                <td style="width:50%;text-align: right">CARTAGE</td>
                                                <td style="width:50%;text-align: right"><?= $getdata['addpack'] ?></td>
                                            </tr>
                                        <?php } ?>

                                        <?php if ($getdata['addgst'] > 0) { ?>
                                            <tr>
                                                <td style="width:50%;text-align: right">ADD GST</td>
                                                <td style="width:50%;text-align: right"><?= $getdata['addgst'] ?></td>
                                            </tr>
                                        <?php } ?>
                                        <tr>
                                            <td style="width:50%;text-align: right">NET AMOUNT</td>
                                            <td style="width:50%;text-align: right;font-weight:bolder;"><?= $getdata['billamt'] ?></td>
                                        </tr>

                                        <?php if ($getdata['addgst'] > 0 && $getdata['addpack'] > 0 && $getdata['totdisc'] > 0) { ?>
                                            <tr>
                                                <td style="width:50%;">&nbsp;</td>
                                                <td style="width:50%;">&nbsp;</td>
                                            </tr>

                                        <?php } ?>
                                        <?php if ($getdata['totadv'] > 0) { ?>
                                            <tr>
                                                <td style="width:50%;text-align: right">LESS ADVANCE</td>
                                                <td style="width:50%;text-align: right"><?= $getdata['totadv'] ?></td>
                                            </tr>
                                        <?php } ?>
                                        <?php if ($getdata['totbal'] >= 0) { ?>
                                            <tr>
                                                <td style="width:50%;text-align: right">BALANCE</td>
                                                <td style="width:50%;text-align: right;font-weight:bolder;"><?= $getdata['totbal'] ?></td>
                                            </tr>
                                        <?php } ?>


                                    </tbody>
                                </table>
                            </td>
                        <tr>
                        <tr>
                            <td style="border:none"><b>REMARKS&nbsp;&nbsp;</b> <?= $getdata['rem1'] ?></td>
                            <td style="border:none;" colspan="9"></td>
                        </tr>
                        <tr>
                            <td style="border:none"><?= $getdata['rem2'] ?>  </td>
                            <td style="border:none;" colspan="9"></td>
                        </tr>
                    </table>
                </div>
                <br>


                <?php if ($getdata['addpack'] > 0) { ?>
                    <table style="border-collapse: collapse; width:60%">
                        <tr style="background-color:white !important; -webkit-print-color-adjust: exact;">
                            <th style="width:100%; text-align:center">SALE EXPENSE</th>
                        </tr>
                    </table>


                    <table border="solid 1px" style="border-collapse: collapse; width:60%">
                        <tr style="background-color:#8F4131 !important; -webkit-print-color-adjust: exact;font-size:13px">
                            <th style="width:10%; text-align:center">SR. NO.</th>
                            <th style="width:10%; text-align:right">AMOUNT</th>
                            <th style="width:45%; text-align:left">DESCRIPTION</th>
                        </tr>
                        <?php
                        $ctr = 0;
                        $gettran2 = mysqli_query($conn, "SELECT * FROM `saleexp` WHERE `vouchno`='$getdata[vouchno]' and amount > 0 ORDER BY `srno`") or die(mysqli_error($conn));
                        while ($tran2 = mysqli_fetch_array($gettran2)) {

                            $gparty = mysqli_query($conn, "SELECT name FROM `accmast` WHERE `accode`='$tran2[descrip]'") or die(mysqli_error($conn));
                            $getdesc = mysqli_fetch_array($gparty);
                            $ctr++;
                            echo "<tr style>";
                            echo "<td style='text-align:center'>$ctr</td>";
                            echo "<td style='text-align:right'>$tran2[amount]</td>";
                            echo "<td style='text-align:left' >$getdesc[name]</td>";
                            echo "</tr>";
                        }
                        ?>

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

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

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

</body>

</html>