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

class Loading_o_ctrl extends user_index {
 
	
	public function __construct(){ 
		parent::__construct();
		$data = array();
		$this->load->model('admin_model');	
		$this->load->model('loading_model');	
	}
	
	/*----------------- load loading vehicle  entry list page -----------------*/
	public function vehicle_entry_list_for_loading(){	
		if($this->user['panel_type']=='2' && in_array("2", $this->u_roles)){
			$data['sdata'] 				= array();
			$data['subview'] 			= 'loading_o/vehicle_entry_list_for_loading';
			$data['active_cls'] 		= 'loadaa_lcs';		
			$data['sdata']['lul_list'] 			= $this->loading_model->get_loading_list('Loading','','','1','0','','','0');		
			$this->load->view('common/sidepanel',$data);
		}else{
			redirect('login_ctrl/logout');
		}
	}
	

	/*----------------- load vehicle slip entry page ----------------*/
	public function vehicle_slip_entry($lul_id){	
		if($this->user['panel_type']=='2' && in_array("2", $this->u_roles)){		
			$data['sdata'] 						= array();
			$data['subview'] 					= 'loading_o/vehicle_slip_entry';
			$data['active_cls'] 				= 'loadaa_lcs';				
			$data['sdata']['lul_data'] 			= $this->loading_model->get_loading_list('Loading',$lul_id);	
			$data['sdata']['luml_data'] 		= $this->loading_model->get_loading_or_unloading_meterial_list($lul_id);	
			// echo $this->db->last_query();return;
			$this->load->view('common/sidepanel',$data);
		}else{
			redirect('login_ctrl/logout');
		}
	}
	
	
	/*----------------- submit W/Bridge Ticket No  -----------------*/
	public function submit_vehicle_slip($lul_id= false){
		if(($this->user['panel_type']!='2') && (!in_array("2", $this->u_roles))){
			redirect('login_ctrl/logout');
		}	

		$this->form_validation->set_rules('lul_W_Bridge_Ticket_No', 'W/Bridge Ticket No', 'trim|required');
		// $this->form_validation->set_rules('lul_w_bridge_gross', 'gross', 'trim|required');
		$this->form_validation->set_rules('lul_w_bridge_tare', 'tare', 'trim|required');
		$this->form_validation->set_rules('lul_w_bridge_net', 'net', 'trim|required');
		$this->form_validation->set_rules('lul_bl_no', 'Bl No', 'trim|required');
			
		if ($this->form_validation->run() === FALSE) {
			$e_msg = '';				  
			$e_msg .= form_error('lul_W_Bridge_Ticket_No');  				  
			// $e_msg .= form_error('lul_w_bridge_gross');  				  
			$e_msg .= form_error('lul_bl_no');  				  
			$e_msg .= form_error('lul_w_bridge_tare');  				  
			$e_msg .= form_error('lul_w_bridge_net');  				  
			$this->session->set_flashdata('tos_error', $e_msg);
			redirect('loading_o_ctrl/vehicle_slip_entry/'.$lul_id);				
				
		}	
			
		$data['lul_details']['lul_lo_id'] 					= $this->user['i_user_id'];
		$data['lul_details']['lul_lo_date_time'] 			= date("Y-m-d h:i a");
		$data['lul_details']['lul_W_Bridge_Ticket_No'] 		= $this->input->post('lul_W_Bridge_Ticket_No');
		$data['lul_details']['lul_w_bridge_gross'] 			= $this->input->post('lul_w_bridge_gross');
		$data['lul_details']['lul_w_bridge_tare'] 			= $this->input->post('lul_w_bridge_tare');
		$data['lul_details']['lul_w_bridge_net'] 			= $this->input->post('lul_w_bridge_net');
		$data['lul_details']['lul_bl_no'] 			= $this->input->post('lul_bl_no');
		$data['lul_details']['lul_return_status'] 			= '1';
		
		$edit_lul = $this->admin_model->update_data('loading_unloading_list','lul_id',$lul_id,$data['lul_details']);

		
			
		if($edit_lul!=false){ 
			$this->session->set_flashdata('tos_success', 'Vehicle slip submitted.');
			redirect('print_ctrl/print_load_o_view_loading/'.$lul_id);
		}else {
			$this->session->set_flashdata('tos_error', 'Try again !...');
			redirect('loading_o_ctrl/vehicle_entry_list_for_loading');
		}			
			
	}

	/*----------------- Vehicle Return Selection Window(After Loading) -----------------*/
	public function vehicle_return_selection_list(){	
		if($this->user['panel_type']=='2' && in_array("2", $this->u_roles)){				
			$data['sdata'] 				= array();
			$data['subview'] 			= 'loading_o/vehicle_return_selection_list';
			$data['active_cls'] 		= 'loadaa_lcs';		
			$data['sdata']['lul_list'] 			= $this->loading_model->get_loading_list('Loading','','','1','','','','1');	
			// echo $this->db->last_query();return;	
			$this->load->view('common/sidepanel',$data);
		}else{
			redirect('login_ctrl/logout');
		} 
	}

