/** * Overrides CommerceLicenseBillingUsageBase::addUsage(). */ public function addUsage($revisionId, $quantity, $start = NULL, $end = NULL) { if (is_null($start)) { // Default $start to current time. $start = commerce_license_get_time(); } if (is_null($end)) { // Default $end to current time. $end = commerce_license_get_time(); } // Open the new usage. parent::addUsage($revisionId, $quantity, $start, $end); }
/** * Implements CommerceLicenseBillingUsageGroupInterface::onRevisionChange(). */ public function onRevisionChange() { $previous_status = $this->license->original->status; $new_status = $this->license->status; $current_time = commerce_license_get_time(); // The license was activated for the first time. Register initial usage. if ($previous_status < COMMERCE_LICENSE_ACTIVE && $new_status == COMMERCE_LICENSE_ACTIVE) { $initial_usage = $this->initialUsage(); if (!is_null($initial_usage)) { $this->addUsage($this->license->revision_id, $initial_usage, $current_time); } } elseif ($previous_status == COMMERCE_LICENSE_ACTIVE) { // Get the open usage for the previous revision. We can't use // $this->currentUsage() because it looks at the current revision instead. $data = array(':group_name' => $this->groupName, ':revision_id' => $this->license->original->revision_id); $query = db_query('SELECT quantity FROM {cl_billing_usage} WHERE usage_group = :group_name AND revision_id = :revision_id AND end = 0 ORDER BY usage_id DESC LIMIT 1', $data); $previous_usage = $query->fetchField(); // Close the open usage for the previous revision (plan). db_update('cl_billing_usage')->fields(array('end' => $current_time - 1))->condition('revision_id', $this->license->original->revision_id)->condition('usage_group', $this->groupName)->condition('end', '0')->execute(); // Reset the usage history static cache. drupal_static_reset('commerce_license_billing_usage_history_list'); // If the license is still active, reopen the usage. if ($new_status == COMMERCE_LICENSE_ACTIVE && is_numeric($previous_usage)) { $this->addUsage($this->license->revision_id, $previous_usage, $current_time); } } elseif ($previous_status == COMMERCE_LICENSE_SUSPENDED && $new_status == COMMERCE_LICENSE_ACTIVE) { // Get the last closed usage quantity for this group. $data = array(':group_name' => $this->groupName, ':license_id' => $this->license->license_id); $query = db_query('SELECT quantity FROM {cl_billing_usage} WHERE usage_group = :group_name AND license_id = :license_id ORDER BY usage_id DESC LIMIT 1', $data); $previous_quantity = $query->fetchField(); $this->addUsage($this->license->revision_id, $previous_quantity, $current_time); } }