You are here

class SensorCommerceTurnover in Monitoring 7

Monitors commerce order turnover stats.

Based on SensorDatabaseAggregator using commerce_order table.

Hierarchy

Expanded class hierarchy of SensorCommerceTurnover

File

lib/Drupal/monitoring/Sensor/Sensors/SensorCommerceTurnover.php, line 15
Contains \Drupal\monitoring\Sensor\Sensors\SensorCommerceTurnover.

Namespace

Drupal\monitoring\Sensor\Sensors
View source
class SensorCommerceTurnover extends SensorDatabaseAggregator {

  /**
   * {@inheritdoc}
   */
  protected function buildQuery() {
    $query = parent::buildQuery();

    // Get the field name for the amount field.
    $field_amount = $this
      ->getFieldName($query, array(
      'field' => 'commerce_order_total.amount',
    ));

    // Build the field name based on that for the currency in the same table.
    list($alias) = explode('.', $field_amount);
    $field_currency = _field_sql_storage_columnname('commerce_order_total', 'currency_code');

    // Get the amount sum.
    $query
      ->addExpression('sum(' . $field_amount . ')', 'sum_amount');
    $query
      ->addField($alias, $field_currency, 'currency_code');
    $query
      ->groupBy($alias . '.' . $field_currency);
    if ($currency_code = $this->info
      ->getSetting('currency_code')) {
      $query
        ->condition($alias . '.' . $field_currency, $currency_code);
    }
    return $query;
  }

  /**
   * {@inheritdoc}
   */
  public function runSensor(SensorResultInterface $result) {
    $sensor_value = 0;
    $currency_code = $this->info
      ->getSetting('currency_code');
    foreach ($this
      ->getQueryResult()
      ->fetchAll() as $row) {

      // In case the sensor defines currency_code in settings and it is equal
      // to the currency_code from the db result the sensor value is sum of
      // amounts for that currency_code.
      if ($currency_code == $row->currency_code) {
        $sensor_value = $row->sum_amount;
        break;
      }
      elseif ($row->currency_code != commerce_default_currency()) {
        $sensor_value += commerce_currency_convert($row->sum_amount, $row->currency_code, commerce_default_currency());
      }
      else {
        $sensor_value += $row->sum_amount;
      }
    }

    // Convert the amount into a decimal for either the configured currency
    // or the default if none is configured.
    $result
      ->setValue(commerce_currency_amount_to_decimal($sensor_value, $currency_code ? $currency_code : commerce_default_currency()));
  }

  /**
   * Adds the order statuses select element to the sensor settings form.
   */
  public function settingsForm($form, &$form_state) {
    $form = parent::settingsForm($form, $form_state);
    $conditions = $this->info
      ->getSetting('conditions');
    $options = array();
    foreach (commerce_order_statuses() as $name => $info) {
      $options[$name] = $info['title'];
    }
    $form['conditions']['status']['value'] = array(
      '#type' => 'select',
      '#title' => t('"Paid" order statuses'),
      '#description' => t('Select order statuses in which the order is considered to be paid.'),
      '#options' => $options,
      '#multiple' => TRUE,
      '#default_value' => $conditions['status']['value'],
    );
    return $form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Sensor::$info protected property Current sensor info object.
Sensor::getSensorName public function Gets sensor name (not the label). Overrides SensorInterface::getSensorName
Sensor::isEnabled public function Determines if sensor is enabled. Overrides SensorInterface::isEnabled
Sensor::__construct function Instantiates a sensor object. 1
SensorCommerceTurnover::buildQuery protected function Builds the database query. Overrides SensorDatabaseAggregator::buildQuery
SensorCommerceTurnover::runSensor public function Runs the sensor, updating $sensor_result. Overrides SensorDatabaseAggregator::runSensor
SensorCommerceTurnover::settingsForm public function Adds the order statuses select element to the sensor settings form. Overrides SensorDatabaseAggregator::settingsForm
SensorDatabaseAggregator::$fetchedObject protected property The fetched object from the query result.
SensorDatabaseAggregator::$queryArguments protected property The arguments of the executed query.
SensorDatabaseAggregator::$queryResult protected property The result of the db query execution.
SensorDatabaseAggregator::buildQueryAggregate protected function Adds aggregate expressions to the query.
SensorDatabaseAggregator::fetchObject public function Get fetched object from the executed query.
SensorDatabaseAggregator::getEntityTypeFromTable protected function Returns the entity type for a given base table.
SensorDatabaseAggregator::getFieldName protected function Returns the field name to use for a condition and ensures necessary joins.
SensorDatabaseAggregator::getQueryArguments protected function Returns query arguments of the last executed query.
SensorDatabaseAggregator::getQueryResult protected function Executes the query and returns the result.
SensorDatabaseAggregator::getTimeIntervalOptions protected function Returns time interval options.
SensorDatabaseAggregator::joinFieldTable protected function Joins the field data table for a given field.
SensorDatabaseAggregator::resultVerbose public function Provide additional info about sensor call. Overrides SensorExtendedInfoInterface::resultVerbose 1
SensorThresholds::setFormError protected function Sets a form error for the given threshold key.
SensorThresholds::settingsFormValidate public function Form validator for a sensor settings form. Overrides SensorConfigurable::settingsFormValidate