diff --git a/src/Plugin/views/field/AccountingAccountViewAccountBalance.php b/src/Plugin/views/field/AccountingAccountViewAccountBalance.php index df1ab657b76c028274012d5a58041157b0ee7f13..4bd07315c243ffe41b118914660a1b86239b2923 100644 --- a/src/Plugin/views/field/AccountingAccountViewAccountBalance.php +++ b/src/Plugin/views/field/AccountingAccountViewAccountBalance.php @@ -3,7 +3,7 @@ namespace Drupal\accounting\Plugin\views\field; use Drupal\views\ResultRow; -use Drupal\views\Plugin\views\field\FieldPluginBase; +use Drupal\views\Plugin\views\field\NumericField; /** * A handler to provide display for the balance. @@ -12,49 +12,35 @@ use Drupal\views\Plugin\views\field\FieldPluginBase; * * @ViewsField("accounting_account_view_account_balance") */ -class AccountingAccountViewAccountBalance extends FieldPluginBase { +class AccountingAccountViewAccountBalance extends NumericField { /** * {@inheritdoc} */ - public function render(ResultRow $values) { - $empty = [ - '#markup' => '', - '#cache' => [ - 'tags' => \Drupal::entityTypeManager()->getDefinition('accounting_account')->getListCacheTags(), - ], + public function query() { + $this->ensureMyTable(); + // Add a subquery to the query that will find the download count. + $subQuery = \Drupal::database()->select('journal_entry_line', 'journal_entry_line'); + $subQuery->addField('journal_entry_line', 'accounting_account_id'); + $subQuery->addExpression("SUM(debit - credit)", 'balance'); + $subQuery->condition('state', 'valid'); + $subQuery->groupBy("journal_entry_line.accounting_account_id"); + + // Add the subquery to as a component of a join. + $joinDefinition = [ + 'table formula' => $subQuery, + 'field' => 'accounting_account_id', + 'left_table' => 'accounting_account', + 'left_field' => 'id', + 'adjust' => TRUE, ]; - if (!$this->getEntity($values)) { - return $empty; - } - - if (get_class($this->getEntity($values)) == 'Drupal\accounting\Entity\AccountingAccount') { - return [ - '#markup' => $this->getEntity($values)->getBalance()['balance_sign'], - '#cache' => [ - 'tags' => \Drupal::entityTypeManager()->getDefinition('journal_entry_line')->getListCacheTags(), - ], - ]; - } - elseif (get_class($this->getEntity($values)) == 'Drupal\accounting\Entity\JournalEntry') { - return [ - '#markup' => $this->getEntity($values)->getBalance()['balance_sign'], - '#cache' => [ - 'tags' => \Drupal::entityTypeManager()->getDefinition('journal_entry_line')->getListCacheTags(), - ], - ]; - } - - return $empty; - } + // Create a join object and create a relationship between the main query and the subquery. + $join = \Drupal::service('plugin.manager.views.join')->createInstance('standard', $joinDefinition); + $this->query->addRelationship('journal_entry_line', $join, 'accounting_account'); - /** - * {@inheritdoc} - */ - public function query() { - // This function exists to override parent query function. - // Do nothing. + // Add the field to the Views interface. + $this->field_alias = $this->query->addField(NULL, 'COALESCE(balance, 0)', 'balance'); } } diff --git a/src/Plugin/views/field/AccountingAccountViewUserBalance.php b/src/Plugin/views/field/AccountingAccountViewUserBalance.php index 7a23ca5815f168dd9ef89305a0f97fd22ae0c837..0f784fe64e3d3cf0c302ea702a84438d80e13c73 100644 --- a/src/Plugin/views/field/AccountingAccountViewUserBalance.php +++ b/src/Plugin/views/field/AccountingAccountViewUserBalance.php @@ -5,7 +5,7 @@ namespace Drupal\accounting\Plugin\views\field; use Drupal\Core\Form\FormStateInterface; use Drupal\views\ResultRow; -use Drupal\views\Plugin\views\field\FieldPluginBase; +use Drupal\views\Plugin\views\field\NumericField; /** * A handler to provide display for the account balance. @@ -14,35 +14,7 @@ use Drupal\views\Plugin\views\field\FieldPluginBase; * * @ViewsField("accounting_account_view_user_balance") */ -class AccountingAccountViewUserBalance extends FieldPluginBase { - - /** - * {@inheritdoc} - */ - public function render(ResultRow $values) { - if (get_class($this->getEntity($values)) == 'Drupal\user\Entity\User') { - $user = $this->getEntity($values); - $total = \Drupal::service('accounting.accounts')->getUserAccountTypeBalance( - $user, - $this->options['account_bundle'] - ); - return [ - '#markup' => $total, - '#cache' => [ - 'tags' => \Drupal::entityTypeManager()->getDefinition('journal_entry_line')->getListCacheTags(), - ], - ]; - } - return ''; - } - - /** - * {@inheritdoc} - */ - public function query() { - // This function exists to override parent query function. - // Do nothing. - } +class AccountingAccountViewUserBalance extends NumericField { /** * {@inheritdoc} @@ -67,4 +39,35 @@ class AccountingAccountViewUserBalance extends FieldPluginBase { ]; } + /** + * {@inheritdoc} + */ + public function query() { + $this->ensureMyTable(); + // Add a subquery to the query that will find the download count. + $subQuery = \Drupal::database()->select('journal_entry_line', 'journal_entry_line'); + $subQuery->join('accounting_account', 'accounting_account', 'accounting_account.id = journal_entry_line.accounting_account_id'); + $subQuery->addField('accounting_account', 'uid'); + $subQuery->addExpression("SUM(debit - credit)", 'balance'); + $subQuery->condition('journal_entry_line.state', 'valid'); + $subQuery->condition('accounting_account.type', $this->options['account_bundle']); + $subQuery->groupBy("accounting_account.uid"); + + // Add the subquery to as a component of a join. + $joinDefinition = [ + 'table formula' => $subQuery, + 'field' => 'uid', + 'left_table' => 'users_field_data', + 'left_field' => 'uid', + 'adjust' => TRUE, + ]; + + // Create a join object and create a relationship between the main query and the subquery. + $join = \Drupal::service('plugin.manager.views.join')->createInstance('standard', $joinDefinition); + $join_alias = $this->query->addRelationship('journal_entry_line', $join, 'users_field_data'); + + // Add the field to the Views interface. + $this->field_alias = $this->query->addField(NULL, "COALESCE({$join_alias}.balance, 0)", 'balance'); + } + }