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

$formname = 'Sale Return / Credit Note (Others)';
$maindb = 'credmain';
$trandb = 'credoth';
//$stkdb = 'transacn';
$accdb = 'actran';
$printfile = "credoth_print.php";
$file = 'credoth.php';
$trandbexist = 1;
$keyfld = "vouchno";
if (isset($_GET['credtype']) != "") {
    $zcredtype = "$_GET[credtype]";
} else {
    $zcredtype = "O";
}
$source = "SR";
$keycond = " AND `credtype`='$zcredtype' ";

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

$zyear = right($zdb, 4);

$zStartDate = getzdates($zyear, 'S');
$zEndDate = getzdates($zyear, 'E');

mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchdt = $_POST["vouchdt"];
    $qvouch = $_POST["vouchno"];
    
    $party = $_POST["party"];
    $godown = $_POST["godown"];
    $subtotal = $_POST["subtotal"];
    $litemdisc = $_POST["litemdisc"];
    $cdiscperc = $_POST["cdiscperc"];
    $cdisamt = $_POST["cdisamt"];
    $netamt = $_POST["netamt"];
    $ladvance = $_POST["ladavnce"];
    $balance = $_POST["balance"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];
    $lessoth = $_POST["lessoth"];
    $vatperc = $_POST["vatperc"];
    $vatamt = $_POST["vatamt"];
    $packing = $_POST["packing"];
    $prodtype = $_POST["prodtype"];

    $count = $_POST["items"];

    $mvouch = $qvouch;

    $salecode = 15;

    if ($a == 1) {

        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE,  $trandb WRITE, $accdb WRITE,  accmast WRITE ") or die(mysqli_error($conn));
        
        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));
//        mysqli_query($conn, "DELETE FROM `$stkdb` WHERE `$keyfld`='$mvouch' AND `source`='$source'")or die(mysqli_error($conn));
        $group = $source . $mvouch;

        $srl = 0;
        for ($i = 0; $i < $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $descrip = $_POST["descrip_$i"];
            $amount = $_POST["amount_$i"];

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

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

############################## Accounts Part ######################################################
        mysqli_query($conn, "DELETE FROM `$accdb` WHERE `vouchno` = '$mvouch' AND `source` = '$source'") OR die(mysqli_error($conn));

        if ($netamt > 0) {
            $minus_amt = ($netamt * -1);

            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount,prodtype,actamt,`godown`)
                    VALUES ('$vouchdt','$mvouch', '$source', '001', '$salecode','005', '$minus_amt','$prodtype','$minus_amt','$godown')") OR die(mysqli_error($conn));

            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount,prodtype,actamt,`godown`)
                     VALUES ('$vouchdt','$mvouch', '$source', '002', '$party','010', '$netamt','$prodtype','$netamt','$godown')") or die(mysqli_error($conn));
        }

        ############################## MAIN ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `party`, `totqty`, `subtotal`, `litemdisc`, `cdiscperc`, `cdisamt`, `discperc`, 
                        `discount`, `netamt`, `ladvance`, `balance`, `lessoth`, `vatperc`, `vatamt` , `packing`, `prodtype`,
                        `remark1`, `remark2`, `credtype`,`godown`) 
                        VALUES ('$qvouch', '$vouchdt', '$party', '$totqty', '$subtotal', '$litemdisc', '$cdiscperc', '$cdisamt', '$discperc', 
                        '$discount', '$netamt', '$ladvance', '$balance', '$lessoth', '$vatperc', '$vatamt', '$packing', '$prodtype',
                        '$remark1', '$remark2', 'O','$godown')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit&credtype=O");
        } else {
            $mvouch = $qvouch; //1

            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt', `party`='$party', `prodtype`='$prodtype',
                    `totqty`='$totqty',`subtotal`='$subtotal', `lessoth`='$lessoth', `vatperc`='$vatperc', `vatamt`='$vatamt', `packing`='$packing',
                    `litemdisc`='$litemdisc',`cdiscperc`='$cdiscperc', `cdisamt`='$cdisamt',
                    `discperc`='$discperc',`discount`='$discount',`netamt`='$netamt',`ladvance`='$ladvance',`balance`='$balance',`remark1`='$remark1',
                    `remark2`='$remark2', `credtype`='O',`godown`='$godown' WHERE `vouchno`='$qvouch'") or die(mysqli_error($conn));
            updmess();
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
        mysqli_query($conn, "UNLOCK TABLES ");
    }
}

