class Invoice in Commerce Invoice 8.2
Same name and namespace in other branches
- 7.2 src/Entity/Invoice.php \Drupal\commerce_invoice\Entity\Invoice
Defines the invoice entity class.
Plugin annotation
@ContentEntityType(
id = "commerce_invoice",
label = @Translation("Invoice"),
label_collection = @Translation("Invoices"),
label_singular = @Translation("invoice"),
label_plural = @Translation("invoices"),
label_count = @PluralTranslation(
singular = "@count invoice",
plural = "@count invoices",
),
bundle_label = @Translation("Invoice type"),
handlers = {
"event" = "Drupal\commerce_invoice\Event\InvoiceEvent",
"storage" = "Drupal\commerce_invoice\InvoiceStorage",
"access" = "Drupal\commerce_invoice\InvoiceAccessControlHandler",
"list_builder" = "Drupal\commerce_invoice\InvoiceListBuilder",
"permission_provider" = "Drupal\commerce_invoice\InvoicePermissionProvider",
"views_data" = "Drupal\commerce\CommerceEntityViewsData",
"form" = {
"default" = "Drupal\commerce_invoice\Form\InvoiceForm",
"delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
},
"local_task_provider" = {
"default" = "Drupal\entity\Menu\DefaultEntityLocalTaskProvider",
},
"route_provider" = {
"default" = "Drupal\commerce_invoice\InvoiceRouteProvider",
},
"translation" = "Drupal\content_translation\ContentTranslationHandler"
},
translatable = TRUE,
base_table = "commerce_invoice",
data_table = "commerce_invoice_field_data",
admin_permission = "administer commerce_invoice",
permission_granularity = "bundle",
entity_keys = {
"id" = "invoice_id",
"label" = "invoice_number",
"langcode" = "langcode",
"uuid" = "uuid",
"owner" = "uid",
"bundle" = "type",
},
links = {
"canonical" = "/admin/commerce/invoices/{commerce_invoice}",
"collection" = "/admin/commerce/invoices",
"download" = "/invoice/{commerce_invoice}/download",
"delete-form" = "/admin/commerce/invoices/{commerce_invoice}/delete",
"payment-form" = "/admin/commerce/invoices/{commerce_invoice}/payment",
},
bundle_entity_type = "commerce_invoice_type",
field_ui_base_route = "entity.commerce_invoice_type.edit_form",
allow_number_patterns = TRUE,
)
Hierarchy
- class \Drupal\Core\Entity\EntityBase implements EntityInterface uses RefinableCacheableDependencyTrait, DependencySerializationTrait
- class \Drupal\Core\Entity\ContentEntityBase implements \Drupal\Core\Entity\IteratorAggregate, ContentEntityInterface, TranslationStatusInterface uses EntityChangesDetectionTrait, SynchronizableEntityTrait
- class \Drupal\commerce\Entity\CommerceContentEntityBase implements CommerceContentEntityInterface
- class \Drupal\commerce_invoice\Entity\Invoice implements InvoiceInterface uses EntityChangedTrait
- class \Drupal\commerce\Entity\CommerceContentEntityBase implements CommerceContentEntityInterface
- class \Drupal\Core\Entity\ContentEntityBase implements \Drupal\Core\Entity\IteratorAggregate, ContentEntityInterface, TranslationStatusInterface uses EntityChangesDetectionTrait, SynchronizableEntityTrait
Expanded class hierarchy of Invoice
9 files declare their use of Invoice
- commerce_invoice.install in ./
commerce_invoice.install - Contains install and update functions for Commerce Invoice.
- commerce_invoice.module in ./
commerce_invoice.module - Defines the Invoice entity and associated features.
- InvoiceConfirmationMailTest.php in tests/
src/ Kernel/ Mail/ InvoiceConfirmationMailTest.php - InvoiceConfirmationTest.php in tests/
src/ Kernel/ InvoiceConfirmationTest.php - InvoiceFileManagerTest.php in tests/
src/ Kernel/ InvoiceFileManagerTest.php
4 string references to 'Invoice'
- commerce_invoice.commerce_invoice_type.default.yml in config/
install/ commerce_invoice.commerce_invoice_type.default.yml - config/install/commerce_invoice.commerce_invoice_type.default.yml
- commerce_invoice.links.menu.yml in ./
commerce_invoice.links.menu.yml - commerce_invoice.links.menu.yml
- commerce_invoice.workflow_groups.yml in ./
commerce_invoice.workflow_groups.yml - commerce_invoice.workflow_groups.yml
- commerce_number_pattern.commerce_number_pattern.invoice_default.yml in config/
install/ commerce_number_pattern.commerce_number_pattern.invoice_default.yml - config/install/commerce_number_pattern.commerce_number_pattern.invoice_default.yml
File
- src/
Entity/ Invoice.php, line 78
Namespace
Drupal\commerce_invoice\EntityView source
class Invoice extends CommerceContentEntityBase implements InvoiceInterface {
use EntityChangedTrait;
/**
* {@inheritdoc}
*/
public function toUrl($rel = 'canonical', array $options = []) {
// For better UX, the collection URL of invoices that are created for a
// single order should be the list of invoices for that specific order.
$invoice_collection_url = NULL;
if ($rel === 'collection' && count($this
->get('orders')) === 1) {
$order = $this
->get('orders')
->first()->entity;
if ($this
->bundle() === 'default') {
$invoice_collection_url = $order
->toUrl('invoices', $options);
}
elseif ($this
->bundle() === 'credit_memo') {
$invoice_collection_url = $order
->toUrl('credit-memos', $options);
}
}
return $invoice_collection_url ?? parent::toUrl($rel, $options);
}
/**
* {@inheritdoc}
*/
public function getInvoiceNumber() {
return $this
->get('invoice_number')->value;
}
/**
* {@inheritdoc}
*/
public function setInvoiceNumber($invoice_number) {
$this
->set('invoice_number', $invoice_number);
return $this;
}
/**
* {@inheritdoc}
*/
public function getStore() {
return $this
->getTranslatedReferencedEntity('store_id');
}
/**
* {@inheritdoc}
*/
public function setStore(StoreInterface $store) {
$this
->set('store_id', $store
->id());
return $this;
}
/**
* {@inheritdoc}
*/
public function getStoreId() {
return $this
->get('store_id')->target_id;
}
/**
* {@inheritdoc}
*/
public function setStoreId($store_id) {
$this
->set('store_id', $store_id);
return $this;
}
/**
* {@inheritdoc}
*/
public function getCustomer() {
$customer = $this
->get('uid')->entity;
// Handle deleted customers.
if (!$customer) {
$customer = User::getAnonymousUser();
}
return $customer;
}
/**
* {@inheritdoc}
*/
public function setCustomer(UserInterface $account) {
$this
->set('uid', $account
->id());
return $this;
}
/**
* {@inheritdoc}
*/
public function getCustomerId() {
return $this
->get('uid')->target_id;
}
/**
* {@inheritdoc}
*/
public function setCustomerId($uid) {
$this
->set('uid', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function getEmail() {
return $this
->get('mail')->value;
}
/**
* {@inheritdoc}
*/
public function setEmail($mail) {
$this
->set('mail', $mail);
return $this;
}
/**
* {@inheritdoc}
*/
public function getBillingProfile() {
return $this
->get('billing_profile')->entity;
}
/**
* {@inheritdoc}
*/
public function setBillingProfile(ProfileInterface $profile) {
$this
->set('billing_profile', $profile);
return $this;
}
/**
* {@inheritdoc}
*/
public function getOrders() {
return $this
->get('orders')
->referencedEntities();
}
/**
* {@inheritdoc}
*/
public function setOrders(array $orders) {
$this
->set('orders', $orders);
return $this;
}
/**
* {@inheritdoc}
*/
public function getItems() {
return $this
->getTranslatedReferencedEntities('invoice_items');
}
/**
* {@inheritdoc}
*/
public function setItems(array $invoice_items) {
$this
->set('invoice_items', $invoice_items);
$this
->recalculateTotalPrice();
return $this;
}
/**
* {@inheritdoc}
*/
public function hasItems() {
return !$this
->get('invoice_items')
->isEmpty();
}
/**
* {@inheritdoc}
*/
public function addItem(InvoiceItemInterface $invoice_item) {
$this
->get('invoice_items')
->appendItem($invoice_item);
$this
->recalculateTotalPrice();
return $this;
}
/**
* {@inheritdoc}
*/
public function removeItem(InvoiceItemInterface $invoice_item) {
$index = $this
->getItemIndex($invoice_item);
if ($index !== FALSE) {
$this
->get('invoice_items')
->offsetUnset($index);
$this
->recalculateTotalPrice();
}
return $this;
}
/**
* {@inheritdoc}
*/
public function hasItem(InvoiceItemInterface $invoice_item) {
return $this
->getItemIndex($invoice_item) !== FALSE;
}
/**
* Gets the index of the given invoice item.
*
* @param \Drupal\commerce_invoice\Entity\InvoiceItemInterface $invoice_item
* The invoice item.
*
* @return int|bool
* The index of the given invoice item, or FALSE if not found.
*/
protected function getItemIndex(InvoiceItemInterface $invoice_item) {
$values = $this
->get('invoice_items')
->getValue();
$invoice_item_ids = array_map(function ($value) {
return $value['target_id'];
}, $values);
return array_search($invoice_item
->id(), $invoice_item_ids);
}
/**
* {@inheritdoc}
*/
public function getAdjustments(array $adjustment_types = []) {
/** @var \Drupal\commerce_order\Adjustment[] $adjustments */
$adjustments = $this
->get('adjustments')
->getAdjustments();
// Filter adjustments by type, if needed.
if ($adjustment_types) {
foreach ($adjustments as $index => $adjustment) {
if (!in_array($adjustment
->getType(), $adjustment_types)) {
unset($adjustments[$index]);
}
}
$adjustments = array_values($adjustments);
}
return $adjustments;
}
/**
* {@inheritdoc}
*/
public function setAdjustments(array $adjustments) {
$this
->set('adjustments', $adjustments);
$this
->recalculateTotalPrice();
return $this;
}
/**
* {@inheritdoc}
*/
public function addAdjustment(Adjustment $adjustment) {
$this
->get('adjustments')
->appendItem($adjustment);
$this
->recalculateTotalPrice();
return $this;
}
/**
* {@inheritdoc}
*/
public function removeAdjustment(Adjustment $adjustment) {
$this
->get('adjustments')
->removeAdjustment($adjustment);
$this
->recalculateTotalPrice();
return $this;
}
/**
* {@inheritdoc}
*/
public function collectAdjustments(array $adjustment_types = []) {
$adjustments = [];
foreach ($this
->getItems() as $invoice_item) {
foreach ($invoice_item
->getAdjustments($adjustment_types) as $adjustment) {
$adjustments[] = $adjustment;
}
}
foreach ($this
->getAdjustments($adjustment_types) as $adjustment) {
$adjustments[] = $adjustment;
}
return $adjustments;
}
/**
* {@inheritdoc}
*/
public function getPaymentMethod() {
return $this
->get('payment_method')->value;
}
/**
* {@inheritdoc}
*/
public function setPaymentMethod($payment_method) {
$this
->set('payment_method', $payment_method);
return $this;
}
/**
* {@inheritdoc}
*/
public function getSubtotalPrice() {
/** @var \Drupal\commerce_price\Price $subtotal_price */
$subtotal_price = NULL;
foreach ($this
->getItems() as $invoice_item) {
if ($invoice_item_total = $invoice_item
->getTotalPrice()) {
$subtotal_price = $subtotal_price ? $subtotal_price
->add($invoice_item_total) : $invoice_item_total;
}
}
return $subtotal_price;
}
/**
* {@inheritdoc}
*/
public function recalculateTotalPrice() {
/** @var \Drupal\commerce_price\Price $total_price */
$total_price = NULL;
foreach ($this
->getItems() as $invoice_item) {
if ($invoice_item_total = $invoice_item
->getTotalPrice()) {
$total_price = $total_price ? $total_price
->add($invoice_item_total) : $invoice_item_total;
}
}
if ($total_price) {
$adjustments = $this
->collectAdjustments();
if ($adjustments) {
/** @var \Drupal\commerce_order\AdjustmentTransformerInterface $adjustment_transformer */
$adjustment_transformer = \Drupal::service('commerce_order.adjustment_transformer');
$adjustments = $adjustment_transformer
->combineAdjustments($adjustments);
$adjustments = $adjustment_transformer
->roundAdjustments($adjustments);
foreach ($adjustments as $adjustment) {
if (!$adjustment
->isIncluded()) {
$total_price = $total_price
->add($adjustment
->getAmount());
}
}
}
}
$this->total_price = $total_price;
return $this;
}
/**
* {@inheritdoc}
*/
public function getTotalPrice() {
if (!$this
->get('total_price')
->isEmpty()) {
return $this
->get('total_price')
->first()
->toPrice();
}
}
/**
* {@inheritdoc}
*/
public function getTotalPaid() {
if (!$this
->get('total_paid')
->isEmpty()) {
return $this
->get('total_paid')
->first()
->toPrice();
}
elseif ($total_price = $this
->getTotalPrice()) {
return new Price('0', $total_price
->getCurrencyCode());
}
}
/**
* {@inheritdoc}
*/
public function setTotalPaid(Price $total_paid) {
$this
->set('total_paid', $total_paid);
}
/**
* {@inheritdoc}
*/
public function getBalance() {
if ($total_price = $this
->getTotalPrice()) {
return $total_price
->subtract($this
->getTotalPaid());
}
}
/**
* {@inheritdoc}
*/
public function isPaid() {
$total_price = $this
->getTotalPrice();
if (!$total_price) {
return FALSE;
}
$balance = $this
->getBalance();
return $balance
->isNegative() || $balance
->isZero();
}
/**
* {@inheritdoc}
*/
public function getState() {
return $this
->get('state')
->first();
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function setData($key, $value) {
$this
->get('data')
->__set($key, $value);
return $this;
}
/**
* {@inheritdoc}
*/
public function unsetData($key) {
if (!$this
->get('data')
->isEmpty()) {
$data = $this
->get('data')
->first()
->getValue();
unset($data[$key]);
$this
->set('data', $data);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this
->get('created')->value;
}
/**
* {@inheritdoc}
*/
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
/**
* {@inheritdoc}
*/
public function getInvoiceDateTime() {
return $this
->get('invoice_date')->value;
}
/**
* {@inheritdoc}
*/
public function setInvoiceDateTime($timestamp) {
$this
->set('invoice_date', $timestamp);
return $this;
}
/**
* {@inheritdoc}
*/
public function getDueDateTime() {
return $this
->get('due_date')->value;
}
/**
* {@inheritdoc}
*/
public function setDueDateTime($timestamp) {
$this
->set('due_date', $timestamp);
return $this;
}
/**
* {@inheritdoc}
*/
public function getFile() {
return $this
->get('invoice_file')->entity;
}
/**
* {@inheritdoc}
*/
public function setFile(FileInterface $file) {
$this
->set('invoice_file', $file
->id());
return $this;
}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
foreach ([
'store_id',
] as $field) {
if ($this
->get($field)
->isEmpty()) {
throw new EntityMalformedException(sprintf('Required invoice field "%s" is empty.', $field));
}
}
// Store the original override language to be able to put it back.
$original_language = $this
->languageManager()
->getConfigOverrideLanguage();
// Store the invoice type data in the invoice data for immutability reasons.
$fields_whitelist = [
'paymentTerms',
'footerText',
'logo',
];
$fields_whitelist = array_combine($fields_whitelist, $fields_whitelist);
// The following code is necessary to store the translated invoice type
// data for each translation.
foreach ($this
->getTranslationLanguages() as $langcode => $language) {
$translated_invoice = $this
->getTranslation($langcode);
if (!$translated_invoice
->getData('invoice_type', FALSE)) {
$this
->languageManager()
->setConfigOverrideLanguage($language);
$invoice_type = InvoiceType::load($this
->bundle());
$invoice_type_data = $invoice_type
->toArray();
// Store in the data array the following invoice type fields.
$invoice_type_data = array_filter(array_intersect_key($invoice_type_data, $fields_whitelist));
if ($invoice_type_data) {
$translated_invoice
->setData('invoice_type', $invoice_type_data);
}
}
}
$this
->languageManager()
->setConfigOverrideLanguage($original_language);
$invoice_type = InvoiceType::load($this
->bundle());
// Skip generating an invoice number for draft invoices.
if ($this
->getState()
->getId() != 'draft' && empty($this
->getInvoiceNumber())) {
/** @var \Drupal\commerce_number_pattern\Entity\NumberPatternInterface $number_pattern */
$number_pattern = $invoice_type
->getNumberPattern();
if ($number_pattern) {
$invoice_number = $number_pattern
->getPlugin()
->generate($this);
$this
->setInvoiceNumber($invoice_number);
}
}
$customer = $this
->getCustomer();
// The customer has been deleted, clear the reference.
if ($this
->getCustomerId() && $customer
->isAnonymous()) {
$this
->setCustomerId(0);
}
// Make sure the billing profile is owned by the invoice, not the customer.
$billing_profile = $this
->getBillingProfile();
if ($billing_profile && $billing_profile
->getOwnerId()) {
$billing_profile
->setOwnerId(0);
$billing_profile
->save();
}
if (empty($this
->getInvoiceDateTime())) {
$this
->setInvoiceDateTime(\Drupal::time()
->getRequestTime());
}
// Calculate the due date if not set and if configured to do so on the
// invoice type.
if ($this
->isNew() && empty($this
->getDueDateTime()) && !empty($invoice_type
->getDueDays())) {
$invoice_date = DrupalDateTime::createFromTimestamp($this
->getInvoiceDateTime());
$due_date = $invoice_date
->modify("+{$invoice_type->getDueDays()} days");
$this
->setDueDateTime($due_date
->getTimestamp());
}
// When the invoice state is updated, clear the invoice file reference.
// (A "paid" invoice probably looks different than a "pending" invoice).
// That'll force the invoice file manager to regenerate an invoice PDF
// the next time it's called.
$original_state = isset($this->original) ? $this->original
->getState()
->getId() : '';
if ($original_state && $original_state !== $this
->getState()
->getId() && !empty($this
->getFile())) {
$this
->set('invoice_file', NULL);
}
}
/**
* {@inheritdoc}
*/
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
// Ensure there's a back-reference on each invoice item.
foreach ($this
->getItems() as $invoice_item) {
if ($invoice_item->invoice_id
->isEmpty()) {
$invoice_item->invoice_id = $this
->id();
$invoice_item
->save();
}
}
}
/**
* {@inheritdoc}
*/
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
// Delete the invoice items of a deleted invoice.
$invoice_items = [];
/** @var \Drupal\commerce_invoice\Entity\InvoiceInterface $entity */
foreach ($entities as $entity) {
foreach ($entity
->getItems() as $invoice_item) {
$invoice_items[$invoice_item
->id()] = $invoice_item;
}
}
if (!$invoice_items) {
return;
}
$invoice_item_storage = \Drupal::service('entity_type.manager')
->getStorage('commerce_invoice_item');
$invoice_item_storage
->delete($invoice_items);
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['invoice_number'] = BaseFieldDefinition::create('string')
->setLabel(t('Invoice number'))
->setDescription(t('The invoice number.'))
->setRequired(TRUE)
->setDefaultValue('')
->setSetting('max_length', 255)
->setDisplayConfigurable('view', TRUE);
$fields['store_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Store'))
->setDescription(t('The store to which the invoice belongs.'))
->setCardinality(1)
->setRequired(TRUE)
->setSetting('target_type', 'commerce_store')
->setSetting('handler', 'default')
->setDisplayConfigurable('view', TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Customer'))
->setDescription(t('The customer.'))
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback('Drupal\\commerce_invoice\\Entity\\Invoice::getCurrentUserId')
->setTranslatable(FALSE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'author',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE);
$fields['mail'] = BaseFieldDefinition::create('email')
->setLabel(t('Contact email'))
->setDescription(t('The email address associated with the invoice.'))
->setDefaultValue('')
->setSetting('max_length', 255)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE);
$fields['billing_profile'] = BaseFieldDefinition::create('entity_reference_revisions')
->setLabel(t('Billing information'))
->setDescription(t('Billing profile'))
->setSetting('target_type', 'profile')
->setSetting('handler', 'default')
->setSetting('handler_settings', [
'target_bundles' => [
'customer',
],
])
->setDisplayConfigurable('view', TRUE);
$fields['orders'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Orders'))
->setDescription(t('The invoice orders.'))
->setRequired(TRUE)
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setSetting('target_type', 'commerce_order')
->setSetting('handler', 'default')
->setDisplayConfigurable('view', TRUE);
$fields['invoice_items'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Invoice items'))
->setDescription(t('The invoice items.'))
->setRequired(TRUE)
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setSetting('target_type', 'commerce_invoice_item')
->setSetting('handler', 'default')
->setDisplayOptions('form', [
'type' => 'inline_entity_form_complex',
'weight' => 0,
'settings' => [
'allow_new' => FALSE,
],
])
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'commerce_invoice_item_table',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE);
$fields['adjustments'] = BaseFieldDefinition::create('commerce_adjustment')
->setLabel(t('Adjustments'))
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDisplayOptions('form', [
'type' => 'commerce_adjustment_default',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE);
$fields['payment_method'] = BaseFieldDefinition::create('string')
->setLabel(t('Payment method'))
->setDescription(t('The payment method.'))
->setRequired(TRUE)
->setDefaultValue('')
->setSetting('max_length', 255);
$fields['total_price'] = BaseFieldDefinition::create('commerce_price')
->setLabel(t('Total price'))
->setDescription(t('The total price of the invoice.'))
->setReadOnly(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'commerce_invoice_total_summary',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE);
$fields['total_paid'] = BaseFieldDefinition::create('commerce_price')
->setLabel(t('Total paid'))
->setDescription(t('The total paid price of the invoice.'))
->setDisplayConfigurable('view', TRUE);
$fields['state'] = BaseFieldDefinition::create('state')
->setLabel(t('State'))
->setDescription(t('The invoice state.'))
->setRequired(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'state_transition_form',
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setSetting('workflow_callback', [
'\\Drupal\\commerce_invoice\\Entity\\Invoice',
'getWorkflowId',
]);
$fields['data'] = BaseFieldDefinition::create('map')
->setLabel(t('Data'))
->setTranslatable(TRUE)
->setDescription(t('A serialized array of additional data.'));
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time when the invoice was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time when the invoice was last edited.'));
$fields['invoice_date'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Invoice date'))
->setDescription(t('The invoice date'))
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'timestamp',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE);
$fields['due_date'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Due date'))
->setDescription(t('The date the invoice is due.'))
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'timestamp',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE);
$fields['invoice_file'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Invoice PDF'))
->setDescription(t('The invoice PDF file.'))
->setSetting('target_type', 'file')
->setDisplayConfigurable('view', TRUE);
return $fields;
}
/**
* Default value callback for 'uid' base field definition.
*
* @see ::baseFieldDefinitions()
*
* @return array
* An array of default values.
*/
public static function getCurrentUserId() {
return [
\Drupal::currentUser()
->id(),
];
}
/**
* Gets the workflow ID for the state field.
*
* @param \Drupal\commerce_invoice\Entity\InvoiceInterface $invoice
* The invoice.
*
* @return string
* The workflow ID.
*/
public static function getWorkflowId(InvoiceInterface $invoice) {
return InvoiceType::load($invoice
->bundle())
->getWorkflowId();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableDependencyTrait:: |
protected | property | Cache contexts. | |
CacheableDependencyTrait:: |
protected | property | Cache max-age. | |
CacheableDependencyTrait:: |
protected | property | Cache tags. | |
CacheableDependencyTrait:: |
protected | function | Sets cacheability; useful for value object constructors. | |
CommerceContentEntityBase:: |
protected | function | Ensures entities are in the current entity's language, if possible. | |
CommerceContentEntityBase:: |
public | function |
Gets the translations of an entity reference field. Overrides CommerceContentEntityInterface:: |
|
CommerceContentEntityBase:: |
public | function |
Gets the translation of a referenced entity. Overrides CommerceContentEntityInterface:: |
|
ContentEntityBase:: |
protected | property | Language code identifying the entity active language. | |
ContentEntityBase:: |
protected | property | Local cache for the default language code. | |
ContentEntityBase:: |
protected | property | The default langcode entity key. | |
ContentEntityBase:: |
protected | property | Whether the revision translation affected flag has been enforced. | |
ContentEntityBase:: |
protected | property | Holds untranslatable entity keys such as the ID, bundle, and revision ID. | |
ContentEntityBase:: |
protected | property | Local cache for field definitions. | |
ContentEntityBase:: |
protected | property | The array of fields, each being an instance of FieldItemListInterface. | |
ContentEntityBase:: |
protected static | property | Local cache for fields to skip from the checking for translation changes. | |
ContentEntityBase:: |
protected | property | Indicates whether this is the default revision. | |
ContentEntityBase:: |
protected | property | The language entity key. | |
ContentEntityBase:: |
protected | property | Local cache for the available language objects. | |
ContentEntityBase:: |
protected | property | The loaded revision ID before the new revision was set. | |
ContentEntityBase:: |
protected | property | Boolean indicating whether a new revision should be created on save. | |
ContentEntityBase:: |
protected | property | The revision translation affected entity key. | |
ContentEntityBase:: |
protected | property | Holds translatable entity keys such as the label. | |
ContentEntityBase:: |
protected | property | A flag indicating whether a translation object is being initialized. | |
ContentEntityBase:: |
protected | property | An array of entity translation metadata. | |
ContentEntityBase:: |
protected | property | Whether entity validation was performed. | |
ContentEntityBase:: |
protected | property | Whether entity validation is required before saving the entity. | |
ContentEntityBase:: |
protected | property | The plain data values of the contained fields. | |
ContentEntityBase:: |
public | function |
Checks data value access. Overrides EntityBase:: |
1 |
ContentEntityBase:: |
public | function |
Adds a new translation to the translatable object. Overrides TranslatableInterface:: |
|
ContentEntityBase:: |
public | function |
Gets the bundle of the entity. Overrides EntityBase:: |
|
ContentEntityBase:: |
public static | function |
Provides field definitions for a specific bundle. Overrides FieldableEntityInterface:: |
4 |
ContentEntityBase:: |
protected | function | Clear entity translation object cache to remove stale references. | |
ContentEntityBase:: |
public | function |
Creates a duplicate of the entity. Overrides EntityBase:: |
1 |
ContentEntityBase:: |
public | function |
Gets a field item list. Overrides FieldableEntityInterface:: |
|
ContentEntityBase:: |
protected | function | Gets the value of the given entity key, if defined. | 1 |
ContentEntityBase:: |
public | function |
Gets the definition of a contained field. Overrides FieldableEntityInterface:: |
|
ContentEntityBase:: |
public | function |
Gets an array of field definitions of all contained fields. Overrides FieldableEntityInterface:: |
|
ContentEntityBase:: |
public | function |
Gets an array of all field item lists. Overrides FieldableEntityInterface:: |
|
ContentEntityBase:: |
protected | function | Returns an array of field names to skip in ::hasTranslationChanges. | 1 |
ContentEntityBase:: |
public | function | ||
ContentEntityBase:: |
protected | function | ||
ContentEntityBase:: |
public | function |
Gets the loaded Revision ID of the entity. Overrides RevisionableInterface:: |
|
ContentEntityBase:: |
public | function |
Gets the revision identifier of the entity. Overrides RevisionableInterface:: |
|
ContentEntityBase:: |
public | function |
Gets an array of field item lists for translatable fields. Overrides FieldableEntityInterface:: |
|
ContentEntityBase:: |
protected | function | Gets a translated field. | |
ContentEntityBase:: |
public | function |
Gets a translation of the data. Overrides TranslatableInterface:: |
|
ContentEntityBase:: |
public | function |
Returns the languages the data is translated to. Overrides TranslatableInterface:: |
|
ContentEntityBase:: |
public | function |
Returns the translation status. Overrides TranslationStatusInterface:: |
|
ContentEntityBase:: |
public | function |
Returns the translatable object referring to the original language. Overrides TranslatableInterface:: |
|
ContentEntityBase:: |
public | function |
Determines whether the entity has a field with the given name. Overrides FieldableEntityInterface:: |
|
ContentEntityBase:: |
public | function |
Checks there is a translation for the given language code. Overrides TranslatableInterface:: |
|
ContentEntityBase:: |
public | function |
Determines if the current translation of the entity has unsaved changes. Overrides TranslatableInterface:: |
|
ContentEntityBase:: |
public | function |
Gets the identifier. Overrides EntityBase:: |
|
ContentEntityBase:: |
protected | function | Instantiates a translation object for an existing translation. | |
ContentEntityBase:: |
public | function |
Checks if this entity is the default revision. Overrides RevisionableInterface:: |
|
ContentEntityBase:: |
public | function |
Checks whether the translation is the default one. Overrides TranslatableInterface:: |
|
ContentEntityBase:: |
public | function |
Checks if untranslatable fields should affect only the default translation. Overrides TranslatableRevisionableInterface:: |
|
ContentEntityBase:: |
public | function |
Checks if this entity is the latest revision. Overrides RevisionableInterface:: |
|
ContentEntityBase:: |
public | function |
Checks whether this is the latest revision affecting this translation. Overrides TranslatableRevisionableInterface:: |
|
ContentEntityBase:: |
public | function |
Determines whether a new revision should be created on save. Overrides RevisionableInterface:: |
|
ContentEntityBase:: |
public | function |
Checks whether the translation is new. Overrides TranslatableInterface:: |
|
ContentEntityBase:: |
public | function |
Checks whether the current translation is affected by the current revision. Overrides TranslatableRevisionableInterface:: |
|
ContentEntityBase:: |
public | function |
Checks if the revision translation affected flag value has been enforced. Overrides TranslatableRevisionableInterface:: |
|
ContentEntityBase:: |
public | function |
Returns the translation support status. Overrides TranslatableInterface:: |
|
ContentEntityBase:: |
public | function |
Checks whether entity validation is required before saving the entity. Overrides FieldableEntityInterface:: |
|
ContentEntityBase:: |
public | function |
Gets the label of the entity. Overrides EntityBase:: |
2 |
ContentEntityBase:: |
public | function |
Gets the language of the entity. Overrides EntityBase:: |
|
ContentEntityBase:: |
public | function |
Reacts to changes to a field. Overrides FieldableEntityInterface:: |
|
ContentEntityBase:: |
public | function |
Acts on a created entity before hooks are invoked. Overrides EntityBase:: |
|
ContentEntityBase:: |
public | function |
Acts on a revision before it gets saved. Overrides RevisionableInterface:: |
2 |
ContentEntityBase:: |
public | function |
Gets a list of entities referenced by this entity. Overrides EntityBase:: |
1 |
ContentEntityBase:: |
public | function |
Removes the translation identified by the given language code. Overrides TranslatableInterface:: |
|
ContentEntityBase:: |
public | function |
Sets a field value. Overrides FieldableEntityInterface:: |
|
ContentEntityBase:: |
protected | function | Populates the local cache for the default language code. | |
ContentEntityBase:: |
public | function |
Enforces an entity to be saved as a new revision. Overrides RevisionableInterface:: |
|
ContentEntityBase:: |
public | function |
Marks the current revision translation as affected. Overrides TranslatableRevisionableInterface:: |
|
ContentEntityBase:: |
public | function |
Enforces the revision translation affected flag value. Overrides TranslatableRevisionableInterface:: |
|
ContentEntityBase:: |
public | function |
Sets whether entity validation is required before saving the entity. Overrides FieldableEntityInterface:: |
|
ContentEntityBase:: |
public | function |
Gets an array of all property values. Overrides EntityBase:: |
|
ContentEntityBase:: |
protected | function | Updates language for already instantiated fields. | |
ContentEntityBase:: |
public | function |
Updates the loaded Revision ID with the revision ID. Overrides RevisionableInterface:: |
|
ContentEntityBase:: |
public | function | Updates the original values with the interim changes. | |
ContentEntityBase:: |
public | function |
Gets the entity UUID (Universally Unique Identifier). Overrides EntityBase:: |
|
ContentEntityBase:: |
public | function |
Validates the currently set values. Overrides FieldableEntityInterface:: |
|
ContentEntityBase:: |
public | function |
Checks whether the entity object was a default revision when it was saved. Overrides RevisionableInterface:: |
|
ContentEntityBase:: |
public | function | Magic method: Implements a deep clone. | |
ContentEntityBase:: |
public | function |
Constructs an Entity object. Overrides EntityBase:: |
|
ContentEntityBase:: |
public | function | Implements the magic method for getting object properties. | |
ContentEntityBase:: |
public | function | Implements the magic method for isset(). | |
ContentEntityBase:: |
public | function | Implements the magic method for setting object properties. | |
ContentEntityBase:: |
public | function |
Overrides EntityBase:: |
|
ContentEntityBase:: |
public | function | Implements the magic method for unset(). | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | Aliased as: traitSleep | 1 |
DependencySerializationTrait:: |
public | function | 2 | |
EntityBase:: |
protected | property | Boolean indicating whether the entity should be forced to be new. | |
EntityBase:: |
protected | property | The entity type. | |
EntityBase:: |
protected | property | A typed data object wrapping this entity. | |
EntityBase:: |
public static | function |
Constructs a new entity object, without permanently saving it. Overrides EntityInterface:: |
|
EntityBase:: |
public | function |
Deletes an entity permanently. Overrides EntityInterface:: |
2 |
EntityBase:: |
public | function |
Enforces an entity to be new. Overrides EntityInterface:: |
|
EntityBase:: |
protected | function | Gets the entity manager. | |
EntityBase:: |
protected | function | Gets the entity type bundle info service. | |
EntityBase:: |
protected | function | Gets the entity type manager. | |
EntityBase:: |
public | function |
The cache contexts associated with this object. Overrides CacheableDependencyTrait:: |
|
EntityBase:: |
public | function |
The maximum age for which this object may be cached. Overrides CacheableDependencyTrait:: |
|
EntityBase:: |
public | function |
The cache tags associated with this object. Overrides CacheableDependencyTrait:: |
|
EntityBase:: |
public | function |
Returns the cache tags that should be used to invalidate caches. Overrides EntityInterface:: |
2 |
EntityBase:: |
public | function |
Gets the key that is used to store configuration dependencies. Overrides EntityInterface:: |
|
EntityBase:: |
public | function |
Gets the configuration dependency name. Overrides EntityInterface:: |
1 |
EntityBase:: |
public | function |
Gets the configuration target identifier for the entity. Overrides EntityInterface:: |
1 |
EntityBase:: |
public | function |
Gets the entity type definition. Overrides EntityInterface:: |
|
EntityBase:: |
public | function |
Gets the ID of the type of the entity. Overrides EntityInterface:: |
|
EntityBase:: |
protected | function | The list cache tags to invalidate for this entity. | |
EntityBase:: |
public | function |
Gets the original ID. Overrides EntityInterface:: |
1 |
EntityBase:: |
public | function |
Gets a typed data object for this entity object. Overrides EntityInterface:: |
|
EntityBase:: |
public | function |
Indicates if a link template exists for a given key. Overrides EntityInterface:: |
|
EntityBase:: |
protected static | function | Invalidates an entity's cache tags upon delete. | 1 |
EntityBase:: |
protected | function | Invalidates an entity's cache tags upon save. | 1 |
EntityBase:: |
public | function |
Determines whether the entity is new. Overrides EntityInterface:: |
2 |
EntityBase:: |
protected | function | Gets the language manager. | |
EntityBase:: |
public | function |
Deprecated way of generating a link to the entity. See toLink(). Overrides EntityInterface:: |
1 |
EntityBase:: |
protected | function | Gets an array link templates. | 1 |
EntityBase:: |
public static | function |
Loads an entity. Overrides EntityInterface:: |
|
EntityBase:: |
public static | function |
Loads one or more entities. Overrides EntityInterface:: |
|
EntityBase:: |
public static | function |
Acts on loaded entities. Overrides EntityInterface:: |
2 |
EntityBase:: |
public static | function |
Changes the values of an entity before it is created. Overrides EntityInterface:: |
5 |
EntityBase:: |
public static | function |
Acts on entities before they are deleted and before hooks are invoked. Overrides EntityInterface:: |
4 |
EntityBase:: |
public | function |
Saves an entity permanently. Overrides EntityInterface:: |
3 |
EntityBase:: |
public | function |
Sets the original ID. Overrides EntityInterface:: |
1 |
EntityBase:: |
public | function |
Generates the HTML for a link to this entity. Overrides EntityInterface:: |
|
EntityBase:: |
public | function |
Gets a list of URI relationships supported by this entity. Overrides EntityInterface:: |
|
EntityBase:: |
public | function |
Gets the public URL for this entity. Overrides EntityInterface:: |
2 |
EntityBase:: |
public | function |
Gets the URL object for the entity. Overrides EntityInterface:: |
1 |
EntityBase:: |
protected | function | Gets an array of placeholders for this entity. | 2 |
EntityBase:: |
protected | function | Gets the UUID generator. | |
EntityChangedTrait:: |
public | function | Gets the timestamp of the last entity change for the current translation. | |
EntityChangedTrait:: |
public | function | Returns the timestamp of the last entity change across all translations. | |
EntityChangedTrait:: |
public | function | Sets the timestamp of the last entity change for the current translation. | |
EntityChangesDetectionTrait:: |
protected | function | Returns an array of field names to skip when checking for changes. Aliased as: traitGetFieldsToSkipFromTranslationChangesCheck | |
Invoice:: |
public | function |
Adds an adjustment. Overrides EntityAdjustableInterface:: |
|
Invoice:: |
public | function |
Adds an invoice item. Overrides InvoiceInterface:: |
|
Invoice:: |
public static | function |
Provides base field definitions for an entity type. Overrides ContentEntityBase:: |
|
Invoice:: |
public | function |
Collects all adjustments that belong to the invoice. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Gets the adjustments. Overrides EntityAdjustableInterface:: |
|
Invoice:: |
public | function |
Gets the invoice balance. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Gets the billing profile. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Gets the invoice creation timestamp. Overrides InvoiceInterface:: |
|
Invoice:: |
public static | function | Default value callback for 'uid' base field definition. | |
Invoice:: |
public | function |
Gets the customer user. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Gets the customer user ID. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Gets an invoice data value with the given key. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Gets the invoice due date timestamp. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Gets the invoice email. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Gets the invoice file. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Gets the invoice date timestamp. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Gets the invoice number. Overrides InvoiceInterface:: |
|
Invoice:: |
protected | function | Gets the index of the given invoice item. | |
Invoice:: |
public | function |
Gets the invoice items. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Gets the invoice orders. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Gets the payment method. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Gets the invoice state. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Gets the store. Overrides EntityStoreInterface:: |
|
Invoice:: |
public | function |
Gets the store ID. Overrides EntityStoreInterface:: |
|
Invoice:: |
public | function |
Gets the invoice subtotal price. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Gets the total paid price. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Gets the invoice total price. Overrides InvoiceInterface:: |
|
Invoice:: |
public static | function | Gets the workflow ID for the state field. | |
Invoice:: |
public | function |
Checks whether the invoice has a given invoice item. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Gets whether the invoice has invoice items. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Gets whether the invoice has been fully paid. Overrides InvoiceInterface:: |
|
Invoice:: |
public static | function |
Acts on deleted entities before the delete hook is invoked. Overrides EntityBase:: |
|
Invoice:: |
public | function |
Acts on a saved entity before the insert or update hook is invoked. Overrides ContentEntityBase:: |
|
Invoice:: |
public | function |
Acts on an entity before the presave hook is invoked. Overrides ContentEntityBase:: |
|
Invoice:: |
public | function |
Recalculates the invoice total price. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Removes an adjustment. Overrides EntityAdjustableInterface:: |
|
Invoice:: |
public | function |
Removes an invoice item. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Sets the adjustments. Overrides EntityAdjustableInterface:: |
|
Invoice:: |
public | function |
Sets the billing profile. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Sets the invoice creation timestamp. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Sets the customer user. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Sets the customer user ID. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Sets an invoice data value with the given key. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Sets the invoice due date timestamp. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Sets the invoice email. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Sets the invoice file (i.e the reference to the generated PDF file). Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Sets the invoice date timestamp. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Sets the invoice number. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Sets the invoice items. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Sets the invoice orders. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Sets the payment method. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Sets the store. Overrides EntityStoreInterface:: |
|
Invoice:: |
public | function |
Sets the store ID. Overrides EntityStoreInterface:: |
|
Invoice:: |
public | function |
Sets the total paid price. Overrides InvoiceInterface:: |
|
Invoice:: |
public | function |
Gets the URL object for the entity. Overrides EntityBase:: |
|
Invoice:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | 1 | |
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
SynchronizableEntityTrait:: |
protected | property | Whether this entity is being created, updated or deleted through a synchronization process. | |
SynchronizableEntityTrait:: |
public | function | ||
SynchronizableEntityTrait:: |
public | function | ||
TranslationStatusInterface:: |
constant | Status code identifying a newly created translation. | ||
TranslationStatusInterface:: |
constant | Status code identifying an existing translation. | ||
TranslationStatusInterface:: |
constant | Status code identifying a removed translation. |