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

class Po_Controller extends CI_Controller {

        public function __construct() {
        parent::__construct();
        $this->load->model('Product_model');
        $this->load->model('General');
         
        }
        
        function index(){
            
            $columns_arr= array("*");            
            $where_arr = array('sup_status!='=> 0);                     
            $resultagent = $this->General->getData('mes_suppliers',$columns_arr,$where_arr);
            $data['agentdetail'] = $resultagent;
            
            $columns_arr= array("*");            
            //$where_arr = array('sup_status!='=> 0);                     
            $resultagent = $this->General->getData('mes_termcat',$columns_arr);
            $data['tccatdetails'] = $resultagent;
            
            $data['category'] = $this->Product_model->cat_list();
           
            $data['curren'] = $this->Product_model->currency_list();
            $data['unit'] = $this->Product_model->unit_list();
            $data['tax'] = $this->Product_model->tax_list();
           
           
           $id = $this->General->getlast_id();
            if(empty($id)){$num = 0;}else{$num = $id[0]->po_id;}
            $data['temp_id'] = ($num + 1);
           
           $this->db->empty_table('temp_po_items');
           
             $data['content'] = "pages/po";        
            $this->load->view('template',$data);
        }
        
        function viewallpo(){
            
            $data['content'] = "pages/allpo";        
            $this->load->view('template',$data);
        }
        
        
          function show_all_po() {
            $this->db->select('mes_po.*,mes_suppliers.*,mes_currancy.*,mes_country.*');
            $this->db->from('mes_po');
            $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("mes_po.po_id","desc");
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            //$data['content'] = ('pages/allinvoice');                       
            //$this->load->view('template',$data);
            
            $link = site_url('Po_Controller');
            
             $det[0]="<a href='$link'><button type='button' class='btn btn-outline-danger' >Create New PO</button></a></br>";
            //$det[0]="";
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														
														<th>PO Number</th>
														<th>MES Ref. No.</th>
														<th>Supplier Ref. No.</th>
														<th>Supplier</th>
														<th>Item</th>
														<th>Qty</th>
														<th>Currency</th>
														<th style='text-align: right;'>Total</th>
														<th>Type</th>
														<th>Date</th>
													    <th>Goods</th>
													    <th>Status</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                    
                   $vw_btn1 = "<a href ='".base_url()."Po_Controller/view_pdf/".$data[$i]->po_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>
                               <form action='poedit'  method='post'> 
                               <button type='submit' class='btn btn-outline-secondary btn-sm'  id='edit_supplier/".$data[$i]->po_id."' name='edtbutton' >Edit</button>
                               <input type='hidden' id='subpo_id'  name='subpo_id' placeholder='' value='".$data[$i]->po_id."'  />
                               </form>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->po_id."'  onclick='po_proceed(this.id)' >Process</button>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->po_id."'  onclick='po_cancel(this.id)' >Cancel</button>
                                
                                ";
                    $vw_btn2 = "<a href ='".base_url()."Po_Controller/view_pdf/".$data[$i]->po_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_btn2;}
                        
                    }
                    if($data[$i]->po_recieved==1){
                        $rec = 'Complete';
                        }else{
                        $rec = 'Partially Completed';}
                        
                    
                    $x = $i+1;
                    $dat =  "
                                 <tr scope='row'><td >".$x."</td>
                                 <td >".$data[$i]->po_code.$data[$i]->po_id."</td>
                                 <td>".$data[$i]->po_cus_qto_refno."</td>
                                 <td>".$data[$i]->po_sup_inv_refno."</td>
                                 <td>".$data[$i]->sup_company."<br>".$data[$i]->sup_addres."<br>".$data[$i]->cnty_name."<br>".$data[$i]->sup_contact."<br>".$data[$i]->sup_conperson."</td>
                                 <td>".($data[$i]->po_tot_items)."</td>
                                <td style='text-align: right;'>".$data[$i]->po_tot_qty."</td>
                                <td>".$data[$i]->curr_symble."</td>
                                <td style='text-align: right;' width='100px'>".number_format($data[$i]->po_tot_price, 2, '.', ',')."</td>
                                <td>".$data[$i]->po_type."</td>
                                <td width='70px'>".$data[$i]->po_cdate."</td>
                                <td>".$rec."</td>
                                $sts
                               
                                
                                
                                <td>$vw_btn   </td>
                               
                              
                              </tr>";
                              $x++;
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
            //echo json_encode($data);
        }
        
        function editpo(){
            
            $data['category'] = $this->Product_model->cat_list();
           
            $data['curren'] = $this->Product_model->currency_list();
            $data['unit'] = $this->Product_model->unit_list();
            $data['tax'] = $this->Product_model->tax_list();
            
            $columns_arr= array("*");            
            $where_arr = array('sup_status!='=> 0);                     
            $resultagent = $this->General->getData('mes_suppliers',$columns_arr,$where_arr);
            $data['agentdetail'] = $resultagent;
            
            $poid = $this->input->post('subpo_id');
            
        $this->db->select('mes_po.*,mes_suppliers.*,mes_currancy.*,mes_country.*');   
        $this->db->from('mes_po');
        $this->db->join( "mes_suppliers", "mes_suppliers.sup_id = mes_po.po_sup_id", 'left');
        $this->db->join( "mes_currancy", "mes_currancy.curr_id = mes_po.po_currancy", 'left');
        $this->db->join( "mes_country", "mes_country.cnty_id = mes_suppliers.sup_country", 'left');
        $this->db->where('mes_po.po_id', $poid);
        $this->db->where('mes_po.po_auth', 0);
        $query = $this->db->get();
       
        $data['editdata'] = $query->result();
        
    
        $tc = explode(',',$data['editdata'][0]->po_tc);
       $length = count($tc);
        
         for ($i = 1; $i < $length; $i++) {
                $this->db->select("*");    
                $this->db->from('mes_tmdes');
                $this->db->join('mes_termcat', 'mes_tmdes.tmdes_catid  = mes_termcat.tcat_id');
                $this->db->where('tmdes_id', $tc[$i]);
                $this->db->order_by("mes_tmdes.tmdes_catid", "asc");
                $query = $this->db->get();
                $tm[] =$query->result();
            } 
        
        $data['tnc'] = $tm;
        
            
            $columns_arr= array("*");            
            //$where_arr = array('sup_status!='=> 0);                     
            $resultagent = $this->General->getData('mes_termcat',$columns_arr);
            $data['tccatdetails'] = $resultagent;
            $data['content'] = "pages/poedit";        
            $this->load->view('template',$data);
        
        
        }
        
        
         function getpo_edit_product($id) {
        
     
        $this->db->select("*");
        $this->db->from('po_items');
        $this->db->join('mes_item', 'po_items.po_itm_item_id   = mes_item.item_id');
        $this->db->join('item', 'item.itm_id   = mes_item.item_name');
        $this->db->join('mes_currancy', 'po_items.po_itm_currancy   = mes_currancy.curr_id');
        $this->db->join('mes_service_type', 'po_items.service_des   = mes_service_type.serv_id');
        $this->db->join('mes_unit', 'po_items.po_itm_unit   = mes_unit.unit_id');
        $this->db->join( "mes_model", "mes_model.model_id = mes_item.item_model", 'left');
        $this->db->join('mes_partnumber', 'po_items.po_itm_partno  = mes_partnumber.part_id');
        $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'left');
        $this->db->join( "mes_spec", "mes_spec.spec_id  = mes_item.item_spec");
        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'left');
        $this->db->where('po_items.po_itm_po_id',$id);
        $hasil=$this->db->get();
        $dataa = $hasil->result();
        
         for($i=0; $i<count($dataa); $i++){
                if($dataa[$i]->service_des!=""){
                    $this->db->from('po_items');
                    $this->db->join('mes_service_type', 'po_items.service_des   = mes_service_type.serv_id');
                    $this->db->join('mes_unit', 'mes_service_type.unit   = mes_unit.unit_id');
                    $this->db->where('po_items.po_itm_po_id',  $id);
                    $hasill=$this->db->get();
                    $dt = $hasill->result();
                    $itm = $dataa[$i]->serv_name;
                    $data[] = array($dt[0]->serv_code,$dt[0]->serv_id,$itm,$dataa[$i]->unit_symble,$dataa[$i]->po_itm_unitprice,$dataa[$i]->po_itm_qty,$dataa[$i]->po_itm_id);    
                }else{
                    $itm = $dataa[$i]->brand_name."| ".$dataa[$i]->itm_name."| ".$dataa[$i]->model_name."| ".$dataa[$i]->part_no."| ".$dataa[$i]->spec_spec;
                    $data[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->po_itm_unitprice,$dataa[$i]->po_itm_qty,$dataa[$i]->po_itm_id); 
                }
        }
        if(!empty($data)){$data;}else{$data="";}
       
         echo json_encode($data);
    }
        
        public function get_draft($temp){
            
            $columns_arr= array("agent_id","agent");
            $resultagent = $this->General->getData('agent',$columns_arr);
            $data['agentdetail'] = $resultagent;
            
            //$data['itemdetails'] = $this->General->get_joint("login", "userlvl", "login.userlevel  = userlvl.lvl_val", "right","*");
            $data['itemdetails'] = $this->General->get_joint("stock", "product", "stock.book_id  = product.p_id", "right","*");
            $data['temp_id'] = $temp;
             $data['content'] = ('pages/invoice');                       
             $this->load->view('template',$data);
        }
        
         function test() {
            
        $this->db->select("*");    
        $this->db->from('temp_po_items');
        $this->db->where('po_itm_po_id', "2");
        $query = $this->db->get();
        print_r($query->result());
            
            foreach ($query->result() as $row) {
                $dat = array(
                'po_itm_partno'  => $row->po_itm_partno,
                'po_itm_item_id'  => $row->po_itm_item_id,
                'po_itm_po_id' => $row->po_itm_po_id,
                'po_itm_qty'  => $row->po_itm_qty, 
                'po_itm_unit'  => $row->po_itm_unit,
                'po_itm_unitprice'  => $row->po_itm_unitprice,
                'po_itm_currancy'  => $row->po_itm_currancy,
                'po_itm_description'  => $row->po_itm_description
            );
                  $this->db->insert('po_items',$dat);
                  
                  $this->db->where('po_itm_id', $row->po_itm_id);
                  $this->db->delete('temp_po_items');
                  
                  
            }
        }
        function test1() {
            $data['content'] = ('pages/test');                       
             $this->load->view('template',$data);
        }
        
        
        public function getagendata (){
            
        $uid = $this->input->post('u_id');
        $fields = array("*");
        
        $where = array(
                    'agent_id' => $uid
                  );
        
        $data = $this->General->getData('agent',$fields,$where);
        echo json_encode($data);
        
        }
        
        
        
          public function getproductdata (){
            
                $uid = $this->input->post('u_id');
               $this->db->select('*');
            $this->db->from('product');
            $this->db->where('p_id', $uid);
            $hasil=$this->db->get();  
            $data = $hasil->result();
            echo json_encode($data);
        
          }
    //put your code here
        
    function addinvoice_product() {
          
            $dat = array(
                'temp_id'  => $this->input->post('temp_id'),
                'po_itm_item_id' => $this->input->post('item'),
                'po_itm_partno' => $this->input->post('prt_id'),
                'po_itm_qty'  => $this->input->post('qty'), 
                'po_itm_unitprice'  => $this->input->post('price'),
                'po_itm_unit'  => $this->input->post('unit'),
                'po_itm_currancy'  => $this->input->post('cur'),
                'service_des'  => $this->input->post('ser')
                
            );
       $data = $this->db->insert('temp_po_items',$dat);
      
            $data = "Added";
       
        
       echo json_encode($data);
    }
    
    

    
          function add_edit_invoice_product() {
          
            $dat = array(
                'po_itm_po_id'  => $this->input->post('temp_id'),
                'po_itm_item_id' => $this->input->post('item'),
                'po_itm_partno' => $this->input->post('prt_id'),
                'po_itm_qty'  => $this->input->post('qty'), 
                'po_itm_unitprice'  => $this->input->post('price'),
                'po_itm_unit'  => $this->input->post('unit'),
                'po_itm_currancy'  => $this->input->post('cur'),
                'service_des'  => $this->input->post('ser')
                
            );
       $data = $this->db->insert('po_items',$dat);
      
            $data = "Added";
       
        
       echo json_encode($data);
    }
    
    function getinvoice_product($id) {
        
     
        $this->db->select("*");
        $this->db->from('temp_po_items');
        $this->db->join('mes_item', 'temp_po_items.po_itm_item_id   = mes_item.item_id');
        $this->db->join('item', 'item.itm_id   = mes_item.item_name');
        $this->db->join('mes_currancy', 'temp_po_items.po_itm_currancy   = mes_currancy.curr_id');
        $this->db->join('mes_service_type', 'temp_po_items.service_des   = mes_service_type.serv_id');
        $this->db->join('mes_unit', 'temp_po_items.po_itm_unit   = mes_unit.unit_id');
        $this->db->join( "mes_model", "mes_model.model_id = mes_item.item_model", 'inner');
        $this->db->join('mes_partnumber', 'temp_po_items.po_itm_partno  = mes_partnumber.part_id');
        $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'inner');
        $this->db->join( "mes_spec", "mes_spec.spec_id  = mes_item.item_spec");
        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'inner');
        $this->db->where('temp_po_items.temp_id',  $id);
        $hasil=$this->db->get();
        $dataa = $hasil->result();
        
         for($i=0; $i<count($dataa); $i++){
                if($dataa[$i]->service_des!=""){
                    $this->db->from('temp_po_items');
                    $this->db->join('mes_service_type', 'temp_po_items.service_des   = mes_service_type.serv_id');
                    $this->db->join('mes_unit', 'mes_service_type.unit   = mes_unit.unit_id');
                    $this->db->where('temp_po_items.temp_id',  $id);
                    $hasill=$this->db->get();
                    $dt = $hasill->result();
                    $itm = $dataa[$i]->serv_name;
                    $data[] = array($dt[0]->serv_code,$dt[0]->serv_id,$itm,$dataa[$i]->unit_symble,$dataa[$i]->po_itm_unitprice,$dataa[$i]->po_itm_qty,$dataa[$i]->po_itm_id);    
                }else{
                    $itm = $dataa[$i]->brand_name."| ".$dataa[$i]->itm_name."| ".$dataa[$i]->model_name."| ".$dataa[$i]->part_no."| ".$dataa[$i]->spec_spec;
                    $data[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->po_itm_unitprice,$dataa[$i]->po_itm_qty,$dataa[$i]->po_itm_id); 
                }
        }
        if(!empty($data)){$data;}else{$data="";}
       
         echo json_encode($data);
    }
    
    function invoive_update() {
     $product_code=$this->input->post('product_code');
        $product_price=$this->input->post('price');
        $product_qty =$this->input->post('qty');
        $tot = $product_price * $product_qty;
 
        $this->db->set('qty', $product_qty);
        $this->db->set('tot_price', $tot);
        $this->db->where('inv_pro_id', $product_code);
        $data=$this->db->update('temp_inv_product');
        
       echo json_encode($data);
    }
    
     function delete() {
        $product_code=$this->input->post('product_code');
        $this->db->where('po_itm_id', $product_code);
        $data=$this->db->delete('temp_po_items');
        echo json_encode($data);
    }
    
     function delete_edititem() {
        $product_code=$this->input->post('product_code');
        $this->db->where('po_itm_id', $product_code);
        $data=$this->db->delete('po_items');
        echo json_encode($data);
    }
    
    function save_inv() {
        
            $temp_id = $this->input->post('temp_id');
            $term = $this->input->post('tcvalues');
            
            $this->db->select('temp_id');
            $this->db->from('temp_po_items');
            $this->db->where('temp_id', $temp_id);
            $hasil=$this->db->get();  
            $count = $hasil->num_rows();
            
            $length = count($term);
            $tc="";
            for ($i = 1; $i < $length; $i++) {
              $tc = $tc.",".$term[$i];
              $i++;
            } 
        
        $dat = array(
                //'date'  => date('Y-m-d'),
                'po_sup_id'  => $this->input->post('agent_id'),
                'po_cus_qto_refno'  => $this->input->post('mes_ref'),
                'po_sup_inv_refno' => $this->input->post('sup_ref'),
                'po_tot_price'  => $this->input->post('total'), 
                'po_discount'  => $this->input->post('dis_val'),
                'po_final_tot'  => $this->input->post('final'),
                'po_banking'  => $this->input->post('bank_fee'),
                'po_shipping' => $this->input->post('ship'),
                'po_nbt'  => $this->input->post('nbt'), 
                'po_vat'  => $this->input->post('vat'),
                'po_tax'  => $this->input->post('tax'),
                'po_trnceport'  => $this->input->post('trfee'), 
                'po_tot_qty'  => $this->input->post('qty'),
                'po_tot_items'  => $count,
                'po_other'  => $this->input->post('other'),
                'po_currancy' => $this->input->post('cur'),
                'po_tc' => $tc,
                'po_delevery'  => "0", 
                'po_type'  => $this->input->post('sup_type'),
                'po_cuser'  => 'testuser', 
                'po_remark' => $this->input->post('remark'),
                'po_cdate'  => date('Y-m-d H:i:s')
                
            );
       $data = $this->db->insert('mes_po',$dat);
        $insert_id = $this->db->insert_id(); 
         
       
        $this->db->set('po_itm_po_id',$insert_id);
        $this->db->set('temp_id',$insert_id);
        $this->db->where('temp_id',  $this->input->post('temp_id'));
        $this->db->update('temp_po_items');
            
        $this->db->select("*");    
        $this->db->from('temp_po_items');
        $this->db->where('po_itm_po_id', $insert_id );
        $query = $this->db->get();
            
        foreach ($query->result() as $row) {
                $dat = array(
                'po_itm_partno'  => $row->po_itm_partno,
                'po_itm_item_id'  => $row->po_itm_item_id,
                'service_des'  => $row->service_des,
                'po_itm_po_id' => $row->po_itm_po_id,
                'po_itm_qty'  => $row->po_itm_qty, 
                'po_itm_unit'  => $row->po_itm_unit,
                'po_itm_unitprice'  => $row->po_itm_unitprice,
                'po_itm_currancy'  => $row->po_itm_currancy,
                'po_itm_description'  => $row->po_itm_description
            );
                  $this->db->insert('po_items',$dat);
                  
                  $this->db->where('po_itm_id', $row->po_itm_id);
                  $this->db->delete('temp_po_items');
                  
                  
            }
              $dt = "ins";
         echo json_encode($insert_id);
         //$this->view_pdf($insert_id);
    }
    
    function update_po() {
        
            $temp_id = $this->input->post('temp_id');
            $term = $this->input->post('tcvalues');
            
            $this->db->select('po_itm_item_id');
            $this->db->from('po_items');
           $this->db->where('po_itm_po_id', $temp_id);
           $hasil=$this->db->get();  
           $count = $hasil->num_rows();
            
            $length = count($term);
            $tc="";
            for ($i = 1; $i < $length; $i++) {
              $tc = $tc.",".$term[$i];
              $i++;
            } 
        
       
         
                $this->db->set('po_sup_id', $this->input->post('agent_id'));
                $this->db->set('po_cus_qto_refno', $this->input->post('mes_ref'));
                $this->db->set('po_sup_inv_refno', $this->input->post('sup_ref'));
                $this->db->set('po_tot_price', $this->input->post('total'));
                $this->db->set('po_discount', $this->input->post('dis_val'));
                $this->db->set('po_final_tot', $this->input->post('final'));
                $this->db->set('po_banking', $this->input->post('bank_fee'));
                $this->db->set('po_nbt', $this->input->post('nbt'));
                $this->db->set('po_vat', $this->input->post('vat'));
                $this->db->set('po_tax', $this->input->post('tax'));
                $this->db->set('po_trnceport', $this->input->post('trfee'));
                $this->db->set('po_tot_qty', $this->input->post('qty'));
                $this->db->set('po_tot_items', $count);
                $this->db->set('po_other', $this->input->post('other'));
                $this->db->set('po_currancy', $this->input->post('cur'));
                $this->db->set('po_tc', $tc);
                $this->db->set('po_currancy', $this->input->post('cur'));
                $this->db->set('po_remark', $this->input->post('remark'));
                $this->db->set('po_euser', 'testuser');
                $this->db->set('po_edate', date('Y-m-d H:i:s'));
                
                
                $this->db->where('po_id', $temp_id);
                $result=$this->db->update('mes_po');
       
      
            
       
              $dt = "ins";
         echo json_encode($result);
         //$this->view_pdf($insert_id);
    }
    
    function view_pdf($insert_id){
        $this->db->select("*");    
        $this->db->from('po_items');
        $this->db->where('po_itm_po_id', $insert_id );
        $this->db->join('mes_item', 'po_items.po_itm_item_id   = mes_item.item_id');
        $this->db->join('item', 'item.itm_id   = mes_item.item_name');
        $this->db->join('mes_model', 'mes_item.item_model   = mes_model.model_id');
        $this->db->join('mes_brand', 'mes_item.item_brand   = mes_brand.brand_id');
        $this->db->join('mes_partnumber', 'po_items.po_itm_partno  = mes_partnumber.part_id');
        $this->db->join('mes_unit', 'po_items.po_itm_unit   = mes_unit.unit_id');
        $this->db->join('mes_currancy', 'po_items.po_itm_currancy   = mes_currancy.curr_id');
        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'inner');
        $this->db->join( "mes_spec", "mes_spec.spec_id  = mes_item.item_spec");
        $this->db->join('mes_service_type', 'po_items.service_des   = mes_service_type.serv_id');
        $query = $this->db->get();
        $dataa=$query->result();
        
         for($i=0; $i<count($dataa); $i++){
                if($dataa[$i]->service_des!=""){
                    $this->db->from('mes_service_type');
                    $this->db->join('mes_unit', 'mes_service_type.unit   = mes_unit.unit_id');
                    $this->db->where('mes_service_type.serv_id',  $dataa[$i]->serv_id);
                    $hasill=$this->db->get();
                    $dt = $hasill->result();
                    $itm = $dataa[$i]->serv_name;
                    $inv[] = array($dt[0]->serv_code,$dt[0]->serv_id,$itm,$dataa[$i]->unit_symble,$dataa[$i]->po_itm_unitprice,$dataa[$i]->po_itm_qty,$dataa[$i]->po_itm_id);    
                }else{
                    
                    if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->model_name!=""){$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]->part_no.$spece2.$dataa[$i]->spec_spec;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->po_itm_unitprice,$dataa[$i]->po_itm_qty,$dataa[$i]->po_itm_id); 
                }
        }
        $data['inv_itm'] = $inv;
        
        
        $this->db->select("*");    
        $this->db->from('mes_po');
        $this->db->where('po_id', $insert_id );
        $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();
       $tc = explode(',',$data['inv'][0]->po_tc);
      // sort($tc);
       $length = count($tc);
        
         for ($i = 1; $i < $length; $i++) {
                $this->db->select("*");    
                $this->db->from('mes_tmdes');
                $this->db->join('mes_termcat', 'mes_tmdes.tmdes_catid  = mes_termcat.tcat_id');
                $this->db->where('tmdes_id', $tc[$i]);
                $this->db->order_by("mes_tmdes.tmdes_catid", "asc");
                $query = $this->db->get();
                $tm[] =$query->result();
            } 
        
        $data['tm'][0] = $tm;
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
     
     
        $this->load->view('pages/popdf',$data);
    }

    function save_po(){

      $dat = array(
                'date'  => date('Y-m-d'),
                'agent_id'  => $this->input->post('agent_id'),
                'order_qty'  => $this->input->post('qty'),
                'total_price' => $this->input->post('total'),
                'discount'  => $this->input->post('dis'), 
                'dis_price'  => $this->input->post('dis_val'),
                'final_total'  => $this->input->post('final'),
                'created_by'  => $this->session->userdata['login_data']['username'], 
                'created_on'  => date('Y-m-d H:i:s')
                
            );
       $data = $this->db->insert('invoice',$dat);
        $insert_id = $this->db->insert_id();  
    }
    
    function save_draft() {
         $dat = array(
                'date'  => date('Y-m-d'),
                'agent_id'  => $this->input->post('agent_id'),
                'order_qty'  => $this->input->post('qty'),
                'total_price' => $this->input->post('total'),
                'discount'  => $this->input->post('dis'), 
                'dis_price'  => $this->input->post('dis_val'),
                'final_total'  => $this->input->post('final'),
                'created_by'  => $this->session->userdata['login_data']['username'], 
                'created_on'  => date('Y-m-d H:i:s')
                
            );
       $data = $this->db->insert('draft_invoice',$dat);
        $insert_id = $this->db->insert_id();    
       
        $this->db->set('temp_id',$insert_id);
        $this->db->where('temp_id',  $this->input->post('temp_id'));
        $this->db->update('temp_inv_product');
            
        $this->db->from('temp_inv_product');
        $this->db->select("*");
        $this->db->where('temp_id', $insert_id );
            $query = $this->db->get();
            
            foreach ($query->result() as $row) {
                $dat = array(
                'inv_id'  => $row->temp_id,
                'product_id'  => $row->product_id,
                'qty' => $row->qty,
                'unit_price'  => $row->unit_price, 
                'discount'  => $row->discount,
                'tot_price'  => $row->tot_price,
                'created_by'  => $row->created_by,
                'date'  => $row->date
            );
                  $this->db->insert('draft_inv_product',$dat);
                  
            }
                $this->db->where('temp_id', $insert_id);
                  $data=$this->db->delete('temp_inv_product');

        
       
       echo json_encode($data);
    }
    
    function cancel_inv() {
                    $this->db->where('temp_id', $this->input->post('temp_id'));
                    $data=$this->db->delete('temp_inv_product');
                    echo json_encode($data);
    }
    
     function cancel_po() {
                    
                    
                $this->db->set('po_status', 0);
                $this->db->set('po_euser', 'testuser');
                $this->db->set('po_edate', date('Y-m-d H:i:s'));
                
                
                $this->db->where('po_id', $this->input->post('id'));
                $result=$this->db->update('mes_po');
                
                    echo json_encode($result);
    }
    
	
	
    public function invprint(){
        
          $inv = $this->input->post('hidnprintid');
            
            $this->db->select('*');
            $this->db->from('invoice');
            $this->db->join( "agent", "invoice.agent_id  = agent.agent_id", 'inner');
            $this->db->join( "inv_product", "invoice.inv_num  = inv_product.inv_id ", 'inner');
            $this->db->join( "product", "inv_product.product_id  = product.p_id ", 'inner');
            $this->db->where('inv_num', $inv);
            $hasil=$this->db->get();
            $data['inv']= $hasil->result();
            $data['test'] = "AAkarsha";
            $data['user'] = $this->session->userdata['login_data']['username'];
            $this->load->view('pages/invoicepdf',$data);
            
              $updatearr = array(                   
                   'print'=> 1
                  );
              $updateware = array(                   
                   'inv_num' => $inv
               );
                $usereditresult = $this->General->updateData('invoice',$updatearr,$updateware);
                
                $this->view_allinvoice();
                
    }
    
    public function invprintre(){
        $inv = $this->input->post('hidnprintid');
         $this->db->select('*');
            $this->db->from('invoice');
            $this->db->join( "agent", "invoice.agent_id  = agent.agent_id", 'inner');
            $this->db->join( "inv_product", "invoice.inv_num  = inv_product.inv_id ", 'inner');
            $this->db->join( "product", "inv_product.product_id  = product.p_id ", 'inner');
            $this->db->where('inv_num', $inv);
            $hasil=$this->db->get();
            $data['inv']= $hasil->result();
            $data['test'] = "AAkarsha";
            $data['user'] = $this->session->userdata['login_data']['username'];
            $this->load->view('pages/invoicerepdf',$data);
            $this->view_allinvoice();
    }
    
    function getbrand() {
        $cat = $this->input->post('u_id');
        
            $columns_arr= array("*");       
            $resultagent = $this->General->getData('mes_brand',$columns_arr);
            $data['brand'] = $resultagent;
            
            $columns_arr= array("*");      
            $where_arr = array('item_cat'=> $cat);  
            $resultitem = $this->General->getData('mes_item',$columns_arr,$where_arr);
            $data['item'] = $resultitem;
            
            echo json_encode($data);
        
    }
    
    function getproduct() {
        $cat = $this->input->post('u_id');
        
            $columns_arr= array("*");       
            $resultagent = $this->General->getData('mes_brand',$columns_arr);
            $data['brand'] = $resultagent;
            
            $resultitem = $this->Product_model->product_cat_list($cat);
            $data['item'] = $resultitem;
            
            echo json_encode($data);
        
    }
    
    function getpart_num(){
       $cat_id = $this->input->post('cat_id');
        //$cat_id ="7";
        
            $this->db->select("*");
            $this->db->from('mes_item');
            $this->db->join( "mes_partnumber", "mes_partnumber.part_itmno = mes_item.item_id", 'inner');
            $this->db->where('mes_item.item_cat',$cat_id);
            $hasil=$this->db->get();
            $data['part'] = $hasil->result();
            
            $this->db->select("*");
            $this->db->from('mes_spec');
            $this->db->join( "mes_item", "mes_spec.spec_id = mes_item.item_spec");
            $this->db->where('mes_item.item_cat',$cat_id);
            $this->db->where('mes_item.item_spec!=','');
            $hasil=$this->db->get();
            $data['spec'] = $hasil->result();
            
            $resultitem = $this->Product_model->product_cat_list($cat_id);
            $data['item'] = $resultitem;
           echo json_encode($data);
    }
    
    function getpart(){
        $item = $this->input->post('itm_id');
            
            $data['item']=  $this->Product_model->single_product($item);
            
            $prt =$this->Product_model->part_num($item);
            foreach($prt as $part){
                $partn[]=$part->part_id."/".$part->part_no;
            }
            if(!empty($partn)){
                $data['prt'] = $partn;
            }else{$data['prt'] = "";}
    
            
            
            echo json_encode($data);
    }
    
    function getprod(){
        
        $this->db->select('*');
            $this->db->from('mes_item');
            $this->db->join( "mes_partnumber", "mes_partnumber.part_itmno  = mes_item.item_id");
            $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_unit", "mes_unit.unit_id = mes_item.item_unit", 'inner');
            $this->db->join( "mes_model", "mes_model.model_id = mes_item.item_model", 'inner');
            $this->db->join( "item", "mes_item.item_name = item.itm_id");
            $this->db->where('mes_partnumber.part_id', $this->input->post('part_id'));
            $hasil=$this->db->get();
            $data['item'] = $hasil->result();
            echo json_encode($data);
    }

    function getprod_spec(){
        
        $this->db->select('*');
            $this->db->from('mes_item');
            $this->db->join( "mes_spec", "mes_spec.spec_id  = mes_item.item_spec");
            $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_unit", "mes_unit.unit_id = mes_item.item_unit", 'inner');
            $this->db->join( "mes_model", "mes_model.model_id = mes_item.item_model", 'inner');
            $this->db->join( "item", "mes_item.item_name = item.itm_id");
            $this->db->where('mes_spec.spec_id', $this->input->post('part_id'));
            $hasil=$this->db->get();
            $data['item'] = $hasil->result();
            echo json_encode($data);
    }



    function getsup(){
        $sup = $this->input->post('sup_id');
            
            $this->db->select('*');
            $this->db->from('mes_suppliers');
            $this->db->join( "mes_currancy", "mes_suppliers.sup_currency  = mes_currancy.curr_id");
            $this->db->where('mes_suppliers.sup_id', $sup);
            $hasil=$this->db->get();
            $data['sup'] = $hasil->result();

            echo json_encode($data);
    }
    
    function getservice(){
            $this->db->select('*');
            $this->db->from('mes_service_type');
            $hasil=$this->db->get();
            $data['service'] = $hasil->result();

            echo json_encode($data);
    }
    
      function gettc(){
          
          $cat = $this->input->post('cat_id');
            $this->db->select('*');
            $this->db->from('mes_tmdes');
            $this->db->join( "mes_termcat", "mes_tmdes.tmdes_catid = mes_termcat.tcat_id", 'inner');
            $this->db->where('tmdes_catid', $cat);
            $hasil=$this->db->get();
            $data['tc'] = $hasil->result();

            echo json_encode($data);
    }
    
    function addtc(){
        
         $dat = array(
                'potrem_catid'  => $this->input->post('tc_cat'),
                'potrem_desid' => $this->input->post('tc_des'),
                'potrem_poid' => $this->input->post('temp_id')
                
                
            );
       $data = $this->db->insert('po_terms',$dat);
      
            $data = "Added";
       
        
       echo json_encode($data);
    }
    
    function termshow($id){
        
        
        $this->db->select("*");
        $this->db->from('po_terms');
        $this->db->join('mes_termcat', 'po_terms.potrem_catid   = mes_termcat.tcat_id');
        $this->db->join('mes_tmdes', 'po_terms.potrem_desid   = mes_tmdes.tmdes_id');
      
        $this->db->where('po_terms.potrem_poid',  $id);
        $this->db->group_by( 'potrem_catid' );
        $hasil=$this->db->get();
        $data = $hasil->result(); 

            echo json_encode($data);
        
    }
    
            function serv_add(){
            $det="
             <div class='form-group row'>
               
               <input type='hidden' name='table' id='table' class='form-control' readonly='' value='save_services'>
                                <label class='col-md-2 col-form-label'>service</label>
                                <div class='col-md-10'>
                                  <textarea name='serv' id='serv' rows='2' placeholder='' class='form-control'></textarea> 
                                 </div>
                            </div>
                            
                               
                            
                          <!--div class='form-group row'>
                                <label class='col-md-2 col-form-label'>Description</label>
                                <div class='col-md-10'>
                                  <input type='text' name='brn_des' id='brn_des' class='form-control' placeholder=''>
                                 </div>
                            </div-->
                        
              ";
                        
                	
               echo json_encode($det);	
        }
        
        function getserv_id(){
            
            $this->db->select('*');
            $this->db->from('mes_service_type');
            $this->db->where('serv_id', $this->input->post('id'));
            $this->db->join('mes_unit', 'mes_unit.unit_id   = mes_service_type.unit');
            $hasil=$this->db->get();
            $data['service'] = $hasil->result();

            echo json_encode($data);
        }
        
        function proceed(){
            
            $this->db->select('po_recieved');
            $this->db->from('mes_po');
            $this->db->where('po_id', $this->input->post('id'));
            $hasil=$this->db->get();
            $podata = $hasil->result();
            
            
            
            if($podata[0]->po_recieved == 0){
                
                $data[0]= "GRN Not Completed";
                
            }else{
                
               $this->db->set('po_auth', 1);
            $this->db->where('po_id', $this->input->post('id'));
            $result=$this->db->update('mes_po');
            if($result){
                    $data[0]= "ok";
                }else{
                    
                   $data[0]= "error"; 
                }
                $data[1] = $this->input->post('id'); 
                 
            }
            echo json_encode($data);
            
        }
        
        function suplst(){
            
            $this->db->from('mes_suppliers');
            $this->db->select("mes_suppliers.*, mes_currancy.*,mes_country.*");
            $this->db->join( "mes_currancy", "mes_currancy.curr_id = mes_suppliers.sup_currency", 'inner');
            $this->db->join( "mes_country", "mes_country.cnty_id = mes_suppliers.sup_country", 'inner');
            $detailtax = $this->db->get();
            $data['sup'] = $detailtax->result();
            echo json_encode($data);
        }
    
        function catlst(){
            
            $data['cat'] = $this->Product_model->cat_list();
            echo json_encode($data);
        }    
        
}  