<?
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
* Class		:	customerLicense
* Version :	1.0
* Date    :	18 October 2009
* Author  :	Darwish A. Khalil
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/

// Class Definition
class customerLicense
{
	var $LicenseID;				// Auto increment (Primary Key)
	var $MachineUID;			// ID stamp of the machine on which the software runs and is unique to that machine
	var $SoftwareUID;			// ID stamp of the software; each type of software has one UID i.e. all Gamma FinUSB will have the same UID
	var $SoftwareName;		// Name of the software
	var $VersionMajor="1";// Major version of  the software
	var $NotBefore;				// Date/time stamp (validity start)
	var $NotAfter;				// Expiry date of the license (validity end)
	var $InstallationDate;// Installation date of the license
	var $Status="Pending";// License Status with one of the following values (Activated, Expired, Pending)
	var $CustomerID;			// Owner of this license
	var $ProductID;				// License's related product id
	var $ProductName;			// License's related product name

	/*
	 * Class constructor
	 */
  function customerLicense($LicenseID = -1)
  {
  	if ((!empty($LicenseID)) && ($LicenseID != -1))
  	{
  		$this->LicenseID = $LicenseID;
			$this->getInfo();
		}
	}

	/*
	 * Retrieves license information
	 */
	function getInfo()
	{
		$query = "
							Select cl.*, p.Title ProductName From customer_license cl
							Inner Join product p On cl.ProductID = p.ProductID
					 	  Where LicenseID = $this->LicenseID
					 	 ";

		$result = mysql_query($query);
		$row = mysql_fetch_object($result);

    // Set class data members
    $this->MachineUID 			= $row->MachineUID;
    $this->SoftwareUID 			= $row->SoftwareUID;
    $this->SoftwareName 		= $row->SoftwareName;
    $this->VersionMajor 		= $row->VersionMajor;
    $this->NotBefore 				= date("Y-m-d H:i:s", $row->NotBefore);
    $this->NotAfter 				= date("Y-m-d H:i:s", $row->NotAfter);
    $this->InstallationDate = $row->InstallationDate;
    $this->Status 					= $row->Status;
    $this->CustomerID 			= $row->CustomerID;
    $this->ProductID 				= $row->ProductID;
    $this->ProductName 			= $row->ProductName;
  }

  /*
   * Retrieves the count of licenses available according to the given search criteria
   */
	function getTotal($Criteria = '')
	{
		$query  = "Select COUNT(*) From customer_license Where Deleted = 0 And CustomerID = $this->CustomerID ";

		// Date Range ------------------------------------------------------
		/*
		if($Criteria['FromDate'] != '') {
			$query .= "And (OrderDate >= '" . $Criteria['FromDate'] . "') ";
		}
		if($Criteria['ToDate'] != '') {
			$query .= "And (OrderDate <= '" . $Criteria['ToDate'] . "') ";
		}
		*/
		// License ID ------------------------------
		if($Criteria['LicenseID'] != '') {
			$query .= "And (LicenseID = '" . $Criteria['LicenseID'] . "') ";
		}
		// Keyword ------------------------------
		if($Criteria['Keyword'] != '')
		{
			$query .= "And ( ";
			$query .= "(MachineUID Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(SoftwareUID Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(SoftwareName Like '%" . $Criteria['Keyword'] . "%') ";
			$query .= ") ";
		}
		// Related Product ------------------------------------------------------------
		if($Criteria['ProductID'] != '-1') {
			$query .= "And (ProductID = " . $Criteria['ProductID'] . ") ";
		}
		// Status (Activated, Expired or Pending) ------------------------------
		if($Criteria['Status'] != '-1') {
			$query .= "And (Status = '" . $Criteria['Status'] . "') ";
		}

	  // echo $query;
   	$result = mysql_result(mysql_query($query), 0, 0);

    return $result;
	}

	/*
	 * Retrieve a list of licenses regarding a certain search criteria
	 */
	function getList($Criteria = '', $SortField = 'Status', $SortType = 'ASC', $Start = -1, $Num = -1)
	{
		$query  =
							"
								Select LicenseID, MachineUID, SoftwareUID, SoftwareName, VersionMajor,
								FROM_UNIXTIME(NotBefore) NotBefore, FROM_UNIXTIME(NotAfter) NotAfter,
								DATEDIFF(FROM_UNIXTIME(NotAfter), NOW()) RemainingDays, InstallationDate,
								Status, cl.CustomerID, cl.ProductID, p.Title ProductName
								From customer_license cl Inner Join product p On cl.ProductID = p.ProductID
								Where cl.Deleted = 0 And cl.CustomerID = $this->CustomerID ";

		// License ID ------------------------------
		if($Criteria['LicenseID'] != '') {
			$query .= "And (LicenseID = '" . $Criteria['LicenseID'] . "') ";
		}
		// Keyword ------------------------------
		if($Criteria['Keyword'] != '')
		{
			$query .= "And ( ";
			$query .= "(MachineUID Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(SoftwareUID Like '%" . $Criteria['Keyword'] . "%') OR ";
			$query .= "(SoftwareName Like '%" . $Criteria['Keyword'] . "%') ";
			$query .= ") ";
		}
		// Related Product ------------------------------------------------------------
		if($Criteria['ProductID'] != '-1') {
			$query .= "And (cl.ProductID = " . $Criteria['ProductID'] . ") ";
		}
		// Status (Activated, Expired or Pending) ------------------------------
		if($Criteria['Status'] != '-1') {
			$query .= "And (Status = '" . $Criteria['Status'] . "') ";
		}

		// Order By
		$query .= "Order By $SortField $SortType ";

		// Limit Results
		if (($Start!=-1) && ($Num!=-1)) {
     $query .= "Limit $Start,$Num";
    }

		// echo($query);
		$result = mysql_query($query);
		return $result;
	}

	/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
	*                    Manage Customer Licenses Functions
	*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
	/*
	 * Updates the license status
	 */
	function updateStatus($LicenseID, $Status)
	{
		$query  = "Update customer_license Set Status = '$Status' Where LicenseID = " . $LicenseID;
		mysql_query($query);
	}

	/*
	 * Sets the status of this license to Expired
	 */
	function setAsExpired($LicenseID)
	{
		$this->updateStatus($LicenseID, "Expired");
	}

	/*
	 * Sets the status of this license to Activated
	 */
	function setAsActivated($LicenseID)
	{
		$this->updateStatus($LicenseID, "Activated");
	}

	/*
	 * Manage the status of all pending & activated licenses where:
	 * - Activated licenses that are expired their statuses should be set as "Expired"
	 * - Pending licenses that are ready to be activated their statuses should be set as "Activated"
	 */
	function manageLicensesStatuses()
	{
		// Activate pending licenses that their ValidityStart date becomes active
		$query  =
						"
							Update customer_license
							Set Status = 'Activated'
							Where ValidityStart <= Now() And `Status` = 'Pending' And Deleted = 0;
						";
		mysql_query($query);

		// Expire active licenses that their ValidityEnd date becomes expired
		$query  =
						"
							Update customer_license
							Set Status = 'Expired'
							Where ValidityEnd <= Now() And `Status` = 'Activated' And Deleted = 0;
						";
		mysql_query($query);
	}

}
?>