forked from systopia/de.systopia.donrec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
donrec.php
483 lines (433 loc) · 20.6 KB
/
donrec.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<?php
/*-------------------------------------------------------+
| SYSTOPIA Donation Receipts Extension |
| Copyright (C) 2013-2016 SYSTOPIA |
| Author: B. Endres (endres -at- systopia.de) |
| http://www.systopia.de/ |
+--------------------------------------------------------+
| License: AGPLv3, see LICENSE file |
+--------------------------------------------------------*/
require_once 'donrec.civix.php';
require_once 'CRM/Donrec/DataStructure.php';
require_once 'CRM/Donrec/Logic/Template.php';
require_once 'CRM/Donrec/Logic/Settings.php';
use CRM_Donrec_ExtensionUtil as E;
/**
* Implementation of hook_civicrm_config
*/
function donrec_civicrm_config(&$config) {
_donrec_civix_civicrm_config($config);
}
/**
* Implementation of hook_civicrm_xmlMenu
*
* @param $files array(string)
*/
function donrec_civicrm_xmlMenu(&$files) {
_donrec_civix_civicrm_xmlMenu($files);
}
/**
* Implementation of hook_civicrm_install
*/
function donrec_civicrm_install() {
return _donrec_civix_civicrm_install();
}
/**
* Implementation of hook_civicrm_uninstall
*/
function donrec_civicrm_uninstall() {
return _donrec_civix_civicrm_uninstall();
}
/**
* Implementation of hook_civicrm_enable
*/
function donrec_civicrm_enable() {
return _donrec_civix_civicrm_enable();
}
/**
* Implementation of hook_civicrm_disable
*/
function donrec_civicrm_disable() {
return _donrec_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 donrec_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _donrec_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 donrec_civicrm_managed(&$entities) {
return _donrec_civix_civicrm_managed($entities);
}
/**
* Implementation of hook_civicrm_caseTypes
*
* Generate a list of case-types
*
* Note: This hook only runs in CiviCRM 4.4+.
*/
function donrec_civicrm_caseTypes(&$caseTypes) {
_donrec_civix_civicrm_caseTypes($caseTypes);
}
/**
* Add an action for creating donation receipts after doing a search
*
* @param string $objectType specifies the component
* @param array $tasks the list of actions
*
* @access public
*/
function donrec_civicrm_searchTasks($objectType, &$tasks) {
// add DONATION RECEIPT task to contact list
if ($objectType == 'contact') {
if (CRM_Core_Permission::check('create and withdraw receipts')) {
$tasks[] = array(
'title' => E::ts('Issue donation receipt(s)'),
'class' => 'CRM_Donrec_Form_Task_DonrecTask',
'result' => false);
$tasks[] = array(
'title' => E::ts('Withdraw donation receipt(s)'),
'class' => 'CRM_Donrec_Form_Task_DonrecResetTask',
'result' => false);
}
}
// add REBOOK task to contribution list
if ($objectType == 'contribution') {
if (CRM_Core_Permission::check('create and withdraw receipts')) {
$tasks[] = array(
'title' => E::ts('Issue donation receipt(s)'),
'class' => 'CRM_Donrec_Form_Task_ContributeTask',
'result' => false);
}
if (CRM_Core_Permission::check('edit contributions')) {
$tasks[] = array(
'title' => E::ts('Rebook to contact'),
'class' => 'CRM_Donrec_Form_Task_RebookTask',
'result' => false);
}
}
}
/**
* 1) add an extra search column 'receipted'
* 2) modify actions for rebook
*/
function donrec_civicrm_searchColumns($objectName, &$headers, &$values, &$selector) {
if ($objectName == 'contribution') {
// ************************************
// ** ADD CONTRIBUTED COLUMN **
// ************************************
// save last element (action list)
$actionList = array_pop($headers);
// insert new column
$headers[] = array(
'name' => E::ts('Receipted'),
// Provide a weight lower than the "actions" column.
'weight' => $actionList['weight'] - 1,
'field_name' => 'is_receipted',
);
$receipted_contribution_ids = array();
// insert new values
foreach ($values as $id => $value ) {
$item_id = CRM_Donrec_Logic_ReceiptItem::hasValidReceiptItem($value['contribution_id'], TRUE);
if($item_id === FALSE) {
$values[$id]['is_receipted'] = E::ts('No');
}else{
$receipted_contribution_ids[] = $value['contribution_id']; // save as receipted for rebook (see below)
$values[$id]['is_receipted'] = sprintf('<a href="%s">%s</a>', CRM_Utils_System::url(
'civicrm/contact/view',
"reset=1&cid={$value['contact_id']}&rid=$item_id&selectedChild=donation_receipts",
TRUE, NULL, TRUE),
E::ts('Yes'));
}
}
// restore last element
$headers[] = $actionList;
// ************************************
// ** ADD REBOOK ACTION **
// ************************************
// only offer rebook only if the user has the correct permissions
if (CRM_Core_Permission::check('edit contributions')) {
$contribution_status_complete = (int) CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name');
$title = E::ts('Rebook');
$url = CRM_Utils_System::url('civicrm/donrec/rebook', "contributionIds=__CONTRIBUTION_ID__");
$action = "<a title=\"$title\" class=\"action-item action-item\" href=\"$url\">$title</a>";
// add 'rebook' action link to each row
foreach ($values as $rownr => $row) {
$contribution_status_id = $row['contribution_status_id'];
// ... but only for completed contributions
if ($contribution_status_id==$contribution_status_complete) {
// receipted contributions cannot be rebooked either...
if (!in_array($row['contribution_id'], $receipted_contribution_ids)) {
// this contribution is o.k. => add the rebook action
$contribution_id = $row['contribution_id'];
$this_action = str_replace('__CONTRIBUTION_ID__', $contribution_id, $action);
$values[$rownr]['action'] = str_replace('</span>', $this_action.'</span>', $row['action']);
}
}
}
}
}
}
/**
* The extra search column (see above) does not alter the template,
* so we inject javascript into the template-content.
*/
function donrec_civicrm_alterContent(&$content, $context, $tplName, &$object) {
// The contribution search results template has changed in 4.7.20, allowing
// to add columns and values in hook_civicrm_searchColumns() implementations.
// Prior to 4.7.20, this has to be done using the following approach.
// @link https://lab.civicrm.org/dev/core/commit/4fb5fcf3b17af6c9f5bf49ecc69902c5b0b78c24
// Commit that introduced the new behavior.
if (version_compare(CRM_Utils_System::version(), '4.7.20', '<')) {
// get page- resp. form-class of the object
$class_name = get_class($object);
if ($class_name == 'CRM_Contribute_Page_Tab' ||
$class_name == 'CRM_Contribute_Form_Search' ||
$class_name == 'CRM_Contribute_Page_DashBoard') {
// parse the template with smarty
$smarty = CRM_Core_Smarty::singleton();
$path = __DIR__ . '/templates/CRM/Contribute/ReceiptedColumn.tpl';
$html = $smarty->fetch($path);
// append the html to the content
$content .= $html;
}
}
}
/**
* Set permissions for runner/engine API call
*/
function donrec_civicrm_alterAPIPermissions($entity, $action, &$params, &$permissions) {
// TODO: adjust to correct permission
$permissions['donation_receipt_engine']['next'] = array('create and withdraw receipts');
$permissions['donation_receipt']['view'] = array('view and copy receipts');
$permissions['donation_receipt']['delete'] = array('delete receipts');
$permissions['donation_receipt']['copy'] = array('view and copy receipts');
$permissions['donation_receipt']['withdraw'] = array('create and withdraw receipts');
}
/**
* Custom permissions
*/
function donrec_civicrm_permission(&$permissions) {
$prefix = E::ts('DonationReceipts') . ': ';
$permissions['view and copy receipts'] = $prefix . E::ts('view and create copies of receipts');
$permissions['create and withdraw receipts'] = $prefix . E::ts('create and withdraw receipts');
$permissions['delete receipts'] = $prefix . E::ts('delete receipts');
}
/**
* Add headers to sent donation receipts
*/
function donrec_civicrm_alterMailParams(&$params, $context) {
CRM_Donrec_Exporters_EmailPDF::addDonrecMailCodeHeader($params, $context);
}
/**
* Set settings
*/
function donrec_civicrm_alterSettingsFolders(&$metaDataFolders = NULL){
static $configured = FALSE;
if ($configured) return;
$configured = TRUE;
$extRoot = dirname( __FILE__ ) . DIRECTORY_SEPARATOR;
$extDir = $extRoot . 'settings';
if(!in_array($extDir, $metaDataFolders)){
$metaDataFolders[] = $extDir;
}
}
function donrec_civicrm_tabs(&$tabs, $contactID) {
if (CRM_Core_Permission::check('view and copy receipts') || CRM_Core_Permission::check('create and withdraw receipts')) {
$url = CRM_Utils_System::url( 'civicrm/donrec/tab',
"reset=1&snippet=1&force=1&cid=$contactID" );
$tabs[] = array( 'id' => 'donation_receipts',
'url' => $url,
'title' => E::ts('Donation receipts'),
'count' => CRM_Donrec_Logic_Receipt::getReceiptCountForContact($contactID),
'weight' => 300);
}
}
/*
* return errors if a receipted contribution is going to be changed
*/
//TODO: the pre-hook need the same informations than this one and is called
// afterwards. Is it possible to make these informations available for it?
function donrec_civicrm_validateForm( $formName, &$fields, &$files, &$form, &$errors ) {
// Validate contribution_form for already existing contributions.
// Therefore we need a contribution-id.
if ($formName == 'CRM_Contribute_Form_Contribution' && !empty($form->_id)) {
// Validate the contribution values to be set.
$errors += CRM_Donrec_Logic_Settings::validateContribution($form->_id, $form->_values, $fields);
}
}
/*
* Implements hook_civicrm_pre().
*/
function donrec_civicrm_pre( $op, $objectName, $id, &$params ) {
// Check for forbidden changes on receipted contributions.
if ($objectName == 'Contribution' && ($op == 'edit' || $op == 'delete')) {
$has_item = CRM_Donrec_Logic_ReceiptItem::hasValidReceiptItem($id);
$in_snapshot = CRM_Donrec_Logic_Snapshot::isInOpenSnapshot($id);
if ($has_item || $in_snapshot) {
if ($op == 'edit') {
// Get the contribution.
$query = "
SELECT *
FROM civicrm_contribution
WHERE id = $id";
$result = CRM_Core_DAO::executeQuery($query);
$result->fetch();
// Validate the contribution values to be set.
CRM_Donrec_Logic_Settings::validateContribution($id, (array)$result, $params, TRUE);
} elseif ($op == 'delete') {
$message = sprintf(E::ts("This contribution [%d] must not be deleted because it has a receipt or is going to be receipted!"), $id);
throw new Exception($message);
}
}
}
return;
}
/**
* Prune the "find contributions" and "advanced contact search" forms
* by removing the fields that don't make sense or don't work
*/
function donrec_civicrm_buildForm($formName, &$form) {
if ($formName=='CRM_Contribute_Form_Search') {
$item_fields = CRM_Donrec_Logic_ReceiptItem::getCustomFields();
// remove unwanted fields
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'financial_type_id');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'total_amount');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'non_deductible_amount');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'currency');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'contribution_hash');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'issued_on');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'receive_date');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'issued_in');
$form->assign('field_ids_to_remove', implode(',', $field_ids_to_remove));
CRM_Core_Region::instance('page-body')->add(array(
'template' => 'CRM/Donrec/Form/Search/RemoveFields.snippet.tpl'
));
// DISABLED: date field search doesn't work
// CRM_Utils_DonrecHelper::relabelDateField($form, $item_fields, 'issued_on', E::E::ts("Issed On - From"), ts("Issed On - To"));
// CRM_Utils_DonrecHelper::relabelDateField($form, $item_fields, 'receive_date', E::E::ts("Received - From"), ts("Received - To"));
// override the standard fields
$status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'status');
if ($status_id) $form->add('select', "custom_{$status_id}",
E::ts('Status'),
// TODO: use future status definitions
array( '' => E::ts('- any -'),
'original' => E::ts('original'),
'copy' => E::ts('copy'),
'withdrawn' => E::ts('withdrawn'),
'withdrawn_copy' => E::ts('withdrawn_copy'),
));
$status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'type');
if ($status_id) $form->add('select', "custom_{$status_id}",
E::ts('Type'),
// TODO: use future status definitions
array( '' => E::ts('- any -'),
'single' => E::ts('single receipt'),
'bulk' => E::ts('bulk receipt'),
));
$status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'issued_in');
if ($status_id) $form->add('text', "custom_{$status_id}", E::ts('Receipt ID'));
$status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'issued_by');
if ($status_id) $form->add('text', "custom_{$status_id}", E::ts('Issued by contact'));
} elseif ($formName=='CRM_Contact_Form_Search_Advanced') {
// remove unwanted fields
$item_fields = CRM_Donrec_Logic_Receipt::getCustomFields();
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'issued_on');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'original_file');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'contact_type');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'gender');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'prefix');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'display_name');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'postal_greeting_display');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'email_greeting_display');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'addressee_display');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'street_address');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'supplemental_address_1');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'supplemental_address_2');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'supplemental_address_3');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'postal_code');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'city');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'country');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'shipping_addressee_display');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'shipping_street_address');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'shipping_supplemental_address_1');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'shipping_supplemental_address_2');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'shipping_supplemental_address_3');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'shipping_postal_code');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'shipping_city');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'shipping_country');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'date_from');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'date_to');
// remove unwanted fields from receipt items (in contribution tab)
$item_fields_receipt = CRM_Donrec_Logic_ReceiptItem::getCustomFields();
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'financial_type_id');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'total_amount');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'non_deductible_amount');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'currency');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'contribution_hash');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'issued_on');
$field_ids_to_remove[] = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'receive_date');
$form->assign('field_ids_to_remove', implode(',', $field_ids_to_remove));
CRM_Core_Region::instance('page-body')->add(array(
'template' => 'CRM/Donrec/Form/Search/RemoveFields.snippet.tpl'
));
// DISABLED: date field search doesn't work
//CRM_Utils_DonrecHelper::relabelDateField($form, $item_fields, 'issued_on', E::E::ts("Issed On - From"), ts("Issed On - To"));
// override the standard fields
$status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'status');
if ($status_id) $form->add('select', "custom_{$status_id}",
E::ts('Status'),
// TODO: use future status definitions
array( '' => E::ts('- any -'),
'original' => E::ts('original'),
'copy' => E::ts('copy'),
'withdrawn' => E::ts('withdrawn'),
'withdrawn_copy' => E::ts('withdrawn_copy'),
));
$status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'type');
if ($status_id) $form->add('select', "custom_{$status_id}",
E::ts('Type'),
// TODO: use future status definitions
array( '' => E::ts('- any -'),
'single' => E::ts('single receipt'),
'bulk' => E::ts('bulk receipt'),
));
$status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields, 'issued_by');
if ($status_id) $form->add('text', "custom_{$status_id}", E::ts('Issued by contact'));
// override the receipt_item standard fields
$status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'status');
if ($status_id) $form->add('select', "custom_{$status_id}",
E::ts('Status'),
// TODO: use future status definitions
array( '' => E::ts('- any -'),
'original' => E::ts('original'),
'copy' => E::ts('copy'),
'withdrawn' => E::ts('withdrawn'),
'withdrawn_copy' => E::ts('withdrawn_copy'),
));
$status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'type');
if ($status_id) $form->add('select', "custom_{$status_id}",
E::ts('Type'),
// TODO: use future status definitions
array( '' => E::ts('- any -'),
'single' => E::ts('single receipt'),
'bulk' => E::ts('bulk receipt'),
));
$status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'issued_in');
if ($status_id) $form->add('text', "custom_{$status_id}", E::ts('Receipt ID'));
$status_id = CRM_Utils_DonrecHelper::getFieldID($item_fields_receipt, 'issued_by');
if ($status_id) $form->add('text', "custom_{$status_id}", E::ts('Issued by contact'));
}
}