<?php
class KdpPengembangan extends Database {

   protected $mSqlFile= 'module/kdp_pengembangan/business/kdp_pengembangan.sql.php';
   
   function __construct($connectionNumber=0) {
      parent::__construct($connectionNumber);
   }
   
	public function GetDataUserByUsername($name)
   {
      if (isset($this->mUserByUserName[$name])) return $this->mUserByUserName[$name];
      $result = $this->Open($this->mSqlQueries['get_data_user_by_user_name'], array($name));  
      $this->mUserByUserName[$name] = $result[0];
		return $result[0];
   }
	
	public function GetDataKdpPengembangan($data, $start, $limit){
		$arrData = array(
			'%'.$data."%",
			'%'.$data."%",
			'%'.$data."%",
			$start,
			$limit
		);
		
      $result = $this->Open($this->mSqlQueries['get_data_kdp_pengembangan'], $arrData);	
		return $result;
	}
	
	public function GetDataKdpPengembanganCount(){
      $result = $this->Open($this->mSqlQueries['get_data_kdp_pengembangan_count'], array());	
		return $result[0]['total'];
	}
	
	public function GetDataKdpPengembanganById($id){
      $result = $this->Open($this->mSqlQueries['get_data_kdp_pengembangan_by_id'], array($id));
      
      if (empty($result)) return false;
      $return = $result[0];
      
      $return['detail_barang'] = $result;
		
		return $return;
	}
	
	public function GetDataKondisiBararang(){
      $result = $this->Open($this->mSqlQueries['get_data_kondisi_barang'], array());	
		return $result;
	}
	
	public function GetDataCaraBangun(){
      $result = $this->Open($this->mSqlQueries['get_data_cara_bangun'], array());	
		return $result;
	}
	
	public function UpdateKdp($data, $id){
		
		$this->StartTrans();
		
		$userId = Security::Instance()->mAuthentication->getcurrentuser()->GetUserId();

		$arrData = array(
			str_replace('.','',$data['kode_kdp']),
			$data['urut_awal'],
			$data['urut_akhir'],
			$data['cara_bangun'],
			$data['persen_kerja'],
			$data['tgl_pembukuan_year'].'-'.$data['tgl_pembukuan_mon'].'-'.$data['tgl_pembukuan_day'],
			$data['kondisi_barang'],
			$data['no_kontrak'],
			$data['nama_kontraktor'],
			$data['alamat_kontraktor'],
			$data['tgl_awal_kontrak_year'].'-'.$data['tgl_awal_kontrak_mon'].'-'.$data['tgl_awal_kontrak_day'],
			$data['tgl_akhir_kontrak_year'].'-'.$data['tgl_akhir_kontrak_mon'].'-'.$data['tgl_akhir_kontrak_day'],
			$data['nilai_rp'],
			$data['nilai_valas'],
			$id
		);

		if(!$this->Execute($this->mSqlQueries['update_aset_kdp_trans_kdp'], $arrData)){
			$this->EndTrans(false);
         return false;
		}
		
		if(empty($data['rincian'])){
			$this->EndTrans(false);
         return false;
		}
		
		$k2 = 0;
		foreach($data['rincian'] as $k=>$v){
			if($v['ID'] != ''){
				$issetId['rincian'][$k2] = $data['rincian'][$k];
				$getArrayDel[] = $data['rincian'][$k]['ID'];
			} else {
				$emptyId['rincian'][$k2++] = $data['rincian'][$k];
			}
			$k2++;
		}

		if(isset($issetId)){
			$idDelete = implode("', '", $getArrayDel);
			if(!$this->Execute($this->mSqlQueries['delete_aset_kdp_trans_kdp_spm'], array($idDelete, $id))){
				$this->EndTrans(false);
				return false;
			}

			foreach($issetId['rincian'] AS $key=>$val){
				$arrDataDetil = array(
					$val['NO_SP2D'],
					$val['TGL_SP2D'],
					$val['KL_BELANJA'],
					$val['NILAI_SPM'],
					$val['KETERANGAN'],
					$userId,
					$val['ID'],
					$id				
				);
		
				if(!$this->Execute($this->mSqlQueries['update_aset_kdp_trans_kdp_spm'], $arrDataDetil)){
					$this->EndTrans(false);
				   return false;
				}
			}
		} elseif(empty($issetId) && isset($emptyId)){
			if(!$this->Execute($this->mSqlQueries['delete_aset_kdp_trans_kdp_spm_by_id'], array($id))){
				$this->EndTrans(false);
				return false;
			}
		}
		
		if(isset($emptyId)){
			foreach($emptyId['rincian'] AS $keys=>$vals){
				$arrDataDetils = array(
					$id,
					$vals['NO_SP2D'],
					$vals['TGL_SP2D'],
					$vals['KL_BELANJA'],
					$vals['NILAI_SPM'],
					$vals['KETERANGAN'],
					$userId,				
				);
		
				if(!$this->Execute($this->mSqlQueries['insert_aset_kdp_trans_kdp_spm'], $arrDataDetils)){
					$this->EndTrans(false);
				   return false;
				}
			}
		}
		
		$this->EndTrans(true);
      return true;
	}
	
	public function DeleteKdp($id){
		$result = $this->Execute($this->mSqlQueries['insert_aset_kdp_trans_kdp_spm'], array($id));
		return $result;
	}
}
?>
