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

$formname = 'Salary Paid / Unpaid';


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

$zyear = right($zdb, 4);
$mfrdate = getzdates($zyear, 'S');
$mtodate = getzdates($zyear, 'E');

$myyear = $_SESSION[$myyear];
$file = "salpd_fr.php";
mysqli_select_db($conn, $zdb);
$date = date("Y-m-d");
include("includes/linkshead.php");
?>
<script src="MyScript/salpd.js" type="text/javascript"></script>
</head>

<?php
if (isset($_POST['submit'])) {

    $count = $_POST['items'];
//browse($count);
    for ($i = 0; $i < $count; $i++) {
        $cnt++;
        $msrno = $_POST["srno_$i"];
        $loan = $_POST["loan_$i"];
        $total_ded = $_POST["total_ded_$i"];
        $netsal = $_POST["net_sal_$i"];
        $paid = $_POST["paid_$i"];
        $deptcode = $_POST["deptcode_$i"];
        $empcode = $_POST["empcode_$i"];
        $accode = $_POST["accode_$i"];
        $monthno = $_POST["monthno_$i"];
        $yearno = $_POST["yearno_$i"];

//        for bank and cash receipt part
        $modepay = $_POST["modepay_$i"];
        $bnkcsh = $_POST["bnkcsh_$i"];
        $bankname = $_POST["bankname_$i"];



        if ($paid == 'Y') {
//            $mcashvouch = $_POST["cashvouch"];
//            if ($modepay == 'C') {
//                if ($bnkcsh == '0' || $bnkcsh == '') {
//                    $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `cashpay` where vouchno!=''  LIMIT 0,1 ") or die(mysqli_error($conn));
//                    if (mysqli_num_rows($gen1) > 0) {
//                        $vvouch = mysqli_fetch_assoc($gen1);
//                        $tcashvouch = $vvouch["maxcode"] + 1;
//                    } else {
//                        $tcashvouch = '1';
//                    }
//                    $bnkcsh = $tcashvouch;
//                }
//
//                $genrecp = mysqli_query($conn, "SELECT * FROM `cashpay` where `vouchno` = '$bnkcsh' ") or die(mysqli_error($conn));
//
////                print table($genrecp);
//                if (mysqli_num_rows($genrecp) > 0) {
//
//                    $fetrecp = mysqli_fetch_array($genrecp);
//                    $mrecpno = $fetrecp[recpno];
//                    $bnkcsh = $fetrecp[vouchno];
//                } else {
//
//                    $gen2 = mysqli_query($conn, "SELECT MAX(recpno) AS maxcode FROM `cashpay` where bankname = '$bankname' LIMIT 0,1 ") or die(mysqli_error($conn));
//                    if (mysqli_num_rows($gen2) > 0) {
//                        $gvouch = mysqli_fetch_assoc($gen2);
//                        $mrecpno1 = $gvouch["maxcode"] + 1;
//                    } else {
//                        $mrecpno1 = '1';
//                    }
//                    $mrecpno = $mrecpno1;
//                }
//
////                browse($bnkcsh);
//                mysqli_query($conn, "DELETE FROM `cashpay` WHERE `vouchno`='$bnkcsh' ") or die(mysqli_error($conn));
////                browse($bnkcsh);
//
//                $qrygrp1 = mysqli_query($conn, "select `grpcode`,bankinit from accmast where `accode` = '$bankname'") or die(mysqli_error($conn));
//                $fetgrp1 = mysqli_fetch_array($qrygrp1);
//                $pgrp1 = $fetgrp1['grpcode'];
//                $pbinit1 = $fetgrp1['bankinit'];
//
//                $qrygrp = mysqli_query($conn, "select `grpcode` from accmast where `accode` = '$accode'") or die(mysqli_error($conn));
//                $fetgrp = mysqli_fetch_array($qrygrp);
//                $pgrp = $fetgrp['grpcode'];
//
//                mysqli_query($conn, "INSERT INTO `cashpay`( vouchno ,`vouchdt`, `bankname`, `bankinit`, `recpno`, `party`,`amount`,`remark1`,`remark2`,`trnflag`, businestype)
//                                       Values ('$bnkcsh', '$date', '$bankname', '$pbinit1', '$mrecpno','$accode','$netsal','Cash Paid. For','Salary of Month .$monthno','S', '$businestype')") or die(mysqli_error($conn));
//
//
//                // cash part  
//
//                $minus_amt = ($netsal * -1);
//
//
//                mysqli_query($conn, "DELETE FROM `actran` WHERE `vouchno`='$bnkcsh' and `source` = 'CP' $keycond") or die(mysqli_error($conn));
//
//
//
//                mysqli_query($conn, "INSERT INTO `actran`(date,vouchno, recpno, source, trnno, bankinit, accode, grpcode, amount, trnflag, businestype)
//                    VALUES ('$date','$bnkcsh', '$mrecpno', 'CP', '001', '$pbinit1', '$bankname','002', '$netsal','S', '$businestype')") or die(mysqli_error($conn));
//
//
//                // party part
//                mysqli_query($conn, "INSERT INTO `actran`(date,vouchno, recpno, source, trnno, bankinit, accode, grpcode, amount, trnflag, businestype)
//                    VALUES ('$date','$bnkcsh', '$mrecpno', 'CP', '002', '$pbinit1', '$accode', '$pgrp', '$minus_amt','S', '$businestype')") or die(mysqli_error($conn));
//            }
//
////            bank part
//
//            if ($modepay == 'B') {
//                if ($bnkcsh == '0' || $bnkcsh == '') {
//                    $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `bankpay` where vouchno!=''  LIMIT 0,1 ") or die(mysqli_error($conn));
//                    if (mysqli_num_rows($gen1) > 0) {
//                        $vvouch = mysqli_fetch_assoc($gen1);
//                        $tcashvouch = $vvouch["maxcode"] + 1;
//                    } else {
//                        $tcashvouch = '1';
//                    }
//                    $bnkcsh = $tcashvouch;
//                }
//
//                $genrecp = mysqli_query($conn, "SELECT * FROM `bankpay` where `vouchno` = '$bnkcsh' ") or die(mysqli_error($conn));
//
////                print table($genrecp);
//                if (mysqli_num_rows($genrecp) > 0) {
//
//                    $fetrecp = mysqli_fetch_array($genrecp);
//                    $mrecpno = $fetrecp[recpno];
//                    $bnkcsh = $fetrecp[vouchno];
//                } else {
//
//                    $gen2 = mysqli_query($conn, "SELECT MAX(recpno) AS maxcode FROM `bankpay` where bankname = '$bankname' LIMIT 0,1 ") or die(mysqli_error($conn));
//                    if (mysqli_num_rows($gen2) > 0) {
//                        $gvouch = mysqli_fetch_assoc($gen2);
//                        $mrecpno1 = $gvouch["maxcode"] + 1;
//                    } else {
//                        $mrecpno1 = '1';
//                    }
//                    $mrecpno = $mrecpno1;
//                }
//
////                browse($bnkcsh);
//                mysqli_query($conn, "DELETE FROM `bankpay` WHERE `vouchno`='$bnkcsh' ") or die(mysqli_error($conn));
////                browse($bnkcsh);
//
//                $qrygrp1 = mysqli_query($conn, "select `grpcode`,bankinit from accmast where `accode` = '$bankname'") or die(mysqli_error($conn));
//                $fetgrp1 = mysqli_fetch_array($qrygrp1);
//                $pgrp1 = $fetgrp1['grpcode'];
//                $pbinit1 = $fetgrp1['bankinit'];
//
//                $qrygrp = mysqli_query($conn, "select `grpcode`,businestype from accmast where `accode` = '$accode'") or die(mysqli_error($conn));
//                $fetgrp = mysqli_fetch_array($qrygrp);
//                $pgrp = $fetgrp['grpcode'];
//                $businestype = $fetgrp['businestype'];
//
//
//
//                mysqli_query($conn, "INSERT INTO `bankpay`( vouchno ,`vouchdt`, `bankname`, `bankinit`, `recpno`, `party`,`amount`,`remark1`,`remark2`,`trnflag`, businestype)
//                                       Values ('$bnkcsh', '$date', '$bankname', '$pbinit1', '$mrecpno','$accode','$netsal','Paid By Bank','Salary of Month .$monthno','S', '$businestype')") or die(mysqli_error($conn));
//
//
//                // cash part  
//
//                $minus_amt = ($netsal * -1);
//
//
//                mysqli_query($conn, "DELETE FROM `actran` WHERE `vouchno`='$bnkcsh' and `source` = 'BP' $keycond") or die(mysqli_error($conn));
//
//
//
//                mysqli_query($conn, "INSERT INTO `actran`(date,vouchno, recpno, source, trnno, bankinit, accode, grpcode, amount, trnflag, businestype)
//                    VALUES ('$date','$bnkcsh', '$mrecpno', 'BP', '001', '$pbinit1', '$bankname','002', '$netsal','S', '$businestype')") or die(mysqli_error($conn));
//
//
//                // party part
//                mysqli_query($conn, "INSERT INTO `actran`(date,vouchno, recpno, source, trnno, bankinit, accode, grpcode, amount, trnflag, businestype)
//                    VALUES ('$date','$bnkcsh', '$mrecpno', 'BP', '002', '$pbinit1', '$accode', '$pgrp', '$minus_amt','S', '$businestype')") or die(mysqli_error($conn));
//            }
//        } else {
//
//            if ($modepay == 'B') {
//                mysqli_query($conn, "Delete from `bankpay` where vouchno='$bnkcsh'") or die(mysqli_error($conn));
//                mysqli_query($conn, "Delete from `actran` where vouchno='$bnkcsh' and source = 'BP'") or die(mysqli_error($conn));
//            } else {
//
//                mysqli_query($conn, "Delete from `cashpay` where vouchno='$bnkcsh'") or die(mysqli_error($conn));
//                mysqli_query($conn, "Delete from `actran` where vouchno='$bnkcsh' and source = 'CP'") or die(mysqli_error($conn));
//            }
//            $bankname = '';
//            $bnkcsh = '';
//            $modepay = '';
//        }

            mysqli_query($conn, "UPDATE `payproc` SET  `loan` = '$loan', `total_ded`='$total_ded', `net_sal`='$netsal', `paid`='$paid',
                         paymode='$modepay', bnkcsh='$bnkcsh', bankname='$bankname' where accode = '$accode' and monthno = '$monthno' and yearno = '$yearno'") or die(mysqli_error($conn));

            mysqli_query($conn, "UPDATE `loanpay` SET  `loanamt` = '$loan' where month = '$monthno' and accode= '$accode' and yearno = '$yearno' ") or die(mysqli_error($conn));
        }
    }

    updmess();
    header("refresh: 1; $file");
    echo "<script>window.close()</script>";
}
?>
<body>
    <?php
//  if (isset($_POST["view"])) {

    $empname = $_POST["empname"];
    $status = $_POST["status"];
    $month = $_POST["month"];
    $depart = $_POST["depart"];
    $year = $_POST["year"];
    $process = $_POST["process"];

    $empnamecond = "";
    $departcond = "";
    $processcond = "";

    if (!empty($empname)) {
        $empnamecond = "AND  `accode`  = '$empname'";
    }
    if (!empty($depart)) {
        $deptcond = "AND  `deptcode`  = '$depart'";
    }
    if (!empty($process)) {
        $processcond = "AND  `process`  = '$process'";
    }
    if ($status != 'B') {
        $statuscond = "AND  `paid`  = '$status' ";
    }

    $mainqry = mysqli_query($conn, "SELECT * FROM payproc where monthno='$month' and yearno = '$year' 
                                    and accode in (Select accode from accmast where accode != '' $processcond) 
                                    $empnamecond $deptcond  $statuscond order by empname")or die(mysqli_errno($conn));
    ?>
    <form action='' method="POST">

        <div class="container-fluid">
            <? include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 ">
                    <div class="table-responsive" style="max-height: 500px;overflow-y: auto;">
                        <table class="table table-striped table-condensed table-bordered"  style="width: 100%">
                            <thead class="mySearchTr">
                                <tr>

                                    <th width="17%">Employee</th>
                                    <th width="4%">Dept</th>
                                    <!--<th width="12%">Division</th>-->
                                    <th width="5%" class='text-right'>Salary</th>
                                    <th width="5%" class='text-right'>Weekly Off</th>
                                    <th width="5%" class='text-right'>HO</th>
                                    <th width="5%" class='text-right'>PR</th>
                                    <!-- <th width="5%" class='text-right'>OT/Night</th> -->
                                    <!--<th width="5%" class='text-right'>Hrs</th>-->
                                    <th width="5%" class='text-right'>PD</th>
                                    <th width="5%" class='text-right'>Advance</th>
                                    <!-- <th width="5%" class='text-right'>Food</th> -->
                                    <th width="5%" class='text-right'>Loan</th>
                                    <th width="5%" class='text-right'>Total.Dedn</th>
                                    <th width="5%" class='text-right'>Net.Salary</th>
                                    <th width="2%" class=''>Paid</th>
<!--                                    <th width="2%" class=''>C/B</th>
                                    <th width="14%" class=''>Bank/Cash Name</th>-->
                                </tr>
                            </thead>
                            <tbody class="tabledata">
                                <?php
                                $ctr = 0;
                                while ($fmain = mysqli_fetch_array($mainqry)) {

                                    $getdep = mysqli_query($conn, "SELECT code,name FROM dept  WHERE code = '$fmain[deptcode]' ")or die(mysqli_error($conn));
                                    $fetdep = mysqli_fetch_array($getdep);
                                    $deptcode = $acc['code'];
                                    $deptname = $fetdep['name'];
                                    
                                    $getbank = mysqli_query($conn, "SELECT accode, name FROM `accmast` WHERE `accode`='$fmain[bankname]'") or die(mysqli_error($conn));
                                    $fetbank = mysqli_fetch_array($getbank);
                                    $rd = '';
                                    $pdflg = $fmain['paid'];
                                    if ($pdflg == 'Y') {
                                        $rd = 'readonly';
                                    }

                                    Echo "<tr>";
                                    Echo "<td><input type='text' class='form-control name'         name='name_$ctr'         value='$fmain[empname]'     readonly>
                                              <input type='hidden' class='form-control empcode'    name='empcode_$ctr'      value='$fmain[empcode]'  readonly>
                                              <input type='hidden' class='form-control monthno'    name='monthno_$ctr'      value='$fmain[monthno]'  readonly>
                                              <input type='hidden' class='form-control yearno'     name='yearno_$ctr'      value='$fmain[yearno]'  readonly>
                                              <input type='hidden' class='form-control bnkcsh'     name='bnkcsh_$ctr'      value='$fmain[bnkcsh]'  readonly>
                                              <input type='hidden' class='form-control accode'     name='accode_$ctr'      value='$fmain[accode]'  readonly></td>";
                                    Echo "<td><input type='text' class='form-control deptname'     name='deptname_$ctr'     value='$deptname'    readonly tabindex='-1'>"
                                    . "<input type='hidden' class='form-control deptcode'     name='deptcode_$ctr'     value='$fmain[deptcode]'    readonly tabindex='-1'></td>";
//                                    Echo "<td><input type='text' class='form-control divcode'     name='divcode_$ctr'     value='$divname'    readonly tabindex='-1'></td>";
                                    Echo "<td><input type='text' class='form-control text-right salary'       name='salary_$ctr'       value='$fmain[salary]'      readonly tabindex='-1'></td>";
                                    Echo "<td><input type='text' class='form-control text-right sunsal'       name='sunsal_$ctr'       value='$fmain[sunsal]'      readonly tabindex='-1'></td>";
                                    Echo "<td><input type='text' class='form-control text-right hoamt'       name='hoamt_$ctr'       value='$fmain[hoamt]'      readonly tabindex='-1'></td>";
                                    Echo "<td><input type='text' class='form-control text-right pramt'       name='pramt_$ctr'       value='$fmain[pramt]'      readonly tabindex='-1'>
                                              <input type='hidden' class='form-control text-right otamt'       name='otamt_$ctr'       value='$fmain[otamt]'      readonly tabindex='-1'></td>";
                                    Echo "<td><input type='text' class='form-control text-right pdamt'       name='pdamt_$ctr'       value='$fmain[pdsal]'      readonly tabindex='-1'></td>";
                                    Echo "<td><input type='text' class='form-control text-right advance'      name='advance_$ctr'      value='$fmain[advance]'     readonly tabindex='-1'>
                                              <input type='hidden' class='form-control text-right food'      name='food_$ctr'      value='$fmain[food]'     readonly tabindex='-1'></td>";
                                    Echo "<td><input type='text' class='form-control text-right loan'         name='loan_$ctr'         value='$fmain[loan]' ></td>";

                                    Echo "<td><input type='text' class='form-control text-right totded'       name='total_ded_$ctr'    value='$fmain[total_ded]'   readonly tabindex='-1'>";
                                    Echo "<input type='hidden' class='form-control text-right provfund'     name='provfund_$ctr'     value='$fmain[provfund]'    readonly tabindex='-1'>";
                                    Echo "<input type='hidden' class='form-control text-right proftax'      name='proftax_$ctr'      value='$fmain[proftax]'     readonly  tabindex='-1'>";
                                    Echo "<input type='hidden' class='form-control text-right esic'         name='esic_$ctr'         value='$fmain[esic]'        readonly  tabindex='-1'></td>";



                                    Echo "<td><input type='text' class='form-control text-right netsal'       name='net_sal_$ctr'      value='$fmain[net_sal]'     readonly tabindex='-1'></td>";
                                    Echo "<td><input type='text' class='form-control  paid'         name='paid_$ctr'         value='$fmain[paid]' ></td>";
//                                    Echo "<td><input type='text' class='form-control text-right basic'        name='basic_$ctr'        value='$fmain[basic]'      readonly tabindex='-1'></td>";
//                                    Echo "<td><input type='text' class='form-control text-right da'           name='da_$ctr'           value='$fmain[da]'      readonly tabindex='-1'></td>";
//                                    Echo "<td><input type='text' class='form-control text-right hra'          name='hra_$ctr'          value='$fmain[hra]'      readonly tabindex='-1'></td>";
//                                    Echo "<td><input type='text' class='form-control text-right ta'           name='ta_$ctr'           value='$fmain[ta]'      readonly tabindex='-1'></td>";
//                                    Echo "<td><input type='text' class='form-control text-right oa'           name='oa_$ctr'           value='$fmain[oa]'      readonly tabindex='-1'> </td>";
//                                    Echo "<td><input type='text' class='form-control modepay' name='modepay_$ctr' value='$fmain[paymode]' $rd></td>";
//                                    Echo "<td>";
//                                    echo "<select class='form-control bankname' name='bankname_$ctr'>";
//                                    echo "<option value='$fetbank[accode]'>$fetbank[name]</option>";
//                                    $getbnk = mysqli_query($conn, "Select accode, name from accmast where grpcode = '001' order by name") or die(mysqli_error($conn));
//                                    while ($fetbnk = mysqli_fetch_array($getbnk)) {
//                                        echo "<option value='$fetbnk[accode]'>$fetbnk[name]</option>";
//                                    }
//                                    echo "</select>";
//                                    echo "</td>";
                                    Echo "</tr>";
                                    $ctr++;
                                }
                                ?>
                            <input type="hidden" id="items" name="items" value="<?php echo $ctr; ?>" >
                            </tbody>
                        </table>
                    </div>
                </div>
                <table class="table-condensed" style="margin: 0 auto;">
                    <tr>
                        <td class=" buttonform tooltip-bottom" id="ssubmit" style="display:<? Echo $savedisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Save Record">
                            <button id='submit' type='submit' name='submit' value='submit' class="btn btn-pad btn-square " style="background-color:#008D4C; ">
                                <i class="fa fa-check" style="color:#ffffff"></i>
                            </button>
                        </td>
                        <td class=" buttonform tooltip-bottom" id="scancel" style="display: <? Echo $canceldisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Close">
                            <button id='cancel' type="reset" class="btn btn-pad btn-square " style="background-color:red;" onclick="window.close()">
                                <i class="fa fa-power-off" style="color:#ffffff"></i>
                            </button>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </form>
    <?php
//    }
    ?>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
