<?php
class Crud_model_stok extends CI_Model{
  //get all categories method
  function get_category(){
      $result=$this->db->get('categories');
      return $result;
  }
  //generate dataTable serverside method
  function get_all_product() {
      $username = $this->session->userdata("username");  
        $this->db->where('username',$username);
        $query = $this->db->get('tb_admin');
        $row = $query->row();
          $level = $row->level;
        $data['id_admin']= $row->id;
        $data['nama_admin']= $row->nama_admin;
        $data['id_kantor']= $row->id_kantor;
        $id_admin = $row->id;
        $id_kantor = $row->id_kantor;

        $this->datatables->select('id_stok,id_barang,nama_barang,stok_aplikasi,id,barcode_ecer,barcode_dos,harga_jual_ecer,nama_rak,harga_jual_dos,isi, stok_aplikasi');
        $this->datatables->from('tb_barang');
         $this->datatables->join('tb_rak', 'tb_rak.id_rak=tb_barang.id_rak');
        $this->datatables->join('tb_stok', 'tb_stok.id_barang=tb_barang.id');
        $this->datatables->join('tb_kantor', 'tb_kantor.id_kantor=tb_stok.id_kantor');
           
       if($level =='manager'){
                                     
         $this->datatables->add_column('view', '<a href="javascript:void(0);" class="edit_record btn btn-info" data-code="$1" data-name="$2" data-stok="$3" >Input Stok</a>','id_stok,nama_barang,stok_aplikasi');
       }
        // $this->datatables->add_column('view', '<a href="#" class="edit_record btn btn-info pilih2" data-id_barang="$1" data-kode3="$1" data-nama_barang="$2"   data-harga_jual_ecer="$3"  data-harga_jual_dos="$4" onClick="masuk(this,$1)" >Pilih</a>  ','id,nama_barang,harga_jual_ecer,harga_jual_dos');
      return $this->datatables->generate();
  }
  //insert data method
  function insert_product(){
      $data=array(
        'product_code'        => $this->input->post('product_code'),
        'product_name'        => $this->input->post('product_name'),
        'product_price'       => $this->input->post('price'),
        'product_category_id' => $this->input->post('category')
      );
      $result=$this->db->insert('product', $data);
      return $result;
  }
  //update data method
  function update_product_satuan(){
        $username = $this->session->userdata("username");  
        $this->db->where('username',$username);
        $query = $this->db->get('tb_admin');
        $row = $query->row();
        $id_admin= $row->id;


      $id_stok=$this->input->post('id_stok');
      $stok_aplikasi=$this->input->post('stok_aplikasi');
      $stok_aplikasi_baru=$this->input->post('stok_aplikasi_baru');
      $tgl_kulakan=$this->input->post('tgl_kulakan');
      $keterangan=$this->input->post('keterangan');
      $nominal=$this->input->post('nominal');

      //cekStok
      $this->db->where('id_stok',$id_stok);
      $query = $this->db->get('tb_stok');
      $row = $query->row();
      $id_barang= $row->id_barang;
      $id_kantor= $row->id_kantor;
      $stok_aplikasi= $row->stok_aplikasi;

      $sisa_stok = $stok_aplikasi + $stok_aplikasi_baru;
      //update stok
      $data=[
        'stok_aplikasi'         => $sisa_stok,
     
      ];
      $this->db->where('id_stok',$id_stok);
      $result=$this->db->update('tb_stok', $data);

       


      return $result;
  }
  function update_product(){
        $username = $this->session->userdata("username");  
        $this->db->where('username',$username);
        $query = $this->db->get('tb_admin');
        $row = $query->row();
        $id_admin= $row->id;


      $id_stok=$this->input->post('id_stok');
      $stok_aplikasi=$this->input->post('stok_aplikasi');
      $stok_aplikasi_baru=$this->input->post('stok_aplikasi_baru');
      $tgl_kulakan=$this->input->post('tgl_kulakan');
      $keterangan=$this->input->post('keterangan');
      $nominal=$this->input->post('nominal');

      //cekStok
      $this->db->where('id_stok',$id_stok);
      $query = $this->db->get('tb_stok');
      $row = $query->row();
      $id_barang= $row->id_barang;
      $id_kantor= $row->id_kantor;
      $stok_aplikasi= $row->stok_aplikasi;

      $sisa_stok = $stok_aplikasi + $stok_aplikasi_baru;
      //update stok
      $data=[
        'stok_aplikasi'         => $sisa_stok,
     
      ];
      $this->db->where('id_stok',$id_stok);
      $result=$this->db->update('tb_stok', $data);

      //masukkhistory
      $data=[
        'id_stok_manual'=>  null,
        'id_admin'=> $id_admin,
         'id_barang'=> $id_barang,
        'id_stok'=> $id_stok,
        'awal_stok'=> $stok_aplikasi,
        'stok_masuk'=> $stok_aplikasi_baru,
        'stok_akhir'=> $sisa_stok,
        'nominal'=> $nominal,
        'tgl_kulakan'=> $tgl_kulakan,
        'keterangan'=> $keterangan,
        'id_kantor'=> $id_kantor,
      
      ];

      $this->db->insert('tb_stok_masuk_manual',$data);


      return $result;
  }
  //delete data method
  function delete_product(){
      $product_code=$this->input->post('product_code');
      $this->db->where('product_code',$product_code);
      $result=$this->db->delete('product');
      return $result;
  }
}
