<?php

/*
@ClassName : PenambahanAtk
@Copyright : PT Gamatechno Indonesia
@Analyzed By : Nanang Ruswianto <nanang@gamatechno.com>
@Author By : Dyan Galih <galih@gamatechno.com>
@Version : 0.1
@StartDate : 2010-12-08
@LastUpdate : 2010-12-08
@Description : Penambahan Atk
*/

class PenambahanAtk extends Database
{

   protected $mSqlFile;
   function __construct($connectionNumber = 0)
   {
      $this->mSqlFile = 'module/pengolahan_atk_gedung/business/penambahan_atk.sql.php';
      parent::__construct($connectionNumber);
      
   }

   private function GetAtkMstByBrgId($barangId)
   {
      $result = $this->Open($this->mSqlQueries['get_atk_mst_id'], array(
         $barangId
      ));

      return $result['0']['invAtkMstId'];
   }

   private function AddAtkMst($data)
   {
      $result = $this->Execute($this->mSqlQueries['penambahan_atk_mst'], $data);

      return $this->AffectedRows();
   }

   private function GetAtkDetId($data)
   {
      $result = $this->Open($this->mSqlQueries['get_atk_det_id'], $data);

      return $result['0']['invAtkDetId'];
   }

   private function UpdateAtkDetil($data)
   {
      $result = $this->Execute($this->mSqlQueries['update_atk_detil'], $data);

      return $this->AffectedRows();
   }

   private function AddAtkDetil($data)
   {
      $result = $this->Execute($this->mSqlQueries['penambahan_atk_detil'], $data);

      return $this->AffectedRows();
   }

   private function AddAtkBiaya($data)
   {
      $result = $this->Execute($this->mSqlQueries['penambahan_atk_biaya'], $data);

      return $this->AffectedRows();
   }

   private function GetAtkBiayaByMstId($id)
   {
      $result = $this->Open($this->mSqlQueries['get_atk_biaya_by_mst_id'], array(
         $id
      ));

      return $result['0']['invAtkBiayaId'];
   }

	private function GetAtkBrgByMstId($id)
   {
      $result = $this->Open($this->mSqlQueries['get_atk_brg_by_mst_id'], array($id));
      return $result;
   }
	
   private function UpdateAtkBiaya($data)
   {
      $result = $this->Execute($this->mSqlQueries['update_atk_biaya'], $data);

      return $this->AffectedRows();
   }

   private function AddAtkBrg($data)
   {
      $result = $this->Execute($this->mSqlQueries['penambahan_atk_brg'], $data);

      return $this->AffectedRows();
   }
	
	private function UpdateAtkBrg($data)
   {
      $result = $this->Execute($this->mSqlQueries['perubahan_atk_brg'], $data);
      return $this->AffectedRows();
   }

   private function AddAtkGudang($data)
   {
      $result = $this->Execute($this->mSqlQueries['penambahan_atk_gudang'], $data);
      return $this->AffectedRows();
   } 
	
	private function UpdateAtkGudangMax($data)
   {
      $result = $this->Execute($this->mSqlQueries['update_atk_gudang_max'], $data);
      return $this->AffectedRows();
   }
	
	private function UpdateAtkGudang($data)
   {
      $result = $this->Execute($this->mSqlQueries['perubahan_atk_gudang'], $data);

      return $this->AffectedRows();
   }

   private function GetMaxAtkBrg()
   {
      $result = $this->Open($this->mSqlQueries['get_atk_max_brg'], array());

      return $result[0]['invBrgAtkId'];
   }
	
	private function GetMaxAtkGdgByDetId($detId, $ruangId)
   {
      $result = $this->Open($this->mSqlQueries['get_atk_max_gdg_by_det_id'], array($detId, $ruangId));
		return $result[0]['jumlah'];
   }

   private function AddLogAtk($data){
      $result = $this->Execute($this->mSqlQueries['add_log_atk'], $data);
      return $this->AffectedRows();
   }
	
	private function AddLogAtkUpdate($data){
      $result = $this->Execute($this->mSqlQueries['add_log_atk_update'], $data);
      return $this->AffectedRows();
   }

