Skip to content

Commit

Permalink
Add and use API4 Get action for SEPA transaction groups checking Fina…
Browse files Browse the repository at this point in the history
…ncial ACLs
  • Loading branch information
jensschuppe committed Aug 22, 2024
1 parent 0d1de98 commit 3ab15c2
Show file tree
Hide file tree
Showing 11 changed files with 654 additions and 245 deletions.
99 changes: 74 additions & 25 deletions CRM/Sepa/Page/DashBoard.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
*/

require_once 'CRM/Core/Page.php';
use CRM_Sepa_ExtensionUtil as E;

class CRM_Sepa_Page_DashBoard extends CRM_Core_Page {

Expand Down Expand Up @@ -83,57 +83,106 @@ function run() {
$this->assign('closed_status_id', CRM_Core_PseudoConstant::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Closed'));

// now read the details
$result = civicrm_api("SepaTransactionGroup", "getdetail", array(
"version" => 3,
"sequential" => 1,
"status_ids" => implode(',', $status_list[$status]),
"order_by" => (($status=='open')?'latest_submission_date':'file.created_date'),
));
if (isset($result['is_error']) && $result['is_error']) {
CRM_Core_Session::setStatus(sprintf(ts("Couldn't read transaction groups. Error was: '%s'", array('domain' => 'org.project60.sepa')), $result['error_message']), ts('Error', array('domain' => 'org.project60.sepa')), 'error');
} else {
try {
// API4 SepaTransactionGroup.get checks Financial ACLs for corresponding contributions.
$sepaTransactionGroups = \Civi\Api4\SepaTransactionGroup::get(TRUE)
->selectRowCount()
->addSelect(
'id',
'reference',
'sdd_file_id',
'type',
'collection_date',
'latest_submission_date',
'status_id',
'sdd_creditor_id',
'sepa_sdd_file.created_date',
'SUM(contribution.total_amount) AS total',
'COUNT(*) AS nb_contrib',
'contribution.currency',
'sepa_sdd_file.reference'
)
->addJoin(
'Contribution AS contribution',
'LEFT',
'SepaContributionGroup'
)
->addJoin(
'SepaSddFile AS sepa_sdd_file',
'LEFT',
['sdd_file_id', '=', 'sepa_sdd_file.id']
)
->addWhere('status_id', 'IN', $status_list[$status])
->addOrderBy('open' === $status ? 'latest_submission_date' : 'sepa_sdd_file.created_date')
->addGroupBy('id')
->addGroupBy('reference')
->addGroupBy('sdd_file_id')
->addGroupBy('type')
->addGroupBy('collection_date')
->addGroupBy('latest_submission_date')
->addGroupBy('status_id')
->addGroupBy('sdd_creditor_id')
->addGroupBy('sepa_sdd_file.created_date')
->addGroupBy('contribution.currency')
->addGroupBy('sepa_sdd_file.reference')
->execute();
$groups = [];
$now = date('Y-m-d');
foreach ($result["values"] as $id => $group) {
// 'beautify'
foreach ($sepaTransactionGroups as $group) {
$group['latest_submission_date'] = date('Y-m-d', strtotime($group['latest_submission_date']));
$group['collection_date'] = date('Y-m-d', strtotime($group['collection_date']));
$group['collection_date_in_future'] = ($group['collection_date'] > $now) ? 1 : 0;
$group['status'] = $status_2_title[$group['status_id']];
$group['currency'] = $group['contribution.currency'];
$group['file_id'] = $group['sdd_file_id'];
$group['file_created_date'] = $group['sepa_sdd_file.created_date'];
$group['creditor_id'] = $group['sdd_creditor_id'];
$group['file'] = $group['sepa_sdd_file.reference'];
$group['file'] = $this->getFormatFilename($group);
$group['status_label'] = $status2label[$group['status_id']];
$remaining_days = (strtotime($group['latest_submission_date']) - strtotime("now")) / (60*60*24);
if ($group['status']=='closed') {
$remaining_days = (strtotime($group['latest_submission_date']) - strtotime("now")) / (60 * 60 * 24);
if ('closed' === $group['status']) {
$group['submit'] = 'closed';
} elseif ($group['type'] == 'OOFF') {
}
elseif ('OOFF' === $group['type']) {
$group['submit'] = 'soon';
} else {
}
else {
if ($remaining_days <= -1) {
$group['submit'] = 'missed';
} elseif ($remaining_days <= 1) {
}
elseif ($remaining_days <= 1) {
$group['submit'] = 'urgently';
} elseif ($remaining_days <= 6) {
}
elseif ($remaining_days <= 6) {
$group['submit'] = 'soon';
} else {
}
else {
$group['submit'] = 'later';
}
}

$group['transaction_message'] = CRM_Sepa_BAO_SEPATransactionGroup::getCustomGroupTransactionMessage($group['id']);
$group['transaction_note'] = CRM_Sepa_BAO_SEPATransactionGroup::getNote($group['id']);

array_push($groups, $group);
$groups[] = $group;
}
$this->assign("groups", $groups);
$this->assign('groups', $groups);
}
catch (CRM_Core_Exception $exception) {
CRM_Core_Session::setStatus(
E::ts(
"Couldn't read transaction groups. Error was: %1",
[1 => $result['error_message']]
),
E::ts('Error'),
'error'
);
}

parent::run();
}

function getTemplateFileName() {
return "CRM/Sepa/Page/DashBoard.tpl";
}

/**
* call the batching API
*/
Expand Down
33 changes: 33 additions & 0 deletions Civi/Api4/SepaContributionGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/*
* Copyright (C) 2024 SYSTOPIA GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation in version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types = 1);

namespace Civi\Api4;

use Civi\Api4\Generic\DAOEntity;

/**
* SepaContributionGroup entity.
*
* Provided by the CiviSEPA extension.
*
* @package Civi\Api4
*/
class SepaContributionGroup extends Generic\DAOEntity {
use Generic\Traits\EntityBridge;
}
4 changes: 2 additions & 2 deletions Civi/Api4/SepaCreditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
namespace Civi\Api4;

/**
* Resource entity.
* SepaCreditor entity.
*
* Provided by the CiviCRM Resource Management extension.
* Provided by the CiviSEPA extension.
*
* @package Civi\Api4
*/
Expand Down
4 changes: 2 additions & 2 deletions Civi/Api4/SepaMandate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
use Civi\Sepa\Api4\Action\SepaMandate\GetAction;

/**
* Resource entity.
* SepaMandate entity.
*
* Provided by the CiviCRM Resource Management extension.
* Provided by the CiviSEPA extension.
*
* @package Civi\Api4
*/
Expand Down
4 changes: 2 additions & 2 deletions Civi/Api4/SepaSddFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
namespace Civi\Api4;

/**
* Resource entity.
* SepaSddFile entity.
*
* Provided by the CiviCRM Resource Management extension.
* Provided by the CiviSEPA extension.
*
* @package Civi\Api4
*/
Expand Down
11 changes: 9 additions & 2 deletions Civi/Api4/SepaTransactionGroup.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
<?php
namespace Civi\Api4;

use Civi\Sepa\Api4\Action\SepaTransactionGroup\GetAction;

/**
* Resource entity.
* SepaTransactionGroup entity.
*
* Provided by the CiviCRM Resource Management extension.
* Provided by the CiviSEPA extension.
*
* @package Civi\Api4
*/
class SepaTransactionGroup extends Generic\DAOEntity {

public static function get($checkPermissions = TRUE) {
return (new GetAction(static::getEntityName(), __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

}
41 changes: 41 additions & 0 deletions Civi/Sepa/Api4/Action/SepaTransactionGroup/GetAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/*
* Copyright (C) 2024 SYSTOPIA GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation in version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types = 1);

namespace Civi\Sepa\Api4\Action\SepaTransactionGroup;

use Civi\Api4\Generic\DAOGetAction;
use Civi\Api4\Generic\Result;

class GetAction extends DAOGetAction {

public function _run(Result $result) {
// Add unique joins for permission checks of Financial ACLs.
if ($this->getCheckPermissions()) {
$contributionAlias = uniqid('contribution_');
$this
->addJoin(
'Contribution AS ' . $contributionAlias,
'INNER',
'SepaContributionGroup'
);
}
return parent::_run($result);
}

}
3 changes: 3 additions & 0 deletions api/v3/SepaTransactionGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ function civicrm_api3_sepa_transaction_group_get($params) {
return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
}

/**
* @deprecated This action will not be ported to APIv4. Use the "Get" action with custom joins and filters instead.
*/
function civicrm_api3_sepa_transaction_group_getdetail($params) {
// $where = "txgroup.id= txgroup_contrib.txgroup_id AND txgroup_contrib.contribution_id = contrib.id";
$where = "1";
Expand Down
Binary file modified l10n/de_DE/LC_MESSAGES/sepa.mo
Binary file not shown.
Loading

0 comments on commit 3ab15c2

Please sign in to comment.