<?php
error_reporting(0);
class Worker extends CI_Controller {

public function __construct(){

        parent::__construct();
    $this->load->model('page_model');
  			$this->load->helper('url');
  	 		$this->load->model('user_model');
        $this->load->library('session');
    $this->load->model('department_model');
    $this->load->helper(array('form', 'url'));
$this->load->library('table');

 $this->load->library('pagination');
    
}

public function index()
{
  $this->load->library('table');

 $this->load->library('pagination');

 $this->load->helper('form');

 $this->load->helper('url');  

}
public function activepagination(){
   
     $b_type=$this->department_model->get_buisness_type();
         $tabid=$this->input->post('workertabid');
        $page=$this->input->post('workerpage');
        $pagination=$this->db->query("select * from pagination where page='$page' and buisness_id='$b_type' ");
          foreach ($pagination->result() as $key => $value)
              $value=$value->page;
        $data=array('page'=>$page,
                   'active'=>$tabid,
                   'buisness_id'=>$b_type,);
        if($value){
           
            $this -> db -> where('page', $page );
        $this -> db -> update('pagination',$data);
        }
        else{
           $this -> db ->insert('pagination',$data); 
        }
    $result=array('success'=>$tabid);
    echo json_encode($result);

}
       public function totalexpense(){
        $filtervalue=$this->input->post('uristring');
        
        $filter=$this->department_model->expensefilter($filtervalue); 
        echo json_encode($filter);
    }
public function totalpay(){
     $filtervalue=$this->input->post('uristring');
        
        $filter=$this->department_model->workerfilter($filtervalue); 
        echo json_encode($filter);
}
public function home(){
    $b_type=$this->department_model->get_buisness_type();
                      $data['cash']=0.0;
                          $data['cheque']=0.0;
                          $data['salary']=0.0;
    


//$this->load->database(); //load library database
  $active = $this->uri->segment(3);
        $filterac=$this->input->post('tabsearchpagi');
       
      
        if($filterac){
          $active=$filterac;  
        }
       
        if(!$active and  !$filterac){
            $active=0;
            
        }
    
        $getTotalworker = $this->page_model->getTotalworker($active);
        $data['cash']=0.0;
                          $data['tr']=0.0;
                          $data['card']=0.0;
                          $data['total']=0.0;
    
  
		
      
         $limit=$this->db->query("select page_limit from page_limit where page='whome' and buisness_id='$b_type' ");
          foreach ($limit->result() as $key => $value)
              $value=$value->page_limit;
          
        if($value){
          $perPage = $value;  
        }
        else{
          $perPage = 2;  
        }
		
		 $page = ($this->uri->segment(4)) ? ($this->uri->segment(4) - 1) : 0;
					 $data['tabpagi'] = $active;
		//$data["page_results"] = $this->page_model->getCustomersByLimit($perPage, $page*$perPage);
		
		
		
		//Basic configuration 
		    $config['base_url'] = base_url().'worker/home/'.$active;
		$config['total_rows'] = $getTotalworker;
		$config['per_page'] = $perPage;
		$config['uri_segment'] = 4;
		$config['use_page_numbers'] = true;
		
		
		//Pagination Container tag
		$config['full_tag_open'] = '<div style="margin:50px 30%;">';
		$config['full_tag_close'] = '</div>';
		
		//First and last Link Text
		$config['first_link'] = 'First';
		$config['last_link'] = 'Last';
		
		//First tag 
		$config['first_tag_open'] = '<span class="pagination-first-tag">';
		$config['first_tag_close'] = '</span>';
		
		//Last tag 
		$config['last_tag_open'] = '<span class="pagination-last-tag">';
		$config['last_tag_close'] = '</span>';
		
		
		//Next and Prev Link
		$config['next_link'] = 'Next';
		$config['prev_link'] = 'Prev';
		
		
		//Next and Prev Link Styling
		$config['next_tag_open'] = '<span class="pagination-next-tag">';
		$config['next_tag_close'] = '</span>';
		
		$config['prev_tag_open'] = '<span class="pagination-prev-tag">';
		$config['prev_tag_close'] = '</span>';
		
		
		//Current page tag
		$config['cur_tag_open'] = '<strong class="pagination-current-tag">';
		$config['cur_tag_close'] = '</strong>';

		
		$config['num_tag_open'] = '<span class="pagination-number">';
		$config['num_tag_close'] = '</span>';



		$this->pagination->initialize($config);
	
		


                 
 
        
     
         $data['limit']=$perPage;
          $data['start']=$page*$perPage;
               
       $data['tabtype'] = $this->department_model->get_workertab_type($active);
        
       $data["pagination_link"] =  $this->pagination->create_links();
  
      $data['tabsearch']=$this->input->post('tabsearch');
    
        //call the model function to get the department data
        $data['deptlist'] = $this->department_model->get_worker_list($perPage, $page*$perPage,$active);           
        $data['banklist'] = $this->department_model->get_bank_list();
    
      
     
         $data['from']=$this->input->post('from');
        $data['cashbank']=$this->department_model->cash_bank();
        $data['to']=$this->input->post('to');
   $data['fromsearch']=$this->input->post('from');
     $data['tosearch']=$this->input->post('to');
        $this->load->view('worker',$data); 
      }
    public function pagelimit(){
        $b_type=$this->department_model->get_buisness_type();
       $page= $this->input->post('limit');
        $limit=$this->db->query("select * from page_limit where page='$page' and buisness_id='$b_type'");
          foreach ($limit->result() as $key => $value)
              $pagelimit=$value->page_limit;
        $result=array('limit'=>$pagelimit);
        echo json_encode($result);
    }
    public function value(){
             $bankrowvalue=$this->department_model->bank_row();
             $data_bank =  $this->input->post('select_bank[]');
             $data1_card = $this->input->post('bank_card[]');
             $workername=$this->department_model->get_workertab_type($this->input->post('select'));
             $data='';
             $b_type=$this->department_model->get_buisness_type();
             $bankcount=$this->department_model->get_bankcount();    
              $value=array();
       
          foreach ($bankcount as $key => $bank){
               
         $value [$data_bank[$bank->id]]=$data1_card[$bank->id];
         }
        $a=(serialize($value));
           $id_post='';
         $p_edit=$this->input->post('edit_id');
        if($p_edit){
        $name=$this->input->post('select');
           
        $data = array(
            
 'user_id' =>$this->session->userdata('user_id'),
   'date' => $this->input->post('date'),
   'month' => $this->input->post('month'),
   'name' => $this->input->post('select'),
            'bank_info'=>$a,
   //'cash_pay' => $this->input->post('bank'),
  // 'cheque_pay' => $this->input->post('ticket'),
    'salary' => $this->input->post('total'),
     'buisness_type' => $b_type,
    'status' => 1,
  );
            
           
          foreach ($data1_card as $bank_id => $bank_value){
               
     if ($bank_value >0 ){
        /*$this->db->set('record',1,false);
        $this -> db -> where('id', $bank_id );
        $this -> db -> update('bank_name'); 
           */
         
       
               
              $purchaseall_detail[] =array(
                  
                   'worker_id' =>$p_edit,
                   'sale_type' =>'bank',
                   'bank_id'=>$bank_id,
                   'pur_amount'=>$bank_value,
                  'date'=>$this->input->post('date'),
                  'type'=>$workername,
                   'status'=>1,
                  'buisness_id'=>$b_type,
               );
          
      
            
          
          
         }
          $this -> db -> where('worker_id', $p_edit);
        $this -> db -> delete('sale_purchases');    
            
        }     
            
        
         $this -> db -> where('id', $p_edit);
        $this -> db -> update('worker',$data);
            if($purchaseall_detail){
              $this -> db -> insert_batch('sale_purchases', $purchaseall_detail);  
            }
            
           
            
          $url = 'worker/home';
       echo'
           <script>
          window.location.href = "'.base_url().'/'.$url.'";
           </script>';
        //redirect('worker/home', 'refresh');
        
    }
      else{  
      $name=$this->input->post('select');
            
        $data = array(
  'user_id' =>$this->session->userdata('user_id'),
   'date' => $this->input->post('date'),
   'month' => $this->input->post('month'),
   'name' => $this->input->post('select'),
    'bank_info'=>$a,
   //'cash_pay' => $this->input->post('bank'),
   //'cheque_pay' => $this->input->post('ticket'),
    'salary' => $this->input->post('total'),
     'status' => 1,
     'buisness_type' => $b_type,
  ); 
       
       
    $this->db->insert('worker', $data);
          
          
   $pur_id=$this->db->insert_id();
    foreach ($data1_card as $bank_id => $bank_value){
               
     if ($bank_value >0 ){
        $this->db->set('record',1,false);
        $this -> db -> where('id', $bank_id );
        $this -> db -> update('bank_name'); 
          
         
        
               
              $purchaseall_detail =array(
                   'worker_id' =>$pur_id,
                   'purchases_type' =>'bank',
                  'date'=>$this->input->post('date'),
                  'type'=>$workername,
                   'bank_id'=>$bank_id,
                   'pur_amount'=>$bank_value,
                   'status'=>1,
                  'buisness_id'=>$b_type,
               );
          
        
          
         
           $this->db->insert('sale_purchases',$purchaseall_detail); 
          
         }
             
            
        }
          
          
          
        }
        redirect('worker/home', 'refresh');
    }
       
 
   
