3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Report_Form_Pledge_Pbnp
extends CRM_Report_Form
{
18 public $_drilldownReport = ['pledge/summary' => 'Link to Detail Report'];
20 protected $_customGroupExtends = [
27 public function __construct() {
29 'civicrm_contact' => [
30 'dao' => 'CRM_Contact_DAO_Contact',
33 'title' => ts('Constituent Name'),
42 'grouping' => 'contact-fields',
45 'dao' => 'CRM_Pledge_DAO_Pledge',
47 'pledge_create_date' => [
48 'title' => ts('Pledge Made'),
51 'financial_type_id' => [
52 'title' => ts('Financial Type'),
56 'title' => ts('Frequency Unit'),
59 'title' => ts('Installments'),
62 'title' => ts('Amount'),
64 'type' => CRM_Utils_Type
::T_MONEY
,
71 'title' => ts('Status'),
75 'pledge_create_date' => [
76 'title' => ts('Pledge Made'),
77 'operatorType' => CRM_Report_Form
::OP_DATE
,
80 'title' => ts('Pledged Amount'),
81 'operatorType' => CRM_Report_Form
::OP_INT
,
84 'title' => ts('Currency'),
85 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
86 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
88 'type' => CRM_Utils_Type
::T_STRING
,
90 'financial_type_id' => [
91 'title' => ts('Financial Type'),
92 'type' => CRM_Utils_Type
::T_INT
,
93 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
94 'options' => CRM_Contribute_PseudoConstant
::financialType(),
96 'pledge_status_id' => [
97 'name' => 'status_id',
98 'title' => ts('Pledge Status'),
99 'type' => CRM_Utils_Type
::T_INT
,
100 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
101 'options' => CRM_Core_OptionGroup
::values('pledge_status'),
104 'title' => ts('Installments'),
105 'type' => CRM_Utils_Type
::T_INT
,
108 'grouping' => 'pledge-fields',
110 'civicrm_pledge_payment' => [
111 'dao' => 'CRM_Pledge_DAO_PledgePayment',
113 'scheduled_date' => [
114 'title' => ts('Next Payment Due'),
115 'type' => CRM_Utils_Type
::T_DATE
,
118 'scheduled_amount' => [
119 'type' => CRM_Utils_Type
::T_MONEY
,
120 'title' => ts('Next Payment Amount'),
124 'scheduled_date' => [
125 'title' => ts('Next Payment Due'),
126 'operatorType' => CRM_Report_Form
::OP_DATE
,
127 'type' => CRM_Utils_Type
::T_DATE
,
130 'grouping' => 'pledge-fields',
132 'civicrm_address' => [
133 'dao' => 'CRM_Core_DAO_Address',
135 'street_address' => NULL,
137 'postal_code' => NULL,
138 'state_province_id' => [
139 'title' => ts('State/Province'),
142 'title' => ts('Country'),
146 'grouping' => 'contact-fields',
149 'dao' => 'CRM_Core_DAO_Email',
150 'fields' => ['email' => NULL],
151 'grouping' => 'contact-fields',
155 // If we have a campaign, build out the relevant elements
156 $this->addCampaignFields('civicrm_pledge');
158 // Add charts support
161 'barChart' => ts('Bar Chart'),
162 'pieChart' => ts('Pie Chart'),
165 $this->_groupFilter
= TRUE;
166 $this->_tagFilter
= TRUE;
167 $this->_currencyColumn
= 'civicrm_pledge_currency';
168 parent
::__construct();
171 public function preProcess() {
172 $this->assign('reportTitle', ts('Pledged but not Paid Report'));
173 parent
::preProcess();
176 public function select() {
178 $this->_columnHeaders
= [];
179 foreach ($this->_columns
as $tableName => $table) {
180 if (array_key_exists('fields', $table)) {
181 foreach ($table['fields'] as $fieldName => $field) {
182 if (!empty($field['required']) ||
183 !empty($this->_params
['fields'][$fieldName])
185 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
186 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'] ??
NULL;
187 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'] ??
NULL;
192 $this->_selectClauses
= $select;
193 $this->_select
= "SELECT " . implode(', ', $select) . " ";
196 public function from() {
199 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
200 $pendingStatus = array_search('Pending', $allStatus);
206 if ($key = CRM_Utils_Array
::key($statusKey, $allStatus)) {
207 $unpaidStatus[] = $key;
211 $statusIds = implode(', ', $unpaidStatus);
214 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
215 INNER JOIN civicrm_pledge {$this->_aliases['civicrm_pledge']}
216 ON ({$this->_aliases['civicrm_pledge']}.contact_id =
217 {$this->_aliases['civicrm_contact']}.id) AND
218 {$this->_aliases['civicrm_pledge']}.status_id IN ( {$statusIds} )\n";
220 // Note that the derived query protects us from providing inaccurate data in the edge case where pledge
221 // payments have been edited such that they are not in id order. This might be better as a temp table.
222 $this->_from
.= "LEFT JOIN (SELECT * FROM civicrm_pledge_payment ORDER BY scheduled_date) as {$this->_aliases['civicrm_pledge_payment']}
223 ON ({$this->_aliases['civicrm_pledge']}.id =
224 {$this->_aliases['civicrm_pledge_payment']}.pledge_id AND {$this->_aliases['civicrm_pledge_payment']}.status_id = {$pendingStatus} ) ";
226 $this->joinAddressFromContact();
227 $this->joinEmailFromContact();
231 public function groupBy() {
233 "{$this->_aliases['civicrm_pledge']}.contact_id",
234 "{$this->_aliases['civicrm_pledge']}.id",
235 "{$this->_aliases['civicrm_pledge']}.currency",
237 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
240 public function orderBy() {
241 $this->_orderBy
= "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_pledge']}.contact_id, {$this->_aliases['civicrm_pledge']}.id";
244 public function postProcess() {
245 // get the acl clauses built before we assemble the query
246 $this->buildACLClause($this->_aliases
['civicrm_contact']);
247 parent
::PostProcess();
251 * Alter display of rows.
253 * Iterate through the rows retrieved via SQL and make changes for display purposes,
254 * such as rendering contacts as links.
258 public function alterDisplay(&$rows) {
261 $display_flag = $prev_cid = $cid = 0;
263 foreach ($rows as $rowNum => $row) {
264 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
265 // don't repeat contact details if its same as the previous row
266 if (array_key_exists('civicrm_contact_id', $row)) {
267 if ($cid = $row['civicrm_contact_id']) {
272 if ($prev_cid == $cid) {
283 foreach ($row as $colName => $colVal) {
284 if (in_array($colName, $this->_noRepeats
)) {
285 unset($rows[$rowNum][$colName]);
294 //handle the Financial Type Ids
295 if (array_key_exists('civicrm_pledge_financial_type_id', $row)) {
296 if ($value = $row['civicrm_pledge_financial_type_id']) {
297 $rows[$rowNum]['civicrm_pledge_financial_type_id'] = CRM_Contribute_PseudoConstant
::financialType($value, FALSE);
302 //handle the Status Ids
303 if (array_key_exists('civicrm_pledge_status_id', $row)) {
304 if ($value = $row['civicrm_pledge_status_id']) {
305 $rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Core_PseudoConstant
::getLabel('CRM_Pledge_BAO_Pledge', 'status_id', $value);
310 // convert display name to links
311 if (array_key_exists('civicrm_contact_sort_name', $row) &&
312 array_key_exists('civicrm_contact_id', $row)
314 $url = CRM_Report_Utils_Report
::getNextUrl('pledge/summary',
315 'reset=1&force=1&id_op=eq&id_value=' .
316 $row['civicrm_contact_id'],
317 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
319 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
320 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Pledge Details for this contact");
324 // If using campaigns, convert campaign_id to campaign title
325 if (array_key_exists('civicrm_pledge_campaign_id', $row)) {
326 if ($value = $row['civicrm_pledge_campaign_id']) {
327 $rows[$rowNum]['civicrm_pledge_campaign_id'] = $this->campaigns
[$value];
332 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, NULL, NULL) ?
TRUE : $entryFound;
333 // skip looking further in rows, if first row itself doesn't
334 // have the column we need