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

$formname = 'Purchase & Expense Entry';
$maindb = 'purchexpmain';
$trandb = 'purchexptran';
$accdb = "actran";
$printfile = "tiktnew_invprint.php";
$file = 'purchexp.php';
$trandbexist = 1;
$keyfld = "vouchno";

$source = "";
$tourcode = $_SESSION['tourcode'];

$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;
    $qvouch = $_POST["vouchno"];
    $vouchdt = $_POST["vouchdt"];
    $class = $_POST["class"];
    $totamt = $_POST["totamt"];


    $count = $_POST["items"];

    $mvouch = $qvouch;

    $salecode = 11;

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

    if ($a == 1) {
        // mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE ");

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) 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));

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

            $srno = $_POST["srno_$i"];
            $expname = $_POST["expname_$i"];
            $amount = $_POST["amount_$i"];

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

        #################################TRAN PART END #################################

        ############################## MAIN ######################################################
        if ($addflag == 1) {

            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `class`, `totamt`)
                                               VALUES ($qvouch, '$vouchdt',  '$class', '$totamt')") 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  `totamt`='$totamt' WHERE `vouchno`= '$mvouch' ") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }

        ################################# MAIN PART END #################################
    }
}

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");
}

include("includes/linkshead.php");
?>
<script src="MyScript/shortkeys.js?a=2" type="text/javascript"></script>
<script src="MyScript/purchexp.js?a=3" type="text/javascript"></script>
<script src="MyScript/purchexp_search.js?a=2" type="text/javascript"></script>
<style>
    .sknewbtn {
        background-image: linear-gradient(135deg, #09C6F9 10%, #045DE9 100%) !important;
        color: white !important;
    }
</style>
</head>

<body>
    <form action='' method='POST'>
        <!-- Default box -->
        <div>
            <div class="col-lg-5 col-md-10 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>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-4  col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Purchase & Expense For </label>
                            </div>
                            <div class="col-lg-4 col-md-3 col-sm-4 col-xs-4 pad">
                                <select name="class" class="form-control form class select2" style="width: 100%;" required>
                                    <option value="<?php echo ($addflag != 1) ? $get['class'] : '' ?>"><?php echo ($addflag != 1) ? $get['class'] : '' ?></option>
                                    <?php
                                    $mclass = mysqli_query($conn, "SELECT `batchno` FROM `bookform` WHERE `batchno` !='' AND tourno = '$tourcode' group by batchno ") or die(mysqli_error($conn));
                                    while ($tclass = mysqli_fetch_array($mclass)) {
                                        echo "<option value='$tclass[batchno]'>$tclass[batchno]</option>";
                                    }
                                    ?>
                                </select>
                                <input type="hidden" class="form-control form vouchno" name="vouchno" value="<?php echo ($addflag != 1) ? $get["vouchno"] : "$qvouch"; ?>">
                                <input type="hidden" class="form-control form vouchdt" name="vouchdt" value="<?php echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>">
                            </div>
                        </div>


                        <div class="table-responsive" style="padding: 1%;">
                            <table class="table table-striped table-condensed table-bordered " id="tabledata" width="60%">
                                <thead class="mySearchTr">
                                    <tr>
                                        <th style="width: 2%"></th>
                                        <th style="width: 3%" class='text-center'>Sr.</th>
                                        <th style="width: 20%"> Name</th>
                                        <th style="width: 10%" 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)) {

                                            $getexpnm = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE accode = '$tran[expname]' ") or die(mysqli_error($conn));
                                            $expnm = mysqli_fetch_array($getexpnm);

                                            $mctr++;
                                            echo "<tr>";
                                            echo "<td style='text-align:center'><i class='fa fa-close remove' style='display:none'></i></td>";
                                            echo "<td><input type='text' class='form-control form text-center srno'       name='srno_$ctr'     value='$mctr'></td>";

                                            echo "<td>";
                                            echo "<select class='form-control form lcheckin select2 fetchpart_$ctr expname' name='expname_$ctr' style='width:100%'>";
                                            echo "<option value='$tran[expname]'>$expnm[name]</option>";
                                            if ($tran['expname'] > '349') { 
                                                $gexp = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE accode > '51'  AND grpcode = '012' ") or die(mysqli_error($conn));
                                                while ($iexp = mysqli_fetch_array($gexp)) {
                                                    echo "<option value='$iexp[accode]'>$iexp[name]</option>";
                                                }
                                            }
                                            echo "</select>";
                                            echo "</td>";

                                            echo "<td><input type='text' class='form-control text-right form amount'    name='amount_$ctr'   value='$tran[amount]'></td>";

                                            echo "</tr>";
                                            $ctr++;
                                        }
                                    } else {
                                        $pctr = 0;

                                        //////////////////////////  Expense 1  ////////////////////////////////

                                        echo "<tr>";
                                        echo "<td style='text-align:center'><i class='fa fa-close remove' style=''></i></td>";
                                        echo "<td><input type='text' class='form-control form text-center srno'  name='srno_1'  value='1' readonly></td>";

                                        echo "<td>";
                                        echo "<select class='form-control form select2 expname' name='expname_1' style='width:100%'>";
                                        $sexp1 = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE name = 'Ticket & Airline' AND grpcode = '012' ") or die(mysqli_error($conn));
                                        while ($aexp1 = mysqli_fetch_array($sexp1)) {
                                            echo "<option value='$aexp1[accode]'>$aexp1[name]</option>";
                                        }
                                        echo "</select>";
                                        echo "</td>";

                                        echo "<td><input type='text' name='amount_1' value='0' class='form-control text-right form amount'></td>";
                                        echo "</tr>";

                                        /////////////////////////// Expense 2 ////////////////////////////////

                                        echo "<tr>";
                                        echo "<td style='text-align:center'><i class='fa fa-close remove' style=''></i></td>";
                                        echo "<td><input type='text' class='form-control form text-center srno'  name='srno_2'    value='2' readonly></td>";

                                        echo "<td>";
                                        echo "<select class='form-control form  select2  expname' name='expname_2' style='width:100%'>";
                                        $sexp2 = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE name = 'Hotel' AND grpcode = '012' ") or die(mysqli_error($conn));
                                        while ($aexp2 = mysqli_fetch_array($sexp2)) {
                                            echo "<option value='$aexp2[accode]'>$aexp2[name]</option>";
                                        }
                                        echo "</select>";
                                        echo "</td>";

                                        echo "<td><input type='text' name='amount_2' value='0' class='form-control text-right form amount'></td>";
                                        echo "</tr>";

                                        /////////////////////////// Expense 3 ////////////////////////////////

                                        echo "<tr>";
                                        echo "<td style='text-align:center'><i class='fa fa-close remove' style=''></i></td>";
                                        echo "<td><input type='text' class='form-control form text-center srno' name='srno_3'    value='3' readonly></td>";

                                        echo "<td>";
                                        echo "<select class='form-control form select2  expname' name='expname_3' style='width:100%'>";
                                        $sexp3 = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE name = 'Food' AND grpcode = '012' ") or die(mysqli_error($conn));
                                        while ($aexp3 = mysqli_fetch_array($sexp3)) {
                                            echo "<option value='$aexp3[accode]'>$aexp3[name]</option>";
                                        }
                                        echo "</select>";
                                        echo "</td>";

                                        echo "<td><input type='text' name='amount_3' value='0' class='form-control text-right form amount'></td>";
                                        echo "</tr>";

                                        /////////////////////////// Expense 4 ////////////////////////////////

                                        echo "<tr>";
                                        echo "<td style='text-align:center'><i class='fa fa-close remove' style=''></i></td>";
                                        echo "<td><input type='text' class='form-control form text-center srno' name='srno_4'    value='4' readonly></td>";

                                        echo "<td>";
                                        echo "<select class='form-control form select2 expname' name='expname_4' style='width:100%'>";
                                        $sexp4 = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE name = 'Laundry' AND grpcode = '012' ") or die(mysqli_error($conn));
                                        while ($aexp4 = mysqli_fetch_array($sexp4)) {
                                            echo "<option value='$aexp4[accode]'>$aexp4[name]</option>";
                                        }
                                        echo "</select>";
                                        echo "</td>";

                                        echo "<td><input type='text' name='amount_4' value='0' class='form-control text-right form amount'></td>";
                                        echo "</tr>";

                                        /////////////////////////// Expense 5 ////////////////////////////////

                                        echo "<tr>";
                                        echo "<td style='text-align:center'><i class='fa fa-close remove' style=''></i></td>";
                                        echo "<td><input type='text' class='form-control form text-center srno' name='srno_5'    value='5' readonly></td>";

                                        echo "<td>";
                                        echo "<select class='form-control form select2 expname' name='expname_5' style='width:100%'>";
                                        $sexp5 = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE name = 'Transportation' AND grpcode = '012' ") or die(mysqli_error($conn));
                                        while ($aexp5 = mysqli_fetch_array($sexp5)) {
                                            echo "<option value='$aexp5[accode]'>$aexp5[name]</option>";
                                        }
                                        echo "</select>";
                                        echo "</td>";

                                        echo "<td><input type='text' name='amount_5' value='0' class='form-control text-right form amount'></td>";
                                        echo "</tr>";

                                        /////////////////////////// Expense 6 ////////////////////////////////

                                        echo "<tr>";
                                        echo "<td style='text-align:center'><i class='fa fa-close remove' style=''></i></td>";
                                        echo "<td><input type='text' class='form-control form text-center srno' name='srno_6'    value='6' readonly></td>";

                                        echo "<td>";
                                        echo "<select class='form-control form select2 expname' name='expname_6' style='width:100%'>";
                                        $sexp6 = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE name = 'Visa' AND grpcode = '012' ") or die(mysqli_error($conn));
                                        while ($aexp6 = mysqli_fetch_array($sexp6)) {
                                            echo "<option value='$aexp6[accode]'>$aexp6[name]</option>";
                                        }
                                        echo "</select>";
                                        echo "</td>";

                                        echo "<td><input type='text' name='amount_6' value='0' class='form-control text-right form amount'></td>";
                                        echo "</tr>";

                                        /////////////////////////// Expense 7 ////////////////////////////////

                                        echo "<tr>";
                                        echo "<td style='text-align:center'><i class='fa fa-close remove' style=''></i></td>";
                                        echo "<td><input type='text' class='form-control form text-center srno' name='srno_7'    value='7' readonly></td>";

                                        echo "<td>";
                                        echo "<select class='form-control form select2 expname' name='expname_7' style='width:100%'>";
                                        $sexp7 = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE name = 'Agent Commission' AND grpcode = '012' ") or die(mysqli_error($conn));
                                        while ($aexp7 = mysqli_fetch_array($sexp7)) {
                                            echo "<option value='$aexp7[accode]'>$aexp7[name]</option>";
                                        }
                                        echo "</select>";
                                        echo "</td>";

                                        echo "<td><input type='text' name='amount_7' value='0' class='form-control text-right form amount'></td>";
                                        echo "</tr>";

                                        /////////////////////////// Expense 8 ////////////////////////////////

                                        echo "<tr>";
                                        echo "<td style='text-align:center'><i class='fa fa-close remove' style=''></i></td>";
                                        echo "<td><input type='text' class='form-control form text-center srno' name='srno_8'    value='8' readonly></td>";

                                        echo "<td>";
                                        echo "<select class='form-control form select2 expname' name='expname_8' style='width:100%'>";
                                        $sexp8 = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE name = 'Miscellaneous' AND grpcode = '012' ") or die(mysqli_error($conn));
                                        while ($aexp8 = mysqli_fetch_array($sexp8)) {
                                            echo "<option value='$aexp8[accode]'>$aexp8[name]</option>";
                                        }
                                        echo "</select>";
                                        echo "</td>";

                                        echo "<td><input type='text' name='amount_8' value='0' class='form-control text-right form amount'></td>";
                                        echo "</tr>";
                                    }
                                    $pctr++;
                                    ?>
                                    <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : 8; ?>" />
                                </tbody>
                            </table>

                            <div class="col-lg-12 " style="margin-top: 10px;">
                                <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>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            </div>
                            <div class="col-lg-7 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Total Amount</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-4 col-xs-4  pad">
                                <input type="text" class="form-control text-right text-size totamt form" name="totamt" value="<?php echo ($addflag != 1) ? $get["totamt"] : "0"; ?>" readonly>
                            </div>
                        </div>

                        <div style="background-color:#E3F2E1;width: 50%;display: none;" class="ptikt">

                            <div class="row">
                                <div class="col-lg-3 text-right pad">
                                    <label class="label-control marg" style="color:red">Supplier</label>
                                </div>
                                <div class="col-lg-6 adjcolj pad">
                                    <select name="tiktsupp" class="form-control form pad tiktsupp" style="color:red;background-color:white;">
                                        <option value="<?php echo ($addflag != 1) ? $get["tiktsupp"] : ""; ?>"> <?php echo ($addflag != 1) ? $fettksupp["name"] : ""; ?></option>
                                        <?php
                                        $gtk = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where grpcode = '009' ORDER BY `name`") or die(mysqli_error($conn));
                                        while ($ftk = mysqli_fetch_array($gtk)) {
                                            echo "<option value='$ftk[accode]'>$ftk[name]</option>";
                                        }
                                        ?>

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

                            <div class="row">
                                <div class="col-lg-3 text-right pad">
                                    <label class="label-control marg" style="color:red">Invoice No.</label>
                                </div>
                                <div class="col-lg-2 adjcolj pad">
                                    <input type="text" class="form-control form text-right  tiktinvno" style="color:red;background-color:white" name="tiktinvno" value="<?php echo ($addflag != 1) ? $get["tiktinvno"] : ""; ?>">
                                </div>
                                <div class="col-lg-2 text-right pad">
                                    <label class="label-control marg" style="color:red">Invoice Date</label>
                                </div>
                                <div class="col-lg-2 text-right pad">
                                    <input type="date" class="form-control form text-right  tiktinvdt" style="color:red;background-color:white" name="tiktinvdt" value="<?php echo ($addflag != 1) ? $get["tiktinvdt"] : ""; ?>">
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-lg-3 text-right pad">
                                    <label class="label-control marg" style="color:red">Amount</label>

                                </div>
                                <div class="col-lg-2 text-right pad">
                                    <input type="text" class="form-control form text-right  tiktpuramt" style="color:red;background-color:white" name="tiktpuramt" value="<?php echo ($addflag != 1) ? $get["tiktpuramt"] : ""; ?>">
                                </div>
                            </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 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">
                    <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">For</option>
                                <option value="4">Total</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">For</option>
                                <option value="4">Total</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:10%">Date</th>
                                    <th style="width:45%">Purchase & Expense For</th>
                                    <th style="width:15%">Total</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>