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

class Expenses_Controller extends CI_Controller {

        public function __construct() {
        parent::__construct();
        $this->load->model('Product_model');
        $this->load->model('General');
        $this->load->model('Common_model');
        $this->load->library('encryption');
        
          if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('login'); 
                        exit();
            }
         
        }
        
         function index(){
           
        $data['content'] = ('pages/allexpense'); 
		$this->load->view('template',$data);
             
         }
         
         
        function alexp(){
            
            $this->db->from('daily_exp');
            $this->db->select("*");
            $this->db->join( "mes_user", " mes_user.user_id = daily_exp.ref_id", 'left');
            $this->db->join( "mes_expreson", "mes_expreson.exp_id  = daily_exp.reason", 'left');
            
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            
            //$det[0]="<button type='button' class='btn btn-outline-danger' type='submit' id='new_customer' onclick='add_data(this.id)' data-toggle='modal' data-target='#addmodel'>Add New Customer</button></br>";
            $det[0]="";
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														<th>Rep</th>
														<th>Reason</th>
														<th style='text-align: right;'>Amount</th>
														<th style='text-align: right;'>Date</th>
														<th>Status</th>
														<th style='text-align: right;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
                for($i=0; $i<count($data); $i++){
                    
                   // if($data[$i]->cust_vat!=""){$tax="&#10003;";}else{$tax="";}
                    if($data[$i]->dexp_stetus==0){$sts = "<td style='color:Green; font-size:25px; text-align: center; width: 60px;'>&#9679;</td>";}else{$sts = "<td style='color:red; font-size:25px; text-align: center;'>&#9679;</td>";}
                   // if($data[$i]->cust_conperson2!=""){$con2 = "<br>".$data[$i]->cust_conperson2;}else{$con2="";}
                   // if($data[$i]->cust_deladd==1){$dadd ="<button type='button' class='btn btn-outline-secondary btn-sm' type='submit' id='view_cusaddress/".$data[$i]->cust_uniqid."' onclick='dadd_view(this.id)' data-toggle='modal' data-target='#showdadd'>Branch</button>";}
                   // else{$dadd = "Main Store";}
                    
                   // $city = $data[$i]->cust_city;
                   // $cusref = $data[$i]->cust_salseperson;
                   // $custid = $data[$i]->cust_id;
                   
                    
                  
                    
                    $dat =  "
                              <tr scope='row'><td >".$data[$i]->dexp_code.$data[$i]->dailyexp_id ."</td>
                               
                                <td>".$data[$i]->user_name."</td>
                                <td>".$data[$i]->exp_reson."</td>
                                
                                <!--td>".$data[$i]->user_fname." ".$data[$i]->user_lname."</td-->
                               
                                <td style='text-align: right;'>".$data[$i]->amount."</td>
                                <td style='text-align: right;'>".$data[$i]->dexp_date."</td>
                                $sts
                                
                                <td style='text-align: right;'>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_dexp/".$data[$i]->dailyexp_id."'  onclick='edit_view(this.id)' >Edit</button>
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
        function dexp_edt(){
                    
    	    $id = $this->input->post('id');
    	    
    	    $this->db->from('daily_exp'); 
    	    $this->db->join( "mes_expreson", "mes_expreson.exp_id  = daily_exp.reason", 'left');
            $this->db->select("*");
            $this->db->where('dailyexp_id ',$id);
            $detail = $this->db->get();
            $data = $detail->result();
            
            $this->db->from('mes_expreson');
            $this->db->select("*");
            $detailtax = $this->db->get();
            $data1 = $detailtax->result();
            
            $act = $inact = "";
            if($data[0]->dexp_stetus=="0"){$act = "selected";}else{$inact = "selected";}
    	    
            
    	    
              $det='
              <input type="hidden" name="data_id" id="data_id" class="form-control" readonly="" value="'.$data[0]->dailyexp_id .'">
               <input type="hidden" name="edit_table" id="edit_table" class="form-control" readonly="" value="update_dexp">
              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Reason</label>
                                <div class="col-md-10">
                                  <select class="form-control" id="eexp_re" name="eexp_re">';
                                  
                                  $det.='<option value="'.$data[0]->reason.'" selected>'.$data[0]->exp_reson.'</option>';
                                  
                                     foreach($data1 as $value) {
                                                $det.='<option value="'.$value->exp_id .'">'.$value->exp_reson.'</option>';
                                            }     
                                  
            $det.='                      
                                        </select>
                                 </div>
                            </div>
                            
                            <div class="form-group row">
                                <label class="col-md-2 col-form-label">Amount</label>
                                <div class="col-md-10">
                                
                                 <input type="text" name="edamount" id="edamount" class="form-control" placeholder="" value="'.$data[0]->amount.'">
                                 </div>
                            </div>
                            
                              <div class="form-group row">
                                <label class="col-md-2 col-form-label">Status</label>
                                <div class="col-md-10">
                                      <select class="form-control" id="status" name="status">
                                            <option value="0" '.$act.'>Active</option>
                                            <option value="1" '.$inact.'>Inactive</option>
                                        </select>
                                 </div>
                            </div>  
                
                            
                            
              ';
                        
                	
               echo json_encode($det);	
    	}
    	
    	function dexp_updt(){
            
                $this->form_validation->set_error_delimiters('', ''); 
                $this->form_validation->set_rules('eexp_re', 'Reason', 'required');
                $this->form_validation->set_rules('edamount', 'Amount', 'trim|required');
            $ret[0]="";    
                if ($this->form_validation->run() == FALSE){
                    $ret[0].= validation_errors();
                    $ret[2] = "error";
    	   }else{
    	       
                
    	    
    	       
    	      
                $this->db->set('reason', $this->input->post('eexp_re'));
                $this->db->set('amount', $this->input->post('edamount'));
                $this->db->set('dexp_stetus', $this->input->post('status'));
                $this->db->set('dexp_euser', $this->session->userdata['login_data']['user_id']);
                $this->db->set('dexp_edate', date('Y-m-d'));
                
                $this->db->where('dailyexp_id', $this->input->post('data_id'));
                $result=$this->db->update('daily_exp');
                
                if($result){
                    $ret[0].= "Expenses Details Updated";
                }else{
                    $ret[0].= "Please Re enter all details";
                }
    	   }
        
    	    $ret[1] = "explist";
    	    echo json_encode($ret);	
    	}
    	
    	function view_commi(){
    	    
    	 
    	     $data['rep_list'] = $this->Product_model->salserep_list();
    	     $data['diver_list'] = $this->Product_model->driver_list();
    	     $data['cleaner_list'] = $this->Product_model->cleaner_list();
    	    
    	    
    	    
    	$data['content'] = ('report/comission_view'); 
		$this->load->view('template',$data);
    	}
    	
    	
    	function pdf_commi(){
    	    
    	    // $rep = $this->input->post('ref');
    	    
    	    $this->db->select("*");    
            $this->db->from('mes_user');
            //$this->db->where('user_id', "0");
            $this->db->where('user_id', $this->input->post('ref'));
            $query = $this->db->get();
            $user_data=$query->result();
        
       
    	    
    	    $user_detail = array();
        
            $user_detail['frmdate']=$this->input->post('frm_date'); 
            $user_detail['todate']=$this->input->post('to_date');
            $user_detail['refname'] =$user_data[0]->user_name;
    	     
    	    $this->db->from('mes_payment_main');
            $this->db->select("*");
            $this->db->join( "mes_payment", " mes_payment_main.pay_id  = mes_payment.pay_main_id", 'left');
            $this->db->join( "mes_qto", "mes_payment.pay_invid  = mes_qto.po_id", 'left');
            $this->db->join('mes_user', 'mes_qto.po_ref_id   = mes_user.user_id','left');
            $this->db->join('mes_customer', 'mes_qto.po_sup_id   = mes_customer.cust_id','left');
            
            $user_detail['ref']=""; 
              
            if($this->input->post('ref')!=0){
                $user_detail['ref'] =$user_data[0]->user_name;
                $this->db->where('mes_qto.po_ref_id',$this->input->post('ref'));
            }
            
            if($this->input->post('frm_date')!=""){
                $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('mes_payment.pay_date>=', $this->input->post('frm_date'));
            }else{
                $user_detail['fdate'] = date('Y-m-d');
                $this->db->where('mes_payment.pay_date', date('Y-m-d'));
            }
            
            if($this->input->post('to_date')!=""){
                $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('mes_payment.pay_date<=', $this->input->post('to_date'));
            }else{
               $user_detail['todate'] = date('Y-m-d');
                 $this->db->where('mes_payment.pay_date', date('Y-m-d'));
            }
            
           //$this->db->where('mes_qto.po_payment',1);
            
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            
                      $inv=array();
                      $drivers=array();
                      $helpers=array();
                         for($i=0; $i<count($data); $i++){
                             
                             if($data[$i]->driver!="0"){
                                 $this->db->select("*");    
                                $this->db->from('mes_user');
                                $this->db->where('user_id',$data[$i]->driver);
                                $queryd = $this->db->get();
                                $datad = $queryd->result();
                                $driver = $datad[0]->user_name;
                                $drivers[] = array($datad[0]->user_name);
                             }else{
                                 $driver = "";
                             }
                             
                             if($data[$i]->driver!="0"){
                                 $this->db->select("*");    
                                $this->db->from('mes_user');
                                $this->db->where('user_id',$data[$i]->cleaner);
                                $queryc = $this->db->get();
                                $datac = $queryc->result();
                                $cleaner = $datac[0]->user_name;
                                $helpers[] = array($cleaner);
                             }else{
                                 $cleaner = "";
                             }   
                                
                                
                             
                                    //$data = $this->Product_model->single_product($dataa[$i]->invt_itemid);
                                   // $itm_code = $data[0]->cat_shortcode.'-'.$data[0]->brand_shordcode.'-'.$data[0]->item_catcount;
                                    
                                    
                            
                                        $inv[] = array($data[$i]->qto_code.$data[$i]->po_tax_id,$data[$i]->po_final_tot,$data[$i]->cust_company,$driver,$cleaner,$data[$i]->return_amnt);
                        
                             
                                    
                           
                       }        
            
    	     
    	     
    	    $this->db->from('daily_exp');
            $this->db->select("*");
            $this->db->join( "mes_expreson", " daily_exp.reason  = mes_expreson.exp_id", 'left');
            //$this->db->join( "mes_qto", "mes_payment.pay_invid  = mes_qto.po_id", 'left');
            
            $user_detail['ref']=""; 
              
            if($this->input->post('ref')!=0){
              //  $user_detail['ref'] =$user_data[0]->user_name;
                $this->db->where('ref_id',$this->input->post('ref'));
            }
            
            if($this->input->post('frm_date')!=""){
               // $user_detail['fdate'] = $this->input->post('frm_date');
                $this->db->where('dexp_date>=', $this->input->post('frm_date'));
            }else{
               // $user_detail['fdate'] = date('Y-m-d');
                $this->db->where('dexp_date', date('Y-m-d'));
            }
            
            if($this->input->post('to_date')!=""){
              // $user_detail['todate'] = $this->input->post('to_date');
                $this->db->where('dexp_date<=', $this->input->post('to_date'));
            }else{
               // $user_detail['todate'] = date('Y-m-d');
                 $this->db->where('dexp_date', date('Y-m-d'));
            }
            
            $this->db->where('dexp_stetus',0);
            
            $detailexp = $this->db->get();
            $dataexp = $detailexp->result();
            
            
           
      
                
                
        //get return
      $ret = array();
      
      for($h=0; $h<count($dataexp); $h++){
             
                
                
             
                    //$data = $this->Product_model->single_product($dataa[$i]->invt_itemid);
                   // $itm_code = $data[0]->cat_shortcode.'-'.$data[0]->brand_shordcode.'-'.$data[0]->item_catcount;
                    
                    
            
                        $ret[] = array($dataexp[$h]->dexp_code.$dataexp[$h]->dailyexp_id,$dataexp[$h]->exp_reson,$dataexp[$h]->dexp_date,$dataexp[$h]->amount);
        
             
                    
           
       }
        $data['ret_itm'] = $ret;
        
        $data['inv'] = $user_detail;
        $data['inv_itm'] = $inv;
       
        // company profile 
        $data['tm'][0] =$drivers;
        $data['tm'][2] =$helpers;
  
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
         
          $this->load->view('report/pdf_salses_commission',$data);
    	     
    	    
    	}
        
    
}     

        
        ?>