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

$formname = 'JOB GIVEN';
$maindb = 'jobgiven';
//$printfile = "cash_print.php";
$file = 'jobgiven.php';
$trandbexist = 0;
$grid = '';
$keyfld = "vouchno";
//$focusfield = "name";
//$mgrpcode = '002';
//$keycond1 = " AND `grpcode`='002' ";
//$keycond = "";
$myyear = $_SESSION[$myyear];


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

    $mvouch = $_POST["vouchno"];
    
    $vouchno = $_POST["vouchno"];
    $vouchdt = $_POST["vouchdt"];
//    $time = $_POST["time"];
    $saleorddate = $_POST["saleorddate"];
    $party = $_POST["party"];
    $prodcode = $_POST["prodcode"];
    $colorname = $_POST["colorname"];
    $process = $_POST["process"];
    $karigar = $_POST["karigar"];
    $expectdate = $_POST["expectdate"];
    $qty = $_POST["qty"];
    $rate = $_POST["rate"];
    $billamt = $_POST["billamt"];
    $remarkl = $_POST["remark1"];
    $remark2 = $_POST["remark2"];


    $mvouch = $qvouch;
    $increcode = $myyear . $mvouch;

    if ($party == " ") {
        $a = 0;
        browse("Name Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?mode=last");
        }
    }

    if (isset($_POST["party"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `party`= '$party' AND vouchno != '$mvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Name Already Exist");
            header("refresh: 1; $file?mode=last");
        }
    }


    if ($a == 1) {
        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX($keyfld) 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 = '51';
            }
        }
        
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `saleorddate`, `party`, `prodcode`, `colorname`, `process`, `karigar`,
                                                     `expectdate` , `qty` , `rate` , `billamt` , `remark1` , `remark2`)
                                                VALUES($mvouch, '$vouchdt', '$saleorddate', '$party', '$prodcode', '$colorname', '$process' ,'$karigar',"
                                                      . "'$expectdate', '$qty' , '$rate', '$billamt' , '$remark1' , '$remark2')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $_GET["vouchno"];
            $mvouch = $qvouch;
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET `vouchno` = '$vouchno',`vouchdt` = '$vouchdt',`saleorddate`='$saleorddate',`party`='$party', `prodcode`='$prodcode', colorname='$colorname', `process`='$process',"
                                                                  . "`kairgar` = '$karigar', `expectdate` = '$expectdate', `qty` = '$qty', `rate` = '$rate',"
                                                                  . "`billamt` = '$billamt', `remark1` = '$remark1', `remark2` = '$remark2' 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");
}

include("includes/linkshead.php");
?>
<script src="MyScript/cash.js" type="text/javascript"></script>
<script>

    $(document).ready(function () {

        $("body").on("keyup, change", ".dc", validation);
        function validation() {
            var val = $(".dc").val();
            if (val != "C" && val != "D") {
                $(".dc").val("");
                $(".openbal").val("");
                alert("Only D or C should be entered");
            }
        }
    });

</script>
</head>
<body>
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-10">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?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-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Voucher No</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="hidden" class="form-control form vouchno" value="<?php Echo ($addflag != 1) ? $qvouch : $qvouch; ?>" >
                                <input type="number" class="form-control form vouchno" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : ""; ?>">
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="date" class="form-control form vouchdt" name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : ""; ?>">
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Sale Order</label>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="number" class="form-control form saleorder" name="saleorder" value="<?php Echo ($addflag != 1) ? $get["saleorder"] : ""; ?>">
                            </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">Customer</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control form party" name="party" value="<?php Echo ($addflag != 1) ? $get["party"] : ""; ?>" >
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Sale Ord Date</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="date" class="form-control form saleorddate" name="saleorddate" value="<?php Echo ($addflag != 1) ? $get["saleorddate"] : ""; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 numeric pad">
                                <label class="label-control marg">Product</label>
                            </div>
                            <div class="col-lg-3 col-md-6 col-sm-6 col-xs-6 pad">
                                <select class="form-control form select2" name="prodcode" style="width:100%">
                                    <option value="<?php echo ($addflag != 1) ? get['prodcode'] : ""; ?>"><? echo ($addflag != 1) ? $get['prodcode'] : ""; ?></option>
                                    <?php
                                    $gitemgrp = mysqli_query($conn, "SELECT * FROM `item` where code = '$get[prodcode]' ")or die(mysqli_error($conn));
                                    while ($itemgrp = mysqli_fetch_object($gitemgrp)) {
                                        Echo "<option value='$itemgrp->code'>$itemgrp->name</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                            
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Color</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="colorname" class="form-control form colorname"  value="<?php echo ($addflag != 1) ? $get['colorname'] : ""; ?> " />
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Process</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="process" class="form-control form process"  value="<?php echo ($addflag != 1) ? $get['process'] : ""; ?> " />
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Karigar / Staff</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="karigar" class="form-control form karigar"  value="<?php echo ($addflag != 1) ? $get['karigar'] : ""; ?> " />
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Expected Date</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="date" class="form-control form expectdate" name="expectdate" value="<?php Echo ($addflag != 1) ? $get["expectdate"] : ""; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Rate</label>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="rate" class="form-control form text-right rate"  value="<?php echo ($addflag != 1) ? $get['rate'] : ""; ?> " />
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Qty.</label>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="qty" class="form-control form text-right qty"  value="<?php echo ($addflag != 1) ? $get['qty'] : ""; ?> " />
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Amount</label>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="billamt" class="form-control form text-right billamt"  value="<?php echo ($addflag != 1) ? $get['billamt'] : ""; ?> " />
                            </div>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <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" class="table table-striped table-bordered">
                            <thead>
                                <tr class="mySearchTr">
                                    <th>Name</th>
                                    <th>Init</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($addflag != 1) {
                                    $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `grpcode`='$mgrpcode'")or die(mysqli_error($conn));
                                    while ($row = mysqli_fetch_array($srchresult)) {
                                        echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                        echo "<td align='left'>$row[name]</td>";
                                        echo "<td align='left'>$row[init]</td>";
                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\"><i class='fa fa-pencil' style='font-size:1.3em;color:#64CCFC;cursor:pointer;'></i></a></td>";
                                        echo "</tr>";
                                    }
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
