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

class Cusorder_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');
         
        }
        
        function test(){
           $data = $this->Common_model->cal_stock("TST","555",2,3,"plus",0); 
        }
        
        function index(){
                    $this->db->select('po_id as id,po_code as code');
                    $this->db->from('mes_qto');
                    $this->db->where('po_status',1);
                    $this->db->where('po_auth',0);
                    $this->db->where('po_recieved',0);
                    $hasil=$this->db->get();
                    $resultagent= $hasil->result(); 
            $data['qtn'] = $resultagent;
            
                    $this->db->from('mes_ret_reason');
                    $this->db->where('ret_reason_status',1);
                    $hasil=$this->db->get();
                    $res= $hasil->result(); 
            $data['ret_res'] = $res;
            $data['tax'] = $this->Product_model->tax_list();
            $columns_arr= array("*");
            $resultagent = $this->General->getData('mes_termcat',$columns_arr);
            $data['tccatdetails'] = $resultagent;
            $data['customer'] = $this->Product_model->customer_list();
            
            $data['content'] = "pages/newcusorder";        
            $this->load->view('template',$data);
        }
        
        function getproduct(){
          
            $data = $this->Common_model->qto_item($this->input->post('po_id'));
            
           $det['item']="";
           $totamnt = 0.00;
                for($i=1; $i<count($data); $i++){
                 
                    
                    $dat =  "
                                 <tr>
                               <td >QTN".$this->input->post('po_id')."</td>
                               <td >".$data[$i][0]."-".$data[$i][1]."</td>
                               <td >".$data[$i][2]."</td>
                               <td style='text-align: center;'>".$data[$i][3]."</td>
                               <td style='text-align: right;'>".$data[$i][4]."</td>
                                <td style='text-align: right;'>".$data[$i][5]."</td>
                               <td><input type='text' id=".$data[$i][7]."  name='po_val[]' placeholder='' value='".$data[$i][5]."' class='col-xs-12 col-sm-12 input-sm form-control-sm form-control txt_val'/ style='text-align: right;' onkeyup='get_cal()'>
                               <input type='hidden'  name='item_id[]' value='".$data[$i][7]."'>
                               </td>
                              
                               
                             <td style='text-align: right;'>
                                <div class='hidden-sm hidden-xs btn-group'>
                                 <button class='btn btn-outline-danger btn-sm' onclick='deleteRow(this)'>X</button>
                   
                               </div>
                                </td>
                                </tr>";
                                
                              $det['item'] =$det['item'].$dat;
                              
                              
                }
           
            $this->db->select('mes_customer.cust_company as company,mes_customer.cust_country as country,mes_qto.po_sup_inv_refno as customer_ref,mes_qto.po_currancy as cur_id,mes_currancy.curr_symble as curr,mes_qto.po_discount as podis,
            mes_qto.po_banking as bank,mes_qto.po_shipping as ship,mes_qto.po_trnceport as trans,mes_qto.po_tax as tax,mes_qto.po_other as other,mes_qto.po_tot_price as total');
            $this->db->from('mes_qto');
            $this->db->where('po_id', $this->input->post('po_id'));
            $this->db->join( "mes_customer", "mes_customer.cust_id = mes_qto.po_sup_id", 'inner');
            $this->db->join( "mes_currancy", "mes_currancy.curr_id = mes_qto.po_currancy", 'inner');
            $this->db->join( "mes_country", "mes_country.cnty_id = mes_customer.cust_country", 'inner');
            $hasil=$this->db->get();
            $det['customer']= $hasil->result(); 
            
            /*$this->db->select('po_tot_price, po_discount, po_final_tot, po_banking, po_shipping, po_trnceport, po_nbt, po_vat, po_tax, po_tot_qty, po_tot_items, po_other');
            $this->db->from('mes_qto');
            $cgr=$this->db->get();
            $chrg= $cgr->result(); 
            
            $det['price'] = "";
            for($i;$i<=mysql_num_fields($chrg);$i++){
                $detail = "<input type='text' id=".$data[$i][5]."  name='po_tot[]' value=".$data[$i][5].">";
                $det['price']+=$detail;
            }
            */
                
                  echo json_encode($det);
        }
        
        function view_all(){
            
            $columns_arr= array("*");            
            $where_arr = array('tech_status ='=> 1);                     
            $resultagentt = $this->General->getData('mes_technician',$columns_arr,$where_arr);
            $data['tccatdetailst'] = $resultagentt;
            
            $data['content'] = "pages/allco";        
            $this->load->view('template',$data);
        }
        
        function vwnew_inv(){
            
            $columns_arr= array("*");
            $resultagent = $this->General->getData('mes_termcat',$columns_arr);
            $data['tccatdetails'] = $resultagent;
            
            $data['id'] = $this->input->post('cuso_id');
            $data['content'] = "pages/new_invoice";        
            $this->load->view('template',$data);
        }
        
        function show_all_cusorder() {
            $this->db->select('*');
            $this->db->from('mes_cus_order');
            $this->db->join( "mes_customer", "mes_customer.cust_id = mes_cus_order.co_cus_id", 'inner');
            $this->db->join( "mes_currancy", "mes_currancy.curr_id = mes_cus_order.co_currancy", 'inner');
            $this->db->join( "mes_country", "mes_country.cnty_id = mes_customer.cust_country", 'inner');
            $this->db->order_by("mes_cus_order.co_id","desc");
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            //$data['content'] = ('pages/allinvoice');                       
            //$this->load->view('template',$data);
            
            $link = site_url('customer_order');
            
             $det[0]="<a href='$link'><button type='button' class='btn btn-outline-danger' >Create New Customer Order</button></a></br>";
            //$det[0]="";
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														
														<th>CO Number</th>
														<th style='word-wrap: anywhere;'>Quotation No.</th>
														<th>Customer</th>
														<th>PO No.</th>
														<th>Item</th>
														<th>Qty</th>
														<th style='text-align: right;'>Total Amount</th>
														<th>Ordered Date</th>
														<th>Date</th>
													    <th>Delivery</th>
													    <th>Status</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                    
                       $this->db->from('inv_items'); 
                   $this->db->select("*");
                   $this->db->where('co_itm_qto_id',$data[$i]->co_id);
                   $this->db->where('service_des!=','');
                   $deta = $this->db->get();
                   $serv = $deta->result(); 
                   
                    $this->db->from('mes_invoice'); 
                   $this->db->select("co_id");
                   $this->db->where('co_qto_id',$data[$i]->co_id);
                   $this->db->where('do_confrm!=','1');
                   $del = $this->db->get();
                 
                        if($data[$i]->co_status==1){
                        $sts = "<td style='color:#c6ab1b85; font-size:25px; text-align: center;'>&#9679;</td>"; 
                        }else{
                        $sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>";  }
                        
                    if($del->num_rows()>0){$rec = 'Delivery Pending';}else{$rec = 'Complete';}    
                   
                    
                    //$vw_btn ="<a href ='".base_url()."view_co/".$data[$i]->co_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>";
                    $vw_btn ="<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='view_coinfo/".$data[$i]->co_id."' name='edtbutton' onclick='edit_view(this.id)' data-target='#Modal_edit'>CO Info</button>";
                    
                    if($deta->num_rows()>0){
                            $this->db->from('mes_int_job'); 
                            $this->db->select("job_id");
                           $this->db->where('job_co_id',$data[$i]->co_id);
                           $jb = $this->db->get();
                        if($jb->num_rows()>0){
                            $vw_btn .=' 
                        <button type="button" class="btn btn-outline-secondary btn-sm" type="button" id="view_jobinfo/'.$data[$i]->co_id.'" onclick="view_jb(this.id)">Job Card</button><br>
                       </br>';
                        }else{
                            $vw_btn .=' 
                        <button type="button" class="btn btn-outline-secondary btn-sm" type="button" id="'.$data[$i]->co_id.'" onclick="crt_jb(this.id)">Create Job Card</button><br>
                       </br>';
                        }
                        
                          
                       
                     }
                    
                    if(($data[$i]->co_profoma<1)&&($data[$i]->co_inv_qty<1)){
                        
                        $vw_btn .= "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_supplier/".$data[$i]->co_id."' name='edtbutton' onclick='edit_view(this.id)' data-target='#Modal_edit'>Edit</button>
                                <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->co_id."'  onclick='po_performa(this.id)' >Create Proforma Invoice</button>
                                <form method='post' action='".site_url('new_inv')."'><input type='hidden' value='".$data[$i]->co_id."' name='cuso_id'><button class='btn btn-outline-secondary btn-sm' type='submit' id='".$data[$i]->co_id."' >Create Invoice</button></form>";
                    
                        $rec = 'Delivery Pending';
                    }elseif(($data[$i]->co_profoma<1)&&($data[$i]->co_inv_qty<$data[$i]->co_tot_qty)){
                        $vw_btn .= "
                                <form method='post' action='".site_url('new_inv')."'><input type='hidden' value='".$data[$i]->co_id."' name='cuso_id'><button class='btn btn-outline-secondary btn-sm' type='submit' id='".$data[$i]->co_id."' >Create Invoice</button></form>";
                        $rec = 'Delivery Pending';
                        
                    }elseif (($data[$i]->co_profoma>99)){
                        $vw_btn .="";
                    }elseif (($data[$i]->co_profoma>1)&&($data[$i]->co_inv<1)){
                        $vw_btn .="";
                    }else{
                        $vw_btn .="";
                    }
                    
                    
                    $x = $i+1;
                    $dat =  "
                                 <tr scope='row'><td >".$x."</td>
                                 <td >".$data[$i]->co_code.$data[$i]->co_id."</td>
                                 <td style='width:20%;word-wrap: anywhere;'>".$data[$i]->co_cus_refno."</td>
                                 <td>".$data[$i]->cust_company."<br>".$data[$i]->cust_addres."<br>".$data[$i]->cust_name."<br>".$data[$i]->cust_contact."<br>".$data[$i]->cust_conperson."</td>
                                 <td>".($data[$i]->co_cusod_ponum)."</td>
                                 <td>".($data[$i]->co_tot_items)."</td>
                                <td style='text-align: right;'>".$data[$i]->co_tot_qty."</td>
                                <td style='text-align: right;' width='100px'>".number_format($data[$i]->co_final_tot, 2, '.', ',').$data[$i]->curr_symble."</td>
                                <td width='70px'>".$data[$i]->co_cdate."</td>
                                <td width='70px'>".$data[$i]->co_date."</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 savecus_order(){
            $id = $this->input->post('ids');
            $qt = $this->input->post('values');
           
           $tot = 0;
            $dat = array(
                        'grn_poid'  => "",
                        'grn_invnum' => $this->input->post('inv'),
                        'grn_donum' => $this->input->post('podo'),
                        'grn_totqty'  => $tot,
                        'grn_cuser'  => "admin",
                        'grn_cdate'  => date('Y-m-d')
                        
                    );
            $data = $this->db->insert('mes_grn',$dat);
           $insert_id = $this->db->insert_id();
           
            for($i=0; $i<count($id); $i++){
                if($qt[$i]!=""){
                    $dat = array(
                        'invt_type'  => "GRN",
                        'invt_type_id'  => $insert_id,
                        'invt_itemid'  => $id[$i],
                        'invt_qty' => $qt[$i],
                        'invt_cuser'  => "admin",
                        'invt_cdate'  =>date('Y-m-d')
                        
                    );
                    $data = $this->db->insert('mes_inventry',$dat);
                    $tot = $tot+$qt[$i];
                }
            }
           
            $this->db->set('grn_totqty', $tot);
            $this->db->where('grn_id', $insert_id);
            $result=$this->db->update('mes_grn');
           
      
            $send = "New GRN Created";
            echo json_encode($send);
        } 
        
        function save_inv(){
               // var_dump($_POST);
                
                 $item = $this->input->post('item_id');
                    $val = $this->input->post('po_val');
                $term = $this->input->post('term');
            
            
            
            $length = count($term);
            $tc="";
            for ($i = 1; $i < $length; $i++) {
              $tc = $tc.",".$term[$i];
             
            } 
        
        $dat = array(
                //'date'  => date('Y-m-d'),
                'co_cus_id'  => $this->input->post('cus_id'),
                'co_qto_refno'  => $this->input->post('poselect'),
                'co_qto_id' => $this->input->post('all_qt'),
                'co_cus_refno' => $this->input->post('all_qt'),
                'co_tot_price'  => $this->input->post('inv_total'), 
                'co_discount'  => $this->input->post('dis_total'),
                'co_final_tot'  => $this->input->post('final1'),
                'co_banking'  => $this->input->post('bank_fee'),
                'co_shipping' => $this->input->post('ship'),
                'co_nbt'  => $this->input->post('nbt'), 
                'co_vat'  => $this->input->post('vat'),
                'co_tax'  => $this->input->post('tax_pc'),
                'co_trnceport'  => $this->input->post('trfee'), 
                'co_tot_qty'  => $this->input->post('inv_tot_item'),
                'co_tot_items'  => count($this->input->post('item_id')),
                'co_other'  => $this->input->post('other_amnt'),
                'co_currancy' => $this->input->post('curid'),
                'co_cusod_ponum' => $this->input->post('cuspo'),
                'co_tc' => $tc,
                'co_delevery'  => "0", 
                'co_cuser'  => 'testuser', 
                'co_remark' => $this->input->post('remark'),
                'co_cdate'  => date('Y-m-d H:i:s'),
                'co_date'  => $this->input->post('codate')
                
            );
       $data = $this->db->insert('mes_cus_order',$dat);
        $insert_id = $this->db->insert_id(); 
         
         
       for($i=0; $i<count($item); $i++){
                if($item[$i]!=""){
                    $this->db->select("*");    
                    $this->db->from('qto_items');
                    $this->db->where('po_itm_id', $item[$i]);
                    $query = $this->db->get();
                    $itmdata = $query->result(); 
                    
                    $dat = array(
                        'co_itm_item_id'  => $itmdata[0]->po_itm_item_id,
                        'co_itm_qto_id'  => $itmdata[0]->po_itm_po_id,
                        'co_itm_co_id'  => $insert_id,
                        'co_itm_partno'  => $itmdata[0]->po_itm_partno,
                        'service_des' => $itmdata[0]->service_des,
                        'co_itm_qty'  => $val[$i],
                        'co_itm_unit'  => $itmdata[0]->po_itm_unit,
                        'co_itm_unitprice'  => $itmdata[0]->po_itm_unitprice,
                        'co_itm_currancy' =>  $this->input->post('curid')
                        
                    );
                    $data = $this->db->insert('co_items',$dat);
                   
                }
            }
   
              $dt = "ins";
         echo json_encode($insert_id);
         //$this->view_pdf($insert_id);
            }
            
        function view_pdf($insert_id){
             $this->db->select("*");    
        $this->db->from('co_items');
        $this->db->where('co_itm_co_id', $insert_id );
        $this->db->join('mes_item', 'co_items.co_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', 'co_items.co_itm_partno  = mes_partnumber.part_id');
        $this->db->join('mes_unit', 'co_items.co_itm_unit   = mes_unit.unit_id');
        $this->db->join('mes_currancy', 'co_items.co_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', 'co_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]->co_itm_unitprice,$dataa[$i]->co_itm_qty,$dataa[$i]->co_itm_id,$dataa[$i]->co_itm_qto_id);    
                }else{
                    
                    if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->part_no!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[$i]->model_name!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name.$spece2.$dataa[$i]->part_no.$spece1.$dataa[$i]->spec_spec;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->co_itm_unitprice,$dataa[$i]->co_itm_qty,$dataa[$i]->co_itm_id,$dataa[$i]->co_itm_qto_id); 
                }
        }
        $data['inv_itm'] = $inv;
        
        
        $this->db->select("mes_invoice.*,mes_customer.*,mes_currancy.*,mes_country.*,mes_cus_order.*, mes_cus_order.co_id as codid, mes_cus_order.co_code as codcode");     
        $this->db->from('mes_cus_order');
        $this->db->where('mes_cus_order.co_id', $insert_id );
        $this->db->join('mes_customer', 'mes_cus_order.co_cus_id  = mes_customer.cust_id');
        $this->db->join('mes_currancy', 'mes_cus_order.co_currancy   = mes_currancy.curr_id');
        $this->db->join('mes_country', 'mes_customer.cust_country   = mes_country.cnty_id');
        $this->db->join('mes_invoice', 'mes_cus_order.co_id  = mes_invoice.co_id','left');
        $query = $this->db->get();
        $data['inv']=$query->result();
       $tc = explode(',',$data['inv'][0]->co_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/copdf',$data);
        }  
        
        function view_cuso($insert_id){
             $this->db->select("*");    
        $this->db->from('co_items');
        $this->db->where('co_itm_co_id', $insert_id );
        $this->db->join('mes_item', 'co_items.co_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', 'co_items.co_itm_partno  = mes_partnumber.part_id');
        $this->db->join('mes_unit', 'co_items.co_itm_unit   = mes_unit.unit_id');
        $this->db->join('mes_currancy', 'co_items.co_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', 'co_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]->co_itm_unitprice,$dataa[$i]->co_itm_qty,$dataa[$i]->co_itm_id,$dataa[$i]->co_itm_qto_id);    
                }else{
                    
                    if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->part_no!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[$i]->model_name!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name.$spece2.$dataa[$i]->part_no.$spece1.$dataa[$i]->spec_spec;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->co_itm_unitprice,$dataa[$i]->co_itm_qty,$dataa[$i]->co_itm_id,$dataa[$i]->co_itm_qto_id); 
                }
        }
        $data['inv_itm'] = $inv;
        
        
        $this->db->select("*");    
        $this->db->from('mes_cus_order');
        $this->db->where('co_id', $insert_id );
        $this->db->join('mes_customer', 'mes_cus_order.co_cus_id  = mes_customer.cust_id');
        $this->db->join('mes_currancy', 'mes_cus_order.co_currancy   = mes_currancy.curr_id');
        $this->db->join('mes_country', 'mes_customer.cust_country   = mes_country.cnty_id');
        $query = $this->db->get();
        $data['inv']=$query->result();
       $tc = explode(',',$data['inv'][0]->co_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();
     
        $data['content'] = "pages/co_view";        
            $this->load->view('template',$data);
        } 
        
        function view_perf_pdf($insert_id){
             $this->db->select("*");    
        $this->db->from('co_items');
        $this->db->where('co_itm_co_id', $insert_id );
        $this->db->join('mes_item', 'co_items.co_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', 'co_items.co_itm_partno  = mes_partnumber.part_id');
        $this->db->join('mes_unit', 'co_items.co_itm_unit   = mes_unit.unit_id');
        $this->db->join('mes_currancy', 'co_items.co_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', 'co_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]->co_itm_unitprice,$dataa[$i]->co_itm_qty,$dataa[$i]->co_itm_id,$dataa[$i]->co_itm_qto_id);    
                }else{
                    
                    if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->part_no!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[$i]->model_name!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name.$spece2.$dataa[$i]->part_no.$spece1.$dataa[$i]->spec_spec;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->co_itm_unitprice,$dataa[$i]->co_itm_qty,$dataa[$i]->co_itm_id,$dataa[$i]->co_itm_qto_id); 
                }
        }
        $data['inv_itm'] = $inv;
        
        
        $this->db->select("mes_customer.*,mes_currancy.*,mes_country.*,mes_cus_order.*,mes_performa.*,mes_cus_order.co_id as cust_order");    
        $this->db->from('mes_cus_order');
        $this->db->where('mes_cus_order.co_id', $insert_id );
        $this->db->join('mes_customer', 'mes_cus_order.co_cus_id  = mes_customer.cust_id');
        $this->db->join('mes_currancy', 'mes_cus_order.co_currancy   = mes_currancy.curr_id');
        $this->db->join('mes_country', 'mes_customer.cust_country   = mes_country.cnty_id');
        $this->db->join('mes_performa', 'mes_cus_order.co_id   = mes_performa.co_id');
        $query = $this->db->get();
        $data['inv']=$query->result();
       $tc = explode(',',$data['inv'][0]->co_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/perf_pdf',$data);
        }  
        
        function view_cominv($insert_id){
             $this->db->select("*");    
        $this->db->from('co_items');
        $this->db->where('co_itm_co_id', $insert_id );
        $this->db->join('mes_item', 'co_items.co_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', 'co_items.co_itm_partno  = mes_partnumber.part_id');
        $this->db->join('mes_unit', 'co_items.co_itm_unit   = mes_unit.unit_id');
        $this->db->join('mes_currancy', 'co_items.co_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', 'co_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]->co_itm_unitprice,$dataa[$i]->co_itm_qty,$dataa[$i]->co_itm_id,$dataa[$i]->co_itm_qto_id);    
                }else{
                    
                    if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->part_no!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[$i]->model_name!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name.$spece2.$dataa[$i]->part_no.$spece1.$dataa[$i]->spec_spec;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->co_itm_unitprice,$dataa[$i]->co_itm_qty,$dataa[$i]->co_itm_id,$dataa[$i]->co_itm_qto_id); 
                }
        }
        $data['inv_itm'] = $inv;
        
        
        $this->db->select("*");    
        $this->db->from('mes_cus_order');
        $this->db->where('co_id', $insert_id );
        $this->db->join('mes_customer', 'mes_cus_order.co_cus_id  = mes_customer.cust_id');
        $this->db->join('mes_currancy', 'mes_cus_order.co_currancy   = mes_currancy.curr_id');
        $this->db->join('mes_country', 'mes_customer.cust_country   = mes_country.cnty_id');
        $query = $this->db->get();
        $data['inv']=$query->result();
       $tc = explode(',',$data['inv'][0]->co_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/comp_inv_pdf',$data);
        }
        
        function view_inv($insert_id){
             $this->db->select("*");    
        $this->db->from('inv_items');
        $this->db->where('co_itm_co_id', $insert_id);
        $this->db->join('mes_item', 'inv_items.co_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', 'inv_items.co_itm_partno  = mes_partnumber.part_id');
        $this->db->join('mes_unit', 'inv_items.co_itm_unit   = mes_unit.unit_id');
        $this->db->join('mes_currancy', 'inv_items.co_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', 'inv_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]->co_itm_unitprice,$dataa[$i]->co_itm_qty,$dataa[$i]->co_itm_id,$dataa[$i]->co_itm_qto_id);    
                }else{
                    
                    if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->part_no!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[$i]->model_name!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name.$spece2.$dataa[$i]->part_no.$spece1.$dataa[$i]->spec_spec;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->co_itm_unitprice,$dataa[$i]->co_itm_qty,$dataa[$i]->co_itm_id,$dataa[$i]->co_itm_qto_id); 
                }
        }
        $data['inv_itm'] = $inv;
        
        
        $this->db->select("mes_invoice.*,mes_customer.*,mes_currancy.*,mes_country.*,mes_cus_order.*, mes_invoice.co_id as invid, mes_invoice.co_qto_id as cod,mes_invoice.co_code as invcode,mes_invoice.co_final_tot as final_tot,mes_invoice.co_discount as dis_val,mes_invoice.co_qto_id as cuor,mes_cus_order.co_code as cus_code");    
        $this->db->from('mes_invoice');
        $this->db->where('mes_invoice.co_id', $insert_id);
        $this->db->join('mes_customer', 'mes_invoice.co_cus_id  = mes_customer.cust_id');
        $this->db->join('mes_currancy', 'mes_invoice.co_currancy   = mes_currancy.curr_id');
        $this->db->join('mes_country', 'mes_customer.cust_country   = mes_country.cnty_id');
        $this->db->join('mes_cus_order', 'mes_invoice.co_qto_id   = mes_cus_order.co_id');
        $query = $this->db->get();
        $data['inv']=$query->result();
       $tc = explode(',',$data['inv'][0]->co_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();
                
                $coid = $data['inv'][0]->cod;
                $this->db->select("co_profoma,co_inv");    
                $this->db->from('mes_cus_order');
                $this->db->where('co_id',$coid);
                $query = $this->db->get();
                $per =$query->result();
                
                 
                if($per[0]->co_inv>1){
                    $this->db->select("MIN(`co_id`) as min_id");    
                    $this->db->from('mes_invoice');
                    $this->db->where('co_qto_id',$coid);
                    $query = $this->db->get();
                    $det =$query->result();
                    
                        if($det[0]->min_id!=$insert_id){
                            $first_inv = 0;
                        }else{ $first_inv = 1; }
                }else{
                    $first_inv = 1;
                }
                array_push($data['tm'][1], $per[0]->co_profoma, $first_inv);
             
             
     
        $this->load->view('pages/invpdf',$data);
        }
       
        function  perf_save(){

            $dat = array(
                        'co_id'  => $this->input->post('table'),
                        'c_date'  => date('Y-m-d')
                        
                    );
                    $data = $this->db->insert('mes_performa',$dat);
                    
                           if($this->input->post('inv_amnt')!=""){
                                $this->db->select("co_final_tot");    
                                $this->db->from('mes_cus_order');
                                $this->db->where('co_id', $this->input->post('table'));
                                $query = $this->db->get();
                                $codata = $query->result();
                                    $cal_per = ($this->input->post('inv_amnt')/$codata[0]->co_final_tot)*100;
                                    
                                    $this->db->set('co_profoma', round($cal_per,2));
                                    $this->db->where('co_id', $this->input->post('table'));
                                    $data=$this->db->update('mes_cus_order');
                                    $perc = $cal_per;
                                    
                           }else{
                                    $this->db->set('co_profoma', $this->input->post('inv_value'));
                                    $this->db->where('co_id', $this->input->post('table'));
                                   $data=$this->db->update('mes_cus_order');
                                    $perc = $this->input->post('inv_value');
                           }
       
           
            $bal = 100 - round($perc,2);
            
        if($data){
           $this->crt_new_inv($this->input->post('table'),$bal);
             $dt = "Data Saved";
        }else{
            
             $dt = "Error";
        }

              
         echo json_encode($dt);
         //$this->view_pdf($insert_id);
            }
            
            function crt_new_inv($coid,$bal){
                
                    $this->db->select("*");    
                    $this->db->from('mes_cus_order');
                    $this->db->where('co_id', $coid);
                    $query = $this->db->get();
                    $codata = $query->result();
                    
                $calbal = ($bal / 100)*$codata[0]->co_final_tot;
                
                 $dat = array(
                //'date'  => date('Y-m-d'),
                'co_cus_id'  => $codata[0]->co_cus_id,
                'co_qto_refno'  => "COD",
                'co_qto_id' => $coid,
                'co_cus_refno' => $codata[0]->co_cus_id,
                'co_tot_price'  => $codata[0]->co_tot_price, 
                'co_discount'  => $codata[0]->co_discount,
                'co_final_tot'  => $codata[0]->co_final_tot,
                'co_banking'  =>$codata[0]->co_banking,
                'co_shipping' => $codata[0]->co_shipping,
                'co_nbt'  => $codata[0]->co_nbt, 
                'co_vat'  => $codata[0]->co_vat,
                'co_tax'  => $codata[0]->co_tax,
                'co_trnceport'  => $codata[0]->co_trnceport, 
                'co_tot_qty'  => $codata[0]->co_tot_qty,
                'co_tot_items'  => $codata[0]->co_tot_items,
                'co_other'  => $codata[0]->co_other,
                'co_currancy' => $codata[0]->co_currancy,
                'co_tc' => $codata[0]->co_tc,
                'co_delevery'  => $codata[0]->co_delevery, 
                'co_cuser'  => $codata[0]->co_cuser, 
                'co_remark' => $codata[0]->co_remark,
                'co_cdate'  => date('Y-m-d'),
                'co_inv_qty' => $codata[0]->co_inv_qty
                
            );
       $data = $this->db->insert('mes_invoice',$dat);
        $insert_id = $this->db->insert_id(); 
         
                    $this->db->select("*");    
                    $this->db->from('co_items');
                    $this->db->where('co_itm_co_id', $coid);
                    $query = $this->db->get();
                    $codata = $query->result();
         
       for($i=0; $i<count($codata); $i++){ 
                    
                    $dat = array(
                        'co_itm_item_id'  => $codata[$i]->co_itm_item_id,
                        'co_itm_qto_id'  => $codata[$i]->co_itm_co_id,
                        'co_itm_co_id'  => $insert_id,
                        'co_itm_partno'  => $codata[$i]->co_itm_partno,
                        'service_des' => $codata[$i]->service_des,
                        'co_itm_qty'  => $codata[$i]->co_itm_qty,
                        'co_itm_unit'  => $codata[$i]->co_itm_unit,
                        'co_itm_unitprice'  => $codata[$i]->co_itm_unitprice,
                        'co_itm_currancy' =>  $codata[$i]->co_itm_currancy
                        
                    );
                    $data = $this->db->insert('inv_items',$dat);
                    
                    //$nwqty = $itmdata[0]->inv_qty+$val[$i];
                    
                    $this->db->set('inv_qty', $codata[$i]->inv_qty);
                    $this->db->where('co_itm_id', $codata[$i]->co_itm_id);
                    $data=$this->db->update('co_items');
                    
                   if($codata[$i]->co_itm_item_id!=""){
                       $cal_stock = $this->Common_model->cal_stock("INV",$insert_id,$codata[$i]->co_itm_item_id,$codata[$i]->co_itm_qty,"min",1);
                   }
               
               
            }
   
                return true;
            }
            
        function co_item(){
                $data = $this->Common_model->co_item($this->input->post('po_id'));
            
           $det['item']="";
           $totamnt = 0.00;
                for($i=1; $i<count($data); $i++){
                 if($data[$i][8]<1){
                     $qty = $data[$i][5];
                 }else{$qty = $data[$i][5]-$data[$i][8];}
                    
                    $dat =  "
                                 <tr>
                               <td >QTN".$this->input->post('po_id')."</td>
                               <td >".$data[$i][0]."-".$data[$i][1]."</td>
                               <td >".$data[$i][2]."</td>
                               <td style='text-align: center;'>".$data[$i][3]."</td>
                               <td style='text-align: right;'>".$data[$i][4]."</td>
                                <td style='text-align: right;'>".$qty."</td>
                               <td><input type='text' id=".$data[$i][7]."  name='po_val[]' placeholder='' value='".$qty."' class='col-xs-12 col-sm-12 input-sm form-control-sm form-control txt_val'/ style='text-align: right;' onkeyup='get_cal()'>
                               <input type='hidden'  name='item_id[]' value='".$data[$i][7]."'><input type='hidden'  name='item_item_id[]' value='".$data[$i][6]."'>
                               </td>
                              
                               
                             <td style='text-align: right;'>
                                <div class='hidden-sm hidden-xs btn-group'>
                                 <button class='btn btn-outline-danger btn-sm' onclick='deleteRow(this)'>X</button>
                   
                               </div>
                                </td>
                                </tr>";
                                
                              $det['item'] =$det['item'].$dat;
                              
                              
                }
           
            $this->db->select('mes_customer.cust_company as company,mes_customer.cust_country as country,mes_cus_order.co_cus_refno as customer_ref,mes_cus_order.co_currancy as cur_id,mes_currancy.curr_symble as curr,mes_cus_order.co_discount as podis,
            mes_cus_order.co_banking as bank,mes_cus_order.co_shipping as ship,mes_cus_order.co_trnceport as trans,mes_cus_order.co_tax as tax,mes_cus_order.co_other as other,mes_cus_order.co_tot_price as total,mes_cus_order.co_cus_id as customer_id,
             mes_cus_order.co_inv as pre_inv');
            $this->db->from('mes_cus_order');
            $this->db->where('co_id', $this->input->post('po_id'));
            $this->db->join( "mes_customer", "mes_customer.cust_id = mes_cus_order.co_cus_id", 'inner');
            $this->db->join( "mes_currancy", "mes_currancy.curr_id = mes_cus_order.co_currancy", 'inner');
            $this->db->join( "mes_country", "mes_country.cnty_id = mes_customer.cust_country", 'inner');
            $hasil=$this->db->get();
            $det['customer']= $hasil->result(); 
            
            
            echo json_encode($det);
            
            }
            
    
            function sv_new_inv(){
                //var_dump($_POST);
                
                $item = $this->input->post('item_id');
                $val = $this->input->post('po_val');
                $term = $this->input->post('term');
            
            $this->db->select("co_profoma,co_inv");    
                $this->db->from('mes_cus_order');
                $this->db->where('co_id',$this->input->post('coid'));
                $query = $this->db->get();
                $per =$query->result();
                if($per[0]->co_inv>1){
                    $finaltot = $this->input->post('inv_total')-$this->input->post('dis_total')+$this->input->post('nbt')+$this->input->post('vat');
                }else{
                    $finaltot = $this->input->post('final1');
                }
            
            $length = count($term);
            $tc="";
            for ($i = 1; $i < $length; $i++) {
              $tc = $tc.",".$term[$i];
             
            } 
        
        $dat = array(
                //'date'  => date('Y-m-d'),
                'co_cus_id'  => $this->input->post('cus_id'),
                'co_qto_refno'  => "COD",
                'co_qto_id' => $this->input->post('coid'),
                'co_cus_refno' => $this->input->post('cus_id'),
                'co_tot_price'  => $this->input->post('inv_total'), 
                'co_discount'  => $this->input->post('dis_total'),
                'co_final_tot'  => $finaltot,
                'co_banking'  => $this->input->post('bank_fee'),
                'co_shipping' => $this->input->post('ship'),
                'co_nbt'  => $this->input->post('nbt'), 
                'co_vat'  => $this->input->post('vat'),
                'co_tax'  => $this->input->post('tax_pc'),
                'co_trnceport'  => $this->input->post('trfee'), 
                'co_tot_qty'  => $this->input->post('inv_tot_item'),
                'co_tot_items'  => count($this->input->post('item_id')),
                'co_other'  => $this->input->post('other_amnt'),
                'co_currancy' => $this->input->post('curid'),
                'co_tc' => $tc,
                'co_delevery'  => "0", 
                'co_cuser'  => 'testuser', 
                'co_remark' => $this->input->post('remark'),
                'co_cdate'  => date('Y-m-d H:i:s'),
                'co_inv_qty' => $this->input->post('inv_tot_item')
                
            );
        $data = $this->db->insert('mes_invoice',$dat);
        $insert_id = $this->db->insert_id(); 
         
         
       for($i=0; $i<count($item); $i++){
                if($item[$i]!=""){
                    $this->db->select("*");    
                    $this->db->from('co_items');
                    $this->db->where('co_itm_id', $item[$i]);
                    $query = $this->db->get();
                    $itmdata = $query->result(); 
                    
                    $dat = array(
                        'co_itm_item_id'  => $itmdata[0]->co_itm_item_id,
                        'co_itm_qto_id'  => $itmdata[0]->co_itm_co_id,
                        'co_itm_co_id'  => $insert_id,
                        'co_itm_partno'  => $itmdata[0]->co_itm_partno,
                        'service_des' => $itmdata[0]->service_des,
                        'co_itm_qty'  => $val[$i],
                        'co_itm_unit'  => $itmdata[0]->co_itm_unit,
                        'co_itm_unitprice'  => $itmdata[0]->co_itm_unitprice,
                        'co_itm_currancy' =>  $this->input->post('curid')
                        
                    );
                    $data = $this->db->insert('inv_items',$dat);
                    
                    $nwqty = $itmdata[0]->inv_qty+$val[$i];
                    
                    $this->db->set('inv_qty', $nwqty);
                    $this->db->where('co_itm_id', $item[$i]);
                    $data=$this->db->update('co_items');
                   
                }
            }
   
            $this->db->select("co_inv_qty,co_inv");    
                    $this->db->from('mes_cus_order');
                    $this->db->where('co_id', $this->input->post('coid'));
                    $query = $this->db->get();
                    $invcnt = $query->result();
                    $newinv = $invcnt[0]->co_inv+1;
                    $newinvqty = $invcnt[0]->co_inv_qty+$this->input->post('inv_tot_item');
   
            $this->db->set('co_inv', $newinv);
            $this->db->set('co_inv_qty',$newinvqty);
            $this->db->where('co_id', $this->input->post('coid'));
            $data=$this->db->update('mes_cus_order');
            
              $dt = "ins";
         echo json_encode($insert_id);
         //$this->view_pdf($insert_id);
            }
            
            function coinfo(){
                
                $id = $this->input->post('id');
    	    
        	    $this->db->from('mes_cus_order'); 
                $this->db->select("mes_cus_order.*, mes_performa.*, mes_cus_order.co_id as pcoid");
                $this->db->join( "mes_performa", "mes_performa.co_id = mes_cus_order.co_id", 'left');
                $this->db->where('mes_cus_order.co_id',$id);
                $detail = $this->db->get();
                $data = $detail->result();
                
                 if($data[0]->co_profoma != 0 ){$perf = "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='print'  ><a href='".base_url()."print_proforma/".$data[0]->pcoid."' target='_blank'>Print Proforma Invoice</a></button><br>";
                     
                 }else{
                     $perf = "";
                 }
           
    	        $det ='';
                $det .='
              
                       <button type="button" class="btn btn-outline-secondary btn-sm" type="button" id="print"  ><a href="'.base_url().'print_co/'.$data[0]->pcoid.'" target="_blank">Print Customer Order</a></button></br>
                        
                       </br>'; 
                       $det .=$perf;
               
               
                $this->db->from('mes_invoice'); 
                $this->db->select("*");
                $this->db->where('co_qto_id',$id);
                $detaiinv = $this->db->get();
                $datainv = $detaiinv->result();
                
                $this->db->from('inv_items'); 
                   $this->db->select("*");
                   $this->db->where('co_itm_qto_id',$id);
                   $this->db->where('service_des!=','');
                   $deta = $this->db->get();
                   $serv = $deta->result();
                
                       for($i=0; $i<$detaiinv->num_rows(); $i++){
                           
                            $this->db->from('inv_items'); 
                           $this->db->select("*");
                           $this->db->where('co_itm_co_id',$datainv[$i]->co_id);
                           $this->db->where('service_des!=','');
                           $deeta = $this->db->get();
                           $service = $deeta->result();
                         
                         
                          $det .=' 
                            <button type="button" class="btn btn-outline-secondary btn-sm" type="button" id="print"  ><a href="'.base_url().'print_invco/'.$datainv[$i]->co_id.'" target="_blank">Print Invoice"'.$datainv[$i]->co_code.$datainv[$i]->co_id.'"</a></button><br>
                           ';
                           
                           
                           if($datainv[$i]->co_tot_items==$deeta->num_rows()){
                                $det .='<button type="button" class="btn btn-outline-secondary btn-sm" type="button" id="'.$datainv[$i]->co_id.'"><a href="'.base_url().'print_job_con/'.$datainv[$i]->co_id.'" target="_blank">Print Job Confirmation</a></button><br>
                                   ';
                           }elseif($deeta->num_rows()>0){
                               $det .='<button type="button" class="btn btn-outline-secondary btn-sm" type="button" id="'.$datainv[$i]->co_id.'"><a href="'.base_url().'print_job_con/'.$datainv[$i]->co_id.'" target="_blank">Print Job Confirmation</a></button>
                                   ';
                                   
                                    if($datainv[$i]->do_date=="0000-00-00"){
                               $det .='<button type="button" class="btn btn-outline-secondary btn-sm" type="button" id="'.$datainv[$i]->co_id.'" onclick="crt_del_order(this.id)" >Create Delivery Note"'.$datainv[$i]->do_code.$datainv[$i]->co_id.'"</button><br>
                               ';
                               }else{
                                   $det .='<button type="button" class="btn btn-outline-secondary btn-sm" type="button" id="print"  ><a href="'.base_url().'print_del_ord/'.$datainv[$i]->co_id.'" target="_blank">Delivery Note"'.$datainv[$i]->do_code.$datainv[$i]->co_id.'"</a></button><br>
                               ';
                               }
                                   
                           }else{
                                if($datainv[$i]->do_date=="0000-00-00"){
                               $det .='<button type="button" class="btn btn-outline-secondary btn-sm" type="button" id="'.$datainv[$i]->co_id.'" onclick="crt_del_order(this.id)" >Create Delivery Note"'.$datainv[$i]->do_code.$datainv[$i]->co_id.'"</button><br>
                               ';
                               }else{
                                   $det .='<button type="button" class="btn btn-outline-secondary btn-sm" type="button" id="print"  ><a href="'.base_url().'print_del_ord/'.$datainv[$i]->co_id.'" target="_blank">Delivery Note"'.$datainv[$i]->do_code.$datainv[$i]->co_id.'"</a></button><br>
                               ';
                               }
                           }
                           
                      
                           
                        //   if($deta->num_rows()>0){
                        // //        $det .='<button type="button" class="btn btn-outline-secondary btn-sm" type="button" id="'.$datainv[$i]->co_id.'"><a href="'.base_url().'print_job_con/'.$datainv[$i]->co_id.'" target="_blank">pPrint Job Confirmation</a></button><br>
                           //        </br>';
                            // }
                           
                           $det .='</br>';
                           
                       }
                   
                            
                     if($deta->num_rows()>0){
                          $det .=' 
                        <button type="button" class="btn btn-outline-secondary btn-sm" type="button" id="print"  ><a href="#" target="_blank">Create Job Card</a></button><br>
                       </br>';
                     }       
                	
                echo json_encode($det);
                
            }
            
            function delivery_ord($insert_id){
                 $this->db->select("*");    
        $this->db->from('inv_items');
        $this->db->where('co_itm_co_id', $insert_id);
        $this->db->join('mes_item', 'inv_items.co_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', 'inv_items.co_itm_partno  = mes_partnumber.part_id');
        $this->db->join('mes_unit', 'inv_items.co_itm_unit   = mes_unit.unit_id');
        $this->db->join('mes_currancy', 'inv_items.co_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', 'inv_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]->co_itm_unitprice,$dataa[$i]->co_itm_qty,$dataa[$i]->co_itm_id,$dataa[$i]->co_itm_qto_id);    
                }else{
                    
                    if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->part_no!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[$i]->model_name!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name.$spece2.$dataa[$i]->part_no.$spece1.$dataa[$i]->spec_spec;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->co_itm_unitprice,$dataa[$i]->co_itm_qty,$dataa[$i]->co_itm_id,$dataa[$i]->co_itm_qto_id); 
                }
        }
        $data['inv_itm'] = $inv;
        
        
        $this->db->select("mes_invoice.*,mes_cus_order.*,mes_customer.*, mes_currancy.*, mes_country.*,mes_invoice.co_tot_qty as dlnqty, mes_invoice.co_qto_id as codrid,mes_invoice.co_qto_refno as codrcode,mes_invoice.co_id as inv_id");    
        $this->db->from('mes_invoice');
        $this->db->where('mes_invoice.co_id', $insert_id);
        $this->db->join('mes_customer', 'mes_invoice.co_cus_id  = mes_customer.cust_id');
        $this->db->join('mes_currancy', 'mes_invoice.co_currancy   = mes_currancy.curr_id');
        $this->db->join('mes_country', 'mes_customer.cust_country   = mes_country.cnty_id');
        $this->db->join('mes_cus_order', 'mes_invoice.co_qto_id   = mes_cus_order.co_id');
        $query = $this->db->get();
        $data['inv']=$query->result();
        
            if($data['inv'][0]->do_adrs!=0){
                  $this->db->select("*");    
                    $this->db->from('cust_address');
                    $this->db->where('cusad_id',$data['inv'][0]->do_adrs);
                    $query = $this->db->get();
                    $det =$query->result();
                    $adrs = $det[0]->cusad_add1.$det[0]->cusad_add2.$det[0]->cusad_add3;
            }else{
                $adrs = $data['inv'][0]->cust_addres.$data['inv'][0]->cust_add1.$data['inv'][0]->cust_add2;
            }
        array_push($data['inv'],$adrs);
       $tc = explode(',',$data['inv'][0]->co_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();
                
                $coid = $data['inv'][0]->co_qto_id;
                $this->db->select("co_profoma,co_inv");    
                $this->db->from('mes_cus_order');
                $this->db->where('co_id',$coid);
                $query = $this->db->get();
                $per =$query->result();
                
                 
                if($per[0]->co_inv>1){
                    $this->db->select("MIN(`co_id`) as min_id");    
                    $this->db->from('mes_invoice');
                    $this->db->where('co_qto_id',$coid);
                    $query = $this->db->get();
                    $det =$query->result();
                    
                        if($det[0]->min_id!=$insert_id){
                            $first_inv = 0;
                        }else{ $first_inv = 1; }
                }else{
                    $first_inv = 1;
                }
                array_push($data['tm'][1], $per[0]->co_profoma, $first_inv);
             
        $this->load->view('pages/dopdf',$data);
            }        
            
            function get_del_ad(){
                           $this->db->from('mes_invoice'); 
                           $this->db->select("*");
                           $this->db->where('co_id',$this->input->post('id'));
                           $this->db->join( "cust_address", "cust_address.cusad_cusid = mes_invoice.co_cus_id", 'left');
                           $dat = $this->db->get();
                           $data = $dat->result();
                           
                            $cus =  $data[0]->co_cus_id;
                                    $this->db->from('mes_customer'); 
                                   $this->db->select("*");
                                   $this->db->where('cust_id',$cus);
                                   $ad = $this->db->get();
                                   $adrs = $ad->result();
                           $cust_addres = $adrs[0]->cust_addres." ".$adrs[0]->cust_add1." ".$adrs[0]->cust_add2;
                         $addrs[] = array(0,$cust_addres);
                         
                          if(isset($data[0]->cusad_id)){
                                 for($i=0; $i<count($data); $i++){
                                  $addrs[] = array($data[$i]->cusad_id,$data[$i]->cusad_add1." ".$data[$i]->cusad_add2." ".$data[$i]->cusad_add3);
                              }  
                              
                          }
                          
                          $ret =' <input type="hidden" name="del_id" value="'.$this->input->post('id').'">
                                 
                          <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="adrs" name="adrs">';
                          
                        for($i=0; $i<count($addrs); $i++){
                            $ret .= '
                                            <option value="'.$addrs[$i][0].'">'.$addrs[$i][1].'</option>
                                        
                                  ';
                        }  
                         
                         $ret .= ' </select></div>
                            </div>' ;
                 echo json_encode($ret);         
            }
            
            function dispatch(){
                
                $data['content'] = "pages/dispatch";        
                $this->load->view('template',$data);
            }
            
            function show_all_dispatch() {
            $this->db->select('*');
            $this->db->from('mes_invoice');
            $this->db->join( "mes_customer", "mes_customer.cust_id = mes_invoice.co_cus_id", 'inner');
            $this->db->join( "mes_country", "mes_country.cnty_id = mes_customer.cust_country", 'inner');
            $this->db->where('do_date!=',"0000-00-00");
            $this->db->order_by("mes_invoice.co_id","desc");
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            //$data['content'] = ('pages/allinvoice');                       
            //$this->load->view('template',$data);
            
           // $link = site_url('print_del_ord/').$data[$i]->co_id;
            
             //$det[0]="<a href='$link'><button type='button' class='btn btn-outline-danger' >Create New Customer Order</button></a></br>";
            //$det[0]="";
            $det[1] ="<tr>
														<th scope='col'>ID</th>
														<th>DO Number</th>
														<th>INV Number</th>
														<th>Customer</th>
														<th>Customer Ref. No.</th>
														<th>Item</th>
														<th>Qty</th>
														
														<th>Date</th>
													    <th>Status</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                    
                            
                        
                    
                        if($data[$i]->do_confrm!=1){
                        $sts = "<td style='color:#c6ab1b85; font-size:25px; text-align: center;'>&#9679;</td>"; 
                        }else{
                        $sts = "<td style='color:#00800085; font-size:25px; text-align: center;'>&#9679;</td>";  }
                        
                   
                          $authbutn = "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->co_id."'  onclick='do_confirm(this.id)' >Authorize</button>";
                          if($data[$i]->do_confrm==1){$abtn = "  Authorized";}else{$abtn = $authbutn;}
                    $vw_btn ="<a href ='".base_url()."print_del_ord/".$data[$i]->co_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View DO</button></a>$abtn";
                    //$vw_btn ="<a href='$link'><button type='button' class='btn btn-outline-secondary btn-sm' >View DO</button></a>";
                    
                   
                    $rec = 'Complete';
                    
                    $x = $i+1;
                    $dat =  "
                                 <tr scope='row'>
                                 <td >".$x."</td>
                                 <td >".$data[$i]->do_code.$data[$i]->co_id."</td>
                                 <td >".$data[$i]->co_code.$data[$i]->co_id."</td>
                                 <td>".$data[$i]->cust_company."<br>".$data[$i]->cust_addres."<br>".$data[$i]->cust_name."<br>".$data[$i]->cust_contact."<br>".$data[$i]->cust_conperson."</td>
                                 <td>".$data[$i]->co_cus_refno."</td>
                                 
                                 <td>".($data[$i]->co_tot_items)."</td>
                                <td style='text-align: right;'>".$data[$i]->co_tot_qty."</td>
                               
                               
                                <td width='70px'>".$data[$i]->do_date."</td>
                               
                                $sts
                               
                                
                                
                                <td>$vw_btn</td>
                               
                              
                              </tr>";
                              $x++;
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
            //echo json_encode($data);
        }
        
        function do_auth(){
            
            $id = $this->input->post('doid');
            
            $this->db->set('do_confrm', 1);
            $this->db->set('do_confm_user', 'admin');
            $this->db->set('do_confm_date', date("Y-m-d"));
                
                $this->db->where('co_id', $this->input->post('doid'));
                $result=$this->db->update('mes_invoice');
                
                if($result){
                    $ret = "DO Authorized";
                        $this->db->select("*");    
                        $this->db->from('inv_items');
                        $this->db->where('co_itm_co_id', $this->input->post('doid'));
                        $query = $this->db->get();
                        $sdata = $query->result();
                             for($i=0; $i<$query->num_rows(); $i++){
                                 $data = $this->Common_model->cal_stock("DVN",$this->input->post('doid'),$sdata[$i]->co_itm_item_id,$sdata[$i]->co_itm_qty,"min",0); 
                             }
                        
                }else{
                    $ret = "Please Re Check";
                }
                
                echo json_encode($ret);
        }
        
       function crt_delivery_ord(){
            
                $this->db->set('do_date',$this->input->post('del_date'));
                $this->db->set('do_adrs',$this->input->post('adrs'));
                $this->db->where('co_id', $this->input->post('del_id'));
                $result=$this->db->update('mes_invoice');
                
                if($result){
                    $ret = "Done";
                }else{
                    $ret = "Please Re Check";
                }
                
                echo json_encode($ret);
       }
       
       function view_jobcon($insert_id){
            $this->db->select("*");    
        $this->db->from('inv_items');
        $this->db->where('co_itm_co_id', $insert_id);
        $this->db->join('mes_item', 'inv_items.co_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', 'inv_items.co_itm_partno  = mes_partnumber.part_id');
        $this->db->join('mes_unit', 'inv_items.co_itm_unit   = mes_unit.unit_id');
        $this->db->join('mes_currancy', 'inv_items.co_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', 'inv_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]->co_itm_unitprice,$dataa[$i]->co_itm_qty,$dataa[$i]->co_itm_id,$dataa[$i]->co_itm_qto_id);    
                }else{
                    
                    if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->part_no!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[$i]->model_name!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name.$spece2.$dataa[$i]->part_no.$spece1.$dataa[$i]->spec_spec;
                    //$inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->co_itm_unitprice,$dataa[$i]->co_itm_qty,$dataa[$i]->co_itm_id,$dataa[$i]->co_itm_qto_id); 
                }
        }
        $data['inv_itm'] = $inv;
        
        
        //$this->db->select("mes_invoice.*,mes_cus_order.*,mes_customer.*, mes_currancy.*, mes_country.*,mes_invoice.co_tot_qty as dlnqty");    
        $this->db->select("mes_invoice.*,mes_cus_order.*,mes_customer.*, mes_currancy.*, mes_country.*,mes_invoice.co_tot_qty as dlnqty, mes_invoice.co_qto_id as codrid,mes_invoice.co_qto_refno as codrcode,mes_invoice.co_id as inv_id");    
        $this->db->from('mes_invoice');
        $this->db->where('mes_invoice.co_id', $insert_id);
        $this->db->join('mes_customer', 'mes_invoice.co_cus_id  = mes_customer.cust_id');
        $this->db->join('mes_currancy', 'mes_invoice.co_currancy   = mes_currancy.curr_id');
        $this->db->join('mes_country', 'mes_customer.cust_country   = mes_country.cnty_id');
        $this->db->join('mes_cus_order', 'mes_invoice.co_qto_id   = mes_cus_order.co_id');
        $query = $this->db->get();
        $data['inv']=$query->result();
       $tc = explode(',',$data['inv'][0]->co_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();
                
                $coid = $data['inv'][0]->co_qto_id;
                $this->db->select("co_profoma,co_inv");    
                $this->db->from('mes_cus_order');
                $this->db->where('co_id',$coid);
                $query = $this->db->get();
                $per =$query->result();
                
                 
                if($per[0]->co_inv>1){
                    $this->db->select("MIN(`co_id`) as min_id");    
                    $this->db->from('mes_invoice');
                    $this->db->where('co_qto_id',$coid);
                    $query = $this->db->get();
                    $det =$query->result();
                    
                        if($det[0]->min_id!=$insert_id){
                            $first_inv = 0;
                        }else{ $first_inv = 1; }
                }else{
                    $first_inv = 1;
                }
                array_push($data['tm'][1], $per[0]->co_profoma, $first_inv);
             
        $this->load->view('pages/job_con_pdf',$data);
       }
                 
        function crt_job_card(){
             //var_dump($_POST);
            
            $term =  $this->input->post('term');
    	   $length = count($term);
              $tc="";
            for ($i = 0; $i < $length; $i++) {
              $tc = $tc.",".$term[$i];
            
            }
            
            $data = array(
                'job_techid' => $tc ,
                 'job_Remarks' => $this->input->post('remark'),
                 'job_co_id' => $this->input->post('tablejob'),
                  'job_sdate' => $this->input->post('sdate'),
                   'job_enddate' => $this->input->post('edste'),
                   'job_cuser' => "admin",
                'job_cdate' => (date('Y-m-d'))
            );
           $result = $this->db->insert('mes_int_job',$data);
           $lastid = $this->db->insert_id();
            echo json_encode("done");
        }
        
        function jbinfo(){
        $id = $this->input->post('id');
            $this->db->from('mes_int_job');
            $this->db->select("*");
            $this->db->where('job_co_id', $id);
            $detail = $this->db->get();
            $data = $detail->result();
            
            $det="";
            $det .= "<a href ='".base_url()."co_job_pdf/".$data[0]->job_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>";
            
            $det .= "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_tech/".$data[0]->job_id."' onclick='edit_view(this.id)' data-target='#Modal_edit'>Edit</button>
                                 <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='edit_tech/".$data[0]->job_id."' onclick='edit_view(this.id)' data-target='#Modal_edit'>Cancel</button></br>
                                 ";
                                 
                    $this->db->select("int_job_items.jobitm_itmid");    
                    $this->db->from('int_job_items');
                    $this->db->join( "mes_int_job", "int_job_items.jobitm_jobid = mes_int_job.job_id", 'left');
                    $this->db->join( "mes_item", "int_job_items.jobitm_itmid = mes_item.item_id", 'left');
                    $this->db->where('int_job_items.jobitm_jobid', $data[0]->job_id);
                    $this->db->where('int_job_items.jobitm_itmid!=',0 );
                    $this->db->where('mes_item.item_cat',10 );
                    $query = $this->db->get();
                            if ($query->num_rows() > 0) { $det .= "<a href ='".base_url()."co_tool_pdf/".$data[0]->job_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >Tool Note</button></a>";
                                
                            }else{ $det .="<button type='button' class='btn btn-outline-secondary btn-sm' >No Tools</button>"; }             
                                 
                                 
                    $this->db->select("int_job_items.jobitm_itmid");    
                    $this->db->from('int_job_items');
                    $this->db->join( "mes_int_job", "int_job_items.jobitm_jobid = mes_int_job.job_id", 'left');
                    $this->db->join( "mes_item", "int_job_items.jobitm_itmid = mes_item.item_id", 'left');
                    $this->db->where('int_job_items.jobitm_jobid', $data[0]->job_id);
                    $this->db->where('int_job_items.jobitm_itmid!=',0 );
                    $this->db->where('mes_item.item_cat!=',10 );
                    $query = $this->db->get();                 
            
            if($data[0]->job_complete==1){
                       $cmp = "<td style='color:Green; font-size:25px; text-align: center;'>&#9679;</td>";
                       $det .= "<button type='button' class='btn btn-outline-secondary btn-sm' id='".$data[0]->job_id."' ><a href ='".base_url()."co_job_inv_pdf/".$data[0]->job_id."' target='_blank'>Completed</a></button>";
                       
                    }else{
                            $det .= "<td style='color:#c6ab1b85; font-size:25px; text-align: center;'>&#9679;</td>";
                        
                       if ($query->num_rows() > 0) { 
                           $det .= "<a href ='".base_url()."co_material_pdf/".$data[0]->job_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >Material Note</button></a>";
                                
                        }else{
                            $det .="<button type='button' class='btn btn-outline-secondary btn-sm' >No Materials</button>"; }
                            $det .= "<a href ='".base_url()."cojob_addpro/".$data[0]->job_id."' > <button type='button' class='btn btn-outline-secondary btn-sm' >Add Tools & Equipment</button></a>
                                  <a href ='".base_url()."co_job_comp/".$data[0]->job_id."' > <button type='button' class='btn btn-outline-secondary btn-sm' >Complete</button></a>";
                  
                   }
            
            echo json_encode($det);
        }
        
        function coaddproduct_job($insert_id){
           
           $data['jobid'] = $insert_id;
           
           
           
           $columns_arr= array("*");            
            $where_arr = array('job_id ='=> $insert_id);                     
            $jobdt = $this->General->getData('mes_int_job',$columns_arr,$where_arr);
            $data['jobdetals'] = $jobdt;
           
            $columns_arr= array("*");            
            $where_arr = array('tech_status ='=> 1);                     
            $resultagentt = $this->General->getData('mes_technician',$columns_arr,$where_arr);
            $data['tccatdetailst'] = $resultagentt;
            
            $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();
           
        $data['content'] = ('pages/add_cojobitem'); 
		$this->load->view('template',$data); 
		
            
            
        }
        
        function newco_jbitem(){
    	            
    	          //print_r($_POST);
    	          $item = $this->input->post('serid');
    	          $qty = $this->input->post('serqty');
                    $desc = $this->input->post('serdesc');
            
            
             for($i=0; $i<count($item); $i++){
                    $this->db->select("item_unit,item_selling_price");    
                    $this->db->from('mes_item');
                    $this->db->where('item_id', $item[$i]);
                    $query = $this->db->get();
                    $itmdata = $query->result();
                 
                  $dat = array(
                        'jobitm_jobid'  => $this->input->post('j_id'),
                        'jobitm_des' => $desc[$i],
                        'jobitm_itmid' => $item[$i],
                        'jobitm_qty'  => $qty[$i], 
                        'jobitm_unt'  => $itmdata[0]->item_unit,
                        'jobitm_rate'  => $itmdata[0]->item_selling_price,
                        'jobitm_tot'  => $itmdata[0]->item_selling_price*$qty[$i],
                        'jobitm_newitm'  => 1
                    );
                $data = $this->db->insert('int_job_items',$dat);
                
                //$this->Common_model->cal_stock("JBC",$this->input->post('j_id'),$item[$i],$qty[$i],"min",1);
             }
            
            
      
            $data = "Added";
       
        
       echo json_encode($data);
        }
        
        function tool_job($insert_id){
            
        $this->db->select("*");    
        $this->db->from('int_job_items');
        $this->db->join( "mes_int_job", "int_job_items.jobitm_jobid = mes_int_job.job_id", 'left');
        $this->db->join( "mes_item", "int_job_items.jobitm_itmid = mes_item.item_id", 'left');
        $this->db->join( "mes_unit", "int_job_items.jobitm_unt = mes_unit.unit_id", 'left');
        $this->db->where('int_job_items.jobitm_jobid', $insert_id );
        $this->db->where('int_job_items.jobitm_itmid!=',0 );
        $this->db->where('mes_item.item_cat',10 );
        $query = $this->db->get();
                if ($query->num_rows() > 0) {
                    
        $dataa=$query->result();
        $data['inv']=$query->result();
        
       
        $tec = explode(',',$data['inv'][0]->job_techid);
        $length = count($tec);
        
   
        
         for($i=0; $i<count($data['inv']); $i++){
                   
                    $inv[] = array($data['inv'][$i]->jobitm_des,$data['inv'][$i]->unit_symble,$data['inv'][$i]->jobitm_qty); 
                
        }
         $data['inv_itm'] = $inv;
        
        
        $tm[]="";
        for ($i = 1; $i < $length; $i++) {
                $this->db->select("*");    
                $this->db->from('mes_technician');
                $this->db->where('tech_id', $tec[$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/toolpdf',$data);
                }else{
                  echo "No data";  
                }
            
        }
        
        function met_job($insert_id){
            
        $this->db->select("*");    
        $this->db->from('int_job_items');
        $this->db->join( "mes_int_job", "int_job_items.jobitm_jobid = mes_int_job.job_id", 'inner');
        $this->db->join( "mes_item", "int_job_items.jobitm_itmid = mes_item.item_id", 'inner');
        $this->db->join( "mes_unit", "int_job_items.jobitm_unt = mes_unit.unit_id", 'left');
        $this->db->where('int_job_items.jobitm_jobid', $insert_id );
        $this->db->where('int_job_items.jobitm_itmid!=',0 );
        $this->db->where('mes_item.item_cat!=',10 );
        $query = $this->db->get();
        $dataa=$query->result();
        $data['inv']=$query->result();
        
        $tec = explode(',',$data['inv'][0]->job_techid);
        $length = count($tec);
        
   
        
         for($i=0; $i<count($data['inv']); $i++){
                   
                    $inv[] = array($data['inv'][$i]->jobitm_des,$data['inv'][$i]->unit_symble,$data['inv'][$i]->jobitm_qty); 
                
        }
         $data['inv_itm'] = $inv;
        
        
        $tm[]="";
        for ($i = 1; $i < $length; $i++) {
                $this->db->select("*");    
                $this->db->from('mes_technician');
                $this->db->where('tech_id', $tec[$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/materialpdf',$data);
            
        }
        
        function view_job_com($id){
        
         $this->db->from('mes_int_job');
            $this->db->select("job_complete");
            $this->db->where('job_complete', 1);
            $this->db->where('job_id', $id);
            $detail = $this->db->get();
            $data = $detail->result(); 
            
            if($detail->num_rows() > 0){
                $ret = "Already Completed";
                $this->index();
            }else{
                 $data['id'] = $id;
                $data['content'] = ('pages/co_job_comp'); 
		        $this->load->view('template',$data); 
            }
        
       
    }
    
        function viewjob_product(){
        
        $this->db->select("*");    
        $this->db->from('int_job_items');
        $this->db->join( "mes_int_job", "int_job_items.jobitm_jobid = mes_int_job.job_id", 'left');
        $this->db->join( "mes_item", "int_job_items.jobitm_itmid = mes_item.item_id", 'left');
        $this->db->join('mes_unit', 'mes_item.item_unit   = mes_unit.unit_id');
        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'inner');
        $this->db->where('int_job_items.jobitm_jobid', $this->input->post('jo_id'));
        $this->db->where('int_job_items.jobitm_itmid!=',0 );
        $this->db->where('int_job_items.jobitm_newitm!=',0 );
        $query = $this->db->get();
        $data = $query->result();
        
        
        $det['item']="";
          
                for($i=0; $i<count($data); $i++){
                    if($data[$i]->item_cat!="10"){
                        $ret = "0";  $dis = "";
                    }else{$ret = $data[$i]->jobitm_qty; $dis = "readonly";}
                    
                  $dat =  "
                                 <tr>
                               <td >".($i+1)."</td>
                               <td >".$data[$i]->cat_shortcode."-".$data[$i]->item_catcount."</td>
                               <td >".$data[$i]->jobitm_des."</td>
                               <td style='text-align: center;'>".$data[$i]->unit_symble."</td>
                               <td style='text-align: right;'>".$data[$i]->jobitm_rate."</td>
                                <td style='text-align: right;'>".$data[$i]->jobitm_qty."</td>
                               <td><input type='text' id=".$data[$i]->jobitm_id."  name='po_val[]' placeholder='' value='".$ret."' ".$dis." class='col-xs-12 col-sm-12 input-sm form-control-sm form-control txt_val'/ style='text-align: right;' onkeyup='get_cal()'>
                               <input type='hidden'  name='item_id[]' value='".$data[$i]->jobitm_id."'>
                               </td>
                              
                                </tr>";
                                
                              $det['item'] =$det['item'].$dat;
                }  
                echo json_encode($det);
    }
    
    function complete_job(){
                    $item = $this->input->post('item_id');
                    $val = $this->input->post('po_val');
                    
                $this->db->from('mes_int_job');
            $this->db->select("job_complete");
            $this->db->where('job_complete', 1);
            $this->db->where('job_id', $this->input->post('job_id'));
            $detail = $this->db->get();
            $data = $detail->result(); 
            
            if($detail->num_rows() > 0){
                $ret = "Already Completed";
            }else{
                for($i=0; $i<count($item); $i++){
                if($item[$i]!=""){
                    $this->db->set('retn_qty', $val[$i]);
                    $this->db->where('jobitm_id', $item[$i]);
                    $result=$this->db->update('int_job_items');
                    
                            $this->db->from('int_job_items');
                            $this->db->select("*");
                            $this->db->where('jobitm_id', $item[$i]);
                            $detai = $this->db->get();
                            $dat = $detai->result();
                        $nqty = $dat[0]->jobitm_qty - $val[$i];
                        
                     $data = $this->Common_model->cal_stock("JBC",$dat[0]->jobitm_jobid,$dat[0]->jobitm_itmid,$nqty,"min",0);
                }
            
        }
                
                
                $this->db->set('job_complete', 1);
                $this->db->set('job_comp_date', $this->input->post('codate'));
                $this->db->where('job_id', $this->input->post('job_id'));
                $result=$this->db->update('mes_int_job');
                
                if($result){
                    $ret = "Job Completed";
                }else{
                    $ret = "Please Re enter all details";
                }
            }
        
        
                echo json_encode($ret);
    }
    
    function viewcom_job($insert_id){
       
        $this->db->select("job_co_id");    
        $this->db->from('mes_int_job');
        $this->db->where('job_id', $insert_id);
        $query = $this->db->get();
        $jbdata=$query->result();
        $inv_id = $jbdata[0]->job_co_id;
        
        $this->db->select("*");    
        $this->db->from('inv_items');
        $this->db->where('co_itm_qto_id', $inv_id);
        $this->db->where('service_des!=',"");
        $this->db->join('mes_service_type', 'inv_items.service_des   = mes_service_type.serv_id');
        $query = $this->db->get();
        $service = $query->result();
       
       for($i=0; $i<count($service); $i++){
           $inv[] = array("","",$service[$i]->serv_name,"LKR",$service[$i]->co_itm_unitprice,$service[$i]->co_itm_qty,"",$insert_id);   
       }
       
        $this->db->select("*");    
        $this->db->from('int_job_items');
        $this->db->where('jobitm_jobid', $insert_id);
        //$this->db->where('jobitm_newitm!=', 1);
        $query = $this->db->get();
        $dt=$query->result();
        
        //$inv[] = array();
        
         for($i=0; $i<count($dt); $i++){
                if($dt[$i]->jobitm_itmid==0){
                    $inv[] = array("","",$dt[$i]->jobitm_des,"LKR",$dt[$i]->jobitm_rate,$dt[$i]->jobitm_qty,"",$insert_id);    
                }else{
                    $this->db->select("*");
                    $this->db->from('mes_item');
                    $this->db->where('item_id', $dt[$i]->jobitm_itmid,'left');
                    $this->db->join('item', 'item.itm_id   = mes_item.item_name','left');
                    $this->db->join('mes_model', 'mes_item.item_model   = mes_model.model_id','left');
                    $this->db->join('mes_brand', 'mes_item.item_brand   = mes_brand.brand_id','left');
                    $this->db->join('mes_partnumber', 'mes_item.item_id  = mes_partnumber.part_itmno','left');
                    $this->db->join('mes_unit', 'mes_item.item_unit   = mes_unit.unit_id','left');
                    $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'left');
                    $this->db->join( "mes_spec", "mes_spec.spec_id  = mes_item.item_spec",'left');
                    $query = $this->db->get();
                    $dataa=$query->result();
                    
                    if($dataa[0]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[0]->part_no!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[0]->model_name!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[0]->brand_name.$spece.$dataa[0]->itm_name."\n".$dataa[0]->model_name.$spece2.$dataa[0]->part_no.$spece1.$dataa[0]->spec_spec;
                    $inv[] = array($dataa[0]->cat_shortcode,$dataa[0]->item_catcount,$itm,"LKR",$dt[$i]->jobitm_rate,$dt[$i]->jobitm_qty,$dt[$i]->jobitm_itmid,$insert_id); 
                }
        }
        
        
        $data['inv_itm'] = $inv;
        
        
        $this->db->select("*");    
        $this->db->from('mes_int_job');
        $this->db->where('job_id', $insert_id);
        $query = $this->db->get();
        $data['inv']=$query->result();
    
        
        $data['tm'][0] = "";
        
                $this->db->select("*");    
                $this->db->from('mes_profile');
                $query = $this->db->get();
                $data['tm'][1] =$query->result();
                
                
        $this->load->view('pages/jobcomp_pdf',$data);
    }
    
    function viewpdf_job($insert_id){
        
        $this->db->select("job_co_id");    
        $this->db->from('mes_int_job');
        $this->db->where('job_id', $insert_id);
        $query = $this->db->get();
        $jbdata=$query->result();
        $inv_id = $jbdata[0]->job_co_id;
        
        $this->db->select("*");    
        $this->db->from('inv_items');
        $this->db->where('co_itm_qto_id', $inv_id);
        $this->db->where('service_des!=',"");
        $this->db->join('mes_service_type', 'inv_items.service_des   = mes_service_type.serv_id');
        $this->db->join( "mes_unit", "inv_items.co_itm_unit = mes_unit.unit_id", 'left');
        $query = $this->db->get();
        $service = $query->result();
       
       for($i=0; $i<count($service); $i++){
           $inv[] = array($service[$i]->serv_name,$service[$i]->unit_symble,$service[$i]->co_itm_qty);   
       }
            
        $this->db->select("*");    
        $this->db->from('mes_int_job');
        $this->db->join( "int_job_items", "int_job_items.jobitm_jobid = mes_int_job.job_id", 'left');
        $this->db->where('job_id', $insert_id );
        $this->db->join( "mes_unit", "int_job_items.jobitm_unt = mes_unit.unit_id", 'left');
        //$this->db->where('int_job_items.jobitm_itmid',0 );
        $query = $this->db->get();
        $dataa=$query->result();
        $data['inv']=$query->result();
        
        $tec = explode(',',$data['inv'][0]->job_techid);
        $length = count($tec);
        
   
        
         for($i=0; $i<count($data['inv']); $i++){
                   
                    $inv[] = array($data['inv'][$i]->jobitm_des,$data['inv'][$i]->unit_symble,$data['inv'][$i]->jobitm_qty); 
                
        }
         $data['inv_itm'] = $inv;
        
        
        $tm[]="";
        for ($i = 1; $i < $length; $i++) {
                $this->db->select("*");    
                $this->db->from('mes_technician');
                $this->db->where('tech_id', $tec[$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/jobpdf',$data);
            
        }
        
    function chk_stock(){
        $data = $this->Common_model->check_stock($this->input->post('item'),$this->input->post('qty'));
        echo json_encode($data);
    }    
    
}        