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

class Dashboard_Controller extends CI_Controller {
    
    var $b_url = 'https://neha.webip.lk/lumen/public';

        public function __construct() {
        parent::__construct();
        $this->load->model('Product_model');
        $this->load->model('General');
        $this->load->model('Common_model');
            if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('login'); 
                        exit();
            }
         
        }
        
        function index(){
            
            
            
            
            $rest_api_base_url = 'https://neha.webip.lk/lumen/public';
        
        
        
         $product = $this->Common_model->productcount();
         $data['item'] = $product;
         
         $product = $this->Common_model->getsalseman();
         $data['rep'] = $product;
         
         $product = $this->Common_model->getcus();
         $data['cus'] = $product;
         
         $product = $this->Common_model->getdepo();
         $data['depo'] = $product;
         
         $product = $this->Common_model->todayinv();
         $data['invtot'] = $product;
         
         $product = $this->Common_model->todayinvcount();
         $data['invtotqty'] = $product;
         
         $product = $this->Common_model->todaypay();
         $data['paytot'] = $product;
         
         $product = $this->Common_model->todaypaycount();
         $data['payqty'] = $product;
         
         $product = $this->Common_model->todayreturncount();
         $data['trnqty'] = $product;
         
         $product = $this->Common_model->todayreturn();
         $data['rtntot'] = $product;
         
         $product = $this->Common_model->todoauthinv();
         $data['todoinv'] = $product;
         
         $product = $this->Common_model->todoauthgrn();
         $data['todogrn'] = $product;
         
         $product = $this->Common_model->todoinvtrn();
         $data['todoinvrtn'] = $product;
         
         
         $product = $this->Common_model->todopay();
         $data['todopay'] = $product;
         
         $product = $this->Common_model->todotrnf();
         $data['todotrnf'] = $product;
         
         $product = $this->Common_model->todaymoq();
         $data['todaymoq'] = $product;
         
         $product = $this->Common_model->todayexp();
         $data['todayexp'] = $product;
         
         $product = $this->Common_model->todaybin();
         $data['todaybin'] = $product;
         
         
         $product = $this->Common_model->todoauthcus();
         $data['todocus'] = $product;
         
         
         
         
        
        
        if(isset($this->session->userdata['login_data']['username'])){
            
            if(($this->session->userdata['login_data']['userlevel']<6)){
                
                    $data['content'] = ('pages/dashboard');                       
                    $this->load->view('template',$data);
            }elseif(($this->session->userdata['login_data']['userlevel']==15)){
                
                    $data['content'] = ('pages/dashboard');                       
                    $this->load->view('template',$data);
            }else if($this->session->userdata['login_data']['userlevel']==9){
                
                $data['content'] = "pages/stodashboard";        
                        $this->load->view('template',$data);
            }else if($this->session->userdata['login_data']['userlevel']<21){
                
                $data['content'] = "pages/refdashboard";        
                $this->load->view('template',$data);
            }
	                
	    }else{
	        $er['ss'] = "";
		        $data['content'] = ('login');                       
                $this->load->view('login',$er);
	    }
            
            
            
        }
        
        function stodash(){
            
        $data['content'] = ('pages/stodashboard');                       
        $this->load->view('template',$data);
        }
        
         function refdash(){
            
        $data['content'] = ('pages/refdashboard');                       
        $this->load->view('template',$data);
        }
        
        
        function show_all_qtn() {
        if(!isset($this->session->userdata['login_data']['username'])) { 
                
                        redirect('login'); 
                        exit();
            }
            $this->db->select('mes_qto.*,mes_customer.*,mes_currancy.*,mes_country.*,mes_user.*');
            $this->db->from('mes_qto');
            $this->db->join( "mes_customer", "mes_customer.cust_id = mes_qto.po_sup_id", 'left');
            $this->db->join( "mes_currancy", "mes_currancy.curr_id = mes_qto.po_currancy", 'left');
            $this->db->join( "mes_country", "mes_country.cnty_id = mes_customer.cust_country", 'left');
            $this->db->join('mes_user', 'mes_qto.po_ref_id   = mes_user.user_id','left');
            $this->db->order_by("mes_qto.po_id","desc");
            $this->db->limit(8);
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            //$data['content'] = ('pages/allinvoice');                       
            //$this->load->view('template',$data);
            
            $link = site_url('qto');
            
            if($this->session->userdata['login_data']['userlevel'] != 9){
                
                $det[0]="<a href='$link'><button type='button' class='btn btn-outline-danger' >Create New Invoice</button></a></br>";
            }
            
             
            //$det[0]="";
            $det[1] ="<tr>
														
														<th>INV Number</th>
														<th>Customer</th>
														<th>Item</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                    
                    if($this->session->userdata['login_data']['userlevel'] != 9){
                        $vw_btn1 = " <a href ='".base_url()."authorize_inv/".$data[$i]->po_id."'> <button type='button' class='btn btn-outline-secondary btn-sm' >Authorize</button></a>";
                    }else{$vw_btn1 ="";}
                    
                   
                    $vw_btn2 = "<a href ='".base_url()."inv_view_pdf/?id=".$data[$i]->uniq_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>
                                ";
                        
                    if($data[$i]->po_auth==1 ){
                        if($data[$i]->po_status==1){
                        $sts = "<td style='color:#00800085; font-size:25px; text-align: center;'>&#9679;</td>"; $vw_btn = $vw_btn2;
                        }else{
                        $sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";  $vw_btn = $vw_btn2;}
                        
                    }else{
                        if($data[$i]->po_status==1){
                        $sts = "<td style='color:#c6ab1b85; font-size:25px; text-align: center;'>&#9679;</td>"; $vw_btn = $vw_btn1;
                        }else{
                        $sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";  $vw_btn = $vw_btn1;}
                        
                    }
                    if($data[$i]->po_recieved==1){
                        $rec = 'Invoiced';
                        }else{
                        $rec = 'No invoice';}
                        
                    
                    $x = $i+1;
                    $dat =  "
                                 <tr scope='row'>
                                 <td >".$data[$i]->po_code.$data[$i]->po_id."</td>
                                 
                               
                                 <td>".$data[$i]->cust_company."<br>".$data[$i]->cust_addres."<br><b>".$data[$i]->user_fname." ".$data[$i]->user_lname."</b></td>
                                 <td style='text-align: center;'>".($data[$i]->po_tot_items)."</td>
                                
                                
                                
                                <td>$vw_btn   </td>
                               
                              
                              </tr>";
                              $x++;
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
            //echo json_encode($data);
        }
}