<?php session_start();
include('../accesscheck2.php');
require_once('../Connections/courier.php');
require_once '../functions/functions.php';
mysql_select_db($database_courier, $courier);
$report_type = $_GET['id'];

?>


<head>
    <meta charset="utf-8" />
    <title> Report Generation </title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content="width=device-width, initial-scale=0" name="viewport" />
    <meta content="" name="description" />
    <meta content="" name="author" />
    <script src="../assets/global/plugins/jquery.min.js" type="text/javascript"></script>
    <link href="http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700&subset=all" rel="stylesheet" type="text/css" media="all" />
    <link href="../assets/global/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" media="all"/>
</head>
<style type="text/css" media="all">
    body{
        margin: 0;
    }
    .text-danger strong {
        color: #9f181c;
    }
    .text-danger {
        color: #9f181c;
    }
    .receipt-main {
        background: #ffffff none repeat scroll 0 0;
        /*border-bottom: 12px solid #333333;*/
        border-top: 12px solid #9f181c;
        /*margin-top: 50px;*/
        margin-bottom: 50px;
        padding: 40px 30px !important;
        position: relative;
        box-shadow: 0 1px 21px #acacac;
        color: #333333;
        font-family: open sans;
    }
    .receipt-main p {
        color: #333333;
        font-family: open sans;
        line-height: 1.42857;
    }
    .receipt-footer h1 {
        font-size: 15px;
        font-weight: 400 !important;
        margin: 0 !important;
    }
    .receipt-main::after {
        background: #414143 none repeat scroll 0 0;
        content: "";
        height: 5px;
        left: 0;
        position: absolute;
        right: 0;
        top: -13px;
    }
    .receipt-main thead {
        background: #414143 none repeat scroll 0 0;
    }
    .receipt-main thead th {
        color:#fff;
    }
    .receipt-main .amount {
        text-align: right
    }
    .receipt-right h5 {
        font-size: 16px;
        font-weight: bold;
        margin: 0 0 7px 0;
    }
    .receipt-right p {
        font-size: 12px;
        margin: 0px;
    }
    .receipt-right p i {
        text-align: center;
        width: 18px;
    }
    .receipt-main td {
        padding: 9px 20px !important;
    }
    .receipt-main th {
        padding: 12px 15px !important;
    }
    .receipt-main td {
        font-size: 13px;
        font-weight: initial !important;
    }
    .receipt-main td p:last-child {
        margin: 0;
        padding: 0;
    }
    .receipt-main td h2 {
        font-size: 20px;
        font-weight: 900;
        margin: 0;
        text-transform: uppercase;
    }
    .receipt-header-mid .receipt-left h1 {
        font-weight: 100;
        margin: 34px 0 0;
        text-align: right;
        text-transform: uppercase;
    }
    .receipt-header-mid {
        margin: 24px 0;
        overflow: hidden;
    }

    #container {
        background-color: #dcdcdc;
    }
</style>



<div class="container-fluid">
    <div class="row">
        <div class="receipt-main col-xs-12 col-sm-12 col-md-12">
            <div class="row">
                <div class="receipt-header">
                    <div class="col-xs-6 col-sm-6 col-md-6">
                        <div class="receipt-left">
                            <img class="img-responsive" src="../images/app_pics/rns_logo.jpeg" style="width: 71px;">
                        </div>
                    </div>
                    <div class="col-xs-6 col-sm-6 col-md-6 text-right">
                        <div class="receipt-right">
                            <h5>ROBERT AND SONS LTD.</h5>
                            <p>+233034789/09678909 <i class="fa fa-phone"></i></p>
                            <p>INFO@ROBERTANDSON.COM <i class="fa fa-envelope-o"></i></p>
                            <p>ADABRAKA BRANCH <i class="fa fa-location-arrow"></i></p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="receipt-header receipt-header-mid">
                    <div class="col-xs-8 col-sm-8 col-md-8 text-left">
                        <div class="receipt-right">
                            <h3> <?php echo strtoupper('STOCK RECONCILATION REPORT'); ?> </h3>
                            <h5 class="text-danger"> BRANCHES : <?php if($report_type=='branch_stock'){ echo strtoupper($_SESSION['name_code']); }else{ echo 'ALL BRANCHES';} ?>  </h5>
                        </div>
                    </div>
                    <div class="col-xs-4 col-sm-4 col-md-4">
                        <div class="receipt-left">
                            <h1>Report</h1>
                        </div>
                    </div>
                </div>
            </div>

            <table class="table table-striped table-bordered table-hover dataTable no-footer" id="datatable_products" aria-describedby="datatable_products_info" role="grid">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>ITEM NAME</th>
                    <th>SYSTEM QTY BEFORE</th>
                    <th>SYSTEM QTY NOW</th>
                    <th>PHYSICAL QTY</th>
                    <th>VARIATION</th>
                    <th>BRANCH</th>
                    <th>ADJUST </th>
                    <th>UNIT PRICE </th>
                    <th>AMOUNT </th>

                </tr>
                </thead>
                <tbody>
                <?php
                function GetAdjustedQuantity($item_id,$tracking_no,$item_date){
                    global $courier;
                    $select = "SELECT * FROM stocktaking WHERE item_id = '$item_id' AND tracking_no = '$tracking_no' AND `date` = '$item_date'";
                    $rsodest_select = mysql_query($select, $courier) or die(mysql_error());
                    $item_quantity = mysql_fetch_assoc($rsodest_select);
                    return $item_quantity['variation'];
                }


                function unitPrice($item_id, $barnch){
                    global $courier;
                    $select = "SELECT * FROM service_items WHERE id = '$item_id'";
                    $rsodest_select = mysql_query($select, $courier) or die(mysql_error());
                    $item_quantity = mysql_fetch_assoc($rsodest_select);
                    return $item_quantity['price_'.$barnch];
                }

                $count = 0;
                $selectedDate = $_GET['date'];
                $currentMonth = date("Y-m", strtotime($selectedDate));
                $row_rsodest_check = "SELECT * FROM stocktaking WHERE DATE_FORMAT(date,'%Y-%m') = '$currentMonth'";
                $query_check = mysql_query($row_rsodest_check, $courier);
                while ($row = mysql_fetch_assoc($query_check)){
                    $count ++;
                    ?>
                    <tr>
                        <td><?php echo $count ?></td>
                        <td><?php echo $row['item'] ?></td>
                        <td><?php echo $row['system_qty'] ?></td>
                        <td><?php echo $row['new_qty'] ?></td>
                        <td><?php echo $row['physical_qty'] ?></td>
                        <td> <span> <?php echo $row['physical_qty'] - $row['system_qty'] ?> </span> </td>
                        <td><?php echo $row['branch'] ?></td>
                        <td>
                            <?php echo GetAdjustedQuantity($row['item_id'], $row['tracking_no'] ,$row['date']) ?>
                        </td>

                        <td>
                            <?php echo unitPrice($row['item_id'], strtolower($row['branch'])); ?>
                        </td>

                        <td>
                            <?php $TOTAL += unitPrice($row['item_id'], strtolower($row['branch'])) * $row['physical_qty'];  echo unitPrice($row['item_id'], strtolower($row['branch'])) * $row['physical_qty']  ?>
                        </td>
                    </tr>
                <?php } ?>

                <tr>
                    <td colspan="9" style="font-weight: bolder">TOTAL STOCK VALUE</td>
                    <td style="font-weight: bolder"><?php echo $TOTAL ?></td>
                </tr>
                </tbody>
            </table>


        </div>
    </div>
</div>
