<?php
class Generate_bill_model extends Crud_model
{

public function get_bill($entered_str)
	{
		$json_data 		=	array(0=>array('id'=>'','text'=>"Select Branch Name"));
		$condition		=	array();
		if($entered_str) 
			$condition["branch_name LIKE "] = "%$entered_str%";
		$drpdwn_data	=	$this->select_data(PORT_TBL,$select='branch_id,branch_name',$condition, 0, array(),'branch_name',"ASC",'', 0, null);
		$i=1;
		foreach($drpdwn_data as $row)
		{
			$json_data[$i]['id']	=	$row['branch_id'];
			$json_data[$i]['text']  = 	$row['branch_name'];
			$i++;
		}	
		$data['results'] = $json_data;
		return json_encode($data);
	}

	public function get_branch($entered_str)
	{
		$json_data 		=	array(0=>array('id'=>'','text'=>"Select Branch"));
		$condition		=	array();
		
		if($entered_str) 
			$condition["branch_name LIKE "] = "%$entered_str%";
		$drpdwn_data   	=	$this->select_data(BRANCH_TBL,$select='branch_id,branch_name',$condition,0, array(),'branch_name',"ASC",'', 0, null);
		$i=1;
		foreach($drpdwn_data as $row)
		{
			$json_data[$i]['id']	=	$row['branch_id'];
			$json_data[$i]['text']  = 	$row['branch_name'];
			$i++;
		}	
		$data['results'] = $json_data;
		return json_encode($data);
	}


	public function get_company($entered_str)
	{
		$json_data 		=	array(0=>array('id'=>'','text'=>"Select Company"));
		$condition		=	array();
		
		if($entered_str) 
			$condition["cmp_name LIKE "] = "%$entered_str%";
		$drpdwn_data   	=	$this->select_data(COMPANY_TBL,$select='cmp_id,cmp_name',$condition,0, array(),'cmp_name',"ASC",'', 0, null);
		$i=1;
		foreach($drpdwn_data as $row)
		{
			$json_data[$i]['id']	=	$row['cmp_id'];
			$json_data[$i]['text']  = 	$row['cmp_name'];
			$i++;
		}	
		$data['results'] = $json_data;
		return json_encode($data);
	}

	public function get_consignor($entered_str){
		$json_data 		=	array(0=>array('id'=>'','text'=>"Select Consignor"));
		$condition		=	array();
		
		if($entered_str) 
			$condition["consignor_name LIKE "] = "%$entered_str%";
		$drpdwn_data   	=	$this->select_data(CONSIGNOR_TBL,$select='consignor_id,consignor_name',$condition,0, array(),'consignor_name',"ASC",'', 0, null);
		$i=1;
		foreach($drpdwn_data as $row)
		{
			$json_data[$i]['id']	=	$row['consignor_id'];
			$json_data[$i]['text']  = 	$row['consignor_name'];
			$i++;
		}	
		$data['results'] = $json_data;
		return json_encode($data);
	}
	public function get_consignee($entered_str){
			$json_data 		=	array(0=>array('id'=>'','text'=>"Select Consignee"));
			$condition		=	array();
			
			if($entered_str) 
				$condition["consignee_name LIKE "] = "%$entered_str%";
			$drpdwn_data   	=	$this->select_data(CONSIGNEE_TBL,$select='consignee_id,consignee_name',$condition,0, array(),'consignee_name',"ASC",'', 0, null);
			$i=1;
			foreach($drpdwn_data as $row)
			{
				$json_data[$i]['id']	=	$row['consignee_id'];
				$json_data[$i]['text']  = 	$row['consignee_name'];
				$i++;
			}	
			$data['results'] = $json_data;
			return json_encode($data);
		}

	public function get_notify($entered_str){
				$json_data 		=	array(0=>array('id'=>'','text'=>"Select Notify Name"));
				$condition		=	array();
				
				if($entered_str) 
					$condition["notify_name LIKE "] = "%$entered_str%";
				$drpdwn_data   	=	$this->select_data(NOTIFY_TBL,$select='notify_id,notify_name',$condition,0, array(),'notify_name',"ASC",'', 0, null);
				$i=1;
				foreach($drpdwn_data as $row)
				{
					$json_data[$i]['id']	=	$row['notify_id'];
					$json_data[$i]['text']  = 	$row['notify_name'];
					$i++;
				}	
				$data['results'] = $json_data;
				return json_encode($data);
			}
	public function get_portname($entered_str){
					$json_data 		=	array(0=>array('id'=>'','text'=>"Select Port Name"));
					$condition		=	array();
					
					if($entered_str) 
						$condition["port_of_dis_name LIKE "] = "%$entered_str%";
					$drpdwn_data   	=	$this->select_data(PORT_TBL,$select='port_of_dis_id,port_of_dis_name',$condition,0, array(),'port_of_dis_name',"ASC",'', 0, null);
					$i=1;
					foreach($drpdwn_data as $row)
					{
						$json_data[$i]['id']	=	$row['port_of_dis_id'];
						$json_data[$i]['text']  = 	$row['port_of_dis_name'];
						$i++;
					}	
					$data['results'] = $json_data;
					return json_encode($data);
				}

