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

class Return_Damage_Cus extends CI_Controller {

    var $main_menu_name = "return_damage_cus";
	var $sub_menu_name = "return_damage_cus";

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

		$this->load->model('Sales_Rep_Issue_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('Sales_Return_Model');
		$this->load->model('User_Model');
		$this->load->model('Transfer_Model');
		$this->load->model('Purchases_Model');
		$this->load->model('Product_Damage_Model');
		$this->load->model('Return_Damage_Cus_Model');
		$this->load->model('Product_Damage_Customer_Model');
		//$this->load->model('Sales_Rep_Model');	
		//$this->load->model('Purchases_Model');
		}
	
	//Sales list page load
	public function index()
	{
		 
		$data['sales_rep_issues']=$this->Sales_Rep_Issue_Model->get_all_sales_rep_issue();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;
        $this->load->view('damage_cus_list',$data);
	}	
	public function list_damage_cus_details()
	{	
	
	$data=array();
	
	$damage_cus_details=$this->Return_Damage_Cus_Model->get_all_damage_cus_details();
	
		
	foreach ( $damage_cus_details as $row)
	{
		$nestedData=array();
		$cus_damage_id=$row['cus_damage_id'];
		$return_quantity=$this->Return_Damage_Cus_Model->get_avalable_product_return_qt($row['product_id'],$row['cus_damage_id']);
		$r_qt = $return_quantity['quantity'];
	    $cus_damage_qty=$row['quantity'];
		$nestedData[]=$row['recieved_date'];
		$nestedData[]=$row['reference_no'];
		$nestedData[]=$row['sale_reference_no']." / ".$row['card_ref_number'];
		$nestedData[]=$row['cus_name'];
		$p_code=$row['product_code'];
		$p_name=$row['product_name'];
		$nestedData[]=$p_name.'('.$p_code.')';
		$nestedData[]=$row['quantity'];
		if ($r_qt==0) {
		  $pay_st = '<span class="label label-warning">Pending</span>';
		}else if($cus_damage_qty==$r_qt){
			$pay_st = '<span class="label label-success">Completed</span>';
		 }
		else if($cus_damage_qty>$r_qt){
			$pay_st = '<span class="label label-info">Partial</span>';
		  }
		
		
		
		$nestedData[]=$pay_st;
		
		$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().'product_damage_customer/view/'.$cus_damage_id.'"><i class="fa fa-file-text-o"></i> Customer Damage Product Details</a></li>
							<li><a href="'.base_url().'return_damage_cus/manage/'.$cus_damage_id.'/'.$row['product_id'].'"><i class="fa fa-file-text-o"></i>Customer Damage Return</a></li>
							<li><a href="'.base_url().'return_damage_cus/view_customer/'.$row['cus_id'].'"><i class="fa fa-file-text-o"></i>Customer Details</a></li>
							
                            </ul></div>';
	
		$data[]=$nestedData;
	}	
	
	
	$json_data = array(
	"data"=>$data
	);
	
	echo json_encode($json_data);
	
		
	}
	public function manage()
	{	
		$cus_damage_id = $this->uri->segment(3);	
		$product_id=$this->uri->segment(4);			
		
			
			$data['cus_damage_id']=$this->$cus_damage_id = $this->uri->segment(3);
			$data['product_id']=$this->$product_id = $this->uri->segment(4);
			$cus_damage_details=$this->Return_Damage_Cus_Model->get_damage_cus_details($cus_damage_id);
			
			$warehouse_id=$cus_damage_details['warehouse_id'];
			$data['sale_details']=$this->Return_Damage_Cus_Model->get_sale_ref_no($cus_damage_id);
		
			$data['username_list'] = $this->Product_Damage_Customer_Model->get_all_username();
			$data['warehouse_list']=$this->Warehouse_Model->get_all_warehouse();
			$data['warehouse_details']=$this->Return_Damage_Cus_Model->warehouse_info($warehouse_id);
			
			
			$data['type']='E';
			//$data['type1']=$sale_rep_issue_id;
			$data['main_menu_name']=$this->main_menu_name;
			$data['sub_menu_name']='manage';
			//$data['sales_rep_issue_details']=$this->Sales_Rep_Issue_Model->get_sales_detail_by_sales_rep_issue_id($sale_rep_issue_id);
			//$data['sales_rep_issue_items_details']=$this->Sales_Rep_Issue_Model->get_sale_rep_issue_item_list_by_sale_id($sale_rep_issue_id);
			$this->load->view('damage_cus_return',$data); 
			
			
		
	}
	 public function suggestions($value='')
    {
		$term=$this->input->get('term');
		$invoice_id=$this->input->get('invoice_id');
		$werehouse_id=$this->input->get('WH');
		$product=$this->input->get('PID');
		$data['product_damage'] = $this->Return_Damage_Cus_Model->get_products_suggestions($invoice_id,$term,$product);
		$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";  
			$qty=0;
			$r_qty=$this->Return_Damage_Cus_Model->get_avalable_product_return_qt($product_id,$invoice_id);
			$return_q=$r_qty['quantity'];
			$d_qty=$this->Return_Damage_Cus_Model->get_cus_damage_item_qty($product_id,$invoice_id);
			$damage_q=$d_qty[0]['quantity'];
			$qty=($damage_q)-($return_q);
			$extraName.=", Qty in stock: ".$qty;
			if($qty>0)
			{
			 $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'],
					 'qtyforcheck'=>$qty,
					'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);		
    }
	public function save_product_damage_cus_return()
	{   $customer_id='';
		$pdmg_reference_no=$this->input->post('pdmg_reference_no');
		$invoice_id=$this->input->post('invoice_id');
		$customer_id=$this->Product_Damage_Customer_Model->get_cus_id($invoice_id);
		$sale_id=$this->Product_Damage_Customer_Model->get_sales_id($invoice_id);
		$cus_damage_id=$this->input->post('cus_damage_id');
		$warehouse_id=$this->input->post('warehouse_id');
		$rowCount=$this->input->post('rowCount');
		$user_id=$this->input->post('user_id');
		$pdmg_datetime=$this->input->post('pdmg_datetime');
		$pdmg_datetime=date('Y-m-d H:i:s');
		//$pdmg_inv_discount=$this->input->post('pdmg_inv_discount');		
		//$pdmg_total=$this->input->post('pdmg_total');
		
		$pdmg_datetime_created=date('Y-m-d H:i:s');
		
		$error='';
		$disMsg='';
		$lastid='';
		$id='';
		
		if(!$error){
			
			$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=array(
				     'cus_damage_id'=>$cus_damage_id,
					 'warehouse_id'=>$warehouse_id,
				     'sale_reference_no'=>$sale_id[0]['sale_reference_no'],
				    // 'user_id'=>$user_id,
					 'deliverer_id'=>$user_id,
					 'product_id'=>$row[$i]['product_id'][0],
					
					 'cus_id'=>$customer_id[0]['customer_id'],
				     'quantity'=>$row[$i]['qty'][0],
					 'return_date'=>$pdmg_datetime
					//'pdmgitm_unit_cost'=>$row[$i]['unit_price'][0],
				
				);
				$_insert=$this->Return_Damage_Cus_Model->save_product_damage_customer($data,$id);
			$lastid=$this->db->insert_id();
			$id=$lastid;
			$disMsg='Sale successfully added';
		  

				}
			}
			 
		
		}else {
			
			$disMsg='Please select these before adding any product:'.$disMsg;
		}	
		
		$this->session->set_flashdata('message', 'Product damage details successfully added!');
		
		echo json_encode(array('cus_damage_id'=>$lastid,'error'=>$error,'disMsg'=>$disMsg,));
		
		}
	public function view_customer()
		{
			 $cus_id = $this->uri->segment(3);	
			 
    if(!empty($cus_id)){

          $data['customer_details'] = $this->Return_Damage_Cus_Model->get_customer_details($cus_id);
		  $data['main_menu_name'] = $this->main_menu_name;
          $data['sub_menu_name'] = $this->sub_menu_name;
          $this->load->view('cus_details',$data);

    }else{

          show_404();
	
		}
}
}
	
