Skip to content

Commit

Permalink
Use API4 for retrieving contributions of SEPA transaction groups, fil…
Browse files Browse the repository at this point in the history
…tering for permissioned financial types only
  • Loading branch information
jensschuppe committed Aug 26, 2024
1 parent d694df2 commit 4a875cf
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 106 deletions.
191 changes: 91 additions & 100 deletions CRM/Sepa/Page/ListGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,119 +14,110 @@
| written permission from the original author(s). |
+--------------------------------------------------------*/

use CRM_Sepa_ExtensionUtil as E;
use Civi\Api4\SepaTransactionGroup;
use Civi\Api4\Contribution;

/**
* back office sepa group content viewer
*
* @package CiviCRM_SEPA
*
*/


require_once 'CRM/Core/Page.php';

class CRM_Sepa_Page_ListGroup extends CRM_Core_Page {

function run() {
CRM_Utils_System::setTitle(ts('SEPA Group Contributions', array('domain' => 'org.project60.sepa')));
if (isset($_REQUEST['group_id'])) {
// get some values
$group_id = (int) $_REQUEST['group_id'];
$financial_types = CRM_Contribute_PseudoConstant::financialType();

// load the group
$txgroup = civicrm_api('SepaTransactionGroup', 'getsingle', array('id'=>$group_id, 'version'=>3));
if (isset($txgroup['is_error']) && $txgroup['is_error']) {
CRM_Core_Session::setStatus(sprintf(ts("Cannot read SEPA transaction group [%s]. Error was: '%s'", array('domain' => 'org.project60.sepa')), $group_id, $txgroup['error_message']), ts("Error", array('domain' => 'org.project60.sepa')), "error");
}

// get contribution status option group ID
$status_option_group_id = 99999;
$option_group = civicrm_api3('OptionGroup', 'get', array(
'name' => 'contribution_status',
'return' => 'id'));
if (!empty($option_group['id'])) {
$status_option_group_id = $option_group['id'];
}

// load the group's contributions
$sql = "
SELECT
civicrm_sdd_txgroup.reference AS reference,
civicrm_contact.display_name AS contact_display_name,
civicrm_contact.contact_type AS contact_contact_type,
civicrm_contact.id AS contact_id,
civicrm_contribution.id AS contribution_id,
civicrm_contribution.total_amount AS contribution_amount,
civicrm_contribution.currency AS contribution_currency,
civicrm_contribution.financial_type_id AS contribution_financial_type_id,
civicrm_campaign.title AS contribution_campaign,
civicrm_option_value.label AS contribution_status
FROM
civicrm_sdd_txgroup
LEFT JOIN
civicrm_sdd_contribution_txgroup ON civicrm_sdd_txgroup.id = civicrm_sdd_contribution_txgroup.txgroup_id
LEFT JOIN
civicrm_contribution ON civicrm_contribution.id = civicrm_sdd_contribution_txgroup.contribution_id
LEFT JOIN
civicrm_contact ON civicrm_contact.id = civicrm_contribution.contact_id
LEFT JOIN
civicrm_campaign ON civicrm_campaign.id = civicrm_contribution.campaign_id
LEFT JOIN
civicrm_option_value ON civicrm_option_value.value = civicrm_contribution.contribution_status_id AND civicrm_option_value.option_group_id = {$status_option_group_id}
WHERE
civicrm_sdd_txgroup.id = $group_id;";

$total_amount = 0.0;
$total_count = 0;
$total_campaigns = array();
$total_types = array();
$total_contacts = array();
$status_stats = array();
$contact_base_link = CRM_Utils_System::url('civicrm/contact/view', '&reset=1&cid=');
$contribution_base_link = CRM_Utils_System::url('civicrm/contact/view/contribution', '&reset=1&id=_cid_&cid=_id_&action=view');
$currency = NULL;

$contributions = array();
$result = CRM_Core_DAO::executeQuery($sql);
while ($result->fetch()) {
$currency = $result->contribution_currency;
$contributions[$total_count] = array(
'contact_display_name' => $result->contact_display_name,
'contact_type' => $result->contact_contact_type,
'contact_id' => $result->contact_id,
'contact_link' => $contact_base_link.$result->contact_id,
'contribution_link' => str_replace('_id_', $result->contact_id, str_replace('_cid_', $result->contribution_id, $contribution_base_link)),
'contribution_id' => $result->contribution_id,
'contribution_status' => $result->contribution_status,
'contribution_amount' => $result->contribution_amount,
'contribution_amount_str' => CRM_Utils_Money::format($result->contribution_amount, $result->contribution_currency),
'financial_type' => $financial_types[$result->contribution_financial_type_id],
'campaign' => $result->contribution_campaign,
);
CRM_Utils_System::setTitle(E::ts('SEPA Group Contributions'));
try {
$groupId = CRM_Utils_Request::retrieve('group_id', 'Integer', NULL, TRUE);
$txGroup = SepaTransactionGroup::get(TRUE)
->selectRowCount()
->addSelect(
'id',
'reference',
'status_id',
'COUNT(DISTINCT contribution.id) AS total_count',
'SUM(contribution.total_amount) AS total_amount',
'COUNT(DISTINCT contribution.campaign_id) AS different_campaigns',
'COUNT(DISTINCT contribution.financial_type_id) AS different_types',
'COUNT(DISTINCT contribution.contact_id) AS different_contacts'
)
->addJoin('Contribution AS contribution', 'INNER', 'SepaContributionGroup')
->addWhere('id', '=', $groupId)
->addGroupBy('id')
->execute()
->single();
}
catch (CRM_Core_Exception $exception) {
CRM_Core_Error::statusBounce(
E::ts(
'Cannot read SEPA transaction group [%1]. Error was: %2',
[
1 => $groupId,
2 => $exception->getMessage(),
]
),
CRM_Utils_System::url('civicrm/sepa')
);
}

$total_count += 1;
$total_amount += $result->contribution_amount;
$total_types[$result->contribution_financial_type_id] = 1;
$total_contacts[$result->contact_id] = 1;
$total_campaigns[$result->contribution_campaign] = 1;
$reference = $result->reference;
$status_stats[$result->contribution_status] = 1 + CRM_Utils_Array::value($result->contribution_status, $status_stats, 0);
}
$result = Contribution::get()
->selectRowCount()
->addSelect(
'contact_id.display_name',
'contact_id.contact_type',
'contact_id.id',
'id',
'total_amount',
'currency',
'financial_type_id',
'financial_type_id:label',
'campaign_id.title',
'contribution_status_id:label'
)
->addJoin('SepaTransactionGroup AS sepa_transaction_group', 'INNER', 'SepaContributionGroup')
->addWhere('sepa_transaction_group.id', '=', $groupId)
->execute();
$statusStats = [];
$contributions = [];
foreach ($result as $contribution) {
$contributions[] = [
'contact_display_name' => $contribution['contact_id.display_name'],
'contact_type' => $contribution['contact_id.contact_type'],
'contact_id' => $contribution['contact_id.id'],
'contact_link' => CRM_Utils_System::url(
'civicrm/contact/view',
'&reset=1&cid=' . $contribution['contact_id.id']
),
'contribution_link' => CRM_Utils_System::url(
'civicrm/contact/view/contribution',
'&reset=1&id=' . $contribution['id'] . '&cid=' . $contribution['contact_id.id'] . '&action=view'
),
'contribution_id' => $contribution['id'],
'contribution_status' => $contribution['contribution_status_id:label'],
'contribution_amount' => $contribution['total_amount'],
'contribution_amount_str' => CRM_Utils_Money::format($contribution['total_amount'], $contribution['currency']),
'financial_type_id' => $contribution['financial_type_id'],
'financial_type' => $contribution['financial_type_id:label'],
'campaign' => $contribution['campaign_id.title'],
];
$statusStats[$contribution['contribution_status_id:label']] =
1 + ($statusStats[$contribution['contribution_status_id:label']] ?? 0);
}

$this->assign("txgroup", $txgroup);
$this->assign("reference", $reference);
$this->assign("group_id", $group_id);
$this->assign("total_count", $total_count);
$this->assign("total_amount", $total_amount);
$this->assign("total_amount_str", CRM_Utils_Money::format($total_amount, $currency));
$this->assign("contributions", $contributions);
$this->assign("different_campaigns", count($total_campaigns));
$this->assign("different_types", count($total_types));
$this->assign("different_contacts", count($total_contacts));
$this->assign("status_stats", $status_stats);
$this->assign('txgroup', $txGroup);
$this->assign('reference', $txGroup['reference']);
$this->assign('group_id', $groupId);
$this->assign('total_count', $txGroup['total_count']);
$this->assign('total_amount', $txGroup['total_amount']);
$this->assign('total_amount_str', CRM_Utils_Money::format($txGroup['total_amount'], $result->first()['currency']));
$this->assign('contributions', $contributions);
$this->assign('different_campaigns', $txGroup['different_campaigns']);
$this->assign('different_types', $txGroup['different_types']);
$this->assign('different_contacts', $txGroup['different_contacts']);
$this->assign('status_stats', $statusStats);

parent::run();
}

}
}
Binary file modified l10n/de_DE/LC_MESSAGES/sepa.mo
Binary file not shown.
18 changes: 14 additions & 4 deletions l10n/de_DE/LC_MESSAGES/sepa.po
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ msgstr ""
#: CRM/Sepa/BAO/SEPAMandate.php CRM/Sepa/Form/CreateMandate.php
#: CRM/Sepa/Logic/Queue/Update.php CRM/Sepa/Logic/Status.php
#: CRM/Sepa/Page/CloseGroup.php CRM/Sepa/Page/CreateMandate.php
#: CRM/Sepa/Page/DashBoard.php CRM/Sepa/Page/EditMandate.php
#: CRM/Sepa/Page/ListGroup.php js/SepaSettings.js
#: CRM/Sepa/Page/DashBoard.php CRM/Sepa/Page/EditMandate.php js/SepaSettings.js
msgid "Error"
msgstr "Fehler"

