Created
November 13, 2017 00:24
-
-
Save clrockwell/a3efa2f2ef462e30d799eac4ec33043b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\commerce_form_customizations\Plugin\Commerce\ReportType; | |
use Drupal\commerce_order\Adjustment; | |
use Drupal\commerce_order\Entity\Order; | |
use Drupal\commerce_order\Entity\OrderInterface; | |
use Drupal\commerce_order\Entity\OrderItemInterface; | |
use Drupal\commerce_price\Price; | |
use Drupal\commerce_promotion\Entity\CouponInterface; | |
use Drupal\commerce_reports\Plugin\Commerce\ReportType\ReportTypeBase; | |
use Drupal\Core\Entity\EntityStorageInterface; | |
use Drupal\entity\BundleFieldDefinition; | |
/** | |
* Provide a report for Promotions on behalf of commerce_promotion | |
* | |
* @CommerceReportType( | |
* id = "promotion_aggregate_report", | |
* label = @Translation("Aggregate Promotion Report"), | |
* description = @Translation("Provide a report that keeps aggregate promotion usage"), | |
* provider = "commerce_promotion" | |
* ) | |
*/ | |
class PromotionReport extends ReportTypeBase { | |
/** | |
* {@inheritdoc} | |
*/ | |
public function generateReport(OrderInterface $order) { | |
/** @var EntityStorageInterface $orderReportStorage */ | |
$orderReportStorage = \Drupal::service('entity_type.manager')->getStorage('commerce_order_report'); | |
$adjustments = $order->collectAdjustments(); | |
/** @var Adjustment $adjustment */ | |
foreach ($adjustments as $adjustment) { | |
if ($adjustment->getType() != 'promotion') { | |
continue; | |
} | |
// Do we already have a report for this promotion? | |
$promotion_report = $orderReportStorage->loadByProperties([ | |
'par_promotion_id' => $adjustment->getSourceId(), | |
'type' => 'promotion_aggregate_report', | |
]); | |
if (!$promotion_report) { | |
$order_report = $orderReportStorage->create([ | |
'type' => $this->getPluginId(), | |
'order_id' => $order->id(), | |
'created' => $order->getPlacedTime(), | |
'par_promotion_id' => $adjustment->getSourceId(), | |
'par_adjustment' => $adjustment->getAmount(), | |
'par_subtotal' => $order->getSubtotalPrice(), | |
'par_total' => $order->getTotalPrice(), | |
'par_usage' => 1, | |
'par_discount_average' => $adjustment->getAmount()->getNumber(), | |
]); | |
$order_report->save(); | |
} | |
else { | |
$promotion_report = reset($promotion_report); | |
// Update adjustment | |
$current_adjustment = $promotion_report->par_adjustment->first()->toPrice(); | |
/** @var Price $new_adjustment */ | |
$new_adjustment = $current_adjustment->add($adjustment->getAmount()); | |
$promotion_report->par_adjustment->setValue($new_adjustment); | |
// Update subtotal | |
$current_subtotal = $promotion_report->par_subtotal->first()->toPrice(); | |
$new_subtotal = $current_subtotal->add($order->getSubtotalPrice()); | |
$promotion_report->par_subtotal->setValue($new_subtotal); | |
// Update total | |
$current_total = $promotion_report->par_total->first()->toPrice(); | |
$new_total = $current_total->add($order->getTotalPrice()); | |
$promotion_report->par_total->setValue($new_total); | |
// Update usage | |
$current_usage = $promotion_report->par_usage->first()->get('value')->getValue(); | |
$new_usage = $current_usage + 1; | |
$promotion_report->set('par_usage', $new_usage); | |
// Update average | |
$new_discount_average = $new_adjustment->divide($new_usage); | |
$promotion_report->par_discount_average->setValue($new_discount_average); | |
$promotion_report->save(); | |
} | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildFieldDefinitions() { | |
$fields = []; | |
$fields['par_promotion_id'] = BundleFieldDefinition::create('entity_reference') | |
->setLabel(t('Promotion')) | |
->setTargetEntityTypeId('commerce_promotion') | |
->setCardinality(1) | |
->setRequired(TRUE) | |
->setDisplayConfigurable('view', TRUE); | |
$fields['par_adjustment'] = BundleFieldDefinition::create('commerce_price') | |
->setLabel(t('Aggregate adjustment amount')) | |
->setCardinality(1) | |
->setRequired(TRUE) | |
->setDisplayConfigurable('view', TRUE); | |
$fields['par_subtotal'] = BundleFieldDefinition::create('commerce_price') | |
->setLabel(t('Aggregate order sub total')) | |
->setCardinality(1) | |
->setRequired(TRUE) | |
->setDisplayConfigurable('view', TRUE); | |
$fields['par_total'] = BundleFieldDefinition::create('commerce_price') | |
->setLabel(t('Aggregate order amount')) | |
->setCardinality(1) | |
->setRequired(TRUE) | |
->setDisplayConfigurable('view', TRUE); | |
$fields['par_usage'] = BundleFieldDefinition::create('integer') | |
->setLabel(t('Usage Count')) | |
->setCardinality(1) | |
->setRequired(FALSE) | |
->setDisplayConfigurable('view', TRUE); | |
$fields['par_discount_average'] = BundleFieldDefinition::create('commerce_price') | |
->setLabel(t('Average discount amount')) | |
->setCardinality(1) | |
->setRequired(FALSE) | |
->setDisplayConfigurable('view', TRUE); | |
return $fields; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment