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

class Product_Damage extends CI_Controller {

    var $main_menu_name = "product_damage";
	var $sub_menu_name = "product_damage";

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

		
		$this->load->model('Product_Damage_Model');
		$this->load->model('Supplier_Model');
		$this->load->model('Warehouse_Model');
		$this->load->model('Common_Model');
		$this->load->model('Tax_Rates_Model');
		$this->load->model('Customer_Model');
		$this->load->model('Sequerty_Model');
		$this->load->model('Sales_Model');
		$this->load->model('Purchases_Model');
		$this->load->model('User_Model');
		
	}
	
	//Product_ Damage list page load
	public function index()
	{
		//$data['product_damage'] = $this->Product_Damage_Model->get_all_product_damage();
		$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['rep_list'] = $this->User_Model->getSalesRep();
        $this->load->view('product_damage',$data);
	}	
	
	
	public function test_update()
	{
	   die();
	    
	    $pdmg_id=317000;
	     echo "test_update pdmg_id:$pdmg_id";
	    if(!$pdmg_id) die();
		$pdmg_details= $this->Product_Damage_Model->get_product_damage_info($pdmg_id);

		//get sale item list
		$pdmg_item_list= $this->Product_Damage_Model->get_product_damage_item_list_by_product_damage_id($pdmg_id);
		
		$damage_tot_cost=0;
		 foreach ($pdmg_item_list as $row)
 {
     $damage_itm_sub_cost=0;
     $pdmgitm_id=$row['pdmgitm_id'];
     $pdmgitm_quantity=$row['pdmgitm_quantity'];
      echo "<br/>".$pdmgitm_id.":".$row['product_name'];
      $sn_list=$this->Product_Damage_Model->get_pro_damage_serial_by_id($pdmg_details['pdmg_id'],$row['pdmgitm_id']);
      
    
if(!empty($sn_list)){
echo "<br/> Serial No. ";
foreach ($sn_list as $sn) { 
echo "($sn->pdmgitmsn_sn) , ";
  $sn_data = $this->Purchases_Model->check_avalable_sn_for_sale_v2("",$sn->pdmgitmsn_sn);
  $pis_cost_price=$sn_data[0]['pis_cost_price'];
  echo "<br/> pis_cost_price:$pis_cost_price";
  $damage_itm_sub_cost=$damage_itm_sub_cost+$pis_cost_price;
  $damage_tot_cost=$damage_tot_cost+$pis_cost_price;
  
  
  
  
 
 //print_r($sn_data);

}
}
   
   echo "<br/>damage_itm_sub_cost:$damage_itm_sub_cost";  
   //Update item sub total
   $data=array();
	$data=array(
				'gross_total'=>$damage_itm_sub_cost,
				'pdmgitm_unit_cost'=>$damage_itm_sub_cost/$pdmgitm_quantity,
			);
	if($pdmgitm_id){
	    echo "Updated!";
    //$this->Product_Damage_Model->update_product_damage_item($data,$pdmgitm_id);
    //echo $this->db->last_query();
	}
   
 }
 
 
 
	echo " <br/> damage_tot_cost:$damage_tot_cost";	
	//Update grand total
	$data=array();
	$data=array(
				'pdmg_total'=>$damage_tot_cost,
			);
	if($pdmg_id){
    //$this->Product_Damage_Model->save_product_damage($data,$pdmg_id);
	}
	
	
	}
	    
	    
	
	//Product  Damage details view
	public function view()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = '';
		
		//get sale id
		$pdmg_id=$this->uri->segment('3');
		$data['pdmg_item_list']= $this->Product_Damage_Model->get_pdmg_item_list_by_pdmg_id($pdmg_id);
		$data['pdmg_details']= $this->Product_Damage_Model->get_pdmg_info($pdmg_id);
		
	
		$data['customer_details']= $this->Customer_Model->get_customer_info($data['pdmg_details']['customer_id']);
		$data['warehouse_details']= $this->Warehouse_Model->get_warehouse_info($data['pdmg_details']['warehouse_id']);		
		
		$data['pdmg_id']=$pdmg_id;
        $this->load->view('product_damage_view',$data);
	}

	//Product  Damage add page
	public function add_pdmg_payments()
	{
		$pdmg_pymnt_amount=$this->input->post('pdmg_pymnt_amount');
		$pdmg_id=$this->input->post('pdmg_id');
		$pdmg_pymnt_ref_no=$this->input->post('pdmg_pymnt_ref_no');
		$pdmg_pymnt_paying_by=$this->input->post('pdmg_pymnt_paying_by');
		$pdmg_pymnt_date_time=$this->input->post('pdmg_pymnt_date_time');
		$pdmg_pymnt_date_time_send=date('Y-m-d H:i:s', strtotime($pdmg_pymnt_date_time));
		$pdmg_pymnt_cheque_no=$this->input->post('pdmg_pymnt_cheque_no');
		$pdmg_pymnt_crdt_card_no=$this->input->post('pdmg_pymnt_crdt_card_no');
		$pdmg_pymnt_crdt_card_holder_name=$this->input->post('pdmg_pymnt_crdt_card_holder_name');
		$pdmg_pymnt_crdt_card_month=$this->input->post('pdmg_pymnt_crdt_card_month');
		$pdmg_pymnt_crdt_card_year=$this->input->post('pdmg_pymnt_crdt_card_year');
		$pdmg_pymnt_crdt_card_type=$this->input->post('pdmg_pymnt_crdt_card_type');
		$pdmg_type = $this->input->post('pdmg_type');

		$pdmg_pymnt_note=$this->input->post('pdmg_pymnt_note');
		$user_id=$this->session->userdata('ss_user_id');
		$pdmg_pymnt_added_date_time=date("Y-m-d H:i:s");
		$pdmg_pymnt_id='';
		
        $this->load->library('form_validation'); //form validation lib
        $this->form_validation->set_rules('pdmg_pymnt_amount', 'Amount', 'required');
		if($pdmg_pymnt_paying_by=='Credit Card'){
			$this->form_validation->set_rules('pdmg_pymnt_crdt_card_type', 'Card Type', 'required');
			$this->form_validation->set_rules('pdmg_pymnt_crdt_card_no', 'Credit Card No', 'required');
			$this->form_validation->set_rules('pdmg_pymnt_crdt_card_holder_name', 'Holder Name', 'required');
			$this->form_validation->set_rules('pdmg_pymnt_crdt_card_month', 'Month', 'required');
			$this->form_validation->set_rules('pdmg_pymnt_crdt_card_year', 'Year', 'required');
		}
		if($pdmg_pymnt_paying_by=='Cheque'){
			$this->form_validation->set_rules('pdmg_pymnt_cheque_no', 'Cheque No', 'required');
		}
		$this->form_validation->set_rules('pdmg_id', 'System Error', 'required');


        if ($this->form_validation->run() == FALSE)
        {
           $st = array('status' =>0,'validation' => validation_errors());
           echo json_encode($st);
        }
        else
        {
			$data=array(
				'pdmg_pymnt_amount'=>$pdmg_pymnt_amount,	
				'pdmg_pymnt_ref_no'=>$pdmg_pymnt_ref_no,
				'pdmg_pymnt_paying_by'=>$pdmg_pymnt_paying_by,
				'pdmg_pymnt_date_time'=>$pdmg_pymnt_date_time_send,
				'pdmg_pymnt_note'=>$pdmg_pymnt_note,
				'user_id'=>$user_id,
				'pdmg_id'=>$pdmg_id,
				'pdmg_pymnt_added_date_time'=>$pdmg_pymnt_added_date_time,
				'pdmg_pymnt_cheque_no'=>$pdmg_pymnt_cheque_no,
				'pdmg_pymnt_crdt_card_no'=>$pdmg_pymnt_crdt_card_no,
				'pdmg_pymnt_crdt_card_holder_name'=>$pdmg_pymnt_crdt_card_holder_name,
				'pdmg_pymnt_crdt_card_type'=>$pdmg_pymnt_crdt_card_type,
				'pdmg_pymnt_crdt_card_month'=>$pdmg_pymnt_crdt_card_month,
				'pdmg_pymnt_crdt_card_year'=>$pdmg_pymnt_crdt_card_year,
				'pdmg_payment_type' => $pdmg_type
			);
			
               if ($this->Product_Damage_Model->save_pdmg_payments($data,$pdmg_pymnt_id)) {
                    $st = array('status' =>1,'validation' =>'Done!');
                    echo json_encode($st);
               } else {
                    $st = array('status' =>0,'validation' =>'error occurred please contact your system administrator');
                    echo json_encode($st);
               }
		}
	}	
	
	//Product  Damage payment page 
	public function payments()
	{
        $data['pdmg_id'] = $this->input->get('id');
        $data['pdmg_type'] = $this->input->get('pdmg_type');
        $this->load->view('models/product_damage_payment',$data);	
	}
	
	//Product  Damage save 
	//Product  Damage item save
	//Add product_damage items to 54 table
	public function save_product_damage()
	{
	    $tot_cost_v2=0;
        $item_sub_tot_cost_v2=0;
		$pdmg_reference_no=$this->input->post('pdmg_reference_no');
		$warehouse_id=$this->input->post('warehouse_id');
		$rowCount=$this->input->post('rowCount');
		
		$pdmg_datetime_1=$this->input->post('pdmg_datetime');
		$pdmg_datetime=date('Y-m-d H:i:s', strtotime($pdmg_datetime_1));
		$pdmg_inv_discount=$this->input->post('pdmg_inv_discount');		
		$pdmg_total=$this->input->post('trnsfr_total');
		$tech_id=$this->input->post('tech_id');
		$pdmg_note=$this->input->post('pdmg_note');
		
		$pdmg_datetime_created=date('Y-m-d H:i:s');
		
		$warehouse_dtls=$this->Warehouse_Model->get_warehouse_info($warehouse_id);
		$warehouse_code=$warehouse_dtls['code'];
		$warehouse_last_id=(1+$this->Product_Damage_Model->get_next_warehouse_ref_no_pd($warehouse_id));
		
	//	echo "warehouse_last_id:$warehouse_last_id";
		//die();
		
		$pdmg_reference_no =$warehouse_code.'/D/'.sprintf("%04d",$warehouse_last_id);
		
		
		$error='';
		$disMsg='';
		$lastid='';
		$pdmg_id='';
		
		if(!$error){
			$data=array(
				'pdmg_reference_no'=>$pdmg_reference_no,
				'warehouse_id'=>$warehouse_id,
				'pdmg_datetime'=>$pdmg_datetime,
				'pdmg_total'=>$pdmg_total,
				'pdmg_datetime'=>$pdmg_datetime,
				'pdmg_datetime_created'=>$pdmg_datetime_created,
				'pdmg_note'=>$pdmg_note,
				'tech_id'=>$tech_id,
				'warehouse_last_id'=>$warehouse_last_id,
			);
			$_insert=$this->Product_Damage_Model->save_product_damage($data,$pdmg_id);
			$lastid=$this->db->insert_id();
			$pdmg_id=$lastid;
			$disMsg='Sale successfully added';
			
		    //	$item_sub_tot_cost_v2=0;
			
			//insert sale item data
			$row=$this->input->post('row');
			$rowCount=$this->input->post('rowCount');
			$data_item=array();
			for($i=1; $i<=$rowCount; $i++){
				if(isset($row[$i]['product_id'][0]))
				{
					
				$data_item=array(
					'pdmg_id'=>$pdmg_id,
					'product_id'=>$row[$i]['product_id'][0],
					'pdmgitm_quantity'=>$row[$i]['qty'][0],
					'pdmgitm_unit_cost'=>$row[$i]['unit_price'][0],
				
				);
				$this->Product_Damage_Model->save_product_damage_item($data_item);
				$pdmgitm_id=$this->db->insert_id();
				
				/* start serial */
				//check serial no exists
				if($row[$i]['product_sn'][0])
				{
				    $item_sub_tot_cost_v2 = 0;
				//add serial numbers
				//exploid serial numbers
					$pieces_sn = explode(",", $row[$i]['product_sn'][0]);
					for($h=0; $h<count($pieces_sn); $h++){
						$sn_data=array(
							'product_id'=>$row[$i]['product_id'][0],	
							'product_w'=>$row[$i]['product_w'][0],	
							'pdmg_id'=>$pdmg_id,
							'pdmgitm_id'=>$pdmgitm_id,
							'pdmgitmsn_sn'=>$pieces_sn[$h],
							
						);
				  		$this->Product_Damage_Model->save_product_damage_item_serial_numbers($sn_data);
						
						
			//	update grn serial number sold status
				$pis_number=$pieces_sn[$h];
				$sn_data=array(	
				//	'pdmg_id'=>$pdmg_id,
				//	'pdmgitm_id'=>$pdmgitm_id,
				//	'pis_transferred'=>1,
					'product_avalable'=>0, // trnser aprove update
							'avalable_warehouse_id'=>0,
						//	'sale_inv_id'=>0,
						//	'pis_sold'=>0,
						//	'pis_sales_return'=>0,
				);
				$this->Purchases_Model->update_grn_serial_numbers($sn_data,$pis_number);
				
				
				 /* Start Get serial cost and add to total */
                    //$tot_cost_v2=0;
                    //$item_sub_tot_cost_v2=0;
                    
                    $pis_cost_price=$this->Purchases_Model->get_serial_no_cost_by_serial_no($pis_number);
                    
                    $item_sub_tot_cost_v2=$item_sub_tot_cost_v2+$pis_cost_price;
                    $tot_cost_v2=$tot_cost_v2+$pis_cost_price;
                    
                    /* End Get serial cost and add to total */
				
				
				
						
					}
                    //end add serial numbers
                    
                    
                   
                    
				}
			
				
				/* end serial */
				
				//add reford for f4 table
				$type='prodcut_damage';
				$ref_id=$pdmg_id;
				$product=$row[$i]['product_id'][0];
				$pdmg_itm_quantity=$row[$i]['qty'][0];
				$unit_cost=$row[$i]['unit_price'][0];
				$this->Common_Model->add_fi_table($type,$ref_id,$product,$pdmg_itm_quantity,$unit_cost);
				
				//Start Update Item Cost , Sub Total Cost
				$sub_cost=$item_sub_tot_cost_v2/$pdmg_itm_quantity;
				$data_item=array(
					'pdmgitm_unit_cost'=>$sub_cost,
					'gross_total'=>$item_sub_tot_cost_v2,
				
				);
				$this->Product_Damage_Model->update_product_damage_item($data_item,$pdmgitm_id);
				//End Update Item Cost , Sub Total Cost
				
				
				}
				
				
			}
			
			//Start Update Total Cost
					$data=array(
				'pdmg_total'=>$tot_cost_v2,
			);
			$_insert=$this->Product_Damage_Model->save_product_damage($data,$pdmg_id);
			//End Update Total Cost
		
		}else {
			
			$disMsg='Please select these before adding any product:'.$disMsg;
		}	
		
		$this->session->set_flashdata('message', 'Product damage details successfully added!');
		
		echo json_encode(array('pdmg_id'=>$lastid,'error'=>$error,'disMsg'=>$disMsg,));
	}

	//Product  Damage reference no jenarate	
	public function get_next_ref_no(){
		$query=$this->Product_Damage_Model->get_next_ref_no();
		$result = $query->row();
		//print_r($result);
		$pdmg_reference_no=sprintf("%05d", $result->pdmg_id+1);
		$pdmg_reference_no=$pdmg_reference_no;
		echo json_encode(array('pdmg_reference_no'=>$pdmg_reference_no));
	}
	
	//Product  Damage ger avalable product qty
	public function get_avalable_product_qty(){
		$product_id=$this->input->get('product_id');
		$warehouse_id=$this->input->get('warehouse_id');
		
		$data['total']=$this->Product_Damage_Model->get_avalable_product_qty($product_id,$warehouse_id);
		echo json_encode(array('remmnaingQty'=>$data['total']));
	}

	//Product  Damage add form
    public function product_damage_add()
    {
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = 'product_damage_add';
		
		//get suppliers list
		$data['suppliers'] = $this->Supplier_Model->get_all_supplier();
		$data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
		$data['tax_rates_list'] = $this->Tax_Rates_Model->get_all_tax_rates();
		$data['customer_list'] = "";//$this->Customer_Model->get_all_customers();
		$data['status_list'] = $this->Common_Model->get_all_status();
			$data['rep_list'] = $this->User_Model->getSalesRep();
		
        $this->load->view('product_damage_add',$data);
    }
    
    
    	//Sales product items get
	 public function suggestions($value='')
    {
		
		//print_r($_GET);
		$term=$this->input->get('term');
		$in_type=$this->input->get('t');
		$cus_type=$this->input->get('cus_type');
		$city_id=$this->input->get('city_id');
		$cat_id=$this->input->get('cat_id');
		$srh_warehouse_id=$this->input->get('srh_warehouse_id');
		$cat_id=$this->input->get('cat_id');
		$manual_search= '';//$this->input->get('manual_search');
		
//	echo $status;	
		//if()
		if($cus_type == 'Special' && $in_type=='Wholesale'){
//		if enabled
		
		$result = $this->Country_Model->get_area_info_by_id($city_id);
		$status = $result[0]['status'];

		if($status == 1){
		//$data['sales'] = $this->Cus_Pricing_Model->get_products_suggestions_sale($term,$city_id);
		//$data['sales'] = $this->Sales_Model->get_products_suggestions($term);
		}
		
		else {
		//$data['sales'] = $this->Sales_Model->get_products_suggestions($term);
		}//end if
		
		}
		else if($cus_type == 'Normal'){
		//$data['sales'] = $this->Sales_Model->get_products_suggestions($term);
 	}
	
	
	$data['sales'] = $this->Sales_Model->get_products_suggestions($term,$cat_id,$srh_warehouse_id);
		
		$json = array();
		$json_itm=array();
		foreach ($data['sales'] as $row)
		{
			//set price
			$price_tmp=0;
			if($in_type=='Wholesale'){
			$price_tmp=floatval($row['wholesale_price']);
			}else {
			$price_tmp=$row['product_price'];
			}
			
			
			$price_tmp_sn='';
			$price_tmp_sn=$row['pis_cash_price'];
			if($price_tmp_sn){
				$price_tmp=$price_tmp_sn;
				
			}else{
				$price_tmp=$price_tmp;
			}
			
		//	echo "price_tmp:".$price_tmp;
			
		//	$price_tmp=$row['product_price'];
			//quantity for label
				$quantity =0;
				$transferd_qty=0;
				$transfer_reseve_qty=0;
				$purchased_qty=0;
				$sales_return_qty=0;
				$product_damaged_qty=0;
				$sold_qty=0;
				$transferd_qty=$this->Transfer_Model->getTransferdQtyByWarehouseId($this->session->userdata('ss_warehouse_id'),$row['product_id']);
				$transfer_reseve_qty=$this->Transfer_Model->getTransferResevedQtyByWarehouseId($this->session->userdata('ss_warehouse_id'),$row['product_id']);
				$sold_qty=$this->Sales_Model->getSoldQtyByWarehouseId($this->session->userdata('ss_warehouse_id'),$row['product_id']);
				$purchased_qty=$this->Purchases_Model->getPurchasedQtyByWarehouseId($this->session->userdata('ss_warehouse_id'),$row['product_id']);
				$product_damaged_qty=$this->Product_Damage_Model->getProductDamagedQtyByWarehouseId($this->session->userdata('ss_warehouse_id'),$row['product_id']);
				
				$sales_return_qty=$this->Sales_Return_Model->getSalesReturnQtyByWarehouseId($this->session->userdata('ss_warehouse_id'),$row['product_id']);
				$quantity = number_format(($purchased_qty+$transfer_reseve_qty+$sales_return_qty-$sold_qty-$transferd_qty-$product_damaged_qty), 2, '.', ',');
			//end qty for label
			$product_name=$row['product_name'];
			$product_code=$row['product_code'];
			$product_sn=$row['pis_number'];
			$product_part_no=$row['product_part_no'];
			$product_oem_part_number=$row['product_oem_part_number'];
			$product_id=$row['product_id'];
			$product_w=$row['product_w'];
			$product_sn_exissts=$row['product_sn_exissts'];
			$product_price=$price_tmp;
			$sendParameters="'$product_id','$product_name','$product_code','$product_price'";
			$sendParameters="$product_id,$product_name,$product_code,$product_price";
			$extraName='';
			//$extraName.=", Serial No: $product_sn";
			$extraName.=",  Price: ".number_format($product_price, 2, '.', ',').", Qty. :".$quantity;			
			
			$is_search_by_sn=0;
			if($row['pis_number']==$term){
				$is_search_by_sn=1;
			}
			
			//product have serial no get grn item serial table cost, val empty set def val
			//if($product_sn_exissts){
				//get cost by sn
				//	get_serial_no_supp_id($sn='');
				
			//}
			
			$json_itm=array(
			 		'id'=> $row['product_id'],
					'product_id'=> $row['product_id'],
					'product_code'=> $row['product_code'],
					'product_name'=> $row['product_name'],
					'product_price'=> $product_price,
					'product_part_no'=> $row['product_part_no'],
					'item_cost'=> $row['product_cost'],
					'product_sn'=> $row['pis_number'],
					'pis_cost_price'=> $row['pis_cost_price'],
					'product_w'=> $product_w,
					'product_sn_exissts'=> $product_sn_exissts,
					'is_search_by_sn'=> $is_search_by_sn,
					'product_oem_part_number'=> $row['product_oem_part_number'],
                    'value'=> $row['product_name']." (".$row['product_code'].")",
                    'label'=> $row['product_name']." (".$row['product_code'].")$extraName | SN: $row[pis_number]"
                    );
					
					//echo "Test:";
					array_push($json,$json_itm);	
					
		}	
		
		
		
		
		$data['sales_non_seril'] = $this->Sales_Model->get_non_serial_products_suggestions($term,$cat_id);
		foreach ($data['sales_non_seril'] as $row)
		{
			//set price
		//	$price_tmp=0;
			
		//	$price_tmp=$row['product_price'];
		$price_tmp=0;
			if($in_type=='Wholesale'){
			$price_tmp=floatval($row['wholesale_price']);
			}else {
			$price_tmp=$row['product_price'];
			}
			//quantity for label
				$quantity =0;
				$transferd_qty=0;
				$transfer_reseve_qty=0;
				$purchased_qty=0;
				$sales_return_qty=0;
				$product_damaged_qty=0;
				$sold_qty=0;	
				
				$transferd_qty=$this->Transfer_Model->getTransferdQtyByWarehouseId($srh_warehouse_id,$row['product_id']);
				$transfer_reseve_qty=$this->Transfer_Model->getTransferResevedQtyByWarehouseId($srh_warehouse_id,$row['product_id']);
				$sold_qty=$this->Sales_Model->getSoldQtyByWarehouseId($srh_warehouse_id,$row['product_id']);
				$purchased_qty=$this->Purchases_Model->getPurchasedQtyByWarehouseId($srh_warehouse_id,$row['product_id']);
				$product_damaged_qty=$this->Product_Damage_Model->getProductDamagedQtyByWarehouseId($srh_warehouse_id,$row['product_id']);
				
				$sales_return_qty=$this->Sales_Return_Model->getSalesReturnQtyByWarehouseId($srh_warehouse_id,$row['product_id']);
				$quantity = number_format(($purchased_qty+$transfer_reseve_qty+$sales_return_qty-$sold_qty-$transferd_qty-$product_damaged_qty), 0, '.', ',');
			//end qty for label
			$product_name=$row['product_name'];
			$product_code=$row['product_code'];
			$product_sn='';
			$product_part_no=$row['product_part_no'];
			$product_oem_part_number=$row['product_oem_part_number'];
			$product_id=$row['product_id'];
			$product_w='';//need check
			$product_price=$price_tmp;
			$sendParameters="'$product_id','$product_name','$product_code','$product_price'";
			$sendParameters="$product_id,$product_name,$product_code,$product_price";
			$extraName='';
			if($product_sn){
				//$extraName.=", Serial No: $product_sn";
			}
			$extraName.=", Selling Price: ".number_format($product_price, 2, '.', ',')." ,Qty. :".$quantity;			
			
			//$json_itm=array();
			//if($srh_warehouse_id!=2){//not smart phone maharagama
			
			$add_details=0;
			
		//	if($quantity){
			///	$add_details=1;
		//	}
		
		
		if($srh_warehouse_id==9 || $srh_warehouse_id==10 || $srh_warehouse_id==11 || $srh_warehouse_id==13){
		    if($quantity){
		        $add_details=1;
		    }
		    
		}else /*{
		    $add_details=1;
		}*/
		if($srh_warehouse_id == 15){
			if($term == "111111"){
				 $add_details=1;
			}else if($term == "222222"){
				$add_details=1;
			}
		}
		
			
			if($add_details)
			{
			$json_itm=array(
			 		'id'=> $row['product_id'],
					'product_id'=> $row['product_id'],
					'product_code'=> $row['product_code'],
					'product_name'=> $row['product_name'],
					'product_price'=> $price_tmp,
					'product_part_no'=> $row['product_part_no'],
					'item_cost'=> $row['product_cost'],
					'product_sn'=> $product_sn,
					'product_w'=> $product_w,
					'product_oem_part_number'=> $row['product_oem_part_number'],
                    'value'=> $row['product_name']." (".$row['product_code'].")",
                    'label'=> $row['product_name']." (".$row['product_code'].")$extraName"
                    );
					array_push($json,$json_itm);
			}
			//}
					
		}
		
		
		
		
		echo json_encode($json);		
    }
	
	//Product  Damage product items get
	 public function suggestions_del($value='')
    {
		$term=$this->input->get('term');
		$data['product_damage'] = $this->Product_Damage_Model->get_products_suggestions($term);
		$json = array();
		//echo "Count:".count($data['product_damage']);
		//print_r($data['product_damage']);
		foreach ($data['product_damage'] as $row)
		{
			$product_name=$row['product_name'];
			$product_code=$row['product_code'];
			$product_part_no=$row['product_part_no'];
			$product_oem_part_number=$row['product_oem_part_number'];
			$product_id=$row['product_id'];
			$product_price=$row['product_price'];
			$sendParameters="'$product_id','$product_name','$product_code','$product_price'";
			$sendParameters="$product_id,$product_name,$product_code,$product_price";
			$extraName='';
			$extraName.=", Selling Price: ".number_format($product_price, 2, '.', ',');
			if($product_part_no) $extraName.=", Part No: $product_part_no";
			if($product_oem_part_number) $extraName.=", OEM Part No: $product_oem_part_number";
			
			 $json_itm=array(
			 		'id'=> $row['product_id'],
					'product_id'=> $row['product_id'],
					'product_code'=> $row['product_code'],
					'product_name'=> $row['product_name'],
					'product_price'=> $row['product_price'],
					'product_part_no'=> $row['product_part_no'],
					'product_oem_part_number'=> $row['product_oem_part_number'],
					'item_cost'=> $row['product_cost'],
					'product_sn'=> $row['product_sn'],
					'product_w'=> $row['product_w'],
					'product_sn_exissts'=> $row['product_sn_exissts'],
					'product_oem_part_number'=> $row['product_oem_part_number'],
                    'value'=> $row['product_name']." (".$row['product_code'].")",
                    'label'=> $row['product_name']." (".$row['product_code'].")$extraName"
                    );
					array_push($json,$json_itm);
		}		
		echo json_encode($json);		
    }
	
	//Sale details page
	public function product_damage_details()
	{
		
		$pdmg_id=$this->input->get('pdmg_id');
		$data['pdmg_details']= $this->Product_Damage_Model->get_product_damage_info($pdmg_id);
		
		$data['warehouse_details']= $this->Warehouse_Model->get_warehouse_info($data['pdmg_details']['warehouse_id']);
		
		
		//get sale item list
		$data['pdmg_item_list']= $this->Product_Damage_Model->get_product_damage_item_list_by_product_damage_id($pdmg_id);
		
		
		
		$data['cr_limit_list'] = $this->Common_Model->get_all_cr_limit();
        $this->load->view('models/product_damage_print',$data);
	}	
	
	//Product  Damage list
	public function list_product_damage()
	{
	$requestData= $_REQUEST;
	
	$columns = array( 
		0 =>'pdmg_id', 
		1 => 'pdmg_id',
		2=> 'pdmg_id',
		3 =>'pdmg_id', 
		4 => 'pdmg_id',
		5=> 'pdmg_id'
	);
	
	$data = array();
	$requestData=$this->input->post();
	
	$search_key=$this->input->post('search');
	$warehouse_id=$this->input->post('warehouse_id');
		$tech_id=$this->input->post('tech_id');
		$srh_from_date=$this->input->post('srh_from_date');
		$srh_to_date=$this->input->post('srh_to_date');
		
		$search_key_val=$search_key['value'];
	$start=$this->input->post('start');
	$length=$this->input->post('length');
	
//	echo "warehouse_id:$warehouse_id";
	$product_damage = $this->Product_Damage_Model->get_all_product_damage($start,$length,$search_key_val,$srh_from_date,$srh_to_date,$warehouse_id,$tech_id,0);
	$totalData = $this->Product_Damage_Model->get_all_product_damage('','',$search_key_val,$srh_from_date,$srh_to_date,$warehouse_id,$tech_id,1);
	$totalFiltered = $totalData;  
	
	foreach ($product_damage as $row){
		$nestedData=array(); 
		$pdmg_id=$row['pdmg_id'];
		$total_paid_amount='';
		$nestedData[] =display_date_time_format($row['pdmg_datetime']);
		$nestedData[] = $row['pdmg_reference_no'];
		
		$nestedData[] = $row['name'];		
		$nestedData[] =number_format($row['pdmg_total'], 2, '.', '');		
		$url=base_url("product_damage/pdmg_details?pdmg_id=$pdmg_id");
		$actionTxtUpdate='<a onClick="fbs_click('.$row['pdmg_id'].')" data-toggle="modal" href="#" class="btn btn-xs btn-blue tooltips" data-placement="top" data-original-title="Edit suppliers"><i class="clip-zoom-in-2"></i></a> &nbsp;';
		
		$actionTxtViewDetails='<a href="'.base_url().'product_damage/view/'.$pdmg_id.'" class="btn btn-xs btn-blue tooltips" data-placement="top" data-original-title="Edit suppliers"><i class="fa fa-file-text-o"></i></a> &nbsp;';
	
	$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 onClick="fbs_click('.$row['pdmg_id'].')" data-toggle="modal" href="#" data-placement="top" data-original-title="Edit suppliers"><i class="fa fa-print"></i> Print Product Damage</a></li>
							
                            </ul></div>';
							
							/*<li><a href="'.base_url().'product_damage/view/'.$pdmg_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); 
	}
}