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

$formname = 'Purchase Order';
$maindb = 'pordmain';
$trandb = 'pordtran';
$accdb = 'actran';
$printfile = "purord_print.php";
$file = 'purchord.php';
$trandbexist = 1;
$keyfld = "vouchno";

$source = "PORD";
//$keycond = " AND `credtype`='$zcredtype' ";

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

if (isset($_POST["submit"])) {
    $vouchdt = trim($_POST["vouchdt"]);
    $invdate = trim($_POST["invdate"]);
    $voucher = trim($_POST["voucher"]);
    $party = trim($_POST["party"]);
    $debitac = $_POST["debitac"];
    $mono = $_POST["monno"];
    $godown = trim($_POST["godown"]);
    $gsttype = trim($_POST["gsttype"]);
    $rem1 = trim($_POST["remark1"]);
    $rem2 = trim($_POST["remark2"]);
    $grossamt = trim($_POST["grossamt"]);
    $discperc = trim($_POST["discperc"]);
    $totdiscamt = trim($_POST["totdiscamt"]);
    $cgstamt = trim($_POST["cgstamt"]);
    $sgstamt = trim($_POST["sgstamt"]);
    $igstamt = trim($_POST["igstamt"]);
    $taxableamt = trim($_POST["taxableamt"]);
    $totbillamt = trim($_POST["totbillamt"]);
    $purtype = trim($_POST["purtype"]);
    $transport = trim($_POST["transport"]);
    $oms = $_POST['oms'];

    $count = $_POST["items"];

    $mvouch = $qvouch;

    $salecode = 15;
    $a = 1;

    if ($a == 1) {
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, `product` WRITE,
                    `$accdb` WRITE, `accmast` WRITE, `gsttype` WRITE,`godown` WRITE, `transport` WRITE ");

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(vouchno+0) AS maxcode FROM `$maindb` where vouchno != ''  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;
        }


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

        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `$keyfld`='$mvouch'") or die(mysqli_error($conn));
        $group = $source . $mvouch;
        $srl = 0;
        for ($i = 0; $i < $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $descrip = $_POST["descrip_$i"];
            $qty = $_POST["qty_$i"];
            $rate = $_POST["rate_$i"];
            $perpcs = $_POST["perpcs_$i"];
            $amount = $_POST["amount_$i"];

            if (!empty($descrip)) {
                $srl = $srl + 1;

                mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`, `vouchdt`, `srno`, `descrip`, `amount`, `qty`, `rate`, `perpcs`) 
                    VALUES ('$qvouch', '$vouchdt', '$srl', '$descrip', '$amount', '$qty', '$rate', '$perpcs')") or die(mysqli_error($conn));
            }
        }



        ############################## MAIN ######################################################
        if ($addflag == 1) {
            $newvoucher = $mono . $mvouch;

            mysqli_query($conn, "INSERT INTO `$maindb`(vouchno, `vouchdt`, `voucher`, `invno`, `invdate`,`monno`, `party`, `oms`, `debitac`,
                                               `rem1`, `rem2`, `discamt`,`grossamt`, `godown`, `purtype`, `transport`,
                                               `taxableamt`, `gsttype`, `discperc`, `cgstamt`, `sgstamt`, `igstamt`, `salecode`, `billamt`)
                                      VALUES('$mvouch', '$vouchdt', '$newvoucher', '$mvouch', '$invdate','$mono', '$party', '$oms', '$debitac',
                                              '$rem1', '$rem2', '$totdiscamt','$grossamt', '$godown', '$purtype', '$transport',
                                            '$taxableamt', '$gsttype', '$discperc', '$cgstamt', '$sgstamt', '$igstamt','$salecode','$totbillamt')") 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', `invno`='$mvouch', `invdate`='$invdate',`monno`='$mono', `party`='$party', `purtype`='$purtype',
                                                `oms`='$oms',`debitac`='$debitac',`rem1`='$rem1', `rem2`='$rem2', `discamt`='$totdiscamt',`grossamt`='$grossamt',
                                                `godown`='$godown', `taxableamt`='$taxableamt', `gsttype`='$gsttype', `discperc`='$discperc', `cgstamt`='$cgstamt',
                                                `sgstamt`='$sgstamt', `igstamt`='$igstamt', `billamt`='$totbillamt', `transport`='$transport' 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);

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

    $getgst = mysqli_query($conn, "SELECT * FROM `gsttype` WHERE per='$get[gsttype]'") or die(mysqli_error($conn));
    $fetgst = mysqli_fetch_array($getgst);

    $getdebac = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$get[debitac]' and grpcode='004'") or die(mysqli_error($conn));
    $ldebitac = mysqli_fetch_array($getdebac);

    $getgodown = mysqli_query($conn, "SELECT * FROM `godown` WHERE `code`= '$get[godown]'") or die(mysqli_error($conn));
    $getgd = mysqli_fetch_array($getgodown);

    $gettrans = mysqli_query($conn, "SELECT * FROM `transport` WHERE `code`= '$get[transport]'") or die(mysqli_error($conn));
    $ltrans = mysqli_fetch_array($gettrans);
}

$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]' ";
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last");
}

include("includes/linkshead.php");
?>
<script src="MyScript/purchord.js" type="text/javascript"></script>
<script src="MyScript/purchord_search.js" type="text/javascript"></script>
</head>
<body>
    <form action='' method='POST'>
        <div class="col-lg-3 pad text-center" id="overlay_form" style="display:none;z-index:999;">
            <button type="button" id="close_form" style="float: right;font-weight: bold;font-size: 18px;color:red;"><i class="fa fa-close"></i></button>
            <div style='width:100%;' id='browdata'>
            </div>
        </div>
        <!-- Default box -->
        <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:#1ABC9C"><?php Echo $formname; ?></h3>
                    </div>
                    <div class="panel-body">
                        <div class="row">

                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="text" name="voucher" class="form-control form voucher" value="<?php echo ($addflag != 1) ? $get['voucher'] : '' ?>" tabindex="-1" readonly>
                                <input type="hidden" name="monno" class="form-control form monno" value="<?php echo ($addflag != 1) ? $get['monno'] : '' ?>" >
                                <input type="hidden" name="vouchno" class="form-control form vouchno" value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch ?>" >
                            </div>
                            <div class="col-lg-1 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-2 col-sm-1 col-xs-8 pad">
                                <input type="date" class="form-control form vouchdt" name="vouchdt"  value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" autofocus>
                            </div>

                            <div class="col-lg-1 pad text-right">
                                <label class="label-control marg">Party</label>
                            </div>
                            <div class="col-lg-2 pad">
                                <select class="form-control form cmbparty select2" name="party" style="width: 100%" required>
                                    <option value="<?php Echo ($addflag != 1) ? $lparty["accode"] : ""; ?>"><?php Echo ($addflag != 1) ? $lparty["name"] : ""; ?></option>
                                    <?php
                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `grpcode`='009' order by name")or die(mysqli_error($conn));
                                    while ($party = mysqli_fetch_array($gparty)) {
                                        Echo "<option value='$party[accode]'>$party[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="col-lg-1 pad text-right">
                                <label class="label-control marg">Type</label>
                            </div>
                            <div class="col-lg-1 pad">
                                <select class="form-control form purtype select2" name="purtype" style="width: 100%" required>
                                    <option value=""></option>
                                    <option value='P'<?php
                                    if ($addflag != 1) {
                                        if ($get["purtype"] == "P") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?>>Packing Material</option>
                                    <option value='R'<?php
                                    if ($addflag != 1) {
                                        if ($get["purtype"] == "R") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?>>Raw Material</option>
                                    <option value='O'<?php
                                    if ($addflag != 1) {
                                        if ($get["purtype"] == "O") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?>>Other</option>
                                </select>
                            </div>
                            <div class="col-lg-1 pad text-right">
                                <label class="label-control marg">Transporter</label>
                            </div>
                            <div class="col-lg-2 pad">
                                <select class="form-control form cmbtrans select2" name="transport" style="width: 100%">
                                    <option value="<?php Echo ($addflag != 1) ? $ltrans["code"] : ""; ?>"><?php Echo ($addflag != 1) ? $ltrans["name"] : ""; ?></option>
                                    <?php
                                    $gtrans = mysqli_query($conn, "SELECT `code`,`name` FROM `transport` order by name")or die(mysqli_error($conn));
                                    while ($trans = mysqli_fetch_array($gtrans)) {
                                        Echo "<option value='$trans[code]'>$trans[name]</option>";
                                    }
                                    ?>
                                </select>
                            </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="table-responsive">
                        <table class="table table-striped table-condensed table-bordered" width="100%">
                            <thead  class="mySearchTr">
                                <tr>
                                    <th width="5%"></th>
                                    <th width="5%" class='text-right'>Sr.</th>
                                    <th width="50%" class='text-left'>Description</th>
                                    <th width="10%" class='text-right'>Qty.</th>
                                    <th width="10%" class='text-right'>Rate</th>
                                    <th width="10%" class=''>Per</th>
                                    <th width="20%" class='text-right'>Amount</th>
                                </tr>
                            </thead>
                            <tbody class="tbody">
                                <?php
                                if ($addflag != 1) {
                                    $ctr = 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)) {
                                        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='$tran[srno]'></td>";
                                        Echo "<td>";
                                        Echo "<input type='text' class='form-control form descrip' name='descrip_$ctr' value='$tran[descrip]'>";
                                        Echo "</td>";
                                        Echo "<td>";
                                        Echo "<input type='number' step='any' class='form-control text-right form qty' name='qty_$ctr' value='$tran[qty]'>";
                                        Echo "</td>";
                                        Echo "<td>";
                                        Echo "<input type='number' step='any' class='form-control text-right form rate' name='rate_$ctr' value='$tran[rate]'>";
                                        Echo "</td>";
                                        Echo "<td>";
                                        Echo "<input type='text' class='form-control form perpcs' name='perpcs_$ctr' value='$tran[perpcs]'>";
                                        Echo "</td>";
                                        Echo "<td>";
                                        Echo "<input type='number' step='any' class='form-control text-right form amount' name='amount_$ctr' value='$tran[amount]'>";
                                        Echo "</td>";
                                        Echo "</tr >";
                                        $ctr++;
                                    }
                                } else {
                                    ?>
                                    <tr>
                                        <td><i class="fa fa-close remove"></i></td>
                                        <td><input type="text" class="form-control form text-right srno" name="srno_0" value="1"></td>
                                        <td><input type="text" class="form-control form itemcode barcode" name="descrip_0" name="descrip"></td>
                                        <td><input type='number' step='any' class="form-control form text-right qty"   name="qty_0"      value="0"></td>
                                        <td><input type='number' step='any' class="form-control form text-right rate"   name="rate_0"      value="0"></td>
                                        <td><input type='text'  class="form-control form text-right perpcs"   name="perpcs_0"      value=""></td>
                                        <td><input type='number' step='any' class="form-control form text-right amount"   name="amount_0"      value="0"></td>
                                    </tr>  
                                    <?
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                    <div class="col-lg-12 pad">
                        <button type="button" class="btn btn-primary btn-square form"  id="addnew">
                            <i class="fa fa-plus" data-toggle='tooltip' title='Add Row'></i>    
                        </button>
                        <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : 1; ?>" />  
                    </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="">
                </div>
                <div class="panel-body">
                    <div class="col-lg-12 col-xs-12 pad">
                        <div class="col-lg-6 col-xs-12 pad">
                            <div class="col-lg-12 col-xs-12 pad">
                                <div class="col-lg-3 pad text-right">
                                    <label class="label-control marg">Remarks</label>
                                </div>
                                <div class="col-lg-9 pad">
                                    <input type="text" name="remark1" class="form-control form " value="<? Echo ($addflag != 1) ? $get["rem1"] : ""; ?>">
                                </div>
                            </div>
                            <div class="col-lg-12 col-xs-12 pad">
                                <div class="col-lg-3 pad text-right"></div>
                                <div class="col-lg-9 pad">
                                    <input type="text" name="remark2" class="form-control form " value="<? Echo ($addflag != 1) ? $get["rem2"] : ""; ?>">
                                </div>
                            </div>
                        </div>

                        <div class="col-lg-6 col-xs-12 pad">
                            <div class="col-lg-12 col-xs-12 pad">
                                <div class="col-lg-3 pad"></div>
                                <div class="col-lg-3 pad"></div>
                                <div class="col-lg-3 pad text-right">
                                    <label class="label-control marg">Subtotal</label>
                                </div>
                                <div class="col-lg-3 pad">
                                    <input type="number" step="any" name="grossamt" class="form-control form text-right grossamt" value="<?php Echo ($addflag != 1) ? $get['grossamt'] : "0"; ?>" readonly >
                                </div>
                            </div>
                            <div class="col-lg-12 col-xs-12 pad">
                                <div class="col-lg-3 pad"></div>
                                <div class="col-lg-3 pad text-right">
                                    <label class="label-control marg">Discount</label>
                                </div>
                                <div class="col-lg-2 pad text-right">
                                    <input type="number" step="any" name="discperc" class="form-control form text-right discperc" value="<?php Echo ($addflag != 1) ? $get['discperc'] : "0"; ?>"  >
                                </div>
                                <div class="col-lg-1 pad">
                                    <label class="label-control marg">%</label>
                                </div>
                                <div class="col-lg-3 pad">
                                    <input type="number" step="any" name="totdiscamt" class="form-control form text-right totdiscamt" value="<?php Echo ($addflag != 1) ? $get['discamt'] : "0"; ?>"  readonly>
                                </div>
                            </div>
                            <!--                                <div class="col-lg-12 col-xs-12 pad">
                                                                <div class="col-lg-3 pad"></div>
                                                                <div class="col-lg-3 pad"></div>
                                                                <div class="col-lg-3 pad text-right">
                                                                    <label class="label-control marg">Taxable Amount</label>
                                                                </div>
                                                                <div class="col-lg-3 pad">
                                                                    <input type="number" step="any" name="taxableamt" class="form-control form text-right taxableamt" value="<?php Echo ($addflag != 1) ? $get['taxableamt'] : "0"; ?>" readonly >
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-12 col-xs-12 pad">
                                                                <div class="col-lg-3 pad"></div>
                                                                <div class="col-lg-3 pad"></div>
                                                                <div class="col-lg-3 pad text-right">
                                                                    <label class="label-control marg">CGST</label>
                                                                </div>
                                                                <div class="col-lg-3 pad">
                                                                    <input type="number" step="any" name="cgstamt" class="form-control form text-right cgstamt" value="<?php Echo ($addflag != 1) ? $get['cgstamt'] : "0"; ?>" readonly >
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-12 col-xs-12 pad">
                                                                <div class="col-lg-3 pad"></div>
                                                                <div class="col-lg-3 pad"></div>
                                                                <div class="col-lg-3 pad text-right">
                                                                    <label class="label-control marg">SGST</label>
                                                                </div>
                                                                <div class="col-lg-3 pad">
                                                                    <input type="number" step="any" name="sgstamt" class="form-control form text-right sgstamt" value="<?php Echo ($addflag != 1) ? $get['sgstamt'] : "0"; ?>" readonly >
                                                                </div>
                                                            </div>
                                                            <div class="col-lg-12 col-xs-12 pad">
                                                                <div class="col-lg-3 pad"></div>
                                                                <div class="col-lg-3 pad"></div>
                                                                <div class="col-lg-3 pad text-right">
                                                                    <label class="label-control marg">IGST</label>
                                                                </div>
                                                                <div class="col-lg-3 pad">
                                                                    <input type="number" step="any" name="igstamt" class="form-control form text-right igstamt" value="<?php Echo ($addflag != 1) ? $get['igstamt'] : "0"; ?>" readonly >
                                                                </div>
                                                            </div>-->
                            <div class="col-lg-12 col-xs-12 pad">
                                <div class="col-lg-3 pad"></div>
                                <div class="col-lg-3 pad"></div>
                                <div class="col-lg-3 pad text-right">
                                    <label class="label-control marg">Net Amount</label>
                                </div>
                                <div class="col-lg-3 pad">
                                    <!--<input type="text"  name="totbillamt" required="true" class="form-control form text-right totbillamt" value="<?php Echo ($addflag != 1) ? $get['billamt'] : "0"; ?>" readonly tabindex="-1">-->
                                    <input type="number" step="any" name="totbillamt" class="form-control form text-right totbillamt" value="<?php Echo ($addflag != 1) ? $get['billamt'] : "0"; ?>" readonly >
                                </div>
                            </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">
                    <?php include("includes/buttons.php"); ?>
                </div>
            </div>
        </div>
    </div>
</form>

<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 $file ?>" class="form-control file">

                <div class="row">
                    <div class="col-lg-2">
                        <select class="form-control sfield">
                            <option value="1">Party</option>
                            <option value="2">Voucher</option>
                            <option value="3">Date</option>
                            <option value="4">Type</option>
                            <option value="6">Net Amount</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">Party</option>
                            <option value="2">Voucher</option>
                            <option value="3">Date</option>
                            <option value="4">Type</option>
                            <option value="6">Net Amount</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">
                    <img src="images/search.gif" alt="loading">       
                </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 table-striped table-condensed table-bordered" width="100%">
                        <thead>
                            <tr style="background-color:#1ABC9C;">
                                <th width="10%">Vocher</th>
                                <th width="15%">Date</th>
                                <th width="20%">Party</th>
                                <th width="10%">Type</th>
                                <th width="15%">Net Amount</th>
                                <th width="10%">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>
