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

class Creditnote_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 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/creditnote";        
            $this->load->view('template',$data);
        }
        
                function get_cus(){
            
        // $data =   $this->Product_model->customer_list();
         //$data = $this->Product_model->po_list();
                $this->db->select("*");
                $this->db->from('mes_cus_order');
                $this->db->where('co_status','1');
                $this->db->where('co_profoma !=', 0);
                $this->db->or_where('co_inv !=', 0);
                $all = $this->db->get();
                $data = $all->result();
         for($i=0; $i<count($data); $i++){
             
             $cust[] = array($data[$i]->co_code,$data[$i]->co_id);
         }
         
         $sup = $cust;
            echo json_encode($sup);
        }
        
                function get_inv(){
            $sup = $this->input->post('sup_id');
            
           
                
                $this->db->select('*');
                    $this->db->from('mes_invoice');
                    $this->db->where('co_status',1);
                    //$this->db->where('po_auth',0);
                    //$this->db->where('po_recieved',0);
                    $this->db->where('co_qto_id',$sup);
                    $hasil=$this->db->get();
                    $data['qtn'] = $hasil->result(); 
                    
                   
                    $cusid =  $data['qtn'][0]->co_cus_id;
                     $this->db->select("mes_currancy.curr_symble as cu_symbol,mes_currancy.curr_id as cu_id,
                                mes_customer.cust_type as custo_type, mes_customer.cust_tax as custo_tax, cust_address.*,cust_company");
                $this->db->from('mes_customer');
                $this->db->where('mes_customer.cust_id', $cusid);
                $this->db->join( "cust_address", "cust_address.cusad_cusid = mes_customer.cust_id", 'left');
                $this->db->join( "mes_currancy", "mes_customer.cust_currency  = mes_currancy.curr_id");
                 
                $this->db->where('cust_status','1');
                $all = $this->db->get();
                $data['sup'] = $all->result();
               
            //$data['qtn'] = $resultagent;

            echo json_encode($data);
        }
        
        
        function getproduct(){
          
            $data = $this->Common_model->inv_id_item($this->input->post('po_id'));
            
           $det['item']=$det['customer']="";
           $totamnt = 0.00;
                for($i=1; $i<count($data); $i++){
                 $bal = $data[$i][5] - $data[$i][8];
                    
                    $dat =  "
                                 <tr>
                               <td >".$i."</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;'>".$bal."</td>
                               <td><input type='text' id=".$data[$i][7]."  name='po_val[]' placeholder='' value='".$bal."' 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='unit_price[]' 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;
                              $i = $i++;
                              
                }
           
            $this->db->select('*');
            $this->db->from('mes_invoice');
            $this->db->where('co_id', $this->input->post('po_id'));
            $this->db->join( "mes_customer", "mes_customer.cust_id = mes_invoice.co_cus_id", 'inner');
            $this->db->join( "mes_currancy", "mes_currancy.curr_id = mes_invoice.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 creditnote_save(){
            
             $id = $this->input->post('item_id');
            $qt = $this->input->post('po_val');
           
           $tot = 0;
            $dat = array(
                        'crnot_invid'  => $this->input->post('poselect'),
                        'crnot_cusordid' => $this->input->post('cus_id'),
                        'crnot_itmqty' => $this->input->post('inv_tot_item'),
                        'crnot_subtot'  => $this->input->post('inv_total'),
                        'crnot_dis'  => $this->input->post('dis_total'),
                        'crnot_nettot'  => $this->input->post('net'),
                        'crnot_grnttot'  => $this->input->post('final1'),
                        'crnot_remark'  => $this->input->post('remark'),
                        'crnot_cuser'  => "admin",
                        'crnot_cdate'  => date('Y-m-d')
                        
                    );
            $data = $this->db->insert('mes_creditnote',$dat);
           $insert_id = $this->db->insert_id();
          
            for($i=0; $i<count($id); $i++){
                if($qt[$i]!=""){
                        $this->db->select("*");    
                        $this->db->from('inv_items');
                        $this->db->where('co_itm_id', $id[$i]);
                        $query = $this->db->get();
                         $dt=$query->result();
                    
                   $data = $this->Common_model->cal_stock("CRN",$insert_id,$dt[0]->co_itm_item_id,$qt[$i],"plus",0);
                    $tot = $tot+$qt[$i];
                    
                    $newded = $dt[0]->co_itm_crqty + $qt[$i];
                    $this->db->set('co_itm_crqty', $newded);
                    $this->db->where('co_itm_id', $id[$i]);
                    $result=$this->db->update('inv_items');
                }
            }
           
          
           
      
            $send = "New Credit Note Created";
            echo json_encode($send);
        }
        
          function crn_all(){
            
             $data['content'] = "pages/allcrn";        
            $this->load->view('template',$data); 
        }
        
        function showallcrn(){
            
            $this->db->select('*');
            $this->db->from(' mes_creditnote');
            $this->db->join( "mes_cus_order", "mes_cus_order.co_id = mes_creditnote.crnot_cusordid", 'inner');
            $this->db->join( "mes_customer", "mes_cus_order.co_cus_id = mes_customer.cust_id", 'inner');
            $this->db->order_by("crnot_id","desc");
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
            
            
            
         
            $link = site_url('credit_note');
           
             $det[0]="<a href='$link'><button type='button' class='btn btn-outline-danger' >Create New Credit Note</button></a></br>";
            
            $det[1] ="<tr>
														<th scope='col' style='text-align: center;'>ID</th>
														
														<th>CRN Number</th>
														<th>COD Number</th>
														<th>INV Number</th>
														<th>Customer</th>
														<th>Total Qty</th>
														<th>Total Price</th>
														<th>Date</th>
													    <th>Status</th>
														<th>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
                for($i=0; $i<count($data); $i++){
                    
           
                    $vw_btn2 = "<a href ='".base_url()."viewpdf_crn/".$data[$i]->crnot_id."' target='_blank'> <button type='button' class='btn btn-outline-secondary btn-sm' >View</button></a>
                                
                                ";
                        
                   
                        if($data[$i]->crnot_stetus==0){
                        $sts = "<td style='color:#00800085; font-size:25px; text-align: center;'>&#9679;</td>";
                        }else{
                        $sts = "<td style='color:#ff000080; font-size:25px; text-align: center;'>&#9679;</td>"; }
                        
                   
                    
                    $x = $i+1;
                    $dat =  "
                                 <tr scope='row'>
                                 <td >".$x."</td>
                                 <td >".$data[$i]->crnot_code.$data[$i]->crnot_id."</td>
                                 <td>COD".$data[$i]->crnot_cusordid."</td>
                                 <td>INV".$data[$i]->crnot_invid."</td>
                                 
                                 <td>".$data[$i]->cust_company."</td>
                                 <td style='text-align: right;'>".$data[$i]->crnot_itmqty."</td>
                                 <td style='text-align: right;'>".$data[$i]->crnot_grnttot."</td>
                                <td style='text-align: right;'>".$data[$i]->crnot_cdate."</td>
                                $sts
                                <td>$vw_btn2  </td>
                               
                              
                              </tr>";
                              $x++;
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
            //echo json_encode($data);
        }
        
                function view_pdf($insert_id){
                    
        $this->db->select("*");    
        $this->db->from('mes_creditnote');
        $this->db->where('crnot_id', $insert_id );
        $this->db->join('mes_invoice', 'mes_creditnote.crnot_invid  = mes_invoice.co_id');
        $this->db->join('mes_cus_order', 'mes_creditnote.crnot_cusordid   = mes_cus_order.co_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');
        $query = $this->db->get();
        $data['inv']=$query->result();
        
        $invcid = $data['inv'][0]->crnot_invid;
       
         $this->db->select("*");    
        $this->db->from('inv_items');
        $this->db->where('co_itm_co_id', $invcid);
        $this->db->where('co_itm_crqty!=', '0');
        $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_crqty,$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_crqty,$dataa[$i]->co_itm_id,$dataa[$i]->co_itm_qto_id); 
                }
        }
        $data['inv_itm'] = $inv;
        
        
        
      // $tc = explode(',',$data['inv'][0]->crnot_crtc);
      // 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/crnpdf',$data);
        } 
        
        
        
}
        