	public function select_max_bill_no($branch_id){

		$this->db->select_max('bill_no');
		$this->db->from('bill_details');		
		$this->db->where('bill_brnch_id',$branch_id);
		$result = $this->db->get()->result_array();
		return $result;
	}

	public function select_bill_details($limit, $start, $search_data=array()){



		$bill_id              = $search_data['bill_id'];
		$bill_created_date    = $search_data['bill_created_date'];
		$bill_status          = $search_data['bill_status'];


		$this->db->select('bill.bill_id,bill.bill_no,bill.bill_products,bill.bill_created_date,bill.bill_status,c.cmp_shrt_code,br.branch_shrt_code,consgnr.consignor_name,consgne.consignee_name,port.port_of_dis_shrt_code,notify.notify_name');
	    $this->db->from('bill_details AS bill');
	    $this->db->join('branch AS br', 'br.branch_id = bill.bill_brnch_id', 'left');
	    $this->db->join('company AS c', 'c.cmp_id = bill.bill_company_id', 'left');
	    $this->db->join('consignee AS consgne', 'consgne.consignee_id = bill.bill_cnsgne_id', 'left');
	    $this->db->join('consignor AS consgnr', 'consgnr.consignor_id = bill.bill_cnsgnr_id', 'left');
	    $this->db->join('notify AS notify', 'notify.notify_id = bill.bill_notify_id', 'left');
	    $this->db->join('port_of_dis AS port', 'port.port_of_dis_id = bill.bill_port_id', 'left');
	   
	    //$this->db->where('bill.bill_status != ','2',FALSE);
	    $this->db->order_by('bill.bill_id','desc');


	    if($bill_id!=''){
			$this->db->where('bill.bill_id',$bill_id);
			
		}
		
		if($bill_created_date!=''){
			$this->db->where('bill.bill_created_date',$bill_created_date);
			
		}
		if($bill_status!=''){
			$this->db->where('bill.bill_status',$bill_status);
		}else{
			$this->db->where('bill.bill_status != ','2',FALSE);
		}




        	$this->db->limit($limit, $start);

	    $result = $this->db->get()->result_array();
	    return $result;
	}


	public function search_record_count($search_data=array()){



		$bill_id              = $search_data['bill_id'];
		$bill_created_date    = $search_data['bill_created_date'];
		$bill_status          = $search_data['bill_status'];


		$this->db->select('bill.bill_id,bill.bill_no,bill.bill_products,bill.bill_created_date,bill.bill_status,c.cmp_shrt_code,br.branch_shrt_code,consgnr.consignor_name,consgne.consignee_name,port.port_of_dis_shrt_code,notify.notify_name');
	    $this->db->from('bill_details AS bill');
	    $this->db->join('branch AS br', 'br.branch_id = bill.bill_brnch_id', 'left');
	    $this->db->join('company AS c', 'c.cmp_id = bill.bill_company_id', 'left');
	    $this->db->join('consignee AS consgne', 'consgne.consignee_id = bill.bill_cnsgne_id', 'left');
	    $this->db->join('consignor AS consgnr', 'consgnr.consignor_id = bill.bill_cnsgnr_id', 'left');
	    $this->db->join('notify AS notify', 'notify.notify_id = bill.bill_notify_id', 'left');
	    $this->db->join('port_of_dis AS port', 'port.port_of_dis_id = bill.bill_port_id', 'left');
	   
	    //$this->db->where('bill.bill_status != ','2',FALSE);
	    $this->db->order_by('bill.bill_id','desc');


	    if($bill_id!=''){
			$this->db->where('bill.bill_id',$bill_id);
			
		}
		
		if($bill_created_date!=''){
			$this->db->where('bill.bill_created_date',$bill_created_date);
			
		}
		if($bill_status!=''){
			$this->db->where('bill.bill_status',$bill_status);
		}else{
			$this->db->where('bill.bill_status != ','2',FALSE);
		}




   		$query = $this->db->get();
        return $query->num_rows();
	}


	 public function record_count() 
    {
        return $this->db->count_all('bill_details');
    }

	public function changestatus($id){

		$this->db->set('bill_status', '1');
		$this->db->where('bill_id', $id);
		$this->db->update('bill_details');
	}



	public function rejectstatus($id){

			$this->db->set('bill_status', '2');
			$this->db->where('bill_id', $id);
			$this->db->update('bill_details');
		}


