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

####################################### ARCHIVING/UNARCHIVING order #######################################
/*if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'archive'){	
	foreach($_REQUEST["order_ids"] as $customer_id){
		mysqli_query($conn,"UPDATE orders SET is_archive = '1' WHERE id = '$customer_id' ");
	}
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
	$query_string = str_ireplace("action=archive", "", $query_string);
	$query_string = "&".$query_string;
	
	$_SESSION['info_message'] = "order(s) archived sucessfully";
	header("Location: index?page=view_order_order$query_string");
	exit();
}
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'unarchive'){	
	foreach($_REQUEST["order_ids"] as $customer_id){
		mysqli_query($conn,"UPDATE orders SET is_archive = '0' WHERE id = '$customer_id' ");
	}
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
	$query_string = str_ireplace("action=unarchive", "", $query_string);
	$query_string = "&".$query_string;
	
	$_SESSION['info_message'] = "order(s) unarchived sucessfully";
	header("Location: index?page=view_order_order$query_string");
	exit();
}*/
################################################## 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']." ";
}else if($from_date){
	$where.= " AND updated_on >= '$from_date' ";
	$next_page_condition.="&from_date=".$_REQUEST['from_date'];
}else if($to_date){
	$where.= " AND updated_on <= '$to_date' ";
	$next_page_condition.="&to_date=".$_REQUEST['to_date'];
}
/*
$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'];
}*/
###########################################  	END  FILTER CONITIONS 	#####################################
/*$admin_in = "";
$admin_in_ids = "";
if($show_only_my_things  == 1){
	$sql1 = "select job_id FROM job_admin_mapping where admin_id = '".$_SESSION['user_id']."' ";
	$rs1 =  mysqli_query($conn,$sql1);
	if(mysqli_num_rows($rs1) > 0){
		while($row1 = mysqli_fetch_assoc($rs1)){
			$admin_in_ids .= $row1["job_id"].",";
		}
		$admin_in_ids = substr($admin_in_ids,0,-1);
	}
	
	if($admin_in_ids){
		$admin_in = " AND id IN($admin_in_ids)";
	}
	
	$where.=$admin_in;
}*/
###########################################  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		= "orders";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Order Management";
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";
$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);

$total_no_of_order = mysqli_num_rows(mysqli_query($conn,$join_query_for_paging));
#################################   DELETE    #########################################
// when we click on delete this area of php code will execute
if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete'){
	$total_stock = utility::getTableFieldById("inventory_items","stock","id",$_REQUEST['sku_id']);
	$order_qunt = utility::getTableFieldById("orders","quantity","id",$_REQUEST['id']);
    $new_stock = $total_stock + $order_qunt;
	mysqli_query($conn,"UPDATE inventory_items SET stock = $new_stock WHERE id = '".$_REQUEST['sku_id']."' ");
	mysqli_query($conn,"DELETE FROM $table_name  WHERE id = '".$_REQUEST['id']."' ");
	//include('common_delete.php');
	$_SESSION['info_message'] = "Order Deleted sucessfully";
	header("location: $file_name?page=view_order".$next_page_condition.$paging);
	exit();
}
#################################  END DELETE    #########################################
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ To dublicate @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@//
if(isset($_REQUEST['action']) && $_REQUEST['action']=='duplicate'){
	include('duplicate.inc.php');
}
//@@@@@@@ Dublicate End @@@@@@@@@@@@@@@//
###########################################  End dublicate  #####################################
#################################  Export CSV    #########################################
*/
if(isset($_REQUEST['action']) && $_REQUEST['action']=='export_top_50_details'){
  include("export_top_50_used_csv.inc.php");
}
###########################################  End Export  #####################################

$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 = "Stock Used";
include("../admin/paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$query = "SELECT part_number, COUNT(part_number) AS total_quantity FROM $table_name $where GROUP BY part_number ORDER BY total_quantity DESC LIMIT 50";
$sql3=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql3);

$total_no_of_order = mysqli_num_rows(mysqli_query($conn,$result));
  
?>

<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-12">
                <h1 class="m-0">ABC By Warehouse   </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="card">
                        <div class="card-body">
                            <!-- search form -->
                            <?php include_once('op_delivery_filter.php');?>
                            <!-- /.search form -->
                            Total no. of  ABC Reports  <?=$result;?>
							<div style="float:right;margin-bottom:4px; color:black;display:none;">Export Top 50 Part Used &nbsp;&nbsp;<a
                                    href="index?page=view_top_50_parts_used&action=export_top_50_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">
                                   
                                    <? 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>
                                
                                <div class="table-responsive">
                                    <table id="example1" class="table table-bordered table-striped">
                                        <thead>
                                            <tr>

												<th>Part Number </th>
												<th>%each  </th>
												<th>ABC  </th>
												<th>Latest Spot  </th>
                                               
                                            </tr>
                                        </thead>
                                        <tbody>
										 <?
										while($row =mysqli_fetch_array($sql3)){ 
										$row=utility::validate_crypt_data($row);?>
										<tr>
										<td>test</td>
										<td>test</td>
										<td>test</td>
										<td>test</td>
										</tr>
										<?}?>

                                        </tbody>
                                    </table>
                                </div>
                                <div class="box-footer clearfix hide">
                                    <!-- 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>
