<?php

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

class M_disposisi 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 getAnalis($data){
        $query = $this->db->query("SELECT ds.*, ds.id_sampel as nomor_sampel,
         l.*, l.air as l_air, l.abu as l_abu, l.protein_kasar as l_protein_kasar, l.lemak_kasar as l_lemak_kasar, l.serat_kasar as l_serat_kasar,l.kalsium as l_kalsium, l.fosfor as l_fosfor, l.tdn as l_tdn, l.ur as l_ur, l.af as l_af, l.andf as l_andf, l.mbm as l_mbm,
         gj.detail as gol_jenis_sampel, js.* FROM data_sampel ds
         LEFT JOIN lhu l ON l.id_sampel = ds.id_sampel AND l.is_active = '1'
         LEFT JOIN jenis_sampel js ON js.id_jenis = ds.id_jenis_sampel
         LEFT JOIN gol_jenis_sampel gj ON gj.id_jenis = js.id_gol_sampel
         WHERE ds.id_uji = ? AND ds.is_active ='1'", $data['id_uji']);
        return $query->result_array();
    }

    function getDataUji($is_count = false, $limit = null, $start = null, $id = null, $history=false){
        $param = array();

        $id_group =  $this->idGroup.'|';
        $getStatus = $this->db->query("SELECT id_jenis FROM status_sampel WHERE id_group_user LIKE ?", '%'.$id_group.'%')->result_array();
        $id_status = array();
        $id_status[] = 0;
        foreach($getStatus as $r){
            $id_status[] = $r['id_jenis'];
        }

        if($is_count)
            $query = "SELECT COUNT(id_uji) AS jumlah";
        else
            $query ="SELECT pu.id_uji, ss.nama_jenis as status_sampel, pu.id_status, ss.detail, pu.keterangan";

        $query .= " FROM permintaan_uji pu";

        if(!$is_count)
			$query .= " LEFT JOIN status_sampel ss ON ss.id_jenis = pu.id_status";

        $query .= " WHERE pu.is_active = '1' AND pu.id_status > ? AND pu.id_status <> '14' ";

        $status = 1;
        if(!in_array($this->idGroup, array('1','2','4'))) $status = 2;
        $param[] = $status;

        if(!is_null($id_status)){
            $query .= " AND pu.id_status IN ?";
            $param[] = $id_status;
        }

        if($history){
            foreach($getStatus as $r){
                $query .= " || pu.id_status > ?";
                $param[] = $r['id_jenis'];
            }
            $query .= " AND last_modified_user = ?";
            $param[] = $this->idUser;
        }

        $query .= " ORDER BY pu.id_uji ASC";

        if($is_count == false) {
            if (isset($limit)) {
                $query .= " LIMIT ?, ?";
                $param[] = intval($start);
                $param[] = intval($limit);
            }
        }

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

        if($is_count){
    			return $result->row_array()['jumlah'];
      	} else {
      			if($id != null)
      				return $result->row_array();
      			else
      				return $result->result_array();
  		  }
    }

