<?php

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

class M_amino extends CI_Model {

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

    public function __construct() {
        parent::__construct();
        $this->idGroup = $this->session->userdata("r_idGroup");
        $this->idUser = $this->session->userdata('r_userId');
    }

    function get_amino($id_uji=null){
      $query = $this->db->query("SELECT * FROM persetujuan_asam_amino WHERE id_uji = ?", $id_uji);
      return $query->row_array();
    }

    function get_lhp_amino($id_uji=null){
      $query = $this->db->query("SELECT * FROM lhp_amino WHERE id_uji = ?", $id_uji);
      return $query->result_array();
    }

    function set_lhp_amino($data, $id=null){
      $this->db->trans_begin();
      
      $cek = $this->db->query("SELECT id FROM lhp_amino WHERE id = ?", $id)->row_array()['id'];

      $param = array();
      $param[] = $data['id_uji'];
      $param[] = $data['lisin'];
      $param[] = $data['min_lisin'];
      $param[] = $data['metionin'];
      $param[] = $data['min_metionin'];
      $param[] = $data['metionin_sistin'];
      $param[] = $data['min_metionin_sistin'];
      $param[] = $data['treonin'];
      $param[] = $data['min_treonin'];
      $param[] = $data['triptofan'];
      $param[] = $data['min_triptofan'];

      if(is_null($cek)){
        $query = "INSERT INTO lhp_amino(id_uji, lisin, min_lisin, metionin, min_metionin, metionin_sistin, min_metionin_sistin, treonin, min_treonin, triptofan, min_triptofan) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
      }else{
        $query = "UPDATE lhp_amino SET id_uji = ?, lisin = ?, min_lisin = ?, metionin = ?, min_metionin = ?, metionin_sistin = ?, min_metionin_sistin = ?, treonin = ?, min_treonin = ?, triptofan = ?, min_triptofan = ?  WHERE id = ?";
        $param[] = $cek;
      }

      $this->db->query($query, $param);

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

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

      $param[] = $data['id_uji'];
      $param[] = $data['waktu_tanggal_persetujuan'];
      $param[] = $data['nama_instansi'];
      $param[] = $data['nama_pelanggan'];
      $param[] = $data['jenis_parameter_pengujian'];
      $param[] = $data['cara_pengujian'];
      $param[] = $data['sampel_yang_diuji'];
      $param[] = $data['laboratorium_subkontraktor'];

      if(is_null($id)){
        $query = "INSERT INTO persetujuan_asam_amino(id_uji, waktu_tanggal_persetujuan, nama_instansi, nama_pelanggan, jenis_parameter_pengujian, cara_pengujian, sampel_yang_diuji, laboratorium_subkontraktor) VALUES (?,?,?,?,?,?,?,?)";
      }else{
        $param[] = $id;

        $query = "UPDATE persetujuan_asam_amino SET id_uji = ?, waktu_tanggal_persetujuan = ?, nama_instansi = ?, nama_pelanggan = ?, jenis_parameter_pengujian = ?, cara_pengujian = ?, sampel_yang_diuji = ?, laboratorium_subkontraktor = ? WHERE id = ?";
      }

      $this->db->query($query, $param);

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