<?php
class Crud_model 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();
        $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,barcode_ecer,barcode_dos,nama_barang,harga_jual_ecer,harga_jual_dos,isi,harga_grosir,stok_aplikasi');
        $this->datatables->from('tb_barang');
        $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');
        $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');


      // $this->datatables->select('ID_BARANG,NAMA_BARANG, HARGA_JUAL , HARGA_JUAL_MEMBER , HARGA_LEVEL1, MIN_LEVEL1,RAK,STOK');
      // $this->datatables->from('tbs_barang_copy');
      // $this->datatables->join('categories', 'product_category_id=category_id');
      // $this->datatables->add_column('view', '<a href="#" class="edit_record btn btn-info pilih3" data-ID_BARANG="$1" onClick="masuk(this,$1)" >Pilih</a>  ','ID_BARANG ');
      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(){
      $product_code=$this->input->post('product_code');
      $data=array(
        'product_name'         => $this->input->post('product_name'),
        'product_price'        => $this->input->post('price'),
        'product_category_id'  => $this->input->post('category')
      );
      $this->db->where('product_code',$product_code);
      $result=$this->db->update('product', $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;
  }
}
