Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add action find latest account for contact #376

Merged
merged 2 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 85 additions & 0 deletions extension/Civi/Banking/Actions/FindLatestAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
namespace Civi\Banking\Actions;

use \Civi\ActionProvider\Action\AbstractAction;
use Civi\ActionProvider\Exception\ExecutionException;
use \Civi\ActionProvider\Exception\InvalidParameterException;
use Civi\ActionProvider\Parameter\OptionGroupByNameSpecification;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;

use Civi\Core\Lock\NullLock;
use CRM_Banking_ExtensionUtil as E;

/**
* Class to find the latest bank account for a contact
*
* @package Civi\Banking\Actions
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @license AGPL-3.0
*/
class FindLatestAccount extends AbstractAction {

/**
* @return SpecificationBag
*/
public function getParameterSpecification() {
$specs = new SpecificationBag();
$specs->addSpecification(new Specification('contact_id', 'Integer', E::ts('Contact ID'), TRUE, NULL));
return $specs;
}

/**
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag();
}

/**
* Do the actual action - find the latest bank account ID for contact and with that ID find account
*
* @param ParameterBagInterface $parameters
* @param ParameterBagInterface $output
* @throws ExecutionException
*/
public function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$contactId = (int) $parameters->getParameter('contact_id');
if (!empty($contactId)) {
try {
$baId = civicrm_api3('BankingAccount', 'getvalue', [
'return' => "id",
'contact_id' => $contactId,
'options' => ['sort' => "id DESC", 'limit' => 1],
]);
if ($baId) {
$bankAccount = civicrm_api3('BankingAccountReference', 'getvalue', [
'return' => "reference",
'ba_id' => $baId,
]);
if ($bankAccount) {
$output->setParameter('bank_account', $bankAccount);
}
}
} catch (\CiviCRM_API3_Exception $ex) {
throw new ExecutionException(E::ts('Could not find a bank account for contact') . $contactId
. E::ts(', error message from API3 BankingAccount or BankingAccountReference getvalue: ') . $ex->getMessage());
}
}
}

/**
* Returns the specification of the output parameters of this action.
*
* This function could be overriden by child classes.
*
* @return SpecificationBag
*/
public function getOutputSpecification() {
return new SpecificationBag([
new Specification("bank_account", "String", E::ts("Bank Account"), FALSE),
]);
}

}
2 changes: 2 additions & 0 deletions extension/Civi/Banking/CompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public function process(ContainerBuilder $container) {
$actionProviderDefinition = $container->getDefinition('action_provider');
$actionProviderDefinition->addMethodCall('addAction',
['AddIban', 'Civi\Banking\Actions\AddIban', E::ts('Add IBAN to Contact'), []]);
$actionProviderDefinition->addMethodCall('addAction',
['FindLatestAccount', 'Civi\Banking\Actions\FindLatestAccount', E::ts('Find Latest Bank Account for Contact'), []]);
}
}
}