Skip to content

Commit

Permalink
Replace various ocurrences of APIv3 calls to CiviSEPA entities with A…
Browse files Browse the repository at this point in the history
…PIv4 calls for checking Financial ACLs permissions
  • Loading branch information
jensschuppe committed Oct 1, 2024
1 parent 5bfdfbd commit 1ac233f
Show file tree
Hide file tree
Showing 19 changed files with 463 additions and 245 deletions.
37 changes: 28 additions & 9 deletions CRM/Sepa/BAO/SEPAMandate.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*
*/

use CRM_Sepa_ExtensionUtil as E;


/**
* Class contains functions for Sepa mandates
Expand Down Expand Up @@ -48,9 +50,14 @@ static function add(&$params) {
// new mandate, use the default creditor
$default_creditor = CRM_Sepa_Logic_Settings::defaultCreditor();
$params['creditor_id'] = $default_creditor->id;
} else {
}
else {
// existing mandate, get creditor
$params['creditor_id'] = civicrm_api3('SepaMandate', 'getvalue', array ('id' => $params['id'], 'return' => 'creditor_id'));
$params['creditor_id'] = \Civi\Api4\SepaMandate::get(TRUE)
->addSelect('creditor_id')
->addWhere('id', '=', $params['id'])
->execute()
->single()['creditor_id'];
}
}
$creditor = civicrm_api3 ('SepaCreditor', 'getsingle', array ('id' => $params['creditor_id'], 'return' => 'mandate_prefix,creditor_type'));
Expand Down Expand Up @@ -371,16 +378,22 @@ static function terminateMandate($mandate_id, $new_end_date_str, $cancel_reason=
}

// first, load the mandate
$mandate = civicrm_api("SepaMandate", "getsingle", array('id'=>$mandate_id, 'version'=>3));
if (isset($mandate['is_error'])) {
try {
$mandate = \Civi\Api4\SepaMandate::get(TRUE)
->addWhere('id', '=', $mandate_id)
->execute()
->single();
}
catch (Exception $exception) {
$lock->release();

$error_message = sprintf(ts("Cannot read mandate [%s]. Error was: '%s'", array('domain' => 'org.project60.sepa')), $mandate_id, $mandate['error_message']);
$error_message = E::ts("Cannot read mandate [%1]. Error was: '%2'", [1 => $mandate_id, 2 => $exception->getMessage()]);
if ($error_to_ui) {
CRM_Core_Session::setStatus($error_message, ts('Error'), 'error');
return FALSE;
} else {
throw new Exception($error_message);
}
else {
throw new CRM_Core_Exception($error_message);
}
}

Expand Down Expand Up @@ -546,7 +559,10 @@ static function modifyMandate($mandate_id, $changes) {
}

// load the mandate
$mandate = civicrm_api3('SepaMandate', 'getsingle', array('id' => $mandate_id));
$mandate = \Civi\Api4\SepaMandate::get(TRUE)
->addWhere('id', '=', $mandate_id)
->execute()
->single();
if ($mandate['type'] != 'RCUR') {
$lock->release();
throw new Exception(ts("You can only modify RCUR mandates.", array('domain' => 'org.project60.sepa')));
Expand Down Expand Up @@ -906,7 +922,10 @@ public static function getLastMandateOfContact($cid) {
]);
if ($result->fetch()) {
// return the mandate
return civicrm_api3('SepaMandate', 'getsingle', array('id' => $result->mandate_id));
return \Civi\Api4\SepaMandate::get(TRUE)
->addWhere('id', '=', $result->mandate_id)
->execute()
->single();
}
else {
return FALSE;
Expand Down
39 changes: 28 additions & 11 deletions CRM/Sepa/BAO/SEPATransactionGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,13 @@ function generateXML ($id = null) {
* @return int id of the sepa file entity created, or an error message string
*/
static function createFile($txgroup_id, $override = false) {
$txgroup = civicrm_api('SepaTransactionGroup', 'getsingle', array('id'=>$txgroup_id, 'version'=>3));
if (isset($txgroup['is_error']) && $txgroup['is_error']) {
try {
$txgroup = \Civi\Api4\SepaTransactionGroup::get(TRUE)
->addWhere('id', '=', $txgroup_id)
->execute()
->single();
}
catch (CRM_Core_Exception $exception) {
return "Cannot find transaction group ".$txgroup_id;
}

Expand Down Expand Up @@ -252,7 +257,10 @@ static function createFile($txgroup_id, $override = false) {
* @return an update array with the txgroup or a string with an error message
*/
static function adjustCollectionDate($txgroup_id, $latest_submission_date) {
$txgroup = civicrm_api3('SepaTransactionGroup', 'getsingle', array('id' => $txgroup_id));
$txgroup = \Civi\Api4\SepaTransactionGroup::get(TRUE)
->addWhere('id', '=', $txgroup_id)
->execute()
->single();
if ($txgroup['type'] == 'RTRY') {
$txgroup['type'] = 'RCUR';
}
Expand All @@ -277,12 +285,16 @@ static function adjustCollectionDate($txgroup_id, $latest_submission_date) {
}

// reload the item
$txgroup = civicrm_api('SepaTransactionGroup', 'getsingle', array('version'=>3, 'id'=>$txgroup_id));
if (!empty($txgroup['is_error'])) {
return $txgroup['error_message'];
} else {
return $txgroup;
try {
$txgroup = \Civi\Api4\SepaTransactionGroup::get(TRUE)
->addWhere('id', '=', $txgroup_id)
->execute()
->single();
}
catch (Exception $exception) {
return $exception->getMessage();
}
return $txgroup;
}


Expand All @@ -302,9 +314,14 @@ static function adjustCollectionDate($txgroup_id, $latest_submission_date) {
*/
static function deleteGroup($txgroup_id, $delete_contributions_mode = 'no') {
// load the group
$txgroup = civicrm_api('SepaTransactionGroup', 'getsingle', array('id' => $txgroup_id, 'version' => 3));
if (!empty($txgroup['is_error'])) {
return "Transaction group [$txgroup_id] could not be loaded. Error was: ".$txgroup['error_message'];
try {
$txgroup = \Civi\Api4\SepaTransactionGroup::get(TRUE)
->addWhere('id', '=', $txgroup_id)
->execute()
->single();
}
catch (Exception $exception) {
return "Transaction group [$txgroup_id] could not be loaded. Error was: " . $exception->getMessage();
}

// first, delete the contents of this group
Expand Down
13 changes: 9 additions & 4 deletions CRM/Sepa/Form/CreateMandate.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,11 @@ public function postProcess() {

try {
$mandate = civicrm_api3('SepaMandate', 'createfull', $mandate_data);
$mandate = civicrm_api3('SepaMandate', 'getsingle', array(
'id' => $mandate['id'],
'return' => 'reference,id,type'));
$mandate = \Civi\Api4\SepaMandate::get(TRUE)
->addSelect('reference', 'id', 'type')
->addWhere('id', '=', $mandate['id'])
->execute()
->single();

// if we get here, everything went o.k.
CRM_Core_Session::setStatus(E::ts("'%3' SEPA Mandate <a href=\"%2\">%1</a> created.", array(
Expand All @@ -483,7 +485,10 @@ public function postProcess() {

// terminate old mandate, of requested
if (!empty($values['replace'])) {
$rpl_mandate = civicrm_api3('SepaMandate', 'getsingle', array('id' => $values['replace']));
$rpl_mandate = \Civi\Api4\SepaMandate::get(TRUE)
->addWhere('id', '=', $values['replace'])
->execute()
->single();

CRM_Sepa_BAO_SEPAMandate::terminateMandate(
$values['replace'],
Expand Down
10 changes: 5 additions & 5 deletions CRM/Sepa/Form/RetryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ protected function getCreditorList() {
*/
protected function getGroupList() {
$txgroup_list = array();
$txgroup_query = civicrm_api3('SepaTransactionGroup', 'get', array(
'option.limit' => 0,
'type' => array('IN' => array('RCUR', 'FRST')),
'return' => 'reference,id'));
foreach ($txgroup_query['values'] as $txgroup) {
$txgroup_query = \Civi\Api4\SepaTransactionGroup::get(TRUE)
->addSelect('reference', 'id')
->addWhere('type', 'IN', ['RCUR', 'FRST'])
->execute();
foreach ($txgroup_query as $txgroup) {
$txgroup_list[$txgroup['id']] = $txgroup['reference'];
}
return $txgroup_list;
Expand Down
36 changes: 26 additions & 10 deletions CRM/Sepa/Logic/Batching.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,10 +541,16 @@ protected static function syncGroups(
}
}
else {
$group = civicrm_api('SepaTransactionGroup', 'getsingle', array('version' => 3, 'id' => $existing_groups[$collection_date][$financial_type_id ?? 0], 'status_id' => $group_status_id_open));
if (!empty($group['is_error'])) {
try {
$group = \Civi\Api4\SepaTransactionGroup::get(TRUE)
->addWhere('id', '=', $existing_groups[$collection_date][$financial_type_id ?? 0])
->addWhere('status_id', '=', $group_status_id_open)
->execute()
->single();
}
catch (Exception $exception) {
// TODO: Error handling
Civi::log()->debug("org.project60.sepa: batching:syncGroups/getGroup ".$group['error_message']);
Civi::log()->debug('org.project60.sepa: batching:syncGroups/getGroup ' . $exception->getMessage());
}
unset($existing_groups[$collection_date][$financial_type_id ?? 0]);
}
Expand Down Expand Up @@ -615,9 +621,17 @@ protected static function syncGroups(
* Check if a transaction group reference is already in use
*/
public static function referenceExists($reference) {
$query = civicrm_api('SepaTransactionGroup', 'getsingle', array('reference'=>$reference, 'version'=>3));
// this should return an error, if the group exists
return !(isset($query['is_error']) && $query['is_error']);
try {
$txgroup = \Civi\Api4\SepaTransactionGroup::get(TRUE)
->addWhere('reference', '=', $reference)
->execute()
->single();
$exists = TRUE;
}
catch (Exception $exception) {
$exists = FALSE;
}
return $exists;
}

public static function getTransactionGroupReference(
Expand Down Expand Up @@ -718,10 +732,12 @@ public static function getCycleDay($rcontribution, $creditor_id) {
if (!empty($rcontribution['mandate_first_executed'])) {
$date = $rcontribution['mandate_first_executed'];
} else {
$mandate = civicrm_api3('SepaMandate', 'getsingle', array(
'entity_table' => 'civicrm_contribution_recur',
'entity_id' => $rcontribution['id'],
'return' => 'status'));
$mandate = \Civi\Api4\SepaMandate::get(TRUE)
->addSelect('status')
->addWhere('entity_table', '=', 'civicrm_contribution_recur')
->addWhere('entity_id', '=', $rcontribution['id'])
->execute()
->single();

if ($mandate['status'] == 'RCUR') {
// support for RCUR without first contribution
Expand Down
22 changes: 16 additions & 6 deletions CRM/Sepa/Logic/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ static function close($txgroup_id) {
}
$status_closed = (int) CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
$group_status_id_open = (int) CRM_Core_PseudoConstant::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Open');
$txgroup = civicrm_api('SepaTransactionGroup', 'getsingle', array('id'=>$txgroup_id, 'version'=>3));
if (isset($txgroup['is_error']) && $txgroup['is_error']) {
try {
$txgroup = \Civi\Api4\SepaTransactionGroup::get(TRUE)
->addWhere('id', '=', $txgroup_id)
->execute()
->single();
}
catch (Exception $exception) {
$lock->release();
return "Cannot find transaction group ".$txgroup_id;
return 'Cannot find transaction group ' . $txgroup_id;
}
$collection_date = $txgroup['collection_date'];

Expand Down Expand Up @@ -169,10 +174,15 @@ static function received($txgroup_id) {
return civicrm_api3_create_error("Status 'Pending', 'Completed' or 'In Progress' does not exist!");

// step 0: load the group object
$txgroup = civicrm_api('SepaTransactionGroup', 'getsingle', array('id'=>$txgroup_id, 'version'=>3));
if (!empty($txgroup['is_error'])) {
try {
$txgroup = \Civi\Api4\SepaTransactionGroup::get(TRUE)
->addWhere('id', '=', $txgroup_id)
->execute()
->single();
}
catch (Exception $exception) {
$lock->release();
return "Cannot find transaction group ".$txgroup_id;
return 'Cannot find transaction group ' . $txgroup_id;
}

// check status
Expand Down
6 changes: 5 additions & 1 deletion CRM/Sepa/Logic/PaymentInstruments.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ public static function getSDDPaymentInstrumentsForContribution($contribution_id)
return null;
} else {
// get the creditor ID
$mandate = civicrm_api3('SepaMandate', 'getsingle', ['return' => 'creditor_id,type,status', 'id' => $mandate_id]);
$mandate = \Civi\Api4\SepaMandate::get(TRUE)
->addSelect('creditor_id', 'type', 'status')
->addWhere('id', '=', $mandate_id)
->execute()
->single();
return self::getPaymentInstrumentsForCreditor($mandate['creditor_id'], $mandate['type']);
}
}
Expand Down
15 changes: 9 additions & 6 deletions CRM/Sepa/Logic/Queue/Close.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,17 @@ public static function launchCloseRunner($txgroup_ids, $target_group_status, $ta
$is_received_runner = $target_contribution_status == (int) CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');

// fetch the groups
$txgroup_query = civicrm_api3('SepaTransactionGroup', 'get', array(
'id' => ['IN' => $txgroup_ids],
'option.limit' => 0
));
$txgroups = \Civi\Api4\SepaTransactionGroup::get(TRUE)
->addWhere('id', 'IN', $txgroup_ids)
->execute();

$group_status_id_busy = (int) CRM_Core_PseudoConstant::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Data Entry');
$group_status_id_busy = (int) CRM_Core_PseudoConstant::getKey(
'CRM_Batch_BAO_Batch',
'status_id',
'Data Entry'
);

foreach ($txgroup_query['values'] as $txgroup) {
foreach ($txgroups as $txgroup) {
// first: set group status to busy

$queue->createItem(new CRM_Sepa_Logic_Queue_Close('set_group_status', $txgroup, $group_status_id_busy));
Expand Down
Loading

0 comments on commit 1ac233f

Please sign in to comment.