View source
<?php
namespace Drupal\commerce_invoice\Entity;
use Drupal\commerce\Entity\CommerceContentEntityBase;
use Drupal\commerce_order\Adjustment;
use Drupal\commerce_order\Entity\OrderItemInterface;
use Drupal\commerce_price\Price;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
class InvoiceItem extends CommerceContentEntityBase implements InvoiceItemInterface {
use EntityChangedTrait;
public function getInvoice() {
return $this
->getTranslatedReferencedEntity('invoice_id');
}
public function getInvoiceId() {
return $this
->get('invoice_id')->target_id;
}
public function getTitle() {
return $this
->get('title')->value;
}
public function setTitle($title) {
$this
->set('title', $title);
return $this;
}
public function getDescription() {
return $this
->get('description')->value;
}
public function setDescription($description) {
$this
->set('description', $description);
return $this;
}
public function getFormat() {
return $this
->get('description')->format;
}
public function setFormat($format) {
$this
->get('description')->format = $format;
return $this;
}
public function getQuantity() {
return (string) $this
->get('quantity')->value;
}
public function setQuantity($quantity) {
$this
->set('quantity', (string) $quantity);
$this
->recalculateTotalPrice();
return $this;
}
public function getUnitPrice() {
if (!$this
->get('unit_price')
->isEmpty()) {
return $this
->get('unit_price')
->first()
->toPrice();
}
}
public function setUnitPrice(Price $unit_price) {
$this
->set('unit_price', $unit_price);
$this
->recalculateTotalPrice();
return $this;
}
public function getTotalPrice() {
if (!$this
->get('total_price')
->isEmpty()) {
return $this
->get('total_price')
->first()
->toPrice();
}
}
public function getAdjustments(array $adjustment_types = []) {
$adjustments = $this
->get('adjustments')
->getAdjustments();
if ($adjustment_types) {
foreach ($adjustments as $index => $adjustment) {
if (!in_array($adjustment
->getType(), $adjustment_types)) {
unset($adjustments[$index]);
}
}
$adjustments = array_values($adjustments);
}
return $adjustments;
}
public function setAdjustments(array $adjustments) {
$this
->set('adjustments', $adjustments);
return $this;
}
public function addAdjustment(Adjustment $adjustment) {
$this
->get('adjustments')
->appendItem($adjustment);
return $this;
}
public function removeAdjustment(Adjustment $adjustment) {
$this
->get('adjustments')
->removeAdjustment($adjustment);
return $this;
}
public function getAdjustedTotalPrice(array $adjustment_types = []) {
$total_price = $this
->getTotalPrice();
if (!$total_price) {
return NULL;
}
$adjusted_total_price = $this
->applyAdjustments($total_price, $adjustment_types);
$rounder = \Drupal::service('commerce_price.rounder');
$adjusted_total_price = $rounder
->round($adjusted_total_price);
return $adjusted_total_price;
}
public function getAdjustedUnitPrice(array $adjustment_types = []) {
$unit_price = $this
->getUnitPrice();
if (!$unit_price) {
return NULL;
}
$adjusted_total_price = $this
->getAdjustedTotalPrice($adjustment_types);
$adjusted_unit_price = $adjusted_total_price
->divide($this
->getQuantity());
$rounder = \Drupal::service('commerce_price.rounder');
$adjusted_unit_price = $rounder
->round($adjusted_unit_price);
return $adjusted_unit_price;
}
protected function applyAdjustments(Price $price, array $adjustment_types = []) {
$adjusted_price = $price;
foreach ($this
->getAdjustments($adjustment_types) as $adjustment) {
if (!$adjustment
->isIncluded()) {
$adjusted_price = $adjusted_price
->add($adjustment
->getAmount());
}
}
return $adjusted_price;
}
public function getData($key, $default = NULL) {
$data = [];
if (!$this
->get('data')
->isEmpty()) {
$data = $this
->get('data')
->first()
->getValue();
}
return isset($data[$key]) ? $data[$key] : $default;
}
public function setData($key, $value) {
$this
->get('data')
->__set($key, $value);
return $this;
}
public function unsetData($key) {
if (!$this
->get('data')
->isEmpty()) {
$data = $this
->get('data')
->first()
->getValue();
unset($data[$key]);
$this
->set('data', $data);
}
return $this;
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public function getOrderItem() {
return $this
->get('order_item_id')->entity;
}
public function getOrderItemId() {
return $this
->get('order_item_id')->target_id;
}
public function populateFromOrderItem(OrderItemInterface $order_item) {
$purchased_entity = $order_item
->getPurchasedEntity();
if ($purchased_entity) {
$langcode = $this
->language()
->getId();
if ($purchased_entity
->hasTranslation($langcode)) {
$purchased_entity = $purchased_entity
->getTranslation($langcode);
}
$title = $purchased_entity
->getOrderItemTitle();
}
else {
$title = $order_item
->getTitle();
}
$this
->set('title', $title);
if ($this
->isDefaultTranslation()) {
$this
->set('adjustments', $order_item
->getAdjustments());
$this
->set('quantity', $order_item
->getQuantity());
$this
->set('unit_price', $order_item
->getUnitPrice());
$this->order_item_id = $order_item
->id();
}
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
$this
->recalculateTotalPrice();
}
protected function recalculateTotalPrice() {
if ($unit_price = $this
->getUnitPrice()) {
$rounder = \Drupal::service('commerce_price.rounder');
$total_price = $unit_price
->multiply($this
->getQuantity());
$this->total_price = $rounder
->round($total_price);
}
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['invoice_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Invoice'))
->setDescription(t('The parent invoice.'))
->setSetting('target_type', 'commerce_invoice')
->setReadOnly(TRUE);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setDescription(t('The invoice item title.'))
->setSettings([
'default_value' => '',
'max_length' => 512,
])
->setTranslatable(TRUE)
->setRequired(TRUE);
$fields['description'] = BaseFieldDefinition::create('text_long')
->setLabel(t('Description'))
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'text_default',
'weight' => 0,
])
->setTranslatable(TRUE);
$fields['quantity'] = BaseFieldDefinition::create('decimal')
->setLabel(t('Quantity'))
->setDescription(t('The number of purchased units.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE)
->setSetting('min', 0)
->setDefaultValue(1)
->setDisplayOptions('form', [
'type' => 'commerce_quantity',
'weight' => 1,
])
->setDisplayConfigurable('form', TRUE);
$fields['unit_price'] = BaseFieldDefinition::create('commerce_price')
->setLabel(t('Unit price'))
->setDescription(t('The price of a single unit.'))
->setRequired(TRUE)
->setDisplayOptions('form', [
'type' => 'commerce_unit_price',
'weight' => 2,
'settings' => [
'require_confirmation' => FALSE,
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['total_price'] = BaseFieldDefinition::create('commerce_price')
->setLabel(t('Total price'))
->setDescription(t('The total price of the invoice item.'))
->setReadOnly(TRUE);
$fields['adjustments'] = BaseFieldDefinition::create('commerce_adjustment')
->setLabel(t('Adjustments'))
->setRequired(FALSE)
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDisplayOptions('form', [
'type' => 'commerce_adjustment_default',
'weight' => 5,
]);
$fields['data'] = BaseFieldDefinition::create('map')
->setLabel(t('Data'))
->setDescription(t('A serialized array of additional data.'));
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time when the invoice item was created.'))
->setRequired(TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time when the invoice item was last edited.'))
->setRequired(TRUE);
$fields['order_item_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Order item'))
->setDescription(t('The reference to the order item.'))
->setSetting('target_type', 'commerce_order_item')
->setReadOnly(TRUE);
return $fields;
}
}