<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$formname = 'Quotation';
$maindb = 'quotmain';
$trandb = 'quottran';

$printfile = "quotation_print.php";
$file = 'quotation.php';
$trandbexist = 1;
$keyfld = "vouchno";
$source = "QUOT";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$compcd = $_SESSION["comp_" . $dbname];


$zroot = $_SESSION["zdir"];

mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchdt = $_POST["vouchdt"];
    $client = $_POST["client"];
    $clientno = $_POST["clientno"];
    $architect = $_POST["architect"];
    $archno = $_POST["archno"];
    $site = $_POST["site"];
    $createby = $_POST["createby"];
    $remark = $_POST["remark"];
    $remark2 = $_POST["remark2"];
    $status = $_POST["status"];
    $appdate = $_POST["appdate"];

    $totamt = $_POST["totamt"];

    $count = $_POST["items"];

    $mvouch = $qvouch;

    $salecode = 11;



    if ($a == 1) {

        #################################TRAN PART #################################



        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `$keyfld`='$mvouch'") or die(mysqli_error($conn));

        $srl = 0;
        $batch = date("Y-m-d") . $mvouch;

        for ($i = 0; $i <= $count; $i++) {

            $srno = $_POST["srno_$i"];
            $boxqty = $_POST["boxqty_$i"];
            $unitbox = $_POST["unitbox_$i"];
            $particulars = $_POST["particulars_$i"];
            $qty = $_POST["qty_$i"];
            $unit = $_POST["unit_$i"];
            $rate = $_POST["rate_$i"];
            $amount = $_POST["amount_$i"];


            $group = $source . $barcode;


            $mqty = $qty * -1;

            $group = $source . $mvouch;

            if (!empty($particulars)) {
                $srl++;
                mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`, `vouchdt`, `srno`, `boxqty`, `unitbox`, 
                                                           `particulars`, `qty`, `unit`, `rate`, `amount`)
                                                    VALUES( '$mvouch', '$vouchdt', '$srl', '$boxqty', '$unitbox',
                                                           '$particulars', '$qty', '$unit', '$rate', '$amount' )") or die(mysqli_error($conn));
            }
        }

        #################################TRAN PART END #################################
        ############################## MAIN ######################################################
        if ($addflag == 1) {

            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `client`, `clientno`, `architect`, `archno`, `totamt`, `remark`, `remark2`, `status`, `appdate`, `site`, `createby`)
                                       VALUES ($qvouch, '$vouchdt',  '$client', '$clientno','$architect', '$archno', '$totamt', '$remark', '$remark2', '$status', '$appdate', '$site', '$createby')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch; //1
            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt', `client`='$client', `clientno`='$clientno', `architect`='$architect', `archno`='$archno', `totamt`='$totamt', `remark`='$remark', `remark2`='$remark2', `status`='$status', `appdate`='$appdate', `site`='$site', `createby`='$createby' WHERE `vouchno`= '$mvouch' ") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
    }
}

if ($addflag != 1) {

    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
}

$senable = "";
$cenable = "";

if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
}

include("includes/linkshead.php");
?>
<!--<script src="MyScript/search_item.js?a=2" type="text/javascript"></script>-->
<script src="MyScript/quotation.js?a=2" type="text/javascript"></script>
<script src="MyScript/quotation_search.js?a=2" type="text/javascript"></script>
</head>

<body>
    <form action='' method='POST'>
        <div>
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12  pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title" style="color:inherit"><?php echo $formname; ?></h3>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control text-size form" name="vouchno" value="<?php echo ($addflag != 1) ? $get["vouchno"] : "$qvouch"; ?>" required readonly>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-4 col-xs-4 pad">
                                <input type="date" class="form-control form focusfield" name="vouchdt" value="<?php echo ($addflag != 1) ? $get["vouchdt"] : date('Y-m-d'); ?>" autofocus>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Client</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4  pad">
                                <input list='client' name='client' value='<?php echo $get['client'] ?>' class='client form-control form ' autocomplete="off">
                                <datalist id='client'>
                                    <option value=''></option>
                                    <?php
                                    $mhsncode = mysqli_query($conn, "SELECT `client` FROM `estmain` where client!='' group by client") or die(mysqli_error($conn));
                                    while ($thsncode = mysqli_fetch_array($mhsncode)) {
                                        echo "<option value='$thsncode[client]'>";
                                    }
                                    ?>
                                </datalist>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Cl. No.</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-4 col-xs-4 pad">
                                <input type="text" name="clientno" class="form-control form clientno" value="<?php echo ($addflag != 1) ? $get['clientno'] : '' ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Architect</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4  pad">
                                <input list='architect' name='architect' value='<?php echo $get['architect'] ?>' class='architect form-control form ' autocomplete="off">
                                <datalist id='architect'>
                                    <option value=''></option>
                                    <?php
                                    $marchitect = mysqli_query($conn, "SELECT `architect` FROM `estmain` where architect!='' group by architect") or die(mysqli_error($conn));
                                    while ($tarchitect = mysqli_fetch_array($marchitect)) {
                                        echo "<option value='$tarchitect[architect]'>";
                                    }
                                    ?>
                                </datalist>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Ar. No.</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-4 col-xs-4  pad">
                                <input type="text" name="archno" class="form-control form archno" value="<?php echo ($addflag != 1) ? $get['archno'] : '' ?>">
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Site</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-4 col-xs-4  pad">
                                <input type="text" name="site" class="form-control form site" value="<?php echo ($addflag != 1) ? $get['site'] : '' ?>">
                            </div>
                            <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Created</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-4 col-xs-4  pad">
                                <input type="text" name="createby" class="form-control form createby" value="<?php echo ($addflag != 1) ? $get['createby'] : '' ?>">
                            </div>
                        </div>
                        <div class="table-responsive">
                            <table class="table table-striped table-condensed table-bordered table-padd" width="100%">
                                <thead class="mySearchTr">
                                    <tr>
                                        <th width="2%"></th>
                                        <th width="3%" class='text-right'>Sr.</th>
                                        <!--                                        <th width="5%" class='text-right'>Box Qty.</th>
                                        <th width="5%">Sq.ft</th>-->
                                        <th width="15%">Particular</th>
                                        <th width="5%">Size.</th>
                                        <th width="5%">Sq.ft</th>
                                        <!--                                        <th width="5%"  class='text-right'>Rate</th>
                                        <th width="5%"  class='text-right'>Amount</th>-->
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        $mctr = 0;
                                        $tqty = 0;
                                        $tamt = 0;
                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$get[vouchno]' ORDER BY `srno`") or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            $mctr++;
                                            echo "<tr>";
                                            echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                            echo "<td><input type='text' class='form-control form text-right srno'  name='srno_$ctr'     value='$mctr'></td>";
                                            //                                            Echo "<td><input type='text' class='form-control form text-right boxqty'         name='boxqty_$ctr' value='$tran[boxqty]'></td>";
                                            //                                            Echo "<td><input type='text' class='form-control form unitbox'          name='unitbox_$ctr'  value='$tran[unitbox]'></td>";
                                            echo "<td><input type='text' class='form-control form particulars'         name='particulars_$ctr' value='$tran[particulars]'></td>";
                                            echo "<td><input type='text' class='form-control form '                 name='qty_$ctr' value='$tran[qty]'></td>";
                                            echo "<td><input type='text' class='form-control form unit'          name='unit_$ctr'  value='$tran[unit]'></td>";
                                            //                                            Echo "<td><input type='text' class='form-control form text-right rate'          name='rate_$ctr'  value='$tran[rate]'></td>";
                                            //                                            Echo "<td><input type='text' class='form-control form text-right amount'          name='amount_$ctr'  value='$tran[amount]'></td>";
                                            echo "</tr>";
                                            $ctr++;
                                        }
                                    } else {
                                        $pctr = 0;
                                        echo "<tr>";
                                        echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                        echo "<td><input type='text' class='form-control form text-right srno'  name='srno_0'     value='1'></td>";
                                        //                                        Echo "<td><input type='text' class='form-control form text-right boxqty'         name='boxqty_0' value=''></td>";
                                        //                                        Echo "<td><input type='text' class='form-control form unitbox'          name='unitbox_0'  value=''></td>";
                                        echo "<td><input type='text' class='form-control form particulars'         name='particulars_0' value=''></td>";
                                        echo "<td><input type='text' class='form-control form '         name='qty_0' value='0'></td>";
                                        echo "<td><input type='text' class='form-control form unit'          name='unit_0'  value=''></td>";
                                        //                                        Echo "<td><input type='text' class='form-control form text-right rate'          name='rate_0'  value='0'></td>";
                                        //                                        Echo "<td><input type='text' class='form-control form text-right amount'          name='amount_0'  value='0'></td>";
                                        echo "</tr>";
                                    }
                                    $pctr++;
                                    ?>
                                </tbody>
                            </table>

                        </div>

                        <div class="row">
                            <div class="col-lg-1">
                                <button type="button" class="btn btn-square form mySearchTr" id="addnew">
                                    <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>
                                </button>
                                <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : $pctr; ?>" />
                            </div>
                            <!--                            <div class="col-lg-10 col-md-1 col-sm-3 col-xs-3 text-right pad" style="" >
                                <label class="label-control text-size marg">Total &nbsp;</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-8 col-xs-8 pad" style="">
                                <input type="text" class="form-control form text-right totamt" name="totamt" value="<?php echo ($addflag != 1) ? $get["totamt"] : ""; ?>" readonly>&nbsp;
                            </div>
                            <br>
                            <br>-->
                        </div>
                        <div class="row">
                            <div class="col-lg-1 col-md-1 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control text-size marg">Remark</label>
                            </div>
                            <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form " name="remark" value="<?php echo ($addflag != 1) ? $get["remark"] : ""; ?>">
                                <input type="text" class="form-control form " name="remark2" value="<?php echo ($addflag != 1) ? $get["remark2"] : ""; ?>">
                            </div>
                            <!--                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Status</label>
                            </div>
                            <div class="col-lg-2 col-md-1 col-sm-2 col-xs-2 pad">
                                <select name="status" class="form-control form mode status select2">
                                    <option <?php
                                            if ($get['status'] == 'P') {
                                                echo "selected='selected'";
                                            }
                                            ?> value="P">Pending</option>
                                    <option <?php
                                            if ($get['status'] == 'A') {
                                                echo "selected='selected'";
                                            }
                                            ?> value="A">Approved</option>
                                </select>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1  pad">
                                <label class="label-control text-size marg applbl">Apprv. Date</label> 
                            </div>
                            <div class="col-lg-2 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <input type="date" step="any" name="apprvdt" class="form-control form  apprvdt" value="<?php echo ($addflag != 1) ? $get['apprvdate'] : "" ?>" >
                            </div>-->
                        </div>

                    </div>
                </div>
            </div>
        </div>

        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
            <div class="panel panel-default">
                <div class="panel-body">
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <img src="MyStyle/pdficon.png" title="Pdf Print" class="btnpdf pdf" style="height:50px;display: <?= $editdisabled ?>" onclick="window.open('quotation_pdf.php?vouchno=<?= $qvouch ?>')">
                        </div>
                        <div class="col-lg-7 col-md-7 col-sm-8 col-xs-8 text-right pad">
                            <?php include("includes/buttons.php"); ?>
                        </div>

                    </div>

                </div>
            </div>
        </div>
        </div>
    </form>
    <div class="modal fade itemModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search <? echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <input type="hidden" value="<?php // Echo $fseries 
                                                ?>" class="form-control series">
                    <input type="hidden" value="<?php echo $get['vouchno']; ?>" class="form-control vouchno">
                    <input type="hidden" value="<?php echo $file ?>" class="form-control file">
                    <!--<input type="hidden" value="<?php echo $para ?>" class="form-control para">-->
                    <div class="row">
                        <div class="col-lg-3">
                            <select class="form-control ifield">
                                <option value="1">Product Name</option>
                                <option value="2">Size</option>
                                <option value="3">Brand</option>
                                <option value="4">Quality</option>
                                <option value="5">Code</option>
                                <!--                            <option value="4">Gram</option>
                                                        <option value="5">Unit</option>-->
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control ioperator">
                                <option value=""></option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-4">
                            <input type="text" value="" class="form-control isearch">
                        </div>
                        <div class="col-lg-3">
                            <input type="button" class="btn btn-primary iload" value="Search">
                        </div>
                    </div>

                    <div class="col-lg-12 iloader text-center">
                        <img src="images/search.gif" alt="loading">
                    </div>
                    <div class="col-lg-12 inoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table id="" class="table table-striped table-condensed " width="100%">
                            <thead>
                                <tr class="mySearchTr">
                                    <th width="20%">Product Name</th>
                                    <th width="10%">Size</th>
                                    <th width="10%">Brand</th>
                                    <th width="10%">Quality</th>
                                    <th width="10%">Itemcode</th>
                                    <th width="10%">Stk.</th>
                                    <th width="10%">Prod Type</th>

                                </tr>
                            </thead>
                            <tbody class="iTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button" class="btn btn-primary iprevious" value="Previous">
                            <input type="button" class="btn btn-primary inext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <div class="modal fade bs-example-modal-lg searchModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search <? echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <input type="hidden" value="<?php echo $zseries ?>" class="form-control series">
                    <input type="hidden" value="<?php echo $get['code']; ?>" class="form-control vouchno">
                    <input type="hidden" value="<?php echo $file ?>" class="form-control file">
                    <input type="hidden" value="<?php echo $para ?>" class="form-control para">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="1">Voucher</option>
                                <option value="2">Date</option>
                                <option value="3">Client</option>
                                <option value="4">Client No</option>
                                <option value="5">Architect</option>
                                <option value="6">Architect No</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control soperator">
                                <option value="">Type of Search</option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control sortBy">
                                <option value="">Sort By</option>
                                <option value="1">Voucher</option>
                                <option value="2">Date</option>
                                <option value="3">Client</option>
                                <option value="4">Client No</option>
                                <option value="5">Architect</option>
                                <option value="6">Architect No</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="desc">Desc</option>
                                <option value="asc">Asc</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <input type="text" value="" class="form-control ssearch" placeholder="Search Here">
                        </div>
                        <div class="col-lg-2">
                            <input type="button" class="btn btn-primary sload" value="Search">
                        </div>
                    </div>
                    <div class="col-lg-12 sloader text-center">
                    </div>
                    <div class="col-lg-12 snoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table id="" class="table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr class="mySearchTr">
                                    <th style="width:8%">Voucher</th>
                                    <th style="width:13%">Date</th>
                                    <th style="width:20%">Client</th>
                                    <th style="width:10%">Client No</th>
                                    <th style="width:20%">Architect</th>
                                    <th style="width:10%">Architect No</th>
                                    <th style="width:5%">Action</th>
                                </tr>
                            </thead>
                            <tbody class="sTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button" class="btn btn-primary sprev" value="Previous">
                            <input type="button" class="btn btn-primary snext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>

</html>