<?php
/*
   @ClassName : Usulan
   @Copyright : PT Gamatechno Indonesia
   @Analyzed By : Dyan Galih<galih@gamatechno.com>
   @Author By :
   @Version : 0.1
   @StartDate : 2011-01-19
   @LastUpdate : 2011-01-19
   @Description : Class untuk usulan barang
*/

class Usulan extends Database
{
   protected $mSqlFile;

   function __construct ($connectionNumber=0)
   {
      $this->mSqlFile = 'module/pb_usulan/business/usulan.sql.php';
      parent::__construct($connectionNumber);
   }
	
	//-- get data usulan
	public function getDataStatus(){
		$result = $this->Open($this->mSqlQueries['get_data_status'], $arg);
		return $result;
	}
	
	public function GenerateNumber(){
		$result = $this->Open($this->mSqlQueries['get_generate_number'], $arg);
		return $result;
	}
	
	public function getDataUsulanBarangById($idUsulan){
		$arg = array
      (
			$idUsulan
      );
		$result = $this->Open($this->mSqlQueries['get_data_usulan_barang_by_id'], $arg);
		return $result;
	}
	
	public function getDataUsulanBarang($data, $start=NULL, $limit=NULL){
		extract($data);
		$arg = array
      (
			$status,
			$status,
			$start,
			$limit
      );
		$result = $this->Open($this->mSqlQueries['get_data_usulan_barang'], $arg);
		return $result;
	}
	
	public function getDataCountUsulanBarang($data){
		extract($data);
		$arg = array
      (
			$status, 
			$status
      );
		$result = $this->Open($this->mSqlQueries['get_data_count_usulan_barang'], $arg);
		if (is_array($result))
         return $result[0]['total'];
      else return 0;
	}
	
	//-- add n update data usulan
	public function Add($data)
   {
		$userId = trim(Security::Instance()->mAuthentication->GetCurrentUser()->GetUserId());
      $this->StartTrans();
		extract($data);
      if ($_FILES['file']['name'] !== '')
      {
         $tmp = dechex(mt_rand(65536, 1048576));
         $fileUsulan = "usulan-".$_FILES['file']['name']."-$tmp.".$_FILES['file']['ext'];
      } else {
			$fileUsulan = $fileOld;
		}
      
		// insert table master
		$tglUsulan = $tanggal_usulan_year.'-'.$tanggal_usulan_mon.'-'.$tanggal_usulan_day;
      $arg = array
      (
         $tglUsulan,
         $no_usul,
         $unit_id,
         $pic,
         $keterangan,
         $fileUsulan,
         $userId
      );
		
		$result = $this->Execute($this->mSqlQueries['add_usulan'], $arg);
      if (!$this->AffectedRows())
      {
         $this->EndTrans(false);
         return false;
      }
      // ---------
      
      // tabel master_detil
      $mstId = $this->LastInsertId();
      
		if (!empty($detail_barang)) foreach ($detail_barang as $value)
      {
         extract($value);
         
         $arg = array
         (
            $mstId,
            $usulanBrgDetBrgId,
            $usulanBrgDetJml,
            $brgHps,
				$usulanBrgNilaiUsulan,
				$userId
         );
			
			$this->Execute($this->mSqlQueries['add_detail_barang'], $arg);
      
         if (!$this->AffectedRows())
         {
            $this->EndTrans(false);
            return false;
         }
      }
      // ---------
      
      $arg = array
      (
         $mstId,
         '1',
         $userId
      );
		
		$result = $this->Execute($this->mSqlQueries['add_history_barang'], $arg);
      if (!$this->AffectedRows())
      {
         $this->EndTrans(false);
         return false;
      }
		
      // uploading file
      if
      (
         $_FILES['file']['name'] !== '' AND
         !move_uploaded_file($_FILES['file']['tmp_name'], GTFW_APP_DIR.'/images/usulan/'.$this->mFilePath.$fileUsulan)
      )
      {
         $this->EndTrans(false);
         return false;
      }
      // ---------
      
      $this->EndTrans(true);
      return true;
   }
	
	function Update ($data, $mstId)
   {
      $userId = trim(Security::Instance()->mAuthentication->GetCurrentUser()->GetUserId());
      $this->StartTrans();
		extract($data);
      if ($_FILES['file']['name'] !== '')
      {
         $tmp = dechex(mt_rand(65536, 1048576));
         $fileUsulan = "usulan-".$_FILES['file']['name']."-$tmp.".$_FILES['file']['ext'];
      } else {
			$fileUsulan = $fileOld;
		}
      
		$tglUsulan = $tanggal_usulan_year.'-'.$tanggal_usulan_mon.'-'.$tanggal_usulan_day;
		
      $arg = array
      (
         $tglUsulan,
         $unit_id,
         $pic,
         $keterangan,
         $fileUsulan,
         $userId,
			$mstId
      );
      
      if (!$this->Execute($this->mSqlQueries['update_usulan'], $arg))
      {
         $this->EndTrans(false);
         return false;
      }
      
      if (!$this->Execute($this->mSqlQueries['delete_detail_usulan_barang'], array($mstId)))
      {
         $this->EndTrans(false);
         return false;
      }
      
      if (!empty($detail_barang)) foreach ($detail_barang as $value)
      {
         extract($value);
         
         $arg = array
         (
            $mstId,
            $usulanBrgDetBrgId,
            $usulanBrgDetJml,
            $brgHps,
				$usulanBrgNilaiUsulan,
				$userId
         );
      
         if (!$this->Execute($this->mSqlQueries['add_detail_barang'], $arg))
         {
            $this->EndTrans(false);
            return false;
         }
      }
		
		$arg = array
      (
         $mstId,
         '1',
         $userId
      );
		
		$result = $this->Execute($this->mSqlQueries['add_history_barang'], $arg);
      if (!$this->AffectedRows())
      {
         $this->EndTrans(false);
         return false;
      }
      
      if
      (
         $_FILES['file']['name'] !== '' AND
         !move_uploaded_file($_FILES['file']['tmp_name'], GTFW_APP_DIR.'/images/usulan/'.$this->mFilePath.$fileUsulan)
      )
      {
         $this->EndTrans(false);
         return false;
      }
      
      $this->EndTrans(true);
      return true;
   }
	
	public function Delete($id){
		$result = $this->Execute($this->mSqlQueries['delete_usulan_barang'], array($id));
		return $result;
	}

   public function GetComboUsulanStatus(){
      $result = $this->Open($this->mSqlQueries['get_combo_usulan_status'], array());
      return $result;
   }
}
?>