<?php
class Kodefikasi extends Database
{
   protected $mSqlFile= 'module/kodefikasi_barang/business/Kodefikasi.sql.php';
   
	function __construct($connectionNumber=0)
   {
		parent::__construct($connectionNumber);
	}

	function GetComboGolongan()
   {
		$result = $this->Open($this->mSqlQueries['get_combo_golongan'], array());
		return $result;
	}

	function GetComboJnsBarang()
   {
		$result = $this->Open($this->mSqlQueries['get_combo_jenis_barang'], array());
		return $result;
	}

	function GetComboStnBarang()
   {
		$result = $this->Open($this->mSqlQueries['get_combo_satuan_barang'], array());
		return $result;
	}
   
   function GetDataBarangCount($data)
   {
		 if ($data['CARI_NAMA_BARANG'] == '')
         $sql = str_replace("%CARINAMA%", "barangNama", $this->mSqlQueries['get_data_barang_count']);
		else $sql = str_replace("%CARINAMA%", "nama", $this->mSqlQueries['get_data_barang_count']);
		/*if($data['CARI_JENIS_BARANG']=='')
			$data['CARI_JENIS_BARANG']='all';
			
		if($data['CARI_GOLONGAN']=='')
			$data['CARI_GOLONGAN']='all';
		*/
      extract ($data);
      $arg = array
      (
         $CARI_GOLONGAN, $CARI_GOLONGAN,
         $CARI_BIDANG_ID, $CARI_BIDANG_NAME,
         $CARI_KELOMPOK_ID, $CARI_KELOMPOK_NAME,
         $CARI_SUB_KELOMPOK_ID, $CARI_SUB_KELOMPOK_NAME,
         "%$CARI_NAMA_BARANG%",
         $CARI_JENIS_BARANG, $CARI_JENIS_BARANG
      );
      
      $result = $this->Open($sql, $arg);
      if (is_array($result))
         return $result[0]['total'];
      else return 0;
   }
   
   function GetDataBarang ($data, $start = 0, $limit = 1000)
   {
		 if ($data['CARI_NAMA_BARANG'] == '')
         $sql = str_replace("%CARINAMA%", "barangNama", $this->mSqlQueries['get_data_barang']);
		else $sql = str_replace("%CARINAMA%", "nama", $this->mSqlQueries['get_data_barang']);
   
		/*if($data['CARI_JENIS_BARANG']=='')
			$data['CARI_JENIS_BARANG']='all';
			
		if($data['CARI_GOLONGAN']=='')
			$data['CARI_GOLONGAN']='all';
      */
      extract ($data);
      $arg = array
      (
         $CARI_GOLONGAN, $CARI_GOLONGAN,
         $CARI_BIDANG_ID, $CARI_BIDANG_NAME,
         $CARI_KELOMPOK_ID, $CARI_KELOMPOK_NAME,
         $CARI_SUB_KELOMPOK_ID, $CARI_SUB_KELOMPOK_NAME,
         "%$CARI_NAMA_BARANG%",
         $CARI_JENIS_BARANG, $CARI_JENIS_BARANG,
         $start, $limit
      );
      $result = $this->Open($sql , $arg);
      return $result;
   }
   
   function GetDataBarangCountOld($data){
      extract ($data);
      
      if (!is_numeric($CARI_GOLONGAN))
         $CARI_GOLONGAN = "";
      
      if (is_numeric($CARI_BIDANG_ID))
         $sqlBid = "  ";
      
      if (is_numeric($CARI_KELOMPOK_ID))
         $sqlKel =  "  ";
      
      if (is_numeric($CARI_SUB_KELOMPOK_ID))
         $sqlSub = "  ";
      
      if (!is_numeric($CARI_JENIS_BARANG))
         $CARI_JENIS_BARANG = '';
      
      $arg = array
      (
         '%'.$CARI_NAMA_BARANG.'%',
         '%'.$CARI_GOLONGAN.'%',
         '%'.$CARI_BIDANG_ID.'%',
         '%'.$CARI_KELOMPOK_ID.'%',
         '%'.$CARI_SUB_KELOMPOK_ID.'%',
         '%'.$CARI_JENIS_BARANG.'%'
      );

      $result = $this->Open($this->mSqlQueries['get_data_barang_count'],$arg);

      if (is_array($result))
         return $result[0]['total'];
      else return 0;
   }
   
   function GetDataBarangOld($data, $start = 0, $limit = 1000)
   {
   
      extract ($data);

      
      if (!is_numeric($CARI_GOLONGAN))
      	$CARI_GOLONGAN = "";
      
      if (is_numeric($CARI_BIDANG_ID))
         $sqlBid = "  ";
      
      if (is_numeric($CARI_KELOMPOK_ID))
         $sqlKel =  "  ";
      
      if (is_numeric($CARI_SUB_KELOMPOK_ID))
         $sqlSub = "  ";
      
      if (!is_numeric($CARI_JENIS_BARANG))
      	$CARI_JENIS_BARANG = '';
      
      $arg = array
      (
         '%'.$CARI_NAMA_BARANG.'%',
      	'%'.$CARI_GOLONGAN.'%',
         '%'.$CARI_BIDANG_ID.'%',
         '%'.$CARI_KELOMPOK_ID.'%',
         '%'.$CARI_SUB_KELOMPOK_ID.'%',
         '%'.$CARI_JENIS_BARANG.'%',
         $start,
         $limit
      );
      /*printf($this->mSqlQueries['get_data_barang'],'%'.$CARI_NAMA_BARANG.'%',
         '%'.$CARI_GOLONGAN.'%',
         '%'.$CARI_BIDANG_ID.'%',
         '%'.$CARI_KELOMPOK_ID.'%',
         '%'.$CARI_SUB_KELOMPOK_ID.'%',
         '%'.$CARI_JENIS_BARANG.'%',
         $start,
         $limit);*/
      $result = $this->Open($this->mSqlQueries['get_data_barang'],$arg);
      return $result;
   }
   
