InvoiceTotalSummary.php in Commerce Invoice 8.2
File
src/InvoiceTotalSummary.php
View source
<?php
namespace Drupal\commerce_invoice;
use Drupal\commerce_invoice\Entity\InvoiceInterface;
use Drupal\commerce_order\Adjustment;
use Drupal\commerce_order\AdjustmentTransformerInterface;
class InvoiceTotalSummary implements InvoiceTotalSummaryInterface {
protected $adjustmentTransformer;
public function __construct(AdjustmentTransformerInterface $adjustment_transformer) {
$this->adjustmentTransformer = $adjustment_transformer;
}
public function buildTotals(InvoiceInterface $invoice) {
$adjustments = $invoice
->collectAdjustments();
$adjustments = $this->adjustmentTransformer
->processAdjustments($adjustments);
$adjustments = array_filter($adjustments, function (Adjustment $adjustment) {
return $adjustment
->getType() === 'tax' || !$adjustment
->isIncluded();
});
$adjustments = array_map(function (Adjustment $adjustment) {
return $adjustment
->toArray();
}, $adjustments);
foreach ($adjustments as $index => $adjustment) {
$adjustments[$index]['total'] = $adjustments[$index]['amount'];
}
return [
'subtotal' => $invoice
->getSubtotalPrice(),
'adjustments' => $adjustments,
'total' => $invoice
->getTotalPrice(),
];
}
}