diff --git a/src/Entity/ShareTransaction.php b/src/Entity/ShareTransaction.php index 51df979aa537756fa1b627d8322082f98e2385a5..5ca62da3a06a659990bc4b67d602526ba588487d 100644 --- a/src/Entity/ShareTransaction.php +++ b/src/Entity/ShareTransaction.php @@ -10,6 +10,7 @@ use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\user\UserInterface; +use Drupal\shareholder_register\EntityEffectTransactionTrait; use Drupal\shareholder_register\Exception\ShareholderRegisterInconsistencyException; use Drupal\shareholder_register\Exception\ShareholderRegisterIntegrityException; use Drupal\shareholder_register\Exception\ShareholderRegisterMissingSharesException; @@ -72,6 +73,7 @@ class ShareTransaction extends ContentEntityBase implements ShareTransactionInte use EntityChangedTrait; use StringTranslationTrait; + use EntityEffectTransactionTrait; /** * {@inheritdoc} @@ -149,35 +151,6 @@ class ShareTransaction extends ContentEntityBase implements ShareTransactionInte } } - /** - * {@inheritdoc} - */ - public function getName() { - return $this->get('name')->value; - } - - /** - * {@inheritdoc} - */ - public function setName($name) { - $this->set('name', $name); - return $this; - } - - /** - * {@inheritdoc} - */ - public function getQuantity() { - return $this->get('quantity')->value; - } - - /** - * {@inheritdoc} - */ - public function setQuantity($quantity) { - return $this->set('quantity', $quantity); - } - /** * {@inheritdoc} */ @@ -217,46 +190,6 @@ class ShareTransaction extends ContentEntityBase implements ShareTransactionInte 2); } - /** - * {@inheritdoc} - */ - public function getDate() { - return $this->get('date')->value; - } - - /** - * {@inheritdoc} - */ - public function setDate($date) { - return $this->set('date', $date); - } - - /** - * {@inheritdoc} - */ - public function getPaymentDate() { - return $this->get('payment_date')->value; - } - - /** - * {@inheritdoc} - */ - public function setPaymentDate($date) { - return $this->set('payment_date', $date); - } - - /** - * {@inheritdoc} - */ - public function getTransactionDate($use_payment_date = FALSE) { - if ($use_payment_date && $this->get('payment_date')->value) { - return $this->get('payment_date')->value; - } - else { - return $this->get('date')->value; - } - } - /** * {@inheritdoc} */ @@ -271,20 +204,6 @@ class ShareTransaction extends ContentEntityBase implements ShareTransactionInte return $this->set('notes', $notes); } - /** - * {@inheritdoc} - */ - public function getState() { - return $this->get('state')->value; - } - - /** - * {@inheritdoc} - */ - public function setState($state) { - return $this->set('state', $state); - } - /** * {@inheritdoc} */ @@ -299,13 +218,6 @@ class ShareTransaction extends ContentEntityBase implements ShareTransactionInte return $this->get('share_issue_id')->entity; } - /** - * {@inheritdoc} - */ - public function getShareholder() { - return $this->get('shareholder_id')->entity; - } - /** * {@inheritdoc} */ @@ -411,6 +323,7 @@ class ShareTransaction extends ContentEntityBase implements ShareTransactionInte */ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields = parent::baseFieldDefinitions($entity_type); + $fields += static::effectTransactionBaseFieldDefinitions($entity_type); $fields['user_id'] = BaseFieldDefinition::create('entity_reference') ->setLabel(t('Authored by')) @@ -426,27 +339,6 @@ class ShareTransaction extends ContentEntityBase implements ShareTransactionInte ]) ->setDisplayConfigurable('view', TRUE); - $fields['name'] = BaseFieldDefinition::create('string') - ->setLabel(t('Transaction Number')) - ->setDescription(t('The Number of the Share transaction entity.')) - ->setSettings([ - 'max_length' => 50, - 'text_processing' => 0, - 'wkf-editable' => ['state' => []], - ]) - ->setDefaultValue('') - ->setDisplayOptions('view', [ - 'label' => 'above', - 'type' => 'string', - 'weight' => 1, - ]) - ->setDisplayOptions('form', [ - 'type' => 'string_textfield', - 'weight' => -4, - ]) - ->setDisplayConfigurable('form', TRUE) - ->setDisplayConfigurable('view', TRUE); - $fields['transaction_group'] = BaseFieldDefinition::create('entity_reference') ->setLabel(t('Transaction Group')) ->setDescription(t('The Share Transaction Group.')) @@ -473,31 +365,6 @@ class ShareTransaction extends ContentEntityBase implements ShareTransactionInte ->setDisplayConfigurable('form', TRUE) ->setDisplayConfigurable('view', TRUE); - $fields['shareholder_id'] = BaseFieldDefinition::create('entity_reference') - ->setLabel(t('Shareholder')) - ->setDescription(t('The Shareholder.')) - ->setSettings([ - 'target_type' => 'shareholder', - 'default_value' => 0, - 'wkf-editable' => ['state' => ['draft']], - ]) - ->setDisplayOptions('view', [ - 'label' => 'above', - 'weight' => 2, - ]) - ->setDisplayOptions('form', [ - 'type' => 'entity_reference_autocomplete', - 'weight' => 2, - 'settings' => [ - 'match_operator' => 'CONTAINS', - 'size' => '60', - 'autocomplete_type' => 'tags', - 'placeholder' => '', - ], - ]) - ->setDisplayConfigurable('form', TRUE) - ->setDisplayConfigurable('view', TRUE); - $fields['share_type_id'] = BaseFieldDefinition::create('entity_reference') ->setLabel(t('Share Type')) ->setDescription(t('The Share Type.')) @@ -561,71 +428,6 @@ class ShareTransaction extends ContentEntityBase implements ShareTransactionInte 'weight' => 15, ]); - $fields['date'] = BaseFieldDefinition::create('datetime') - ->setLabel(t('Transaction Date')) - ->setDescription(t('The Date of the Share transaction entity.')) - ->setSettings([ - 'datetime_type' => 'date', - 'wkf-editable' => ['state' => ['draft']], - ]) - ->setDefaultValue('') - ->setDisplayOptions('view', [ - 'label' => 'above', - 'type' => 'datetime_default', - 'settings' => [ - 'format_type' => 'date', - ], - 'weight' => 0, - ]) - ->setDisplayOptions('form', [ - 'type' => 'string_textfield', - 'weight' => 0, - ]) - ->setDisplayConfigurable('form', TRUE) - ->setDisplayConfigurable('view', TRUE); - - $fields['payment_date'] = BaseFieldDefinition::create('datetime') - ->setLabel(t('Date of Payment')) - ->setDescription(t('The Date the Share transaction was paid in full.')) - ->setSettings([ - 'datetime_type' => 'date', - 'wkf-editable' => ['state' => ['draft']], - ]) - ->setDefaultValue('') - ->setDisplayOptions('view', [ - 'label' => 'above', - 'type' => 'datetime_default', - 'settings' => [ - 'format_type' => 'date', - ], - 'weight' => 4, - ]) - ->setDisplayOptions('form', [ - 'type' => 'string_textfield', - 'weight' => 4, - ]) - ->setDisplayConfigurable('form', TRUE) - ->setDisplayConfigurable('view', TRUE); - - $fields['quantity'] = BaseFieldDefinition::create('integer') - ->setLabel(t('Quantity')) - ->setDescription(t('The Number of Shares in this transaction.')) - ->setSettings([ - 'wkf-editable' => ['state' => ['draft']], - ]) - ->setDefaultValue(1) - ->setDisplayOptions('view', [ - 'label' => 'above', - 'type' => 'string', - 'weight' => 5, - ]) - ->setDisplayOptions('form', [ - 'type' => 'string_textfield', - 'weight' => 5, - ]) - ->setDisplayConfigurable('form', TRUE) - ->setDisplayConfigurable('view', TRUE); - $fields['share_value'] = BaseFieldDefinition::create('decimal') ->setLabel(t('Share Value')) ->setDescription(t('The Value (nominal or fractional) per Share for this Transaction.')) @@ -703,12 +505,6 @@ class ShareTransaction extends ContentEntityBase implements ShareTransactionInte 'weight' => 10, ]); - $fields['state'] = BaseFieldDefinition::create('simple_workflow_state_field') - ->setLabel(t('State')) - ->setReadonly(TRUE) - ->setDescription(t('State of the Share Transaction.')) - ->setDefaultValue('draft'); - $fields['created'] = BaseFieldDefinition::create('created') ->setLabel(t('Created')) ->setDescription(t('The time that the entity was created.')); diff --git a/src/Entity/ShareTransactionInterface.php b/src/Entity/ShareTransactionInterface.php index befeb0208add07c62219eacdab9afeb2361af953..9a0a7052b476f77a562c972de1f7ac66146ee1c3 100644 --- a/src/Entity/ShareTransactionInterface.php +++ b/src/Entity/ShareTransactionInterface.php @@ -6,33 +6,14 @@ use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityChangedInterface; use Drupal\user\EntityOwnerInterface; +use Drupal\shareholder_register\EntityEffectTransactionInterface; + /** * Provides an interface for defining Share transaction entities. * * @ingroup shareholder_register */ -interface ShareTransactionInterface extends ContentEntityInterface, EntityChangedInterface, EntityOwnerInterface { - - // Add get/set methods for your configuration properties here. - - /** - * Gets the Share transaction name. - * - * @return string - * Name of the Share transaction. - */ - public function getName(); - - /** - * Sets the Share transaction name. - * - * @param string $name - * The Share transaction name. - * - * @return \Drupal\shareholder_register\Entity\ShareTransactionInterface - * The called Share transaction entity. - */ - public function setName($name); +interface ShareTransactionInterface extends ContentEntityInterface, EntityChangedInterface, EntityOwnerInterface, EntityEffectTransactionInterface { /** * Gets the Share transaction creation timestamp. diff --git a/src/EntityEffectTransactionInterface.php b/src/EntityEffectTransactionInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..8e7a552fec0f691666a0ec1085352f11b3b8b7c7 --- /dev/null +++ b/src/EntityEffectTransactionInterface.php @@ -0,0 +1,129 @@ +<?php + +namespace Drupal\shareholder_register; + +/** + * Defines a common interface for entities that are effect transactions. + */ +interface EntityEffectTransactionInterface { + + /** + * Gets the transaction name. + * + * @return string + * Name of the transaction. + */ + public function getName(); + + /** + * Sets the transaction name. + * + * @param string $name + * The transaction name. + * + * @return \Drupal\shareholder_register\EntityEffectTransactionInterface + * The called transaction entity. + */ + public function setName($name); + + /** + * Gets the shareholder. + * + * @return \Drupal\shareholder_register\Entity\ShareholderInterface|null + * The shareholder entity. + */ + public function getShareholder(); + + /** + * Gets the shareholder id. + * + * @return int|null + * The shareholder entity id. + */ + public function getShareholderId(); + + /** + * Gets the date. + * + * @return string|null + * The date. + */ + public function getDate(); + + /** + * Gets the date. + * + * @return \Drupal\Core\Datetime\DrupalDateTime|null + * The date. + */ + public function getDateObj(); + + /** + * Sets the date. + * + * @param \Drupal\Core\Datetime\DrupalDateTime|null $date + * The date. + */ + public function setDate($date); + + /** + * Gets the payment date. + * + * @return string|null + * The payment date. + */ + public function getPaymentDate(); + + /** + * Gets the payment date. + * + * @return \Drupal\Core\Datetime\DrupalDateTime|null + * The payment date. + */ + public function getPaymentDateObj(); + + /** + * Sets the payment date. + * + * @param \Drupal\Core\Datetime\DrupalDateTime|string|null $date + * The payment date. + */ + public function setPaymentDate($date); + + /** + * {@inheritdoc} + */ + public function getTransactionDate($use_payment_date = FALSE); + + /** + * Gets the quantity. + * + * @return string + * The quantity. + */ + public function getQuantity(); + + /** + * {@inheritdoc} + */ + public function setQuantity($quantity); + + /** + * Gets the transaction state. + * + * @return string + * State of the transaction. + */ + public function getState(); + + /** + * Sets the transaction state. + * + * @param string $state + * The bond transaction state. + * + * @return \Drupal\shareholder_register\EntityEffectTransactionInterface + * The called transaction entity. + */ + public function setState($state); +} diff --git a/src/EntityEffectTransactionTrait.php b/src/EntityEffectTransactionTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..42ea0abeef0721e1410441c8cca3246e82e9c578 --- /dev/null +++ b/src/EntityEffectTransactionTrait.php @@ -0,0 +1,261 @@ +<?php + +namespace Drupal\shareholder_register; + +use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException; +use Drupal\Core\Field\BaseFieldDefinition; +use Drupal\Core\StringTranslation\TranslatableMarkup; + +use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; + +/** + * Provides a trait for effect transaction entities. + */ +trait EntityEffectTransactionTrait { + + /** + * Returns an array of base field definitions for effect transactions. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type to add the owner field to. + * + * @return \Drupal\Core\Field\BaseFieldDefinition[] + * An array of base field definitions. + * + * @throws \Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException + * Thrown when the entity type does not implement EntityEffectTransactionInterface. + */ + public static function effectTransactionBaseFieldDefinitions(EntityTypeInterface $entity_type) { + if (!is_subclass_of($entity_type->getClass(), EntityEffectTransactionInterface::class)) { + throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type->id() . ' does not implement \Drupal\shareholder_register\EntityEffectTransactionInterface.'); + } + + return [ + 'name' => BaseFieldDefinition::create('string') + ->setLabel(t('Transaction Number')) + ->setReadonly(TRUE) + ->setDescription(t('The number of the transaction entity.')) + ->setSetting('max_length', 255) + ->setDisplayOptions('view', [ + 'label' => 'hidden', + 'type' => 'string', + 'weight' => -5, + ]) + ->setDisplayConfigurable('view', TRUE), + + 'shareholder_id' => BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Shareholder')) + ->setDescription(t('The Shareholder.')) + ->setSettings([ + 'target_type' => 'shareholder', + 'default_value' => 0, + 'wkf-disabled' => ['state' => ['valid']], + ]) + ->setDisplayOptions('view', [ + 'label' => 'above', + 'weight' => 2, + ]) + ->setDisplayOptions('form', [ + 'type' => 'entity_reference_autocomplete', + 'weight' => 2, + 'settings' => [ + 'match_operator' => 'CONTAINS', + 'size' => '60', + 'autocomplete_type' => 'tags', + 'placeholder' => '', + ], + ]) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayConfigurable('view', TRUE), + + 'date' => BaseFieldDefinition::create('datetime') + ->setLabel(t('Transaction Date')) + ->setDescription(t('The transaction date of the transaction entity.')) + ->setSettings([ + 'datetime_type' => 'date', + 'wkf-editable' => ['state' => ['draft']], + ]) + ->setDefaultValue('') + ->setDisplayOptions('view', [ + 'label' => 'above', + 'type' => 'datetime_default', + 'settings' => [ + 'format_type' => 'date', + ], + 'weight' => 0, + ]) + ->setDisplayOptions('form', [ + 'type' => 'string_textfield', + 'weight' => 0, + ]) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayConfigurable('view', TRUE), + + 'payment_date' => BaseFieldDefinition::create('datetime') + ->setLabel(t('Date of Payment')) + ->setDescription(t('The date the transaction was paid in full.')) + ->setSettings([ + 'datetime_type' => 'date', + 'wkf-editable' => ['state' => ['draft']], + ]) + ->setDefaultValue('') + ->setDisplayOptions('view', [ + 'label' => 'above', + 'type' => 'datetime_default', + 'settings' => [ + 'format_type' => 'date', + ], + 'weight' => 4, + ]) + ->setDisplayOptions('form', [ + 'type' => 'string_textfield', + 'weight' => 4, + ]) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayConfigurable('view', TRUE), + + 'quantity' => BaseFieldDefinition::create('integer') + ->setLabel(t('Quantity')) + ->setDescription(t('The number of effects in this transaction.')) + ->setSettings([ + 'wkf-editable' => ['state' => ['draft']], + ]) + ->setDefaultValue(1) + ->setDisplayOptions('view', [ + 'label' => 'above', + 'type' => 'string', + 'weight' => 5, + ]) + ->setDisplayOptions('form', [ + 'type' => 'string_textfield', + 'weight' => 5, + ]) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayConfigurable('view', TRUE), + + 'state' => BaseFieldDefinition::create('simple_workflow_state_field') + ->setLabel(t('State')) + ->setReadonly(TRUE) + ->setDescription(t('State of the Transaction.')) + ->setDefaultValue('draft'), + ]; + } + + /** + * {@inheritdoc} + */ + public function getName() { + return $this->get('name')->value; + } + + /** + * {@inheritdoc} + */ + public function setName($name) { + $this->set('name', $name); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getShareholder() { + return $this->get('shareholder_id')->entity; + } + + /** + * {@inheritdoc} + */ + public function getShareholderId() { + return $this->get('shareholder_id')->target_id; + } + + /** + * {@inheritdoc} + */ + public function getDate() { + return $this->get('date')->value; + } + + /** + * {@inheritdoc} + */ + public function getDateObj() { + return $this->get('date')->date; + } + + /** + * {@inheritdoc} + */ + public function setDate($date) { + $date_value = $date; + if ($date_value instanceof \DateTime) { + $date_value = $date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT); + } + return $this->set( + 'date', + $date_value); + } + + /** + * {@inheritdoc} + */ + public function getPaymentDate() { + return $this->get('payment_date')->value; + } + + /** + * {@inheritdoc} + */ + public function getPaymentDateObj() { + return $this->get('payment_date')->date; + } + + /** + * {@inheritdoc} + */ + public function setPaymentDate($date) { + return $this->set('payment_date', $date); + } + + /** + * {@inheritdoc} + */ + public function getTransactionDate($use_payment_date = FALSE) { + if ($use_payment_date && $this->get('payment_date')->value) { + return $this->get('payment_date')->value; + } + else { + return $this->get('date')->value; + } + } + + /** + * {@inheritdoc} + */ + public function getQuantity() { + return $this->get('quantity')->value; + } + + /** + * {@inheritdoc} + */ + public function setQuantity($quantity) { + return $this->set('quantity', $auantity); + } + + /** + * {@inheritdoc} + */ + public function getState() { + return $this->get('state')->value; + } + + /** + * {@inheritdoc} + */ + public function setState($state) { + return $this->set('state', $state); + } +}