Expand Down Expand Up @@ -1789,8 +1788,9 @@ msgid "SEPA Group Contributions"
msgstr "Zuwendungen der SEPA Gruppe"

#: CRM/Sepa/Page/ListGroup.php
msgid "Cannot read SEPA transaction group [%s]. Error was: '%s'"
msgstr "Die SEPA Gruppe [%s] konnte nicht geladen werden. Fehler ist: '%s'"
msgid "Cannot read SEPA transaction group [%1]. Error was: %2"
msgstr ""
"Die SEPA-Transaktionsgruppe [%1] konnte nicht geladen werden. Fehler: %2"

#: CRM/Sepa/Page/MandateTab.php sepa.php
msgid "SEPA Mandates"
Expand Down Expand Up @@ -3149,6 +3149,14 @@ msgstr "Dauereinzüge Aktualisieren"
msgid "retry collection"
msgstr "Einzug erneut versuchen"

#: templates/CRM/Sepa/Page/DashBoard.tpl
msgid ""
"Note that only groups with contributions of authorized financial types are "
"being displayed."
msgstr ""
"Beachten Sie, dass nur Gruppen mit Zuwendungen angezeigt werden, für deren "
"Zuwendungsart Sie die Berechtigung haben."

#: templates/CRM/Sepa/Page/DashBoard.tpl
msgid "Group Name"
msgstr "Gruppenname"
Expand Down Expand Up @@ -3804,6 +3812,8 @@ msgid ""
"Note that only mandates associated with contributions of authorized "
"financial types are being displayed."
msgstr ""
"Beachten Sie, dass nur Mandate mit zugehörigen Zuwendungen angezeigt werden, "
"für deren Zuwendungsart Sie die Berechtigung haben."