   public function delete(){
        $id_post = $this->input->post('uristring'); 
       
       
     
        if($id_post){
             $this->db->set('status',0,false);
        $this -> db -> where('id', $id_post);
        $this -> db -> update('worker');
            
           
       
                 $this->db->set('status',0,false);
        $this -> db -> where('worker_id', $id_post);
        $this -> db -> update('sale_purchases');
           
        }
         $b_type=$this->department_model->get_buisness_type();
       $limit='';
       $status=1;
        $type='';
            $cash=0.0;
            $cheque=0.0;
            $salary=0.0;
            
             $limit=$this->db->query("select page_limit from page_limit");
          foreach ($limit->result() as $key => $value)
            $value=$value->page_limit;  
        $limit=$value-1;
         $id_post = $this->input->post('uristring'); 
        
        $id_post1 = $this->input->post('type');
        
          $type=$this->db->query("select * from worker   where status=1 and buisness_type='$b_type'  order by id   limit ".$limit." ",false);
          foreach ($type->result() as $key => $value){
           
               $valuen=unserialize($value->bank_info);
              $cash+=$valuen[5];
              $cheque+=$value->salary-$valuen[5];
              $salary+=$value->salary;
              
              
          }
          $result = array(
            'cash' => $cash,
              'cheque' => $cheque,
              'salary' => $salary,
              
              'id'=>$id_post,
              
        );
     
   echo json_encode($result);
         
   }

