forked from Project60/org.project60.sepa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sepa.php
475 lines (423 loc) · 15.8 KB
/
sepa.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
<?php
/*-------------------------------------------------------+
| Project 60 - SEPA direct debit |
| Copyright (C) 2013-2014 Project60 |
+--------------------------------------------------------+
| This program is released as free software under the |
| Affero GPL license. You can redistribute it and/or |
| modify it under the terms of this license which you |
| can read by viewing the included agpl.txt or online |
| at www.gnu.org/licenses/agpl.html. Removal of this |
| copyright header is strictly prohibited without |
| written permission from the original author(s). |
+--------------------------------------------------------*/
require_once 'sepa.civix.php';
require_once 'sepa_pp_sdd.php';
function sepa_civicrm_pageRun( &$page ) {
if (get_class($page) == "CRM_Contribute_Page_Tab") {
// single contribuion view
if (!CRM_Sepa_Logic_Settings::isSDD(array('payment_instrument_id' => $page->getTemplate()->get_template_vars('payment_instrument_id'))))
return;
if ($page->getTemplate()->get_template_vars('contribution_recur_id')) {
// This is an installment of a recurring contribution.
$mandate = civicrm_api3('SepaMandate', 'getsingle', array('entity_table'=>'civicrm_contribution_recur', 'entity_id'=>$page->getTemplate()->get_template_vars('contribution_recur_id')));
}
else {
// this is a OOFF contribtion
$mandate = civicrm_api3('SepaMandate', 'getsingle', array('entity_table'=>'civicrm_contribution', 'entity_id'=>$page->getTemplate()->get_template_vars('id')));
}
$page->assign('sepa', $mandate);
CRM_Core_Region::instance('page-body')->add(array(
'template' => 'Sepa/Contribute/Form/ContributionView.tpl'
));
}
elseif ( get_class($page) == "CRM_Contribute_Page_ContributionRecur") {
// recurring contribuion view
$recur = $page->getTemplate()->get_template_vars("recur");
// This is a one-off contribution => try to show mandate data.
$template_vars = $page->getTemplate()->get_template_vars('recur');
$payment_instrument_id = $template_vars['payment_instrument_id'];
if (!CRM_Sepa_Logic_Settings::isSDD(array('payment_instrument_id' => $payment_instrument_id)))
return;
$mandate = civicrm_api("SepaMandate","getsingle",array("version"=>3, "entity_table"=>"civicrm_contribution_recur", "entity_id"=>$recur["id"]));
if (!array_key_exists("id",$mandate)) {
CRM_Core_Error::fatal(ts("Can't find the sepa mandate"));
}
$page->assign("sepa",$mandate);
CRM_Core_Region::instance('page-body')->add(array(
'template' => 'Sepa/Contribute/Page/ContributionRecur.tpl'
));
}
}
function sepa_civicrm_buildForm ( $formName, &$form ) {
// incorporate payment processor
sepa_pp_buildForm($formName, $form);
}
function sepa_civicrm_postProcess( $formName, &$form ) {
// incorporate payment processor
sepa_pp_postProcess($formName, $form);
}
/**
* Implementation of hook_civicrm_config
*/
function sepa_civicrm_config(&$config) {
/*
when civi 4.4, not sure how to make it compatible with both
CRM_Core_DAO_AllCoreTables::$daoToClass["SepaMandate"] = "CRM_Sepa_DAO_SEPAMandate";
CRM_Core_DAO_AllCoreTables::$daoToClass["SepaCreditor"] = "CRM_Sepa_DAO_SEPACreditor";
*/
_sepa_civix_civicrm_config($config);
}
/**
* Implementation of hook_civicrm_xmlMenu
*
* @param $files array(string)
*/
function sepa_civicrm_xmlMenu(&$files) {
_sepa_civix_civicrm_xmlMenu($files);
}
/**
* Implementation of hook_civicrm_install
*/
function sepa_civicrm_install() {
$config = CRM_Core_Config::singleton();
//create the tables
$sql = file_get_contents(dirname( __FILE__ ) .'/sql/sepa.sql', true);
CRM_Utils_File::sourceSQLFile($config->dsn, $sql, NULL, true);
//add the required option groups
sepa_civicrm_install_options(sepa_civicrm_options());
return _sepa_civix_civicrm_install();
}
function sepa_civicrm_install_options($data) {
foreach ($data as $groupName => $group) {
// check group existence
$result = civicrm_api('option_group', 'getsingle', array('version' => 3, 'name' => $groupName));
if (isset($result['is_error']) && $result['is_error']) {
$params = array(
'version' => 3,
'sequential' => 1,
'name' => $groupName,
'is_reserved' => 1,
'is_active' => 1,
'title' => $group['title'],
'description' => $group['description'],
);
$result = civicrm_api('option_group', 'create', $params);
$group_id = $result['values'][0]['id'];
} else {
$group_id = $result['id'];
}
if (is_array($group['values'])) {
$groupValues = $group['values'];
$weight = 1;
foreach ($groupValues as $valueName => $value) {
$result = civicrm_api('option_value', 'getsingle', array('version' => 3, 'name' => $valueName));
if (isset($result['is_error']) && $result['is_error']) {
$params = array(
'version' => 3,
'sequential' => 1,
'option_group_id' => $group_id,
'name' => $valueName,
'label' => $value['label'],
'weight' => isset($value['weight']) ? $value['weight'] : $weight,
'is_default' => $value['is_default'],
'is_active' => 1,
);
if (isset($value['value'])) {
$params['value'] = $value['value'];
}
$result = civicrm_api('option_value', 'create', $params);
} else {
$weight = $result['weight'] + 1;
}
}
}
}
}
function sepa_civicrm_options() {
$result = civicrm_api('option_group', 'getsingle', array('version' => 3, 'name' => 'payment_instrument'));
if (!isset($result['id']))
die ($result["error_message"]);
$gid= $result['id'];
//find the value to give to the payment instruments
$query = "SELECT max( `weight` ) as weight FROM `civicrm_option_value` where option_group_id=" . $gid;
$dao = new CRM_Core_DAO();
$dao->query($query);
$dao->fetch();
$weight = $dao->weight + 1;
// start with the lowest weight value
return array(
'msg_tpl_workflow_contribution' => array(
'values' => array(
'sepa_mandate_pdf' => array(
'label' => 'PDF Mandate',
'value' => 1,
'is_default' => 0,
),
'sepa_mandate' => array(
'label' => 'Mail Sepa Mandate',
'value' => 1,
'is_default' => 0,
),
),
),
// These will be used to mark a contribution with the correct type and will
// greatly facilitate batching later on
'payment_instrument' => array(
'values' => array(
'FRST' => array(
'label' => 'SEPA DD First Transaction',
'weight' => $weight,
'is_default' => 0,
),
'RCUR' => array(
'label' => 'SEPA DD Recurring Transaction',
'weight' => $weight+1,
'is_default' => 0,
),
'OOFF' => array(
'label' => 'SEPA DD One-off Transaction',
'weight' => $weight+2,
'is_default' => 0,
),
),
),
'sepa_file_format' => array(
'title' => 'SEPA XML File Format Variants',
'description' => '',
'is_reserved' => 1,
'is_active' => 1,
'values' => array(
'pain.008.001.02' => array(
'label' => ts('pain.008.001.02 (ISO 20022/official SEPA guidelines)'),
'is_default' => 1,
'is_reserved' => 1,
'value' => 1,
),
'pain.008.003.02' => array(
'label' => ts('pain.008.003.02 container core direct debit (CDC EBICS-2.7)'),
'is_default' => 0,
'is_reserved' => 1,
'value' => 2,
),
'pain.008.003.02 COR1' => array(
'label' => ts('pain.008.003.02 COR1 direct debit (CD1 EBICS-2.7)'),
'is_default' => 0,
'is_reserved' => 1,
'value' => 3,
),
),
),
'batch_status' => array(
'values' => array(
'Received' => array(
'label' => 'Received',
'is_default' => 0,
'weight' => 6,
),
),
),
);
}
/**
* Implementation of hook_civicrm_uninstall
*/
function sepa_civicrm_uninstall() {
//should we delete the tables?
return _sepa_civix_civicrm_uninstall();
}
/**
* Implementation of hook_civicrm_enable
*/
function sepa_civicrm_enable() {
// install/activate SEPA payment processor
sepa_pp_install();
// create a dummy creditor if no creditor exists
$creditorCount = CRM_Core_DAO::singleValueQuery('SELECT COUNT(*) FROM `civicrm_sdd_creditor`;');
if (empty($creditorCount)) {
error_log("org.project60.sepa_dd: Trying to install dummy creditor.");
// to create, we need to first find a default contact
$default_contact = 0;
$domains = civicrm_api('Domain', 'get', array('version'=>3));
foreach ($domains['values'] as $domain) {
if (!empty($domain['contact_id'])) {
$default_contact = $domain['contact_id'];
break;
}
}
if (empty($default_contact)) {
error_log("org.project60.sepa_dd: Cannot install dummy creditor - no default contact found.");
} else {
error_log("org.project60.sepa_dd: Inserting dummy creditor into database.");
// remark: we're within the enable hook, so we cannot use our own API/BAOs...
$create_creditor_sql = "
INSERT INTO civicrm_sdd_creditor
(`creditor_id`, `identifier`, `name`, `address`, `country_id`, `iban`, `bic`, `mandate_prefix`, `mandate_active`, `sepa_file_format_id`, `category`)
VALUES
($default_contact, 'TESTCREDITORDE', 'TEST CREDITOR', '221B Baker Street\nLondon', '1226', 'DE12500105170648489890', 'SEPATEST', 'TEST', 1, 1, 'TEST');";
CRM_Core_DAO::executeQuery($create_creditor_sql);
}
}
return _sepa_civix_civicrm_enable();
}
/**
* Implementation of hook_civicrm_disable
*/
function sepa_civicrm_disable() {
sepa_pp_disable();
return _sepa_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 sepa_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _sepa_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 sepa_civicrm_managed(&$entities) {
return _sepa_civix_civicrm_managed($entities);
}
function sepa_civicrm_summaryActions( &$actions, $contactID ) {
// add "create SEPA mandate action"
$actions['sepa_contribution'] = array(
'title' => ts("Record SEPA Contribution"),
'weight' => 5,
'ref' => 'new-sepa-contribution',
'key' => 'sepa_contribution',
'component' => 'CiviContribute',
'href' => CRM_Utils_System::url('civicrm/sepa/cmandate', "cid=$contactID"),
'permissions' => array('access CiviContribute', 'edit contributions')
);
}
/**
* Support SEPA mandates in merge operations
*/
function sepa_civicrm_merge ( $type, &$data, $mainId = NULL, $otherId = NULL, $tables = NULL ) {
switch ($type) {
case 'relTables':
// Offer user to merge SEPA Mandates
$data['rel_table_sepamandate'] = array(
'title' => ts('SEPA Mandates'),
'tables' => array('civicrm_sdd_mandate'),
'url' => CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=$cid&selectedChild=contribute'), // '$cid' will be automatically replaced
);
break;
case 'cidRefs':
// this is the only field that needs to be modified
$data['civicrm_sdd_mandate'] = array('contact_id');
break;
}
}
/**
* PREVENT the user to delete a (recurring) contribution when there's a mandate attached.
*/
function sepa_civicrm_pre($op, $objectName, $id, &$params) {
// FIXME: move this into validation?
// disallow the deletion of a (recurring) contribution if it is attached to mandates
if ($op=='delete' && ($objectName=='Contribution' || $objectName=='ContributionRecur')) {
if ($objectName=='Contribution') {
$table = 'civicrm_contribution';
} else {
$table = 'civicrm_contribution_recur';
}
$query = "SELECT id FROM civicrm_sdd_mandate WHERE entity_id=$id AND entity_table='$table';";
$result = CRM_Core_DAO::executeQuery($query);
if ($result->fetch()) {
die(sprintf(ts("You cannot delete this contribution because it is connected to SEPA mandate [%s]. Delete the mandate instead!"), $result->id));
}
}
}
// totten's addition
function sepa_civicrm_entityTypes(&$entityTypes) {
// add my DAO's
$entityTypes[] = array(
'name' => 'SepaMandate',
'class' => 'CRM_Sepa_DAO_SEPAMandate',
'table' => 'civicrm_sepa_mandate',
);
$entityTypes[] = array(
'name' => 'SepaCreditor',
'class' => 'CRM_Sepa_DAO_SEPACreditor',
'table' => 'civicrm_sepa_creditor',
);
$entityTypes[] = array(
'name' => 'SepaTransactionGroup',
'class' => 'CRM_Sepa_BAO_SEPATransactionGroup',
'table' => 'civicrm_sepa_txgroup',
);
$entityTypes[] = array(
'name' => 'SepaSddFile',
'class' => 'CRM_Sepa_DAO_SEPASddFile',
'table' => 'civicrm_sepa_file',
);
$entityTypes[] = array(
'name' => 'SepaContributionGroup',
'class' => 'CRM_Sepa_DAO_SEPAContributionGroup',
'table' => 'civicrm_sepa_contribution_txgroup',
);
}
/**
* Implementation of hook_civicrm_config
*/
function sepa_civicrm_alterSettingsFolders(&$metaDataFolders = NULL){
_sepa_civix_civicrm_alterSettingsFolders($metaDataFolders);
}
/**
* Implementation of hook_civicrm_navigationMenu
*/
function sepa_civicrm_navigationMenu(&$params) {
// Check that our item doesn't already exist
$menu_item_search = array('url' => 'civicrm/sepa');
$menu_items = array();
CRM_Core_BAO_Navigation::retrieve($menu_item_search, $menu_items);
if ($menu_items) {
return;
}
// Find the CiviContribute menu
$contribute_menu_id = Null;
foreach($params as $key => $value) {
if($value['attributes']['name'] == "Contributions") {
$contribute_menu_id = $key;
}
}
if ($contribute_menu_id) {
// Add the new menu option
$new_nav_id = CRM_Core_DAO::singleValueQuery("SELECT max(id) FROM civicrm_navigation");
if (is_integer($new_nav_id)) {
$new_nav_id++;
}
$params[$contribute_menu_id]['child'][$new_nav_id] = array(
'attributes' => array (
'label' => ts('CiviSEPA Dashboard', array('domain' => 'org.project60.sepa')),
'name' => 'Dashboard',
'url' => 'civicrm/sepa',
'permission' => 'administer CiviCRM',
'operator' => NULL,
'separator' => 2,
'parentID' => $contribute_menu_id,
'navID' => $new_nav_id,
'active' => 1
)
);
}
}
/**
* Set permission to the API calls
*/
function sepa_civicrm_alterAPIPermissions($entity, $action, &$params, &$permissions) {
// TODO: add more
$permissions['sepa_alternative_batching']['received'] = array('edit contributions');
$permissions['sepa_logic']['received'] = array('edit contributions');
$permissions['sepa_transaction_group']['toaccgroup'] = array('edit contributions');
}