   public function AddAtk($data)
   {
      $this->StartTrans();
      $userId = Security::Instance()->mAuthentication->GetCurrentUser()->GetUserId();
      $mstId = $this->GetAtkMstByBrgId($data['barangId']);

      if (empty($mstId))
      {
         $dataMst = array(
            $data['barangId'],
				$data['satuan_barang'],
            $data['keterangan'],
            $userId
         );

         if ($this->AddAtkMst($dataMst)) $mstId = $this->GetAtkMstByBrgId($data['barangId']);
      }
      $dataDetAtk = array(
         $data['barcode'],
         $data['barcode'],
         $mstId
      );
      $detId = $this->GetAtkDetId($dataDetAtk);

      if (empty($detId))
      {
         $dataDet = array(
            $mstId,
            //$data['barcode'],
				empty($data['barcode'])?$data['barcode']:NULL,
            $data['label_barang'],
            $data['merk_barang'],
            $data['spesifikasi'],
            $userId
         );
         $this->AddAtkDetil($dataDet);
         $detId = $this->GetAtkDetId($dataDetAtk);
      }
      else
      {
         $dataDet = array(
            $mstId,
            $data['label_barang'],
            $data['merk_barang'],
            $data['spesifikasi'],
            $userId,
            $detId
         );
         $this->UpdateAtkDetil($dataDet);
      }
      $biayaId = $this->GetAtkBiayaByMstId($mstId);

      if (empty($biayaId))
      {
         $dataDet = array(
            $mstId,
            $data['invAtkBiayaNominal']
         );
         $this->AddAtkBiaya($dataDet);
      }
      else
      {
         $dataDet = array(
            $mstId,
            $data['invAtkBiayaNominal'],
            $biayaId
         );
         $this->UpdateAtkBiaya($dataDet);
      }
      $dataDet = array(
         $detId,
         $data['invAtkBiayaNominal'],
         $data['jumlah_barang']
      );
      $this->AddAtkBrg($dataDet);
      $atkBrgId = $this->GetMaxAtkBrg();
      $dataDet = array(
         $data['gudang'],
         $atkBrgId,
         $data['jumlah_barang'],
         $data['invAtkBiayaNominal'] / $atkBrgId,
         $data['jumlah_barang']
      );
      $this->AddAtkGudang($dataDet);

      $dataDet = array($data['gudang'],$atkBrgId,$data['jumlah_barang'],$data['keterangan']);

      $this->AddLogAtk($dataDet);

      #echo $this->GetLastError();
      $arrStatus = explode(" ", $this->GetLastError());

      if($this->EndTrans(true))
         return true;
      else
         return false;
      #$result = $this->Execute($this->mSqlQueries['add_atk'], array($data));

   }
	
	public function ab($nilai,$data,$i,$akhir,$habis){
		if ($i < $akhir){
			if ($habis==1){
				$sisa = $data[$i];
			}	else	{
				$sisa = $nilai-$data[$i];	
				$sisa2 = $sisa;
				if($sisa<0){
					$sisa = $sisa*(-1);
					$habis = 1;
				} elseif($sisa){
					$sisa=0;
				}
			}
			$x = $i+1;
			$this->ab($sisa2,$data,$x,$akhir,$habis);
			$_SESSION['hasil'][] = $sisa;
			//if(count($_SESSION['hasil']) > $akhir)unset($_SESSION['hasil']);
			
		}
	}
	
	public function UpdateAtk($data)
   {		
      $this->StartTrans();
      $userId = Security::Instance()->mAuthentication->GetCurrentUser()->GetUserId();
		$ruangId = $data['gudang']->Raw();
      $mstId = $this->GetAtkMstByBrgId($data['barang_id']);
		$detId = $this->GetAtkBrgByMstId($mstId);
		$keymax=array_keys($detId, max($detId));
		$detIdMax = $detId[$keymax[0]]['invBrgAtkId'];
		$jumlahMax = (int)$this->GetMaxAtkGdgByDetId($detIdMax, $ruangId); 
		$stokBaru = $data['sisa_barang']->Raw();
		$stokLama = $data['sisa_barang_lama']->Raw();

		switch ($stokBaru) {
			case ($stokBaru == $stokLama ): //-- sama
				return true;
				break;
			case ($stokBaru < $stokLama ): //-- kurang
				foreach($detId as $val){
					$jumlah[] = (int)$this->GetMaxAtkGdgByDetId($val['invBrgAtkId'], $ruangId);
				}
				
				$x = 0;
				$habis = 2;
				$akhir = sizeof($jumlah);
				$this->ab($stokBaru, $jumlah, $x, $akhir,$habis);
				$result = array_reverse($_SESSION['hasil']);
				
				foreach($detId as $val){
					$dataDet = array(($stokLama-$result[$x]), $ruangId, $val['invBrgAtkId']);
					$this->UpdateAtkGudangMax($dataDet);
					$x++;
				}
				break;
			case ($stokBaru > $stokLama ): //-- tambah
				$total = $jumlahMax + ($stokBaru - $stokLama);
				$dataDet = array($total, $ruangId, $detIdMax);
				$this->UpdateAtkGudangMax($dataDet);
				break;
		}

		//-- add log
		$dataDet = array($ruangId, $detIdMax, $stokBaru, $data['keterangan']->Raw());
		$this->AddLogAtkUpdate($dataDet);

      $this->EndTrans(true);
      $arrStatus = explode(" ", $this->GetLastError());
      if($arrStatus[1]=="ROLLBACK")
         return false;
      else
         return true;
	}
}
?>