X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FReport%2FForm%2FContribute%2FHistory.php;h=b10f6f3b00c56847a7eef496f5453d5687337ca9;hb=9d72cede30c1a6986256fba4cac6b28ea963f543;hp=37a0e4e63e8db2158b8aa6610c13421de5a29812;hpb=a760e4816e466882e5de0bb038ca99988d750320;p=civicrm-core.git diff --git a/CRM/Report/Form/Contribute/History.php b/CRM/Report/Form/Contribute/History.php index 37a0e4e63e..b10f6f3b00 100644 --- a/CRM/Report/Form/Contribute/History.php +++ b/CRM/Report/Form/Contribute/History.php @@ -59,9 +59,9 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form { */ function __construct() { $yearsInPast = 4; - $date = CRM_Core_SelectValues::date('custom', NULL, $yearsInPast, 0); - $count = $date['maxYear']; - $optionYear = array('' => ts('-- select --')); + $date = CRM_Core_SelectValues::date('custom', NULL, $yearsInPast, 0); + $count = $date['maxYear']; + $optionYear = array('' => ts('- select -')); $this->_yearStatisticsFrom = $date['minYear']; $this->_yearStatisticsTo = $date['maxYear']; @@ -83,157 +83,135 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form { } $this->_columns = array( - 'civicrm_contact' => - array( - 'dao' => 'CRM_Contact_DAO_Contact', - 'fields' => - array( - 'sort_name' => - array('title' => ts('Contact Name'), - 'default' => TRUE, - 'required' => TRUE, - 'no_repeat' => TRUE, + 'civicrm_contact' => array( + 'dao' => 'CRM_Contact_DAO_Contact', + 'fields' => array( + 'sort_name' => array( + 'title' => ts('Contact Name'), + 'default' => TRUE, + 'required' => TRUE, + 'no_repeat' => TRUE, + ), + 'id' => array( + 'no_display' => TRUE, + 'default' => TRUE, + 'required' => TRUE, + ), + 'contact_type' => array( + 'title' => ts('Contact Type'), + ), + 'contact_sub_type' => array( + 'title' => ts('Contact Subtype'), + ), ), - 'id' => - array( - 'no_display' => TRUE, - 'default' => TRUE, - 'required' => TRUE, - ), - 'contact_type' => - array( - 'title' => ts('Contact Type'), - ), - 'contact_sub_type' => - array( - 'title' => ts('Contact SubType'), + 'grouping' => 'contact-fields', + 'filters' => array( + 'sort_name' => array('title' => ts('Contact Name')), + 'id' => array( + 'title' => ts('Contact ID'), + 'no_display' => TRUE, + ), ), ), - 'grouping' => 'contact-fields', - 'filters' => - array( - 'sort_name' => - array('title' => ts('Contact Name')), - 'id' => - array('title' => ts('Contact ID'), - 'no_display' => TRUE, + 'civicrm_email' => array( + 'dao' => 'CRM_Core_DAO_Email', + 'fields' => array( + 'email' => array( + 'title' => ts('Email'), + 'no_repeat' => TRUE, + ), ), + 'grouping' => 'contact-fields', ), - ), - 'civicrm_email' => - array( - 'dao' => 'CRM_Core_DAO_Email', - 'fields' => - array( - 'email' => - array('title' => ts('Email'), - 'no_repeat' => TRUE, + 'civicrm_phone' => array( + 'dao' => 'CRM_Core_DAO_Phone', + 'fields' => array( + 'phone' => array( + 'title' => ts('Phone'), + 'no_repeat' => TRUE, + ), ), + 'grouping' => 'contact-fields', ), - 'grouping' => 'contact-fields', - ), - 'civicrm_phone' => - array( - 'dao' => 'CRM_Core_DAO_Phone', - 'fields' => - array( - 'phone' => - array('title' => ts('Phone'), - 'no_repeat' => TRUE, + ) + $this->addAddressFields(FALSE, FALSE, FALSE, array()) + array( + 'civicrm_relationship' => array( + 'dao' => 'CRM_Contact_DAO_Relationship', + 'fields' => array( + 'relationship_type_id' => array( + 'title' => ts('Relationship Type'), + 'default' => TRUE, + ), + 'contact_id_a' => array('no_display' => TRUE), + 'contact_id_b' => array('no_display' => TRUE), ), - ), - 'grouping' => 'contact-fields', - ), - ) + $this->addAddressFields(FALSE, FALSE, FALSE, array( - )) + array('civicrm_relationship' => - array( - 'dao' => 'CRM_Contact_DAO_Relationship', - 'fields' => - array( - 'relationship_type_id' => - array('title' => ts('Relationship Type'), - 'default' => TRUE, - ), - 'contact_id_a' => - array('no_display' => TRUE), - 'contact_id_b' => - array('no_display' => TRUE), - ), - 'filters' => - array( - 'relationship_type_id' => - array('title' => ts('Relationship Type'), - 'operatorType' => CRM_Report_Form::OP_MULTISELECT, - 'options' => $relationTypeOp, - 'type' => CRM_Utils_Type::T_STRING, + 'filters' => array( + 'relationship_type_id' => array( + 'title' => ts('Relationship Type'), + 'operatorType' => CRM_Report_Form::OP_MULTISELECT, + 'options' => $relationTypeOp, + 'type' => CRM_Utils_Type::T_STRING, + ), ), ), - ), - ) + array( - 'civicrm_contribution' => - array( - 'dao' => 'CRM_Contribute_DAO_Contribution', - 'fields' => - array( - 'total_amount' => - array('title' => ts('Amount Statistics'), - 'default' => TRUE, - 'required' => TRUE, - 'no_display' => TRUE, - 'statistics' => - array('sum' => ts('Aggregate Amount')), - ), - 'receive_date' => - array( - 'required' => TRUE, - 'default' => TRUE, - 'no_display' => TRUE, - ), - ), - 'grouping' => 'contri-fields', - 'filters' => - array( - 'this_year' => - array( - 'title' => ts('This Year'), - 'operatorType' => CRM_Report_Form::OP_SELECT, - 'options' => $optionYear, - 'default' => '', - ), - 'other_year' => - array( - 'title' => ts('Other Years'), - 'operatorType' => CRM_Report_Form::OP_SELECT, - 'options' => $optionYear, - 'default' => '', + ) + array( + 'civicrm_contribution' => array( + 'dao' => 'CRM_Contribute_DAO_Contribution', + 'fields' => array( + 'total_amount' => array( + 'title' => ts('Amount Statistics'), + 'default' => TRUE, + 'required' => TRUE, + 'no_display' => TRUE, + 'statistics' => array('sum' => ts('Aggregate Amount')), + ), + 'receive_date' => array( + 'required' => TRUE, + 'default' => TRUE, + 'no_display' => TRUE, + ), ), - 'receive_date' => - array('operatorType' => CRM_Report_Form::OP_DATE), - 'contribution_status_id' => - array('title' => ts('Donation Status'), - 'operatorType' => CRM_Report_Form::OP_MULTISELECT, - 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), - 'default' => array(1), - ), - 'financial_type_id' => array( - 'title' => ts('Financial Type'), - 'operatorType' => CRM_Report_Form::OP_MULTISELECT, - 'options' => CRM_Contribute_PseudoConstant::financialType(), - ), - 'total_amount' => - array('title' => ts('Donation Amount'), - ), - 'total_sum' => - array('title' => ts('Aggregate Amount'), - 'type' => CRM_Report_Form::OP_INT, - 'dbAlias' => 'civicrm_contribution_total_amount_sum', - 'having' => TRUE, + 'grouping' => 'contri-fields', + 'filters' => array( + 'this_year' => array( + 'title' => ts('This Year'), + 'operatorType' => CRM_Report_Form::OP_SELECT, + 'options' => $optionYear, + 'default' => '', + ), + 'other_year' => array( + 'title' => ts('Other Years'), + 'operatorType' => CRM_Report_Form::OP_SELECT, + 'options' => $optionYear, + 'default' => '', + ), + 'receive_date' => array('operatorType' => CRM_Report_Form::OP_DATE), + 'contribution_status_id' => array( + 'title' => ts('Contribution Status'), + 'operatorType' => CRM_Report_Form::OP_MULTISELECT, + 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), + 'default' => array(1), + ), + 'financial_type_id' => array( + 'title' => ts('Financial Type'), + 'operatorType' => CRM_Report_Form::OP_MULTISELECT, + 'options' => CRM_Contribute_PseudoConstant::financialType(), + ), + 'total_amount' => array( + 'title' => ts('Contribution Amount'), + ), + 'total_sum' => array( + 'title' => ts('Aggregate Amount'), + 'type' => CRM_Report_Form::OP_INT, + 'dbAlias' => 'civicrm_contribution_total_amount_sum', + 'having' => TRUE, + ), ), ), - ), - ); + ); - $this->_columns['civicrm_contribution']['fields']['civicrm_upto_' . $this->_yearStatisticsFrom] = array('title' => ts('Up To %1 Donation', array(1 => $this->_yearStatisticsFrom)), + $this->_columns['civicrm_contribution']['fields']['civicrm_upto_' . + $this->_yearStatisticsFrom] = array( + 'title' => ts('Up To %1 Donation', array(1 => $this->_yearStatisticsFrom)), 'default' => TRUE, 'type' => CRM_Utils_Type::T_MONEY, 'is_statistics' => TRUE, @@ -242,7 +220,8 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form { $yearConter = $this->_yearStatisticsFrom; $yearConter++; while ($yearConter <= $this->_yearStatisticsTo) { - $this->_columns['civicrm_contribution']['fields'][$yearConter] = array('title' => ts('%1 Donation', array(1 => $yearConter)), + $this->_columns['civicrm_contribution']['fields'][$yearConter] = array( + 'title' => ts('%1 Donation', array(1 => $yearConter)), 'default' => TRUE, 'type' => CRM_Utils_Type::T_MONEY, 'is_statistics' => TRUE, @@ -250,7 +229,8 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form { $yearConter++; } - $this->_columns['civicrm_contribution']['fields']['aggregate_amount'] = array('title' => ts('Aggregate Amount'), + $this->_columns['civicrm_contribution']['fields']['aggregate_amount'] = array( + 'title' => ts('Aggregate Amount'), 'type' => CRM_Utils_Type::T_MONEY, 'is_statistics' => TRUE, ); @@ -272,7 +252,9 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form { if (array_key_exists('fields', $table)) { foreach ($table['fields'] as $fieldName => $field) { - if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) { + if (!empty($field['required']) || + !empty($this->_params['fields'][$fieldName]) + ) { if ($tableName == 'civicrm_address') { $this->_addressField = TRUE; } @@ -295,12 +277,18 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form { continue; } elseif ($fieldName == 'receive_date') { - if ((CRM_Utils_Array::value('this_year_op', $this->_params) == 'fiscal' && !empty($this->_params['this_year_value'])) || - (CRM_Utils_Array::value('other_year_op', $this->_params == 'fiscal') && !empty($this->_params['other_year_value']))){ - $select[] = self::fiscalYearOffset($field['dbAlias']) . " as {$tableName}_{$fieldName}"; - }else{ - $select[] = " YEAR(".$field['dbAlias'].")" . " as {$tableName}_{$fieldName}"; - } + if ((CRM_Utils_Array::value('this_year_op', $this->_params) == + 'fiscal' && !empty($this->_params['this_year_value'])) || + (CRM_Utils_Array::value('other_year_op', $this->_params == + 'fiscal') && !empty($this->_params['other_year_value'])) + ) { + $select[] = self::fiscalYearOffset($field['dbAlias']) . + " as {$tableName}_{$fieldName}"; + } + else { + $select[] = " YEAR(" . $field['dbAlias'] . ")" . + " as {$tableName}_{$fieldName}"; + } } elseif ($fieldName == 'total_amount') { $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}"; @@ -364,10 +352,13 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form { $clause = NULL; if ($fieldName == 'this_year' || $fieldName == 'other_year') { continue; - }elseif (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) { + } + elseif (CRM_Utils_Array::value('type', $field) & + CRM_Utils_Type::T_DATE + ) { $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); - $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); - $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); + $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); + $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); } @@ -422,7 +413,8 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form { } if (!empty($relationshipWhere)) { - $this->_relationshipWhere = ' AND ' . implode(' AND ', $relationshipWhere); + $this->_relationshipWhere = ' AND ' . + implode(' AND ', $relationshipWhere); } } @@ -459,12 +451,14 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form { $count++; } } - $statistics['counts']['rowCount'] = array('title' => ts('Primary Contact(s) Listed'), + $statistics['counts']['rowCount'] = array( + 'title' => ts('Primary Contact(s) Listed'), 'value' => $count, ); if ($this->_rowsFound && ($this->_rowsFound > $count)) { - $statistics['counts']['rowsFound'] = array('title' => ts('Total Primary Contact(s)'), + $statistics['counts']['rowsFound'] = array( + 'title' => ts('Total Primary Contact(s)'), 'value' => $this->_rowsFound, ); } @@ -481,7 +475,8 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form { */ static function formRule($fields, $files, $self) { $errors = array(); - if (!empty($fields['this_year_value']) && !empty($fields['other_year_value']) && + if (!empty($fields['this_year_value']) && + !empty($fields['other_year_value']) && ($fields['this_year_value'] == $fields['other_year_value']) ) { $errors['other_year_value'] = ts("Value for filters 'This Year' and 'Other Years' can not be same."); @@ -501,11 +496,12 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form { $this->customDataFrom(); $this->groupBy(); + $sql = NULL; $rows = array(); // build array of result based on column headers. This method also allows // modifying column headers before using it to build result set i.e $rows. - $this->buildRows($rows); + $this->buildRows($sql, $rows); // format result set. $this->formatDisplay($rows, FALSE); @@ -529,18 +525,20 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form { /** * @param $rows */ - function buildRows(&$rows) { + function buildRows($sql, &$rows) { $contactIds = array(); $addWhere = ''; if (!empty($this->_referenceYear['other_year'])) { - (CRM_Utils_Array::value('other_year_op', $this->_params) == 'calendar') ? $other_receive_date = 'YEAR (contri.receive_date)' : $other_receive_date = self::fiscalYearOffset('contri.receive_date'); - $addWhere .= " AND {$this->_aliases['civicrm_contact']}.id NOT IN ( SELECT DISTINCT cont.id FROM civicrm_contact cont, civicrm_contribution contri WHERE cont.id = contri.contact_id AND {$other_receive_date} = {$this->_referenceYear['other_year']} AND contri.is_test = 0 ) "; + (CRM_Utils_Array::value('other_year_op', $this->_params) == + 'calendar') ? $other_receive_date = 'YEAR (contri.receive_date)' : $other_receive_date = self::fiscalYearOffset('contri.receive_date'); + $addWhere .= " AND {$this->_aliases['civicrm_contact']}.id NOT IN ( SELECT DISTINCT cont.id FROM civicrm_contact cont, civicrm_contribution contri WHERE cont.id = contri.contact_id AND {$other_receive_date} = {$this->_referenceYear['other_year']} AND contri.is_test = 0 ) "; } if (!empty($this->_referenceYear['this_year'])) { - (CRM_Utils_Array::value('this_year_op', $this->_params) == 'calendar') ? $receive_date = 'YEAR (contri.receive_date)' : $receive_date = self::fiscalYearOffset('contri.receive_date'); - $addWhere .= " AND {$this->_aliases['civicrm_contact']}.id IN ( SELECT DISTINCT cont.id FROM civicrm_contact cont, civicrm_contribution contri WHERE cont.id = contri.contact_id AND {$receive_date} = {$this->_referenceYear['this_year']} AND contri.is_test = 0 ) "; + (CRM_Utils_Array::value('this_year_op', $this->_params) == + 'calendar') ? $receive_date = 'YEAR (contri.receive_date)' : $receive_date = self::fiscalYearOffset('contri.receive_date'); + $addWhere .= " AND {$this->_aliases['civicrm_contact']}.id IN ( SELECT DISTINCT cont.id FROM civicrm_contact cont, civicrm_contribution contri WHERE cont.id = contri.contact_id AND {$receive_date} = {$this->_referenceYear['this_year']} AND contri.is_test = 0 ) "; } $this->limit(); $getContacts = "SELECT SQL_CALC_FOUND_ROWS {$this->_aliases['civicrm_contact']}.id as cid, SUM({$this->_aliases['civicrm_contribution']}.total_amount) as civicrm_contribution_total_amount_sum {$this->_from} {$this->_where} {$addWhere} GROUP BY {$this->_aliases['civicrm_contact']}.id {$this->_having} {$this->_limit}"; @@ -569,9 +567,9 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form { $relatedContributions = $this->buildContributionRows($relatedContactIds); - $summaryYears = array(); + $summaryYears = array(); $summaryYears[] = "civicrm_upto_{$this->_yearStatisticsFrom}"; - $yearConter = $this->_yearStatisticsFrom; + $yearConter = $this->_yearStatisticsFrom; $yearConter++; while ($yearConter <= $this->_yearStatisticsTo) { $summaryYears[] = $yearConter; @@ -628,11 +626,14 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form { return $rows; } - $sqlContribution = "{$this->_select} {$this->_from} WHERE {$this->_aliases['civicrm_contact']}.id IN (" . implode(',', $contactIds) . ") AND {$this->_aliases['civicrm_contribution']}.is_test = 0 {$this->_statusClause} {$this->_groupBy} "; + $sqlContribution = + "{$this->_select} {$this->_from} WHERE {$this->_aliases['civicrm_contact']}.id IN (" . + implode(',', $contactIds) . + ") AND {$this->_aliases['civicrm_contribution']}.is_test = 0 {$this->_statusClause} {$this->_groupBy} "; - $dao = CRM_Core_DAO::executeQuery($sqlContribution); + $dao = CRM_Core_DAO::executeQuery($sqlContribution); $contributionSum = 0; - $yearcal = array(); + $yearcal = array(); while ($dao->fetch()) { if (!$dao->civicrm_contact_id) { continue; @@ -644,7 +645,9 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form { } } if ($dao->civicrm_contribution_receive_date) { - if ($dao->civicrm_contribution_receive_date > $this->_yearStatisticsFrom) { + if ($dao->civicrm_contribution_receive_date > + $this->_yearStatisticsFrom + ) { $rows[$dao->civicrm_contact_id][$dao->civicrm_contribution_receive_date] = $dao->civicrm_contribution_total_amount; } else { @@ -681,7 +684,10 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form { if (!empty($this->_relationshipColumns)) { $addRelSelect = ', ' . implode(', ', $this->_relationshipColumns); } - $sqlRelationship = "SELECT {$this->_aliases['civicrm_relationship']}.relationship_type_id as relationship_type_id, {$this->_aliases['civicrm_relationship']}.contact_id_a as contact_id_a, {$this->_aliases['civicrm_relationship']}.contact_id_b as contact_id_b {$addRelSelect} FROM civicrm_contact {$relContactAlias} {$this->_relationshipFrom} WHERE {$relContactAlias}.id IN (" . implode(',', $contactIds) . ") AND {$this->_aliases['civicrm_relationship']}.is_active = 1 {$this->_relationshipWhere} GROUP BY {$this->_aliases['civicrm_relationship']}.contact_id_a, {$this->_aliases['civicrm_relationship']}.contact_id_b"; + $sqlRelationship = + "SELECT {$this->_aliases['civicrm_relationship']}.relationship_type_id as relationship_type_id, {$this->_aliases['civicrm_relationship']}.contact_id_a as contact_id_a, {$this->_aliases['civicrm_relationship']}.contact_id_b as contact_id_b {$addRelSelect} FROM civicrm_contact {$relContactAlias} {$this->_relationshipFrom} WHERE {$relContactAlias}.id IN (" . + implode(',', $contactIds) . + ") AND {$this->_aliases['civicrm_relationship']}.is_active = 1 {$this->_relationshipWhere} GROUP BY {$this->_aliases['civicrm_relationship']}.contact_id_a, {$this->_aliases['civicrm_relationship']}.contact_id_b"; $relationshipTypes = CRM_Core_PseudoConstant::relationshipType(); $dao = CRM_Core_DAO::executeQuery($sqlRelationship); @@ -716,7 +722,10 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form { */ function getOperationPair($type = "string", $fieldName = NULL) { if ($fieldName == 'this_year' || $fieldName == 'other_year') { - return array('calendar' => ts('Is Calendar Year'), 'fiscal' => ts('Fiscal Year Starting')); + return array( + 'calendar' => ts('Is Calendar Year'), + 'fiscal' => ts('Fiscal Year Starting') + ); } return parent::getOperationPair($type, $fieldName); } @@ -757,7 +766,9 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form { } // Convert Display name into link - if (!empty($row['civicrm_contact_sort_name']) && !empty($row['civicrm_contact_id'])) { + if (!empty($row['civicrm_contact_sort_name']) && + !empty($row['civicrm_contact_id']) + ) { $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'], $this->_absoluteUrl, $this->_id @@ -767,5 +778,5 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form { } } } - } +}