<?php
session_start();
$where 					= " WHERE 1=1 AND part_number!=''";
$next_page_condition 	= "";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
} 


################################################## END ARCHIVING/UNARCHIVING ORGANISATION ###################################
###########################################  STRAT FILTER CONITIONS    #####################################
/*$search 	= isset($_REQUEST['search']) ? $_REQUEST['search'] : "";
if($search) {
	$where	.= " AND sku_no LIKE '%$search%' ";
    $next_page_condition.="&search=$search";
}
$order_date 	= isset($_REQUEST['order_date']) ? utility::setFormatedDate($_REQUEST['order_date']) : "";
if($order_date) {
	 $where	.= " AND order_date LIKE '%$order_date%' ";
    $next_page_condition.="&order_date=$order_date";
}
$order_ref 	= isset($_REQUEST['order_ref']) ? $_REQUEST['order_ref'] : "";
if($order_ref) {
	$tmp_order_ref=addslashes($order_ref);
	$where	.= " AND order_ref LIKE '%$tmp_order_ref%'";
    $next_page_condition.="&order_ref=$order_ref";
}

$supplier_id 	= isset($_REQUEST['supplier_id']) ? $_REQUEST['supplier_id'] : "";
if($supplier_id) {
    $where	.= " AND supplier_id = '$supplier_id' ";
    $next_page_condition.="&supplier_id=$supplier_id";
}

$is_archive 	= isset($_REQUEST['is_archive']) ? $_REQUEST['is_archive'] : "";
if($is_archive) {	
    $where	.= " AND is_archive = '$is_archive' ";
    $next_page_condition.="&is_archive=$is_archive";
}else{
	//$where	.= " AND is_archive = '0' ";
}*/
/*$show_archive 	= isset($_REQUEST['show_archive']) ? $_REQUEST['show_archive'] : "";
if(!($show_archive)) {
	$archive_status_id = $config->archive_status_id;
    if(!$job_id){
		$where	.= " AND prospect_status != '$archive_status_id' ";
		$next_page_condition.="&show_archive=$show_archive";
	}
}else{
		$archive_status_id = $config->archive_status_id;
		$where	.= " AND prospect_status = '$archive_status_id' ";
		$next_page_condition.="&show_archive=$show_archive";
}*/
$from_date = isset($_REQUEST['from_date']) ? utility::setFormatedDate($_REQUEST['from_date']) : "";
$to_date = isset($_REQUEST['to_date']) ? utility::setFormatedDate($_REQUEST['to_date']) : "";

if ($from_date && $to_date) {
    $where .= " AND updated_on BETWEEN '$from_date 00:00:00' AND '$to_date 23:59:59'";
    $next_page_condition .= "&from_date=$_REQUEST[from_date]&to_date=$_REQUEST[to_date]"; // Corrected string concatenation
} else if ($from_date) {
    $where .= " AND updated_on = '$from_date'";
    $next_page_condition .= "&from_date=$_REQUEST[from_date]"; // Corrected string concatenation
} else if ($to_date) {
    $where .= " AND updated_on = '$to_date'";
    $next_page_condition .= "&to_date=$_REQUEST[to_date]"; // Corrected string concatenation
} else {
    $currentDate = date("Y-m-d");
    $oneMonthAgo = date("Y-m-d", strtotime("-1 month", strtotime($currentDate)));
    $where .= " AND updated_on >= '$oneMonthAgo'";
    $next_page_condition .= "&to_date=$from_date"; // Corrected string concatenation
}

$last_week = isset($_REQUEST['last_week']) ? $_REQUEST['last_week'] : "";
if ($last_week) {
    $currentDate = date("Y-m-d");
    $oneWeekAgo = date("Y-m-d", strtotime("-1 week", strtotime($currentDate))); // Changed variable name
    $where .= " AND updated_on >= '$oneWeekAgo'";
    $next_page_condition .= "&last_week=$last_week"; // Corrected string concatenation
}

$part_number = isset($_REQUEST['part_number']) ? $_REQUEST['part_number'] : "";
if ($part_number) {
    $part_number1 = utility::getTableFieldById("inventory_items", "sku_no", "id", $part_number);
    $where .= " AND part_number = '$part_number1'";
    $next_page_condition .= "&part_number=$part_number"; // Corrected string concatenation
}

$location = isset($_REQUEST['location']) ? $_REQUEST['location'] : "";
if ($location) {
    $where .= " AND location_id = '$location'";
    $next_page_condition .= "&location=$location"; // Corrected string concatenation
}



