<?php
require_once (PATH_CONTROLLERS . "Common.php") ;
require_once (PATH_MODELS . "activity/CurrencyClass.php") ;
require_once (PATH_MODELS . "activity/CurrencyRateClass.php") ;

class Currency extends ActivityBase {
	private $type = "" ;
	function __construct() {
		$this->db = $_SESSION[SE_DB] ;
	}
	function __destruct() {
		unset($this->db) ;
	}
	public function processRequest($params) {
		$this->type = REQ_VIEW ;
		
		try {
			$this->db->open() ;
			if (isset($params) && count($params) > 0) {
				if (isset($params['type']))
					$this->type = $params['type'] ;
			}
			switch ($this->type) {
				case REQ_ADD:
					$this->addRecord($params) ;
					break ;
				case REQ_UPDATE:
					$this->updateRecord($params) ;
					break ;
				case REQ_DELETE:
					$this->deleteRecord($params) ;
					break ;
				case REQ_GET:
					$this->getRecord($params) ;
					break ;
				case REQ_LIST:
					$this->getList($params) ;
					break ;
				case REQ_REPORT:
					$this->getReport($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 addRecord($params) {
		$cls = new CurrencyClass($this->db) ;
		$datas = array() ;
		$orgid = $_SESSION[SE_ORGID] ;
		$modifyby = $_SESSION[SE_USERID] ;
		$modifydate = date_create('now')->format('Y-m-d H:i:s') ;
		$ws = $_SESSION[SE_REMOTE_IP] ;
		
		$datas[] = $this->db->fieldValue(CurrencyTable::C_DESC,$this->getParam($params,'desc',"")) ;
		$datas[] = $this->db->fieldValue(CurrencyTable::C_REF,$this->getParam($params,'ref',"")) ;
		$datas[] = $this->db->fieldValue(CurrencyTable::C_RATE,$this->getParam($params,'def_rate',"")) ;
		$datas[] = $this->db->fieldValue(CurrencyTable::C_WS_ID,$ws) ;
		$datas[] = $this->db->fieldValue(CurrencyTable::C_MODIFY_BY,$modifyby) ;
		$datas[] = $this->db->fieldValue(CurrencyTable::C_CREATE_BY,$modifyby) ;
		$datas[] = $this->db->fieldValue(CurrencyTable::C_MODIFY_DATE,$modifydate) ;
		$datas[] = $this->db->fieldValue(CurrencyTable::C_CREATE_DATE,$modifydate) ;
		$datas[] = $this->db->fieldValue(CurrencyTable::C_ORG_ID,$orgid) ;
		
        $rates = $this->getParam($params,'rate',"");
		try {
			$id = $cls->addRecord($datas) ;
			if ($id > 0) {
                $this->addRate($id, $rates);
				$this->sendJsonResponse(Status::Ok,"Currency successfully added to the system.",$id,$this->type);
			} else {
				$this->sendJsonResponse(Status::Error,"Sorry, there is a error in adding new country to the system.",$id, $this->type) ;
			}
		} catch (Exception $e) {
			Log::write('[Currency]' . $e->getMessage());
			$this->sendJsonResponse(Status::Error,"sorry, there is a error in database operation.","",$this->type) ;
		}
		unset($cls) ;
	}
	private function updateRecord($params) {
		if (isset($params['id'])) {
			$id = $params['id'] ;
			$cls = new CurrencyClass($this->db) ;
            $clsRates = new CurrencyRateClass($this->db);
			try {
				$datas = array() ;
				$modifyby = $_SESSION[SE_USERID] ;
				$modifydate = date_create('now')->format('Y-m-d H:i:s') ;
				$ws = $_SESSION[SE_REMOTE_IP] ;

				$datas[] = $this->db->fieldValue(CurrencyTable::C_DESC,$this->getParam($params,'desc',"")) ;
				$datas[] = $this->db->fieldValue(CurrencyTable::C_RATE,$this->getParam($params,'def_rate',"")) ;
				$datas[] = $this->db->fieldValue(CurrencyTable::C_REF,$this->getParam($params,'ref',"")) ;
				$datas[] = $this->db->fieldValue(CurrencyTable::C_WS_ID,$ws) ;
				$datas[] = $this->db->fieldValue(CurrencyTable::C_MODIFY_BY,$modifyby) ;
				$datas[] = $this->db->fieldValue(CurrencyTable::C_MODIFY_DATE,$modifydate) ;
                
				$cls->updateRecord($id,$datas) ;
                
                $rates = $this->getParam($params,'rate',"");
                $clsRates->deleteRatesById($id);
                $this->addRate($id, $rates);
                
				$this->sendJsonResponse(Status::Ok,"Currency detail successfully updated to the system.",$id,$this->type) ;
			} catch (Exception $e) {
				Log::write('[Currency]' . $e->getMessage());
				$this->sendJsonResponse(Status::Error,"Sorry, there is a error in updating country detail to the system.","",$this->type) ;
			}
			unset($cls) ;
			unset($clsRates) ;
		}else {
			$this->sendJsonResponse(Status::Error,"You must supply the country id you wish to update. Please try again.","",$this->type);
		}
	}
	private function deleteRecord($params) {
		if (isset($params['id'])) {
			$id = $params['id'] ;
			$cls = new CurrencyClass($this->db) ;
            $clsRates = new CurrencyRateClass($this->db);
			try {
				$cls->deleteRecord($id) ; 
                $clsRates->deleteRatesById($id);
				$this->sendJsonResponse(Status::Ok,"Currency successfully deleted from the system.","",$this->type);
			} catch (Exception $e) {
				Log::write('[Currency]' . $e->getMessage());
				$this->sendJsonResponse(Status::Error,"Sorry, there is a problem in deleting country record from the system.","",$this->type) ;
			}
			unset($cls) ;
            unset($clsRates);
		} else {
			$this->sendJsonResponse(Status::Error,"You must supply the country id you wish to delete. Please try again.","",$this->type);
		}
	}
	private function getList($params=null) {
		// $cls = new CurrencyClass($this->db) ;
		// $filter = $this->db->fieldParam(CurrencyTable::C_ORG_ID) ;
		// $params = array() ;
		// $params[] = $this->db->valueParam(CurrencyTable::C_ORG_ID,$_SESSION[SE_ORGID]) ;
		// $rows = $cls->getTable($filter,CurrencyTable::C_DESC,$params) ;
		// $list = "" ;
		// foreach ($rows as $row) {
		// 	$id = $row[CurrencyTable::C_ID] ;
		// 	$list .= "<tr>" ;
		// 	$list .= "<td>" . $id . "</td>" ;
		// 	$list .= "<td>" . $row[CurrencyTable::C_DESC] . "</td>" ;
		// 	$list .= "<td>" . number_format($row[CurrencyTable::C_RATE],6) . "</td>" ;
		// 	$list .= "<td>" . $row[CurrencyTable::C_REF] . "</td>" ;
		// 	$list .= "<td style='text-align:center'><a href='javascript:' onclick='editCurrency(" . $id . ",this)'><img src='image/edit_16.png' title='Edit'></img></a></td>" ;
		// 	$list .= "<td style='text-align:center'><a href='javascript:' onclick='deleteCurrency(" . $id . ",this)'><img src='image/delete_16.png' title='Delete'></img></a></td>" ;
		// 	$list .= "</tr>" ;
		// }
		// unset($rows) ;
		// unset($cls) ;
		// return $list ;

		//=============================================================

		$cls = new CurrencyClass($this->db) ;

		$filter = $this->db->fieldParam(CurrencyTable::C_ORG_ID) ;
		$params = array() ;
		$params[] = $this->db->valueParam(CurrencyTable::C_ORG_ID,$_SESSION[SE_ORGID]) ;
		$rows = $cls->getTable($filter,CurrencyTable::C_DESC,$params) ;
		$arrList = [];
		foreach ($rows as $i => $row) {
			$arrList[$i]["id"] = $row[CurrencyTable::C_ID];
			$arrList[$i]["desc"] = $row[CurrencyTable::C_DESC];	
			$arrList[$i]["rate"] = number_format($row[CurrencyTable::C_RATE],6);	
			$arrList[$i]["ref"] = $row[CurrencyTable::C_REF];	
			$arrList[$i]["actions"] = '<a onclick="javascript:editCurrency('. $row[CurrencyTable::C_ID] .',this);" class="btn btn-sm btn-clean btn-icon btn-icon-md" title="Edit details">
									  	<i class="fa fa-edit"></i>
									  </a>
									  <a onclick="javascript:deleteCurrency('. $row[CurrencyTable::C_ID] .',this);" class="btn btn-sm btn-clean btn-icon btn-icon-md" title="Delete">
									  	<i class="fa fa-trash"></i>
									  </a>';	
		}

		$this->sendJsonResponse(Status::Ok,"",$arrList,$this->type);
		
		unset($rows) ;
		unset($cls) ;
	}
	private function getRecord($params=null) {
		if (isset($params['id'])) {
			$id = $params['id'] ;
			$cls = new CurrencyClass($this->db) ;
			$row = $cls->getRecord($id) ;
			if (is_null($row)) {
				$this->sendJsonResponse(Status::Error,"Invalid country id. Please try again.",$id,$this->type);
			} else {
				$datas = array() ;
				$datas['id'] = $id ;
				$datas['desc'] = $row[CurrencyTable::C_DESC];
				$datas['def_rate'] = $row[CurrencyTable::C_RATE] ;
				$datas['ref'] = $row[CurrencyTable::C_REF] ;
                
                $datas['rate'] = $this->getRate($id);
				$this->sendJsonResponse(Status::Ok,"",$datas,$this->type) ;
			}
			unset($cls) ;
		} else {
			$this->sendJsonResponse(Status::Error,"Missing country id. Please try again.","",$this->type);
		}
	}
    private function getRate($id){
        $clsCur = new CurrencyClass($this->db);
        $clsRate = new CurrencyRateClass($this->db);
        
        $rows = $clsRate->getRatesById($id);
        
        $result = "";
        
        if (!is_null($rows) && count($rows) > 0){
            foreach($rows as $row){
                $curRow = $clsCur->getRecord($row[CurrencyRateTable::C_ID_FROM]);
                
                if($result != "")
                    $result .= "|";
                
                $result .= $row[CurrencyRateTable::C_ID_FROM] . 
                        "~" . $curRow[CurrencyTable::C_REF] . 
                        "~" . number_format($row[CurrencyRateTable::C_RATE], 6) ;
            }
        }
        
        unset($clsRate);
        unset($clsCur);
        return $result;
    }
    private function addRate($id, $rates){
        
		$cls = new CurrencyRateClass($this->db) ;
		if ($rates != "") {
			$lines = explode("|",$rates) ;
			for ($i= 0;$i < count($lines) ;$i++) {
				$line = explode("~",$lines[$i]) ;
				if (count($line) == 2) {	
                    $cls->addRate($id, $line[0], $line[1]);
				}
			}
		}
        unset($cls);
    }
    private function getCurrencyList(){
		$vls = $this->getValueList(CurrencyTable::C_TABLE, CurrencyTable::C_ID, CurrencyTable::C_REF,null) ;
		return Util::createOptionValue($vls) ;
    }
	private function getView() {
		ob_start() ;
		include (PATH_VIEWS . "activity/CurrencyView.php") ;
		echo Util::minifyHtml(ob_get_clean()) ;
	}
	private function getReport($params=null) {
		require_once(PATH_LIB . 'ListPdf.php');
		
		$cls = new CurrencyClass($this->db) ;
		$filter = $this->db->fieldParam(CurrencyTable::C_ORG_ID) ;
		$params = array() ;
		$params[] = $this->db->valueParam(CurrencyTable::C_ORG_ID,$_SESSION[SE_ORGID]) ;
		$rows = $cls->getTable($filter,CurrencyTable::C_DESC,$params) ;
		$i = 'items';
		$nr = 'newrow';
		$datas = array() ;
		foreach ($rows as $row) {
			$items = array() ;
			$items[$i][] = $this->createPdfItem($row[CurrencyTable::C_ID],30) ;
			$items[$i][] = $this->createPdfItem($row[CurrencyTable::C_DESC],200) ;
			$items[$i][] = $this->createPdfItem(number_format($row[CurrencyTable::C_RATE],6),100) ;
			$items[$i][] = $this->createPdfItem($row[CurrencyTable::C_REF],100) ;
			$items[$nr] = "1" ;
			$datas[] = $items ;
		}
		$cols = array() ;
		$cols[] = $this->createPdfItem("ID",30,0,"C","B");
		$cols[] = $this->createPdfItem("Description",200,0,"C","B") ;
		$cols[] = $this->createPdfItem("Rate",100,0,"C","B") ;
		$cols[] = $this->createPdfItem("Ref",100,0,"C","B") ;
		$pdf = new ListPdf('P');
		$pdf->setCompanyName($_SESSION[SE_ORGNAME]) ;
		$pdf->setReportTitle("Currency Listing") ;
		$pdf->setColumnsHeader($cols) ;
		$pdf->render($datas) ;
		$pdf->Output('country.pdf', 'I');
		unset($rows) ;
		unset($cls) ;
		unset($datas) ;
		unset($params) ;
		unset($items) ;
		unset($cols) ;
	}
}
?>