View source
<?php
namespace Drupal\commerce_reports\Plugin\Commerce\ReportType;
use Drupal\commerce_order\Adjustment;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_price\Price;
use Drupal\commerce_tax\Plugin\Commerce\TaxType\LocalTaxTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryAggregateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\entity\BundleFieldDefinition;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TaxReport extends ReportTypeBase implements ContainerFactoryPluginInterface {
protected $taxTypeStorage;
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');
}
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'));
}
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;
}
public function generateReports(OrderInterface $order) {
$adjustments = $order
->collectAdjustments();
$adjustments = array_filter($order
->collectAdjustments(), function (Adjustment $adjustment) {
return $adjustment
->getType() == 'tax' && !empty($adjustment
->getSourceId());
});
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;
}
$tax_type = $this->taxTypeStorage
->load($tax_entity_id);
if (!$tax_type) {
continue;
}
$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();
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);
}
}
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'),
];
}
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;
}
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');
}
}