CRM-16189, added code to validate financial type for contribution
[civicrm-core.git] / CRM / Contribute / Form / Task / PDF.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 */
33
34/**
35 * This class provides the functionality to email a group of
36 * contacts.
37 */
38class CRM_Contribute_Form_Task_PDF extends CRM_Contribute_Form_Task {
39
40 /**
41 * Are we operating in "single mode", i.e. updating the task of only
42 * one specific contribution?
43 *
44 * @var boolean
45 */
46 public $_single = FALSE;
47
48 protected $_rows;
49
50 /**
fe482240 51 * Build all the data structures needed to build the form.
95ea96be 52 */
389bcebf 53 public function preProcess() {
6a488035
TO
54 $id = CRM_Utils_Request::retrieve('id', 'Positive',
55 $this, FALSE
56 );
57
58 if ($id) {
59 $this->_contributionIds = array($id);
60 $this->_componentClause = " civicrm_contribution.id IN ( $id ) ";
61 $this->_single = TRUE;
62 $this->assign('totalSelectedContributions', 1);
63 }
64 else {
65 parent::preProcess();
66 }
67
68 // check that all the contribution ids have pending status
69 $query = "
70SELECT count(*)
71FROM civicrm_contribution
72WHERE contribution_status_id != 1
73AND {$this->_componentClause}";
74 $count = CRM_Core_DAO::singleValueQuery($query);
75 if ($count != 0) {
76 CRM_Core_Error::statusBounce("Please select only online contributions with Completed status.");
77 }
78
79 // we have all the contribution ids, so now we get the contact ids
80 parent::setContactIDs();
81 $this->assign('single', $this->_single);
82
83 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
84 $urlParams = 'force=1';
85 if (CRM_Utils_Rule::qfKey($qfKey)) {
86 $urlParams .= "&qfKey=$qfKey";
87 }
88
89 $url = CRM_Utils_System::url('civicrm/contribute/search', $urlParams);
90 $breadCrumb = array(
874c9be7 91 array(
353ffa53 92 'url' => $url,
6a488035 93 'title' => ts('Search Results'),
389bcebf 94 ),
353ffa53 95 );
6a488035
TO
96
97 CRM_Utils_System::appendBreadCrumb($breadCrumb);
98 CRM_Utils_System::setTitle(ts('Print Contribution Receipts'));
99 }
100
101 /**
fe482240 102 * Build the form object.
6a488035
TO
103 */
104 public function buildQuickForm() {
105
106 $this->addElement('radio', 'output', NULL, ts('Email Receipts'), 'email_receipt',
107 array('onClick' => "document.getElementById('selectPdfFormat').style.display = 'none';")
108 );
109 $this->addElement('radio', 'output', NULL, ts('PDF Receipts'), 'pdf_receipt',
110 array('onClick' => "document.getElementById('selectPdfFormat').style.display = 'block';")
111 );
112 $this->addRule('output', ts('Selection required'), 'required');
113
114 $this->add('select', 'pdf_format_id', ts('Page Format'),
115 array(0 => ts('- default -')) + CRM_Core_BAO_PdfFormat::getList(TRUE)
116 );
10708045 117 $this->add('checkbox', 'receipt_update', ts('Update receipt dates for these contributions'), FALSE);
be4478f0 118 $this->add('checkbox', 'override_privacy', ts('Override privacy setting? (Do not email / Do not mail)'), FALSE);
6a488035
TO
119
120 $this->addButtons(array(
121 array(
122 'type' => 'next',
123 'name' => ts('Process Receipt(s)'),
124 'isDefault' => TRUE,
125 ),
126 array(
127 'type' => 'back',
128 'name' => ts('Cancel'),
129 ),
130 )
131 );
132 }
133
134 /**
fe482240 135 * Set default values.
6a488035 136 */
00be9182 137 public function setDefaultValues() {
6a488035 138 $defaultFormat = CRM_Core_BAO_PdfFormat::getDefaultValues();
10708045 139 return array('pdf_format_id' => $defaultFormat['id'], 'receipt_update' => 1, 'override_privacy' => 0);
6a488035
TO
140 }
141
142 /**
fe482240 143 * Process the form after the input has been submitted and validated.
6a488035
TO
144 */
145 public function postProcess() {
146 // get all the details needed to generate a receipt
2826a42e
RK
147 $message = array();
148 $template = CRM_Core_Smarty::singleton();
6a488035 149
6efffa5d
PB
150 $params = $this->controller->exportValues($this->_name);
151 $elements = self::getElements($this->_contributionIds, $params, $this->_contactIds);
6a488035 152
2826a42e 153 foreach ($elements['details'] as $contribID => $detail) {
6a488035
TO
154 $input = $ids = $objects = array();
155
3a1261ac 156 if (in_array($detail['contact'], $elements['excludeContactIds'])) {
6a488035
TO
157 continue;
158 }
159
160 $input['component'] = $detail['component'];
161
162 $ids['contact'] = $detail['contact'];
2826a42e 163 $ids['contribution'] = $contribID;
6a488035
TO
164 $ids['contributionRecur'] = NULL;
165 $ids['contributionPage'] = NULL;
166 $ids['membership'] = CRM_Utils_Array::value('membership', $detail);
167 $ids['participant'] = CRM_Utils_Array::value('participant', $detail);
168 $ids['event'] = CRM_Utils_Array::value('event', $detail);
169
3a1261ac 170 if (!$elements['baseIPN']->validateData($input, $ids, $objects, FALSE)) {
6a488035
TO
171 CRM_Core_Error::fatal();
172 }
173
174 $contribution = &$objects['contribution'];
6a488035
TO
175
176 // set some fake input values so we can reuse IPN code
353ffa53
TO
177 $input['amount'] = $contribution->total_amount;
178 $input['is_test'] = $contribution->is_test;
6a488035
TO
179 $input['fee_amount'] = $contribution->fee_amount;
180 $input['net_amount'] = $contribution->net_amount;
353ffa53
TO
181 $input['trxn_id'] = $contribution->trxn_id;
182 $input['trxn_date'] = isset($contribution->trxn_date) ? $contribution->trxn_date : NULL;
cc7b912f 183 $input['receipt_update'] = $params['receipt_update'];
d5580ed4 184 $input['contribution_status_id'] = $contribution->contribution_status_id;
6a488035 185
665e5ec7 186 // CRM_Contribute_BAO_Contribution::composeMessageArray expects mysql formatted date
6a488035
TO
187 $objects['contribution']->receive_date = CRM_Utils_Date::isoToMysql($objects['contribution']->receive_date);
188
6a488035 189 $values = array();
ec7e3954 190 $mail = CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $objects['contribution']->id, $values, FALSE,
a35dd516 191 $elements['createPdf']);
6a488035
TO
192
193 if ($mail['html']) {
194 $message[] = $mail['html'];
195 }
196 else {
197 $message[] = nl2br($mail['body']);
198 }
199
200 // reset template values before processing next transactions
201 $template->clearTemplateVars();
202 }
3a1261ac
RK
203
204 if ($elements['createPdf']) {
6a488035
TO
205 CRM_Utils_PDF_Utils::html2pdf($message,
206 'civicrmContributionReceipt.pdf',
207 FALSE,
3a1261ac 208 $elements['params']['pdf_format_id']
6a488035
TO
209 );
210 CRM_Utils_System::civiExit();
211 }
212 else {
3a1261ac
RK
213 if ($elements['suppressedEmails']) {
214 $status = ts('Email was NOT sent to %1 contacts (no email address on file, or communication preferences specify DO NOT EMAIL, or contact is deceased).', array(1 => $elements['suppressedEmails']));
6a488035
TO
215 $msgTitle = ts('Email Error');
216 $msgType = 'error';
217 }
218 else {
219 $status = ts('Your mail has been sent.');
220 $msgTitle = ts('Sent');
221 $msgType = 'success';
222 }
223 CRM_Core_Session::setStatus($status, $msgTitle, $msgType);
224 }
225 }
3a1261ac
RK
226
227 /**
fe482240 228 * Declaration of common variables for Invoice and PDF.
3a1261ac 229 *
3a1261ac 230 *
014c4014
TO
231 * @param array $contribIds
232 * Contribution Id.
233 * @param array $params
234 * Parameter for pdf or email invoices.
235 * @param array $contactIds
236 * Contact Id.
6efffa5d 237 *
a6c01b45
CW
238 * @return array
239 * array of common elements
2826a42e 240 *
3a1261ac 241 */
dce4c3ea 242 static public function getElements($contribIds, $params, $contactIds) {
3a1261ac
RK
243 $pdfElements = array();
244
6efffa5d 245 $pdfElements['contribIDs'] = implode(',', $contribIds);
3a1261ac
RK
246
247 $pdfElements['details'] = CRM_Contribute_Form_Task_Status::getDetails($pdfElements['contribIDs']);
248
249 $pdfElements['baseIPN'] = new CRM_Core_Payment_BaseIPN();
250
6efffa5d 251 $pdfElements['params'] = $params;
3a1261ac
RK
252
253 $pdfElements['createPdf'] = FALSE;
830b3e83 254 if (!empty($pdfElements['params']['output']) &&
353ffa53
TO
255 ($pdfElements['params']['output'] == "pdf_invoice" || $pdfElements['params']['output'] == "pdf_receipt")
256 ) {
3a1261ac
RK
257 $pdfElements['createPdf'] = TRUE;
258 }
259
260 $excludeContactIds = array();
261 if (!$pdfElements['createPdf']) {
262 $returnProperties = array(
353ffa53
TO
263 'email' => 1,
264 'do_not_email' => 1,
265 'is_deceased' => 1,
266 'on_hold' => 1,
267 );
3a1261ac 268
6efffa5d 269 list($contactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, $returnProperties, FALSE, FALSE);
3a1261ac 270 $pdfElements['suppressedEmails'] = 0;
830b3e83 271 $suppressedEmails = 0;
3a1261ac 272 foreach ($contactDetails as $id => $values) {
10708045
EM
273 if (empty($values['email']) ||
274 (empty($params['override_privacy']) && !empty($values['do_not_email']))
275 || CRM_Utils_Array::value('is_deceased', $values)
353ffa53
TO
276 || !empty($values['on_hold'])
277 ) {
874c9be7
TO
278 $suppressedEmails++;
279 $pdfElements['suppressedEmails'] = $suppressedEmails;
280 $excludeContactIds[] = $values['contact_id'];
3a1261ac
RK
281 }
282 }
283 }
284 $pdfElements['excludeContactIds'] = $excludeContactIds;
2826a42e 285
3a1261ac
RK
286 return $pdfElements;
287 }
96025800 288
6a488035 289}