<?php
require_once GTFW_APP_DIR.'/module/inventarisasi/business/AppInventarisasi.class.php';

class Verifikasi extends Database
{
	protected $mSqlFile;
	
	function __construct($connectionNumber=0)
   {
      $this->mPID = (int) $_GET['id']->Integer()->Raw();
      $this->mSqlFile = 'module/'.Dispatcher::Instance()->mModule.'/business/Verifikasi.sql.php';
      $this->mImagePengadaanPath = 'images/pengadaan/';  // ambil dari pengadaan/bussiness/Komponen.class.php :: mImagePath
		$this->mImageGedungPath = 'images/gedung/'; // ambil dari gedung/response/ProcGedung.proc.class.php :: UploadImage ()
		parent::__construct($connectionNumber);
	}
   
   function ConvertCombo($combo)
   {
      foreach ($combo as $value) $return[$value['id']] = $value['name'];
      return $return;
   }
   
   // combo box
   function GetCbxStatusVerifikasi() {
      $result = $this->Open($this->mSqlQueries['get_cbx_status_verifikasi'], array());
      $array = eval('return '.str_replace('enum', 'array', $result[0]['Type']).';');
      foreach ($array as $val)
      {
         if ($val == 'sesuai' OR $val == 'tidak sesuai') continue;
         $combo[] = array('id' => $val, 'name' => ucfirst($val));
      }
      return $combo;
   }
   // ---------
   
   function GetPengadaanDetail()
   {
      $result = $this->Open($this->mSqlQueries['get_pengadaan_detail'], array($this->mPID));
      return $result[0];
   }
   
   function GetKomponenList ()
   {
      return $this->Open($this->mSqlQueries['get_komponen_list'], array($this->mPID));
   }
   
   function GetDataForDuplication ($idPengadaan)
   {
      $result = $this->Open($this->mSqlQueries['get_data_for_duplication'], array(intval(Security::Instance()->mAuthentication->GetCurrentUser()->GetUserId()),$idPengadaan));
      if (empty($result)) return false;
      else $return = $result[0];
      
      // nambah data biaya lain
      $return['biaya_lain'] = array();
      $result = $this->Open($this->mSqlQueries['get_biaya_lain_by_id'], array($idPengadaan));
      foreach ($result as $val)
         $return['biaya_lain'][$val['biayaId']] = $val['biayaNilai'];
      // ---------
      
      return $return;
   }
   
   /////////
   // Do Function
   /////////
   
   function Verifikasi ($idPengadaan, $data)
   {
      extract($data);
      $User = Security::Instance()->mAuthentication->GetCurrentUser();
      $this->StartTrans();
      
      // update tabel pengadaan master
      $arg = array
      (
         $idPengadaan
      );
      
      if (!$this->Execute($this->mSqlQueries['update_pengadaan_mst'], $arg))
      {
         $this->EndTrans(false);
         return 1;
      }
      // ---------
      
      $idList = array();
      // update tabel pengadaan detail
      foreach ($id as $val)
      {
         if ($name[$val] != 'sesuai') continue; // detail yang status bukan sesuai di skip
         
         if (!$this->Execute($this->mSqlQueries['update_pengadaan_detail'], array($statusVerifikasi, $val)))
         {
            $this->EndTrans(false);
            return 2;
         }
         
         $idList[] = $val;
      }
      // ---------
      
      if (empty($idList))
      {
         $this->EndTrans(true);
         return 0;
      }
      
      // hapus data inv_atk, inventarisasi atau gedung
      $tmp = $this->GetPengadaanDetail();
      if ($tmp['jenis_pengadaan'] == 'BHP') foreach ($idList as $value)
      {
         // hapus data inv_atk
         if
         (
            !$this->Execute($this->mSqlQueries['delete_atk_gudang'], array($value)) OR
            $this->Affected_Rows() == 0
         )
         {
            $this->EndTrans(false);
            return 3;
         }
         
         if (!$this->Execute($this->mSqlQueries['delete_atk_barang_dan_biaya'], array($value)))
         {
            $this->EndTrans(false);
            return 4;
         }
         // ---------
      }
      else
      {
         $Obj = new AppInventarisasi;
         foreach ($idList as $value)
         {
            if ($golbrgId[$value] == 3)
            {
               // hapus data gedung
               if (!$this->Execute($this->mSqlQueries['insert_into_gedung_from_pengadaan'], array(intval($User->GetUserId()), $value)))
               {
                  $this->EndTrans(false);
                  return 5;
               }
               
               if (!$this->Execute($this->mSqlQueries['insert_into_gedung_biaya_from_pengadaan_biaya'], array($value)))
               {
                  $this->EndTrans(false);
                  return 6;
               }
               
               $tmp = $this->Open($this->mSqlQueries['get_foto_pengadaan_by_id'], array($value));
               if (is_readable(GTFW_APP_DIR.'/'.$this->mImagePengadaanPath.'/'.$tmp[0]['denah']))
                  copy(GTFW_APP_DIR.'/'.$this->mImagePengadaanPath.'/'.$tmp[0]['denah'],GTFW_APP_DIR.'/'.$this->mImageGedungPath.'/'.$tmp[0]['denah']);
               if (is_readable(GTFW_APP_DIR.'/'.$this->mImagePengadaanPath.'/'.$tmp[0]['foto']))
                  copy(GTFW_APP_DIR.'/'.$this->mImagePengadaanPath.'/'.$tmp[0]['foto'],GTFW_APP_DIR.'/'.$this->mImageGedungPath.'/'.$tmp[0]['foto']);
               // ---------
            }
            else
            {
               // hapus data inventarisasi
               if (!$this->Execute($this->mSqlQueries['delete_inventarisasi_mst'], array($idPengadaan)))
               {
                  $this->EndTrans(false);
                  return 7;
               }
               // ---------
            }
         }
      }
      // ---------
      
      $this->EndTrans(true);
      return 0;
   }
}
?>
