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

$action = "gendinv_fr.php";
$formname = 'Generate Bill';
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$file = 'geninv.php';
$maindb = 'invmain';
$trandb = 'invtran';

$para = $_GET["gentype"];

$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

if (isset($_POST["submit"])) {
    $a = 1;
    $count = $_POST["items"];
    $vouchdt = $_POST["date"];
    $exam = $_POST["exam"];
    $section = $_POST["section"];
    $session = $_POST["session"];
    $demdvouch = $_POST["demdvouch"];
//    $school = $_POST["school"];
    $cntr = 0;

    if ($para == 'S') {
        $count = 1;
        $schlchk = "1";
    }

    for ($j = 0; $j < $count; $j++) {
//        if ($para == 'S') {
//            $saccode = $school;
//        } else {
        $saccode = $_POST["accode_$j"];
        

        if ($para != 'S') {
            $schlchk = $_POST["check_$j"];
        }
        $scust = $_POST["scust_$j"];

        if ($schlchk == "1") {

            $ncust = $_POST["ncust_$j"];

            if ($para == 'S') {
                $result = mysqli_query($conn, "SELECT * FROM `demdmain` WHERE `increvouch`!= 0 and `increvouch`='$demdvouch'") or die(mysqli_error($conn));
                $num = mysqli_num_rows($result);
                if ($num > 0) {
                    $get = mysqli_fetch_array($result);
                } else {
                    $result = mysqli_query($conn, "SELECT * FROM `demdmain` WHERE `vouchno`='$demdvouch'") or die(mysqli_error($conn));
                    $get = mysqli_fetch_array($result);
                }


                $oldvouch = $get['vouchno'];
                $examcode = $get['examcode'];
                $msess = $get['session'];
                $msec = $get['section'];
                $custcode = $get['custcode'];
                $nofpack = $get['nopack'];
                $rem1 = $get['rem1'];
                $rem2 = $get['rem2'];
                $mexam = $examcode;
                $msession = $msess;
                $msection = $msec;
//                browse(1);
                $getac = mysqli_query($conn, "SELECT accode,medmcode FROM `accmast` WHERE custcode = '$custcode'") or die(mysqli_error($conn));
                $facc = mysqli_fetch_array($getac);
                $saccode = $facc['accode'];
                $medium = $facc['medmcode'];

//
//
//                $result2 = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `examcode`='$mexam' AND `section`='$msection' AND 
//                                       `session`='$msession' AND custcode = '$custcode' ") or die(mysqli_error($conn));
//                $get2 = mysqli_num_rows($result2);
                $result2 = mysqli_query($conn, "SELECT * FROM `invmain` WHERE `demdvouch`='$demdvouch'") or die(mysqli_error($conn));
                $get2 = mysqli_num_rows($result2);

                if ($get2 > 0) {
                    browse('Invoice Already Exist For This Demand Voucher');
                    $break = 1;
                    break;
                }
            } else {

                $result = mysqli_query($conn, "SELECT * FROM `demdmain` WHERE `examcode`='$exam' AND `section`='$section' AND `session`='$session'  AND accode = '$saccode'") or die(mysqli_error($conn));
                $get = mysqli_fetch_array($result);
                $oldvouch = $get['vouchno'];
                $getac = mysqli_query($conn, "SELECT medmcode FROM `accmast` WHERE custcode = '$custcode'") or die(mysqli_error($conn));
                $facc = mysqli_fetch_array($getac);
                $medium = $get['medmcode'];
                $nofpack = $get['nopack'];
                $rem1 = $get['rem1'];
                $rem2 = $get['rem2'];
                $custcode = $get['custcode'];
                $mexam = $exam;
                $msession = $session;
                $msection = $section;
            }

            $getvouch = mysqli_query($conn, "SELECT max(vouchno)as newvouch FROM `$maindb`") or die(mysqli_error($conn));
            $fetvouch = mysqli_fetch_array($getvouch);
            $increvouch = $fetvouch['newvouch'];
            if ($increvouch == 0) {
                $newvouch = 1;
            } else {
                $newvouch = $increvouch + 1;
            }
            if (!empty($saccode)) {


                $msubtot = 0;
                $totpq = 0;
                $mmpackamt = 0;
                $gettran = mysqli_query($conn, "SELECT * FROM `demdtran` WHERE `vouchno`='$oldvouch'") or die(mysqli_error($conn));
                while ($ftran = mysqli_fetch_array($gettran)) {
                    $subname = $ftran['subjcode'];
                    $subject = $ftran['subjname'];
                    $srno = $ftran['srno'];
                    $pq1 = $ftran['pq1'];
                    $pq2 = $ftran['pq2'];
                    $pq3 = $ftran['pq3'];
                    $pq4 = $ftran['pq4'];
                    $pq5 = $ftran['pq5'];
                    $pq6 = $ftran['pq6'];
                    $pq7 = $ftran['pq7'];
                    $pq8 = $ftran['pq8'];
                    $pq9 = $ftran['pq9'];
                    $pq10 = $ftran['pq10'];
                    $pqrelm = $ftran['pqrelm'];

                    if ($pq1 < 2) {
                        $pq1 = 0;
                    }
                    if ($pq2 < 2) {
                        $pq2 = 0;
                    }
                    if ($pq3 < 2) {
                        $pq3 = 0;
                    }
                    if ($pq4 < 2) {
                        $pq4 = 0;
                    }
                    if ($pq5 < 2) {
                        $pq5 = 0;
                    }
                    if ($pq6 < 2) {
                        $pq6 = 0;
                    }
                    if ($pq7 < 2) {
                        $pq7 = 0;
                    }
                    if ($pq8 < 2) {
                        $pq8 = 0;
                    }
                    if ($pq9 < 2) {
                        $pq9 = 0;
                    }
                    if ($pq10 < 2) {
                        $pq10 = 0;
                    }

                    if ($pqrelm < 2) {
                        $pqrelm = 0;
                    }

                    $prate = mysqli_query($conn, "SELECT * FROM `paprate` WHERE `section`='$msection' AND `examcode`='$mexam' AND `subjcode` = '$subname'") or die(mysqli_error($conn));
                    $pnum = mysqli_num_rows($prate);
                    
                    if ($pnum > 0) {
                        $gtprate = mysqli_fetch_array($prate);
                    } else {
                        $prate = mysqli_query($conn, "SELECT * FROM `paprate` WHERE `section`='$msection' AND `examcode`='$mexam'") or die(mysqli_error($conn));
                        $gtprate = mysqli_fetch_array($prate);
                    }
                    $rt1 = $gtprate['rt1'];
                    $rt2 = $gtprate['rt2'];
                    $rt3 = $gtprate['rt3'];
                    $rt4 = $gtprate['rt4'];
                    $rt5 = $gtprate['rt5'];
                    $rt6 = $gtprate['rt6'];
                    $rt7 = $gtprate['rt7'];
                    $rt8 = $gtprate['rt8'];
                    $rt9 = $gtprate['rt9'];
                    $rt10 = $gtprate['rt10'];
                    $rtprelm = $gtprate['rtprelm'];

                    $ppackrate = $gtprate['ppackrate'];
                    $spackrate = $gtprate['spackrate'];
                    $mpackrate = $gtprate['mpackrate'];
                    $psection = $gtprate['section'];

//                  if ($pq1 == 0) {
//                        $rt1 = 0;
//                    }
//                    if ($pq2 == 0) {
//                        $rt2 = 0;
//                    }
//                    if ($pq3 == 0) {
//                        $rt3 = 0;
//                    }
//                    if ($pq4 == 0) {
//                        $rt4 = 0;
//                    }
//                    if ($pq5 == 0) {
//                        $rt5 = 0;
//                    }
//                    if ($pq6 == 0) {
//                        $rt6 = 0;
//                    }
//                    if ($pq7 == 0) {
//                        $rt7 = 0;
//                    }
//                    if ($pq8 == 0) {
//                        $rt8 = 0;
//                    }
//                    if ($pq9 == 0) {
//                        $rt9 = 0;
//                    }
//                    if ($pq10 == 0) {
//                        $rt10 = 0;
//                    }
//
//                    if ($pqrelm == 0) {
//                        $rtprelm = 0;
//                    }  

                    $amt1 = $pq1 * $rt1;
                    $amt2 = $pq2 * $rt2;
                    $amt3 = $pq3 * $rt3;
                    $amt4 = $pq4 * $rt4;
                    $amt5 = $pq5 * $rt5;
                    $amt6 = $pq6 * $rt6;
                    $amt7 = $pq7 * $rt7;
                    $amt8 = $pq8 * $rt8;
                    $amt9 = $pq9 * $rt9;
                    $amt10 = $pq10 * $rt10;
                    $amtprelm = $pqrelm * $rtprelm;

                    $mmpackrate = 0;
                    $mtotqty = 0;
                    $totamt = 0;

                    switch ($msection) {

                        CASE
                        'P' : $mtotqty = $pq1 + $pq2 + $pq3 + $pq4;
                            $totamt = $amt1 + $amt2 + $amt3 + $amt4;
                            $mmpackrate = $ppackrate;
                            break;
                        CASE
                        'S' :
                            $mtotqty = $pq5 + $pq6 + $pq7 + $pq8 + $pq9 + $pq10;
                            $totamt = $amt5 + $amt6 + $amt7 + $amt8 + $amt9 + $amt10;
                            $mmpackrate = $spackrate;
                            break;
                        CASE
                        'M' :
                            $mtotqty = $pqprelm;
                            $totamt = $amtprelm;
                            $mmpackrate = $mpackrate;
                            break;
                    }


                    $msubtot = $msubtot + $totamt;
                    $totpq = $totpq + $mtotqty;
                    $mmpackamt = $mmpackrate * $nofpack;
                    mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`, `vouchdt`, `srno`, `subjcode`, `subjname`, `section`, `pq1`, `pq2`, `pq3`, `pq4`,
                                    `pq5`, `pq6`, `pq7`, `pq8`, `pq9`, `pq10`, `pqrelm`, `rt1`, `rt2`, `rt3`, `rt4`, `rt5`, `rt6`,
                                      `rt7`, `rt8`, `rt9`, `rt10`, `rtprelm`,`amount`)
                VALUES ('$newvouch', '$vouchdt', '$srno', '$subname', '$subject',  '$msection',  '$pq1', '$pq2', '$pq3', '$pq4',
                        '$pq5', '$pq6', '$pq7', '$pq8', '$pq9', '$pq10', '$pqrelm', '$rt1', '$rt2', '$rt3', '$rt4', '$rt5',
                         '$rt6', '$rt7', '$rt8', '$rt9', '$rt10', '$rtprelm','$totamt')") or die(mysqli_error($conn));
                }
                ########### MAIN ##########

                $mbillamt = round($msubtot + $mmpackamt);
                
                $bamtinwds = rsword($mbillamt);
                
                mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`,  `billno`,  `billdt`,`accode`, `custcode`, `medmcode`,
                                               `examcode`, `section`, `session`,`nopack`, `rem1`, `rem2`, `packrate`, `packamt`,`amount`,`subtotal`,
                                               `totqty`, `demdvouch`,`billamt`,`genflag`,`year`,`rsword`,balamt)
                                     VALUES ('$newvouch', '$vouchdt', '$newvouch', '$vouchdt', '$saccode', '$custcode', '$medium', 
                                            '$mexam', '$msection', '$msession', '$nofpack', '$rem1', '$rem2','$mmpackrate','$mmpackamt',
                                           '$msubtot', '$msubtot','$totpq','$oldvouch','$mbillamt','A','$myyear','$bamtinwds','$mbillamt')") or die(mysqli_error($conn));




                mysqli_query($conn, "UPDATE `demdmain` SET `billno`='$newvouch' WHERE `vouchno`= '$oldvouch' ") or die(mysqli_error($conn));
                ########### ACTRAN########## 
                $source = 'SL';

                ### FOR  SALE #####
                $svouchno = $newvouch;
                $strnno = '001';
                $accode = '015';
                $sgrpcode = '005';
                $samount = $mbillamt;

                mysqli_query($conn, "INSERT INTO `actran`(`vouchno`, `date`, `vouchdt`, `trnno`, `accode`, `grpcode`, `amount`,`source`)
                VALUES ('$svouchno', '$vouchdt', '$vouchdt', '$strnno', '$accode', '$sgrpcode',  '$samount', '$source')") or die(mysqli_error($conn));

                ### for party #####
                $trnno = '002';
                $grpcode = '010';
                $amount = $mbillamt * -1;

                mysqli_query($conn, "INSERT INTO `actran`(`vouchno`, `date`, `vouchdt`, `trnno`, `accode`, `grpcode`, `amount`,`source`)
                VALUES ('$svouchno', '$vouchdt', '$vouchdt', '$trnno', '$saccode', '$grpcode',  '$amount', '$source')") or die(mysqli_error($conn));
            }
        }
    }

    if ($break == 1) {
        
    } else {
        browse('Generated Successfully');
    }
}

include("includes/linkshead.php");
?>
<script src="MyScript/geninv.js" type="text/javascript"></script>
</head>
<body>
    <form method='POST' >
        <!-- Default box -->
        <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12  pad">
            <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">
                    <?php if ($para == 'M') { ?>                        

                        <div class="row">
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right">
                                <label class="control-label marg">Exam</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-8 col-xs-8 pad">
                                <select  style="height:34px;"  name="exam" class="form-control select2 exam" required>
                                    <option></option>
                                    <?php
                                    $mown1 = mysqli_query($conn, "SELECT * FROM `exam` ORDER BY `name`") or die(mysqli_error($conn));
                                    while ($trow1 = mysqli_fetch_array($mown1)) {
                                        echo "<option value=$trow1[code]>$trow1[name]</option>";
                                    }
                                    ?>
                                </select>                          
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Section</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-8 col-xs-8 pad">
                                <select class="form-control select2 section"  name="section" style="width: 100%" required>

                                    <option value=""></option>
                                    <option value="P" <?php
                                    if ($get['section'] == 'P') {
                                        echo 'selected="selected"';
                                    }
                                    ?>>Primary</option>
                                    <option value="S" <?php
                                    if ($get['section'] == 'S') {
                                        echo 'selected="selected"';
                                    }
                                    ?>>Secondary</option>
                                    <option value="M" <?php
                                    if ($get['section'] == 'M') {
                                        echo 'selected="selected"';
                                    }
                                    ?>>Preliums</option>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Session</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-8 col-xs-8 pad">
                                <select class="form-control select2 session"  name="session" style="width: 100%" required>
                                    <option value=""></option>
                                    <option value="M" <?php
                                    if ($get['session'] == 'M') {
                                        echo 'selected="selected"';
                                    }
                                    ?>>Morning</option>
                                    <option value="A" <?php
                                    if ($get['session'] == 'A') {
                                        echo 'selected="selected"';
                                    }
                                    ?>>Afternoon</option>
                                </select>
                            </div>
                        </div>
<!--                        <div class="row">
                            <div class="col-lg-4 col-md-4 col-sm-3 col-xs-3 text-right">
                                <label class="control-label marg">From Demand Voucher</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-4 col-xs-4 pad">
                                <input type="text" size="80" name="fromdemd" value="" class="form-control"/>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-4 col-md-4 col-sm-3 col-xs-3 text-right">
                                <label class="control-label marg">To Demand Voucher</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-4 col-xs-4 pad">
                                <input type="text" size="80" name="todemd" value="" class="form-control"/>
                            </div>
                        </div>-->
                    <?php } else { ?>
                        <div class="row">
                            <div class="col-lg-4 col-md-4 col-sm-3 col-xs-3 text-right">
                                <label class="control-label marg">Demand Voucher</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="text" size="80" name="demdvouch" value="" required class="form-control"/>
                            </div>
                        </div>
                        <!--                        <div class="row">
                                                    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-3 text-right pad">
                                                        <label class="label-control marg" style="color: #6082BF;">School</label>
                                                    </div>
                                                    <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                                        <select class="form-control  select2 school"  name="school" style="width: 100%" required>
                                                            <option value=""></option>
                        <?php
//                                    $gschl = mysqli_query($conn, "SELECT accode,name FROM `accmast` where `grpcode`='010' order by name")or die(mysqli_error($conn));
//                                    while ($fschl = mysqli_fetch_array($gschl)) {
//                                        Echo "<option value='$fschl[accode]'>$fschl[name]</option>";
//                                    }
                        ?>
                                                        </select>
                                                    </div>
                                                </div>-->

                    <?php } ?>    

                    <div class="row">
                        <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right">
                            <label class="control-label marg">Date</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-5 col-xs-5 pad">
                            <input type="date" size="80" name="date" value="<?php echo date('Y-m-d'); ?>" required class="form-control "/>
                        </div>
                    </div>
                </div>


                <div class="modal fade" id="custmodal"  role="dialog" aria-labelledby="myLargeModalLabel" >
                    <div class="modal-dialog modal-lg" style="margin-top: 5%;">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button class="close" data-dismiss="modal"><span>&times;</span></button>
                                <div class="row">
                                    <div class="col-lg-2 text-right padi">
                                        <label class="label-control">Search By Code</label>
                                    </div>
                                    <div class="col-lg-2 padi" >
                                        <input type='text' style="" name='search' class="form-control tsearch">    
                                    </div>
                                    <div class="col-lg-2 padi" >
                                        <input type="submit" id='submit' name="submit" class="btn btn-pad btn-square" style="background-color:#6082BF;color:#ffffff" value="Submit">
                                    </div>

                                </div>
                            </div>
                            <div class="modal-body">
                                <!--                                <div class="table-responsive" style="height: 300px;">
                                                                    <input type="hidden" name="sections" class="sections form-control">
                                                                    <input type="hidden" name="sessions" class="sessions form-control">
                                <?php
//                                    browse($section);
//                                    if(!empty($section)){
//                                    $sectcond = " AND section = '$section' ";
//                                    }
////                                    $sesncond = "";
////                                    $srchresult = mysqli_query($conn, "SELECT a.,b.name FROM `demdmain` a, `accmast` b WHERE a.`custcode` = b.`custcode` group by a.custcode order by a.custcode")or die(mysqli_error($conn));
//                                    $srchresult = mysqli_query($conn, "SELECT a.custcode,a.section, a.session, b.name FROM `demdmain` a, `accmast` b WHERE a.`custcode` = b.`custcode` 
//                                                               $sectcond $sesncond group by a.custcode order by a.custcode")or die(mysqli_error($conn));
//
//
//                                    if (mysqli_num_rows($srchresult) > 0) {
//                                        echo "<table class='table table-bordered table-hover myTable' style='width: 100%;'>";
//                                        echo "<tr class='clickable-row' style='background-color: #6082BF;color: white;'>";
//                                        echo "<td style='width: 8%;'><input type='checkbox' class='allchk' value='' > &nbsp; ALL S/US</td>";
//                                        echo "<td style='width: 10%;'><b>Code</b></td>";
//                                        echo "<td style='width: 60%;'><b>Name</b></td>";
//                                        echo "<td style='width: 10%;'><b>Section</b></td>";
//                                        echo "<td style='width: 10%;'><b>Session</b></td>";
//                                        echo "</tr>";
//                                        $sctr = 0;
//                                        $ctr = 0;
//                                        while ($row2 = mysqli_fetch_array($srchresult)) {
//                                            $modcode = $row2['custcode'];
//                                            $modname = $row2['name'];
//                                            $section = $row2['section'];
//                                            $session = $row2['session'];
//
//                                            echo "<tr>";
//                                            echo "<td>";
//                                            echo "<input type='checkbox' class='mcheck'>";
//                                            echo "</td>";
//                                            echo "<td><b>$modcode</b>
//                                                  <input type='hidden' style='border: none' name='scust_$ctr' class='scust' value='$modcode' >
//                                                  <input type='hidden' name='accode_$ctr' value='$row2[accode]' class='accode'>
//                                                      <input type='hidden' name='ncust_$ctr' class='ncust' value=''>
//                                                      <input type='hidden' name='saccode_$ctr' class='saccode' value=''>
//                                                  </td>";
//                                            echo "<td><b>$modname<input type='hidden' style='border: none' class='sname' name='sname_$ctr' value='$modname' readonly></b>
//                                                
//                                                    </td>";
//                                            echo "<td><b>$section</b></td>";
//                                            echo "<td><b>$session</b></td>";
//
//                                            echo "</tr>";
//
//                                            echo "";
//                                            $ctr++;
//                                        }
//
//                                        echo "</table>";
//                                    }
                                ?>
                                                                    <input type="hidden" id="items" name="items" value="<?php echo $ctr; ?>" />
                                                                </div>-->
                                <div class="table-responsive mySearchBody" style="height: 300px;">

                                </div>
                                <input type="hidden" id="items" name="items" value="" >
                            </div>
                            <div class="modal-footer"></div>
                        </div>
                    </div>
                </div>
                <div class="panel-footer">
                    <div class="text-center pad">
                        <?php if ($para == 'M') { ?>  
                            <input type="button" id='proceed' class="btn btn-pad btn-square proceed" style="background-color:#008D4C;color:#ffffff" value="Proceed">
                        <?php } else { ?>  
                            <input type="submit" name='submit' class="btn btn-pad btn-square" style="background-color:#008D4C;color:#ffffff" value="Proceed">
                        <?php } ?>  
                        <input tye="button" id='cancel' type="reset" class="btn btn-pad btn-square " style="background-color:red;color:#ffffff" onClick="window.location.href = '<?= 'default.php' ?>'" value="Exit">

                    </div>
                </div>
            </div>
        </div>
    </form>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
