<?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);
    
    
//$getparty = mysqli_query($conn, "SELECT `accode`,`name` From accmast WHERE `accode` = '$getdata[party]'")or die(mysqli_error($conn));
//$showparty = mysqli_fetch_array($getparty);
//include("includes/linkshead.php");  
?>

<!DOCTYPE HTML>
<html lang="en-US">
    <style>
        body {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            /*background-color: #FAFAFA;*/
            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;
            /*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 onclick="window.print();return false;" class="btn btn-primary noPrint" style="width: 150px;height:35px;background-color: #A0B1BA;color:white;">Print</button>
        &nbsp;&nbsp;&nbsp;
        <button class="btn btn-primary goBack noPrint" onclick="window.close()"  style="width: 150px;height:35px;background-color: white;color:#A0B1BA;padding-left: 1%">Go Back</button>
        <div class="page">
            <div class="">
            <div class="row" style="text-align:center;">
        <img style="width:50%; height:110px;width:290px;" src="<?=$logo?>">
            </div >
                
                <div class="row" style="text-align:center;">
                    <b style="font-size:14px;">
                    <?php echo $getcmpname['hoadd1'] . " " . $getcmpname['hoadd2'] . " " .$getcmpname['hoadd3'] . " " . $getcmpname['hocity'] . " " . $getcmpname['hopin']?><br>
                    </b>
                    <b style="font-size:14px">
                                   <?php echo "Tel : ". $getcmpname['htel'] . " . Email : " . $getcmpname['email'] ?><br>
                    </b>
                    <br>
                    <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 colspan="" style="width:50%;">M/s. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <b><?php echo $lparty["name"]; ?></b></td>
                            <?php } else {?>
                            <td colspan="" style="width:50%;">M/s. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <b><?php echo $getdata["ntprint"]; ?></b></td>
                            <?php  } ?>
                            <td style="width:50%; text-align: right;">No. : <b><?php echo $getdata['vouchno']; ?></b>&nbsp;&nbsp;&nbsp;&nbsp;
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp</td>

                        </tr>
                        <tr>
                            <td colspan="" style="width:50%;">Architect / Site : <b><?php echo $larchitect['name']; ?></b></td>
                            <td colspan="" style="width:50%;text-align: right;">
                                <?php if ( $getdata['mobile']  > 0 ){  
                                     echo " Mobile No.: <b>$getdata[mobile]";                                   
                                } else {
                                    echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                }
                            ?></b></td>
                        </tr>
                        <tr>
                            <td colspan="" style="width:50%;">Salesman &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <b><?php echo $lsale['name']; ?></b></td>
                            <td colspan="" style="width:50%;text-align: right;">Date : <b><?php echo $getdata['mydt']; ?></b></td>
                        </tr>
<!--                        <tr>
                            <td colspan="" style="width:50%;">Mobile No. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <b><?php echo $getdata['mobile']; ?></b></td>
                            <td colspan="" style="width:50%;text-align: right;"></td>
                        </tr>-->
                    </tbody>
                </table>
                <div>
                    <table  border="solid 1px" style="border-collapse: collapse; width:100%">
                        <tr style="background-color:#CEA962 !important; -webkit-print-color-adjust: exact;">
                            <th style="width:5%">Sr.</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:10%" 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:10%;text-align:right" class="text-right">Rate</th>
                            <th style="width:10%;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 = "Pcs";
                                            } 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 style="">
                            <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>

                        <tr>
                        <td colspan="4">
                            <table  border="solid 1px" style="border-collapse: collapse; width:100%">
                                <tbody style="border-collapse: collapse;">

                                    <tr>
                                        <td style="width:35%;">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['acname'] ?></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 style="">
                                
                                <?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 }?> 
                                

                            </tbody >
                            </table>
                            </td>
                   <tr>
                <?php // if ($getdata['rem1'] > 0) { ?>
                   <tr>
                       <td style="text-align: right;border:none"><b>Remarks&nbsp;&nbsp;   </b> </td>
                        <td colspan= "9" style="text-align:left;"> <?= $getdata['rem1']?></td>
                    </tr>
                   <tr>
                       <td style="text-align: right;border:none "><b></b></td>
                        <td colspan= "9" style="text-align:left;"> <?= $getdata['rem2']?></td>
                    </tr>
                <?php //   } ?>
                    </table>
                </div>


                
<?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:#CEA962 !important; -webkit-print-color-adjust: exact;">
                            <th style="width:5%; text-align:right">Sr.</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:right'>$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>
