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

class AdditionalAttachment 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_LIST:
					$this->getList($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 SalesQuotationAdditionalAttachmentClass($this->db) ;

			$attachments = json_decode($params['attachments'], true);
			if(!is_null($attachments) && count($attachments)>0) {			
				$this->db->beginTran() ;
				foreach($attachments as $attachment) {
					$id = $this->getParamInt($attachment,'id',0);
					$datas 	 = array() ;
					$datas[] = $this->db->fieldValue(SalesQuotationAdditionalAttachmentTable::C_CAPTION,$this->getParam($attachment,'attach_caption',"")) ;
					$datas[] = $this->db->fieldValue(SalesQuotationAdditionalAttachmentTable::C_ORDER,$this->getParamInt($attachment,'attach_order',0)) ;
					if($cls->isFound($id)) {
						$cls->updateRecord($id, $datas) ;
					} else {						
						$cls->addRecord($datas) ;
					}
				}

				$this->db->commitTran() ;
				$this->sendJsonResponse(Status::Ok,"Additional Attachment successfully added to the system.","",$this->type);
			} else {
				$this->sendJsonResponse(Status::Ok,"No Additional Attachment to be added to the system.","",$this->type);
			}
		} catch (Exception $e) {
			$this->db->rollbackTran() ;
			Log::write('[Add Additional Attachment] ' . $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 getList() {
		$cls = new SalesQuotationAdditionalAttachmentClass($this->db);
		$rows = $cls->getTable('', SalesQuotationAdditionalAttachmentTable::C_ORDER);

		$arrList 	= [];
		if(!is_null($rows) && count($rows)>0) {
			foreach ($rows as $i => $row) {
				$arrList[$i]["ID"] 		= $row[SalesQuotationAdditionalAttachmentTable::C_ID];
				$arrList[$i]["Order"] 	= $row[SalesQuotationAdditionalAttachmentTable::C_ORDER];
				$arrList[$i]["Caption"] = $row[SalesQuotationAdditionalAttachmentTable::C_CAPTION];
			}
		}
		$this->sendJsonResponse(Status::Ok,"Additional Attachment successfully added to the system.",$arrList,$this->type);
		unset($cls) ;
		unset($rows) ;		
	}
	private function getView() {
		// $this->getRecord();
		ob_start() ;
		include (PATH_VIEWS . "sales_quotation/AdditionalAttachmentView.php") ;
		echo Util::minifyHtml(ob_get_clean()) ;
	}
}
?>