<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Physical_Verifcation extends CI_Controller {

    var $main_menu_name = "reports";
	var $sub_menu_name = "physical_verifcation";

	public function __construct()
	{
		parent::__construct();

		
		$this->load->model('Common_Model');
		$this->load->model('Warehouse_Model');
		$this->load->model('Transfer_Model');
		$this->load->model('Sales_Model');
		$this->load->model('Purchases_Model');
		
		$this->load->model('Product_Damage_Model');
		$this->load->model('Sales_Return_Model');
		$this->load->model('Sequerty_Model');
		$this->load->model('Product_Models');
		$this->load->model('Customer_Model');
		$this->load->model('User_Model');
		$this->load->model('Product_Models');
		$this->load->model('Expenses_Model');
		$this->load->model('Physical_Verifcation_Model');
		$this->load->model('category_models');
		$this->load->model('Custom_Model');
		$this->load->model('Service_Model');
		$this->load->model('Supplier_Model');
		
	
		
	}
	

	
	
	
	public function index()
	{
		$data['suppliers'] = $this->Supplier_Model->get_all_supplier();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;
		$this->load->view('physical_verifcation',$data);
	}

	public function stock_reconcilation_report()
	{
		$data['suppliers'] = $this->Supplier_Model->get_all_supplier();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;
		$data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
		$data['physical_verifcation_header_list'] = $this->Physical_Verifcation_Model->physical_verifcation_header_list();
		$this->load->view('stock_reconcilation_report',$data);
	}
	
	public function stock_reconcilation_with_dep_report()
	{
		$data['suppliers'] = $this->Supplier_Model->get_all_supplier();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;
		$data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
		$data['physical_verifcation_header_list'] = $this->Physical_Verifcation_Model->physical_verifcation_header_list();
		$this->load->view('stock_reconcilation_with_dep_report',$data);
	}
	
	public function manage($pvh_id)
	{
		//echo "pvh_id:".$pvh_id;
		if(!$pvh_id) die();
		$data['suppliers'] = $this->Supplier_Model->get_all_supplier();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;
		$data['pvh_id'] = $pvh_id;
		$this->load->view('physical_verifcation_manage',$data);
	}	
	
	public function save_physical_verifcation_item()
	{
	    error_reporting(0);
		$pvi_serial_no=$this->input->post('serail_no');
		$pvh_id=$this->input->post('pvh_id');
		
		
		
		
		
		if($pvi_serial_no){
			
			$product_id=$this->Physical_Verifcation_Model->get_product_serial_info($pvi_serial_no);
		
		//print_r($item_sn_des);
		
		//get allredy added check
		$added_item_des=$this->Physical_Verifcation_Model->get_physical_verifcation_item_by_pvh_id($pvh_id,$product_id,$pvi_serial_no);
		
		if(!count($added_item_des)){
			
			
			$data=array(
				'pvi_serial_no'=>$pvi_serial_no,
				'pvh_id'=>$pvh_id,
				'pvi_user_id'=>$this->session->userdata('ss_user_id'),
				'pvi_added_datetime'=>date('Y-m-d H:i:s'),
				'product_id'=>$product_id,
		);
		
			
		$_insert=$this->Physical_Verifcation_Model->save_physical_verifcation_item($data);
			$lastid=$this->db->insert_id();
		}
		}
			
			//echo json_encode(array('pvi_serial_no'=>$pvi_serial_no,'error'=>'','disMsg'=>'',));
			
			$data=array();
			
			$item_list=$this->Physical_Verifcation_Model->get_physical_verifcation_item_by_pvh_id($pvh_id);
			
			$totalData=0;
			
			$totalFiltered=0;
			if(!empty($item_list)){
			foreach ($item_list as $item) { 
				//$sale_sn_list.=" $sn->sis_number , ";
				
				
			$nestedData=array(); 
			
			$nestedData[] =  $item->pvi_id;
			$nestedData[] = $item->pvi_serial_no;
			$nestedData[] = $item->product_name;
			$nestedData[] = '';
			$nestedData[] = '';
			
			
			
			$data[] = $nestedData;
			}
			}
			
			$json_data = array(
			"draw"            => intval( '' ),  
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => $data 
			);

	echo json_encode($json_data); 
			
		
	}
	
	
	public function stock_reconcilation_report_get_details()
	{
	$requestData= $_REQUEST;
	$search_key=$this->input->post('search');

	$search_key_val=$search_key['value'];

	$start=$this->input->post('start');

	$length=$this->input->post('length');
	$pvh_id=$this->input->post('pvh_id');
	$srh_warehouse_id=$this->input->post('srh_warehouse_id');
	$with_emi_no=$this->input->post('with_emi_no');
	
	//get phv details
	$pvh_des=$this->Physical_Verifcation_Model->physical_verifcation_header_list($pvh_id);
	
		$srh_warehouse_id=$pvh_des[0]->warehouse_id;
	
//	echo "srh_warehouse_id:$srh_warehouse_id";
	$columns = array( 
		0 =>'service_id', 
		1 => 'service_id',
		2=> 'service_id',
		3 =>'service_id', 
		4 => 'service_id',
		5=> 'service_id'
	);
	
	$data = array();
		$system_qty_tot=0;
	$physical_qty_tot=0;
	$def_qty_tot=0;
	
	$warehouse_id='';//$this->input->get('warehouse_id');
	$service_item_avalable=$this->input->get('service_item_avalable');
	//echo "service_item_avalable:$service_item_avalable";
	//$service = $this->Service_Model->get_all_service($warehouse_id);
	$service = $this->Physical_Verifcation_Model->physical_verifcation_header_list_for_stock_reconcilation_report($warehouse_id,$pvh_id);
	
	$totalData =$this->Physical_Verifcation_Model->physical_verifcation_header_list_for_stock_reconcilation_report($warehouse_id,$pvh_id,'get_count');
	//$totalData = count($service);
	$totalFiltered = $totalData;  
	$item_no=0;
	foreach ($service as $row){
		$item_no++;
		$nestedData=array(); 
		$product_id=$row->product_id;
		$service_id='';//$row['service_id'];
		$total_paid_amount='';
		
		$system_qty=0;
		$system_qty=0;
	
		 $item_des=$this->Physical_Verifcation_Model->get_total_balance_by_product_id_supp_sales_v1($product_id,$srh_warehouse_id);
		  $system_qty=count($item_des);
		$physical_qty=$row->physical_qty;
		$def_qty=0;
		$def_qty=$system_qty-$physical_qty;
		
		$system_sn_list='';
		$miss_serial_no='';
		
		$phy_sn_list_ary=$this->Physical_Verifcation_Model->get_physical_verifcation_item_by_pvh_id($pvh_id,$product_id);
		$phy_sn_list_ary_on_sn=$this->Physical_Verifcation_Model->get_physical_verifcation_item_by_pvh_id_only_ary($pvh_id,$product_id);
		if(!empty($phy_sn_list_ary_on_sn))
		{
		//print_r($phy_sn_list_ary_on_sn[0]);
		/*
		if(!array_search('353109114668926', array_column($phy_sn_list_ary_on_sn, 'pvi_serial_no')) !== false) {
    echo 'value is in multidim array no';
}
else {
    echo 'value is not in multidim array';
}*/

	}
		
		if($with_emi_no){
			if(!empty($item_des)){	
			$s_c=0;
			$d_c=0;
		foreach ($item_des as $p_sn){
			$s_c++;
			$tmp='';
			
			//check missing serial check
			//$miss_serial_no='';
			$tmp="";
			
			/*
			$os = array("357757105076743", "NT", "Irix", "Linux");
if (in_array("357757105076743", $phy_sn_list_ary_on_sn)) {
    echo "<br/>Got Irix";
}
*/



			if(array_search($p_sn->sis_number, array_column($phy_sn_list_ary_on_sn, 'pvi_serial_no')) !== false)  {
   				// echo "Got :".$p_sn->sis_number;
				
				//$tmp='- Avalable';
				
			}else{
				// echo "No :".$p_sn->sis_number;
				//$tmp='- Bo';
				$d_c++;
				$miss_serial_no.=$d_c.". ".$p_sn->sis_number." / <br/>";
			}
			
			
			$system_sn_list.="<br/> $s_c. ".$p_sn->sis_number.$tmp." / ";
		}
			}
		}
		
		
		
		
		
		$phy_sn_list='';
		if($with_emi_no){
			$p_c=0;
			if(!empty($phy_sn_list_ary)){	
			
				foreach ($phy_sn_list_ary as $p_sn){
					$p_c++;
					$phy_sn_list.="<br/> $p_c. ".$p_sn->pvi_serial_no ." / ";
					
					
			
			
				}
			}
		}
		
		
		
		//$phy_sn_list=1111;
		
		$nestedData[] = $item_no;
		$nestedData[] = $row->name;
		$nestedData[] = $row->cat_name;
		
		$nestedData[] = $row->department_name;
		$nestedData[] = $row->ptype_name;;
		$nestedData[] = $row->product_code;
		$nestedData[] = $row->product_name;
		
		$nestedData[] = $system_qty." / ".$system_sn_list;
		$nestedData[] = $physical_qty." / ".$phy_sn_list;
		$nestedData[] = $def_qty;
		$nestedData[] = $miss_serial_no;

	$system_qty_tot=$system_qty_tot+$system_qty;
	$physical_qty_tot=$physical_qty_tot+$physical_qty;
	$def_qty_tot=$def_qty_tot+($system_qty-$physical_qty);
	
	
	$data[] = $nestedData;
}

$nestedData=array();
$nestedData[] = "*";
$nestedData[] = "";
$nestedData[] = "";
$nestedData[] = "";
$nestedData[] = "";
$nestedData[] = "";
$nestedData[] = "";
$nestedData[] = "$system_qty_tot";
$nestedData[] = "$physical_qty_tot";
$nestedData[] = "$def_qty_tot";
$nestedData[] = "";
$data[] = $nestedData;

	$json_data = array(
			//"draw"            => intval( $requestData['draw'] ),  
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => $data 
			);

	echo json_encode($json_data); 
	}
	
	
	
		
	public function stock_reconcilation_report_get_details_v2()
	{
	$requestData= $_REQUEST;
	$search_key=$this->input->post('search');

	$search_key_val=$search_key['value'];

	$start=$this->input->post('start');

	$length=$this->input->post('length');
	$pvh_id=$this->input->post('pvh_id');
	$srh_warehouse_id=$this->input->post('srh_warehouse_id');
	$with_emi_no=$this->input->post('with_emi_no');
		$show_all=$this->input->post('show_all');
			$only_show_def=$this->input->post('only_show_def');
	
	
	
	//get phv details
	$pvh_des=$this->Physical_Verifcation_Model->physical_verifcation_header_list($pvh_id);
	
		$srh_warehouse_id=$pvh_des[0]->warehouse_id;
			$cat_id=$pvh_des[0]->cat_srh;
		$to_date=$pvh_des[0]->pvh_datetime;
//	print_r($pvh_des[0]);
	//echo "cat_id:$cat_id";
	$columns = array( 
		0 =>'service_id', 
		1 => 'service_id',
		2=> 'service_id',
		3 =>'service_id', 
		4 => 'service_id',
		5=> 'service_id'
	);
	
	$data = array();
		$system_qty_tot=0;
	$physical_qty_tot=0;
	$def_qty_tot=0;

	
	$warehouse_id=$srh_warehouse_id;//$this->input->get('warehouse_id');
	$service_item_avalable=$this->input->get('service_item_avalable');
	//echo "service_item_avalable:$service_item_avalable";
	//$service = $this->Service_Model->get_all_service($warehouse_id);
	$service = $this->Physical_Verifcation_Model->physical_verifcation_header_list_for_stock_reconcilation_report_v2($warehouse_id,$pvh_id,"",$cat_id);
	
	$totalData =$this->Physical_Verifcation_Model->physical_verifcation_header_list_for_stock_reconcilation_report_v2($warehouse_id,$pvh_id,'get_count',$cat_id);
	//$totalData = count($service);
	$totalFiltered = $totalData;  
	$item_no=0;
	foreach ($service as $row){
		$item_no++;
		$nestedData=array(); 
		$product_id=$row->product_id;
		$service_id='';//$row['service_id'];
		$total_paid_amount='';
		
		$system_qty=0;
		$system_qty=0;
	
		 $item_des=$this->Physical_Verifcation_Model->get_total_balance_by_product_id_supp_sales_v1($product_id,$srh_warehouse_id,$to_date);
		 
	//	 echo "test : srh_warehouse_id:$srh_warehouse_id";
	//	 die();
		  $system_qty=count($item_des);
		$physical_qty=0;// test $row->physical_qty;
		$def_qty=0;
		$def_qty=$system_qty-$physical_qty;
		
		$system_sn_list='';
		$miss_serial_no='';
		
		$phy_sn_list_ary=$this->Physical_Verifcation_Model->get_physical_verifcation_item_by_pvh_id($pvh_id,$product_id);
		$phy_sn_list_ary_on_sn=$this->Physical_Verifcation_Model->get_physical_verifcation_item_by_pvh_id_only_ary($pvh_id,$product_id);
		if(!empty($phy_sn_list_ary_on_sn))
		{
		//print_r($phy_sn_list_ary_on_sn[0]);
		/*
		if(!array_search('353109114668926', array_column($phy_sn_list_ary_on_sn, 'pvi_serial_no')) !== false) {
    echo 'value is in multidim array no';
}
else {
    echo 'value is not in multidim array';
}*/

	}
	
	$array1=array();
	$array2=array();
		$result=array();
	//$result
		
		if($with_emi_no){
		    
			if(!empty($item_des)){	
			$s_c=0;
			$d_c=0;
		foreach ($item_des as $p_sn){
			$s_c++;
			$tmp='';
			
			//check missing serial check
			//$miss_serial_no='';
			$tmp="";
			
			/*
			$os = array("357757105076743", "NT", "Irix", "Linux");
if (in_array("357757105076743", $phy_sn_list_ary_on_sn)) {
    echo "<br/>Got Irix";
}
*/



		$array1[]=$p_sn->sis_number;// array("a" => "green", "red", "blue", "red");
			
			$system_sn_list.="<br/> $s_c. ".$p_sn->sis_number.$tmp." / ";
		}
			}
		
		}
		
		
		
		
		$array2=array();
	$with_emi_no=1;
		$phy_sn_list='';
		if($with_emi_no){
			$p_c=0;
			
			//	$array2[]="tese";
			if(!empty($phy_sn_list_ary)){	
			
				foreach ($phy_sn_list_ary as $p_sn){
					$p_c++;
					$phy_sn_list.="<br/> $p_c. ".$p_sn->pvi_serial_no ." / ";
					
					
			        	$array2[]=$p_sn->pvi_serial_no;
			
				}
			}
			
			
			
		}
		
		$physical_qty=0;
		if(isset($phy_sn_list_ary))
		$physical_qty=count($phy_sn_list_ary);
		
		$def_qty=$system_qty-$physical_qty;
		
		


	$miss_serial_no='';
		$mis_count=0;
		$result=array();
		$result = array_diff($array1, $array2); //array_diff
		
		
		
		
		if(isset($result) && count($result)!=0){
	//	print_r($result);
	
	
	foreach ($result as $food)  {
           // echo $food ."<br />";
            $mis_count++;
		    $miss_serial_no.="<br/> $mis_count. ".$food;
        }
	

		
		   
		}
		
	
	//	if(count($result)){
	//	for($k=0; $k<count($miss_serial_no); $k++){
		    
	//	    $miss_serial_no.="<br/> ".$result[$k];
	//	}
		//}
		
		
		//
		//$phy_sn_list=1111;
		
		
		$is_display=0;
		
		if($def_qty!=0) $is_display=1;
		
		if($show_all==1) $is_display=1;
		
		//only_show_def
		if($only_show_def){
		  //  if($def_qty!=0) $is_display=1;
		   // else $is_display=0;
		}
		
		
		if($is_display){
		$nestedData[] = $item_no;
		$nestedData[] = "";//$row->name;
		$nestedData[] = $row->cat_name;
		
		$nestedData[] = $row->department_name;
		$nestedData[] = $row->ptype_name;;
		$nestedData[] = $row->product_code;
		$nestedData[] = $row->product_name;
		
		$nestedData[] = $system_qty." / ".$system_sn_list;
		$nestedData[] = $physical_qty." / ".$phy_sn_list;
		$nestedData[] = $def_qty;
		$nestedData[] = $miss_serial_no;

	$system_qty_tot=$system_qty_tot+$system_qty;
	$physical_qty_tot=$physical_qty_tot+$physical_qty;
	$def_qty_tot=$def_qty_tot+($system_qty-$physical_qty);
	
	if($system_qty)
	$data[] = $nestedData;
	
		}
}

$nestedData=array();
$nestedData[] = "z";
$nestedData[] = "";
$nestedData[] = "";
$nestedData[] = "";
$nestedData[] = "";
$nestedData[] = "";
$nestedData[] = "";
$nestedData[] = "$system_qty_tot";
$nestedData[] = "$physical_qty_tot";
$nestedData[] = "$def_qty_tot";
$nestedData[] = "";
$data[] = $nestedData;

	$json_data = array(
			//"draw"            => intval( $requestData['draw'] ),  
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => $data 
			);

	echo json_encode($json_data); 
	}
	
	
	public function list_physical_verifcation()
	{
	$requestData= $_REQUEST;
	$search_key=$this->input->get('search');
	$warehouse_id=$this->input->get('warehouse_id');
	$search_key_val=$search_key['value'];
	$start=$this->input->get('start');
	$length=$this->input->get('length');
	
	$columns = array( 
		0 =>'trnsfr_id', 
		1 => 'trnsfr_id',
		2=> 'trnsfr_id',
		3 =>'trnsfr_id', 
		4 => 'trnsfr_id',
		5=> 'trnsfr_id'
	);
	
	$data = array();
	//$transfer = $this->Transfer_Model->get_all_transfer();
	$transfer_tot = $this->Physical_Verifcation_Model->list_physical_verifcation("","","","","",$warehouse_id);
	$transfer = $this->Physical_Verifcation_Model->list_physical_verifcation($start,$length,$search_key_val,"","",$warehouse_id);
	$totalData = count($transfer_tot);
	$totalFiltered = $totalData;  
	
	foreach ($transfer as $row){
		$nestedData=array(); 
		$trnsfr_id=$row['pvh_id'];
		$total_paid_amount='';
		$nestedData[] =$row['pvh_id'];
			$nestedData[] = $row['pvh_refno'];
			$nestedData[] = $row['name']. " - Department :  ".$row['cat_name'];
		$nestedData[] =site_date($row['pvh_datetime']);
	
		$fromwhe=$this->Transfer_Model->getwarehousename($row['name']);
		
		//$towhe=$this->Transfer_Model->getwarehousename($row['trnsfr_to_warehouse_id']);
		//$nestedData[] = $towhe['name'];		
		$nestedData[] =$row['pvh_note'];		
		$url=base_url("transfer/trnsfr_details?trnsfr_id=$trnsfr_id");
		
		
		
	
	$nestedData[] = '<div class="btn-group text-left">
                            <button data-toggle="dropdown" class="btn btn-default btn-xs btn-primary dropdown-toggle" type="button">Actions <span class="caret"></span></button>
                            <ul role="menu" class="dropdown-menu pull-right">
                            
                           
							<li><a href="'.base_url().'physical_verifcation/manage/'.$trnsfr_id.'" class="" data-placement="top" data-original-title="Edit suppliers"><i class="fa fa-file-text-o"></i> Add Details</a><li>
							
                            </ul></div>';
							
							/*<li><a href="'.base_url().'transfer/view/'.$trnsfr_id.'"><i class="fa fa-file-text-o"></i> Transfer Details</a></li>*/
	
	$data[] = $nestedData;
}

	$json_data = array(
			//"draw"            => intval( $requestData['draw'] ),  
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => $data 
			);

	echo json_encode($json_data); 
	}
	
		public function create_physical_verifcation()
	{
        
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = 'transactions_list';
		$data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
			$data['category_list'] 	= $this->category_models->getCategory();
		$ss=$this->input->get('ss');
			$data['ss'] = $ss;
		
		$acctrnss_id='';
		//print_r($this->input->get());
		//echo "test:".$this->input->get('acctrnss_id');
		
        if ($this->input->get('acctrnss_id')) {
			$acctrnss_id=$this->input->get('acctrnss_id');
		}
		else {
			$acctrnss_id='';
		}
		
		//echo "t:$acctrnss_id";
		if($acctrnss_id){
		
			
		}
		else {
			$data['acctrnss_id']='';
			$data['type']='A';
			if($ss){
			    	$data['pageName']='Shift Start';
			}else{
			$data['pageName']='Add Physical Verifcation';
			}
			if($ss){
			    $data['btnText']='Shift Start';
			}else{
			$data['btnText']='Add';
			}
			$data['btnText']='Add';
			$data['pageName']='Add Physical Verifcation';
			$data['transactions']=array();
		}
		
        $this->load->view('models/create_physical_verifcation',$data);	
	}
	
	
	public function save_physical_verifcation()
	{
	
		$type=$this->input->post('type');
		$warehouse_id=$this->input->post('warehouse_id');
		$pvh_datetime=date('Y-m-d',strtotime($this->input->post('pvh_datetime')));
		$pvh_user_id=$this->session->userdata('ss_user_id');
		$pvh_note=$this->input->post('pvh_note');
		
			$cat_srh=$this->input->post('cat_srh');
		
		
		
		$this->load->library('form_validation'); //form validation lib
		if($type=='A')
		{
			$this->form_validation->set_rules('warehouse_id','required');
		}
		
		
		
		if ($this->form_validation->run() == FALSE)
        {
           $st = array('status' =>0,'validation' => validation_errors());
           echo json_encode($st);
        }
        else
        {
			
			$pvh_refno='';
			
			$warehouse_dtls=$this->Warehouse_Model->get_warehouse_info($warehouse_id);
				//print_r($warehouse_dtls);
				$warehouse_code=$warehouse_dtls['code'];
				
			
			$pvh_last_warehouse_id=$this->Common_Model->get_next_warehouse_ref_no($warehouse_id,'warehouse_id','pvh_last_warehouse_id','physical_verifcation_header')+1;
	
	$pvh_refno=$warehouse_code.'/PV/'.sprintf("%04d",$pvh_last_warehouse_id);
	
		
			$data=array(
			'warehouse_id'=>$warehouse_id,
			'pvh_datetime'=>$pvh_datetime,	
			'pvh_note'=>$pvh_note,
			'pvh_user_id'=>$pvh_user_id,
			'pvh_refno'=>$pvh_refno,
			'cat_srh'=>$cat_srh,
			'pvh_last_warehouse_id'=>$pvh_last_warehouse_id
			);
			$_insert=$this->Physical_Verifcation_Model->save_physical_verifcation($data);

		
		
		
		
	//	echo $this->db->last_query();
		$lastid=$this->db->insert_id();
		
		
		

		if($type=='A'){
			if ($lastid) {
				echo json_encode(array('id'=>$lastid,'type'=>$type,'status' =>1));
			} else {
				echo json_encode(array('status'=>'0'));
			}
		}
		if($type=='E'){
			echo json_encode(array('type'=>$type,'status' =>1));
		}	
			
		}
	}

	
}