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

$formname = 'CUSTOMER ITEM RATE';
$maindb = 'itemrate';
$trandb = 'itemlink';

$printfile = "custitlst.php";
$invprintfile = "";
$file = 'customeritemrate.php';
$trandbexist = 0;
$keyfld = "id";
$focusfield = "code";

$zseries = $_SESSION['user_series'];
$keycond = "";
$msource = "";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    /*     * Start sale exist check** */
    if ($_GET["mode"] == 'true') {
        $checkSaleExist = mysqli_query($conn, "SELECT `id` FROM `$maindb` WHERE `id`='$qvouch' ") or die(mysqli_error($conn));
        $checkSaleExt = mysqli_num_rows($checkSaleExist);
        if ($checkSaleExt > 0) {
            $a = 0;
        }
    }
    /** End sale exist check** */
    $series = $zseries;
//        $id         = $_POST[id];
    $qvouch = $_POST['id'];
    $code = $_POST['custcode'];
    $name = $_POST['custname'];
    $count = 0;

    if (empty($code)) {
        browse("Code Cannot Be Empty");
        $a = 0;
    }

    if (empty($name)) {
        browse("Name Cannot Be Empty");
        $a = 0;
    }

    if ($a == 1) {
        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(id) AS maxcode FROM `$maindb` LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_assoc($gen1);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
        }
        $mvouch = $qvouch;


        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, `item` WRITE, `ratehist` WRITE ");
        ###################### TRAN PART #####################################################

        $count = $_POST["items"];

        mysqli_query($conn, "DELETE FROM `$trandb` WHERE  custcode='$code'") or die(mysqli_error($conn));
        $cnt = 0;
        for ($i = 0; $i < $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $mitemname = $_POST["itemname_$i"];
            $msize = $_POST["size_$i"];
            $munit = $_POST["unit_$i"];
            $mvat = $_POST["vat_$i"];
            $mrate = $_POST["rate_$i"];

            if (!empty($mitemname)) {
                $cnt = $cnt + 1;
                $getmyitem = mysqli_query($conn, "SELECT * FROM `item` WHERE `name`='$mitemname'") or die(mysqli_error($conn));
                $gmyitem = mysqli_fetch_array($getmyitem);
                $mitemcode = $gmyitem[code];



                mysqli_query($conn, "INSERT INTO `$trandb`(`custcode`, `srno`, `itemcode`, `itemname`, `size`, `unit`, `rate`,  `vat` )
                                    VALUES('$code', '$cnt', '$mitemcode', '$mitemname', '$msize', '$munit', '$mrate', '$mvat')") or die(mysqli_error($conn));

                if ($addflag != 1) {
                    $getoldrate = mysqli_query($conn, "SELECT itemcode, itemname, size, comp, unit, rate, rate AS oldrate, vat, xkey FROM `itemlink`  WHERE `custcode`='$code' and rate != $mrate") or die(mysqli_error($conn));
//                    $countering = mysqli_num_rows($getoldrate);
                    $mdate = date("Y-m-d");
//                    browse($countering);
                    $getrate = mysqli_fetch_array($getoldrate);
//                    while ($getrate = mysqli_fetch_array($getoldrate)) {
//                        $getmyitem = mysqli_query($conn, "SELECT * FROM `item` WHERE `name`='$mitemname'") or die(mysqli_error($conn));
//                        $gmyitem = mysqli_fetch_array($getmyitem);
//                        $mitcode = $gmyitem[code];
//            browse($mitemcode);
                    while ($getrate = mysqli_fetch_array($getoldrate)) {
                        mysqli_query($conn, "INSERT INTO `ratehist`(`custcode`, `date`, `srno`, `itemcode`, `itemname`, `size`, `unit`, `oldrate`, `rate`,  `vat` )
                                    VALUES('$code', '$mdate', '$msrno', '$mitemcode', '$mitemname', '$msize', '$munit', '$getrate[oldrate]', '$mrate', '$mvat')") or die(mysqli_error($conn));
                    }
                }
            }
        }
    }


    if ($addflag == 1) {
        mysqli_query($conn, "INSERT INTO `$maindb`(`id`, `code`, `name`, `bcode` )
                                    VALUES($qvouch, '$code', '$name', '$zseries')") or die(mysqli_error($conn));
        $mvouch = $qvouch; //1
    } else {
        $mvouch = $qvouch; //1

        mysqli_query($conn, "UPDATE `$maindb` SET `code`='$code', `name` = '$name', `bcode`='$zseries'
                         WHERE id = '$mvouch' ") or die(mysqli_error($conn));
    }



    ############################## MAIN  - challan part ######################################################
    mysqli_query($conn, "UNLOCK TABLES");

    if ($addflag == 1) {
        insmess();
        header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
    } else {
        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);

    $getgcust = mysqli_query($conn, "SELECT `custcode`, `name` FROM `customer` WHERE `custcode`='$get[code]'") or die(mysqli_error($conn));
    $gcust = mysqli_fetch_array($getgcust);
//    

    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `custcode` = '$get[code]' order by srno") or die(mysqli_error($conn));
}

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

if (isset($_GET["delete"])) {
//    $delflag = 1;
//    if ($delflag == 1) {

    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE");

    $delcond = " id = '$getvouch' ";
    $delcond1 = " `custcode` = '$get[code]' ";
    delnew($zdb, "$maindb", "$delcond", "$file?mode=last");
    delnew($zdb, "$trandb", "$delcond1", "$file?mode=last");
    mysqli_query($conn, "UNLOCK TABLES");

//    }
}

include("includes/linkshead.php");
?>
<script src="MyScript/customeritemrate.js" type="text/javascript"></script>
</head>
<body>
    <div id="preloader">
        <div><img src="assets/img/main.gif"></div>
    </div>
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-5 pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title" style="color :#05B2EA;"><?php Echo $formname; ?></h3>
                        <div class="actions pull-right" style="margin-right: 50px;">
                            <?php
                            $getmseries = mysqli_query($conn, "SELECT `code`, `name` FROM `series` WHERE `code`='$zseries'") or die(mysqli_error($conn));
                            $mseries = mysqli_fetch_array($getmseries);
                            ?>
                            <label class="label-control"><?php Echo $zseries; ?></label> - <label class="label-control"><?php Echo $mseries[name]; ?></label> 
                        </div>
                        <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 pad text-right'>
                                <label class='label-control tmarg'>Code</label>
                            </div>
                            <div class='col-lg-2 col-md-2 col-sm-2 col-xs-2 pad'>
                                <input type='text' class='form-control form custcode' name='custcode' <?php if ($addflag != 1) { ?>readonly <?php } ?> value='<?php echo ($addflag != 1) ? $get['code'] : ""; ?>' autofocus  required>
                                <input type='hidden'  name='id' value='<?php echo ($addflag != 1) ? $get['id'] : ""; ?>'>
                            </div>
                        </div>
                        <div class='row'>
                            <div class='col-lg-2 col-md-2 col-sm-2 col-xs-2 pad text-right'>
                                <label class='label-control tmarg'>Name</label>
                            </div>
                            <div class='col-lg-9 col-md-9 col-sm-9 col-xs-9 pad'>
                                <select class="fetchpart form-control form custname" id="custname" name="custname">
                                    <option value="<?php Echo ($addflag != 1) ? $gcust[name] : ''; ?>"><?php Echo ($addflag != 1) ? $gcust[name] : ''; ?></option>
                                    <?php
                                    if ($addflag == 1) {
                                        $getcust = mysqli_query($conn, "SELECT `custcode`, `name` FROM `customer` where `custcode` in (select `custcode` from `sercust` where `sercode` = '$zseries') order by `name` ") or die(mysqli_error($conn));
                                        while ($cust = mysqli_fetch_array($getcust)) {
                                            Echo "<option value='$cust[name]'>$cust[name]</option>";
                                        }
                                    }
                                    ?>
                                </select>
                                <!--<input type='text' class='form-control form name' name='name' value='<?php // echo ($addflag != 1) ? $get['name'] : "";        ?>'>-->
                            </div>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
            <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12 ">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title">Item Details</h3>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                        </div>
                    </div>
                    <div class="panel-body"  >
                        <div class='row table-responsive' style="overflow-y: scroll; height: 400px;">
                            <table  class="table" width="100%">
                                <tr>
                                    <th style="width: 5%"></th>
                                    <th style="width: 7%">Sr</th>
                                    <th style="width: 40%">Item Name</th>
                                    <th style="width: 12%">Size</th>
                                    <!--<th style="width: 12%">Act.Size</th>-->
                                    <th style="width: 12%">Unit</th>
                                    <th style="width: 12%;text-align: right;">Rate</th>
                                    <th style="width: 12%;text-align: right;">Vat / Cst</th>
                                    <th style="width: 1%;text-align: right;"></th>
                                </tr> 
                                <tbody id="tabledata">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            Echo "<tr>";
                                            Echo "<td align='center' valign='top'><span class='remove fa fa-times ' name='remove' value='remove' style='display:none;'></span></td>";
                                            Echo "<td><input type='text' value='$tran[srno]'  name='srno_$ctr'  class='form-control form text-right' readonly></td>";
                                            Echo "<td>";
                                            Echo "<select class='form form-control detailpart itemname' name='itemname_$ctr' style='width:100%'>";
                                            Echo "<option value='$tran[itemname]'>$tran[itemname]</option>";

                                            $getditem = mysqli_query($conn, "SELECT `code`, `name` FROM `item` GROUP BY `name` ORDER BY `name` ") or die(mysqli_error($conn));
                                            while ($ditem = mysqli_fetch_array($getditem)) {
                                                Echo "<option value='$ditem[name]'>$ditem[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";

                                            Echo "<td>";
                                            Echo "<select class='form form-control detailpart size' name='size_$ctr' style='width:100%'>";
                                            Echo "<option value='$tran[size]'>$tran[size]</option>";
                                            $getdsize = mysqli_query($conn, "SELECT `size` FROM `item` where `name`='$tran[itemname]' GROUP BY `size` ORDER BY `size` ") or die(mysqli_error($conn));
                                            while ($dsize = mysqli_fetch_array($getdsize)) {
                                                Echo "<option value='$dsize[size]'>$dsize[size]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";

//                                            Echo "<td>";
//                                            Echo "<select class='form form-control detailpart actsize' name='actsize_$ctr' style='width:100%'>";
//                                            Echo "<option value='$tran[actsize]'>$tran[actsize]</option>";
//                                            $getdsize2 = mysqli_query($conn, "SELECT `size` FROM `item` where `name`='$tran[itemname]' GROUP BY `size` ORDER BY `size` ")or die(mysqli_error($conn));
//                                            while ($dsize2 = mysqli_fetch_array($getdsize2)) {
//                                                Echo "<option value='$dsize2[size]'>$dsize2[size]</option>";
//                                            }
//                                            Echo "</select>";
//                                            Echo "</td>";

                                            Echo "<td>";
                                            Echo "<input type='text' value='$tran[unit]' name='unit_$ctr' class='form-control form unit' readonly>";
                                            Echo "</td>";
                                            Echo "<td>";
                                            Echo "<input type='text' style='text-align:right;' value='$tran[rate]' name='rate_$ctr' class='form-control form rate'>";
                                            Echo "</td>";
                                            Echo "<td>";
                                            Echo "<input type='text' style='text-align:right;' value='$tran[vat]' name='vat_$ctr' class='form-control form vat'>";
                                            Echo "</td>";
                                            Echo "<td><input type='text' value=''  name='rowitem_$ctr' class='form-control form rowitem' readonly></td>";

//                                            Echo "<td><input type='text' value='$tran[qty]' name='qty_$ctr'  class='form-control form qty text-right'></td>";

                                            Echo "</tr>";
                                            $ctr++;
                                        }
                                    } else {
                                        $pctr = 0;
                                        $yctr = 0;
                                        for ($i = $pctr; $i < 10; $i++) {
                                            $yctr++;
                                            echo "<tr>";
                                            Echo "<td align='center'><span class='remove fa fa-times ' name='remove' value='remove' style='display:none;'></span></td>";
                                            Echo "<td><input type='text' value='$yctr'  name='srno_$pctr'  class='form-control form text-right'  readonly></td>";
                                            Echo "<td>";
                                            Echo "<select class='form form-control detailpart itemname' name='itemname_$pctr' style='width:100%'>";
                                            Echo "<option value=''></option>";

                                            $getditem = mysqli_query($conn, "SELECT `code`, `name` FROM `item` GROUP BY `name` ORDER BY `name` ") or die(mysqli_error($conn));
                                            while ($ditem = mysqli_fetch_array($getditem)) {
                                                Echo "<option value='$ditem[name]'>$ditem[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";

                                            Echo "<td>";
                                            Echo "<select class='form form-control detailpart size' name='size_$pctr' style='width:100%'>";
                                            Echo "<option value=''></option>";
                                            $getdsize = mysqli_query($conn, "SELECT `size` FROM `item` where `name`='$ditem[name]' GROUP BY `size` ORDER BY `size` ") or die(mysqli_error($conn));
                                            while ($dsize = mysqli_fetch_array($getdsize)) {
                                                Echo "<option value='$dsize[size]'>$dsize[size]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";

//                                            Echo "<td>";
//                                            Echo "<select class='form form-control detailpart actsize' name='actsize_$pctr' style='width:100%'>";
//                                            Echo "<option value='$tran[actsize]'>$tran[actsize]</option>";
//                                            $getdsize2 = mysqli_query($conn, "SELECT `size` FROM `item` where `name`='$tran[itemname]' GROUP BY `size` ORDER BY `size` ")or die(mysqli_error($conn));
//                                            while ($dsize2 = mysqli_fetch_array($getdsize2)) {
//                                                Echo "<option value='$dsize2[size]'>$dsize2[size]</option>";
//                                            }
//                                            Echo "</select>";
//                                            Echo "</td>";

                                            Echo "<td>";
                                            Echo "<input type='text' value='' name='unit_$pctr' class='form-control form unit' readonly>";
                                            Echo "</td>";
                                            Echo "<td>";
                                            Echo "<input type='text' style='text-align:right;' value='' name='rate_$pctr' class='form-control form rate'>";
                                            Echo "</td>";
                                            Echo "<td>";
                                            Echo "<input type='text' style='text-align:right;' value='' name='vat_$pctr' class='form-control form vat'>";
                                            Echo "</td>";
                                            Echo "<td><input type='text' value=''  name='rowitem_$pctr' class='form-control form rowitem' readonly></td>";
                                            echo "</tr>";
                                            $pctr++;
                                        }
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="row">
<!--                            <input type="button" style="height: 30px; width: 30px; padding: 0px" id="addnew" class="btn btn-success btn-lg form" accode="addnew2" value="+" />-->
                            <button type="button" class="btn btn-success btn-square form"  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>
                </div>
            </div>
        </div>
    </form>

    <!--search part-->
    <div class="modal fade bs-example-modal-lg" 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">
                    <div class=" table-responsive">
                        <table id="example1" style="width: 100%;" class="table table-striped table-bordered">
                            <thead>
                                <tr style="background-color: #05B2EA">

                                    <th>Code</th>
                                    <th>Customer Name </th>
                                    <!--<th>Action</th>-->
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld!='' ORDER BY 'id' ") or die(mysqli_error($conn));
                                while ($row = mysqli_fetch_array($srchresult)) {


                                    echo "<tr>";
                                    echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\">$row[code]</a></td>";
                                    echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\">$row[name]</a></td>";
//                                    echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\"><i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></a></td>";
                                    echo "</tr>";
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <!--search part-->
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
