<?php
class MitraKerja extends Database
{
   protected $mSqlFile= 'module/mitra_kerja/business/MitraKerja.sql.php';
   
	function __construct($connectionNumber=0) {
		parent::__construct($connectionNumber);
		
	}
   
	# multiunit
   function GetMitraView ($mkNama = '', $mkAlamat = '', $mkCP = '', $mkBidUsaha = 'all', $start = 0, $limit = 1000)
   {
		if ($mkBidUsaha == 'all' and $mkCP == ''){
			$sql = str_replace("%OPERATOR%", "AND", $this->mSqlQueries['get_mitra_view']);
			$sql = str_replace("%OPERATOR2%", "OR", $sql);
		} elseif ($mkBidUsaha == 'all' and $mkCP != '' ){
			$sql = str_replace("%OPERATOR%", "OR", $this->mSqlQueries['get_mitra_view']);
			$sql = str_replace("%OPERATOR2%", "AND", $sql);
		} elseif ($mkBidUsaha != 'all' and $mkCP == '' ){
			$sql = str_replace("%OPERATOR%", "AND", $this->mSqlQueries['get_mitra_view']);
			$sql = str_replace("%OPERATOR2%", "AND", $sql);
		} elseif ($mkBidUsaha != 'all' and $mkCP != '' ) {	
			$sql = str_replace("%OPERATOR%", "AND", $this->mSqlQueries['get_mitra_view']);
			$sql = str_replace("%OPERATOR2%", "AND", $sql);
		}

	  $kodeSistem = $this->GetKodeSistemByUserId();
      $result = $this->Open($sql, array($mkBidUsaha, "%$mkCP%", "%$mkNama%", "%$mkAlamat%", "$kodeSistem","$kodeSistem.%",$start, $limit));
		
      return $result;
   }
   
   function GetKodeSistemByUserId(){
   		$userId = Security::Instance()->mAuthentication->GetCurrentUser()->GetUserId();
   		$result = $this->Open($this->mSqlQueries['get_kode_sistem_by_user_id'],array($userId));
   		return $result[0]['unitkerjaKodeSistem'];
   }
   
   function GetCount(){
      $result = $this->Open($this->mSqlQueries['get_count'],array());
      return $result[0]['total'];
   }
   
   function GetCountMitraView ($mkNama = '', $mkAlamat = '', $mkCP = '', $mkBidUsaha = 'all')
   {
       if ($mkBidUsaha == 'all' and $mkCP == ''){
         $sql = str_replace("%OPERATOR%", "OR", $this->mSqlQueries['get_count_mitra_view']);
		 $sql = str_replace("%OPERATOR2%", "OR", $sql);}
	
     elseif ($mkBidUsaha == 'all' and $mkCP != '' ){
		$sql = str_replace("%OPERATOR%", "OR", $this->mSqlQueries['get_count_mitra_view']);
		$sql = str_replace("%OPERATOR2%", "AND", $sql);
		}
	 else {	
		$sql = str_replace("%OPERATOR%", "AND", $this->mSqlQueries['get_count_mitra_view']);
		$sql = str_replace("%OPERATOR2%", "AND", $sql);
		}
      
      $result = $this->Open($sql, array($mkBidUsaha, "%$mkCP%", "%$mkNama%", "%$mkAlamat%"));
      return count($result);
   }
   
   function GetBidangUsahaList ()
   {
      $result = $this->Open($this->mSqlQueries['get_bidang_usaha_list'], array());
      return $result;
   }
   
    function GetBankList ()
   {
      $result = $this->Open($this->mSqlQueries['get_bank_list'], array());
      return $result;
   }
   
     function GetCpList ($mkId)
   {
      $result = $this->Open($this->mSqlQueries['get_cp_list'], array($mkId));
      return $result;
   }
   
   function GetMitraById ($mkId)
   {
      $result = $this->Open($this->mSqlQueries['get_mitra_by_id'], array($mkId));
      
      if (!is_array($result) OR count($result) == 0) return false;
      else $return = $result[0];
      
      $return['mkBidUsaha'] = $this->GetMKBidangUsahaById($mkId);
	  $return['mkBank'] = $this->GetMKBankById($mkId);
      return $return;
   }
   
   function GetMKBidangUsahaById ($mkId)
   {
      $result = $this->Open($this->mSqlQueries['get_mitra_bidang_usaha_by_id'], array($mkId));
      if (count($result) == 0) return $result;
      
      foreach($result as $value) $return[] = $value['buId'];
      return $return;
   }
   
   // untuk bank---
    function GetMKBankById ($mkId)
   {
      $result = $this->Open($this->mSqlQueries['get_mitra_bank_by_id'], array($mkId));
      if (count($result) == 0) return $result;
      
      foreach($result as $value) $return[] = $value['bankId'];
      return $return;
   }
   //------untuk contactPerson------------------
   function GetMkCpById ($mkId)
   {
      $result = $this->Open($this->mSqlQueries['get_cp_by_id'], array($mkId)); 
      return $result;
   }
   
