You are here

class OrderReport in Commerce Reporting 8

Same name in this branch
  1. 8 src/Entity/OrderReport.php \Drupal\commerce_reports\Entity\OrderReport
  2. 8 src/Plugin/Commerce/ReportType/OrderReport.php \Drupal\commerce_reports\Plugin\Commerce\ReportType\OrderReport

Provides the basic Order Report.

Plugin annotation


@CommerceReportType(
  id = "order_report",
  label = @Translation("Order Report"),
  description = @Translation("Basic order report with order id, total, and created date")
)

Hierarchy

Expanded class hierarchy of OrderReport

File

src/Plugin/Commerce/ReportType/OrderReport.php, line 19

Namespace

Drupal\commerce_reports\Plugin\Commerce\ReportType
View source
class OrderReport extends ReportTypeBase {

  /**
   * {@inheritdoc}
   */
  public function buildFieldDefinitions() {
    $fields = [];
    $fields['order_type_id'] = BundleFieldDefinition::create('entity_reference')
      ->setLabel(t('Order type'))
      ->setDescription(t('The order type.'))
      ->setSetting('target_type', 'commerce_order_type')
      ->setReadOnly(TRUE);
    $fields['amount'] = BundleFieldDefinition::create('commerce_price')
      ->setLabel(t('Total Amount'))
      ->setDescription(t('The total amount of the order'))
      ->setCardinality(1)
      ->setRequired(TRUE)
      ->setDisplayConfigurable('view', TRUE);
    $fields['mail'] = BundleFieldDefinition::create('email')
      ->setLabel(t('Contact email'))
      ->setDescription(t('The email address associated with the order.'))
      ->setCardinality(1)
      ->setSetting('max_length', 255)
      ->setRequired(TRUE)
      ->setDisplayConfigurable('view', TRUE);
    $fields['billing_address'] = BundleFieldDefinition::create('address')
      ->setLabel(t('Address'))
      ->setDescription(t('The store address.'))
      ->setCardinality(1)
      ->setRequired(TRUE)
      ->setDisplayConfigurable('view', TRUE);
    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public function generateReports(OrderInterface $order) {
    $billing_profile = $order
      ->getBillingProfile();

    /** @var \Drupal\address\Plugin\Field\FieldType\AddressItem $address */
    $address = $billing_profile
      ->get('address')
      ->first();
    $values = [
      'order_type_id' => $order
        ->bundle(),
      'amount' => $order
        ->getTotalPrice(),
      'mail' => $order
        ->getEmail(),
      'billing_address' => $address
        ->toArray(),
    ];
    $this
      ->createFromOrder($order, $values);
  }

  /**
   * {@inheritdoc}
   */
  public function buildQuery(QueryAggregateInterface $query) {
    $query
      ->aggregate('mail', 'COUNT');
    $query
      ->aggregate('amount.number', 'SUM');
    $query
      ->aggregate('amount.number', 'AVG');
    $query
      ->groupBy('amount.currency_code');
  }

  /**
   * {@inheritdoc}
   */
  protected function doBuildReportTableHeaders() {
    return [
      'formatted_date' => t('Date'),
      'order_id_count' => t('# Orders'),
      'mail_count' => t('# Customers'),
      'amountnumber_sum' => t('Total revenue'),
      'amountnumber_avg' => t('Average revenue'),
      'amount_currency_code' => t('Currency'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function doBuildReportTableRow(array $result) {
    $currency_code = $result['amount_currency_code'];
    $row = [
      $result['formatted_date'],
      $result['order_id_count'],
      $result['mail_count'],
      [
        'data' => [
          '#type' => 'inline_template',
          '#template' => '{{price|commerce_price_format}}',
          '#context' => [
            'price' => new Price($result['amountnumber_sum'], $currency_code),
          ],
        ],
      ],
      [
        'data' => [
          '#type' => 'inline_template',
          '#template' => '{{price|commerce_price_format}}',
          '#context' => [
            'price' => new Price($result['amountnumber_avg'], $currency_code),
          ],
        ],
      ],
      $currency_code,
    ];
    return $row;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OrderReport::buildFieldDefinitions public function Builds the field definitions for entities of this bundle. Overrides BundlePluginInterface::buildFieldDefinitions
OrderReport::buildQuery public function Builds the aggregate query. Overrides ReportTypeInterface::buildQuery
OrderReport::doBuildReportTableHeaders protected function Builds the report table headers. Overrides ReportTypeBase::doBuildReportTableHeaders
OrderReport::doBuildReportTableRow protected function Build the report table row. Overrides ReportTypeBase::doBuildReportTableRow
OrderReport::generateReports public function Generates order reports for an order. Overrides ReportTypeInterface::generateReports
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
ReportTypeBase::$orderReportStorage protected property The order report storage.
ReportTypeBase::buildReportTable public function Build a report table from query results. Overrides ReportTypeInterface::buildReportTable
ReportTypeBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 2
ReportTypeBase::createFromOrder public function Creates a new order report using the given order and values. Overrides ReportTypeInterface::createFromOrder
ReportTypeBase::getDescription public function Gets the order report type description. Overrides ReportTypeInterface::getDescription
ReportTypeBase::getLabel public function Gets the order report type label. Overrides ReportTypeInterface::getLabel
ReportTypeBase::__construct public function Constructs a new ReportTypeBase object. Overrides PluginBase::__construct 2