Merge pull request #585 from agh1/fintyperel-relfirst
[civicrm-core.git] / CRM / Contribute / Form / ContributionView.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for Payment-Instrument
38 *
39 */
40 class CRM_Contribute_Form_ContributionView extends CRM_Core_Form {
41
42 /**
43 * Function to set variables up before form is built
44 *
45 * @return void
46 * @access public
47 */
48 public function preProcess() {
49 $id = $this->get('id');
50 $values = $ids = array();
51 $params = array('id' => $id);
52 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
53 $this->assign('context', $context);
54
55 CRM_Contribute_BAO_Contribution::getValues($params, $values, $ids);
56
57 $softParams = array('contribution_id' => CRM_Utils_Array::value('contribution_id', $values));
58 if ($softContribution = CRM_Contribute_BAO_Contribution::getSoftContribution($softParams, TRUE)) {
59 $values = array_merge($values, $softContribution);
60 }
61 CRM_Contribute_BAO_Contribution::resolveDefaults($values);
62
63 if (CRM_Utils_Array::value('contribution_page_id', $values)) {
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'] = '';
71 if (CRM_Utils_Array::value('financialTrxnId', $financialTrxnId)) {
72 $values['to_financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $financialTrxnId['financialTrxnId'], 'to_financial_account_id');
73 if ($values['to_financial_account_id']) {
74 $values['to_financial_account'] = CRM_Contribute_PseudoConstant::financialAccount($values['to_financial_account_id']);
75 }
76 }
77
78 if (CRM_Utils_Array::value('honor_contact_id', $values)) {
79 $sql = "SELECT display_name FROM civicrm_contact WHERE id = %1";
80 $params = array(1 => array($values['honor_contact_id'], 'Integer'));
81 $dao = CRM_Core_DAO::executeQuery($sql, $params);
82 if ($dao->fetch()) {
83 $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=$values[honor_contact_id]");
84 $values['honor_display'] = "<A href = $url>" . $dao->display_name . "</A>";
85 }
86 $honor = CRM_Core_PseudoConstant::honor();
87 $values['honor_type'] = CRM_Utils_Array::value(CRM_Utils_Array::value('honor_type_id', $values), $honor);
88 }
89
90 if (CRM_Utils_Array::value('contribution_recur_id', $values)) {
91 $sql = "SELECT installments, frequency_interval, frequency_unit FROM civicrm_contribution_recur WHERE id = %1";
92 $params = array(1 => array($values['contribution_recur_id'], 'Integer'));
93 $dao = CRM_Core_DAO::executeQuery($sql, $params);
94 if ($dao->fetch()) {
95 $values['recur_installments'] = $dao->installments;
96 $values['recur_frequency_unit'] = $dao->frequency_unit;
97 $values['recur_frequency_interval'] = $dao->frequency_interval;
98 }
99 }
100
101 $groupTree = CRM_Core_BAO_CustomGroup::getTree('Contribution', $this, $id, 0, CRM_Utils_Array::value('financial_type_id', $values));
102 CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree);
103
104 $premiumId = NULL;
105 if ($id) {
106 $dao = new CRM_Contribute_DAO_ContributionProduct();
107 $dao->contribution_id = $id;
108 if ($dao->find(TRUE)) {
109 $premiumId = $dao->id;
110 $productID = $dao->product_id;
111 }
112 }
113
114 if ($premiumId) {
115 $productDAO = new CRM_Contribute_DAO_Product();
116 $productDAO->id = $productID;
117 $productDAO->find(TRUE);
118
119 $this->assign('premium', $productDAO->name);
120 $this->assign('option', $dao->product_option);
121 $this->assign('fulfilled', $dao->fulfilled_date);
122 }
123
124 // Get Note
125 $noteValue = CRM_Core_BAO_Note::getNote(CRM_Utils_Array::value('id', $values), 'civicrm_contribution');
126 $values['note'] = array_values($noteValue);
127
128 // show billing address location details, if exists
129 if (CRM_Utils_Array::value('address_id', $values)) {
130 $addressParams = array('id' => CRM_Utils_Array::value('address_id', $values));
131 $addressDetails = CRM_Core_BAO_Address::getValues($addressParams, FALSE, 'id');
132 $addressDetails = array_values($addressDetails);
133 $values['billing_address'] = $addressDetails[0]['display'];
134 }
135
136 //get soft credit record if exists.
137 if ($softContribution = CRM_Contribute_BAO_Contribution::getSoftContribution($softParams)) {
138
139 $softContribution['softCreditToName'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
140 $softContribution['soft_credit_to'], 'display_name'
141 );
142 //hack to avoid dispalyName conflict
143 //for viewing softcredit record.
144 $softContribution['displayName'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
145 $values['contact_id'], 'display_name'
146 );
147 $values = array_merge($values, $softContribution);
148 }
149
150 $lineItems = array();
151 if ($id) {
152 $lineItem = CRM_Price_BAO_LineItem::getLineItems($id, 'contribution', 1);
153 empty($lineItem) ? null :$lineItems[] = $lineItem;
154 }
155 $this->assign('lineItem', empty($lineItems) ? FALSE : $lineItems);
156 $values['totalAmount'] = $values['total_amount'];
157
158 //do check for campaigns
159 if ($campaignId = CRM_Utils_Array::value('campaign_id', $values)) {
160 $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($campaignId);
161 $values['campaign'] = $campaigns[$campaignId];
162 }
163
164 // assign values to the template
165 $this->assign($values);
166
167 $displayName = CRM_Contact_BAO_Contact::displayName($values['contact_id']);
168 $this->assign('displayName', $displayName);
169
170 // Check if this is default domain contact CRM-10482
171 if (CRM_Contact_BAO_Contact::checkDomainContact($values['contact_id'])) {
172 $displayName .= ' (' . ts('default organization') . ')';
173 }
174
175 // omitting contactImage from title for now since the summary overlay css doesn't work outside of our crm-container
176 CRM_Utils_System::setTitle(ts('View Contribution from') . ' ' . $displayName);
177
178 // add viewed contribution to recent items list
179 $url = CRM_Utils_System::url('civicrm/contact/view/contribution',
180 "action=view&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home"
181 );
182
183 $title = $displayName . ' - (' . CRM_Utils_Money::format($values['total_amount']) . ' ' . ' - ' . $values['financial_type'] . ')';
184
185 $recentOther = array();
186 if (CRM_Core_Permission::checkActionPermission('CiviContribute', CRM_Core_Action::UPDATE)) {
187 $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/contribution',
188 "action=update&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home"
189 );
190 }
191 if (CRM_Core_Permission::checkActionPermission('CiviContribute', CRM_Core_Action::DELETE)) {
192 $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/contribution',
193 "action=delete&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home"
194 );
195 }
196 CRM_Utils_Recent::add($title,
197 $url,
198 $values['id'],
199 'Contribution',
200 $values['contact_id'],
201 NULL,
202 $recentOther
203 );
204 }
205
206 /**
207 * Function to build the form
208 *
209 * @return None
210 * @access public
211 */
212 public function buildQuickForm() {
213
214 $this->addButtons(array(
215 array(
216 'type' => 'cancel',
217 'name' => ts('Done'),
218 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
219 'isDefault' => TRUE,
220 ),
221 )
222 );
223 }
224 }
225