You are here

class BalanceTransactor in Transaction 8

Transactor for accounting type transactions.

Plugin annotation


@Transactor(
  id = "transaction_balance",
  title = @Translation("Balance"),
  description = @Translation("Transactor for accounting type transactions."),
  transaction_fields = {
    {
      "name" = "amount",
      "type" = "decimal",
      "title" = @Translation("Amount"),
      "description" = @Translation("A numeric field with the amount of the transaction."),
      "required" = TRUE,
      "list" = TRUE,
    },
    {
      "name" = "balance",
      "type" = "decimal",
      "title" = @Translation("Balance"),
      "description" = @Translation("A numeric field to store the current balance."),
      "required" = TRUE,
      "list" = TRUE,
    },
    {
      "name" = "log_message",
      "type" = "string",
      "title" = @Translation("Log message"),
      "description" = @Translation("A log message with details about the transaction."),
      "required" = FALSE,
    },
  },
  target_entity_fields = {
    {
      "name" = "last_transaction",
      "type" = "entity_reference",
      "title" = @Translation("Last transaction"),
      "description" = @Translation("A reference field in the target entity type to update with a reference to the last executed transaction of this type."),
      "required" = FALSE,
    },
    {
      "name" = "target_balance",
      "type" = "decimal",
      "title" = @Translation("Balance"),
      "description" = @Translation("A numeric field to update with the current balance."),
      "required" = FALSE,
    },
  },
)

Hierarchy

Expanded class hierarchy of BalanceTransactor

File

src/Plugin/Transaction/BalanceTransactor.php, line 57

Namespace

Drupal\transaction\Plugin\Transaction
View source
class BalanceTransactor extends GenericTransactor {

  /**
   * {@inheritdoc}
   */
  public function executeTransaction(TransactionInterface $transaction, TransactionInterface $last_executed = NULL) {
    if (!parent::executeTransaction($transaction, $last_executed)) {
      return FALSE;
    }
    $settings = $transaction
      ->getType()
      ->getPluginSettings();

    // Current balance from the last executed transaction. The current
    // transaction balance will take as the initial balance.
    $balance = $last_executed ? $last_executed
      ->get($settings['balance'])->value : $transaction
      ->get($settings['balance'])->value;

    // Transaction amount.
    $amount = $transaction
      ->get($settings['amount'])->value;

    // Set result into transaction balance.
    $result = $balance + $amount;
    $transaction
      ->get($settings['balance'])
      ->setValue($result);

    // Reflect balance on the target entity.
    $target_entity = $transaction
      ->getTargetEntity();
    if (isset($settings['target_balance']) && $target_entity
      ->hasField($settings['target_balance'])) {
      $target_entity
        ->get($settings['target_balance'])
        ->setValue($result);

      // Set the property indicating that the target entity was updated on
      // execution.
      $transaction
        ->setProperty(TransactionInterface::PROPERTY_TARGET_ENTITY_UPDATED, TRUE);
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function getTransactionDescription(TransactionInterface $transaction, $langcode = NULL) {
    if ($transaction
      ->isNew()) {
      return parent::getTransactionDescription($transaction, $langcode);
    }
    $settings = $transaction
      ->getType()
      ->getPluginSettings();

    // Transaction amount.
    $amount = $transaction
      ->get($settings['amount'])->value;
    $t_options = $langcode ? [
      'langcode' => $langcode,
    ] : [];
    $t_args = [
      '@status' => $transaction
        ->isPending() ? $this
        ->t('(pending)') : '',
    ];
    if ($amount > 0) {
      $description = $this
        ->t('Credit transaction @status', $t_args, $t_options);
    }
    elseif ($amount < 0) {
      $description = $this
        ->t('Debit transaction @status', $t_args, $t_options);
    }
    else {
      $description = $this
        ->t('Zero amount transaction @status', $t_args, $t_options);
    }
    return $description;
  }

  /**
   * {@inheritdoc}
   */
  public function getExecutionIndications(TransactionInterface $transaction, $langcode = NULL) {
    $settings = $transaction
      ->getType()
      ->getPluginSettings();

    // Transaction amount.
    $amount = $transaction
      ->get($settings['amount'])->value;

    // @todo pretty print of amount according to default display settings
    $t_args = [
      '@amount' => $transaction
        ->get($settings['amount'])
        ->getString(),
    ];
    $t_options = $langcode ? [
      'langcode' => $langcode,
    ] : [];
    if ($amount > 0) {
      $indication = $this
        ->t('The current balance will increase by @amount.', $t_args, $t_options);
    }
    elseif ($amount < 0) {
      $indication = $this
        ->t('The current balance will decrease by @amount.', $t_args, $t_options);
    }
    else {
      $indication = $this
        ->t('The current balance will not be altered.', [], $t_options);
    }
    return $indication . ' ' . parent::getExecutionIndications($transaction, $langcode);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BalanceTransactor::executeTransaction public function Executes a transaction. Overrides GenericTransactor::executeTransaction
BalanceTransactor::getExecutionIndications public function Compose a messsage with execution indications for the given transaction. Overrides TransactorBase::getExecutionIndications
BalanceTransactor::getTransactionDescription public function Compose a human readable description for the given transaction. Overrides TransactorBase::getTransactionDescription
GenericTransactor::getTransactionDetails public function Compose human readable details for the given transaction. Overrides TransactorBase::getTransactionDetails
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TransactorBase::$currentUser protected property The current user.
TransactorBase::$fieldManager protected property The field manager.
TransactorBase::$fieldPrefix protected property Prefix for new field creation.
TransactorBase::$transactionService protected property The transaction service.
TransactorBase::$transactionStorage protected property The transaction entity storage.
TransactorBase::buildConfigurationForm public function Provides a form for this transactor plugin settings. Overrides PluginFormInterface::buildConfigurationForm
TransactorBase::buildTargetFieldsForm protected function Build configuration form fields to the target entity.
TransactorBase::buildTransactionFieldsForm protected function Build configuration form fields to the transaction.
TransactorBase::buildTransactionOptionsForm protected function Build transaction options configuration form.
TransactorBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
TransactorBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
TransactorBase::createFieldConfig protected function Creates a field config.
TransactorBase::createFieldStorage protected function Creates a new field.
TransactorBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration
TransactorBase::fieldExists public static function Machine name exists callback for "inline" field creation.
TransactorBase::fieldReferenceSettingsFormField protected function Builds a form field to reference a field.
TransactorBase::getAvailableFields protected function Search for fields of a given type in a given entity type.
TransactorBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
TransactorBase::getResultMessage public function Compose a message that describes the execution result of a transaction. Overrides TransactorPluginInterface::getResultMessage
TransactorBase::isApplicable public function Check if the transactor is applicable to a particular entity. Overrides TransactorPluginInterface::isApplicable
TransactorBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
TransactorBase::setFieldDisplay protected function Enable the display of a field.
TransactorBase::submitConfigurationForm public function Handles the settings form submit for this transactor plugin. Overrides PluginFormInterface::submitConfigurationForm
TransactorBase::validateConfigurationForm public function Handles the validation for the transactor plugin settings form. Overrides PluginFormInterface::validateConfigurationForm
TransactorBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
TransactorPluginInterface::RESULT_ERROR constant Generic result code for failed execution.
TransactorPluginInterface::RESULT_OK constant Generic result code for successful execution.