CIVI-28 Added check for lineitems edit and delete
[civicrm-core.git] / CRM / Contribute / Form / ContributionView.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 */
33
34 /**
35 * This class generates form components for Payment-Instrument.
36 */
37 class CRM_Contribute_Form_ContributionView extends CRM_Core_Form {
38
39 /**
40 * Set variables up before form is built.
41 */
42 public function preProcess() {
43 $id = $this->get('id');
44 $values = $ids = array();
45 $params = array('id' => $id);
46 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
47 $this->assign('context', $context);
48
49 CRM_Contribute_BAO_Contribution::getValues($params, $values, $ids);
50 if ($this->_action & CRM_Core_Action::VIEW) {
51 $financialTypeID = CRM_Contribute_PseudoConstant::financialType($values['financial_type_id']);
52 CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($id, 'view');
53 if (CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($id, 'edit', FALSE)) {
54 $this->assign('canEdit', TRUE);
55 }
56 if (CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($id, 'delete', FALSE)) {
57 $this->assign('canDelete', TRUE);
58 }
59 if (!CRM_Core_Permission::check('view contributions of type ' . $financialTypeID)) {
60 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
61 }
62 }
63 CRM_Contribute_BAO_Contribution::resolveDefaults($values);
64 $cancelledStatus = TRUE;
65 $status = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
66 if (CRM_Utils_Array::value('contribution_status_id', $values) == array_search('Cancelled', $status)) {
67 $cancelledStatus = FALSE;
68 }
69 $this->assign('cancelledStatus', $cancelledStatus);
70
71 if (!empty($values['contribution_page_id'])) {
72 $contribPages = CRM_Contribute_PseudoConstant::contributionPage(NULL, TRUE);
73 $values['contribution_page_title'] = CRM_Utils_Array::value(CRM_Utils_Array::value('contribution_page_id', $values), $contribPages);
74 }
75
76 // get received into i.e to_financial_account_id from last trxn
77 $financialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($values['contribution_id'], 'DESC');
78 $values['to_financial_account'] = '';
79 if (!empty($financialTrxnId['financialTrxnId'])) {
80 $values['to_financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $financialTrxnId['financialTrxnId'], 'to_financial_account_id');
81 if ($values['to_financial_account_id']) {
82 $values['to_financial_account'] = CRM_Contribute_PseudoConstant::financialAccount($values['to_financial_account_id']);
83 }
84 $values['payment_processor_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $financialTrxnId['financialTrxnId'], 'payment_processor_id');
85 if ($values['payment_processor_id']) {
86 $values['payment_processor_name'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessor', $values['payment_processor_id'], 'name');
87 }
88 }
89
90 if (!empty($values['contribution_recur_id'])) {
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 (!empty($values['address_id'])) {
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 //assign soft credit record if exists.
137 $SCRecords = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($values['contribution_id'], TRUE);
138 if (!empty($SCRecords['soft_credit'])) {
139 $this->assign('softContributions', $SCRecords['soft_credit']);
140 unset($SCRecords['soft_credit']);
141 }
142
143 //assign pcp record if exists
144 foreach ($SCRecords as $name => $value) {
145 $this->assign($name, $value);
146 }
147
148 $lineItems = array();
149 if ($id) {
150 $lineItem = CRM_Price_BAO_LineItem::getLineItems($id, 'contribution', 1, TRUE, TRUE);
151 if (!empty($lineItem)) {
152 $lineItems[] = $lineItem;
153 }
154
155 }
156 $this->assign('lineItem', empty($lineItems) ? FALSE : $lineItems);
157 $values['totalAmount'] = $values['total_amount'];
158
159 //do check for campaigns
160 if ($campaignId = CRM_Utils_Array::value('campaign_id', $values)) {
161 $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($campaignId);
162 $values['campaign'] = $campaigns[$campaignId];
163 }
164
165 // assign values to the template
166 $this->assign($values);
167 $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings');
168 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
169 $this->assign('invoicing', $invoicing);
170 if ($invoicing && isset($values['tax_amount'])) {
171 $this->assign('totalTaxAmount', $values['tax_amount']);
172 }
173
174 $displayName = CRM_Contact_BAO_Contact::displayName($values['contact_id']);
175 $this->assign('displayName', $displayName);
176
177 // Check if this is default domain contact CRM-10482
178 if (CRM_Contact_BAO_Contact::checkDomainContact($values['contact_id'])) {
179 $displayName .= ' (' . ts('default organization') . ')';
180 }
181
182 // omitting contactImage from title for now since the summary overlay css doesn't work outside of our crm-container
183 CRM_Utils_System::setTitle(ts('View Contribution from') . ' ' . $displayName);
184
185 // add viewed contribution to recent items list
186 $url = CRM_Utils_System::url('civicrm/contact/view/contribution',
187 "action=view&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home"
188 );
189
190 $title = $displayName . ' - (' . CRM_Utils_Money::format($values['total_amount']) . ' ' . ' - ' . $values['financial_type'] . ')';
191
192 $recentOther = array();
193 if (CRM_Core_Permission::checkActionPermission('CiviContribute', CRM_Core_Action::UPDATE)) {
194 $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/contribution',
195 "action=update&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home"
196 );
197 }
198 if (CRM_Core_Permission::checkActionPermission('CiviContribute', CRM_Core_Action::DELETE)) {
199 $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/contribution',
200 "action=delete&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home"
201 );
202 }
203 CRM_Utils_Recent::add($title,
204 $url,
205 $values['id'],
206 'Contribution',
207 $values['contact_id'],
208 NULL,
209 $recentOther
210 );
211 }
212
213 /**
214 * Build the form object.
215 */
216 public function buildQuickForm() {
217
218 $this->addButtons(array(
219 array(
220 'type' => 'cancel',
221 'name' => ts('Done'),
222 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
223 'isDefault' => TRUE,
224 ),
225 )
226 );
227 }
228
229 }