<?php /*created:20170517 modified:20170809*/

if(empty($Form))
{
	require_once $Cms->getIncludePath('library/form.php');
	$Form = new FormC(0);
}

class PortalC
{
	private $putids = NULL; //list of portal user type ids
	private $types = NULL; //list of portal user type types
	private $typesD = NULL;  //list of all portal user types information
	
	//custom variables for this domain
	private $specialties = []; //dictiony of specialties; id => specialty
	
  public function __construct()
	{
	}
	
	public function createDirectoryHTML()
	{
		/**
		* Create a member's directory
		*/
		
		global $Cms, $Format, $mysql;
		
		$query = "
SELECT
  pu.pu_nameFirst,
	pu.pu_nameLast,
	pu.pu_phone,
	pu.pu_email, 
	put.put_type
FROM 
	`lt_portalUsers` pu
LEFT JOIN
	`lt_portalUserTypes` put
ON
	pu.put_id = put.put_id
WHERE
	put.put_directoryList = 1
ORDER BY
	pu.pu_nameLast ASC,
	pu.pu_nameFirst ASC";
				
		$r = $mysql->query($query) or die($mysql->error.' -- line 46: '.$query);
		
		ob_start();
		
		if($r)
		{
			echo '<ul class="membersList">',
				'<li class="membersListHeader">
					<ul>
						<li>First Name</li>'.
						'<li>Last Name</li>'.
						'<li>Member Type</li>'.
						'<li>Phone</li>'.
						'<li>Email</li>
					</ul>
				</li>';
				
			while($row = $r->fetch_object())
			{			
				echo
					'<li>
						<ul>
							<li>',$row->pu_nameFirst,'</li>',
							'<li>',$row->pu_nameLast,'</li>',
							'<li>',$row->put_type,'</li>',
							'<li>',($row->pu_phone ? '<a href="tel:+1'.FormatC::phone($row->pu_phone).'" title="'.FormatC::phone($row->pu_phone, '(a) p-l').'">'.FormatC::phone($row->pu_phone, '(a) p-l').'</a>' : ''),'</li>',
							'<li><a href="mailto:',$row->pu_email,'" title="',$row->pu_email,'">',$row->pu_email,'</a></li>
						</ul>
					</li>';
			}
							
			echo '</ul>';
		}
		
		return ob_get_clean();
	}
	
	public function isRequired($required = [])
	{
		/**
		* Checks if $required is true and returns required attribute
		*/
		
		if((is_array($required) && in_array(true, $required)) || $required)
			return ' required="required" ';
		
		return '';
	}
	
	public function getInvoiceHTML($erinid, $table)
	{
		/**
		* Gets the requsted invoice
		*/
		
		global $Cms, $Format, $mysql;
		
		if(empty($Forms))
		{
			require_once $Cms->getIncludePath('library/form.php');
			
			$Forms = new FormsC();
		}
		
		$query = "SELECT `erin_amount`, `erin_date`, `erin_description`, `erin_discount`, `erin_id`, `erin_time`, `erin_transactionId` FROM `".FormatC::databaseEscape($table).'-invoices` WHERE `erin_id` = '.FormatC::number($erinid);
				
		$r = $mysql->query($query) or die($mysql->error.' -- line 34: '.$query);
		
		ob_start();
		
		if($r && $row = $r->fetch_object())
		{
			echo
			  '<h1>',$row->erin_description,'</h1>',
				'<strong>Date:</strong> ',FormatC::date($row->erin_date, 'j F, Y'),'<br />',
				'<strong>Transaction Id:</strong> ',$row->erin_transactionId,'<br />',
				'<strong>Amount:</strong> $',$row->erin_amount,'<br />',
				($row->erin_discount ?
					'<strong>Discount:</strong> $'.FormatC::currency($row->erin_discount) : ''),
				'<h2>Purchases</h2>';					
		
			$query = "SELECT `erp_amount`, `erp_item`, `erp_quantity`, `erp_specifics` FROM `".FormatC::databaseEscape($table).'-purchases` WHERE `erin_id` = '.FormatC::number($erinid);
					
			$r = $mysql->query($query) or die($mysql->error.' -- line 51: '.$query);
			
			if($r)
			{
				echo '<ul class="purchasesList">',
					'<li class="purchasesListHeader">
						<ul>
							<li>Item</li>'.
							'<li>Amount</li>'.
							'<li>Quantity</li>'.
							'<li>Subtotal</li>
						</ul>
					</li>';
				
				while($row = $r->fetch_object())
					echo
						'<li>
							<ul>
								<li>',$row->erp_item,
								($row->erp_specifics ?
									' - '.$row->erp_specifics : ''),':</li>',
								'<li>$',$row->erp_amount,'</li>',
								'<li>',$row->erp_quantity,'</li>',
								'<li>$',FormatC::currency($row->erp_amount * $row->erp_quantity),'</li>
							</ul>
						</li>';	
						
				echo '</ul>';
			}
		}
		
		return ob_get_clean();
	}
		