   function GetKelompokList()
   {
		$result = $this->Open($this->mSqlQueries['get_kelompok_list'], array());
      if (count($result) == 0) return array();
      foreach ($result as $value)
         $return[$value['id']] = $value;
      return $return;
   }
   
   function GetSubKelompokList()
   {
		$result = $this->Open($this->mSqlQueries['get_sub_kelompok_list'], array());
      foreach ($result as $value)
         $return[$value['idKelompok']][$value['id']] = $value;
      return $return;
   }
   
   function GetDataKelompokById($id)
   {
		$result = $this->Open($this->mSqlQueries['get_data_kelompok_by_id'], array($id));
      if (count($result) == 0) return array();
      return $result[0];
   }
   
   function GetDataSubKelompokById($id)
   {
		$result = $this->Open($this->mSqlQueries['get_data_sub_kelompok_by_id'], array($id));
      if (count($result) == 0) return array();
      return $result[0];
   }
   
   function GetDataBarangById($id)
   {
		$result = $this->Open($this->mSqlQueries['get_data_barang_by_id'], array($id));
      if (count($result) == 0) return array();
      return $result[0];
   }
   
   function GetDetailBarangById($id)
   {
		$result = $this->Open($this->mSqlQueries['get_detail_barang_by_id'], array($id));
      if (count($result) == 0) return false;
      return $result[0];
   }
   
   function CekKodeKelompok ($id, $kode, $parent)
   {
      $result = $this->Open($this->mSqlQueries['cek_kode_kelompok'], array((int)$kode, (int)$parent));
      if (empty($result) or $result[0]['id'] == $id) return false;
      else return true;
   }
   
   function CekKodeSubKelompok ($id, $kode, $parent)
   {
      $result = $this->Open($this->mSqlQueries['cek_kode_sub_kelompok'], array((int)$kode, (int)$parent));
      if (empty($result) or $result[0]['id'] == $id) return false;
      else return true;
   }
   
   function CekKodeBarang ($id, $kode, $parent)
   {
      $result = $this->Open($this->mSqlQueries['cek_kode_barang'], array((int)$kode, (int)$parent));
      if (empty($result) or $result[0]['id'] == $id) return false;
      else return true;
   }
   
   // Do Query....
   function AddKelompok($data)
   {
      extract($data);
      $result = $this->Execute($this->mSqlQueries['add_kelompok'], array($kode, $nama, $bidang));
      if ($result) return 0;
      else return 1;
   }
   
   function UpdateKelompok($data)
   {
      extract($data);
      $result = $this->Execute($this->mSqlQueries['update_kelompok'], array($kode, $nama, $bidang, $_GET['id']->Integer()->Raw()));
      if ($result) return 0;
      else return 1;
   }
   
   function DeleteKelompok($id)
   {
      $result = $this->Execute($this->mSqlQueries['delete_kelompok'], array($id));
      if ($result) return 0;
      else return 1;
   }
   
   function AddSubKelompok($data)
   {
      extract($data);
      $result = $this->Execute($this->mSqlQueries['add_sub_kelompok'], array($kode, $nama, $kelompok));
      if ($result) return 0;
      else return 1;
   }
   
   function UpdateSubKelompok($data)
   {
      extract($data);
      $result = $this->Execute($this->mSqlQueries['update_sub_kelompok'], array($kode, $nama, $kelompok, $_GET['id']->Integer()->Raw()));
      if ($result) return 0;
      else return 1;
   }
   
   function DeleteSubKelompok($id)
   {
      $result = $this->Execute($this->mSqlQueries['delete_sub_kelompok'], array($id));
      if ($result) return 0;
      else return 1;
   }
   
   function AddBarang($data)
   {
      extract($data);
      $result = $this->Execute($this->mSqlQueries['add_barang'], array($kode, $nama, $jenisBarang, $satuanBarang, $subKelompok, $hps, $fotoBarang, $umurEkonomis, $nilaiResidu));
      if ($result) return 0;
      else return 1;
   }
   
   function UpdateBarang($data)
   {
      extract($data);
      $result = $this->Execute($this->mSqlQueries['update_barang'], array($kode, $nama, $jenisBarang, $satuanBarang, $subKelompok,$hps, $fotoBarang, $umurEkonomis, $nilaiResidu, $_GET['id']->Integer()->Raw()));
      if ($result) return 0;
      else return 1;
   }
   
   function DeleteBarang($id)
   {
      $result = $this->Execute($this->mSqlQueries['delete_barang'], array($id));
      if ($result) return 0;
      else return 1;
   }
}
?>