$admin_id_search 	= isset($_REQUEST['admin_id_search']) ? $_REQUEST['admin_id_search'] : "";
if($_REQUEST['admin_id_search']){
	$consultant_ids = "";
	$sql1 = "select job_id FROM job_admin_mapping where admin_id = '".$admin_id_search."' ";
	$rs1 =  mysqli_query($conn,$sql1);
	if(mysqli_num_rows($rs1) > 0){
		while($row1 = mysqli_fetch_assoc($rs1)){
			$consultant_ids .= $row1["job_id"].",";
		}
		$consultant_ids = substr($consultant_ids,0,-1);
	}
	$where 	.= " AND id IN($consultant_ids) ";
	$next_page_condition.="&admin_id_search=".$_REQUEST['admin_id_search'];
}

if(isset($_REQUEST['action']) && $_REQUEST['action']=='export_stock_details'){
  include("export_stock_used_to_csv.inc.php");
}
###########################################  PAGING WITH PER PAGE  #####################################
$file_name		= "index"; // this is file name which is used during paging  , included at the bottom of the page
$table_name		= "products_parts_required";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name $where ";
$page_header = "Sales Order Stock Report";
include("../admin/paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$query = "SELECT * FROM $table_name $where ORDER BY id DESC LIMIT $eu, $limit";
$sql3=mysqli_query($conn,$query);

$result3 = mysqli_num_rows(mysqli_query($conn,$join_query_for_paging));
  
?>

<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-12">
                <h1 class="m-0">Sales Order Stock Report</h1>
            </div>
        </div>
        <!-- /.row -->
    </div>
    <!-- /.container-fluid -->
</div>
<section class="content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <!----------------------- Message ------------------------------->
                    <? /*if(isset($_SESSION['info_message'])){ ?>
                    <div class="alert alert-info alert-dismissable">
                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                        <? echo $_SESSION['info_message']; unset($_SESSION['info_message']);?>
                    </div>
                    <? }*/ ?>
                    <!----------------------- End Message ------------------------------->
                    <div class="">
                        <div class="card-body">
                            <!-- search form -->
                            <?php include_once('sales_order_stock_search_options.inc.php');?>
                            <!-- /.search form -->
                            Total No. of Record <?=$result3;?>
							 <div style="float:right;margin-bottom:4px; color:black;display:none;">Export Parts Used Details &nbsp;&nbsp;<a
                                    href="index?page=view_stock_used&action=export_stock_details<?=$next_page_condition;?>"><i
                                        class="fa fa-download text-primary" style="font-size:20px;"></i></a></div>
                            <form action="index?page=add_order" method="post" name="form1" id="form1">
                                <div class="card-footer">
                                    <button type="submit" name="add" class="btn btn-primary hide">Add order</button>
                                    <? if(!$is_archive){ ?>
                                    <button type="button" name="archive_customer" id="archive_customer"
                                        class="hide btn btn-warning pull-right">Archive</button>
                                    <? }else{ ?>
                                    <button type="button" name="unarchive_customer" id="unarchive_customer"
                                        class=" hide btn btn-warning pull-right">Unarchive</button>
                                    <? } ?>
                                </div>
                                 <? if($result3==0){?>
                                <div class="table-responsive">
                                    <table id="example1" class="table table-bordered table-striped">
                                        <thead>
                                            <tr>

                                                <th style="width:180px;">Required Date</th>
												<th style="width:120px;">Part Number</th>
												<th style="width:120px;">Part Decription</th>
                                                <th style="width:170px;">Sales Order Number</th>
												<th style="width:130px;">Current Stock </th>
												<th style="width:160px;">Qty Due </th>
                                                
                                            </tr>
                                        </thead>
                                        <tbody>
										     	<?
													while($row =mysqli_fetch_array($sql3)){ 
													$row=utility::validate_crypt_data($row);
													
												?>
                                            <tr class="" alt="">
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                               
                                                
                                            </tr>
													<?}?>

                                        </tbody>
                                    </table>
                                </div>
								 <? }else{ ?>
                                 <th> No Stock Record Between This Date...</th>
                                <? } ?>
                              <div class="box-footer clearfix ">
                                    <!-- Paging starts here  -->
                                   <span class="text-primary"> <? 
									$next_page_condition .="&page=view_stock_used";
									include("../admin/paging/paging_row.inc.php") 
								?></span>
                                    <!-- Paging ends starts here  -->
                                </div>
                        </div>
                        <div class="card-footer">
                            <button type="submit" name="add" class="btn btn-primary hide">Add Order</button>
                        </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<script type="text/javascript">
$(document).ready(function () {
 /*   $('#example1').DataTable({
    "responsive": true, 
    "autoWidth": false,
    // Add any other DataTable options you need
  }); */
});
</script>