<?php
/*
Author : Nikolius
Email : n1colius.lau@gmail.com
Date : 23-Jul-2015 12:07 PM
*/
require_once $cfg->mValue['docroot'] . "module/sia_setting/business/sia_setting.class.php";
class Nilai extends SiaSetting {
   function Nilai(&$configObject, $dataProceed) {
      SiaSetting::SiaSetting($configObject, $dataProceed);
      $this->AppendSqlQueries("module/nilai/business/nilai.sql.php");
   }

   function GetDataListForInsertSync(){
      return $this->GetAllDataAsArray($this->mSqlQueries['GetDataListForInsertSync'], array());
   }

   function GetDataListForInsertSyncByLimit(){
      $dataParam = $this->GetProperty("DataProceed");

      $p = array(
         (int) $dataParam[0]['offsetProses'],
         (int) $dataParam[0]['limitProses']
      );
      return $this->GetAllDataAsArray($this->mSqlQueries['GetDataListForInsertSyncByLimit'], $p);
   }

   function GetDataListForInsertSyncCountRow(){
      return $this->GetAllDataAsArray($this->mSqlQueries['GetDataListForInsertSyncCountRow'], array());
   }

   function SyncData(){
      $dataList = $this->GetProperty("DataProceed");
      $this->StartTransaction();
      $prosesInsertUpdate = TRUE;
      $totalAfektedRow = 0;

      for ($i=0; $i < count($dataList); $i++) {
         //get gt_id yaitu krsdtId terlebih dahulu
         $p = array(
            $dataList[$i]['id_kls'],
            $dataList[$i]['id_reg_pd']
         );
         $dataId = $this->GetAllDataAsArray($this->mSqlQueries['getDataIdKrsdtId'], $p);
         $gt_id = $dataId[0]['krsdtId'];

         //check apakah harus insert or update
         $p = array(
            $dataList[$i]['id_kls'],
            $dataList[$i]['id_reg_pd']
         );
         $cekInsertUpdate = $this->GetAllDataAsArray($this->mSqlQueries['cekInsertUpdate'], $p);
         if($cekInsertUpdate[0]['id_kls'] != ""){
            //update
            $p = array(
               #$dataList[$i]['asal_data'],
               '7',
               $dataList[$i]['nilai_angka'],
               $dataList[$i]['nilai_huruf'],
               $dataList[$i]['nilai_indeks'],
               $gt_id,
               $dataList[$i]['id_kls'],
               $dataList[$i]['id_reg_pd']
            );
            $proses = $this->ExecuteUpdateQuery($this->mSqlQueries['syncDataUpdate'], $p);
         }else{
            //insert
            $p = array(
               $dataList[$i]['id_kls'],
               $dataList[$i]['id_reg_pd'],
               '7',
               $dataList[$i]['nilai_angka'],
               $dataList[$i]['nilai_huruf'],
               $dataList[$i]['nilai_indeks'],
               $gt_id
            );
            $proses = $this->ExecuteUpdateQuery($this->mSqlQueries['syncDataInsert'], $p);
         }

         if($proses == FALSE) $prosesInsertUpdate = FALSE;
         $totalAfektedRow += $this->getAffectedRow();
      }

      $this->CompleteTransaction($prosesInsertUpdate);
      return $totalAfektedRow;
   }
}
?>