<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'module/integrasi_data/business/AppIntegrasiDataSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'module/integrasi_data/business/AppIntegrasiDataAdmisi.class.php';

class ProcessIntegrasiData{
   var $_POST;
   var $Obj;
   var $pageView;
   var $cssDone = "notebox-done";
   var $cssFail = "notebox-warning";
   var $cssInfo = "notebox-info";
   var $cssAlert = "notebox-alert";
   var $return;
   var $decId;
   var $encId;
   var $tahunMasuk;
   var $semesterMasuk;
   var $prodiKode;
   var $nim;
   var $nama;


   function __construct() {
      $this->_POST = $_POST->AsArray();
      $this->objSia = new AppIntegrasiDataSia();
      $this->ObjAdmisi = new AppIntegrasiDataAdmisi();
      $this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
      $this->encId = Dispatcher::Instance()->Encrypt($this->decId);
      $this->pageView = Dispatcher::Instance()->GetUrl('integrasi_data', 'integrasiData', 'view', 'html');
   }
   
   
   function Send(){      
      $dataMhs=$this->ObjAdmisi->GetDataMahasiswaById($this->decId);
      $dataOrtu=$this->ObjAdmisi->GetDataOrtuMahasiswaById($this->decId);
      $this->tahunMasuk = $dataMhs[0]['mhs_thn_masuk'];
      if (empty($dataMhs[0]['mhs_sem_masuk']))
         $dataMhs[0]['mhs_sem_masuk'] = '1';
      $this->semesterMasuk = $dataMhs[0]['mhs_sem_masuk'];
      $this->prodiKode = $dataMhs[0]['mhs_prodi_kode'];
      $this->nim = $dataMhs[0]['mhs_niu'];
      $this->nama = $dataMhs[0]['mhs_nama'];
      
      $dataMhs[0]['mhs_nama'] = addslashes($dataMhs[0]['mhs_nama']);
      $dataMhs[0]['mhs_tempat_lahir'] = addslashes($dataMhs[0]['mhs_tempat_lahir']);
      $dataMhs[0]['mhs_alamat'] = addslashes($dataMhs[0]['mhs_alamat']);
      $dataMhs[0]['mhs_alamat_smta'] = addslashes($dataMhs[0]['mhs_alamat_smta']);
      $dataMhs[0]['mhs_almt_tempat_kerja'] = addslashes($dataMhs[0]['mhs_almt_tempat_kerja']);
      $dataMhs[0]['mhs_hobi'] = addslashes($dataMhs[0]['mhs_hobi']);
      $arrDataMhs=$dataMhs[0];
      foreach($dataMhs[0] as $k => $v){
         $arrDataMhs[$k.'_2'] = $dataMhs[0][$k];
      }

      $dataOrtu[0]['ortu_nama_ayah'] = addslashes($dataOrtu[0]['ortu_nama_ayah']);
      $dataOrtu[0]['ortu_nama_ibu'] = addslashes($dataOrtu[0]['ortu_nama_ibu']);
      $dataOrtu[0]['ortu_nama_wali'] = addslashes($dataOrtu[0]['ortu_nama_wali']);
      $dataOrtu[0]['ortu_alamat'] = addslashes($dataOrtu[0]['ortu_alamat']);
      $arrDataOrtu=$dataOrtu[0];
      $arrDataOrtu['ortu_mhs_id'] = $arrDataMhs['mhs_niu']; // di sia relasi mhs_ortu dengan mhs menggunakan nim
      
      foreach($dataOrtu[0] as $k => $v){
         $arrDataOrtu[$k.'_2'] = $dataOrtu[0][$k];
      }
      $arrDataOrtu['ortu_mhs_id_2'] = $arrDataMhs['mhs_niu']; // di sia relasi mhs_ortu dengan mhs menggunakan nim
      $addMhsToSia=$this->objSia->DoAddMahasiswaToSia($arrDataMhs);
      //print_r($arrDataMhs);exit;
      $addOrtuToSia=$this->objSia->DoAddOrangTuaMahasiswaToSia($arrDataOrtu);   
      // var_dump($addMhsToSia);
      // var_dump($addOrtuToSia);exit;
      $addData = $addMhsToSia && $addOrtuToSia;
      return $addData;
   }
   