      public function show(){
           $b_type=$this->department_model->get_buisness_type();
         $type='';
            $cash=0.0;
            $cheque=0.0;
            $salary=0.0;
            $total=0.0;
            
         $id_post = $this->input->post('uristring'); 
        
        $id_post1 = $this->input->post('type');
            $html='';
         $id_post = $this->input->post('uristring'); 
        
        $id_post1 = $this->input->post('type');
         $query = $this->db->query("SELECT * FROM bank_name where status=1 and record=1 and buisness_id='$b_type' ");
           $row= $query->num_rows();
         
        $bankname=$this->db->query("select * from bank_name  where   status=1 
                                     and record=1 and buisness_id='$b_type' ");
         $type=$this->db->query("select * from worker b  where b.id  = '$id_post' and buisness_type='$b_type' and status=1 ",false);
          $totalbank=0.0;
          foreach ($type->result() as $key => $value){
           
              $month=$value->month;
              $name=$value->name;
              $cash=$value->cash_pay;
              $cheque=$value->cheque_pay;
              $salary=$value->salary;
               $date= date("Y-m-d",strtotime($value->date));
            
            $bank_card=(unserialize($value->bank_info));
             
                                    foreach ($bankname->result() as $key => $bank){
                                        
                        
                                     if(!isset($bank_card[$bank->id])) {
                                       $bank_card[$bank->id] = 0;
                                         }
                                           
                                     
                                      $totalbank+=$bank_card[$bank->id];
                                            if($bank_card[$bank->id]){
                                                $chk="checked";
                                               $style="";
                                            }
                                            else{
                                              $chk="";
                                                $style="display:none";
                                                
                                            }
                                     $html.= '<div id="checkboxdivedit"  class="checkboxdivedit" style="display:inline-block;vertical-align:top;max-width:100px"><input  type="checkbox" id="myCheck'.$bank->id.'" name="select_bank['.$bank->id.']" onclick="pbankFunction('.$bank->id.')" value="'.$bank->id.'" '.$chk.'>'.$bank->type.' </br>
                                        <input id="text'.$bank->id.'"  style="'.$style.'" type="text" name="bank_card['.$bank->id.']" class="banktext paybankclass" value="'.$bank_card[$bank->id].'"  onKeyPress="workercalculate()" onKeyUp="workercalculate()""></br></div>
                                    ';
                                              
                                      
                                       
        
                                  
                                        
                                    }
             
            
              
          }
        
    /*  $query = $this->db->query("SELECT * FROM worker_name where status=1  and buisness_type='$b_type' and name='$name'");
           $typeid= $query->result();
        foreach($typeid as $name);
          $workerid=$name->id;*/
        
      
        
          
          $result = array(
              'date'=>$date,
              'name' => $name,
            'month' => $month,
             'salary'=>$salary,
              'html'=>$html,
              'id'=>$id_post,
              'workerid'=>$workerid,
        );
     
   echo json_encode($result);
         
       
    }
        public function newcheck(){
            $b_type= $this->department_model->get_buisness_type();
            $date=date("Y-m-d");
         $type=$this->db->query("select * from bank_name  where  status=1 and buisness_id='$b_type'
                                      ");
                 $html='';
                     foreach ($type->result() as $key => $type){
                      
                   $html.='<div id="checkboxdiv" style="display:inline-block;vertical-align:top;max-width:100px"><input  type="checkbox" id="payCheck'.$type->id.'" name="select_bank['.$type->id.']" onclick="paybankFunction('.$type->id.')" value="'.$type->id.'"  >'.$type->type.' </br>
                   
                      <input id="paytext'.$type->id.'" type="text" name="bank_card['.$type->id.']" class="banktext paybankclass" style="display:none; width:100%" value="" onKeyPress="workeralculate()" onKeyUp="workercalculate()"></br>
                       <input id="paybank'.$type->id.'" type="hidden"  class="banktext class" style="display:none; width:100%" value="" ></br></div>  <input type="hidden" id="bankvalue'.$type->id.'">';
                      }
        $result=array(
            'date'=>$date,
        'html'=>$html,
        );
         echo json_encode($result);
    }
    
    public function trash(){

 $this->load->database(); //load library database

 $b_type=$this->department_model->get_buisness_type();
                          $data['cash']=0.0;
                          $data['tr']=0.0;
                          $data['card']=0.0;
                          $data['total']=0.0;
    
         //load library database
        $active=$this->uri->segment(3);
        $getTotalwholesale = $this->page_model->getTotalworkertrash($active);
        $data['cash']=0.0;
                          $data['tr']=0.0;
                          $data['card']=0.0;
                          $data['total']=0.0;
    
  
		
      
         $pagelimited=$this->db->query("select page_limit from page_limit where page='whome' and buisness_id='$b_type' ");
          foreach ($pagelimited->result() as $key => $pagevalue)
              $value=$pagevalue->page_limit;
          
        if($value){
          $perPage = $value;  
        }
        else{
          $perPage = 20;  
        }
		
		 $page = ($this->uri->segment(4)) ? ($this->uri->segment(4) - 1) : 0;
					 $data['tabpagi'] = $active;
		//$data["page_results"] = $this->page_model->getCustomersByLimit($perPage, $page*$perPage);
		
		
		
		//Basic configuration 
		    $config['base_url'] = base_url().'worker/trash/'.$sctive;
		$config['total_rows'] = $getTotalwholesale;
		$config['per_page'] = $perPage;
		$config['uri_segment'] = 4;
		$config['use_page_numbers'] = true;
		
		
		

		$this->pagination->initialize($config);
	

         $data['limit']=$perPage;
          $data['start']=$page*$perPage;
      
        //call the model function to get the department data
        $data['deptlist'] = $this->department_model->get_workertrash($perPage, $page*$perPage,$active);           
        $data['tabsearch']=$active;
        $data['pagination'] = $this->pagination->create_links();
         $data['from']='';
        $data['to']='';
        $data['from']=$this->input->post('from');
        $data['cashbank']=$this->department_model->cash_bank();
        $data['to']=$this->input->post('to');
        
       $this->load->view('worker_trash',$data);
    }
     public function trash_delete(){
           $id_post = $this->input->post('uristring'); 
       
         
    
        if($id_post){
              $this -> db -> where('id', $id_post);
        $this -> db -> delete('worker');
        
            
            
        $this -> db -> where('worker_id', $id_post);
        $this -> db -> delete('sale_purchases');
            
            
        }
      }
    public function tashalldelete(){
           $id_post = $this->input->post('uristring'); 
        $this -> db -> where('id', $id_post);
        $this -> db -> delete('worker');
      }
    public function name_delete(){
           $id_post = $this->input->post('uristring'); 
        $this->db->set('status',0,false);
        $this -> db -> where('id', $id_post);
        $this -> db -> update('worker_name');
      }
    public function trash_recover(){
          $id_post = $this->input->post('uristring'); 
      
        if($id_post){
             $this->db->set('status',1,false);
        $this -> db -> where('id', $id_post);
        $this -> db -> update('worker');
        
            
            
                 $this->db->set('status',1,false);
        $this -> db -> where('worker_id', $id_post);
        $this -> db -> update('sale_purchases');
          
            
        }
    }
    
    public function name(){
 

 $this->load->database(); //load library database


//$num_rows=$this->db->count_all("users");

 $config['base_url'] = base_url().'worker/name';
        $b_type='';
         $bus_type=$this->db->query("select * from buisness_type  
           ");
          
        $row = $bus_type->row();
      $b_type= $this->department_model->get_buisness_type();
       $query = $this->db->query("SELECT * FROM sale_type where status=1 and buisness_type='$b_type' ");
       $row= $query->num_rows();
      $config['total_rows'] = ($row+$row);

 //$config['total_rows'] = $num_rows;

 //$config['per_page'] = 2;

 //['num_links'] = $num_rows;

 $config['use_page_numbers'] = TRUE;

 $config['full_tag_open'] = '<ul class="pagination">';

 $config['full_tag_close'] = '</ul>';

 $config['prev_link'] = '&laquo;';

 $config['prev_tag_open'] = '<li>';

 $config['prev_tag_close'] = '</li>';

 $config['next_tag_open'] = '<li>';

 $config['next_tag_close'] = '</li>';

 $config['cur_tag_open'] = '<li class="active"><a href="#">';

 $config['cur_tag_close'] = '</a></li>';

 $config['num_tag_open'] = '<li>';

 $config['num_tag_close'] = '</li>';

$config['next_link'] = '&raquo;';
        //pagination settings
        $seting=$this->input->post('seting');
       
        $config['base_url'] = site_url('worker/name');
       
       
        $limit=$this->db->query("select page_limit from page_limit");
          foreach ($limit->result() as $key => $value)
              $value=$value->page_limit;
          
        if($value){
          $config['per_page'] = $value;  
        }
        else{
           $config['per_page'] = "3";    
        }
         
       
        
        $config["uri_segment"] = 3;
        $choice = $config["total_rows"] / $config["per_page"];
        $config["num_links"] = floor($choice);

        //config for bootstrap pagination class integration
       
        $this->pagination->initialize($config);
        $data['page'] = ($this->uri->segment(3)) ? $this->uri->segment(3) :0;

        //call the model function to get the department data
        $data['deptlist'] = $this->department_model->get_worker_name($config["per_page"], $data['page']);           

        $data['pagination'] = $this->pagination->create_links();

        $this->load->view('worker_name',$data);
    }
    
    
    public function name_value(){
        $b_type= $this->department_model->get_buisness_type();
         $name=$this->input->post('cat');
          $data = array(
  'user_id' =>$this->session->userdata('user_id'),
   
   'name' =>$name,
              
              'buisness_type'=>$b_type,
              'status' =>1,
  
       );
          $value='';
          $p_type=$this->db->query("select * from worker_name where name='$name' and buisness_type='$b_type' and status=1 ");
          foreach ($p_type->result() as $key => $value)
              $value=$value->name;
          if($value){
            // $data['exit']=1;
          //redirect('worker/name', $data,'refresh');
             echo '<script>swal("Name Already exit!");</script>';
   
        }   
        else{
          $this->db->insert('worker_name', $data);  
        }
         
        
       
        redirect('worker/name', 'refresh');
    }
    
    
    public function wn_edit(){
       $b_type= $this->department_model->get_buisness_type();
       
        $name='';
           
            
            
         $id_post = $this->input->post('uristring'); 
        
       // $id_post1 = $this->input->post('type');
        
          $type=$this->db->query("select * from worker_name b  where b.id  = '$id_post' and buisness_type='$b_type' ",false);
          foreach ($type->result() as $key => $value){
              $name=$value->name;
              
              
              
          }
          $result = array(
              'name' => $name,
           
              
              'id'=>$id_post,
              
        );
     
   echo json_encode($result);
        
    }
     public function name_update(){
         $id=$this->input->post('workerid');
         $data = array(
              'name' => $this->input->post('name'),   
              
        );
     $prename=  $this->page_model->get_preworkername($id);
         $this -> db -> where('id', $id);
        $this -> db -> update('worker_name',$data);
        redirect('worker/name', 'refresh'); 
         
     }
    
    
      }  
        

?>