if ($addflag != 1) {

    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch' and `credtype`='$zcredtype'") 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);

    $ggod = mysqli_query($conn, "SELECT `code`,`name` FROM `godown` WHERE `code`='$get[godown]'") or die(mysqli_error($conn));
    $lgodown = mysqli_fetch_array($ggod);
}

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

if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last&credtype=O");
    delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last&credtype=O");
    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
//    delnew($zdb, "$stkdb", "$delcond", "$file?mode=last");
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last&credtype=O");
}

include("includes/linkshead.php");
?>
<script src="MyScript/credoth.js" type="text/javascript"></script>
<script src="MyScript/checkdate.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 class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                        <input type="date" value="<?= $zStartDate ?>" style="width: 1px;border: none" class="zstdate">
                        <input type="date" value="<?= $zEndDate ?>" style="width: 1px;border: none" class="zenddate">
                    </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-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : " $qvouch"; ?>" required readonly>
                            </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-8 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control focusfield form vouchdt" name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" autofocus>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Party</label>
                            </div>
                            <div class="col-lg-2 col-md-5 col-sm-5 col-xs-5 pad">
                                <select class="form-control form  select2" name="party" style="width: 100%" required>
                                    <option value="<?php Echo ($addflag != 1) ? $get["party"] : ""; ?>"><?php Echo ($addflag != 1) ? $lparty["name"] : ""; ?></option>
                                    <?php
                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `grpcode`='010'")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 col-md-1 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Godown</label>
                            </div>
                            <div class="col-lg-2 col-md-4 col-sm-8 col-xs-8 pad">
                                <select class="form-control form godown select2" name="godown" style="width: 100%">
                                    <option style="font-weight:bold;" value="<?php echo ($addflag != 1) ? $get["godown"] : ""; ?>"><?php echo ($addflag != 1) ? $lgodown["name"] : ""; ?></option>
                                    <?php
                                    $ggodown = mysqli_query($conn, "SELECT `code`,`name` FROM `godown` WHERE `code`!='' ORDER by name") or die(mysqli_error($conn));
                                    while ($fgodown = mysqli_fetch_array($ggodown)) {
                                        echo "<option style='font-weight:bold;' value='$fgodown[code]'>$fgodown[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
<!--                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Product Type</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form prodtype select2" name="prodtype" style="width: 100%" required>

                                    <option value=""></option>
                                    <option value='P'<?php
                                    if ($addflag != 1) {
                                        if ($get["prodtype"] == "P") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?>>Perfumes</option>
                                    <option value='O'<?php
                                    if ($addflag != 1) {
                                        if ($get["prodtype"] == "O") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?>>Others</option>
                                </select>
                            </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="">
                        <!--                        <h3 class="panel-title">Item Details</h3>-->
                        <!--                        <div class="actions pull-right">
                                                    <i class="fa fa-chevron-down"></i>
                                                </div>-->
                    </div>
                    <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="70%" class='text-left'>Description</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 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 amount"   name="amount_0"      value="0"></td>
                                        </tr>  
                                        <?
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="col-lg-12 pad">
                            <!--<input type="button" style="height: 30px; width: 30px; padding: 0px" id="addnew" class="btn btn-success btn-lg form" accode="addnew2" value="+" />-->
                            <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 class="actions pull-right">
                                                    <i class="fa fa-chevron-down"></i>
                                                </div>-->
                    </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">
                                    </div>
                                    <div class="col-lg-6 pad">
                                    </div>
                                </div>
                                <div class="col-lg-12 col-xs-12 pad">
                                    <div class="col-lg-3 pad text-right">
                                    </div>
                                    <div class="col-lg-6 pad">
                                    </div>
                                </div>
                                <div class="col-lg-12 col-xs-12 pad">
                                    <div class="col-lg-3 pad text-right">
                                    </div>
                                    <div class="col-lg-2 pad text-right">
                                    </div>
                                    <div class="col-lg-1 pad text-left">
                                    </div>
                                    <div class="col-lg-2 pad">
                                    </div>
                                    <div class="col-lg-1 pad">
                                    </div>
                                    <div class="col-lg-3 pad">
                                    </div>
                                </div>
                                <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 remark1" value="<? Echo ($addflag != 1) ? $get["remark1"] : ""; ?>">
                                    </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 remark2" value="<? Echo ($addflag != 1) ? $get["remark2"] : ""; ?>">
                                    </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="text" class="form-control form subtotal text-right" value="<? Echo ($addflag != 1) ? $get["subtotal"] : "0"; ?>" name="subtotal" 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">Less Item Discount</label>
                                                                    </div>
                                                                    <div class="col-lg-3 pad">
                                                                        <input type="text" class="form-control form litemdisc text-right" value="<? Echo ($addflag != 1) ? $get["litemdisc"] : "0"; ?>" name="litemdisc" 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">Less Cash Discount</label>
                                    </div>
                                    <div class="col-lg-2 pad">
                                        <input type="text" class="form-control form cdicsperc text-right" value="<? Echo ($addflag != 1) ? $get["cdiscperc"] : "0"; ?> "  name="cdiscperc">
                                    </div>
                                    <div class="col-lg-1 pad">
                                        <label class="label-control marg">%</label>
                                    </div>
                                    <div class="col-lg-3 pad">
                                        <input type="text" class="form-control form cdiscamt text-right" value="<? Echo ($addflag != 1) ? $get["cdisamt"] : "0"; ?>" name="cdisamt" readonly>
                                    </div>
                                </div>
                                <div class="col-lg-12 col-xs-12 pad">
                                    <div class="col-lg-5 pad text-right">
                                        <label class="label-control marg">Less Other</label>
                                    </div>
                                    <div class="col-lg-2 pad text-right">

                                    </div>

                                    <div class="col-lg-2 pad">
                                    </div>
                                    <div class="col-lg-3 pad">
                                        <input type="text" class="form-control form lessoth text-right" value="<? Echo ($addflag != 1) ? $get["lessoth"] : "0"; ?>" name="lessoth">
                                    </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">Add GST</label>
                                    </div>
                                    <div class="col-lg-2 pad">
                                        <input type="text" class="form-control form vatperc text-right" value="<? Echo ($addflag != 1) ? $get["vatperc"] : "0"; ?> "  name="vatperc">
                                    </div>
                                    <div class="col-lg-1 pad">
                                        <label class="label-control marg">%</label>
                                    </div>
                                    <div class="col-lg-3 pad">
                                        <input type="text" class="form-control form vatamt text-right" value="<? Echo ($addflag != 1) ? $get["vatamt"] : "0"; ?>" name="vatamt" 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">Add Packing</label>
                                    </div>
                                    <div class="col-lg-3 pad">
                                        <input type="text" class="form-control form packing text-right" value="<? Echo ($addflag != 1) ? $get["packing"] : "0"; ?>" name="packing">
                                    </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">Netamt (Rs.)</label>
                                    </div>
                                    <div class="col-lg-3 pad">
                                        <input type="text" class="form-control form netamt text-right" value="<? Echo ($addflag != 1) ? $get["netamt"] : "0"; ?>" name="netamt" 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" 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">
                            <thead>
                                <tr class="mySearchTr" style="background-color: #78909C;">
                                    <th>Voucher</th>
                                    <th>Date</th>
                                    <th>Party</th>
                                    <th>Product Type</th>
                                    <th>Net Amount</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($addflag != 1) {
                                    $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb` where `vouchno`!='' $keycond")or die(mysqli_error($conn));
                                    while ($row = mysqli_fetch_array($srchresult)) {
                                        echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                        echo "<td align='left'>$row[vouchno]</td>";
                                        echo "<td align='left'>$row[vouchdt]</td>";
                                        echo "<td align='left'>$row[party]</td>";
                                        echo "<td align='left'>$row[prodtype]</td>";
                                        echo "<td align='left'>$row[netamt]</td>";
                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\"><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>
