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



class Hw_Delivery extends CI_Controller {

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

    public function __construct()
    {
        parent::__construct();
        date_default_timezone_set("Asia/Colombo");
        $this->load->model('Marketing_Delivery_Model');
		$this->load->model('Common_Model');
		$this->load->model('Product_Models');
		$this->load->model('pos_model');
		$this->load->model('Hw_Delivery_Model');
		$this->load->model('customer_model');
		$this->load->model('Warehouse_Model');
		$this->load->model('store_notes_model');
		
    }

	public function index()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;		
	}

	public function manage()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;	
		$stn_id = $this->input->get('id');	    
	    $data['stn_info']=$this->store_notes_model->get_store_note_info($stn_id);
	    $data['stn_items']=$this->store_notes_model->get_store_note_items($data['stn_info']['warehouse_id'],$data['stn_info']['stn_no']);
		$data['warehouse_details']=$this->Warehouse_Model->get_warehouse_info($data['stn_info']['warehouse_id']);
		$data['vehicle_list']=$this->Marketing_Delivery_Model->get_all_vehicle();
		$this->load->view('store_delivery/hw_delevery',$data);	
	}
	
		public function pos_manage()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;	
		$stn_id = $this->input->get('id');	    
	    $data['stn_info']=$this->store_notes_model->get_store_note_info($stn_id);
	    $data['stn_items']=$this->store_notes_model->get_store_note_items($data['stn_info']['warehouse_id'],$data['stn_info']['stn_no']);
		$data['warehouse_details']=$this->Warehouse_Model->get_warehouse_info($data['stn_info']['warehouse_id']);
		$data['vehicle_list']=$this->Marketing_Delivery_Model->get_all_vehicle();
		$this->load->view('store_delivery/pos_hw_delevery',$data);	
	}

	public function update_vehicle()
	{		
		$vh_id=$this->input->get('vh_id');
		$mst_id=$this->input->get('mst_id');
		$data=array('vhicle_id'=>$vh_id);
		$result=$this->Hw_Delivery_Model->update_vehicle($mst_id,$data);	

		if($result==true)
		{
		$st = array('error'=>0,'status' =>1,'validation'=>"Done",'delivery_id'=>$vh_id);	
		}
		else
		{
		$st = array('error'=>1,'status' =>0,'validation'=>"Error ! Data not saved",'delivery_id'=>$vh_id);		
		}
           echo json_encode($st);
	}

	public function update_customer_issue()
	{		
		$vh_id=$this->input->get('vh_id');
		$mst_id=$this->input->get('mst_id');
		$data=array('customer_issued'=>$vh_id);
		$result=$this->Hw_Delivery_Model->update_vehicle($mst_id,$data);	

		if($result==true)
		{
		$st = array('error'=>0,'status' =>1,'validation'=>"Done",'delivery_id'=>$vh_id);	
		}
		else
		{
		$st = array('error'=>1,'status' =>0,'validation'=>"Error ! Data not saved",'delivery_id'=>$vh_id);		
		}
           echo json_encode($st);
	}
	
		public function update_store_note()
	{		
		$vh_id=$this->input->get('vh_id');
		$mst_id=$this->input->get('mst_id');
		$data=array('st_note_status'=>$vh_id,'completed_by'=>$this->session->userdata('ss_user_id'),'completed_date_tiime'=>date("Y-m-d H:i:s"));
		$result=$this->Hw_Delivery_Model->update_master($mst_id,$data);	

		if($result==true)
		{
		$st = array('error'=>0,'status' =>1,'validation'=>"Done",'delivery_id'=>$vh_id);	
		}
		else
		{
		$st = array('error'=>1,'status' =>0,'validation'=>"Error ! Data not saved",'delivery_id'=>$vh_id);		
		}
           echo json_encode($st);
	}
	
	public function update_issue_qty()
	{		
		$iq=$this->input->get('iq');
		$mst_id=$this->input->get('mst_id');
		$veh_id=$this->input->get('veh_id');
		
		$data=array('issue_qty'=>$iq);
		$result=$this->Hw_Delivery_Model->update_vehicle($mst_id,$data);	
	
		if($result==true){//echo "sss";
    		$data=array(
    		    'stn_item_id'=>$mst_id,
    		    'issue_qty'=>$iq,
    		    'issued_date'=> date("Y-m-d H:i:s"),
    		    'veh_id'=> $veh_id,
    		    'issued_by'=> $this->session->userdata('ss_user_id')
    		    );
    		$result=$this->Hw_Delivery_Model->issued_items($data);
		}
		
		if($result==true)
		{
		    $st = array('error'=>0,'status' =>1,'validation'=>"Done",'issue_qty'=>$iq);	
		}
		else
		{
		    $st = array('error'=>1,'status' =>0,'validation'=>"Error ! Data not saved",'issue_qty'=>$iq);		
		}
           echo json_encode($st);
	}

	
	
	
	function hw_list(){
			
		$data['main_menu_name'] = "hw";
		$data['sub_menu_name'] = 'list_2';
        $this->load->view('store_delivery/hw_list',$data);
	
	}
	
	 function list_sales()
	{		
	$requestData= $_REQUEST;	
	$start=$this->input->get('start');
	$length=$this->input->get('length');
	$search=$this->input->get('search');	
	$direct_sale=$this->input->get('direct_sale');	
	$data = array();	
	$count = $this->pos_model->get_all_sales("","",$search,$direct_sale,1);
	$totalData =$count ;
	$sales = $this->pos_model->get_all_sales($start,$length,$search,$direct_sale,1);
	$totalFiltered = $totalData; 	
	foreach ($sales as $row){
		$nestedData=array(); 
		$sale_id=$row['sale_id'];
		
		$store = $this->Hw_Delivery_Model->get_all_store_note($sale_id);
		
		$table_head="<small><table><tbody>";
		$table_boddy="";
		$table_footer="</tbody></table></small>";
		$table_row="";
		$completed_status=1;
		foreach($store as $st){
		    $com_st=" <samll> <span class='btn btn-xs btn-warning'>Pending</span></small>";
		    if($st['st_note_status']==1){
		          $com_st=" <samll> <span class='btn btn-xs btn-success'>Completed</span></small>";
		    }else{
		        $completed_status=0;
		    }
		  $table_row="<tr>"."<td>".$st['stn_code']."</td>" ."<td>".$com_st."</td>" ." </tr>";
		 $table_boddy.=   $table_row;
		}
		$name=$row['cus_name'];
		$address=$row['cus_address'];
		
		 if($row['cus_id']==1){
	    	     $name= $name."<br>".$row['cash_cus_name'];
	    	     $address= $address." ".$row['address_input'];
	    	    }
		$nestedData[] =display_date_time_format($row['sale_datetime']);
		$nestedData[] = $row['sale_reference_no']." / ".$row['card_ref_number'];
		$nestedData[] = $name;
		$nestedData[] = $address;
		$nestedData[] = $row['cus_phone'];	
		$nestedData[] = $table_head.$table_boddy.$table_footer;
		$nestedData[] = "";
		
		if($row['cus_issue_status']==1){
		    //$nestedData[] = "<samll><span class='btn btn-xs btn-success'>Completed</span></small>";  
		}else{
		    //$completed_status=0;
		   //$nestedData[] = "<small><span class='btn btn-xs btn-warning'>Pending</span></small>";    
		}
		
		if($completed_status==0){
		     $nestedData[] = "<small><span class='btn btn-xs btn-warning'>Pending</span></small>";    
		   // $nestedData[] = "<samll><span class='btn btn-xs btn-success'>Completed</span></small>";  
		}else{
		   
		   $nestedData[] = "<small><span class='btn btn-xs btn-warning'>Pending</span></small>";    
		}
	
	
			
		$actionTxtDisble='';
		$actionTxtEnable='';
		$actionTxtUpdate='';
		$actionTxtDelete='';
		$actionTxtViewDetails='<a target="_blank" href="'.base_url().'hw_delivery/view/'.$sale_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  target="_blank" href="'.base_url().'hw_delivery/sale_view/'.$sale_id.'"><i class="fa fa-file-text-o"></i> Sale Details</a></li>
                            
                            </ul></div>'; 
	
		$data[] = $nestedData;
	}

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

	echo json_encode($json_data); 
	}
	
		function sale_view()
	{
        $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name']  = '';
        $sale_id                = $this->uri->segment('3');
        $data['sale_details']       = $this->pos_model->get_sale_info($sale_id);
        $data['store_note_list']       = $this->Hw_Delivery_Model->get_all_store_note($sale_id);
        $data['vehicle_list']=$this->Marketing_Delivery_Model->get_all_vehicle();
        $this->load->view('store_delivery/store_noe_bind', $data);
   
    }	 
    
    function print_delivary_sheet()
	{	
	    //$data['main_menu_name'] = "marketing_delivery_process_1";
		//$data['sub_menu_name'] = "loading_approved_list";
	//	$this->load->model('Sales_Model');
		$this->load->model('Pos_Model');
		$vehicle_id =$this->uri->segment('3');		
		//$data['delivary_details'] = $this->Hw_Delivery_Model->get_delivary_details($vehicle_id);
		
		$data['delivary_stns'] = $this->Hw_Delivery_Model->get_delivary_stns($vehicle_id);
		//print_r($data['delivary_stns']);
		//exit();
	    $data['warehouse_details']= $this->Warehouse_Model->get_warehouse_info(4);	
           
		$this->load->view('delivery/hw_print_delivery_sheet_customer_wise',$data);	
	}
	

	



	
	
	
	
	
	



}