<?php

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

class Public_Controller extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('General');
        $this->load->model('Product_model');
        }

        
         public function index(){
             
            $data['content'] = "pages/allgrn";        
            $this->load->view('template',$data);  
             
             
         }
         
         
         function showallgrn(){
             
             
            $this->db->select('*');
            $this->db->from('mes_grn');
           // $this->db->join( "mes_suppliers", "mes_suppliers.sup_id = mes_po.po_sup_id", 'inner');
           // $this->db->join( "mes_currancy", "mes_currancy.curr_id = mes_po.po_currancy", 'inner');
            //$this->db->join( "mes_country", "mes_country.cnty_id = mes_suppliers.sup_country", 'inner');
            $this->db->order_by("grn_id","desc");
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            //$data['content'] = ('pages/allinvoice');                       
            //$this->load->view('template',$data);
            
            $link = site_url('new_grn');
           
             $det[0]="<a href='$link'><button type='button' class='btn btn-outline-danger' >Create New GRN</button></a></br>";
            
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														
														<th>GRN Number</th>
														<th>PO ID</th>
														<th>Invoice ID</th>
														<th>DO</th>
														<th>Qty</th>
														<th>Date</th>
													    <th>Status</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                    
                   $vw_btn1 = "<a href ='".base_url()."Public_Controller/view_pdf/".$data[$i]->grn_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_supplier/".$data[$i]->grn_id."' name='edtbutton' onclick='edit_view(this.id)' data-target='#Modal_edit'>Cancel</button>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->grn_id."'  onclick='po_proceed(this.id)' >Process</button>
                                ";
                    $vw_btn2 = "<a href ='".base_url()."Po_Controller/view_pdf/".$data[$i]->grn_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>
                                
                                ";
                        
                    if($data[$i]->grn_proceed==1){
                        if($data[$i]->grn_stetus==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]->grn_stetus==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_btn2;}
                        
                        
                    }
                    
                    
                    $x = $i+1;
                    $dat =  "
                                 <tr scope='row'>
                                 <td >".$x."</td>
                                 <td >".$data[$i]->grn_code.$data[$i]->grn_id."</td>
                                 <td>".$data[$i]->grn_poid."</td>
                                 <td>".$data[$i]->grn_invnum."</td>
                                 <td>".$data[$i]->grn_donum."</td>
                                 <td>".$data[$i]->grn_totqty."</td>
                                <td style='text-align: right;'>".$data[$i]->grn_cdate."</td>
                                $sts
                                <td>$vw_btn   </td>
                               
                              
                              </tr>";
                              $x++;
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
            //echo json_encode($data);
        }
        
        function view_pdf($insert_id){
            
        $this->db->select("*");    
        $this->db->from('mes_grn');
        $this->db->where('grn_id', $insert_id );
        //$this->db->join('mes_po', 'mes_po.po_id  = mes_grn.grn_poid');
        //$this->db->join('mes_suppliers', 'mes_po.po_sup_id  = mes_suppliers.sup_id');
        //$this->db->join('mes_currancy', 'mes_po.po_currancy   = mes_currancy.curr_id');
        //$this->db->join('mes_country', 'mes_suppliers.sup_country   = mes_country.cnty_id');
        $query = $this->db->get();
        $data['inv']=$query->result();
        
        $this->db->select("*");    
        $this->db->from('mes_inventry');
        $this->db->where('invt_type',"GRN");
        $this->db->where('invt_type_id',$insert_id);
        $this->db->join( "mes_item", "mes_item.item_id = mes_inventry.invt_itemid", 'inner');
        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'inner');
        $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'inner');
        $this->db->join( "mes_model", "mes_model.model_id = mes_item.item_model", 'inner');
        $this->db->join( "mes_unit", "mes_unit.unit_id = mes_item.item_unit", 'inner');
        $this->db->join( "item", "mes_item.item_name = item.itm_id");
        $this->db->join( "mes_spec", "mes_spec.spec_id = mes_item.item_spec", 'inner');
        $query = $this->db->get();
        $dataa = $query->result();
        
         for($i=0; $i<count($dataa); $i++){
                    if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->spec_spec!=""){$spece1 = "  ";}else{$spece1 = "";}
                   // if($dataa[$i]->part_no!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name.$spece1.$dataa[$i]->spec_spec;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->invt_qty); 
                
        }
        $data['inv_itm'] = $inv;
        
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
      
            
            $this->load->view('pages/grnnotepdf',$data);
            
        }
             
         
}