-
Notifications
You must be signed in to change notification settings - Fork 4
/
financialsummaries.php
436 lines (345 loc) · 15 KB
/
financialsummaries.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
<?php
require_once 'financialsummaries.civix.php';
function financialsummaries_civicrm_tokens( &$tokens ){
if( CRM_Core_Permission::check('access CiviContribute')){
$tokens['finances'] = array();
retrieveFinancialTokenNames( $tokens['finances'] ) ;
}
}
function retrieveFinancialTokenNames( &$financial_tokens ){
require_once('utils/FinancialCategory.php');
$tmpFinanceCat = new FinancialCategory();
// Create an array of all the various contribution type prefixes, ie the part before the dash.
$contribution_type_prefixes = $tmpFinanceCat->retrieveFinancialPrefixes();
// At this point we have all the contrib. type prefixes.
$finance_partial_token = array();
$finance_partial_token['___total_balance'] = 'Total Balance';
$finance_partial_token['___total_due___today'] = 'Total Due Today';
$finance_partial_token['___obligations_show_due___today'] = 'Obligations';
$finance_partial_token['___contributions___curfiscalyear'] = 'Contributions, current fiscal year';
$finance_partial_token['___obligations_show_due___today___exclude_after_curfiscalyear_closeditems'] = 'Obligations, Exclude: future fiscal years, closed balances' ;
$finance_partial_token['___obligations_show_due___today___exclude_after_curfiscalyear_closeditems_bal'] = 'Obligations, Exclude: future fiscal years, closed balances, bal col.' ;
$finance_partial_token['___obligations_show_due___today___exclude_after_curfiscalyear_closeditems_bal_adj'] = 'Obligations, Exclude: future fiscal years, closed balances, bal col., adj col.' ;
$finance_partial_token['___obligations_show_due___today___exclude_after_curfiscalyear_closeditems_due'] = 'Obligations, Exclude: future fiscal years, closed balances, due col.' ;
$finance_partial_token['___obligations_show_due___today___exclude_after_curfiscalyear_closeditems_detailrows'] = 'Obligations, Exclude: detail rows, future fiscal years, closed balances' ;
$finance_partial_token['___obligations_show_due___today___exclude_closeditems'] = 'Obligations, Exclude: closed balances' ;
$finance_partial_token['___adjustments___curfiscalyear'] = 'Adjustments, current fiscal year' ;
$finance_partial_token['___prepayments___curfiscalyear'] = 'Prepayments, current fiscal year' ;
while ( $cur_ct_prefix = current($contribution_type_prefixes) ){
reset($finance_partial_token) ;
//print "<br>cur ct prefix: ".$cur_ct_prefix;
//print "<br>cur ct prefix key: ".key($contribution_type_prefixes);
while( $cur_partial_token = current($finance_partial_token)){
//print "<br>cur partial token: ".$cur_partial_token;
$tmp_full_token_key = 'finances.'.key($contribution_type_prefixes).key($finance_partial_token) ;
$tmp_full_token_label = 'Finances: '.$cur_ct_prefix .': '.$cur_partial_token;
//print "<br><br>full token key: ".$tmp_full_token_key;
$financial_tokens[$tmp_full_token_key] = $tmp_full_token_label;
next($finance_partial_token);
}
//print "<br><br>About to move to next, outer loop.";
next($contribution_type_prefixes);
}
}
function financialsummaries_civicrm_tokenValues( &$values, &$contactIDs, $job = null, $tokens = array(), $context = null) {
if ( !empty($tokens['finances']) && CRM_Core_Permission::check('access CiviContribute') ) {
require_once ('utils/FinancialUtils.php');
$tmpFinancialUtils = new FinancialUtils();
require_once('utils/Prepayment.php');
$tmpPrepayment = new Prepayment();
$tmp_token_format = "";
while( $cur_finances_token_raw = current( $tokens['finances'] )){
//print "<br>";
//print "key: ".key($tokens['finances']);
//print " value: ".$cur_finances_token_raw;
//print "<br>";
$tmp_key = key($tokens['finances']);
// CiviCRM is buggy here, if token is being used in CiviMail, we need to use the key
// as the token. Otherwise ( PDF Letter, one-off email, etc) we
// need to use the value.
$cur_finances_token = '';
if( is_numeric( $tmp_key)){
$cur_finances_token = $cur_finances_token_raw;
}else{
// Its being used by CiviMail.
$cur_finances_token = $tmp_key;
}
$token_as_array = explode("___", $cur_finances_token );
$ct_prefix_id = $token_as_array[0];
$ct_prefix_name = $token_as_array[1];
$partial_token = $token_as_array[2];
$token_date = $token_as_array[3];
if( isset( $token_as_array[4] ) ){
$exclusions = $token_as_array[4];
}else{
$exclusions = "";
}
$exclusions_as_array = explode("_" , $exclusions);
//print_r($exclusions_as_array);
$exclude_after_date = "";
if(count($exclusions_as_array) > 2 && $exclusions_as_array[1] == "after"){
$exclude_after_date = $exclusions_as_array[2];
}
//print "<br>prefix id: ".$ct_prefix_id." prefix name: ".$ct_prefix_name." partial token: ".$partial_token ;
//print "<br> current token to fill: ".$cur_finances_token;
$token_to_fill = 'finances.'.$cur_finances_token;
//$output_wanted = $partial_token;
// print "<br>partial token: ".$partial_token ;
require_once ('utils/FormattingUtils.php' );
$tmpFormattingUtils = new FormattingUtils();
require_once( 'utils/FinancialDates.php');
$tmpFinDates = new FinancialDates();
// to make sure obligation tokens do not include closed balances from prior fiscal years.
$tmp_fiscal_start_date_sql = $tmpFinDates->get_current_fiscal_year_start_date("sql");
$where_clause_sql = " ( f1.balance <> 0 OR date(f1.rec_date) >= date(".$tmp_fiscal_start_date_sql.") ) ";
// deal with date parameter within token
$start_date = "";
$end_date = "";
$tmpFormattingUtils->determineDateRange( $token_date , $start_date , $end_date );
if( $partial_token == 'obligations_show_due'){
//print "<br>Exclusions: ".$exclusions;
$tmp = strpos($exclusions, 'closeditems');
//print "<br>tmp: ".$tmp;
if($tmp === false ){
// No mention of excluding closed items, so include them.
$include_closed_items = true;
}else{
// token indicates exlude closeditems.
//print "<br>Token indicates exclude closeditems";
$include_closed_items = false;
}
// full token: obligations_show_due___today___exclude_after_curfiscalyear_closeditems_detailrows
$tmp_details = strpos($exclusions, 'detailrows');
if( $tmp_details === false){
$include_detailrows = true;
}else{
$include_detailrows = false;
}
// TODO: Need to figure out if we should exclude records
// after a certain date, such as the end of the current fiscal year.
$output_wanted = "detail_table";
// Get all available columns setup
$include_cols = array();
$include_cols["date"] = 1;
$include_cols["id"] = 1;
$include_cols["description"] = 1;
$include_cols["tax"] = 1;
$include_cols["billed"] = 1;
$include_cols["recv"] = 1;
$include_cols["adj"] = 1;
$include_cols["bal"] = 1;
$include_cols["due"] = 1;
// remove the ones the user does not want.
$tmp_balcol = strpos($exclusions, 'bal');
if( !( $tmp_balcol === false) ){
unset($include_cols["bal"]);
}else{
//print "<br>user wants bal column";
}
$tmp_adjcol = strpos($exclusions, 'adj');
if( !( $tmp_adjcol === false) ){
unset($include_cols["adj"]);
}else{
//print "<br>user wants adj column";
}
$tmp_duecol = strpos($exclusions, 'due');
if( !( $tmp_duecol === false) ){
unset($include_cols["due"]);
}else{
//print "<br>user wants due column";
}
/*
(&$values, &$contactIDs, &$ct_prefix_id, &$cur_finances_token, &$output_wanted, &$include_closed_items, &$exclude_after_date, &$end_date_raw, &$extra_where_clause)
*/
$tmpFinancialUtils->getObligationsShowSubtotals( $values, $contactIDs, $ct_prefix_id,
$token_to_fill, $output_wanted,
$include_closed_items, $exclude_after_date, $token_date, $where_clause_sql , $include_detailrows, $include_cols ) ;
}else if( $partial_token == 'contributions' ){
//$date_range = 'cur_fiscal_year';
$output_wanted = "detail_table";
require_once('utils/Payment.php');
$tmpPayment = new Payment();
$tmpPayment->getContributionDetails($values, $contactIDs, $ct_prefix_id, $token_to_fill, $output_wanted, $start_date, $end_date, $tmp_token_format);
}else if($partial_token == 'adjustments'){
//$date_range = 'cur_fiscal_year';
$output_wanted = "detail_table";
$tmpFinancialUtils->getAdjustmentDetails($values, $contactIDs, $ct_prefix_id, $token_to_fill, $output_wanted, $start_date, $end_date);
}else if($partial_token == 'prepayments'){
//$date_range = 'cur_fiscal_year';
$output_wanted = "detail_table";
$tmpPrepayment->getPrepaymentDetails($values, $contactIDs, $ct_prefix_id, $token_to_fill , $output_wanted, $start_date, $end_date);
}else if($partial_token == 'total_due'){
$end_date_tmp = $tmpFormattingUtils->getSqlDate($token_date);
$start_date_tmp = '';
$include_automated_payments = false;
$output_wanted = "amount_due";
//$tmpFinancialUtils->getAmountDue($values, $contactIDs, $ct_prefix_id, $token_to_fill, $output_wanted, $end_date_tmp, $start_date_tmp, $include_automated_payments);
$include_detailrows = false;
$tmpFinancialUtils->getObligationsShowSubtotals( $values, $contactIDs, $ct_prefix_id, $token_to_fill, $output_wanted, $include_closed_items, $exclude_after_date, $token_date, $where_clause_sql, $include_detailrows ) ;
}else if($partial_token == 'total_balance'){
$include_closed_items = true;
$output_wanted = "amount_balance";
$include_detailrows = false;
$tmpFinancialUtils->getObligationsShowSubtotals( $values, $contactIDs, $ct_prefix_id, $token_to_fill, $output_wanted, $include_closed_items, $exclude_after_date, $token_date, $where_clause_sql, $include_detailrows) ;
}else{
print "<br>Error: Unrecognized Finances mail merge token: ".$token_to_fill."<br>";
}
next($tokens['finances']);
//print_r($token_as_array);
}
}
}
/**
* Implements hook_civicrm_tabset().
*/
function financialsummaries_civicrm_tabset($tabsetName, &$tabs, $context) {
// if (pogstone_is_user_authorized('access CiviContribute')){
if ($tabsetName === 'civicrm/contact/view') {
$contactID = $context['contact_id'];
$contactIds = [$contactID];
$token_format = "backoffice_screen";
require_once ('utils/FinancialUtils.php');
$tmpFinancialUtils = new FinancialUtils();
$ct_prefix_id = "0"; // get all contribution type data.
//require_once ('utils/FormattingUtils.php' );
//$tmpFormattingUtils = new FormattingUtils();
$token_amount_due_long = 'contact.amount_due';
$start_date_tmp = '';
$today_date = date("Ymd");
$include_automated_payments = false;
$output_wanted = "amount_due";
$tmpFinancialUtils->process_obligation_with_balances_subtotals_tokens2($values, $contactIds, $ct_prefix_id ,$token_amount_due_long , $need_subtotals, $token_format, $today_date,$default_start_date, $need_due_column, $default_exclude_after_date, $output_wanted, $include_closed_items, $where_clause_sql );
$amount_due_by_today = $values[$contactID][$token_amount_due_long] ?? '';
$count_parm = $amount_due_by_today;
//$count_parm = "0";
$url = CRM_Utils_System::url( 'civicrm/fountaintribe/fstab',
"reset=1&snippet=1&force=1&cid=$contactID" );
$tabs[] = [ 'id' => 'financialsummaries',
'url' => $url,
'title' => 'Financial Summary',
'count' => $count_parm,
'weight' => 1,
'icon' => 'crm-i fa-money',
];
}
}
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function financialsummaries_civicrm_config(&$config) {
_financialsummaries_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_xmlMenu().
*
* @param $files array(string)
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
*/
function financialsummaries_civicrm_xmlMenu(&$files) {
_financialsummaries_civix_civicrm_xmlMenu($files);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function financialsummaries_civicrm_install() {
_financialsummaries_civix_civicrm_install();
}
/**
* Implements hook_civicrm_uninstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
*/
function financialsummaries_civicrm_uninstall() {
_financialsummaries_civix_civicrm_uninstall();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function financialsummaries_civicrm_enable() {
_financialsummaries_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_disable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
*/
function financialsummaries_civicrm_disable() {
_financialsummaries_civix_civicrm_disable();
}
/**
* Implements 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
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
*/
function financialsummaries_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _financialsummaries_civix_civicrm_upgrade($op, $queue);
}
/**
* Implements hook_civicrm_managed().
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
*/
function financialsummaries_civicrm_managed(&$entities) {
_financialsummaries_civix_civicrm_managed($entities);
}
/**
* Implements hook_civicrm_caseTypes().
*
* Generate a list of case-types
*
* Note: This hook only runs in CiviCRM 4.4+.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes
*/
function financialsummaries_civicrm_caseTypes(&$caseTypes) {
_financialsummaries_civix_civicrm_caseTypes($caseTypes);
}
/**
* Implements hook_civicrm_angularModules().
*
* Generate a list of Angular modules.
*
* Note: This hook only runs in CiviCRM 4.5+. It may
* use features only available in v4.6+.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes
*/
function financialsummaries_civicrm_angularModules(&$angularModules) {
_financialsummaries_civix_civicrm_angularModules($angularModules);
}
/**
* Implements hook_civicrm_alterSettingsFolders().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
*/
function financialsummaries_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_financialsummaries_civix_civicrm_alterSettingsFolders($metaDataFolders);
}
/**
* Functions below this ship commented out. Uncomment as required.
*
/**
* Implements hook_civicrm_preProcess().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_preProcess
*
function financialsummaries_civicrm_preProcess($formName, &$form) {
}
*/