#: templates/CRM/Sepa/Page/MandateTab.tpl
msgid "One-Off SEPA Mandates"
Expand Down
8 changes: 6 additions & 2 deletions l10n/org.project60.sepa.pot
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ msgstr ""
msgid "Cannot close mandate [%s], batching in progress!"
msgstr ""

#: CRM/Sepa/BAO/SEPAMandate.php CRM/Sepa/Form/CreateMandate.php CRM/Sepa/Logic/Queue/Update.php CRM/Sepa/Logic/Status.php CRM/Sepa/Page/CloseGroup.php CRM/Sepa/Page/CreateMandate.php CRM/Sepa/Page/DashBoard.php CRM/Sepa/Page/EditMandate.php CRM/Sepa/Page/ListGroup.php js/SepaSettings.js
#: CRM/Sepa/BAO/SEPAMandate.php CRM/Sepa/Form/CreateMandate.php CRM/Sepa/Logic/Queue/Update.php CRM/Sepa/Logic/Status.php CRM/Sepa/Page/CloseGroup.php CRM/Sepa/Page/CreateMandate.php CRM/Sepa/Page/DashBoard.php CRM/Sepa/Page/EditMandate.php js/SepaSettings.js
msgid "Error"
msgstr ""

Expand Down Expand Up @@ -1519,7 +1519,7 @@ msgid "SEPA Group Contributions"
msgstr ""

#: CRM/Sepa/Page/ListGroup.php
msgid "Cannot read SEPA transaction group [%s]. Error was: '%s'"
msgid "Cannot read SEPA transaction group [%1]. Error was: %2"
msgstr ""

#: CRM/Sepa/Page/MandateTab.php sepa.php
Expand Down Expand Up @@ -2550,6 +2550,10 @@ msgstr ""
msgid "retry collection"
msgstr ""

#: templates/CRM/Sepa/Page/DashBoard.tpl
msgid "Note that only groups with contributions of authorized financial types are being displayed."
msgstr ""

#: templates/CRM/Sepa/Page/DashBoard.tpl
msgid "Group Name"
msgstr ""
Expand Down

0 comments on commit 4a875cf

Please sign in to comment.