<?php
require_once(__DIR__."/../includes/sysinit.php");
require_once(SABLE_CLASS_PATH."/sable_validation_functions.php");
class Report 
{
	// Properties of the class:
	var $edtStartDate,	$edtEndDate, $selClientId, $reportId, $edtReportName, $selPmId, $edtHardwareModel;
	private $sableDB, $heading, $data, $sql, $idx;
	
	function __construct()
	{
		$this->sableDB = new SableDB;
	}
	
	function __destruct(){}
	
	function runReport()
	{
		switch(intval($this->reportId))
		{
			case 1:  {$this->runClientReport();	break;}
			default: {SableSystem::ajaxResponse(false, "", "No report selected.", 5, "edtStartDate", false, "edtStartDate");}
		}
	}
	
	private function sendAjaxResponse($url, $paramArray)
	{
		$hash = SableDBFunctions::storeAjaxResponse($paramArray);
		SableSystem::ajaxPageRedirect($url.'+'.$hash, '');
	}
	
	private function getFormattedSQL($condition)
	{
		$prfx = $this->idx ? " and " : " where ";
		$this->sql .= $prfx.$condition;
		$this->idx++;
	}
	
	private function runClientReport()
	{
		$this->sql="select txn_amount, client.client_id, 
					concat(client_name, ' ', client_surname) as client_full_name,
					client_cell, client_phone, client_email, txn_date 
					from client 
					left join finance on finance.client_id = client.client_id 
					";
		
		$this->idx = 0;
				
		if (!empty($this->edtStartDate))	$this->getFormattedSQL("(DATE(txn_date) >= '".$this->edtStartDate."')");
		if (!empty($this->edtEndDate))		$this->getFormattedSQL("(DATE(txn_date) <= '".$this->edtEndDate."')");
		if ($this->selClientId)				$this->getFormattedSQL("(client.client_id = ".$this->selClientId.")");
		if ($this->selPmId)					$this->getFormattedSQL("(pm_id = ".$this->selPmId.")");

		//SableDebug::debug($sql);
		//$rows = $this->sableDB->getRows($sql);
		$out = ['sql' => $this->sql];
		$this->sendAjaxResponse('/report/finance/', $out);	
	}	
}