forked from tttp/CDNTaxReceipts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cdntaxreceipts.php
349 lines (305 loc) · 11.3 KB
/
cdntaxreceipts.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
require_once 'cdntaxreceipts.civix.php';
require_once 'cdntaxreceipts.functions.inc';
require_once 'cdntaxreceipts.db.inc';
define('CDNTAXRECEIPTS_MODE_BACKOFFICE', 1);
define('CDNTAXRECEIPTS_MODE_PREVIEW', 2);
define('CDNTAXRECEIPTS_MODE_WORKFLOW', 3);
function cdntaxreceipts_civicrm_buildForm( $formName, &$form ) {
if (is_a( $form, 'CRM_Contribute_Form_ContributionView')) {
// add "Issue Tax Receipt" button to the "View Contribution" page
// if the Tax Receipt has NOT yet been issued -> display a white maple leaf icon
// if the Tax Receipt has already been issued -> display a red maple leaf icon
CRM_Core_Resources::singleton()->addStyleFile('org.civicrm.cdntaxreceipts', 'css/civicrm_cdntaxreceipts.css');
$contributionId = $form->get('id');
$buttons = array(
array(
'type' => 'cancel',
'name' => ts('Done'),
'spacing' => ' ',
'isDefault' => TRUE,
)
);
$subName = 'view_tax_receipt';
if ( isset($contributionId) && cdntaxreceipts_eligibleForReceipt($contributionId) ) {
list($issued_on, $receipt_id) = cdntaxreceipts_issued_on($contributionId);
$is_original_receipt = empty($issued_on);
if ($is_original_receipt) {
$subName = 'issue_tax_receipt';
}
$buttons[] = array(
'type' => 'submit',
'subName' => $subName,
'name' => ts('Tax Receipt', array('domain' => 'org.civicrm.cdntaxreceipts')),
'isDefault' => FALSE
);
$form->addButtons($buttons);
}
}
}
/**
* Implementation of hook_civicrm_postProcess().
*
* Called when a form comes back for processing. Basically, we want to process
* the button we added in cdntaxreceipts_civicrm_buildForm().
*/
function cdntaxreceipts_civicrm_postProcess( $formName, &$form ) {
// first check whether I really need to process this form
if ( ! is_a( $form, 'CRM_Contribute_Form_ContributionView' ) ) {
return;
}
$types = array('issue_tax_receipt','view_tax_receipt');
$action = '';
foreach($types as $type) {
$post = '_qf_ContributionView_submit_'.$type;
if (isset($_POST[$post])) {
if ($_POST[$post] == ts('Tax Receipt', array('domain' => 'org.civicrm.cdntaxreceipts'))) {
$action = $post;
}
}
}
if (empty($action)) {
return;
}
// the tax receipt button has been pressed. redirect to the tax receipt 'view' screen, preserving context.
$contributionId = $form->get( 'id' );
$contactId = $form->get( 'cid' );
$session = CRM_Core_Session::singleton();
$session->pushUserContext(CRM_Utils_System::url('civicrm/contact/view/contribution',
"reset=1&id=$contributionId&cid=$contactId&action=view&context=contribution&selectedChild=contribute"
));
$urlParams = array('reset=1', 'id='.$contributionId, 'cid='.$contactId);
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/cdntaxreceipts/view', implode('&',$urlParams)));
}
/**
* Implementation of hook_civicrm_searchTasks().
*
* For users with permission to issue tax receipts, give them the ability to do it
* as a batch of search results.
*/
function cdntaxreceipts_civicrm_searchTasks($objectType, &$tasks ) {
if ( $objectType == 'contribution' && CRM_Core_Permission::check( 'issue cdn tax receipts' ) ) {
$single_in_list = FALSE;
$aggregate_in_list = FALSE;
foreach ($tasks as $key => $task) {
if($task['class'] == 'CRM_Cdntaxreceipts_Task_IssueSingleTaxReceipts') {
$single_in_list = TRUE;
}
}
foreach ($tasks as $key => $task) {
if($task['class'] == 'CRM_Cdntaxreceipts_Task_IssueAggregateTaxReceipts') {
$aggregate_in_list = TRUE;
}
}
if (!$single_in_list) {
$tasks[] = array (
'title' => ts('Issue Tax Receipts (Separate Receipt for Each Contribution)', array('domain' => 'org.civicrm.cdntaxreceipts')),
'class' => 'CRM_Cdntaxreceipts_Task_IssueSingleTaxReceipts',
'result' => TRUE);
}
if (!$aggregate_in_list) {
$tasks[] = array (
'title' => ts('Issue Tax Receipts (Combined Receipt with Total Contributed)'),
'class' => 'CRM_Cdntaxreceipts_Task_IssueAggregateTaxReceipts',
'result' => TRUE);
}
}
elseif ( $objectType == 'contact' && CRM_Core_Permission::check( 'issue cdn tax receipts' ) ) {
$annual_in_list = FALSE;
foreach ($tasks as $key => $task) {
if($task['class'] == 'CRM_Cdntaxreceipts_Task_IssueAnnualTaxReceipts') {
$annual_in_list = TRUE;
}
}
if (!$annual_in_list) {
$tasks[] = array (
'title' => ts('Issue Annual Tax Receipts'),
'class' => 'CRM_Cdntaxreceipts_Task_IssueAnnualTaxReceipts',
'result' => TRUE);
}
}
}
/**
* Implementation of hook_civicrm_permission().
*/
function cdntaxreceipts_civicrm_permission( &$permissions ) {
$prefix = ts('CiviCRM CDN Tax Receipts') . ': ';
$permissions += array(
'issue cdn tax receipts' => $prefix . ts('Issue Tax Receipts', array('domain' => 'org.civicrm.cdntaxreceipts')),
);
}
/**
* Implementation of hook_civicrm_config
*/
function cdntaxreceipts_civicrm_config(&$config) {
_cdntaxreceipts_civix_civicrm_config($config);
}
/**
* Implementation of hook_civicrm_xmlMenu
*
* @param $files array(string)
*/
function cdntaxreceipts_civicrm_xmlMenu(&$files) {
_cdntaxreceipts_civix_civicrm_xmlMenu($files);
}
/**
* Implementation of hook_civicrm_install
*/
function cdntaxreceipts_civicrm_install() {
// copy tables civicrm_cdntaxreceipts_log and civicrm_cdntaxreceipts_log_contributions IF they already exist
// Issue: #1
return _cdntaxreceipts_civix_civicrm_install();
}
/**
* Implementation of hook_civicrm_uninstall
*/
function cdntaxreceipts_civicrm_uninstall() {
return _cdntaxreceipts_civix_civicrm_uninstall();
}
/**
* Implementation of hook_civicrm_enable
*/
function cdntaxreceipts_civicrm_enable() {
CRM_Core_Session::setStatus(ts('Configure the Tax Receipts extension at Administer >> CiviContribute >> CDN Tax Receipts.', array('domain' => 'org.civicrm.cdntaxreceipts')));
return _cdntaxreceipts_civix_civicrm_enable();
}
/**
* Implementation of hook_civicrm_disable
*/
function cdntaxreceipts_civicrm_disable() {
return _cdntaxreceipts_civix_civicrm_disable();
}
/**
* Implementation of hook_civicrm_upgrade
*
* @param $op string, the type of operation being performed; 'check' or 'enqueue'
* @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks
*
* @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending)
* for 'enqueue', returns void
*/
function cdntaxreceipts_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _cdntaxreceipts_civix_civicrm_upgrade($op, $queue);
}
/**
* Implementation of hook_civicrm_managed
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*/
function cdntaxreceipts_civicrm_managed(&$entities) {
return _cdntaxreceipts_civix_civicrm_managed($entities);
}
/**
* Implementation of hook_civicrm_managed
*
* Add entries to the navigation menu, automatically removed on uninstall
*/
function cdntaxreceipts_civicrm_navigationMenu(&$params) {
// Check that our item doesn't already exist
$cdntax_search = array('url' => 'civicrm/cdntaxreceipts/settings?reset=1');
$cdntax_item = array();
CRM_Core_BAO_Navigation::retrieve($cdntax_search, $cdntax_item);
if ( ! empty($cdntax_item) ) {
return;
}
// Get the maximum key of $params using method mentioned in discussion
// https://issues.civicrm.org/jira/browse/CRM-13803
$navId = CRM_Core_DAO::singleValueQuery("SELECT max(id) FROM civicrm_navigation");
if (is_integer($navId)) {
$navId++;
}
// Find the Memberships menu
foreach($params as $key => $value) {
if ('Administer' == $value['attributes']['name']) {
$parent_key = $key;
foreach($value['child'] as $child_key => $child_value) {
if ('CiviContribute' == $child_value['attributes']['name']) {
$params[$parent_key]['child'][$child_key]['child'][$navId] = array (
'attributes' => array (
'label' => ts('CDN Tax Receipts',array('domain' => 'org.civicrm.cdntaxreceipts')),
'name' => 'CDN Tax Receipts',
'url' => 'civicrm/cdntaxreceipts/settings?reset=1',
'permission' => 'access CiviContribute,administer CiviCRM',
'operator' => 'AND',
'separator' => 2,
'parentID' => $child_key,
'navID' => $navId,
'active' => 1
)
);
}
}
}
}
}
function cdntaxreceipts_civicrm_validate( $formName, &$fields, &$files, &$form ) {
if ($formName == 'CRM_Cdntaxreceipts_Form_Settings') {
$errors = array();
$allowed = array('gif', 'png', 'jpg', 'pdf');
foreach ($files as $key => $value) {
if (CRM_Utils_Array::value('name', $value)) {
$ext = pathinfo($value['name'], PATHINFO_EXTENSION);
if (!in_array($ext, $allowed)) {
$errors[$key] = ts('Please upload a valid file. Allowed extensions are (.gif, .png, .jpg, .pdf)');
}
}
}
return $errors;
}
}
function cdntaxreceipts_civicrm_alterMailParams(&$params, $context) {
/*
When CiviCRM core sends receipt email using CRM_Core_BAO_MessageTemplate, this hook was invoked twice:
- once in CRM_Core_BAO_MessageTemplate::sendTemplate(), context "messageTemplate"
- once in CRM_Utils_Mail::send(), which is called by CRM_Core_BAO_MessageTemplate::sendTemplate(), context "singleEmail"
Hence, cdntaxreceipts_issueTaxReceipt() is called twice, sending 2 receipts to archive email.
To avoid this, only execute this hook when context is "messageTemplate"
*/
if( $context != 'messageTemplate'){
return;
}
$msg_template_types = array('contribution_online_receipt', 'contribution_offline_receipt');
if (isset($params['groupName'])
&& $params['groupName'] == 'msg_tpl_workflow_contribution'
&& isset($params['valueName'])
&& in_array($params['valueName'], $msg_template_types)) {
// get the related contribution id for this message
if (isset($params['tplParams']['contributionID'])) {
$contribution_id = $params['tplParams']['contributionID'];
}
else if( isset($params['contributionId'])) {
$contribution_id = $params['contributionId'];
}
else {
return;
}
// is the extension configured to send receipts attached to automated workflows?
if (!Civi::settings()->get('attach_to_workflows')) {
return;
}
// is this particular donation receiptable?
if (!cdntaxreceipts_eligibleForReceipt($contribution_id)) {
return;
}
$contribution = new CRM_Contribute_DAO_Contribution();
$contribution->id = $contribution_id;
$contribution->find(TRUE);
$nullVar = NULL;
list($ret, $method, $pdf_file) = cdntaxreceipts_issueTaxReceipt(
$contribution,
$nullVar,
CDNTAXRECEIPTS_MODE_WORKFLOW
);
if ($ret) {
$last_in_path = strrpos($pdf_file, '/');
$clean_name = substr($pdf_file, $last_in_path);
$attachment = array(
'fullPath' => $pdf_file,
'mime_type' => 'application/pdf',
'cleanName' => $clean_name,
);
$params['attachments'] = array($attachment);
}
}
}