    function getDisposisi($is_count = false, $limit = null, $start = null, $id = null, $history=false){
        $param = array();

        $id_group =  $this->idGroup.'|';
        $getStatus = $this->db->query("SELECT id_jenis FROM status_sampel WHERE id_group_user LIKE ?", '%'.$id_group.'%')->result_array();
        $id_status = array();
        $id_status[] = 0;
        foreach($getStatus as $r){
            $id_status[] = $r['id_jenis'];
        }

        if($is_count){
            $query = "SELECT COUNT(d.id_disposisi) as c FROM data_sampel ds
                WHERE ds.id_uji = ? AND ds.is_active = '1'";

            $result = $this->db->query($query, $id_uji)->row_array();
        }else {
            $query = "SELECT ds.id_sampel, ds.jenis_pengujian, ds.kualitas, ps.nama_jenis AS jenis_penanganan,d.kemasan, bs.nama_jenis AS jenis_berat, d.berat_sampel, sk.id_jenis as sampel_kerja, sa.id_jenis as sampel_arsip, pu.id_uji, ss.nama_jenis as status_sampel, pu.id_status, ss.detail, pu.keterangan, gjs.nama_jenis as jenis_klasifikasi
                FROM data_sampel ds
                LEFT JOIN disposisi d ON d.id_sampel = ds.id_sampel AND d.is_active = '1'
                LEFT JOIN jenis_penanganan_sampel ps ON ps.id_jenis = d.id_penanganan_sampel
                LEFT JOIN kondisi_sampel bs ON bs.id_jenis = d.id_kondisi_sampel
                LEFT JOIN penyimpanan_sampel sk ON sk.id_jenis = d.id_sampel_kerja
                LEFT JOIN penyimpanan_sampel sa ON sa.id_jenis = d.id_sampel_arsip
                LEFT JOIN permintaan_uji pu ON pu.id_uji = ds.id_uji
                LEFT JOIN status_sampel ss ON ss.id_jenis = pu.id_status
                LEFT JOIN gol_jenis_sampel gjs ON gjs.id_jenis = ds.klasifikasi
                WHERE pu.is_active = '1' AND ds.is_active = '1' AND pu.id_status > ? AND pu.id_status <> '14'";

            $status = 1;
            if(!in_array($this->idGroup, array('1','2','4'))) $status = 2;
            $param[] = $status;

            if(!is_null($id_status)){
                $query .= " AND pu.id_status IN ?";
                $param[] = $id_status;
            }

            if($history){
                foreach($getStatus as $r){
                    $query .= " || pu.id_status > ?";
                    $param[] = $r['id_jenis'];
                }
                $query .= " AND pu.last_modified_user = ?";
                $param[] = $this->idUser;
            }

            if(!empty($this->input->post('filter_cari'))){
                $data = $this->input->post();
                $where = $this->whereHandler($query, $param, $data);

                $query = $where['query'];
                $param = $where['param'];
            }

            $query .= " ORDER BY pu.id_uji DESC";

            if($is_count == false) {
                if (isset($limit)) {
                    $query .= " LIMIT ?, ?";
                    $param[] = intval($start);
                    $param[] = intval($limit);
                }
            }
        }

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

        //echo $this->db->last_query(); die();

        if($is_count){
    			return $result->row_array()['jumlah'];
    		} else {
    			if($id != null)
    				return $result->row_array();
    			else
    				return $result->result_array();
    		}
    }

    function whereHandler($query, $param, $data){

        if(isset($data['id_sampel']) && $data['id_sampel'] != ''){
            $query .= " AND ds.id_sampel = ?";
            $param[] = $data['id_sampel'];
        }

        if(isset($data['kualtas_sampel'])  && $data['kualitas_sampel'] != ''){
            $query .= " AND ds.kualitas_sampel = ?";
            $param[] = $data['kualitas_sampel'];
        }

        if(isset($data['keterangan'])  && $data['keterangan'] != ''){
            $query .= " AND ds.id_keterangan = ?";
            $param[] = $data['keterangan'];
        }

        if(isset($data['parameter_uji'])){
            $query .= " AND ds.jenis_pengujian LIKE ?";
            $param[] = "%".$data['parameter_uji']."|%";
        }

        if(isset($data['penanganan_sampel']) && $data['penanganan_sampel'] != ''){
            $query .= " AND d.id_penanganan_sampel = ?";
            $param[] = $data['id_penanganan_sampel'];
        }

        $result['query'] = $query;
        $result['param'] = $param;

        return $result;
    }