    function GetLastMitraId ()
   {
      @$result = $this->Open($this->mSqlQueries['get_last_mitra_id'], array($mkId));
      return $result[0]['mkId'];
   }
   
   // Do category....
   function UpdateMitraKerja($data)
   {
      $return = 0;
      $mkId = $_GET['mkId']->Integer()->Raw();
      
      if ($this->GetMitraById($mkId) === false) return 1;
      
      // Updating Mitra Kerja Information
      $result = $this->Execute($this->mSqlQueries['update_mitra'], array(	$data['mkNama'],
																		    $data['mkAlamat'],
																			$data['mkHp'],
																			$data['mkTelp'],
																			$data['mkFax'],
																			$data['mkCity'],
																			$data['mkZipCode'],
																			$data['mkCountry'],
																			$data['mkEmail'],
																			$data['mkWeb'],
																		    $data['mkWsAddress'],
																		    $data['mkWsAddressCity'],
																		    $data['mkWsZipCode'],
																		    $data['mkWsTelepon'],
																		    $data['mkWsFax'],
																		    $data['mkNpwp'],
																		    $data['mkSiup'],
																		    $data['mkTdp'],
																		    $data['mkAoa'],
																		    $data['mkDocProfile'],
																		    $data['mkDataPrepared'], 
																			$mkId));
      if ($result == false) $return |= 2;
      
      // Updating Mitra Kerja Bidang Usaha
      $OLD = $this->GetMKBidangUsahaById($mkId);
      $NEW = isset($data['mkBidUsaha']) ? $data['mkBidUsaha'] : array();
      $BUDelete = array();
      $BUAdd = array();
      
      foreach ($OLD as $value) if (!in_array($value, $NEW))
         $BUDelete[] = $value;
      foreach ($NEW as $value) if (!in_array($value, $OLD))
         $BUAdd[] = $value;
      
      $result = $this->DeleteMKBidUsahaById($mkId, $BUDelete);
      if (!$result) $return |= 4;
      $result = $this->AddMKBidUsahaById($mkId, $BUAdd);
      if (!$result) $return |= 4;
	  
	  //updating bank
	  $OLDBANK = $this->GetMKBankById($mkId);
      $NEWBANK = isset($data['mkBank']) ? $data['mkBank'] : array();
      $BANKDelete = array();
      $BANKAdd = array();
      
      foreach ($OLDBANK as $value) if (!in_array($value, $NEWBANK))
         $BANKDelete[] = $value;
      foreach ($NEWBANK as $value) if (!in_array($value, $OLDBANK))
         $BANKAdd[] = $value;
      
      $result = $this->DeleteMKBankById($mkId, $BANKDelete);
      if (!$result) $return |= 5;
      $result = $this->AddMKBankById($mkId, $BANKAdd);
      if (!$result) $return |= 5;
	  
	  //updating  Contact Person 
	  $CPDelete = $this->GetMKCpById($mkId);
      if (count($CPDelete) > 0)
      {	
		 $result = $this->DeleteMKCpById($mkId, $CPDelete);
	     $result = $this->DeleteMKCpRefById($CPDelete);
      }
	  
	  $cpAdd = isset($data['cp']) ? $data['cp'] : array();
	  $result = $this->AddCpRef($mkId, $cpAdd);
		// if (!$result) $return |= 6;
      
      return $return;
   }
   
   function AddMitraKerja($data)
   {
      $return = 0;
      // Adding Mitra Kerja
      $result = $this->Execute($this->mSqlQueries['add_mitra'], array(		$data['mkNama'],
																		    $data['mkAlamat'],
																			$data['mkHp'],
																			$data['mkTelp'],
																			$data['mkFax'],
																			$data['mkCity'],
																			$data['mkZipCode'],
																			$data['mkCountry'],
																			$data['mkEmail'],
																			$data['mkWeb'],
																		    $data['mkWsAddress'],
																		    $data['mkWsAddressCity'],
																		    $data['mkWsZipCode'],
																		    $data['mkWsTelepon'],
																		    $data['mkWsFax'],
																		    $data['mkNpwp'],
																		    $data['mkSiup'],
																		    $data['mkTdp'],
																		    $data['mkAoa'],
																		    $data['mkDocProfile'],
																		    $data['mkDataPrepared']
																		));
	
      if ($result == false) $return |= 2;
      
      // Adding Mitra Kerja Bidang Usaha
      $mkId = $this->GetLastMitraId();
      $BUAdd = isset($data['mkBidUsaha']) ? $data['mkBidUsaha'] : array();
      $result = $this->AddMKBidUsahaById($mkId, $BUAdd);
	  if (!$result) $return |= 4;
    
	  //Adding Bank Relation
	  $bankAdd = isset($data['mkBank']) ? $data['mkBank'] : array();
	  $result = $this->AddMKBankById($mkId, $bankAdd);
	  if (!$result) $return |= 5;
	  
	  //Adding CP ref 
	  $cpAdd = isset($data['cp']) ? $data['cp'] : array();
	  $result = $this->AddCpRef($mkId, $cpAdd);
	 // if (!$result) $return |= 6;
	  
	  return $return;
   }
   