	public function	get_branch_bill_no($id){
		$this->db->select('branch_bill_no');
		$this->db->from('branch');
		$this->db->where('branch_id',$id);
		 $result = $this->db->get()->row();
		    return json_encode($result);


	}

		public function get_bill_details($bill_id){

			$this->db->select('n1.notify_name as notify_name_party_2,n1.notify_addr1 as notify_addr1_party_2,n1.notify_addr2 as notify_addr2_party_2,n1.notify_addr3 as notify_addr3_party_2,n1.notify_addr4 as notify_addr4_party_2,n_country.name as notify_country_name,n_state.state as notify_state_name,consgne_country.name as consignee_country,consgne_state.state as consignee_state,consgnr_country.name as consignor_country,consgnr_state.state as consignor_state,consgne.consignee_name,consgne.consignee_addr1,consgne.consignee_addr2,consgne.consignee_addr3,consgne.consignee_addr4,consgne.consignee_pin,consgne.consignee_con_no,consgne.consignee_fax,consgnr.consignor_name,consgnr.consignor_addr1,consgnr.consignor_addr2,consgnr.consignor_addr3,consgnr.consignor_addr4,consgnr.consignor_pin,consgnr.consignor_con_no,consgnr.consignor_fax,n.notify_name,n.notify_addr1,n.notify_addr2,n.notify_addr3,n.notify_addr4,n.notify_pin,n.notify_con_no,n.notify_fax,c.cmp_shrt_code,br.branch_shrt_code,port.port_of_dis_shrt_code,port.port_of_dis_name,bill.bill_no,bill.bill_products,bill.date_of_issue,bill.place_issue,bill.place_issue,bill.freight_charges,bill.place_of_delivery,bill.ocean_vessel,bill.freight_payment,bill.no_of_bl,bill.port_of_loading,bill.place_receipt,bill.delivery_of_goods,bill.voyage,bill.pre_carriage,bill.container_no,bill.goods,bill.gross_weight,bill.cube_measure,bill.desc_goods,bill.temp_cntrl,bill.bill_created_date');
		    $this->db->from('bill_details AS bill');
		    $this->db->join('branch AS br', 'br.branch_id = bill.bill_brnch_id', 'left');
		    $this->db->join('company AS c', 'c.cmp_id = bill.bill_company_id', 'left');
		    $this->db->join('consignee AS consgne', 'consgne.consignee_id = bill.bill_cnsgne_id', 'left');
		    $this->db->join('country as consgne_country', 'consgne_country.country_id = consgne.consignee_country_id', 'left');
		    $this->db->join('state as consgne_state', 'consgne_state.state_id = consgne.consignee_state_id', 'left');
		    $this->db->join('consignor AS consgnr', 'consgnr.consignor_id = bill.bill_cnsgnr_id', 'left');
		    $this->db->join('country as consgnr_country', 'consgnr_country.country_id = consgnr.consignor_country_id', 'left');
		    $this->db->join('state as consgnr_state', 'consgnr_state.state_id = consgnr.consignor_state_id', 'left');
		   	$this->db->join('notify AS n', 'n.notify_id = bill.bill_notify_id', 'left');
		   	$this->db->join('notify AS n1', 'n1.notify_id = bill.bill_notify_party2', 'left');
		   	$this->db->join('country as n_country', 'n_country.country_id = n.notify_country_id', 'left');
		    $this->db->join('state as n_state', 'n_state.state_id = n.notify_state_id', 'left');
		    $this->db->join('port_of_dis AS port', 'port.port_of_dis_id = bill.bill_port_id', 'left');
		    $this->db->where('bill.bill_status != ','2',FALSE);
		    $this->db->where('bill.bill_id',$bill_id);
		    $this->db->order_by('bill.bill_id','asc');
		    $result = $this->db->get()->row();
		    return $result;
		}

		public function select_bill_no(){
			$this->db->select('bill.bill_id,c.cmp_shrt_code,br.branch_shrt_code,port.port_of_dis_shrt_code,bill.bill_no');
			$this->db->from('bill_details AS bill');
			$this->db->join('branch AS br', 'br.branch_id = bill.bill_brnch_id', 'left');
		    $this->db->join('company AS c', 'c.cmp_id = bill.bill_company_id', 'left');
			$this->db->join('port_of_dis AS port', 'port.port_of_dis_id = bill.bill_port_id', 'left');
			$result = $this->db->get()->result_array();
	    	return $result;
		}

		public function select_brnach_name($bill_id){
			$this->db->select('b.branch_name');
			$this->db->from('bill_details as bill');
			$this->db->join('branch as b','b.branch_id = bill.bill_brnch_id','left');
			$this->db->where('bill.bill_id',$bill_id);
			$result = $this->db->get()->row();
		    return $result;
		}

	
}
?>