<?php
require("../connection.php");
include("../user.php");
$user_id = $_SESSION['user'];
$branch_id = $_SESSION['branch_id'];
$username = $_SESSION['user_name'];
$ho_access = $_SESSION['ha'];
$branch = $_SESSION['division_name'];
date_default_timezone_set('Asia/Kolkata');
// Calculate financial year options
$currentYear = date('Y');
$currentMonth = date('n');
$fy_start = ($currentMonth >= 4) ? $currentYear : $currentYear - 1;
$fy_end = $fy_start + 1;
$currentFY = "$fy_start-$fy_end";
$selectedYear = $_POST['financial_year'] ?? $currentFY;

$from_date = explode('-', $selectedYear)[0] . "-04-01";
$to_date = explode('-', $selectedYear)[1] . "-03-31";

// Create year options starting from 2024
$yearOptions = "";
for ($y = 2024; $y <= $fy_start; $y++) {
    $fy = "$y-" . ($y + 1);
    $selected = ($selectedYear == $fy) ? 'selected' : '';
    $yearOptions .= "<option value='$fy' $selected>$fy</option>";
}
?>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title><?php echo TITLE; ?></title>
    <meta name="description" content="Ela Admin - HTML5 Admin Template">
    <meta name="viewport" content="width=device-width, initial-scale=1">
<script src="../js/sweet-alert.js"></script>
    <link rel="apple-touch-icon" href="../images/bluebase.png">
    <link rel="shortcut icon" href="../images/bluebase.png">

    <link rel="stylesheet" href="../css/normalize.min.css">
    <link rel="stylesheet" href="../css/bootstrap.min.css">
    <link rel="stylesheet" href="../css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/themify-icons.css">
    <link rel="stylesheet" href="../css/pe-icon-7-stroke.min.css">
    <link rel="stylesheet" href="../css/flag-icon.min.css">
    <link rel="stylesheet" href="../assets/css/cs-skin-elastic.css">
    <link rel="stylesheet" href="../assets/css/style.css">
    <link href="../css/chartist.min.css" rel="stylesheet">
    <link href="../css/jqvmap.min.css" rel="stylesheet">

    <link href="../css/weather-icons.css" rel="stylesheet" />
    <link href="../css/fullcalendar.min.css" rel="stylesheet" />

    <script src="../js/jquery-1.12.4.min.js"></script>
    <script src="../js/jquery-3.3.1.js"></script>
    <script src="../js/jquery.dataTables.min.js"></script>


    <script src="../js/popper.min.js"></script>
    <script src="../js/bootstrap.min.js"></script>
    <script src="../js/jquery.matchHeight.min.js"></script>
    <script src="../assets/js/main.js"></script>
    <script src="../js/jquery-3.3.1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
    <link rel="stylesheet" href="/asset_temp/datatable/css/jquery.dataTables.min.css">
    <link rel="stylesheet" type="text/css" href="/asset_temp/button/css/buttons.css" />
    <script src="/asset_temp/datatable/js/jquery.dataTables.min.js"></script>
    <script src="../js/jquery.table2excel.min.js"></script>
    <style>
        #weatherWidget .currentDesc {
            color: #ffffff !important;
        }

        .traffic-chart {
            min-height: 335px;
        }

        #flotPie1 {
            height: 150px;
        }

        #flotPie1 td {
            padding: 3px;
        }

        #flotPie1 table {
            top: 20px !important;
            right: -10px !important;
        }

        .chart-container {
            display: table;
            min-width: 270px;
            text-align: left;
            padding-top: 10px;
            padding-bottom: 10px;
        }

        #flotLine5 {
            height: 105px;
        }

        #flotBarChart {
            height: 150px;
        }

        #cellPaiChart {
            height: 160px;
        }

        th {
            text-transform: uppercase;
        }

        td {
            text-transform: uppercase;
        }
    </style>

    <link rel="stylesheet" href="<?php echo URL; ?>datatable/css/jquery.dataTables.min.css">
    <script src="<?php echo URL; ?>datatable/js/jquery.dataTables.min.js"></script>
    <!-- button -->
    <link rel="stylesheet" type="text/css" href="<?php echo URL; ?>button/css/buttons.css" /><!-- button -->

</head>

