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

$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];
$employee = $_POST["cmbemp"];
$cmbdept = $_POST["cmbdept"];

$month = $_POST["month"];
mysqli_select_db($conn, $zdb);
$zyear = right($zdb, 4);
$myyear = $_SESSION[$myyear];

//$getyear = mysqli_query($conn, "SELECT syear, eyear from $zroot.compyear where finyear = '$zyear' ") or die(mysqli_error($conn));
//$fyear = mysqli_fetch_array($getyear);


$yearno = $_POST["year"];
//if ($month > 3) {
//    $year = $fyear['syear'];
//} else if ($month < 4) {
//    $year = $fyear['eyear'];
//}
//browse($employee);
if (!empty($employee)) {
    $emplcond = "AND accode = '$employee'";
}

//browse($month);
//browse($cmbdept);
//browse($yearno);
//browse($emplcond);

$mainqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt, DATE_FORMAT(`dateproc`, '%d-%m-%Y')as procdt
                                FROM `payproc` WHERE  monthno='$month'
                                AND deptcode = '$cmbdept' AND yearno='$yearno' 
                                $emplcond  ORDER BY empname") or die(mysqli_error($conn));

$getno = mysqli_num_rows($mainqry);

if ($getno == 0) {
    browse("No Record Found ");
    echo "<script>window.close()</script>";
}

if ($month == 1) {
    $curr_month = "January";
}
if ($month == 2) {
    $curr_month = "February";
}
if ($month == 3) {
    $curr_month = "March";
}
if ($month == 4) {
    $curr_month = "April";
}
if ($month == 5) {
    $curr_month = "May";
}
if ($month == 6) {
    $curr_month = "June";
}
if ($month == 7) {
    $curr_month = "July";
}
if ($month == 8) {
    $curr_month = "August";
}
if ($month == 9) {
    $curr_month = "September";
}
if ($month == 10) {
    $curr_month = "October";
}
if ($month == 11) {
    $curr_month = "November";
}
if ($month == 12) {
    $curr_month = "December";
}

$compcode = $_SESSION["comp_" . $dbname];

$getcomp2 = mysqli_query($conn, "SELECT * FROM $zroot.`company` WHERE code = '$compcode' ") or die(mysqli_error($conn));
$comp2 = mysqli_fetch_array($getcomp2);
include('include/linkshead.php');
?>
<html>

<head>
    <title>Salary |Slip</title>
    <link href="MyStyle/inv_format.css" rel="stylesheet">

    <style>
        body {
            font-size: 13px;
        }

        .bor_rg {
            /*border-left: 1px solid black;*/
            border-right: 1px solid black;
        }

        .numeric {
            text-align: right
        }

        .fontnew {
            font-family: 'Verdana';
            font-size: 13px;
            font: 'bold';
        }

        .border {
            border-top-style: solid;
            border-right-style: solid;
            border-bottom-style: solid;
            border-left-style: solid;
            border-width: 1px;
            border-color: black;
        }

        .uppercase {
            text-transform: uppercase;
        }

        table tr td {
            font-size: 15px;
        }

        .table-condensed>tbody>tr>td {
            padding: 3px;
            font-size: 12px;
        }

        .padd {
            padding: 8px;
        }

        .bold {
            font-weight: bold;
        }

        .page {
            width: 210mm;
            min-height: 297mm;
            padding: 1mm;
            margin: 10mm auto;
            border: 1px #D3D3D3 solid;
            border-radius: 5px;
            background: white;

        }

        .subpage {
            padding-right: 0.5cm;
            padding-left: 0.5cm;
            padding: 0cm;
            height: 288mm;
            outline: 2cm #ffffff solid;
        }

        table {
            border-collapse: collapse;
            width: 100%;
        }

        .text-center {
            text-align: center;
        }

        .text-right {
            text-align: right;
        }
    </style>
</head>

