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 protected $_charts = [
20 'barChart' => 'Bar Chart',
21 'pieChart' => 'Pie Chart',
23 public $_drilldownReport = ['pledge/summary' => 'Link to Detail Report'];
25 protected $_customGroupExtends = [
32 public function __construct() {
34 'civicrm_contact' => [
35 'dao' => 'CRM_Contact_DAO_Contact',
38 'title' => ts('Constituent Name'),
47 'grouping' => 'contact-fields',
50 'dao' => 'CRM_Pledge_DAO_Pledge',
52 'pledge_create_date' => [
53 'title' => ts('Pledge Made'),
56 'financial_type_id' => [
57 'title' => ts('Financial Type'),
61 'title' => ts('Frequency Unit'),
64 'title' => ts('Installments'),
67 'title' => ts('Amount'),
69 'type' => CRM_Utils_Type
::T_MONEY
,
76 'title' => ts('Status'),
80 'pledge_create_date' => [
81 'title' => ts('Pledge Made'),
82 'operatorType' => CRM_Report_Form
::OP_DATE
,
85 'title' => ts('Pledged Amount'),
86 'operatorType' => CRM_Report_Form
::OP_INT
,
89 'title' => ts('Currency'),
90 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
91 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
93 'type' => CRM_Utils_Type
::T_STRING
,
95 'financial_type_id' => [
96 'title' => ts('Financial Type'),
97 'type' => CRM_Utils_Type
::T_INT
,
98 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
99 'options' => CRM_Contribute_PseudoConstant
::financialType(),
101 'pledge_status_id' => [
102 'name' => 'status_id',
103 'title' => ts('Pledge Status'),
104 'type' => CRM_Utils_Type
::T_INT
,
105 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
106 'options' => CRM_Core_OptionGroup
::values('pledge_status'),
109 'title' => ts('Installments'),
110 'type' => CRM_Utils_Type
::T_INT
,
113 'grouping' => 'pledge-fields',
115 'civicrm_pledge_payment' => [
116 'dao' => 'CRM_Pledge_DAO_PledgePayment',
118 'scheduled_date' => [
119 'title' => ts('Next Payment Due'),
120 'type' => CRM_Utils_Type
::T_DATE
,
123 'scheduled_amount' => [
124 'type' => CRM_Utils_Type
::T_MONEY
,
125 'title' => ts('Next Payment Amount'),
129 'scheduled_date' => [
130 'title' => ts('Next Payment Due'),
131 'operatorType' => CRM_Report_Form
::OP_DATE
,
132 'type' => CRM_Utils_Type
::T_DATE
,
135 'grouping' => 'pledge-fields',
137 'civicrm_address' => [
138 'dao' => 'CRM_Core_DAO_Address',
140 'street_address' => NULL,
142 'postal_code' => NULL,
143 'state_province_id' => [
144 'title' => ts('State/Province'),
147 'title' => ts('Country'),
151 'grouping' => 'contact-fields',
154 'dao' => 'CRM_Core_DAO_Email',
155 'fields' => ['email' => NULL],
156 'grouping' => 'contact-fields',
160 // If we have a campaign, build out the relevant elements
161 $this->addCampaignFields('civicrm_pledge');
163 $this->_groupFilter
= TRUE;
164 $this->_tagFilter
= TRUE;
165 $this->_currencyColumn
= 'civicrm_pledge_currency';
166 parent
::__construct();
169 public function preProcess() {
170 $this->assign('reportTitle', ts('Pledged but not Paid Report'));
171 parent
::preProcess();
174 public function select() {
176 $this->_columnHeaders
= [];
177 foreach ($this->_columns
as $tableName => $table) {
178 if (array_key_exists('fields', $table)) {
179 foreach ($table['fields'] as $fieldName => $field) {
180 if (!empty($field['required']) ||
181 !empty($this->_params
['fields'][$fieldName])
183 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
184 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'] ??
NULL;
185 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'] ??
NULL;
190 $this->_selectClauses
= $select;
191 $this->_select
= "SELECT " . implode(', ', $select) . " ";
194 public function from() {
197 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
198 $pendingStatus = array_search('Pending', $allStatus);
204 if ($key = CRM_Utils_Array
::key($statusKey, $allStatus)) {
205 $unpaidStatus[] = $key;
209 $statusIds = implode(', ', $unpaidStatus);
212 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
213 INNER JOIN civicrm_pledge {$this->_aliases['civicrm_pledge']}
214 ON ({$this->_aliases['civicrm_pledge']}.contact_id =
215 {$this->_aliases['civicrm_contact']}.id) AND
216 {$this->_aliases['civicrm_pledge']}.status_id IN ( {$statusIds} )\n";
218 // Note that the derived query protects us from providing inaccurate data in the edge case where pledge
219 // payments have been edited such that they are not in id order. This might be better as a temp table.
220 $this->_from
.= "LEFT JOIN (SELECT * FROM civicrm_pledge_payment ORDER BY scheduled_date) as {$this->_aliases['civicrm_pledge_payment']}
221 ON ({$this->_aliases['civicrm_pledge']}.id =
222 {$this->_aliases['civicrm_pledge_payment']}.pledge_id AND {$this->_aliases['civicrm_pledge_payment']}.status_id = {$pendingStatus} ) ";
224 $this->joinAddressFromContact();
225 $this->joinEmailFromContact();
229 public function groupBy() {
231 "{$this->_aliases['civicrm_pledge']}.contact_id",
232 "{$this->_aliases['civicrm_pledge']}.id",
233 "{$this->_aliases['civicrm_pledge']}.currency",
235 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
238 public function orderBy() {
239 $this->_orderBy
= "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_pledge']}.contact_id, {$this->_aliases['civicrm_pledge']}.id";
242 public function postProcess() {
243 // get the acl clauses built before we assemble the query
244 $this->buildACLClause($this->_aliases
['civicrm_contact']);
245 parent
::PostProcess();
249 * Alter display of rows.
251 * Iterate through the rows retrieved via SQL and make changes for display purposes,
252 * such as rendering contacts as links.
256 public function alterDisplay(&$rows) {
259 $display_flag = $prev_cid = $cid = 0;
261 foreach ($rows as $rowNum => $row) {
262 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
263 // don't repeat contact details if its same as the previous row
264 if (array_key_exists('civicrm_contact_id', $row)) {
265 if ($cid = $row['civicrm_contact_id']) {
270 if ($prev_cid == $cid) {
281 foreach ($row as $colName => $colVal) {
282 if (in_array($colName, $this->_noRepeats
)) {
283 unset($rows[$rowNum][$colName]);
292 //handle the Financial Type Ids
293 if (array_key_exists('civicrm_pledge_financial_type_id', $row)) {
294 if ($value = $row['civicrm_pledge_financial_type_id']) {
295 $rows[$rowNum]['civicrm_pledge_financial_type_id'] = CRM_Contribute_PseudoConstant
::financialType($value, FALSE);
300 //handle the Status Ids
301 if (array_key_exists('civicrm_pledge_status_id', $row)) {
302 if ($value = $row['civicrm_pledge_status_id']) {
303 $rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Core_PseudoConstant
::getLabel('CRM_Pledge_BAO_Pledge', 'status_id', $value);
308 // convert display name to links
309 if (array_key_exists('civicrm_contact_sort_name', $row) &&
310 array_key_exists('civicrm_contact_id', $row)
312 $url = CRM_Report_Utils_Report
::getNextUrl('pledge/summary',
313 'reset=1&force=1&id_op=eq&id_value=' .
314 $row['civicrm_contact_id'],
315 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
317 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
318 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Pledge Details for this contact");
322 // If using campaigns, convert campaign_id to campaign title
323 if (array_key_exists('civicrm_pledge_campaign_id', $row)) {
324 if ($value = $row['civicrm_pledge_campaign_id']) {
325 $rows[$rowNum]['civicrm_pledge_campaign_id'] = $this->campaigns
[$value];
330 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, NULL, NULL) ?
TRUE : $entryFound;
331 // skip looking further in rows, if first row itself doesn't
332 // have the column we need