You are here

class TaxReport in Commerce Reporting 8

Provide a report for Taxes on behalf of commerce_tax.

Plugin annotation


@CommerceReportType(
  id = "tax_report",
  label = @Translation("Tax Report"),
  description = @Translation("Tax Report"),
  provider = "commerce_tax",
)

Hierarchy

Expanded class hierarchy of TaxReport

File

src/Plugin/Commerce/ReportType/TaxReport.php, line 25

Namespace

Drupal\commerce_reports\Plugin\Commerce\ReportType
View source
class TaxReport extends ReportTypeBase implements ContainerFactoryPluginInterface {

  /**
   * The tax type storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $taxTypeStorage;

  /**
   * Constructs a new TaxReport object.
   *
   * @param array $configuration
   *   The plugin configuration, i.e. an array with configuration values keyed
   *   by configuration option name.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager);
    $this->taxTypeStorage = $entity_type_manager
      ->getStorage('commerce_tax_type');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildFieldDefinitions() {
    $fields = [];
    $fields['tax_amount'] = BundleFieldDefinition::create('commerce_price')
      ->setLabel(t('Tax amount'))
      ->setCardinality(1)
      ->setRequired(TRUE)
      ->setDisplayConfigurable('view', TRUE);
    $fields['tax_type_id'] = BundleFieldDefinition::create('entity_reference')
      ->setLabel(t('Tax type'))
      ->setSetting('target_type', 'commerce_tax_type')
      ->setCardinality(1)
      ->setRequired(TRUE)
      ->setDisplayConfigurable('view', TRUE);
    $fields['tax_type_label'] = BundleFieldDefinition::create('string')
      ->setLabel(t('Name'))
      ->setDescription(t('The tax type name.'))
      ->setSettings([
      'default_value' => '',
      'max_length' => 255,
    ])
      ->setRequired(TRUE)
      ->setDisplayConfigurable('view', TRUE);
    $fields['zone_id'] = BundleFieldDefinition::create('string')
      ->setLabel(t('Zone ID'))
      ->setDescription(t('The tax zone id.'))
      ->setSettings([
      'default_value' => '',
      'max_length' => 255,
    ])
      ->setRequired(FALSE)
      ->setDisplayConfigurable('view', TRUE);
    $fields['zone_label'] = BundleFieldDefinition::create('string')
      ->setLabel(t('Zone label'))
      ->setDescription(t('The tax zone label.'))
      ->setSettings([
      'default_value' => '',
      'max_length' => 255,
    ])
      ->setRequired(FALSE)
      ->setDisplayConfigurable('view', TRUE);
    $fields['rate_id'] = BundleFieldDefinition::create('string')
      ->setLabel(t('Rate ID'))
      ->setDescription(t('The tax rate id.'))
      ->setSettings([
      'default_value' => '',
      'max_length' => 255,
    ])
      ->setRequired(FALSE)
      ->setDisplayConfigurable('view', TRUE);
    $fields['rate_label'] = BundleFieldDefinition::create('string')
      ->setLabel(t('Rate label'))
      ->setDescription(t('The tax rate label.'))
      ->setSettings([
      'default_value' => '',
      'max_length' => 255,
    ])
      ->setRequired(FALSE)
      ->setDisplayConfigurable('view', TRUE);
    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public function generateReports(OrderInterface $order) {
    $adjustments = $order
      ->collectAdjustments();
    $adjustments = array_filter($order
      ->collectAdjustments(), function (Adjustment $adjustment) {
      return $adjustment
        ->getType() == 'tax' && !empty($adjustment
        ->getSourceId());
    });

    /** @var \Drupal\commerce_order\Adjustment $adjustment */
    foreach ($adjustments as $adjustment) {
      $source_id = $adjustment
        ->getSourceId();
      list($tax_entity_id, $zone_id, $rate_id) = explode('|', $source_id) + [
        NULL,
        NULL,
        NULL,
      ];
      if (empty($tax_entity_id)) {
        continue;
      }

      /** @var \Drupal\commerce_tax\Entity\TaxTypeInterface $tax_type */
      $tax_type = $this->taxTypeStorage
        ->load($tax_entity_id);
      if (!$tax_type) {
        continue;
      }

      // Base values for order report.
      $values = [
        'tax_amount' => $adjustment
          ->getAmount(),
        'tax_type_id' => $tax_type
          ->id(),
        'tax_type_label' => $tax_type
          ->label(),
        'zone_id' => NULL,
        'zone_label' => NULL,
        'rate_id' => NULL,
        'rate_label' => NULL,
      ];
      if ($tax_type
        ->getPlugin() instanceof LocalTaxTypeInterface) {
        $zones = $tax_type
          ->getPlugin()
          ->getZones();
        if (!empty($zones) && isset($zones[$zone_id])) {
          $values['zone_id'] = $zone_id;
          $values['zone_label'] = $zones[$zone_id]
            ->getLabel();

          /** @var \Drupal\commerce_tax\TaxRate $rate */
          foreach ($zones[$zone_id]
            ->getRates() as $rate) {
            if ($rate
              ->getId() == $rate_id) {
              $values['rate_id'] = $rate
                ->getId();
              $values['rate_label'] = $rate
                ->getLabel();
              break;
            }
          }
        }
      }
      $this
        ->createFromOrder($order, $values);
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function doBuildReportTableHeaders() {
    return [
      'formatted_date' => t('Date'),
      'tax_type_id_count' => t('# Tax charges'),
      'tax_amount_sum' => t('Total tax amount'),
      'tax_amount_currency_code' => t('Currency'),
    ];
  }

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

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

}

Members

Namesort descending Modifiers Type Description Overrides
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::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
TaxReport::$taxTypeStorage protected property The tax type storage.
TaxReport::buildFieldDefinitions public function Builds the field definitions for entities of this bundle. Overrides BundlePluginInterface::buildFieldDefinitions
TaxReport::buildQuery public function Builds the aggregate query. Overrides ReportTypeInterface::buildQuery
TaxReport::create public static function Creates an instance of the plugin. Overrides ReportTypeBase::create
TaxReport::doBuildReportTableHeaders protected function Builds the report table headers. Overrides ReportTypeBase::doBuildReportTableHeaders
TaxReport::doBuildReportTableRow protected function Build the report table row. Overrides ReportTypeBase::doBuildReportTableRow
TaxReport::generateReports public function Generates order reports for an order. Overrides ReportTypeInterface::generateReports
TaxReport::__construct public function Constructs a new TaxReport object. Overrides ReportTypeBase::__construct