<?php

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

class M_bahan_kimia extends CI_Model {

    protected $idGroup = null;
    protected $iduser = null;

    public function __construct() {
        parent::__construct();
        $this->id_group = $this->session->userdata("id_group");
        $this->id_user = $userId = $this->session->userdata('id_user');
    }

    function setData($data, $id = null) {
        $this->db->trans_begin();

        array_shift($data);
        if (!isset($id)) {
            $this->db->insert('bahan_kimia', $data);
            $id = $this->db->insert_id();
        } else {
            $this->db->where('id', $id);
            $this->db->update('bahan_kimia', $data);
        }

        if($this->db->trans_status() == false){
          $this->db->trans_rollback();
          return false;
        }else{
          $this->db->trans_commit();
          return $id;
        }
    }

    function hapus($id){
      $this->db->trans_begin();
        $this->db->where('id', $id);
        $this->db->update('bahan_kimia', array('is_active'=>'0'));
      if($this->db->trans_status() == false){
        $this->db->trans_rollback();
        return false;
      }else{
        $this->db->trans_commit();
        return true;
      }
    }

    function reads() {
      $param = array();
      $query = "SELECT * FROM bahan_kimia WHERE is_active = '1'";

      $result = $this->db->query($query, $param);
      return $result->result_array();
    }

    function detail($id){
      $param = array();
      $query = "SELECT * FROM bahan_kimia WHERE id=? AND is_active = '1'";
      $param[] =  $id;

      $result = $this->db->query($query, $param);
      return $result->row_array();
    }

    function set_lot($data, $id=null, $type=null){
      $this->db->trans_begin();
      if(is_null($id)){
        $this->db->query("INSERT INTO lot (id_bahan_kimia, nomor_lot, tanggal_kadaluarsa) VALUES (?,?,?)", array($data['id_bahan_kimia'], $data['nomor_lot'], $data['tanggal_kadaluarsa']));
      }else{
        if(is_null($type)){

        }else{
          $this->db->query("UPDATE lot SET is_active = '0' WHERE id = ?", $id);
        }
      }

      if($this->db->trans_status() == false){
        $this->db->trans_rollback();
        return false;
      }else{
        $this->db->trans_commit();
        return true;
      }
    }

    function get_lot($id){
      $query = $this->db->query("SELECT * FROM lot WHERE id_bahan_kimia = ? AND is_active = '1'", $id);
      return $query->result_array();
    }
}
