<?php

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

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

        
         public function index(){
            
             $columns_arr= array("*");            
            $where_arr = array('sup_status!='=> 0);                     
            $resultagent = $this->General->getData('mes_suppliers',$columns_arr,$where_arr);
            $data['agentdetail'] = $resultagent;
            
            $columns_arr= array("*");            
            $where_arr = array('cat_status!='=> 0);                     
            $resultcat = $this->General->getData('mes_category',$columns_arr,$where_arr);
            $data['category'] = $resultcat;
           
           $id = $this->General->getlast_id();
            if(empty($id)){$num = 0;}else{$num = $id[0]->po_id;}
            $data['temp_id'] = ($num + 1);
           
             $data['content'] = ('pages/po');                       
             $this->load->view('template',$data);
        }
        
        function view_allinvoice() {
            $this->db->select('invoice.*,invoice.discount as agdis,agent.agent');
            $this->db->from('invoice');
            $this->db->join( "agent", "invoice.agent_id = agent.agent_id", 'inner');
            $this->db->order_by("inv_num","desc");
            $hasil=$this->db->get();
            $data['inv']= $hasil->result(); 
            $data['content'] = ('pages/allinvoice');                       
            $this->load->view('template',$data);
        }
        
        
        public function get_draft($temp){
            
            $columns_arr= array("agent_id","agent");
            $resultagent = $this->General->getData('agent',$columns_arr);
            $data['agentdetail'] = $resultagent;
            
            //$data['itemdetails'] = $this->General->get_joint("login", "userlvl", "login.userlevel  = userlvl.lvl_val", "right","*");
            $data['itemdetails'] = $this->General->get_joint("stock", "product", "stock.book_id  = product.p_id", "right","*");
            $data['temp_id'] = $temp;
             $data['content'] = ('pages/invoice');                       
             $this->load->view('template',$data);
        }
        
         function test() {
           /*     $this->db->select('*');
                $this->db->from('temp_qto_items');
                $this->db->where('po_user', 'jayanath');
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            
            foreach ($hasil->result() as $row) {
                    $this->db->select('item_selling_price');
                $this->db->from('mes_item');
                $this->db->where('item_id', $row->po_itm_item_id);
            $hasil1=$this->db->get();
            $det= $hasil1->result();
                
                
                $this->db->set('po_itm_unitprice', $det[0]->item_selling_price);
                $this->db->where('po_itm_id', $row->po_itm_id);
                $data=$this->db->update('temp_qto_items');
            } */
                
                $this->db->select('*');
                $this->db->from('mes_inventry');
                $this->db->where('invt_cuser', 'nance');
                $this->db->where('invt_type', 'temp');
                $this->db->where('invt_id>', '9173');
            $hasil=$this->db->get();
           // $data= $hasil->result();
            
            foreach ($hasil->result() as $row) {
                //echo "ins";
                 echo $stk = $this->Common_model->cal_stock('0',"URGENT",$row->invt_id,$row->invt_itemid,$row->invt_qty,"plus",0);
            }
               
               
        }
        
        
        public function getagendata (){
            
        $uid = $this->input->post('u_id');
        $fields = array("*");
        
        $where = array(
                    'agent_id' => $uid
                  );
        
        $data = $this->General->getData('agent',$fields,$where);
        echo json_encode($data);
        
        }
        
        
        
          public function getproductdata (){
            
                $uid = $this->input->post('u_id');
               $this->db->select('*');
            $this->db->from('product');
            $this->db->where('p_id', $uid);
            $hasil=$this->db->get();  
            $data = $hasil->result();
            echo json_encode($data);
        
          }
    //put your code here
        
    function addinvoice_product() {
          
            $dat = array(
                'temp_id'  => $this->input->post('temp_id'),
                'po_itm_item_id' => $this->input->post('item'),
                'po_itm_partno' => $this->input->post('prt_id'),
                'po_itm_qty'  => $this->input->post('qty'), 
                'po_itm_unitprice'  => $this->input->post('price'),
                'po_itm_unit'  => $this->input->post('unit'),
                'po_itm_currancy'  => $this->input->post('cur')
                
            );
       $data = $this->db->insert('temp_po_items',$dat);
      
            $data = "Added";
       
        
       echo json_encode($data);
    }
    
    
    function getinvoice_product($id) {
     
        $this->db->select("*");
        $this->db->from('temp_po_items');
        $this->db->join('mes_item', 'temp_po_items.po_itm_item_id   = mes_item.item_id');
        $this->db->join('mes_currancy', 'temp_po_items.po_itm_currancy   = mes_currancy.curr_id');
        $this->db->join('mes_unit', 'temp_po_items.po_itm_unit   = mes_unit.unit_id');
        $this->db->join('mes_partnumber', 'temp_po_items.po_itm_partno  = mes_partnumber.part_id');
        $this->db->where('temp_po_items.temp_id',  $id);
        $hasil=$this->db->get();
        $data = $hasil->result(); 
   
        
       echo json_encode($data);
    }
    
    function invoive_update() {
     $product_code=$this->input->post('product_code');
        $product_price=$this->input->post('price');
        $product_qty =$this->input->post('qty');
        $tot = $product_price * $product_qty;
 
        $this->db->set('qty', $product_qty);
        $this->db->set('tot_price', $tot);
        $this->db->where('inv_pro_id', $product_code);
        $data=$this->db->update('temp_inv_product');
        
       echo json_encode($data);
    }
    
     function delete() {
        $product_code=$this->input->post('product_code');
        $this->db->where('po_itm_id', $product_code);
        $data=$this->db->delete('temp_po_items');
        echo json_encode($data);
    }
    
    function save_inv() {
        
            $temp_id = $this->input->post('temp_id');
          
        
        $dat = array(
                //'date'  => date('Y-m-d'),
                'po_sup_id'  => $this->input->post('agent_id'),
                'po_cus_qto_refno'  => $this->input->post('mes_ref'),
                'po_sup_inv_refno' => $this->input->post('sup_ref'),
                'po_tot_price'  => $this->input->post('total'), 
                'po_discount'  => $this->input->post('dis_val'),
                'po_final_tot'  => $this->input->post('final'),
                'po_banking'  => $this->input->post('bank_fee'),
                'po_shipping' => $this->input->post('ship'),
                'po_nbt'  => $this->input->post('nbt'), 
                'po_vat'  => $this->input->post('vat'),
                'po_tot_qty'  => $this->input->post('qty'),
                'po_tot_items'  => $this->input->post('qty'),
                'po_other'  => $this->input->post('other'),
                'po_currancy' => $this->input->post('cur'),
                'po_delevery'  => "0", 
                'po_type'  => $this->input->post('sup_type'),
                'po_cuser'  => 'testuser', 
                'po_cdate'  => date('Y-m-d H:i:s')
                
            );
       $data = $this->db->insert('mes_po',$dat);
        $insert_id = $this->db->insert_id(); 
         
       
        $this->db->set('po_itm_po_id',$insert_id);
        $this->db->set('temp_id',$insert_id);
        $this->db->where('temp_id',  $this->input->post('temp_id'));
        $this->db->update('temp_po_items');
            
        $this->db->select("*");    
        $this->db->from('temp_po_items');
        $this->db->where('po_itm_po_id', $insert_id );
        $query = $this->db->get();
            
      foreach ($query->result() as $row) {
                $dat = array(
                'po_itm_partno'  => $row->po_itm_partno,
                'po_itm_item_id'  => $row->po_itm_item_id,
                'po_itm_po_id' => $row->po_itm_po_id,
                'po_itm_qty'  => $row->po_itm_qty, 
                'po_itm_unit'  => $row->po_itm_unit,
                'po_itm_unitprice'  => $row->po_itm_unitprice,
                'po_itm_currancy'  => $row->po_itm_currancy,
                'po_itm_description'  => $row->po_itm_description
            );
                  $this->db->insert('po_items',$dat);
                  
                  $this->db->where('po_itm_id', $row->po_itm_id);
                  $this->db->delete('temp_po_items');
                  
                  
            }
            
              $dt = "ins";
         echo json_encode($insert_id);
         //$this->view_pdf($insert_id);
    }
    
    function view_pdf($insert_id){
        $this->db->select("*");    
        $this->db->from('po_items');
        $this->db->where('po_itm_po_id', $insert_id );
        $this->db->join('mes_item', 'po_items.po_itm_item_id   = mes_item.item_id');
        $this->db->join('mes_partnumber', 'po_items.po_itm_partno  = mes_partnumber.part_id');
        $this->db->join('mes_unit', 'po_items.po_itm_unit   = mes_unit.unit_id');
        $this->db->join('mes_currancy', 'po_items.po_itm_currancy   = mes_currancy.curr_id');
        $query = $this->db->get();
        $data['inv_itm']=$query->result();
        
        $this->db->select("*");    
        $this->db->from('mes_po');
        $this->db->where('po_id', $insert_id );
        $this->db->join('mes_suppliers', 'mes_po.po_sup_id  = mes_suppliers.sup_id');
        $this->db->join('mes_currancy', 'mes_po.po_currancy   = mes_currancy.curr_id');
        $query = $this->db->get();
        $data['inv']=$query->result();
     
         $this->load->view('pages/popdf',$data);
    }

    function save_po(){

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

        
       
       echo json_encode($data);
    }
    
    function cancel_inv() {
                    $this->db->where('temp_id', $this->input->post('temp_id'));
                    $data=$this->db->delete('temp_inv_product');
                    echo json_encode($data);
    }
    
	
	
    public function invprint(){
        
          $inv = $this->input->post('hidnprintid');
            
            $this->db->select('*');
            $this->db->from('invoice');
            $this->db->join( "agent", "invoice.agent_id  = agent.agent_id", 'inner');
            $this->db->join( "inv_product", "invoice.inv_num  = inv_product.inv_id ", 'inner');
            $this->db->join( "product", "inv_product.product_id  = product.p_id ", 'inner');
            $this->db->where('inv_num', $inv);
            $hasil=$this->db->get();
            $data['inv']= $hasil->result();
            $data['test'] = "AAkarsha";
            $data['user'] = $this->session->userdata['login_data']['username'];
            $this->load->view('pages/invoicepdf',$data);
            
              $updatearr = array(                   
                   'print'=> 1
                  );
              $updateware = array(                   
                   'inv_num' => $inv
               );
                $usereditresult = $this->General->updateData('invoice',$updatearr,$updateware);
                
                $this->view_allinvoice();
                
    }
    
    public function invprintre(){
        $inv = $this->input->post('hidnprintid');
         $this->db->select('*');
            $this->db->from('invoice');
            $this->db->join( "agent", "invoice.agent_id  = agent.agent_id", 'inner');
            $this->db->join( "inv_product", "invoice.inv_num  = inv_product.inv_id ", 'inner');
            $this->db->join( "product", "inv_product.product_id  = product.p_id ", 'inner');
            $this->db->where('inv_num', $inv);
            $hasil=$this->db->get();
            $data['inv']= $hasil->result();
            $data['test'] = "AAkarsha";
            $data['user'] = $this->session->userdata['login_data']['username'];
            $this->load->view('pages/invoicerepdf',$data);
            $this->view_allinvoice();
    }
    
    function getbrand() {
        $cat = $this->input->post('u_id');
        
            $columns_arr= array("*");       
            $resultagent = $this->General->getData('mes_brand',$columns_arr);
            $data['brand'] = $resultagent;
            
            $columns_arr= array("*");      
            $where_arr = array('item_cat'=> $cat);  
            $resultitem = $this->General->getData('mes_item',$columns_arr,$where_arr);
            $data['item'] = $resultitem;
            
            echo json_encode($data);
        
    }
    
    function getpart(){
        $item = $this->input->post('u_id');
        
        $columns_arr= array("*");      
            $where_arr = array('part_itmno'=> $item);  
            $resultitem = $this->General->getData('mes_partnumber',$columns_arr,$where_arr);
            $data['part'] = $resultitem;
            
            $this->db->select('*');
            $this->db->from('mes_item');
            $this->db->join( "mes_unit", "mes_item.item_unit  = mes_unit.unit_id");
            $this->db->where('mes_item.item_id', $item);
            $hasil=$this->db->get();
            $data['itm']= $hasil->result();
            
            
            echo json_encode($data);
    }

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

            echo json_encode($data);
    }
    
    }
