Skip to content

Commit

Permalink
CIWEMB-569: Add credit note tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
shahrukh-moby committed Dec 12, 2023
1 parent d9d075f commit fe2900e
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CRM/Financeextras/Form/Contribute/CreditNoteEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function preProcess() {
* @throws \CRM_Core_Exception
*/
public function listTokens() {
$tokenProcessor = new TokenProcessor(Civi::dispatcher(), ['schema' => ['contactId']]);
$tokenProcessor = new TokenProcessor(Civi::dispatcher(), ['schema' => ['contactId'], 'creditNoteId' => $this->creditNoteId]);
$tokens = $tokenProcessor->listTokens();

return $tokens;
Expand Down
115 changes: 115 additions & 0 deletions Civi/Financeextras/Token/CreditNote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Civi\Financeextras\Token;

use Civi\Token\Event\TokenValueEvent;
use Civi\Token\AbstractTokenSubscriber;
use Civi\Token\TokenRow;

/**
* Class CRM_Financeextras_Token_CreditNote
*
* Generate "credit_note.*" tokens.
*
* This class defines tokens for credit note fields.
*/
class CreditNote extends AbstractTokenSubscriber {

private const TOKEN = 'creditNote';

/**
* Class constructor.
*/
public function __construct() {
parent::__construct('creditNote', $this->geTokens());
}

public static function getSubscribedEvents() {
return [
'civi.token.list' => 'registerTokens',
'civi.token.eval' => 'evaluateTokens',
];
}

/**
* Determine whether this token-handler should be used with
* the given processor.
*
* @param \Civi\Token\TokenProcessor $processor
* @return bool
*/
public function checkActive(\Civi\Token\TokenProcessor $processor) {
$creditNoteId = $processor->getContextValues('creditNoteId');

return is_array($creditNoteId) && count($creditNoteId) === 1;
}

/**
* To perform a bulk lookup before rendering tokens
*
* @param \Civi\Token\Event\TokenValueEvent $e
*
* @return mixed
*/
public function prefetch(TokenValueEvent $e): array {
$creditNoteId = $e->getTokenProcessor()->getContextValues('creditNoteId');
$resolvedTokens = [];

try {
if (is_array($creditNoteId) && count($creditNoteId) === 1) {
$creditNoteId = $creditNoteId[0];
$creditNote = CRM_Financeextras_BAO_CreditNote::findById($creditNoteId);

if (empty($creditNote)) {
return $resolvedTokens;
}

$resolvedTokens = array_merge($this->geTokens(), (array) $creditNote);
}
}
catch (Exception $e) {
CRM_Core_Session::setStatus('Error resolving credit note tokens');
}

return $resolvedTokens;
}

/**
* Evaluate the content of a single token.
*
* @param \Civi\Token\TokenRow $row
* The record for which we want token values.
* @param string $entity
* The name of the token entity.
* @param string $field
* The name of the token field.
* @param mixed $prefetch
* Any data that was returned by the prefetch().
*/
public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL): void {
$value = CRM_Utils_Array::value($field, $prefetch);
$prefix = CRM_Utils_String::munge(self::TOKEN);

if ($value) {
$row->format('text/plain')->tokens($prefix, $field, $value);
$row->format('text/html')->tokens($prefix, $field, $value);
}
}

private function geTokens(): array {
return [
'id' => 'Credit Note Id',
'contact_id' => 'Credit Note Contact Id',
'cn_number' => 'Credit Note Number',
'date' => 'Credit Note Date',
'reference' => 'Credit Note Reference',
'currency' => 'Credit Note Currency',
'description' => 'Credit Note Description',
'comment' => 'Credit Note Comment',
'subtotal' => 'Credit Note Sub Total',
'sales_tax' => 'Credit Note Sales Tax',
'total_credit' => 'Credit Note Total Credit',
];
}

}
1 change: 1 addition & 0 deletions financeextras.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function financeextras_civicrm_config(&$config) {
Civi::dispatcher()->addListener('fe.contribution.received_payment', ['\Civi\Financeextras\Event\Listener\ContributionPaymentUpdatedListener', 'handle']);
Civi::dispatcher()->addListener('civi.api.prepare', ['Civi\Financeextras\APIWrapper\BatchListPage', 'preApiCall']);
Civi::dispatcher()->addListener('civi.token.list', 'financeextras_register_tokens');
Civi::dispatcher()->addSubscriber(new Civi\Financeextras\Token\CreditNote());
}

/**
Expand Down

0 comments on commit fe2900e

Please sign in to comment.