   function InsertRegistrasi() {
      //--untuk insert tambahan field registrasi ke sia--//
      $dataMhsReg = $this->ObjAdmisi->GetDataMhsRegById($this->decId);
      $kuitansi = $dataMhsReg[0]['mhs_reg_kuitansi'];
      $jml_bayar = $dataMhsReg[0]['mhs_reg_jml_bayar'];
      $tgl_bayar = $dataMhsReg[0]['mhs_reg_tgl_bayar'];
      //--end tambahan--//
      
      $sem_id = $this->tahunMasuk . $this->semesterMasuk;
      $prodi_kode = $this->prodiKode;
      // cek apakah semester prodi sudah dibuat
      $semester_prodi = $this->objSia->GetCekSemesterProdi(array($sem_id, $prodi_kode));
      // jika sudah ada, insert jatah sks
      if (!empty($semester_prodi))
         $insert_jatah = $this->objSia->DoInsertJatahSks(array($this->nim, $sem_id, $prodi_kode));
      // insert registrasi
      $insert_registrasi = $this->objSia->DoInsertRegistrasi(array($this->nim, $kuitansi, $sem_id, $jml_bayar, $tgl_bayar));
      
      return $insert_registrasi;
   }  
   
   function InsertStatusBayar(){
      $dataMhsStatusBayar = $this->ObjAdmisi->GetDataStatusBayarById($this->decId);
      
      $idBayar = $dataMhsStatusBayar[0]['mhs_id_pembayaran'];
      $jnsBayar = $dataMhsStatusBayar[0]['mhs_jenis_pembayaran'];
      $isLunas = $dataMhsStatusBayar[0]['mhs_is_lunas'];
		
      $sem_id = $this->tahunMasuk . $this->semesterMasuk;
      
      //insert ke mahasiswa status bayar
      $insert_status_bayar = $this->objSia->DoInsertStatusBayar(array($this->nim, $sem_id, $idBayar, $jnsBayar, $isLunas));
      
      return $insert_status_bayar;
   }
   
   function SendAlert($alert, $sub_modul, $css='') {
      Messenger::Instance()->Send('integrasi_data', $sub_modul, 'view', 'html', array($this->_POST, $alert, $css),Messenger::NextRequest);
   }
   
   function SendToSia () {
      $dataMhsStatusBayar = $this->ObjAdmisi->GetDataStatusBayarById($this->decId);
      
      $this->objSia->StartTrans();
      $this->ObjAdmisi->StartTrans();
      $send_to_sia = $this->Send();
      if ($send_to_sia) {
         $update_is_sent = $this->ObjAdmisi->DoUpdateIsSent($this->decId);
         $insert_registrasi = $this->InsertRegistrasi();
         $insert_status_bayar = $this->InsertStatusBayar();
      }
      
		if(!empty($dataMhsStatusBayar)){
			$sent_data = $send_to_sia && $update_is_sent && $insert_registrasi && $insert_status_bayar;
		}else{
			$no_status_bayar = true;
		}
		
      $this->ObjAdmisi->EndTrans($sent_data);
      $this->objSia->EndTrans($sent_data);

      $aplikasi = 'Sistem Informasi Akademik';
      if ($sent_data === true) {
         $arr_pesan[] = array("Data mahasiswa dengan NIM \"$this->nim\" berhasil dikirim ke $aplikasi");
         $arr_css[] = array($this->cssDone);
      } else {
   		if ($no_status_bayar === true){			//tambahan cek status bayar
   			$arr_pesan[] = array("Data mahasiswa dengan nama \"$this->nama\" belum dapat dikirim ke $aplikasi karena data status bayar masih kosong");
   			$arr_css[] = array($this->cssFail);
   		} else {
   			$arr_pesan[] = array("Data mahasiswa dengan NIM \"$this->nim\" gagal dikirim ke $aplikasi");
   			$arr_css[] = array($this->cssFail);
   		}
      }
      
      $this->SendAlert($arr_pesan, 'IntegrasiData', $arr_css);
      return $this->pageView;
   }
   
}

?>
