sample code
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
name: Daftar Core
|
||||
description: 'Defines core entities like account, portfolio and transaction (order, dividend and transfers).'
|
||||
type: module
|
||||
package: Daftar Content
|
||||
core_version_requirement: ^8.8 || ^9
|
||||
dependencies:
|
||||
- drupal:user
|
||||
- drupal:views
|
||||
- entity:entity
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\daftar_core;
|
||||
|
||||
use Drupal\Core\Entity\EntityChangedInterface;
|
||||
use Drupal\Core\Entity\ContentEntityInterface;
|
||||
use Drupal\daftar_core\Entity\Helper\EntityCreatedInterface;
|
||||
use Drupal\daftar_core\Entity\Helper\EntityKhatuInterface;
|
||||
use Drupal\daftar_core\Entity\Helper\EntityReferencedInterface;
|
||||
|
||||
/**
|
||||
* Provides an interface defining a chopda (portfolio) entity.
|
||||
*
|
||||
* @ingroup daftar
|
||||
*/
|
||||
interface ChopdaInterface extends
|
||||
ContentEntityInterface,
|
||||
EntityKhatuInterface,
|
||||
EntityCreatedInterface,
|
||||
EntityChangedInterface {
|
||||
|
||||
// no code
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\daftar_core;
|
||||
|
||||
use Drupal\Core\Entity\ContentEntityInterface;
|
||||
use Drupal\Core\Entity\EntityChangedInterface;
|
||||
use Drupal\Core\Entity\EntityChangedTrait;
|
||||
use Drupal\Core\Entity\ContentEntityBase;
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\Core\Field\BaseFieldDefinition;
|
||||
use Drupal\daftar_core\Entity\Helper\EntityCreatedInterface;
|
||||
use Drupal\daftar_core\Entity\Helper\EntityCreatedTrait;
|
||||
|
||||
/**
|
||||
* Implements base class for all daftar entities.
|
||||
*
|
||||
* @ingroup daftar
|
||||
*/
|
||||
class DaftarBase extends ContentEntityBase implements
|
||||
ContentEntityInterface,
|
||||
EntityCreatedInterface,
|
||||
EntityChangedInterface {
|
||||
|
||||
use EntityCreatedTrait;
|
||||
use EntityChangedTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Define the field properties here.
|
||||
*
|
||||
* Field name, type and size determine the table structure.
|
||||
*
|
||||
* In addition, we can define how the field and its content can be manipulated
|
||||
* in the GUI. The behaviour of the widgets used can be determined here.
|
||||
* @throws \Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException
|
||||
*/
|
||||
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
||||
$fields = parent::baseFieldDefinitions($entity_type);
|
||||
|
||||
if ($entity_type->hasKey('label')) {
|
||||
$fields[$entity_type->getKey('label')] = BaseFieldDefinition::create('string')
|
||||
->setLabel(t('Label'))
|
||||
->setSetting('max_length', 100)
|
||||
->setRequired(TRUE);
|
||||
}
|
||||
|
||||
if ($entity_type->hasKey('created')) {
|
||||
$fields[$entity_type->getKey('created')] = BaseFieldDefinition::create('created')
|
||||
->setLabel(t('Created'))
|
||||
->setDescription(t('The time that the entity was created.'))
|
||||
->setRequired(TRUE);
|
||||
}
|
||||
|
||||
if ($entity_type->hasKey('changed')) {
|
||||
$fields[$entity_type->getKey('changed')] = BaseFieldDefinition::create('changed')
|
||||
->setLabel(t('Changed'))
|
||||
->setDescription(t('The time that the entity was last edited.'))
|
||||
->setRequired(TRUE);
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\daftar_core;
|
||||
|
||||
use Drupal\Core\Entity\EntityChangedInterface;
|
||||
use Drupal\Core\Entity\ContentEntityInterface;
|
||||
use Drupal\daftar_core\Entity\Helper\EntityCreatedInterface;
|
||||
use Drupal\daftar_core\Entity\Helper\EntityKhatuInterface;
|
||||
|
||||
/**
|
||||
* Provides an interface defining a dividend entity.
|
||||
*
|
||||
* @ingroup daftar
|
||||
*/
|
||||
interface DividendInterface extends
|
||||
ContentEntityInterface,
|
||||
EntityKhatuInterface,
|
||||
EntityCreatedInterface,
|
||||
EntityChangedInterface {
|
||||
|
||||
public const DEFAULT_TYPE = 'cash';
|
||||
public const DEFAULT_TAX_RATE = 'qualified';
|
||||
|
||||
public const TYPES = [
|
||||
'cash' => 'Cash',
|
||||
];
|
||||
|
||||
public const TAX_RATES = [
|
||||
'qualified' => 'Qualified',
|
||||
'ordinary' => 'Ordinary',
|
||||
'other' => 'Other',
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\daftar_core\Entity;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\daftar_core\ChopdaInterface;
|
||||
use Drupal\daftar_core\DaftarBase;
|
||||
use Drupal\daftar_core\Entity\Helper\EntityKhatuTrait;
|
||||
|
||||
/**
|
||||
* Defines the Chopda (Portfolio) entity.
|
||||
*
|
||||
* @ContentEntityType(
|
||||
* id = "daftar_chopda",
|
||||
* label = @Translation("Portfolio"),
|
||||
* label_singular = @Translation("portfolio"),
|
||||
* label_plural = @Translation("portfolios"),
|
||||
* label_count = @PluralTranslation(
|
||||
* singular = "@count portfolio",
|
||||
* plural = "@count portfolios",
|
||||
* ),
|
||||
* base_table = "daftar_chopdao",
|
||||
* entity_keys = {
|
||||
* "id" = "cid",
|
||||
* "uuid" = "uuid",
|
||||
* "khatu" = "kid",
|
||||
* "label" = "label",
|
||||
* "created" = "created",
|
||||
* "changed" = "changed",
|
||||
* },
|
||||
* handlers = {
|
||||
* "views_data" = "Drupal\views\EntityViewsData"
|
||||
* }
|
||||
* )
|
||||
*
|
||||
* @ingroup daftar
|
||||
*/
|
||||
class Chopda extends DaftarBase implements ChopdaInterface {
|
||||
|
||||
use EntityKhatuTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Define the field properties here.
|
||||
*
|
||||
* Field name, type and size determine the table structure.
|
||||
*
|
||||
* In addition, we can define how the field and its content can be manipulated
|
||||
* in the GUI. The behaviour of the widgets used can be determined here.
|
||||
* @throws \Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException
|
||||
*/
|
||||
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
||||
$fields = parent::baseFieldDefinitions($entity_type);
|
||||
$fields += static::khatuBaseFieldDefinitions($entity_type);
|
||||
|
||||
$fields['cid']->setLabel(t('Portfolio ID'));
|
||||
$fields['uuid']->setLabel(t('Portfolio UUID'));
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function toArray() {
|
||||
return[
|
||||
'cid' => $this->id(),
|
||||
'uuid' => $this->uuid(),
|
||||
'kid' => $this->getKhatuId(),
|
||||
'label' => $this->label(),
|
||||
'created' => $this->getCreatedTime(),
|
||||
'changed' => $this->getChangedTime(),
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\daftar_core\Entity\Helper;
|
||||
|
||||
/**
|
||||
* Defines an interface for entity create timestamp tracking.
|
||||
*
|
||||
* @ingroup daftar
|
||||
*/
|
||||
interface EntityCreatedInterface {
|
||||
|
||||
/**
|
||||
* Returns the time that the entity was created.
|
||||
*
|
||||
* @return int
|
||||
* The timestamp of when the entity was created.
|
||||
*/
|
||||
public function getCreatedTime();
|
||||
|
||||
/**
|
||||
* Sets the creation date of the entity.
|
||||
*
|
||||
* @param int $timestamp
|
||||
* The timestamp of when the entity was created.
|
||||
*
|
||||
* @return $this
|
||||
* The class instance that this method is called on.
|
||||
*/
|
||||
public function setCreatedTime($timestamp);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\daftar_core\Entity\Helper;
|
||||
|
||||
/**
|
||||
* Provides a trait for accessing created time.
|
||||
*
|
||||
* @see \Drupal\daftar_core\Entity\Helper\EntityCreatedInterface
|
||||
* @ingroup daftar
|
||||
*/
|
||||
trait EntityCreatedTrait {
|
||||
|
||||
/**
|
||||
* Returns the time that the entity was created.
|
||||
*
|
||||
* @return int
|
||||
* The timestamp of when the entity was created.
|
||||
*/
|
||||
public function getCreatedTime() {
|
||||
return $this->get('created')->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the creation date of the entity.
|
||||
*
|
||||
* @param int $timestamp
|
||||
* The timestamp of when the entity was created.
|
||||
*
|
||||
* @return $this
|
||||
* The class instance that this method is called on.
|
||||
*/
|
||||
public function setCreatedTime($timestamp) {
|
||||
$this->set('created', $timestamp);
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\daftar_core\Entity\Helper;
|
||||
|
||||
use Drupal\daftar_core\KhatuInterface;
|
||||
|
||||
/**
|
||||
* Defines a common interface for entities that belong to a khatu (account).
|
||||
*
|
||||
* @ingroup daftar
|
||||
*/
|
||||
interface EntityKhatuInterface {
|
||||
|
||||
public const KEY = 'khatu';
|
||||
public const LABEL = 'Account ID';
|
||||
public const TARGET_ENTITY_TYPE = 'daftar_khatu';
|
||||
|
||||
/**
|
||||
* Returns the entity's khatu entity.
|
||||
*
|
||||
* @return \Drupal\daftar_core\KhatuInterface
|
||||
* The khatu entity.
|
||||
*/
|
||||
public function getKhatu();
|
||||
|
||||
/**
|
||||
* Sets the entity's khatu entity.
|
||||
*
|
||||
* @param \Drupal\daftar_core\KhatuInterface $khatu
|
||||
* The khatu entity.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setKhatu(KhatuInterface $khatu);
|
||||
|
||||
/**
|
||||
* Returns the entity's khatu ID.
|
||||
*
|
||||
* @return int|null
|
||||
* The khatu ID, or NULL in case the khatu ID field has not been set on
|
||||
* the entity.
|
||||
*/
|
||||
public function getKhatuId();
|
||||
|
||||
/**
|
||||
* Sets the entity's khatu entity ID.
|
||||
*
|
||||
* @param int $khatu_id
|
||||
* The khatu entity ID.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setKhatuId($khatu_id);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\daftar_core\Entity\Helper;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException;
|
||||
use Drupal\Core\Field\BaseFieldDefinition;
|
||||
use Drupal\daftar_core\KhatuInterface;
|
||||
|
||||
/**
|
||||
* Provides a trait for entities that belong to a khatu (account).
|
||||
*
|
||||
* @see \Drupal\daftar_core\Entity\Helper\EntityKhatuInterface
|
||||
* @ingroup daftar
|
||||
*/
|
||||
trait EntityKhatuTrait {
|
||||
|
||||
/**
|
||||
* Returns an array of base field definitions for entity khatas (accounts).
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
|
||||
* The entity type to add the reference 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 EntityKhatuInterface
|
||||
* or if it does not have a "khatu" entity key.
|
||||
*/
|
||||
public static function khatuBaseFieldDefinitions(EntityTypeInterface $entity_type) {
|
||||
if (!is_subclass_of($entity_type->getClass(), EntityKhatuInterface::class)) {
|
||||
throw new UnsupportedEntityTypeDefinitionException(
|
||||
sprintf('The entity type %s does not implement \Drupal\daftar\Entity\Helper\EntityReferencedInterface.', $entity_type->id())
|
||||
);
|
||||
}
|
||||
if (!$entity_type->hasKey(EntityKhatuInterface::KEY)) {
|
||||
throw new UnsupportedEntityTypeDefinitionException(
|
||||
sprintf('The entity type %s does not have a "%s" entity key.', $entity_type->id(), EntityKhatuInterface::KEY)
|
||||
);
|
||||
}
|
||||
|
||||
return [
|
||||
$entity_type->getKey(EntityKhatuInterface::KEY) => BaseFieldDefinition::create('entity_reference')
|
||||
->setLabel(t(EntityKhatuInterface::LABEL))
|
||||
->setDescription(t('The {daftar_khatao}.kid of the account.'))
|
||||
->setSetting('target_type', EntityKhatuInterface::TARGET_ENTITY_TYPE)
|
||||
->setRequired(TRUE),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getKhatu() {
|
||||
$key = $this->getEntityType()->getKey(EntityKhatuInterface::KEY);
|
||||
return $this->get($key)->entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setKhatu(KhatuInterface $entity) {
|
||||
$key = $this->getEntityType()->getKey(EntityKhatuInterface::KEY);
|
||||
$this->set($key, $entity);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getKhatuId() {
|
||||
return $this->getEntityKey(EntityKhatuInterface::KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setKhatuId($khatu_id) {
|
||||
$key = $this->getEntityType()->getKey(EntityKhatuInterface::KEY);
|
||||
$this->set($key, $khatu_id);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\daftar_core\Entity\Helper;
|
||||
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
|
||||
/**
|
||||
* Defines an interface for entity create timestamp tracking.
|
||||
*
|
||||
* @ingroup daftar
|
||||
*/
|
||||
interface EntityReferencedInterface {
|
||||
|
||||
|
||||
/**
|
||||
* Returns the entity being referenced.
|
||||
*
|
||||
* @return \Drupal\Core\Entity\EntityInterface|null
|
||||
* The referenced entity or NULL if nothing is referenced.
|
||||
*/
|
||||
public function getReference();
|
||||
|
||||
/**
|
||||
* Sets the entity being referenced.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityInterface $entity
|
||||
* The referenced entity.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setReference(EntityInterface $entity);
|
||||
|
||||
/**
|
||||
* Returns the referenced entity's ID.
|
||||
*
|
||||
* @return int|null
|
||||
* The referenced entity ID, or NULL in case the reference field has not
|
||||
* been set on the entity.
|
||||
*/
|
||||
public function getReferenceId();
|
||||
|
||||
/**
|
||||
* Sets the referenced entity ID.
|
||||
*
|
||||
* @param int $entity_id
|
||||
* The referenced entity ID.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setReferenceId($entity_id);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\daftar_core\Entity\Helper;
|
||||
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException;
|
||||
use Drupal\Core\Field\BaseFieldDefinition;
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
|
||||
/**
|
||||
* Provides a trait for accessing referenced entity.
|
||||
*
|
||||
* @see \Drupal\daftar_core\Entity\Helper\EntityReferencedInterface
|
||||
* @ingroup daftar
|
||||
*/
|
||||
trait EntityReferencedTrait {
|
||||
|
||||
/**
|
||||
* Returns an array of base field definitions for entity references.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
|
||||
* The entity type to add the reference field to.
|
||||
* @param string $target_type
|
||||
* The entity type ID of the referenced entity.
|
||||
*
|
||||
* @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 EntityReferencedInterface
|
||||
* or if it does not have a "reference" entity key.
|
||||
*/
|
||||
public static function referenceBaseFieldDefinitions(EntityTypeInterface $entity_type, string $target_type) {
|
||||
if (!is_subclass_of($entity_type->getClass(), EntityReferencedInterface::class)) {
|
||||
throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type->id() . ' does not implement \Drupal\daftar\Entity\Helper\EntityReferencedInterface.');
|
||||
}
|
||||
if (!$entity_type->hasKey('reference')) {
|
||||
throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type->id() . ' does not have a "reference" entity key.');
|
||||
}
|
||||
|
||||
return [
|
||||
$entity_type->getKey('reference') => BaseFieldDefinition::create('entity_reference')
|
||||
->setLabel(new TranslatableMarkup('Reference ID'))
|
||||
->setSetting('target_type', $target_type)
|
||||
->setRequired(TRUE),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getReference() {
|
||||
$key = $this->getEntityType()->getKey('reference');
|
||||
return $this->get($key)->entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setReference(EntityInterface $entity) {
|
||||
$key = $this->getEntityType()->getKey('reference');
|
||||
$this->set($key, $entity);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getReferenceId() {
|
||||
return $this->getEntityKey('reference');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setReferenceId($entity_id) {
|
||||
$key = $this->getEntityType()->getKey('reference');
|
||||
$this->set($key, $entity_id);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\daftar_core\Entity;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\daftar_core\DaftarBase;
|
||||
use Drupal\daftar_core\KhatuInterface;
|
||||
use Drupal\user\EntityOwnerTrait;
|
||||
|
||||
/**
|
||||
* Defines the Khatu (Account) entity.
|
||||
*
|
||||
* @ContentEntityType(
|
||||
* id = "daftar_khatu",
|
||||
* label = @Translation("Account"),
|
||||
* label_singular = @Translation("account"),
|
||||
* label_plural = @Translation("accounts"),
|
||||
* label_count = @PluralTranslation(
|
||||
* singular = "@count account",
|
||||
* plural = "@count accounts",
|
||||
* ),
|
||||
* bundle_label = @Translation("Account Type"),*
|
||||
* base_table = "daftar_khatao",
|
||||
* entity_keys = {
|
||||
* "id" = "kid",
|
||||
* "uuid" = "uuid",
|
||||
* "bundle" = "type",
|
||||
* "owner" = "uid",
|
||||
* "label" = "label",
|
||||
* "created" = "created",
|
||||
* "changed" = "changed",
|
||||
* },
|
||||
* handlers = {
|
||||
* "views_data" = "Drupal\views\EntityViewsData"
|
||||
* }
|
||||
* )
|
||||
*
|
||||
* @ingroup daftar
|
||||
*/
|
||||
class Khatu extends DaftarBase implements KhatuInterface {
|
||||
|
||||
use EntityOwnerTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Define the field properties here.
|
||||
*
|
||||
* Field name, type and size determine the table structure.
|
||||
*
|
||||
* In addition, we can define how the field and its content can be manipulated
|
||||
* in the GUI. The behaviour of the widgets used can be determined here.
|
||||
* @throws \Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException
|
||||
*/
|
||||
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
||||
$fields = parent::baseFieldDefinitions($entity_type);
|
||||
$fields += static::ownerBaseFieldDefinitions($entity_type);
|
||||
|
||||
$fields['kid']->setLabel(t('Account ID'));
|
||||
$fields['uuid']->setLabel(t('Account UUID'));
|
||||
$fields['type']->setLabel(t('Account Type'));
|
||||
|
||||
$fields['uid']
|
||||
->setDescription(t('The {user}.uid of the account owner.'));
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isBank() {
|
||||
return $this->bundle() === 'bank';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isBrokerage() {
|
||||
return $this->bundle() === 'brokerage';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function toArray() {
|
||||
return[
|
||||
'kid' => $this->id(),
|
||||
'uuid' => $this->uuid(),
|
||||
'type' => $this->bundle(),
|
||||
'uid' => $this->getOwnerId(),
|
||||
'label' => $this->label(),
|
||||
'created' => $this->getCreatedTime(),
|
||||
'changed' => $this->getChangedTime(),
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\daftar_Core\Entity\Sodo;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\Core\Field\BaseFieldDefinition;
|
||||
use Drupal\daftar_core\DividendInterface;
|
||||
|
||||
/**
|
||||
* Defines the Dividend Transaction entity.
|
||||
*
|
||||
* @ContentEntityType(
|
||||
* id = "daftar_dividend",
|
||||
* label = @Translation("Dividend"),
|
||||
* label_singular = @Translation("dividend"),
|
||||
* label_plural = @Translation("dividends"),
|
||||
* label_count = @PluralTranslation(
|
||||
* singular = "@count dividend",
|
||||
* plural = "@count dividends",
|
||||
* ),
|
||||
*
|
||||
* base_table = "daftar_dividends",
|
||||
*
|
||||
* entity_keys = {
|
||||
* "id" = "sid",
|
||||
* "uuid" = "uuid",
|
||||
* "khatu" = "kid",
|
||||
* "bundle" = "type",
|
||||
* "created" = "created",
|
||||
* "changed" = "changed",
|
||||
* },
|
||||
* )
|
||||
*
|
||||
* @ingroup daftar
|
||||
*/
|
||||
class Dividend extends SodoEntityBase implements DividendInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Define the field properties here.
|
||||
*
|
||||
* Field name, type and size determine the table structure.
|
||||
*
|
||||
* In addition, we can define how the field and its content can be manipulated
|
||||
* in the GUI. The behaviour of the widgets used can be determined here.
|
||||
*/
|
||||
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
||||
|
||||
$fields = parent::baseFieldDefinitions($entity_type);
|
||||
|
||||
// Override parent label and description.
|
||||
$fields['date']->setLabel(t('Date Paid'));
|
||||
$fields['date']->setDescription(t('Date when this dividend was paid.'));
|
||||
|
||||
$fields['ex_date'] = BaseFieldDefinition::create('datetime')
|
||||
->setLabel(t('Execution Date'))
|
||||
->setDescription(t('Date when this dividend was executed.'))
|
||||
->setSetting('datetime_type', 'date');
|
||||
|
||||
$fields['symbol'] = BaseFieldDefinition::create('string')
|
||||
->setLabel(t('Symbol'))
|
||||
->setSetting('max_length', 15)
|
||||
->setRequired(TRUE);
|
||||
|
||||
$fields['quantity'] = BaseFieldDefinition::create('decimal')
|
||||
->setLabel(t('Number of shares'))
|
||||
->setDescription(t('Number of shares on which the dividend was paid out.'))
|
||||
->setSetting('precision', 12)
|
||||
->setSetting('scale', 4)
|
||||
->setRequired(TRUE);
|
||||
|
||||
$fields['tax_rate'] = BaseFieldDefinition::create('string')
|
||||
->setLabel(t('Tax Rate Classification'))
|
||||
->setSetting('max_length', 15)
|
||||
->setDescription(t('Tax rate classification of the dividend (e.g. qualified and ordinary).'));
|
||||
|
||||
$fields['auto_created'] = BaseFieldDefinition::create('boolean')
|
||||
->setLabel(t('Auto Created'))
|
||||
->setDescription(t('A boolean flag indicating whether the dividend was automatically added.'))
|
||||
->setDefaultValue(FALSE);
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\daftar_core\Entity\Sodo;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\Core\Field\BaseFieldDefinition;
|
||||
use Drupal\daftar_core\OrderInterface;
|
||||
|
||||
/**
|
||||
* Defines the Order Transaction entity.
|
||||
*
|
||||
* @ContentEntityType(
|
||||
* id = "daftar_order",
|
||||
* label = @Translation("Order"),
|
||||
* label_singular = @Translation("order"),
|
||||
* label_plural = @Translation("orders"),
|
||||
* label_count = @PluralTranslation(
|
||||
* singular = "@count order",
|
||||
* plural = "@count orders",
|
||||
* ),
|
||||
*
|
||||
* base_table = "daftar_orders",
|
||||
*
|
||||
* entity_keys = {
|
||||
* "id" = "sid",
|
||||
* "uuid" = "uuid",
|
||||
* "khatu" = "kid",
|
||||
* "bundle" = "type",
|
||||
* "created" = "created",
|
||||
* "changed" = "changed",
|
||||
* },
|
||||
* )
|
||||
*
|
||||
* @ingroup daftar
|
||||
*/
|
||||
class Order extends SodoEntityBase implements OrderInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Define the field properties here.
|
||||
*
|
||||
* Field name, type and size determine the table structure.
|
||||
*
|
||||
* In addition, we can define how the field and its content can be manipulated
|
||||
* in the GUI. The behaviour of the widgets used can be determined here.
|
||||
*/
|
||||
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
||||
|
||||
$fields = parent::baseFieldDefinitions($entity_type);
|
||||
|
||||
$fields['action'] = BaseFieldDefinition::create('string')
|
||||
->setLabel($entity_type->getBundleLabel())
|
||||
->setRequired(TRUE)
|
||||
->setReadOnly(TRUE);
|
||||
|
||||
$fields['symbol'] = BaseFieldDefinition::create('string')
|
||||
->setLabel(t('Symbol'))
|
||||
->setSetting('max_length', 15)
|
||||
->setRequired(TRUE);
|
||||
|
||||
$fields['quantity'] = BaseFieldDefinition::create('decimal')
|
||||
->setLabel(t('Quantity'))
|
||||
->setDescription(t('Number of shares exchanged.'))
|
||||
->setSetting('precision', 10)
|
||||
->setSetting('scale', 4)
|
||||
->setRequired(TRUE);
|
||||
|
||||
$fields['on_hand'] = BaseFieldDefinition::create('decimal')
|
||||
->setLabel(t('Quantity On Hand'))
|
||||
->setDescription(t('Number of shares currently on hand.'))
|
||||
->setSetting('precision', 10)
|
||||
->setSetting('scale', 4);
|
||||
|
||||
$fields['cps'] = BaseFieldDefinition::create('decimal')
|
||||
->setLabel(t('Cost per Share'))
|
||||
->setDescription(t('Price paid per share (computed).'))
|
||||
->setSetting('precision', 10)
|
||||
->setSetting('scale', 4)
|
||||
->setComputed(TRUE);
|
||||
|
||||
$fields['commission'] = BaseFieldDefinition::create('decimal')
|
||||
->setLabel(t('Commission'))
|
||||
->setDescription(t('Commission charged by the brokerage.'))
|
||||
->setSetting('precision', 10)
|
||||
->setSetting('scale', 4)
|
||||
->setDefaultValue(0);
|
||||
|
||||
$fields['fees'] = BaseFieldDefinition::create('decimal')
|
||||
->setLabel(t('Fees'))
|
||||
->setDescription(t('Fees charged for the transaction.'))
|
||||
->setSetting('precision', 10)
|
||||
->setSetting('scale', 4)
|
||||
->setDefaultValue(0);
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\daftar_core\Entity\Sodo;
|
||||
|
||||
use Drupal\Core\Entity\ContentEntityInterface;
|
||||
use Drupal\Core\Entity\EntityChangedInterface;
|
||||
use Drupal\Core\Entity\EntityChangedTrait;
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\Core\Field\BaseFieldDefinition;
|
||||
use Drupal\daftar_core\DaftarBase;
|
||||
use Drupal\daftar_core\Entity\Helper\EntityCreatedInterface;
|
||||
use Drupal\daftar_core\Entity\Helper\EntityCreatedTrait;
|
||||
use Drupal\daftar_core\Entity\Helper\EntityKhatuInterface;
|
||||
use Drupal\daftar_core\Entity\Helper\EntityKhatuTrait;
|
||||
|
||||
/**
|
||||
* Implements base class for sodo (transaction) entity types.
|
||||
*
|
||||
* @ingroup daftar
|
||||
*/
|
||||
abstract class SodoEntityBase extends DaftarBase implements
|
||||
ContentEntityInterface,
|
||||
EntityKhatuInterface,
|
||||
EntityCreatedInterface,
|
||||
EntityChangedInterface {
|
||||
|
||||
use EntityKhatuTrait;
|
||||
use EntityCreatedTrait;
|
||||
use EntityChangedTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Define the field properties here.
|
||||
*
|
||||
* Field name, type and size determine the table structure.
|
||||
*
|
||||
* In addition, we can define how the field and its content can be manipulated
|
||||
* in the GUI. The behaviour of the widgets used can be determined here.
|
||||
* @throws \Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException
|
||||
*/
|
||||
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
||||
$fields = parent::baseFieldDefinitions($entity_type);
|
||||
$fields += static::khatuBaseFieldDefinitions($entity_type);
|
||||
|
||||
// Fields attached to all transactions.
|
||||
|
||||
$fields['date'] = BaseFieldDefinition::create('datetime')
|
||||
->setLabel(t('Transaction Date'))
|
||||
->setDescription(t('Date when this transaction took place.'))
|
||||
->setSetting('datetime_type', 'date')
|
||||
->setRequired(TRUE);
|
||||
|
||||
$fields['amount'] = BaseFieldDefinition::create('decimal')
|
||||
->setLabel(t('Amount'))
|
||||
->setDescription(t('Total transaction amount.'))
|
||||
->setSetting('precision', 12)
|
||||
->setSetting('scale', 4)
|
||||
->setRequired(TRUE);
|
||||
|
||||
$fields['notes'] = BaseFieldDefinition::create('string')
|
||||
->setLabel(t('Notes'))
|
||||
->setDescription(t('Short descriptive text for future references.'))
|
||||
->setSetting('max_length', 255);
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\daftar_core\Entity\Sodo;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\Core\Field\BaseFieldDefinition;
|
||||
use Drupal\daftar_core\TransferInterface;
|
||||
|
||||
/**
|
||||
* Defines the Transfer Transaction entity.
|
||||
*
|
||||
* @ContentEntityType(
|
||||
* id = "daftar_transfer",
|
||||
* label = @Translation("Transfer"),
|
||||
* label_singular = @Translation("transfer"),
|
||||
* label_plural = @Translation("transfers"),
|
||||
* label_count = @PluralTranslation(
|
||||
* singular = "@count transfer",
|
||||
* plural = "@count transfers",
|
||||
* ),
|
||||
*
|
||||
* base_table = "daftar_transfers",
|
||||
|
||||
* entity_keys = {
|
||||
* "id" = "id",
|
||||
* "uuid" = "uuid",
|
||||
* "khatu" = "kid",
|
||||
* "bundle" = "type",
|
||||
* "created" = "created",
|
||||
* "changed" = "changed",
|
||||
* },
|
||||
* )
|
||||
*
|
||||
* @ingroup daftar
|
||||
*/
|
||||
class Transfer extends SodoEntityBase implements TransferInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Define the field properties here.
|
||||
*
|
||||
* Field name, type and size determine the table structure.
|
||||
*
|
||||
* In addition, we can define how the field and its content can be manipulated
|
||||
* in the GUI. The behaviour of the widgets used can be determined here.
|
||||
*/
|
||||
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
||||
|
||||
$fields = parent::baseFieldDefinitions($entity_type);
|
||||
|
||||
// Override parent label and description.
|
||||
$fields['kid']->setDescription(t('The account from/to which the amount was transferred.'));
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\daftar_core;
|
||||
|
||||
use Drupal\Core\Entity\EntityChangedInterface;
|
||||
use Drupal\Core\Entity\ContentEntityInterface;
|
||||
use Drupal\daftar_core\Entity\Helper\EntityCreatedInterface;
|
||||
use Drupal\user\EntityOwnerInterface;
|
||||
|
||||
|
||||
/**
|
||||
* Provides an interface defining a khatu (account) entity.
|
||||
*
|
||||
* @ingroup daftar
|
||||
*/
|
||||
interface KhatuInterface extends
|
||||
ContentEntityInterface,
|
||||
EntityOwnerInterface,
|
||||
EntityCreatedInterface,
|
||||
EntityChangedInterface {
|
||||
|
||||
/**
|
||||
* Khatu (Account) entity types.
|
||||
* Key/value pairs of machine names and labels.
|
||||
*/
|
||||
const TYPES = [
|
||||
'bank' => 'Bank',
|
||||
'brokerage' => 'Brokerage',
|
||||
'ira_tradition' => 'Traditional IRA',
|
||||
'ira_rollover' => 'Rollover IRA',
|
||||
'ira_roth' => 'Roth IRA',
|
||||
];
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* Returns TRUE if this entity is of bank type.
|
||||
* FALSE otherwise.
|
||||
*/
|
||||
public function isBank();
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* Returns TRUE if this entity is of brokerage type.
|
||||
* FALSE otherwise.
|
||||
*/
|
||||
public function isBrokerage();
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\daftar_core;
|
||||
|
||||
use Drupal\Core\Entity\EntityChangedInterface;
|
||||
use Drupal\Core\Entity\ContentEntityInterface;
|
||||
use Drupal\daftar_core\Entity\Helper\EntityCreatedInterface;
|
||||
use Drupal\daftar_core\Entity\Helper\EntityKhatuInterface;
|
||||
|
||||
/**
|
||||
* Provides an interface defining an order entity.
|
||||
*
|
||||
* @ingroup daftar
|
||||
*/
|
||||
interface OrderInterface extends
|
||||
ContentEntityInterface,
|
||||
EntityKhatuInterface,
|
||||
EntityCreatedInterface,
|
||||
EntityChangedInterface {
|
||||
|
||||
public const DEFAULT_TYPE = 'mkt';
|
||||
|
||||
public const TYPES = [
|
||||
'mkt' => 'Market',
|
||||
'lmt' => 'Limit',
|
||||
'stp' => 'Stop',
|
||||
'stplmt' => 'Stop Limit',
|
||||
];
|
||||
|
||||
public const ACTIONS = [
|
||||
'buy' => 'Buy',
|
||||
'sell' => 'Sell',
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\daftar_core;
|
||||
|
||||
use Drupal\Core\Entity\EntityChangedInterface;
|
||||
use Drupal\Core\Entity\ContentEntityInterface;
|
||||
use Drupal\daftar_core\Entity\Helper\EntityCreatedInterface;
|
||||
use Drupal\daftar_core\Entity\Helper\EntityKhatuInterface;
|
||||
|
||||
/**
|
||||
* Provides an interface defining a transfer entity.
|
||||
*
|
||||
* @ingroup daftar
|
||||
*/
|
||||
interface TransferInterface extends
|
||||
ContentEntityInterface,
|
||||
EntityKhatuInterface,
|
||||
EntityCreatedInterface,
|
||||
EntityChangedInterface {
|
||||
|
||||
// no code
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user