Merge pull request #4961 from pratikshad/core-bugs
[civicrm-core.git] / CRM / Contribute / Form / ContributionView.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates form components for Payment-Instrument
38 *
39 */
40class CRM_Contribute_Form_ContributionView extends CRM_Core_Form {
41
42 /**
100fef9d 43 * Set variables up before form is built
6a488035
TO
44 *
45 * @return void
6a488035
TO
46 */
47 public function preProcess() {
353ffa53
TO
48 $id = $this->get('id');
49 $values = $ids = array();
50 $params = array('id' => $id);
6a488035
TO
51 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
52 $this->assign('context', $context);
53
54 CRM_Contribute_BAO_Contribution::getValues($params, $values, $ids);
6a488035 55 CRM_Contribute_BAO_Contribution::resolveDefaults($values);
914c8124 56 $cancelledStatus = TRUE;
d230e755 57 $status = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
914c8124
PB
58 if (CRM_Utils_Array::value('contribution_status_id', $values) == array_search('Cancelled', $status)) {
59 $cancelledStatus = FALSE;
60 }
d230e755 61 $this->assign('cancelledStatus', $cancelledStatus);
6a488035 62
a7488080 63 if (!empty($values['contribution_page_id'])) {
6a488035
TO
64 $contribPages = CRM_Contribute_PseudoConstant::contributionPage(NULL, TRUE);
65 $values['contribution_page_title'] = CRM_Utils_Array::value(CRM_Utils_Array::value('contribution_page_id', $values), $contribPages);
66 }
67
68 // get recieved into i.e to_financial_account_id from last trxn
69 $financialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($values['contribution_id'], 'DESC');
70 $values['to_financial_account'] = '';
a7488080 71 if (!empty($financialTrxnId['financialTrxnId'])) {
eea16664 72 $values['to_financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $financialTrxnId['financialTrxnId'], 'to_financial_account_id');
6a488035 73 if ($values['to_financial_account_id']) {
eea16664 74 $values['to_financial_account'] = CRM_Contribute_PseudoConstant::financialAccount($values['to_financial_account_id']);
6a488035
TO
75 }
76 }
eea16664 77
a7488080 78 if (!empty($values['contribution_recur_id'])) {
353ffa53 79 $sql = "SELECT installments, frequency_interval, frequency_unit FROM civicrm_contribution_recur WHERE id = %1";
6a488035 80 $params = array(1 => array($values['contribution_recur_id'], 'Integer'));
353ffa53 81 $dao = CRM_Core_DAO::executeQuery($sql, $params);
6a488035
TO
82 if ($dao->fetch()) {
83 $values['recur_installments'] = $dao->installments;
84 $values['recur_frequency_unit'] = $dao->frequency_unit;
85 $values['recur_frequency_interval'] = $dao->frequency_interval;
86 }
87 }
88
89 $groupTree = CRM_Core_BAO_CustomGroup::getTree('Contribution', $this, $id, 0, CRM_Utils_Array::value('financial_type_id', $values));
90 CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree);
91
92 $premiumId = NULL;
93 if ($id) {
94 $dao = new CRM_Contribute_DAO_ContributionProduct();
95 $dao->contribution_id = $id;
96 if ($dao->find(TRUE)) {
97 $premiumId = $dao->id;
98 $productID = $dao->product_id;
99 }
100 }
101
102 if ($premiumId) {
103 $productDAO = new CRM_Contribute_DAO_Product();
104 $productDAO->id = $productID;
105 $productDAO->find(TRUE);
106
107 $this->assign('premium', $productDAO->name);
108 $this->assign('option', $dao->product_option);
109 $this->assign('fulfilled', $dao->fulfilled_date);
110 }
111
112 // Get Note
113 $noteValue = CRM_Core_BAO_Note::getNote(CRM_Utils_Array::value('id', $values), 'civicrm_contribution');
114 $values['note'] = array_values($noteValue);
115
116 // show billing address location details, if exists
a7488080 117 if (!empty($values['address_id'])) {
6a488035
TO
118 $addressParams = array('id' => CRM_Utils_Array::value('address_id', $values));
119 $addressDetails = CRM_Core_BAO_Address::getValues($addressParams, FALSE, 'id');
120 $addressDetails = array_values($addressDetails);
121 $values['billing_address'] = $addressDetails[0]['display'];
122 }
123
b6545333 124 //assign soft credit record if exists.
125 $SCRecords = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($values['contribution_id'], TRUE);
00a1afe2 126 if (!empty($SCRecords['soft_credit'])) {
127 $this->assign('softContributions', $SCRecords['soft_credit']);
128 unset($SCRecords['soft_credit']);
129 }
b6545333 130
131 //assign pcp record if exists
132 foreach ($SCRecords as $name => $value) {
133 $this->assign($name, $value);
134 }
6a488035
TO
135
136 $lineItems = array();
137 if ($id) {
0d6f29cd 138 $lineItem = CRM_Price_BAO_LineItem::getLineItems($id, 'contribution', 1, TRUE, TRUE);
139 if (!empty($lineItem)) {
140 $lineItems[] = $lineItem;
141 }
142
6a488035
TO
143 }
144 $this->assign('lineItem', empty($lineItems) ? FALSE : $lineItems);
145 $values['totalAmount'] = $values['total_amount'];
146
147 //do check for campaigns
148 if ($campaignId = CRM_Utils_Array::value('campaign_id', $values)) {
149 $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($campaignId);
150 $values['campaign'] = $campaigns[$campaignId];
151 }
152
153 // assign values to the template
154 $this->assign($values);
874c9be7 155 $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings');
03b412ae
PB
156 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
157 $this->assign('invoicing', $invoicing);
158 if ($invoicing && isset($values['tax_amount'])) {
a32709be
PB
159 $this->assign('totalTaxAmount', $values['tax_amount']);
160 }
6a488035
TO
161
162 $displayName = CRM_Contact_BAO_Contact::displayName($values['contact_id']);
163 $this->assign('displayName', $displayName);
eea16664 164
6a488035
TO
165 // Check if this is default domain contact CRM-10482
166 if (CRM_Contact_BAO_Contact::checkDomainContact($values['contact_id'])) {
167 $displayName .= ' (' . ts('default organization') . ')';
168 }
169
170 // omitting contactImage from title for now since the summary overlay css doesn't work outside of our crm-container
86bfa4f6 171 CRM_Utils_System::setTitle(ts('View Contribution from') . ' ' . $displayName);
6a488035
TO
172
173 // add viewed contribution to recent items list
174 $url = CRM_Utils_System::url('civicrm/contact/view/contribution',
175 "action=view&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home"
176 );
177
178 $title = $displayName . ' - (' . CRM_Utils_Money::format($values['total_amount']) . ' ' . ' - ' . $values['financial_type'] . ')';
179
180 $recentOther = array();
181 if (CRM_Core_Permission::checkActionPermission('CiviContribute', CRM_Core_Action::UPDATE)) {
182 $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/contribution',
183 "action=update&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home"
184 );
185 }
186 if (CRM_Core_Permission::checkActionPermission('CiviContribute', CRM_Core_Action::DELETE)) {
187 $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/contribution',
188 "action=delete&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home"
189 );
190 }
191 CRM_Utils_Recent::add($title,
192 $url,
193 $values['id'],
194 'Contribution',
195 $values['contact_id'],
196 NULL,
197 $recentOther
198 );
199 }
200
201 /**
c490a46a 202 * Build the form object
6a488035 203 *
355ba699 204 * @return void
6a488035
TO
205 */
206 public function buildQuickForm() {
207
208 $this->addButtons(array(
209 array(
210 'type' => 'cancel',
211 'name' => ts('Done'),
212 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
213 'isDefault' => TRUE,
214 ),
215 )
216 );
217 }
218}