	/*----------------- load vehicle slip authorizing page ----------------*/
	public function vehicle_slip_return_form($lul_id){	
		if($this->user['panel_type']=='2' && in_array("2", $this->u_roles)){						
			$data['sdata'] 						= array();
			$data['subview'] 					= 'loading_o/vehicle_slip_return_form';
			$data['active_cls'] 				= 'loadaa_lcs';				
			$data['sdata']['lul_data'] 			= $this->loading_model->get_loading_list('Loading',$lul_id);
			$data['sdata']['luml_data'] 		= $this->loading_model->get_loading_or_unloading_meterial_list($lul_id);	
			// echo $this->db->last_query();return;
			$this->load->view('common/sidepanel',$data);
		}else{
			redirect('login_ctrl/logout');
		}
	}
	

	/*----------------- submit_vehicle_slip_return_form -----------------*/
	public function submit_vehicle_slip_return_form($lul_id= false){	
		if(($this->user['panel_type']!='2') && (!in_array("2", $this->u_roles))){
			redirect('login_ctrl/logout');
		}	
			
		$this->form_validation->set_rules('lul_load_by', 'Load By', 'trim|required');
		$this->form_validation->set_rules('lul_l_name', 'Name', 'trim|required');
		$this->form_validation->set_rules('lul_bag_loaded', 'Bags Loaded', 'trim|required');
		$this->form_validation->set_rules('lul_bag_short', 'Bags(Short)', 'trim|required');
		$this->form_validation->set_rules('lul_kgs', 'KGS', 'trim|required');
		$this->form_validation->set_rules('lul_w_bridge_gross', 'gross', 'trim|required');
		//$this->form_validation->set_rules('lul_w_bridge_tare', 'tare', 'trim|required');
		$this->form_validation->set_rules('lul_w_bridge_net', 'net', 'trim|required');

		$this->form_validation->set_rules('lul_container_no', 'Container No', 'trim|required');
		$this->form_validation->set_rules('lul_tra_seal_no', 'TRA Seal No ', 'trim|required');
		$this->form_validation->set_rules('lul_container_tare_wt', 'Container Tare Wt', 'trim|required');
		$this->form_validation->set_rules('lul_shipping_line', 'Shipping Line', 'trim|required');

			
		if ($this->form_validation->run() === FALSE) {
			$e_msg = '';
			$e_msg .= form_error('lul_load_by');  				  
			$e_msg .= form_error('lul_l_name');  				  
			$e_msg .= form_error('lul_bag_loaded');  				  
			$e_msg .= form_error('lul_bag_short');  				  
			$e_msg .= form_error('lul_kgs');  					  
			$e_msg .= form_error('lul_w_bridge_gross');  				  
			//$e_msg .= form_error('lul_w_bridge_tare');  				  
			$e_msg .= form_error('lul_w_bridge_net'); 	
				
			$e_msg .= form_error('lul_container_no');  				  
			$e_msg .= form_error('lul_tra_seal_no');  				  
			$e_msg .= form_error('lul_container_tare_wt');  				  
			$e_msg .= form_error('lul_shipping_line');  				  
				
			$this->session->set_flashdata('tos_error', $e_msg);
			redirect('loading_o_ctrl/vehicle_slip_return_form/'.$lul_id);					
		}	
			
		$data['lul_details']['lul_w_bridge_gross'] 			= $this->input->post('lul_w_bridge_gross');
    	//$data['lul_details']['lul_w_bridge_tare'] 			= $this->input->post('lul_w_bridge_tare');
    	$data['lul_details']['lul_w_bridge_net'] 			= $this->input->post('lul_w_bridge_net');
		
		$data['lul_details']['lul_load_by'] 				= $this->input->post('lul_load_by');
		$data['lul_details']['lul_l_name'] 					= $this->input->post('lul_l_name');
		$data['lul_details']['lul_bag_loaded'] 				= $this->input->post('lul_bag_loaded');
		$data['lul_details']['lul_bag_short'] 				= $this->input->post('lul_bag_short');
		$data['lul_details']['lul_kgs'] 					= $this->input->post('lul_kgs');
		$data['lul_details']['lul_loading_date'] 			= date('Y-m-d');
		$data['lul_details']['lul_return_status'] 			= '2';

		$data['lul_details']['lul_container_no'] 			= $this->input->post('lul_container_no');
		$data['lul_details']['lul_tra_seal_no'] 			= $this->input->post('lul_tra_seal_no');
		$data['lul_details']['lul_container_tare_wt'] 		= $this->input->post('lul_container_tare_wt');
		$data['lul_details']['lul_shipping_line'] 			= $this->input->post('lul_shipping_line');

			
			
		$edit_lul = $this->admin_model->update_data('loading_unloading_list','lul_id',$lul_id,$data['lul_details']);
	
		if($edit_lul==1){
			$this->session->set_flashdata('tos_success', 'Loading vehicle slip authorized details updated.');
			redirect('loading_o_ctrl/vehicle_return_selection_list');
		}else {
			$this->session->set_flashdata('tos_error', 'Try again !...');
			redirect('loading_o_ctrl/vehicle_return_selection_list');
		}			
				
	}

	
}