<body>
    <?php require('../sidemenu.php'); ?>
    <!-- /#left-panel -->
    <!-- Right Panel -->
    <div id="right-panel" class="right-panel">
        <!-- Header-->
        <?php require('../header.php'); ?>
        <div class="content">
            <!-- Animated -->
            <div class="animated fadeIn">
                <div style="background-color:white;margin-bottom:10px;">
                    <strong style="color:blue">
                        <center>Outward LIST</center>
                    </strong>
                </div>
                <button class="button button--moema button--text-thick button--text-upper button--size-s"
                    style="padding:1.5px;" onclick="createoutward()"><i
                        class="fa fa-shopping-basket">CREATE</i></button>
                <button class="button button--moema button--text-thick button--text-upper button--size-s"
                    style="padding:1.5px;" onclick="uploadoutward()"><i class="fa fa-upload">outward Upload</i></button>
                <div style="margin-bottom:10px;">
                    <input type="button" class="btn btn-success" value="Export to Excel" onclick="ExportOutward()" />
                    <button class="btn btn-primary" onclick="printDivOutward()"><i class="fa fa-print"></i>
                        Print</button>
                </div>

                <!-- insert this into your form section -->
                <form method="POST" class="form-inline" style="margin-bottom: 15px;">
                    <label for="financial_year">Financial Year:</label>
                    <select name="financial_year" class="form-control mx-2" onchange="this.form.submit()">
                        <?php echo $yearOptions; ?>
                    </select>
                </form>

<div id="editOutwardContainer" style="display:none;"></div>
                <div id="DivIdToPrint">
                    <table id="outwardexample" class="table table-striped table-bordered">
                        <?php
                        if ($ho_access == 'yes') {
                            $where = '';
                        } else {
                            $department_name = mysqli_fetch_array(mysqli_query($con, "select type from type_master where id='$branch_id'"));
                            $department = $department_name['type'];
                            $where = " and department='$department'";
                        }


                        $outward = mysqli_query($con, "
    SELECT outward_master.*, 
           category_master.category_type AS material, 
           mm.name AS qum_name, 
           type_master.type AS dept 
    FROM outward_master 
    LEFT JOIN category_master ON category_master.id = outward_master.material_name 
    LEFT JOIN measurement_master mm ON mm.id = outward_master.qum 
    LEFT JOIN type_master ON type_master.id = outward_master.department 
    WHERE outward_master.status = 0 
      AND so_date BETWEEN '$from_date' AND '$to_date' 
      $where 
    ORDER BY outward_master.id DESC
");


                        ?>
                        <thead>
                            <tr>
                                <th>S.No</th>
                                <th>DATE</th>
                                <th>MR NO</th>
                                <th>TICKET NO</th>
                                <th>MATERIALS NAME</th>
                                <th>QUANTITY</th>
                                <th>QUM</th>
                                <th>DEPARTMENT</th>
                                <th>BRANCH</th>
                                <th>DIVISION</th>
                                <th>MATERIAL REQUESTED BY TECHNICIAN NAME</th>
                                <th>APPROVED BY SHIFT ENGINEER</th>
                                <th>PURPOSE</th>
                                <th>RATE</th>
                                <th>TOTAL AMOUNT</th>
                                <th>ACTION</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $s = 1;
                            while ($type = mysqli_fetch_array($outward)) {
                                ?>
                                <tr>
                                    <td><?php echo $s++; ?></td>
                                    <td><?php echo date("d-m-Y", strtotime($type['so_date'])); ?></td>
                                    <td><?php echo $type['mr_no']; ?></td>
                                    <td><?php echo $type['ticket_no']; ?></td>
                                    <td><?php echo $type['material']; ?></td>
                                    <td><?php echo $type['qty']; ?></td>
                                    <td><?php echo $type['qum_name']; ?></td>
                                    <td><?php echo $type['dept']; ?></td>
                                    <td>
                                        <?php
                                        $brnch = $type['branch'];
                                        $get_brnch_type = $con->query("select * from location_master where id='$brnch'");
                                        $brnch_det = $get_brnch_type->fetch_array();
                                        $brnch_name = $brnch_det['location'];
                                        echo $brnch_name;
                                        ?>
                                    </td>
                                    <td>
                                        <?php
                                        $divsn = $type['division'];
                                        $get_divsn_type = $con->query("select * from branch_master where id='$divsn'");
                                        $divsn_det = $get_divsn_type->fetch_array();
                                        $divsn_name = $divsn_det['branch_name'];
                                        echo $divsn_name;
                                        ?>
                                    </td>
                                    <td><?php echo $type['technician']; ?></td>
                                    <td><?php echo $type['approved_by']; ?></td>
                                    <td><?php echo $type['purpose']; ?></td>
                                    <td><?php echo $type['value']; ?></td>
                                    <td><?php echo $type['total_amount']; ?></td>
                                    <td class="text-left">
                                        <button type="button" class="btn btn-link btn-round" id="<?php echo $type['id']; ?>"
                                            title="Edit User" onclick="editoutward(this.id)"><i
                                                class="fa fa-edit"></i></button>
                                        <button type="button" onclick="delete_outward(this.id)"
                                            id="<?php echo $type['id']; ?>" class="btn btn-link btn-round" title="Delete"><i
                                                class="fa fa-trash"></i></button>
                                    </td>
                                </tr>
                                <?php
                            }
                            ?>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
    

</body>

</html>

<?php require("../mainjavascript.js"); ?>