<?php
require_once (PATH_CONTROLLERS . "Common.php") ;
require_once (PATH_LIB . 'LicenseFile.php') ;
require_once (PATH_MODELS . "activity/salesquotation/SalesQuotationAdditionalInformationClass.php") ;

class AdditionalInformation extends ControllerBase {
	private $type = "" ;
	private $datas = array() ;
	function __construct() {
		$this->db = $_SESSION[SE_DB] ;
	}
	function __destruct() {
		unset($this->db) ;
	}
	public function processRequest($params) {
		$this->type = "v" ;
		
		try {
			$this->db->open() ;
			if (isset($params) && count($params) > 0) {
				if (isset($params['type']))
					$this->type = $params['type'] ;
			}
			switch ($this->type) {
				case REQ_UPDATE:
					$this->updateRecord($params) ;
					break ;
				case REQ_GET:
					$this->getRecord($params) ;
					break ;
				case REQ_VIEW:
					$this->getView() ;
					break ;
				default:
					$this->sendJsonResponse(Status::Error,"invalid request.","",$this->type) ;
					break ;
			}
			$this->db->close() ;
			return true ;
		} catch (Exception $e) {
			$this->db->close() ;
			die ($e->getMessage()) ;
		}
	}
	private function updateRecord($params) {
		try {
			$cls 		= new SalesQuotationAdditionalInformationClass($this->db) ;

			$this->db->beginTran() ;
			$cls->deleteAllRecord();
			
			if($this->getParam($params,'user_defined_1',"") != "") {
				$datas 	 = array() ;
				$datas[] = $this->db->fieldValue(SalesQuotationAdditionalInformationTable::C_USER_DEFINED_1,$this->getParam($params,'user_defined_1',"")) ;
				$datas[] = $this->db->fieldValue(SalesQuotationAdditionalInformationTable::C_USER_DEFINED_2,$this->getParam($params,'user_defined_2',"")) ;
				$datas[] = $this->db->fieldValue(SalesQuotationAdditionalInformationTable::C_USER_DEFINED_3,$this->getParam($params,'user_defined_3',"")) ;
				$datas[] = $this->db->fieldValue(SalesQuotationAdditionalInformationTable::C_USER_DEFINED_4,$this->getParam($params,'user_defined_4',"")) ;
				$cls->addRecord($datas) ;
			}
			$this->db->commitTran() ;
			$this->sendJsonResponse(Status::Ok,"Additional Information successfully added to the system.","",$this->type);
		} catch (Exception $e) {
			$this->db->rollbackTran() ;
			Log::write('[Add Additional Information] ' . $e->getMessage());
			$this->sendJsonResponse(Status::Error,"Sorry, we are unable to process your request as there is a error in database operation.","",$this->type) ;
		}
		unset($cls) ;
	}
	private function getRecord() {
		try {
			$cls 		= new SalesQuotationAdditionalInformationClass($this->db) ;
			$rows = $cls->getTable();

			$datas = array();
			if(!is_null($rows) && count($rows)>0) {
				foreach($rows as $row) {
					$datas['user_defined_1'] = $row[SalesQuotationAdditionalInformationTable::C_USER_DEFINED_1];
					$datas['user_defined_2'] = $row[SalesQuotationAdditionalInformationTable::C_USER_DEFINED_2];
					$datas['user_defined_3'] = $row[SalesQuotationAdditionalInformationTable::C_USER_DEFINED_3];
					$datas['user_defined_4'] = $row[SalesQuotationAdditionalInformationTable::C_USER_DEFINED_4];
				}
			}
			$this->sendJsonResponse(Status::Ok,"Additional Information successfully retrieved.",$datas,$this->type);
			unset($cls) ;
			unset($row) ;
		} catch (Exception $e) {
			Log::write('[Get Additional Information] ' . $e->getMessage());
			$this->sendJsonResponse(Status::Error,"Sorry, we are unable to process your request as there is a error in database operation.","",$this->type) ;
		}
	}
	private function getView() {
		// $this->getRecord();
		ob_start() ;
		include (PATH_VIEWS . "sales_quotation/AdditionalInformationView.php") ;
		echo Util::minifyHtml(ob_get_clean()) ;
	}
}
?>