   function AddMKBidUsahaById($mkId, $BU)
   {
      if (!is_array($BU) OR count($BU) < 1) return true;
      $string = implode(', ', array_fill (0, count($BU), '(%d, %d)'));
      $newQuery = str_replace('%VALUES%', $string, $this->mSqlQueries['add_mkbu_by_mkid']);
      
      foreach ($BU as $value)
      {
         $input[] = $mkId;
         $input[] = $value;
      }
      
      $result = $this->Execute($newQuery, $input);
      return $result;
   }
   
   function AddMKBankById($mkId, $bank)
   {
      if (!is_array($bank) OR count($bank) < 1) return true;
      $string = implode(', ', array_fill (0, count($bank), '(%d, %d)'));
      $newQuery = str_replace('%VALUES%', $string, $this->mSqlQueries['add_mkbank_by_mkid']);
      
      foreach ($bank as $value)
      {
         $input[] = $mkId;
         $input[] = $value;
      }
      
      $result = $this->Execute($newQuery, $input);
      return $result;
   }
   
    function AddCpRef($mkId, $cp)
   {
	  $idUser=Security::Instance()->mAuthentication->getcurrentuser()->GetUserId();
		
	  for($i=1;$i<=sizeof($cp);$i++){
	  $result = $this->Execute($this->mSqlQueries['add_cp_ref'], array($cp[$i]['nama'], $cp[$i]['alamat'], $cp[$i]['hp'], $cp[$i]['telp'], $cp[$i]['email'], $cp[$i]['foto'], $idUser));
	  $result = $this->Execute($this->mSqlQueries['add_cp_relation'], array($mkId));
	  }
	  return @$result;
   }
   
   function DeleteMKBidUsahaById($mkId, $BU)
   {
      if (!is_array($BU) OR count($BU) < 1) return true;
      $string = implode(', ', array_fill (0, count($BU), '%d'));
      $newQuery = str_replace('%IN%', $string, $this->mSqlQueries['delete_mkbu_by_mkid']);
      
      array_unshift($BU, $mkId);
      $result = $this->Execute($newQuery, $BU);
      return $result;
   }
   
    function DeleteMKBankById($mkId, $BANK)
   {
      if (!is_array($BANK) OR count($BANK) < 1) return true;
      $string = implode(', ', array_fill (0, count($BANK), '%d'));
      $newQuery = str_replace('%IN%', $string, $this->mSqlQueries['delete_mkbank_by_mkid']);
      
      array_unshift($BANK, $mkId);
      $result = $this->Execute($newQuery, $BANK);
      return $result;
   }
   
    function DeleteMKCpById($mkId, $CP)
   {
      if (!is_array($CP) OR count($CP) < 1) return true;
      $string = implode(', ', array_fill (0, count($CP), '%d'));
      $newQuery = str_replace('%IN%', $string, $this->mSqlQueries['delete_mkcp_by_mkid']);
      
      array_unshift($CP, $mkId);
      $result = $this->Execute($newQuery, $CP);
      return $result;
   }
   
    function DeleteMKCpRefById($CP)
   {
      if (!is_array($CP) OR count($CP) < 1) return true;
      $string = implode(', ', array_fill (0, count($CP), '%d'));
      $newQuery = str_replace('%IN%', $string, $this->mSqlQueries['delete_mkcpref_by_mkid']);
      
     // array_unshift($CP, $mkId);
      $result = $this->Execute($newQuery, $CP);
      return $result;
   }
   
   function DeleteMitra($mkId)
   {
      $return = 0;
      
	  //untuk delete bidang usaha
      $BUDelete = $this->GetMKBidangUsahaById($mkId);
      if (count($BUDelete) > 0)
      {
         $result = $this->DeleteMKBidUsahaById($mkId, $BUDelete);
         if (!$result)
         {
            $return |= 3;
            return $return;
         }
      }
	  
	  //untuk delete bank
	  $BANKDelete = $this->GetMKBankById($mkId);
      if (count($BANKDelete) > 0)
      {
         $result = $this->DeleteMKBankById($mkId, $BANKDelete);
         if (!$result)
         {
            $return |= 4;
            return $return;
         }
      }
	  
	  //untuk delete contact person
	  $CPDelete = $this->GetMKCpById($mkId);
      if (count($CPDelete) > 0)
      {
         $result = $this->DeleteMKCpById($mkId, $CPDelete);
         if (!$result)
         {
            $return |= 5;
            return $return;
         }
      }
		 $result = $this->DeleteMKCpRefById($CPDelete);
      
      $result = $this->Execute($this->mSqlQueries['delete_mitra'], array($mkId));
      if (!$result) $return |= 2;
      
      return $return;
   }

}
?>