    function siapkanSampel($data){
        $this->db->trans_begin();

        $param = array();
        $param[] = $data['id_status'];
        $param[] = $data['id_user'];
        $param[] = $data['id_uji'];

        $query = "UPDATE permintaan_uji SET id_status = ?, last_modified_user = ?, last_modified_time = NOW() ";

        if($data['id_status'] == 9){
          $query .= " , tgl_pukul_terbit_lhu = NOW() ";
        }

        if($data['id_status'] == 11){
          $query .= " , tgl_pukul_terbit_lhp = NOW() ";
        }

        $query .= " WHERE id_uji = ? ";

        $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 penyiapanUji($id_uji){
        $selectSampel = $this->db->query("SELECT id_sampel FROM data_sampel WHERE id_uji = ? AND is_active = '1'", $id_uji)->result_array();
        $cek_tgl_penyiapan = $this->db->query("SELECT tgl_penyiapan_sampel FROM permintaan_uji WHERE id_uji = ? AND is_active = '1'", $id_uji)->row_array()['tgl_penyiapan_sampel'];

        $this->db->trans_begin();
        if(!isset($cek_tgl_penyiapan)){
          $this->db->query("UPDATE permintaan_uji SET tgl_penyiapan_sampel = ? WHERE id_uji = ?", array(date('Y-m-d'), $id_uji));
        }

        $id = array();
        foreach ($selectSampel as $r) {
            $cek = $this->db->query("SELECT id_penyiapan FROM penyiapan_sampel WHERE id_sampel = ? AND is_active = '1'", $r['id_sampel'])->row_array();
            if(empty($cek['id_penyiapan']) && !isset($cek['id_penyiapan'])){
                $this->db->query("INSERT INTO penyiapan_sampel (id_sampel) VALUES(?)", $r['id_sampel']);
                $id[] = $this->db->insert_id();
            }else{
                $id[] = $cek['id_penyiapan'];
            }
        }

        if($this->db->trans_status() === FALSE){
            $this->db->trans_rollback();
            return FALSE;
        }else {
            $this->db->trans_commit();
            $query = $this->db->query("SELECT ps.*, ds.bentuk_jenis, pu.tgl_penyiapan_sampel, jg.nama_jenis as nama_grinder, jl.nama_jenis as nama_layanan
              FROM data_sampel ds
                LEFT JOIN penyiapan_sampel ps ON ps.id_sampel = ds.id_sampel AND ps.is_active = '1'
                LEFT JOIN jenis_grinder jg ON jg.id_jenis = ps.id_jenis_grinder
                LEFT JOIN permintaan_uji pu ON pu.id_uji = ds.id_uji
                LEFT JOIN jenis_layanan jl ON jl.id_jenis = pu.id_jenis_layanan
                WHERE ds.id_uji = ? AND ds.is_active = '1'", $id_uji)->result_array();

            return $query;
        }
    }

    function penyiapan_sampel($data) {
        $this->db->trans_begin();
        $data[] = '1';

        $this->db->query("UPDATE penyiapan_sampel SET  pukul_mulai = ?, pukul_selesai = ?, id_bentuk_sampel = ?, id_penanganan_arsip = ?, id_penanganan_kerja = ?, id_jenis_grinder = ?, keterangan = ?, last_modified_user = ?, ket_penanganan_kerja = ? WHERE id_sampel = ? AND is_active = ?", $data);

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

    function update_disposisi($data){
      $this->db->trans_begin();
      $data[] = '1';
      $this->db->query("UPDATE disposisi SET id_sampel_kerja = ?, id_sampel_arsip = ?, berat_sampel = ?, satuan_berat = ?, kemasan = ? WHERE id_sampel = ? AND is_active = ?", $data);

      //echo $this->db->last_query(); die();

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

    function getEditSampel($id_sampel){
        return $this->db->query("SELECT ps.*, ks.nama_jenis as kondisi_sampel, jp.nama_jenis as arsip_penanganan, ks.nama_jenis as kerja_penanganan, jg.nama_jenis as nama_grinder, d.id_sampel_kerja, d.id_sampel_arsip, d.kemasan, d.satuan_berat, d.berat_sampel
            FROM penyiapan_sampel ps
            LEFT JOIN bentuk_sampel ks ON ks.id_jenis = ps.id_bentuk_sampel
            LEFT JOIN jenis_penanganan_sampel jp ON jp.id_jenis = ps.id_penanganan_arsip
            LEFT JOIN jenis_penanganan_sampel js ON js.id_jenis = ps.id_penanganan_kerja
            LEFT JOIN jenis_grinder jg ON jg.id_jenis = ps.id_jenis_grinder
            LEFT JOIN disposisi d ON d.id_sampel = ps.id_sampel
            WHERE ps.id_sampel = ? AND d.id_sampel = ? AND ps.is_active = ?", array($id_sampel, $id_sampel, '1'))->row_array();
    }

    function deletePenyiapanSampel($id_uji){
        $sampel = $this->db->query("SELECT id_sampel FROM data_sampel WHERE id_uji = ? AND is_active = ?", array($id_uji, '1'))->result_array();

        $this->db->trans_begin();
        foreach ($sampel as $r) {
            $this->db->query("DELETE FROM penyiapan_sampel WHERE id_sampel = ? AND is_active = ?", array($r['id_sampel'], '1'));
        }

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

    function tolak_deputi($data){
        $this->db->trans_begin();
        $this->db->query("UPDATE permintaan_uji SET keterangan = ? WHERE id_uji = ?", array($data['keterangan'], $data['id_uji2']));

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

    function saveAnalis($data){
        $this->db->trans_begin();
        $cek = $this->db->query("SELECT id_sampel FROM lhu WHERE id_sampel = ? AND is_active = '1'", $data['id_sampel'])->row_array()['id_sampel'];

        if(isset($cek)){
            $data['id_sampel_param'] = $cek;
            $this->db->query("UPDATE lhu SET id_sampel = ?, air = ?, abu = ?, protein_kasar = ?, lemak_kasar = ?, serat_kasar = ?, kalsium = ?, fosfor = ?, tdn = ?, ur = ?, af = ?, andf = ?, mbm = ?,
              mulai_uji_air = ?, mulai_uji_abu = ?, mulai_uji_protein_kasar = ?, mulai_uji_lemak_kasar = ?, mulai_uji_serat_kasar = ?, mulai_uji_kalsium = ?, mulai_uji_fosfor = ?, mulai_uji_tdn = ?, mulai_uji_ur = ?, mulai_uji_af = ?, mulai_uji_andf = ?, mulai_uji_mbm = ?,
              selesai_uji_air = ?, selesai_uji_abu = ?, selesai_uji_protein_kasar = ?, selesai_uji_lemak_kasar = ?, selesai_uji_serat_kasar = ?, selesai_uji_kalsium = ?, selesai_uji_fosfor = ?, selesai_uji_tdn = ?, selesai_uji_ur = ?, selesai_uji_af = ?, selesai_uji_andf = ?, selesai_uji_mbm = ?,
              last_modified_user = ? WHERE id_sampel = ?",$data);
        }else{
            $this->db->query("INSERT INTO lhu(id_sampel, air, abu, protein_kasar, lemak_kasar, serat_kasar, kalsium, fosfor, tdn, ur, af, andf, mbm,
              mulai_uji_air, mulai_uji_abu, mulai_uji_protein_kasar, mulai_uji_lemak_kasar, mulai_uji_serat_kasar, mulai_uji_kalsium, mulai_uji_fosfor, mulai_uji_tdn, mulai_uji_ur, mulai_uji_af, mulai_uji_andf, mulai_uji_mbm,
              selesai_uji_air, selesai_uji_abu, selesai_uji_protein_kasar, selesai_uji_lemak_kasar, selesai_uji_serat_kasar, selesai_uji_kalsium, selesai_uji_fosfor, selesai_uji_tdn, selesai_uji_ur, selesai_uji_af, selesai_uji_andf, selesai_uji_mbm,
              last_modified_user) VALUE
            (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", $data);
        }

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

    function getPenangananSampelArsip($type) {
        $query = "SELECT id_jenis, nama_jenis FROM jenis_penanganan_sampel WHERE is_active ='1' AND detail = 'Arsip'";

        if($type === '1') $query .= " AND id_jenis = '1'";
        if($type !== '1') $query .= " AND id_jenis <> '1'";

        $query = $this->db->query($query)->result_array();

        $result = '<option></option>';
        foreach($query as $r) {
            $result .= "<option value='".$r['id_jenis']."'>".$r['nama_jenis']."</option>";
        }

        return $result;
    }

    function udpateLhu($id_sampel, $tabel, $field, $field2){
        $query = "SELECT id_sampel, ".$field.", mulai_uji, selesai_uji FROM ".$tabel." WHERE id_sampel = ? AND is_active = '1'";
        $data = $this->db->query($query, $id_sampel)->row_array();
        $data['mulai_uji'] = toi_date01($data['mulai_uji']);
        $data['selesai_uji'] = toi_date01($data['selesai_uji']);
        $id_sampel = $this->db->query("SELECT id_sampel FROM lhu WHERE id_sampel = ? AND is_active = '1'",$id_sampel)->row_array()['id_sampel'];

        $this->db->trans_begin();
        if(isset($id_sampel)) {
            $query = "UPDATE lhu SET ".$field2." = ?, mulai_uji_".$field2." = ?, selesai_uji_".$field2." = ? WHERE id_sampel = ?";
            $this->db->query($query, array($data[$field], $data['mulai_uji'], $data['selesai_uji'], $data['id_sampel']));
        }else {
            $query = "INSERT INTO lhu (id_sampel, ".$field2.", mulai_uji_".$field2.", selesai_uji_".$field2.") VALUES
            (?,?,?,?)";
            $this->db->query($query, array($data['id_sampel'], $data[$field], $data['mulai_uji'], $data['selesai_uji']));
        }

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

    function get_data_kadar($table, $id_sampel){
        return $this->db->query("SELECT * FROM $table WHERE id_sampel = ? AND is_active = '1'", $id_sampel)->row_array();
    }

    function cek_kadar($tabel, $id_sampel){
        $query = 'SELECT id_kadar FROM '.$tabel.' WHERE id_sampel = ?';
        return $this->db->query($query, $id_sampel)->row_array()['id_kadar'];
    }

    function dataUji($id_uji) {
        return $this->db->query("SELECT * FROM permintaan_uji WHERE id_uji = ? AND is_active = '1'", $id_uji)->row_array();
    }

    function get_tdn($data){
      $param = array();
      $query = "SELECT t.*, ds.id_sampel as nomor_sampel, l.*, pu.registrasi_laboratorium as nomor_lab FROM data_sampel ds
        LEFT JOIN tdn t ON ds.id_sampel = t.id_sampel
        LEFT JOIN lhu l ON ds.id_sampel = l.id_sampel
        LEFT JOIN permintaan_uji pu ON pu.id_uji = ds.id_uji
        WHERE pu.id_uji = ? AND pu.id_status = ? AND pu.is_active = '1' AND ds.is_active = '1'
      ";

      $param[] = $data['id_uji'];
      $param[] = $data['status'];

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

    function set_tdn($param, $param_key){
      $this->db->trans_begin();
      $ctdn = $this->db->query("SELECT id_sampel FROM tdn WHERE id_sampel  = ?", $param['id_sampel'])->row_array()['id_sampel'];

      if(!isset($ctdn)){
        $query = "INSERT INTO tdn (".$param_key['insert'].") VALUES(".$param_key['insert_p'].")";
      }else {
        $query = "UPDATE tdn SET ".$param_key['update']." WHERE ".$param_key['update_p'];
      }

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

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

    function get_kadar_air_total($data){
      $param = array();
      $query = "SELECT ds.id_sampel as nomor_sampel, t.kadar_air3, t.pj_air3, t.tgl_selesai_perhitungan as tgl3, t.tgl_mulai_perhitungan as tgl3_mulai,
        l.kadar_air as kadar_air1, l.nama_pelaksana as pj_air1, l.tgl_pukul_setelah_dioven as tgl1,
        lh.air as kadar_air2, ka.nama_analis as pj_air2,lh.selesai_uji_air as tgl2,
        pu.registrasi_laboratorium as nomor_lab
        FROM data_sampel ds
        LEFT JOIN kadar_air_total t ON ds.id_sampel = t.id_sampel
        LEFT JOIN pelayuan_sampel l ON ds.id_sampel = l.id_sampel
        LEFT JOIN kadar_air ka ON ka.id_sampel = ds.id_sampel
        LEFT JOIN lhu lh ON lh.id_sampel = ds.id_sampel
        JOIN permintaan_uji pu ON pu.id_uji = ds.id_uji
        WHERE pu.id_uji = ? AND pu.id_status = ? AND pu.is_active = '1' AND ds.is_active = '1'
      ";

      $param[] = (int)$data['id_uji'];
      $param[] = $data['status'];

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

    function set_kat($data){
      $this->db->trans_begin();
      $param = array();

      $cek = $this->db->query("SELECT id_sampel FROM kadar_air_total WHERE id_sampel = ?", $data['id_sampel'])->row_array()['id_sampel'];

      if($cek){
        $query = "UPDATE kadar_air_total SET kadar_air3 = ?, pj_air3 = ?, tgl_mulai_perhitungan = NOW(), tgl_selesai_perhitungan =NOW() WHERE id_sampel = ?";
      }else{
        $query = "INSERT INTO kadar_air_total (kadar_air3, pj_air3, tgl_mulai_perhitungan, tgl_selesai_perhitungan, id_sampel) VALUES(?,?,NOW(),NOW(),?)";
      }

      $param[] = $data['kadar_air3'];
      $param[] = $data['pj_air3'];
      $param[] = $data['id_sampel'];

      $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 get_deret_fosfor($nomor_batch=null){
      $param = array();
      $query = "SELECT * FROM deret_fosfor WHERE 1";

      $query .=" AND nomor_batch = ?";
      $param[] = $nomor_batch;
      $result = $this->db->query($query, $param);
      return $result->result_array();
    }

    function get_deret_af($nomor_batch=null){
      $param = array();
      $query = "SELECT * FROM deret_af WHERE 1";

      $query .=" AND nomor_batch = ?";
      $param[] = $nomor_batch;
      $result = $this->db->query($query, $param);
      return $result->result_array();
    }

    function get_deret_ur($nomor_batch=null){
      $param = array();
      $query = "SELECT * FROM deret_ur WHERE 1";

      $query .=" AND nomor_batch = ?";
      $param[] = $nomor_batch;
      $result = $this->db->query($query, $param);
      return $result->result_array();
    }

    function get_deret_ca($nomor_batch=null){
      $param = array();
      $query = "SELECT * FROM deret_ca WHERE 1";

      $query .=" AND nomor_batch = ?";
      $param[] = $nomor_batch;
      $result = $this->db->query($query, $param);
      return $result->result_array();
    }

    function set_deret_ur($data){
      //print('<pre>'.print_r($data, true).'</pre>'); die();
      $this->db->trans_begin();
      $cek =  $this->db->query('SELECT id FROM deret_ur WHERE deret_std_urea = ? AND nomor_batch = ?', array($data['deret_std_urea'], $data['nomor_batch']))->row_array();

      $param = array();
      $param[] = $data['nomor_batch'];
      $param[] = $data['deret_std_urea'];
      $param[] = $data['abs'];

      if(!empty($cek['id'])){
        $query = "UPDATE deret_ur SET nomor_batch = ?, deret_std_urea = ?, `abs` = ? WHERE id = ?";
        $param[] = $cek['id'];
      }else{
        $query =  "INSERT INTO deret_ur (nomor_batch, deret_std_urea, `abs`) VALUES
        (?,?,?)";
      }

      $this->db->query($query, $param);
      //echo $this->db->last_query(); die();

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

    function set_deret_ca($data){
      $this->db->trans_begin();
      $cek =  $this->db->query('SELECT id FROM deret_ca WHERE deret_std_ca = ? AND nomor_batch = ?', array($data['deret_std_ca'], $data['nomor_batch']))->row_array();

      $param = array();
      $param[] = $data['nomor_batch'];
      $param[] = $data['deret_std_ca'];
      // $param[] = $data['volume_pipet_std_ca'];
      // $param[] = $data['labu_ukur_std_ca'];
      // $param[] = $data['std_ca40_ppm'];
      $param[] = $data['abs'];

      if(!empty($cek['id'])){
        $query = "UPDATE deret_ca SET nomor_batch = ?, deret_std_ca = ?, `abs` = ? WHERE id = ?";
        $param[] = $cek['id'];
      }else{
        $query =  "INSERT INTO deret_ca (nomor_batch, deret_std_ca, `abs`) VALUES
        (?,?,?)";
      }

      $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_deret_af($data){
      $this->db->trans_begin();
      $cek =  $this->db->query('SELECT id FROM deret_af WHERE level_standart = ? AND nomor_batch = ?', array($data['level_standart'], $data['nomor_batch']))->row_array();

      $param = array();
      $param[] = $data['nomor_batch'];
      $param[] = $data['level_standart'];
      $param[] = $data['abs'];

      if(!empty($cek['id'])){
        $query = "UPDATE deret_af SET nomor_batch = ?, level_standart = ?, `abs` = ? WHERE id = ?";
        $param[] = $cek['id'];
      }else{
        $query =  "INSERT INTO deret_af (nomor_batch, level_standart, `abs`) VALUES
        (?,?,?)";
      }

      $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_deret_fosfor($data){
      $this->db->trans_begin();
      $cek =  $this->db->query('SELECT id FROM deret_fosfor WHERE deret_std_p = ? AND nomor_batch = ?', array($data['deret_std_p'], $data['nomor_batch']))->row_array();

      $param = array();
      $param[] = $data['nomor_batch'];
      $param[] = $data['deret_std_p'];
      $param[] = $data['volume_pipet_std_p'];
      $param[] = $data['labu_ukur_std_p'];
      $param[] = $data['std_p_100_ppm'];
      $param[] = $data['abs'];

      if(!empty($cek['id'])){
        $query = "UPDATE deret_fosfor SET nomor_batch = ?, deret_std_p = ?, volume_pipet_std_p = ?, labu_ukur_std_p = ?, std_p_100_ppm = ?, `abs` = ? WHERE id = ?";
        $param[] = $cek['id'];
      }else{
        $query =  "INSERT INTO deret_fosfor (nomor_batch, deret_std_p, volume_pipet_std_p, labu_ukur_std_p, std_p_100_ppm, `abs`) VALUES
        (?,?,?,?,?,?)";
      }

      $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_tgl_persetujuan($id_uji){
      $this->db->trans_begin();
        $data_sampel = $this->db->query("SELECT id_sampel FROM data_sampel WHERE id_uji = ? AND is_active = '1'", $id_uji)->result_array();
        $parameter_uji = $this->db->query("SELECT id_jenis FROM parameter_uji WHERE is_active = '1'")->result_array();

        $tabel_uji = array('air', 'abu', 'protein_kasar', 'lemak_kasar', 'serat_kasar', 'kalsium', 'fosfor', 'tdn', 'ur', 'af', 'andf', 'mbm');

        if($data_sampel){
          foreach($data_sampel as $d){
            $p_uji = $this->db->query("SELECT jenis_pengujian FROM data_sampel WHERE id_sampel = ? AND is_active = '1'", $d['id_sampel'])->row_array()['jenis_pengujian'];
            if($p_uji){
              $p_uji = explode('|', $p_uji);
              foreach($p_uji as $p){
                if(!is_null($p) && $p != ''){
                  if(!in_array((int)$p, array(7,8,9,10,11))){
                    $tabel = 'kadar_'.$tabel_uji[(int)$p];
                    $this->db->query("UPDATE ".$tabel." SET persetujuan = ? WHERE id_sampel = ?", array(date('Y-m-d H:i:s'), $d));
                  }
                }
              }
            }
          }
        }
      if($this->db->trans_status() == false){
        $this->db->trans_rollback();
        return false;
      }else{
        $this->db->trans_commit();
        return true;
      }
    }

    function update_deret($data, $jenis_uji){
        //print('<pre>'.print_r($data,  true).'</pre>'); die();

      $this->db->trans_begin();
      $x = count($data['abs']);
      for($i=0; $i<$x; $i++){
        if($jenis_uji == 'fosfor'){
          $table = 'deret_fosfor';
          $field = 'c_std_p, deret_std_p';
        }
        if($jenis_uji == 'ur'){
          $table = 'deret_ur';
          $field = 'c_std_urea, deret_std_urea';
        }
        if($jenis_uji == 'af'){
          $table = 'deret_af';
          $field = 'total_af, level_standart';
        }
        if($jenis_uji == 'ca'){
          $table = 'deret_ca';
          $field = 'c_std_ca, deret_std_ca';
        }
        $param = array();
        $query = "INSERT INTO ".$table." (abs, intercept, slope, ".$field;

        if(in_array($jenis_uji, array('ca', 'ur', 'fosfor'))){
            $query .=", d_range, nomor_batch ";
            $query .= ') value(?,?,?,?,?,?,?)';
        }

        if($jenis_uji == 'af'){
            $query .= ", k_b, k_c, k_d, d_range, nomor_batch ";
            $query .= ') value(?,?,?,?,?,?,?,?,?,?)';
        }
        $param[] = $data['abs'][$i];
        $param[] = $data['intercept'];
        $param[] = $data['slope'];
        $param[] = $data['c_std'][$i];
        $param[] = $data['deret_level'][$i];

        if(in_array($jenis_uji, array('ca', 'ur', 'fosfor'))){
            $param[] = $data['d_range'];
        }
        if($jenis_uji == 'af'){
            $param[] = $data['k_b'][$i];
            $param[] = $data['k_c'][$i];
            $param[] = $data['k_d'][$i];
            $param[] = $data['d_range'];   
        }

        $param[] = $data['nomor_batch'];

        $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_intruksi($data){
      $this->db->trans_begin();
        $this->db->query("UPDATE lhu SET ".$data['nama_field']." = ? WHERE id_sampel = ?", array($data['ket'], $data['id_sampel']));
      if($this->db->trans_status() == false) {
        $this->db->trans_rollback();
        return false;
      }else {
        $this->db->trans_commit();
        return true;
      }
    }
}
