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

$formname = 'Challan Entry';
$ztit = $_SESSION["tit_" . $dbname];

$maindb = 'challan';
//$trandb = 'invrttn' . $ztit;
$acc = 'actran';
$cash = 'cashrecp';
$bank = 'bankrecp';
$printfile = "invrt_print.php";
$file = 'chal.php';
$grid = 'invoicertmain.php';
$trandbexist = 0;
$keyfld = "vouchno";
$source = "SLRT";
$usertype = $_SESSION["usertype_" . $dbname];

$mixinv = 'mixinv' . $ztit;

$cust = 'accmast';

//$ztit = $_SESSION["tit_" . $dbname];
$keycond = "";
//$ztit = $_SESSION["tit_" . $dbname];





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


$usertype = $_SESSION["usertype"];
$user = $_SESSION['username_' . $dbname];

mysqli_select_db($conn,$zdb);


$zroot = $_SESSION["zdir"];

//$getrtcomp = mysqli_query($conn,"Select * from $zroot.`godown` where `database`= '$companycode'") or die(mysqli_error($conn));
//$fetrtcomp = mysqli_fetch_array($getrtcomp);
//mysqli_select_db($conn,$zdb);

include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchno = $_POST["vouchno"];
    $vouchdt = $_POST["vouchdt"];
    $make = $_POST["make"];
    $delv = $_POST["delv"];
    $party = $_POST["party"];
    $nag = $_POST["nag"];
    $sizemm = $_POST["sizemm"];
    $guagemm = $_POST["guagemm"];
    $makemm = $_POST["makemm"];
    $dia = $_POST["dia"];
    $ap = $_POST["ap"];
    $weld = $_POST["weld"];
    $straight = $_POST["straight"];
    $foot = $_POST["foot"];
    $totfeet = $_POST["totfeet"];
    $weight = $_POST["weight"];
    $rate = $_POST["rate"];
    $amount = $_POST["amount"];
    $cashcred = $_POST["cashcred"];
    $descrip = $_POST["descrip"];
    $cashvouch = $_POST["cashvouch"];
    


    $count = $_POST["items"];
    $mvouch = $qvouch;
    $salecode = 15;



    if ($a == 1) {


        



        if ($addflag == 1) {
            $genv = mysqli_query($conn,"SELECT MAX(vouchno) AS maxcode FROM $zroot.`$maindb` where vouchno != ''  LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($genv) > 0) {
                $vouchn = mysqli_fetch_assoc($genv);
                $qvouch = $vouchn["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
          
        }

        $mvouch = $qvouch;

        #############AUto Cash Receipt Start####################
        $mcashamt = $cashamt;


    




#################################TRAN PART END #################################
############################### Accounts Part ##################################

//        mysqli_query($conn,"DELETE FROM $zroot.`$accdb` WHERE `vouchno` = '$mvouch' AND `source`='$source' and `company`='$cmbcomp' and `brancode`='$brancode'") OR die(mysqli_error($conn));
//
//        if ($netamt > 0) {
//            $minus_amt = ($netamt * -1);
//
//// sale part
//            mysqli_query($conn,"INSERT INTO $zroot.`$accdb`( `company`, date, vouchno, source, trnno, accode, grpcode, amount, `brancode`)
//                    VALUES ('$cmbcomp', '$vouchdt','$mvouch', '$source', '001', '$salecode','005', '$netamt', '$brancode')") OR die(mysqli_error($conn));
//// party part
//            mysqli_query($conn,"INSERT INTO $zroot.`$accdb`(`company`, date, vouchno, source, trnno, accode, grpcode, amount, `brancode`)
//                     VALUES ('$cmbcomp', '$vouchdt','$mvouch', '$source', '002', '$name', '010', '$minus_amt', '$brancode')") or die(mysqli_error($conn));
//        }




############################## MAIN ######################################################
        if ($addflag == 1) {
            mysqli_query($conn,"INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `make`, `delv`, `party`, `nag`, `sizemm`,
                        `guagemm`,`makemm`, `dia`,`ap`, `weld`, `straight`, `foot`, `totfeet`, `weight`, `rate`,
                        `amount`, `cashcred`, `descrip`, `cashvouch`)
                 VALUES ($qvouch, '$vouchdt', '$make', '$delv','$party', '$nag', '$sizemm',
                        '$guagemm', '$makemm', '$dia', '$ap', '$weld', '$straight', '$foot', '$totfeet', '$weight', '$rate',
                        '$amount', '$cashcred', '$descrip', '$cashvouch')") or die(mysqli_error($conn));

          
            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $grid?$keyfld=$mvouch&mode=last");

        } else {
            $mvouch = $qvouch; //1

            mysqli_query($conn,"UPDATE `$maindb` SET `vouchdt`='$vouchdt', `make`='$make', `delv`='$delv',`party`='$party',
                        `nag`='$nag', `sizemm`='$sizemm', `guagemm`='$guagemm', `makemm`='$makemm', `dia`='$dia', 
                        `ap`='$ap', `weld`='$weld', `straight`='$straight', `foot`='$foot', `totfeet`='$totfeet',
                        `weight`='$weight', `rate`='$rate',`amount`='$amount', `cashcred`='$cashcred', `descrip`='$descrip', `cashvouch`='$cashvouch' 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);
    $delcomp = $get['company'];
    $delbran = $get['brancode'];

//    $getpary = mysqli_query($conn,"SELECT * FROM $zroot.`$custdb` WHERE accode='$get[name]'") or die(mysqli_error($conn));
//    $lparty = mysqli_fetch_array($getpary);
//    $prtytype = '';
//    if ($lparty['partytype'] == 'R') {
//        $prtytype = 'Retail';
//    } elseif ($lparty['partytype'] == 'W') {
//        $prtytype = 'Wholesale';
//    }


//    $getcare = mysqli_query($conn,"SELECT * FROM $zroot.`$custdb` WHERE accode='$get[careoff]'") or die(mysqli_error($conn));
//    $fetcare = mysqli_fetch_array($getcare);

//    $getcomp = mysqli_query($conn,"SELECT * FROM $zroot.`company` WHERE code='$get[company]'") or die(mysqli_error($conn));
//    $lcomp = mysqli_fetch_array($getcomp);
//
//    $getgst = mysqli_query($conn,"SELECT * FROM $zroot.`saletype` WHERE code='$get[gsttype]'") or die(mysqli_error($conn));
//    $lgsttype = mysqli_fetch_array($getgst);
//
//    $getgdw = mysqli_query($conn,"SELECT * FROM $zroot.`godown` WHERE code='$get[brancode]'") or die(mysqli_error($conn));
//    $lgdw = mysqli_fetch_array($getgdw);
}

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

if (isset($_GET["delete"])) {
    $delcond = " vouchno = '$_GET[vouchno]' and `company`='$delcomp' and `brancode`='$delbran'";
    $delcond2 = " source = '$source' and vouchno = '$_GET[vouchno]'  AND `company`='$delcomp' and `brancode`='$delbran'";
    $delcond3 = " source = '$source' and vouchno = '$_GET[vouchno]'  and `compcode`='$delbran'";
    delnew($zroot, "$maindb", "$delcond", "$grid?mode=last");
    delnew($zroot, "$trandb", "$delcond", "$grid?mode=last");
    delnew($zroot, "$accdb", "$delcond2", "$grid?mode=last");

    delnew($zroot, "$saledb", "$delcond3", "$grid?mode=last");
    delnew($zroot, "$mixinv", "$delcond2", "$grid?mode=last");
}

include("includes/linkshead.php");
?>
<!--<script src="MyScript/invoicert.js?a=4" type="text/javascript"></script>-->
</head>
<body>
    <!--    <div id="preloader" class="col-lg-12 text-center">
            <h3 class="text-center">Loading</h3>
            <div id="status">&nbsp;</div>
        </div>-->
    <form action='' method='POST'>
        <!-- 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:black"><?php Echo $formname; ?> <span style="font-size: 14px;color:green"></span></h3>


                        <div class="actions pull-right">

                            <i class=""></i>
                            <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-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-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-size form vouchno" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : ""; ?>" required readonly>
                                <input type="hidden" class="form-control form text-size form saveupd" name="saveupd" value="" >
                                <input type="hidden" class="form-control form text-size form addflg" name="addflg" value="<?php Echo ($addflag != 1) ? $addflag : $addflag; ?>" required readonly>
                                <input type="hidden" class="form-control form text-size form yearno" name="yearno" value="<?php Echo ($addflag != 1) ? $ztit : $ztit; ?>" required readonly>
                                <input type="hidden" size="6" maxlength="6" id="cashvouch" maxlength="6" name="cashvouch" value="<?php echo ($addflag != 1) ? $get['cashvouch'] : ""; ?>" />      
                                <input type="hidden" class="form-control form text-size form usertyp" name="usertyp" value="<?= $usertype ?>" >

                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">?</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-size form make" name="make" value="<?php Echo ($addflag != 1) ? $get["make"] : ""; ?>" >                                
                            </div>
                            <div class="col-lg-1 col-md-2 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-8 col-xs-8 pad">
                                <input type="date" class="form-control form form"  name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date('Y-m-d'); ?>" style="width:140px">
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">P/R/D</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-size form delv" name="delv" value="<?php Echo ($addflag != 1) ? $get["delv"] : ""; ?>" >                                
                            </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 form  select2 company" name="party" style="" required>
                                    <option value="<?php Echo ($addflag != 1) ? $get["accode"] : ""; ?>"><?php Echo ($addflag != 1) ? $lcomp["name"] : ""; ?></option>
                                    <?php
                                    if ($addflag == 1) {
                                        $gparty = mysqli_query($conn,"SELECT `accode`,`name` FROM `accmast` where grpcode='010' order by name")or die(mysqli_error($conn));
                                        while ($fparty = mysqli_fetch_array($gparty)) {
                                            Echo "<option value='$fparty[accode]'>$fparty[compname]</option>";
                                        }
                                    }
                                    ?>
                                </select>
                            </div>

                        </div>
                        <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">Nags</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-size form nag" name="nag" value="<?php Echo ($addflag != 1) ? $get["nag"] : ""; ?>" >                               
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Size(MM)</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-size form sizemm" name="sizemm" value="<?php Echo ($addflag != 1) ? $get["sizemm"] : ""; ?>" >                                
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">T(MM)</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form form"  name="guagemm" value="<?php Echo ($addflag != 1) ? $get["guagemm"] : ""; ?>" >
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Make(MM)</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-size form makemm" name="makemm" value="<?php Echo ($addflag != 1) ? $get["makemm"] : ""; ?>" >                                
                            </div>
                            <div class="col-lg-1 pad text-right">
                                <label class="label-control marg">Dia</label>
                            </div>
                            <div class="col-lg-1 pad">
                                <input type="text" class="form-control form text-size form dia" name="dia" value="<?php Echo ($addflag != 1) ? $get["dia"] : ""; ?>" >                                
                            </div>

                        </div>
                        <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">A/P</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-size form ap" name="ap" value="<?php Echo ($addflag != 1) ? $get["ap"] : ""; ?>" >                               
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Weld</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-size form weld" name="weld" value="<?php Echo ($addflag != 1) ? $get["sizemm"] : ""; ?>" >                                
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Straight</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form form"  name="straight" value="<?php Echo ($addflag != 1) ? $get["straight"] : ""; ?>" >
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">FTS</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-size form foot" name="foot" value="<?php Echo ($addflag != 1) ? $get["foot"] : ""; ?>" >                                
                            </div>
                            <div class="col-lg-1 pad text-right">
                                <label class="label-control marg">T.FTS</label>
                            </div>
                            <div class="col-lg-1 pad">
                                <input type="text" class="form-control form text-size form dia" name="totfeet" value="<?php Echo ($addflag != 1) ? $get["totfeet"] : ""; ?>" >                                
                            </div>

                        </div>
                        <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">Weight</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-size form weight" name="weight" value="<?php Echo ($addflag != 1) ? $get["weight"] : ""; ?>" >                               
                            </div>
                            <div class="col-lg-1 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-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-size form rate" name="rate" value="<?php Echo ($addflag != 1) ? $get["rate"] : ""; ?>" >                                
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Amount</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form form"  name="amount" value="<?php Echo ($addflag != 1) ? $get["amount"] : ""; ?>" >
                            </div>
                            <div class="col-lg-1 pad text-right">
                                <label class="label-control marg">C/R</label>
                            </div>
                            <div class="col-lg-1 pad">
                                <input type="text" class="form-control form text-size form cashcred" name="cashcred" value="<?php Echo ($addflag != 1) ? $get["cashcred"] : ""; ?>" >                                
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Description</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-size form descrip" name="descrip" value="<?php Echo ($addflag != 1) ? $get["descrip"] : ""; ?>" >                                
                            </div>


                        </div>
                        <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">Cashvouch</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-size form cashvouch" name="cashvouch" value="<?php Echo ($addflag != 1) ? $get["cashvouch"] : ""; ?>" >                               
                            </div>                                                        
                        </div>
                    </div>                         



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

    <div class= "row" >
        <div class="panel panel-default">

            <div class="panel-body">

                <div class="col-lg-3"></div>

                <div class="col-lg-4 text-right pad">

                    <?php include("includes/buttons.php"); ?>

                </div>
                <div class="col-lg-2">

                        <!--<input type="button" id="finishTab" class="btn btn-primary pull-right form" style="background-color:#78909C;color: #fff;" value="Check & Finish">-->

                </div>
            </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" style="background-color: #78909C;">
                                <th>Voucher</th>
                                <th>Date</th>
                                <th>Party</th>
                                <th>Godown</th>
                                <th>Company</th>
                                <th>Bill No.</th>
                                <th>Net Amount</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
//                            if ($addflag != 1) {
//                                $srchresult = mysqli_query($conn,"SELECT *, date_format(`vouchdt`,'%d/%m/%Y') as mydt FROM `$maindb`")or die(mysqli_error($conn));
//                                while ($row = mysqli_fetch_array($srchresult)) {
//
//                                    $getpart = mysqli_query($conn,"Select accode,name from `$custdb` where accode = '$row[name]'") or die(mysqli_error($conn));
//                                    $fetpart = mysqli_fetch_array($getpart);
//
//                                    $getgodw = mysqli_query($conn,"Select name from `godown` where code = '$row[compcode]'") or die(mysqli_error($conn));
//                                    $fetgodw = mysqli_fetch_array($getgodw);
//
//                                    $getcompa = mysqli_query($conn,"Select compname from `company` where code = '$row[company]'") or die(mysqli_error($conn));
//                                    $fetcomp = mysqli_fetch_array($getcompa);
//
//                                    echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]&compcode$row[compcode]'>";
//                                    echo "<td align='left'>$row[vouchno]</td>";
//                                    echo "<td align='left'>$row[mydt]</td>";
//                                    echo "<td align='left'>$fetpart[name]</td>";
//                                    echo "<td align='left'>$fetgodw[name]</td>";
//                                    echo "<td align='left'>$fetcomp[compname]</td>";
//                                    echo "<td align='left'>$row[billno]</td>";
//                                    echo "<td align='left'>$row[netamt]</td>";
//                                    echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]&compcode$row[compcode]\"><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>-->
<?php include("includes/linksfoot.php"); ?>
</body>
</html>