<body>
    <?php
    $ctr = 0;
    $netsalary = 0;

    while ($fmain = mysqli_fetch_array($mainqry)) {

        $deptcode = $fmain['deptcode'];
        $empcode = $fmain['empcode'];
        $getdept = mysqli_query($conn, "SELECT name FROM `dept` WHERE code='$deptcode'") or die(mysqli_error($conn));
        $dept = mysqli_fetch_array($getdept);

        $getempdet = mysqli_query($conn, "SELECT acno,designation FROM `accmast` WHERE custcode='$empcode' ") or die(mysqli_error($conn));
        $empdet = mysqli_fetch_array($getempdet);

        $ctr++;
        if ($ctr == 1) {
            ?>
            <div class="page">
                <div class="subpage">
                <? } ?>
                <br>
                <br>
                <table>
                    <tr class="border">
                        <td colspan="4" class="uppercase text-center" style="padding: 10px">
                            <label style="text-align: center;font-size: 25px;font-family: 'Times New Roman';">
                                <b><? echo $comp2['compname'] ?></b></label><br>
                            <label style="text-align: center;font-size: 15px;font-family: 'Times New Roman';">
                                <?= $comp2['hoadd1'] . " " . $comp2['hoadd2'] . " " . $comp2['hoadd3']; ?><br>
                            </label>
                            <label style="text-align: center;font-size: 15px;font-family: 'Times New Roman';">
                                <? echo $comp2['hocity'] . " - " . $comp2['hopin'] ?></label><br>
                            <label style="text-align: center;font-size: 15px;font-family: 'Times New Roman';">
                                Contact No.:  <? echo $comp2['htel']  ?></label><br>
                        </td>
                    </tr>
                    <tr class="border">
                        <td colspan="4" class="text-center padd bold" style="font-size:14px">Salary For The Month Of -
                            <?= $curr_month . " " . $year; ?></td>
                    </tr>
                    <tr class="border">
                        <td style="width:17%" class="bor_rg padd">
                            <table>
                                <tr>
                                    <td>EmpCode :</td>
                                </tr>
                                <tr>
                                    <td>Name :</td>
                                </tr>
                                <tr>
                                    <td>Designation :</td>
                                </tr>
                                <tr>
                                    <td>No of Days :</td>
                                </tr>
                            </table>
                        </td>
                        <td style="width:30%" class="padd">
                            <table>
                                <tr>
                                    <td><?= $fmain['empcode']; ?></td>
                                </tr>
                                <tr>
                                    <td><?= $fmain['empname']; ?></td>
                                </tr>
                                <tr>
                                    <td><?= $empdet['designation']; ?> &nbsp;</td>
                                </tr>
                                <tr>
                                    <td><?= $fmain['daydetail']; ?></td>
                                </tr>
                            </table>
                        </td>
                        <td style="width:22%" class="bor_rg padd">
                            <table>
                                <tr>
                                    <td class="text-right">Department</td>
                                </tr>
                                <tr>
                                    <td class="text-right">Account No.</td>
                                </tr>
                                <tr>
                                    <td>&nbsp;</td>
                                </tr>
                                <tr>
                                    <td>&nbsp;</td>
                                </tr>
                            </table>
                        </td>
                        <td style="width:33%" class="padd">
                            <table>
                                <tr>
                                    <td>&nbsp;<?= $dept['name']; ?></td>
                                </tr>
                                <tr>
                                    <td><?= $empdet['acno']; ?> &nbsp;</td>
                                </tr>
                                <tr>
                                    <td>&nbsp; </td>
                                </tr>
                                <tr>
                                    <td>&nbsp;</td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>

                <table>
                    <tr>
                        <td class="border text-center padd bold" colspan="2">Earning</td>
                        <td class="border text-center padd bold" colspan="2">Deduction</td>
                    </tr>
                    <tr class="border">
                        <td style="width:17%" class="bor_rg padd">
                            <table>
                                <tr>
                                    <td>Monthly Salary</td>
                                </tr>
                            </table>
                        </td>

                        <td style="width:16%" class="padd bor_rg">
                            <table>
                                <tr>
                                    <td class="text-right "><?= $fmain['totalamt']; ?></td>
                                </tr>
                            </table>
                        </td>

                        <td style="width:18%" class="bor_rg padd">
                            <table>

                                <tr>
                                    <td>Loan</td>
                                </tr>
                                <tr>
                                    <td>Advance/Other Dedn.</td>
                                </tr>
                            </table>
                        </td>

                        <td style="width:16%" class="padd bor_rg">
                            <table>
                                <tr>
                                    <td class="text-right ">&nbsp;<?= $fmain['loan']; ?></td>
                                </tr>
                                <tr>
                                    <td class="text-right ">&nbsp;<?= $fmain['advance'] ?></td>
                                </tr>
                            </table>
                        </td>
                    </tr>

                    <tr class="border">
                        <td style="width:17%" class="bor_rg text-right padd bold">
                            Total
                        </td>
                        <td style="width:16%" class="padd bor_rg text-right padd ">
                            <?= number_format($fmain['totalamt'], 2); ?>
                        </td>
                        <td style="width:21%" class="bor_rg text-right padd bold">
                            Total
                        </td>
                        <td style="width:16%" class="padd bor_rg text-right ">
                            <?= number_format($fmain['total_ded'], 2); ?>
                        </td>
                    </tr>

                    <?php $netsalary = $fmain['totalamt'] - $fmain['total_ded']; ?>

                    <tr class="bold">
                        <td colspan="4" class="padd text-left" style="border: 1px solid #000;">Net Salary :-
                            ₹<?= round($netsalary); ?>   &nbsp;&nbsp;&nbsp;&nbsp; ( <?= rsword($netsalary) ?>) </td>
                    </tr>
                    <tr>
                        <td colspan="6">
                            <h5>This is a Computer Generated Slip,hence no Signature Required.</h5>
                        </td>
                    </tr>
                </table>


                <?php 
                if ($ctr == 1) {
?>
                <br>
                <br>
                <br>
                <?php 
                }
?>
                <br>
                <br>
                <br>
                <br>
                <?php
                $mwords = round($qry['billamt']);
                $words = rsword($mwords);
                if ($ctr == 2) {
                    echo "</div>";
                    echo "</div>";
                    $ctr = 0;
                }
                ?>


            <? } ?>
</body>

</html>