/**
  * Assign Service
  *
  * Create a HostingServicePurchaseDBO and add it to the database
  */
 function assign_service()
 {
     // If this HostingService requires a unique IP, make sure the user selected one
     if ($this->post['service']->getUniqueIP() == "Required" && !isset($this->post['ipaddress'])) {
         throw new FieldMissingException("ipaddress");
     }
     // If this HostingService requires a domain, make sure the user selected one
     if ($this->post['service']->isDomainRequired() && !isset($this->post['domainname'])) {
         throw new FieldMissingException("domainname");
     }
     // Create new HostingServicePurchase DBO
     $serverID = isset($this->post['server']) ? $this->post['server']->getID() : null;
     $purchase_dbo = new HostingServicePurchaseDBO();
     $purchase_dbo->setAccountID($this->get['account']->getID());
     $purchase_dbo->setPurchasable($this->post['service']);
     $purchase_dbo->setTerm(isset($this->post['term']) ? $this->post['term']->getTermLength() : null);
     $purchase_dbo->setServerID($serverID);
     $purchase_dbo->setDate(DBConnection::format_datetime($this->post['date']));
     $purchase_dbo->setDomainName($this->post['domainname']);
     $purchase_dbo->setNote($this->post['note']);
     // Save purchase
     add_HostingServicePurchaseDBO($purchase_dbo);
     // If an IP address was selected, assign that IP address to this purchase
     if (isset($this->post['ipaddress'])) {
         if ($this->post['ipaddress']->getServerID() != $serverID) {
             // Roll-back
             delete_HostingServicePurchaseDBO($purchase_dbo);
             throw new SWUserException("[IP_MISMATCH]");
         }
         // Update IP Address record
         $this->post['ipaddress']->setPurchaseID($purchase_dbo->getID());
         try {
             update_IPAddressDBO($this->post['ipaddress']);
         } catch (DBException $e) {
             // Roll-back
             delete_HostingServicePurchaseDBO($purchase_dbo);
             throw new SWUserException("[DB_IP_UPDATE_FAILED]");
         }
     }
     // Success
     $this->setMessage(array("type" => "[HOSTING_ASSIGNED]"));
     $this->gotoPage("accounts_view_account", null, "action=services&account=" . $this->get['account']->getID());
 }
 /**
  * Execute Hosting Service Order
  *
  * Create a new Hosting Service Purchase for this order item
  *
  * @param AccountDBO $accountDBO Account object
  * @return boolean True for success
  */
 public function execute($accountDBO)
 {
     // Create a hosting service purchase record
     $purchaseDBO = new HostingServicePurchaseDBO();
     $purchaseDBO->setAccountID($accountDBO->getID());
     $purchaseDBO->setHostingServiceID($this->getServiceID());
     $purchaseDBO->setTerm($this->getTerm());
     $purchaseDBO->setDate(DBConnection::format_datetime(time()));
     $purchaseDBO->setDomainName($this->getDomainName());
     $purchaseDBO->setPrevInvoiceID(-1);
     $purchaseDBO->incrementNextBillingDate();
     add_HostingServicePurchaseDBO($purchaseDBO);
     // Fulfill the order and return
     $this->setStatus("Fulfilled");
     update_OrderHostingDBO($this);
     // Success
     return true;
 }
/**
 * Load multiple HostingServicePurchaseDBO's from database
 *
 * @param string $filter A WHERE clause
 * @param string $sortby Field name to sort results by
 * @param string $sortdir Direction to sort in (ASEC or DESC)
 * @param int $limit Limit the number of results
 * @param int $start Record number to start the results at
 * @return array Array of HostingServicePurchaseDBO's
 */
function &load_array_HostingServicePurchaseDBO($filter = null, $sortby = null, $sortdir = null, $limit = null, $start = null)
{
    $DB = DBConnection::getDBConnection();
    // Build query
    $sql = $DB->build_select_sql("hostingservicepurchase", "*", $filter, $sortby, $sortdir, $limit, $start);
    // Run query
    if (!($result = @mysql_query($sql, $DB->handle()))) {
        // Query error
        throw new DBException(mysql_error($DB->handle()));
    }
    if (mysql_num_rows($result) == 0) {
        // No services found
        throw new DBNoRowsFoundException();
    }
    // Build an array of DBOs from the result set
    $dbo_array = array();
    while ($data = mysql_fetch_array($result)) {
        // Create and initialize a new DBO with the data from the DB
        $dbo = new HostingServicePurchaseDBO();
        $dbo->load($data);
        // Add HostingServiceDBO to array
        $dbo_array[] = $dbo;
    }
    return $dbo_array;
}