	public function getInvoicesHTML($puid)
	{
		/**
		* Gets all the invoices for a requested portal user
		*/
		
		global $Cms, $Format, $Forms, $mysql;
		
		if(empty($Forms))
		{
			require_once $Cms->getIncludePath('library/form.php');
			
			$Forms = new FormsC();
		}
		
		$invoices = []; //holds a dictionary list of invoice information
		$tables = array('prefix', 'name'); //list of form tables
		
		$tables = $Forms->getFormTableInformation(NULL, array('name' => 'f_tableName', 'prefix' =>'f_tablePrefix'));
		
		for($i=0, $len=count($tables); $i<$len; ++$i)
		{			
			if($invoice = $Forms->getInvoicesForPortalUser($puid, $tables[$i]['name'], $tables[$i]['prefix']))
				$invoices = array_merge($invoices, $invoice);
				
		}
			
		//this next part should be fetched from the above code but it has to be specially added for now 
		$r = $mysql->query("SELECT `erin_amount`, `erin_date`, `erin_description`, `erin_discount`, `erin_id`, `erin_time`, `erin_transactionId` FROM `lt_portalUser-invoices` WHERE `pu_id` = $puid") or die($mysql->error.' -- line 47: ');
		
		if($r)
			while($row = $r->fetch_object())
				$invoices[] = array
				(
					'amount' => $row->erin_amount, 
					'date' => $row->erin_date, 
					'description' => $row->erin_description,
					'discountAmount' => (float)$row->erin_discount,
					'discountDescription' => NULL, /*$row->erin_discountDescription,*/
					'erinid' => $row->erin_id, 
					'table' =>  'lt_portalUser',
					'time' => $row->erin_description,
					'transactionId' => $row->erin_transactionId
				);
			
		if($invoices)
		{	
			//order invoices by date				
			$Cms->subArraySortKeys = array('date', 'time');
			$Cms->subArraySortOrder = 'desc';
			usort($invoices, [$Cms, 'subArraySortAlt']);
			
			ob_start();
			
			echo '<ul class="invoiceList">'.
					'<li class="invoiceListHeader">
						<ul>
							<li>Purchased</li>'.
							'<li>Total</li>'.
							'<li>Description</li>
						</ul>
					</li>';
			
			for($i=0, $len=count($invoices); $i<$len; ++$i)
				echo 
					'<li>
						<a href="'.SITE_ROOT.'user/my-invoices/'.$invoices[$i]['table'].'-'.$invoices[$i]['erinid'].'.html">
							<ul>
								<li>'.FormatC::date($invoices[$i]['date'], 'm/d/Y').'</li>'.
								'<li>$'.$invoices[$i]['amount'].'</li>'.
								'<li>'.$invoices[$i]['description'].'</li>
							</ul>
						</a>
					</li>';
			
			echo '</ul>';
			
			return ob_get_clean();
		}
		else
			return 'there are none';
	}
	
	public function createUserTypeRadiolistHTML($checked = [], $exclude = [], $columns = 4)
	{
		/*
		* Creates a radiolist of types for a form
		* - $checked is a list of types to select
		* - $exclude is a list of types to exclude from the list
		* - $columns is the number of columns to divide the list into
		*/
		
		global $Form, $mysql;
		
		$name = 'types'; //maybe passed as an argument later
		
		return $Form->generateRadiolistHTML($name, $this->getUserTypeTypes(), $this->getUserTypeIds(), $columns, $checked, $exclude);
	}
	
	public function getUserTypeCost($id)
	{
		$types = $this->getUserTypeAll();
		
		for($i=0, $len=count($types); $i<$len; ++$i)
			if($types[$i]['putid'] == $id)
				return $types[$i]['cost'];
				
		return false;
	}
	
	public function getUserTypeIds()
	{
		/**
		* Returns a list of portal user type ids
		*/
		
		if($this->putids === NULL)
			$this->setUserTypes();
			
		return $this->putids;
	}
	
	public function getUserTypeTypes()
	{
		/**
		* Returns a list of portal user type types
		*/
		
		if($this->types === NULL)
			$this->setUserTypes();
			
		return $this->types;
	}
	
	public function getUserTypeAll()
	{
		/**
		* Returns a dictionary of all portal user type information
		*/
		
		if($this->typesD === NULL)
			$this->setUserTypes();
			
		return $this->typesD;
	}
	
	public function setUserTypes()
	{
		/**
		* Sets portal user type variables
		* - Initialize all as an empty array to signify set
		* - Get information from database
		* - Add information to appropriate variables
		*/
		
		global $mysql;
		
		$this->putids = [];
		$this->types = [];
		$this->typesD = [];
		
		$r = $mysql->query("SELECT `put_cost`, `put_id`, `put_type` FROM `lt_portalUserTypes`");
								
		if($r)
			while($row = $r->fetch_object())
			{
				$this->putids[] = (int)$row->put_id;
				$this->types[] = $row->put_type;
				$this->typesD[] = array
				(
				  'cost' => $row->put_cost,
				  'putid' => (int)$row->put_id,
					'type' => $row->put_type
				);
			}
	}
	
	//custom methods for the domain	
	public function getSpecialties()
	{
		if(!$this->specialties)
		{
			global $mysql;
			
			$query = "SELECT `s_id`, `s_specialty` FROM `lt_specialties` ORDER BY `s_specialty` ASC";
			
			$r = $mysql->query($query) or die($mysql->error.' -- line 352: '.$query);
			
			if($r)
				while($row = $r->fetch_object())
					$this->specialties[$row->s_id] = $row->s_specialty;
		}
		
		return $this->specialties;
	}
}
?>