forked from tttp/CDNTaxReceipts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdntaxreceipts.functions.inc
1334 lines (1136 loc) · 49.1 KB
/
cdntaxreceipts.functions.inc
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
require_once 'tcpdf/tcpdf.php';
require_once('FPDI/fpdi.php');
class PDF extends FPDI {
/**
* "Remembers" the template id of the imported page
*/
var $_tplIdx;
/**
* include a background template for every page
*/
function Header() {
$pdf_template_file = Civi::settings()->get('receipt_pdftemplate');
if (!empty($pdf_template_file)) {
if (is_null($this->_tplIdx)) {
$pdf_template_file = Civi::settings()->get('receipt_pdftemplate');
$this->setSourceFile($pdf_template_file);
$this->_tplIdx = $this->importPage(1);
}
$this->useTemplate($this->_tplIdx);
}
else {
$this->parsers = array();
}
}
function Footer() {}
}
const CDNTAX_SETTINGS = 'CDNTaxReceipts';
const CDNTAX_DELIVERY_PRINT_ONLY = 0;
const CDNTAX_DELIVERY_PRINT_EMAIL = 1;
const CDNTAX_DELIVERY_DATA_ONLY = 2;
/**
* processTaxReceipt()
* Accepts an associative array containing receipt variables, and:
* - generates a PDF file using the provided variables
* - sends a copy of the receipt to the email archive
* - emails the receipt to the donor if issue_method='email'
* - logs the receipt to the audit log
*
* This method is common to both single and annual receipts.
*/
function cdntaxreceipts_processTaxReceipt($receipt, &$collectedPdf = NULL, $mode, $method = NULL) {
// Get contact details
list($displayname, $email) = CRM_Contact_BAO_Contact::getContactDetails($receipt['contact_id']);
// add support for preferred language
$old_locale = CRM_Core_I18n::singleton()->getLocale();
$preferred_language = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $receipt['contact_id'], 'preferred_language');
_cdntaxreceipts_setReceiptanguage($preferred_language);
$serial_receipt = Civi::settings()->get('receipt_serial');
if ($serial_receipt && empty($receipt['is_duplicate'])) {
// use my experimental alternative to generating serial receipt numbers, unless this is a re-issue
$lock = new CRM_Core_Lock('cdntaxreceipts.processTaxReceipt');
if (!$lock->acquire()) {
$userAlert = ts('Failed to acquire lock. No receipts were processed.');
CRM_Core_Session::setStatus($userAlert, ts('Warning'), 'alert');
return array(FALSE,'',NULL);
}
$next_id = cdntaxreceipts_log_next_id();
$receipt['receipt_no'] = Civi::settings()->get('receipt_prefix') . str_pad($next_id, 8, 0, STR_PAD_LEFT);
}
// allow modules to alter any details
// invoke hook_cdntaxreceipts_alter_receipt(&$receipt)
// modules should accept a reference to the $receipt array and alter it directly if they wish
// one useful use case: alter the $receipt['source'] line to include custom text
// to alter receipt amount, use hook_cdntaxreceipts_eligibleAmount() instead
CRM_Utils_Hook::singleton()->invoke(1, $receipt, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, 'cdntaxreceipts_alter_receipt');
if ($receipt['issue_method'] == 'data') {
// generate log entry only, no PDF file needed
$pdf_file = NULL;
$sent = TRUE;
}
else {
// generate the PDF file
list($pdf_file, $user_friendly) = cdntaxreceipts_generateFormattedReceipt($receipt, $collectedPdf, $mode);
if ($mode == CDNTAXRECEIPTS_MODE_PREVIEW) {
// no email, just PDF
$sent = TRUE;
}
else {
// generate an email to the tax archive and possibly to the donor
$org_name = Civi::settings()->get('org_name');
$email_from = Civi::settings()->get('email_from');
$email_archive = Civi::settings()->get('email_archive');
// get all the basic contact information
$params = array(
'id' => $receipt['contact_id'],
);
$contact = civicrm_api3('contact', 'getsingle', $params);
// add in the greetings (not return as part of the api result)
$params = array(
'id' => $receipt['contact_id'],
'return.email_greeting' => 1,
);
$greetings = civicrm_api3('contact', 'getsingle', $params);
$contact['email_greeting_display'] = $greetings['email_greeting_display'];
$attachment = array(
'fullPath' => $pdf_file,
'mime_type' => 'application/pdf',
'cleanName' => $user_friendly,
);
$tplParams = array(
'orgName' => $org_name,
'contact' => $contact,
'receipt' => $receipt,
);
$sendTemplateParams = array(
'groupName' => 'msg_tpl_workflow_cdntaxreceipts',
'contactId' => $receipt['contact_id'],
'tplParams' => $tplParams,
'from' => $org_name . ' <' . $email_from . '> ',
'toName' => $contact['display_name'],
'attachments' => array( $attachment ),
);
if ($receipt['issue_type'] == 'single') {
$sendTemplateParams['valueName'] = 'cdntaxreceipts_receipt_single';
}
else {
$sendTemplateParams['valueName'] = 'cdntaxreceipts_receipt_aggregate';
}
// send a copy (without open tracking) to the tax archive
$sendTemplateParams['toEmail'] = $email_archive;
list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
// send a copy (with open tracking) to the donor
if ($sent && $receipt['issue_method'] == 'email' && $mode != CDNTAXRECEIPTS_MODE_WORKFLOW) {
$sendTemplateParams['toEmail'] = $contact['email'];
$sendTemplateParams['tplParams']['openTracking'] = "<img src=\"" . CRM_Utils_System::baseURL() .
"civicrm/cdntaxreceipts/open?r={$receipt['email_tracking_id']}\" width='1' height='1' alt='' border='0'>";
list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
}
}
}
if (!empty($lock)) {
$lock->release();
}
// Switch the locale back to the original locale
CRM_Core_I18n::singleton()->setLocale($old_locale);
// log the receipt
if (!$receipt['is_duplicate'] && $sent && $mode != CDNTAXRECEIPTS_MODE_PREVIEW) {
cdntaxreceipts_log($receipt);
// TODO: log an activity
}
if (isset($collectedPdf) && $pdf_file) {
unlink($pdf_file); // file is no longer needed
$pdf_file = NULL;
}
// if not unset, cron will clean up the PDF file later on
return array( $sent, $receipt['issue_method'], $pdf_file );
}
/**
* generateFormattedReceipt()
* Accepts an associative array of receipt variables and produces a PDF file.
* This function should always be called through processTaxReceipt(). It is imperative
* that each receipt generated by the system gets logged and archived properly.
*/
function cdntaxreceipts_generateFormattedReceipt($receipt, &$collectedPdf = NULL, $mode) {
require_once 'tcpdf/tcpdf.php';
$pdf_img_files_path = dirname(__FILE__) . '/img/';
$address = cdntaxreceipts_getAddress($receipt['contact_id']);
$address_line_1 = isset($address['street_address']) ? $address['street_address'] : '';
$parts = array();
foreach(array('city', 'state_province', 'postal_code') as $addr_part) {
if (isset($address[$addr_part])) {
$parts[] = $address[$addr_part];
}
}
$address_line_1b = isset($address['supplemental_address_1']) ? $address['supplemental_address_1'] : '';
$address_line_2 = implode(' ', $parts);
$address_line_3 = isset($address['country']) ? $address['country'] : '';
list($displayname, $email) = CRM_Contact_BAO_Contact::getContactDetails($receipt['contact_id']);
$result_get_name = civicrm_api3('Contact', 'get', array(
'sequential' => 1,
'return' => array("first_name", "last_name", "middle_name","contact_type"),
'id' => $receipt['contact_id'],
));
if ($result_get_name['values'][0]['contact_type'] == 'Individual') {
if (strlen(trim($result_get_name['values'][0]['middle_name'])) > 0) {
$displayname = $result_get_name['values'][0]['first_name'] . ' ' . $result_get_name['values'][0]['middle_name'][0] . ' ' . $result_get_name['values'][0]['last_name'];
}
else {
$displayname = $result_get_name['values'][0]['first_name'] . ' ' . $result_get_name['values'][0]['last_name'];
}
}
if ( $receipt['issue_type'] == 'single' ) {
$pos = strpos($receipt['receive_date'], '-');
if ($pos === FALSE) {
$date = substr($receipt['receive_date'], 0, 8);
$display_date = substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-' . substr($date, 6, 2);
}
else {
$display_date = substr($receipt['receive_date'], 0, 10);
}
$display_year = '';
}
else {
$display_year = $receipt['receive_date'];
$display_date = date('M j, Y', mktime(0,0,0,1,1,$receipt['receive_date'])) . ' - ' . date('M j, Y', mktime(0,0,0,12,31,$receipt['receive_date']));
}
$line_1 = ts("This is your Official Receipt for income tax purposes.", array('domain' => 'org.civicrm.cdntaxreceipts'));
$config = CRM_Core_Config::singleton();
$pdf_file = $config->uploadDir . 'Receipt-' . $receipt['receipt_no'] . '.pdf';
$user_friendly = 'Receipt-' . $receipt['receipt_no'] . '.pdf';
if (!defined('pdf_page_format')) {
define('PDF_PAGE_FORMAT', 'USLETTER', true); // orig: 'A4'
}
$pdf = new PDF(PDF_PAGE_ORIENTATION, PDF_UNIT, 'LETTER', TRUE, 'UTF-8', FALSE);
$pdf->Open();
$pdf->SetAuthor(Civi::settings()->get('org_name'));
$mymargin_left = 12;
$mymargin_top = 6;
$mymargin_right = 12;
$pdf->SetMargins($mymargin_left, $mymargin_top, $mymargin_right);
$pdf->setJPEGQuality('100');
$pdf->SetAutoPageBreak('', $margin=0);
$pdf_variables = array(
"mode" => $mode,
"mymargin_left" => $mymargin_left,
"mymargin_top" => $mymargin_top,
"is_duplicate" => $receipt['is_duplicate'],
"pdf_img_files_path" => $pdf_img_files_path,
"line_1" => $line_1,
"source_funds" => isset($receipt['source']) ? $receipt['source'] : '',
"amount" => $receipt['receipt_amount'],
"display_date" => $display_date,
"display_year" => $display_year,
"issued_on" => date('Y-m-d', $receipt['issued_on']),
"issue_type" => $receipt['issue_type'],
"receipt_number" => $receipt['receipt_no'],
"displayname" => $displayname,
"address_line_1" => $address_line_1,
"address_line_1b" => $address_line_1b,
"address_line_2" => $address_line_2,
"address_line_3" => $address_line_3,
"inkind_values" => isset($receipt['inkind_values']) ? $receipt['inkind_values'] : array(),
"receipt_contributions" => $receipt['contributions'],
"receipt_status" => $receipt['receipt_status'],
);
$output_files = array($pdf);
if ( isset($collectedPdf) && ( $receipt['issue_method'] == 'print' || $mode == CDNTAXRECEIPTS_MODE_PREVIEW) ) {
$output_files[] = $collectedPdf;
}
foreach ( $output_files as $f ) {
$f->AddPage();
_cdntaxreceipts_writePage($f, $pdf_variables, $receipt);
}
// close and output the single receipt, but not the collected receipts...
$pdf->Close();
$pdf->Output($pdf_file, 'F');
return array($pdf_file, $user_friendly);
}
function _cdntaxreceipts_writePage(&$f, $pdf_variables, $receipt) {
//
// hook_cdntaxreceipts_writeReceipt(&$pdf, $pdf_variables, $receipt)
//
// If there's a custom function to write the page, invoke it.
// Otherwise, provide a default receipt format.
//
// Note: if hook_cdntaxreceipts_writeReceipt is implemented in more than one active
// module/extension, the receipt will get written into the PDF file multiple
// times. It's one peril of using the hook system here. Implementers will need to catch
// this when testing their particular installs.
//
$results = CRM_Utils_Hook::singleton()->invoke(3, $f, $pdf_variables, $receipt, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, 'cdntaxreceipts_writeReceipt');
if (is_array($results)) {
foreach ($results as $result) {
if ($result == TRUE) {
return;
}
}
}
// if no custom function, go with default formatting...
if ($pdf_variables['is_duplicate']) {
// print duplicate copy
_cdntaxreceipts_writeReceipt($f, $pdf_variables);
}
else {
// print original AND duplicate copy
$mymargin_top = $pdf_variables['mymargin_top'];
_cdntaxreceipts_writeReceipt($f, $pdf_variables);
$pdf_variables["mymargin_top"] = $mymargin_top + 90;
$pdf_variables["is_duplicate"] = TRUE;
_cdntaxreceipts_writeReceipt($f, $pdf_variables);
$pdf_variables["mymargin_top"] = $mymargin_top + 90*2;
$pdf_variables["is_duplicate"] = TRUE;
_cdntaxreceipts_writeReceipt($f, $pdf_variables);
}
}
function _cdntaxreceipts_writeReceipt(&$pdf, $pdf_variables) {
// Extract variables
$mode = $pdf_variables["mode"];
$mymargin_left = $pdf_variables["mymargin_left"];
$mymargin_top = $pdf_variables["mymargin_top"];
$is_duplicate = $pdf_variables["is_duplicate"];
$pdf_img_files_path = $pdf_variables["pdf_img_files_path"];
$line_1 = $pdf_variables["line_1"];
$source_funds = $pdf_variables["source_funds"];
$amount = $pdf_variables["amount"];
$display_date = $pdf_variables["display_date"];
$issued_on = $pdf_variables["issued_on"];
$receipt_number = $pdf_variables["receipt_number"];
$displayname = $pdf_variables["displayname"];
$address_line_1 = $pdf_variables["address_line_1"];
$address_line_1b = $pdf_variables["address_line_1b"];
$address_line_2 = $pdf_variables["address_line_2"];
$address_line_3 = $pdf_variables["address_line_3"];
$inkind_values = $pdf_variables["inkind_values"];
$display_year = $pdf_variables["display_year"];
$issue_type = $pdf_variables["issue_type"];
$receipt_contributions = $pdf_variables['receipt_contributions'];
$receipt_status = $pdf_variables['receipt_status'];
// Middle center section
if ($mode == CDNTAXRECEIPTS_MODE_PREVIEW) {
$pdf->Image($pdf_img_files_path . 'preview_mode.png', $mymargin_left + 65, $mymargin_top, '', 45);
}
else if ($receipt_status == 'cancelled') {
$pdf->Image($pdf_img_files_path . 'cancelled_trans.png', $mymargin_left + 65, $mymargin_top, '', 45);
}
else if ($is_duplicate) {
$pdf->Image($pdf_img_files_path . 'duplicate_trans.png', $mymargin_left + 65, $mymargin_top, '', 45);
}
// Top left section
$pdf_template_file = Civi::settings()->get('receipt_pdftemplate');
if (!empty($pdf_template_file)) {
}
else {
$receipt_logo = Civi::settings()->get('receipt_logo');
if ( empty($receipt_logo) ) {
$receipt_logo = $pdf_img_files_path . 'your-logo.png';
}
$pdf->Image($receipt_logo, $mymargin_left, $mymargin_top, '', 30);
// Top right section
$pdf->SetFont('Helvetica', '', 8);
$pdf->SetY($mymargin_top);
$pdf->Write(10, Civi::settings()->get('org_name'), '', 0, 'R', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetY($mymargin_top + 4);
$pdf->Write(10, Civi::settings()->get('org_address_line1'), '', 0, 'R', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetY($mymargin_top + 8);
$pdf->Write(10, Civi::settings()->get('org_address_line2'), '', 0, 'R', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetY($mymargin_top + 12);
$telTxt = ts('Tel: %1', array(1 => Civi::settings()->get('org_tel'), 'domain' => 'org.civicrm.cdntaxreceipts'));
if ( Civi::settings()->get('org_fax' ) != '' ) {
$telTxt .= '; ' . ts('Fax: %1', array(1 => Civi::settings()->get('org_fax'), 'domain' => 'org.civicrm.cdntaxreceipts'));
}
$pdf->Write(10, $telTxt, '', 0, 'R', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetFont('Helvetica', 'I', 8);
$pdf->SetY($mymargin_top + 16);
$emailTxt = ts('Email: %1; Website: %2', array(
1 => Civi::settings()->get('org_email'),
2 => Civi::settings()->get('org_web'),
'domain' => 'org.civicrm.cdntaxreceipts')
);
$registrationTxt = ts('Charitable Registration: %1', array(1 => Civi::settings()->get('org_charitable_no'), 'domain' => 'org.civicrm.cdntaxreceipts'));
$pdf->Write(10, $emailTxt, '', 0, 'R', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetY($mymargin_top + 20);
$pdf->Write(10, $registrationTxt, '', 0, 'R', TRUE, 0, FALSE, FALSE, 0);
}
// Right section
$x_detailscolumn = 120;
$y_detailscolumnstart = 22;
$pdf_template_file = Civi::settings()->get('receipt_pdftemplate');
if (!empty($pdf_template_file)) {
}
else {
$background_image = Civi::settings()->get('receipt_watermark');
if ( $background_image ) $pdf->Image(Civi::settings()->get('receipt_watermark'), $mymargin_left + $x_detailscolumn, $mymargin_top + $y_detailscolumnstart + 6, '', 40);
}
$pdf->SetXY($mymargin_left + $x_detailscolumn, $mymargin_top + $y_detailscolumnstart + 6);
$pdf->SetFont('Helvetica', 'B', 8);
$pdf->Write(10, ts("Issue Date: %1", array(1 => $issued_on, 'domain' => 'org.civicrm.cdntaxreceipts')));
$pdf->SetXY($mymargin_left + $x_detailscolumn, $mymargin_top + $y_detailscolumnstart + 12);
$pdf->Write(10, ts("Received on: %1", array(1 => $display_date, 'domain' => 'org.civicrm.cdntaxreceipts')));
$pdf->SetXY($mymargin_left + $x_detailscolumn, $mymargin_top + $y_detailscolumnstart + 18);
$pdf->Write(10, ts("Eligible Amount: $%1", array(1 => number_format($amount, 2), 'domain' => 'org.civicrm.cdntaxreceipts')));
$pdf->SetXY($mymargin_left + $x_detailscolumn, $mymargin_top + $y_detailscolumnstart + 34);
if ($issue_type == 'annual') {
$pdf->Write(10, ts("Thank you for giving in %1!", array( 1 => $display_year)));
// $pdf->Write(10, "Contribution(s): ");
// foreach ($receipt_contributions as $key => $value) {
// $test = $receipt_contributions[$key]['contribution_id'];
// $pdf->Write(10, $receipt_contributions[$key]['contribution_id'] . ' ');
// }
}
else if (strlen($source_funds) < 36 && strlen($source_funds) > 0) {
$pdf->Write(10, $source_funds);
}
else if (strlen($source_funds) > 0) {
$source_funds_words = explode(" ", substr($source_funds, 0, 36));
$source_funds_lastbit = array_pop($source_funds_words);
$pdf->Write(10, implode(" ", $source_funds_words));
$source_funds_count = count($source_funds_words);
// $source_funds_nextline = array_splice(explode(" ", $source_funds), $source_funds_count);
$exploded = explode(" ", $source_funds);
$source_funds_nextline = array_splice($exploded, $source_funds_count);
$pdf->SetXY($mymargin_left + $x_detailscolumn + 16, $mymargin_top + $y_detailscolumnstart + 38);
$pdf->Write(10, implode(" ", $source_funds_nextline));
}
// Left section
$pdf->SetFont('Helvetica', 'B', 8);
$pdf->SetY($mymargin_top + $y_detailscolumnstart + 6);
$pdf->Write(10, ts("Receipt No: %1", array(1 => $receipt_number, 'domain' => 'org.civicrm.cdntaxreceipts')));
$pdf->SetFont('Helvetica', 'B', 8);
$pdf->SetY($mymargin_top + $y_detailscolumnstart + 10);
$pdf->Write(10, ts("Received from: ", array('domain' => 'org.civicrm.cdntaxreceipts')), '', 0, 'L', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetFont('Helvetica', 'B', 10);
$pdf->SetXY($mymargin_left+10, $mymargin_top + $y_detailscolumnstart + 20);
$pdf->Write(10, strtoupper($displayname), '', 0, 'L', TRUE, 0, FALSE, FALSE, 0);
if (!$address_line_1b){
$pdf->SetFont('Helvetica', '', 10);
$pdf->SetXY($mymargin_left+10, $mymargin_top + $y_detailscolumnstart + 24);
$pdf->Write(10, strtoupper($address_line_1), '', 0, 'L', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetXY($mymargin_left+10, $mymargin_top + $y_detailscolumnstart + 28);
$pdf->Write(10, strtoupper($address_line_2), '', 0, 'L', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetXY($mymargin_left+10, $mymargin_top + $y_detailscolumnstart + 32);
$pdf->Write(10, strtoupper($address_line_3), '', 0, 'L', TRUE, 0, FALSE, FALSE, 0);
}
else {
$pdf->SetFont('Helvetica', '', 10);
$pdf->SetXY($mymargin_left+10, $mymargin_top + $y_detailscolumnstart + 24);
$pdf->Write(10, strtoupper($address_line_1), '', 0, 'L', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetXY($mymargin_left+10, $mymargin_top + $y_detailscolumnstart + 28);
$pdf->Write(10, strtoupper($address_line_1b), '', 0, 'L', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetXY($mymargin_left+10, $mymargin_top + $y_detailscolumnstart + 32);
$pdf->Write(10, strtoupper($address_line_2), '', 0, 'L', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetXY($mymargin_left+10, $mymargin_top + $y_detailscolumnstart + 36);
$pdf->Write(10, strtoupper($address_line_3), '', 0, 'L', TRUE, 0, FALSE, FALSE, 0);
}
// If this is an in-kind donation
if ( !empty($inkind_values) ) {
$inkind_str = ts('Donation in-kind: %1', array(1 => $inkind_values[0], 'domain' => 'org.civicrm.cdntaxreceipts'));
if (!empty($inkind_values[3])) {
if ($inkind_values[3] < $amount) {
$inkind_str .= ' - ' . ts('Cost: %1', array(1 => $inkind_values[3], 'domain' => 'org.civicrm.cdntaxreceipts'));
}
}
$pdf->SetFont('Helvetica', '', 8);
$pdf->SetY($mymargin_top + 68);
$pdf->Write(10, $inkind_str, ' - ', 0, 'L', TRUE, 0, FALSE, FALSE, 0);
$appraiser = $inkind_values[1] . ' - '. $inkind_values[2];
$pdf->SetFont('Helvetica', 'B', 8);
$pdf->SetXY($mymargin_left + $x_detailscolumn, $mymargin_top + $y_detailscolumnstart + 24);
if (strlen($appraiser) < 36) {
$pdf->Write(10, ts("Appraiser: %1", array(1 => $appraiser, 'domain' => 'org.civicrm.cdntaxreceipts')));
}
else {
$appraiser_words = explode(" ", substr($appraiser, 0, 36));
$appraiser_lastbit = array_pop($appraiser_words);
$pdf->Write(10, ts("Appraiser: %1", array(1 => implode(" ", $appraiser_words), 'domain' => 'org.civicrm.cdntaxreceipts')));
$appraiser_count = count($appraiser_words);
$appraiser_nextline = array_splice(explode(" ", $appraiser), $appraiser_count);
$pdf->SetXY($mymargin_left + $x_detailscolumn + 16, $mymargin_top + $y_detailscolumnstart + 28);
$pdf->Write(10, implode(" ", $appraiser_nextline));
}
}
// Bottom left section
if (!empty($pdf_template_file)) {
}
else {
$pdf->SetFont('Helvetica', 'B', 8);
$pdf->SetY($mymargin_top + 72);
$pdf->Write(10, $line_1, '', 0, 'L', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetFont('Helvetica', '', 8);
$pdf->SetY($mymargin_top + 76, 0, 'L', TRUE, 0, FALSE, FALSE, 0);
$pdf->Write(10, ts('Canada Revenue Agency: canada.ca/charities-giving', array('domain' => 'org.civicrm.cdntaxreceipts')));
// Bottom center section
$pdf->SetFont('Helvetica', 'B', 8);
$pdf->SetXY($mymargin_left + 92, $mymargin_top + 74);
$pdf->Write(10, ts('Thank you!', array('domain' => 'org.civicrm.cdntaxreceipts')));
// Bottom right section
$signature = Civi::settings()->get('receipt_authorized_signature_text');
if ( $signature == '' ) {
$signature = ts('Authorized Signature', array('domain' => 'org.civicrm.cdntaxreceipts'));
}
$sig_offset = strlen($signature) - 20;
$pdf_template_file = Civi::settings()->get('receipt_pdftemplate');
$sig_image = Civi::settings()->get('receipt_signature');
if (empty($sig_image)) {
$sig_image = $pdf_img_files_path . 'authorized-signature.png';
}
$pdf->Image($sig_image, $mymargin_left + 137, $mymargin_top + 67, '', 15);
$pdf->Line($mymargin_left + 136, $mymargin_top + 79, $mymargin_left + 186, $mymargin_top + 79);
$pdf->SetXY($mymargin_left + 148 - $sig_offset, $mymargin_top + 76);
$pdf->SetFont("Helvetica", "I", 7);
$pdf->Write(10, $signature);
}
// Line at the bottom
$pdf->Line($mymargin_left, $mymargin_top + 85, 198, $mymargin_top + 85, 'dash');
}
/**************************************
* SECTION: Utility Functions
*/
/**
* set up the custom field structure for In-Kind tax receipts
*/
function cdntaxreceipts_configure_inkind_fields() {
// check if the In-kind contribution type exists. if not, create it.
require_once 'CRM/Financial/DAO/FinancialType.php';
$dao = new CRM_Financial_DAO_FinancialType();
$dao->name = 'In-kind';
if ( ! $dao->find(true) ) {
$dao->name = 'In-kind';
$dao->is_deductible = TRUE;
$dao->is_active = TRUE;
$dao->save();
}
$contrib_type_id = $dao->id;
// check if the custom group exists. if not, create it.
$params = array(
'title' => 'In-kind donation fields',
'version' => 3,
);
require_once 'api/api.php';
$result = civicrm_api( 'custom_group', 'get', $params );
if ( $result['count'] == 0 ) {
$group = array(
'title' => 'In-kind donation fields',
'extends' => array( 'Contribution' ),
'extends_entity_column_value' => array( $contrib_type_id ),
'collapse_display' => 0,
'style' => 'Inline',
'is_active' => 1,
'version' => 3
);
$result =& civicrm_api('custom_group', 'create', $group);
}
foreach ( $result['values'] as $id => $detail ) {
$custom_group_id = $id;
}
// check if the custom fields exist. if not, create them.
foreach ( array( 'Description of property', 'Appraised by', 'Address of Appraiser', 'Original cost' ) as $field ) {
$params = array(
'custom_group_id' => $custom_group_id,
'label' => $field,
'version' => 3,
);
$result = civicrm_api( 'custom_field', 'get', $params);
if ( $result['count'] == 0 ) {
$field_params = array(
'custom_group_id' => $custom_group_id,
'label' => $field,
'data_type' => 'String',
'html_type' => 'Text',
'is_required' => 1,
'is_active' => 1,
'version' => 3,
);
if ( $field == 'Original cost' ) {
$field_params['is_required'] = 0;
$field_params['help_post'] = 'Amount originally paid for the item. This item is needed for a special rule'.
' that applies if either something was purchased in the last ten years'.
' with the intent of making a donation, or if an item is donated within three years'.
' of being purchased. In both cases the amount that can be claimed is the lesser'.
' of the market value, and the amount paid for the item. This item is used to show'.
' the cost of the item donated (and the creditable amount) if either of those '.
' situations apply.';
}
$result =& civicrm_api('custom_field', 'create', $field_params);
}
}
}
/**************************************
* SECTION: Tax Receipt API
*/
/**
* issueTaxReceipt()
* Issues a tax receipt for a single contribution.
* Detects if the receipt is a duplicate and reacts appropriately.
*/
function cdntaxreceipts_issueTaxReceipt($contribution, &$collectedPdf = NULL, $mode = CDNTAXRECEIPTS_MODE_BACKOFFICE) {
// This function basically needs to set up a $receipt[] array and pass it to
// processTaxReceipt() which does the hard work
$receipt = array();
// check if a Tax Receipt has already been issued previously for this Contribution
// user input could be tainted! we want this method to check everything for validity...
list($issued_on, $receipt_id) = cdntaxreceipts_issued_on($contribution->id);
// When returning issued_on field:
// returns false for non existing $contribution ID
// returns 0 for existing but old data
// returns timestamp for new data
$is_duplicate = empty($issued_on) ? 0 : 1;
if ( $is_duplicate ) {
// if this was issued as part of an annual receipt, then jump over to the Annual method
$receipt = cdntaxreceipts_load_receipt($receipt_id);
if ( $receipt['issue_type'] == 'annual' ) {
$contactId = $receipt['contact_id'];
$year = substr($receipt['contributions'][0]['receive_date'], 0, 4);
return cdntaxreceipts_issueAnnualTaxReceipt($contactId, $year, $collectedPdf, $mode);
}
else if ( $receipt['issue_type'] == 'aggregate' ) {
$contactId = $receipt['contact_id'];
$year = substr($receipt['contributions'][0]['receive_date'], 0, 4);
list($method, $email) = cdntaxreceipts_sendMethodForContact($contactId);
return cdntaxreceipts_issueAggregateTaxReceipt($contactId, $year, $receipt['contributions'],
$method, $collectedPdf, $mode);
}
// be sure I'm not changing the receipt number
$receipt_no = $receipt['receipt_no'];
}
else { // generate a receipt number
$receipt_no = Civi::settings()->get('receipt_prefix') . str_pad($contribution->id, 8, 0, STR_PAD_LEFT);
}
// determine the send method
list($method, $email) = cdntaxreceipts_sendMethodForContact($contribution->contact_id);
// Cancelled receipts are not sent via email
if ($is_duplicate && $receipt['receipt_status'] == 'cancelled' && $method != 'data') {
$method = 'print';
}
// "Attach" receipts are email (we save the file & attach to an email)
if ($mode == CDNTAXRECEIPTS_MODE_WORKFLOW) {
$method = 'email';
}
// process In-Kind variables if this is a receipt for an in-kind contribution
$contributiontype = _cdntaxreceipts_get_type_for_contribution($contribution);
$inkind_values = array();
// check if this is an 'In-kind" contribution.
if ( $contributiontype->name == 'In-kind' ) {
// in this case get the custom field values:
require_once 'CRM/Core/BAO/CustomField.php';
$groupTitle = 'In-kind donation fields';
$fieldLabel_description = 'Description of property';
$customFieldID_description = CRM_Core_BAO_CustomField::getCustomFieldID( $fieldLabel_description, $groupTitle );
$fieldLabel_appraisedby = 'Appraised by';
$customFieldID_appraisedby = CRM_Core_BAO_CustomField::getCustomFieldID( $fieldLabel_appraisedby, $groupTitle );
$fieldLabel_appraiseraddress = 'Address of Appraiser';
$customFieldID_appraiseraddress = CRM_Core_BAO_CustomField::getCustomFieldID( $fieldLabel_appraiseraddress, $groupTitle );
$fieldLabel_cost = 'Original cost';
$customFieldID_cost = CRM_Core_BAO_CustomField::getCustomFieldID( $fieldLabel_cost, $groupTitle );
require_once 'CRM/Core/BAO/CustomValueTable.php';
$custom_id = 'custom_' . $customFieldID_description;
$params = array('entityID' => $contribution->id, $custom_id => 1);
$values = CRM_Core_BAO_CustomValueTable::getValues($params);
$inkind_values[] = $values[$custom_id];
$custom_id = 'custom_' . $customFieldID_appraisedby;
$params = array('entityID' => $contribution->id, $custom_id => 1);
$values = CRM_Core_BAO_CustomValueTable::getValues($params);
$inkind_values[] = $values[$custom_id];
$custom_id = 'custom_' . $customFieldID_appraiseraddress;
$params = array('entityID' => $contribution->id, $custom_id => 1);
$values = CRM_Core_BAO_CustomValueTable::getValues($params);
$inkind_values[] = $values[$custom_id];
$custom_id = 'custom_' . $customFieldID_cost;
$params = array('entityID' => $contribution->id, $custom_id => 1);
$values = CRM_Core_BAO_CustomValueTable::getValues($params);
$inkind_values[] = $values[$custom_id];
}
$receipt = array(
'receipt_no' => $receipt_no,
'issued_on' => (int) $_SERVER['REQUEST_TIME'],
'contact_id' => $contribution->contact_id,
'receipt_amount' => cdntaxreceipts_eligibleAmount($contribution->id),
'is_duplicate' => $is_duplicate,
'issue_type' => 'single',
'issue_method' => $method,
'receive_date' => $contribution->receive_date,
'inkind_values' => $inkind_values,
'receipt_status' => ($is_duplicate && isset($receipt)) ? $receipt['receipt_status'] : 'issued',
'email_tracking_id' => md5(uniqid(rand(), TRUE)),
);
if ( $contribution->source != '' ) {
$receipt['source'] = 'Source: ' . $contribution->source;
}
$receipt['contributions'] = array(
array(
'contribution_id' => $contribution->id,
'contribution_amount' => $contribution->total_amount,
'receipt_amount' => $receipt['receipt_amount'],
'receive_date' => $contribution->receive_date,
)
);
return cdntaxreceipts_processTaxReceipt($receipt, $collectedPdf, $mode);
}
function cdntaxreceipts_eligibleForReceipt( $contributionId ) {
// This function may be called multiple times throughout a request lifecycle, for the
// same contribution. This can be resource intensive, since it must load the Contribution
// object each time and will invoke any custom hooks, which may themselves hit the DB. This
// adds up quickly, particularly during bulk runs. The basic eligibility of a
// contribution will not change through the request lifecycle, so we can cache those we
// have already computed.
static $cache = array();
if ( array_key_exists($contributionId, $cache) ) {
return $cache[$contributionId];
}
require_once('CRM/Contribute/DAO/Contribution.php');
$contribution = new CRM_Contribute_DAO_Contribution();
$contribution->id = $contributionId;
if ( ! $contribution->find( TRUE ) ) {
CRM_Core_Error::fatal( "CDNTaxReceipts: Could not retrieve details for this contribution" );
}
// 1. check that contribution amount is deductible
$deductibleAmount = cdntaxreceipts_eligibleAmount($contributionId);;
// 2. we used to check here the financial type -- to see if it is deductible.
// Since the introduction of split transactions (4.5 is the cleanest starting point, though they existed prior),
// we no longer check the financial type of the contribution, and check instead the tax eligibility of each
// line item. This is done in the eligibleAmount() function.
// 3. check if the Contribution Status is Completed ( i.e. = 1 )
$contributionStatus = $contribution->contribution_status_id;
if ( !isset($contributionStatus) ) {
CRM_Core_Error::fatal( "CDNTaxReceipts: Could not find status of this Contribution." );
}
// invoke hook_cdntaxreceipts_eligible:
// module implementations should return an array containing a single item,
// either TRUE or FALSE. assumes TRUE until we receive a FALSE. one FALSE
// disqualifies the contribution for a tax receipt.
$results = CRM_Utils_Hook::singleton()->invoke(1, $contribution, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, 'cdntaxreceipts_eligible');
$hookOk = TRUE;
if (is_array($results)) {
foreach ( $results as $result ) {
if ( $result == FALSE ) {
$hookOk = FALSE;
}
}
}
if ( $contributionStatus == '1' && $contribution->is_test == 0 && $deductibleAmount > 0 && $hookOk ) {
$cache[$contributionId] = TRUE;
}
else {
$cache[$contributionId] = FALSE;
}
return $cache[$contributionId];
}
function cdntaxreceipts_eligibleAmount( $contributionId ) {
// This function may be called multiple times throughout a request lifecycle, for the
// same contribution. This can be resource intensive, since it must load the Contribution
// object each time and will invoke any custom hooks, which may themselves hit the DB. This
// adds up quickly, particularly during bulk runs. The basic eligibility amount of a
// contribution will not change through the request lifecycle, so we can cache those we
// have already computed.
static $cache = array();
if ( array_key_exists($contributionId, $cache) ) {
return $cache[$contributionId];
}
require_once('CRM/Contribute/DAO/Contribution.php');
$contribution = new CRM_Contribute_DAO_Contribution();
$contribution->id = $contributionId;
if ( ! $contribution->find( TRUE ) ) {
CRM_Core_Error::fatal( "CDNTaxReceipts: Could not retrieve details for this contribution" );
}
// 1. calculate deductible amount
$deductibleAmount = 0;
// compute by line item in 4.5+ if there are line items
// (if the contribution was done pre-upgrade, it might not have line items)
$line_item = new CRM_Price_DAO_LineItem();
$line_item->contribution_id = $contributionId;
$count = $line_item->find();
if ($count > 0 && $contribution->total_amount > 0) {
while ( $line_item->fetch() ) {
$financialType = _cdntaxreceipts_get_type_for_line_item($line_item);
if ( $financialType->is_deductible ) {
$deductibleAmount += $line_item->line_total;
}
}
}
else {
// compute for overall contribution
$financialType = _cdntaxreceipts_get_type_for_contribution($contribution);
if ( $financialType->is_deductible ) {
$deductibleAmount = $contribution->total_amount;
}
}
// account for manually-entered non_deductible_amount
$deductibleAmount = $deductibleAmount - $contribution->non_deductible_amount;
if ( $deductibleAmount < 0 ) {
$deductibleAmount = 0;
}
// 2. allow modules to alter the amount. lowest amount wins.
$results = CRM_Utils_Hook::singleton()->invoke(1, $contribution, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, 'cdntaxreceipts_eligibleAmount');
if (is_array($results)) {
foreach ( $results as $result ) {
if ( $result < $deductibleAmount ) {
$deductibleAmount = $result;
}
}
}
$cache[$contributionId] = $deductibleAmount;
return $deductibleAmount;
}
/**
* issueAnnualTaxReceipt()
* Issues an annual tax receipt for a given year.
* Detects if the receipt is a duplicate and reacts appropriately.
*/
function cdntaxreceipts_issueAnnualTaxReceipt( $contactId, $year, &$collectedPdf = NULL, $mode = CDNTAXRECEIPTS_MODE_BACKOFFICE ) {
// determine the send method
list( $method, $email ) = cdntaxreceipts_sendMethodForContact($contactId);
// query for duplicate receipt
list( $issued_on, $receipt_id ) = cdntaxreceipts_annual_issued_on($contactId, $year);
// When returning issued_on field:
// returns false for non existing $contribution ID
// returns 0 for existing but old data
// returns timestamp for new data
$is_duplicate = empty($issued_on) ? 0 : 1;
if ( $is_duplicate ) {
// issue an identical receipt
$receipt = cdntaxreceipts_load_receipt($receipt_id);
// Cancelled receipts are not sent via email
if ($receipt['receipt_status'] == 'cancelled' && $method != 'data') {
$method = 'print';
}
$receipt['issued_on'] = (int) $_SERVER['REQUEST_TIME'];
$receipt['is_duplicate'] = 1;
$receipt['receive_date'] = $year;
$receipt['issue_method'] = $method;
}
else {
$contributions = cdntaxreceipts_contributions_not_receipted($contactId, $year);
if ( count($contributions) == 0 ) {
return array(FALSE, $method, NULL);
}
// calculate total amount
$receiptContributions = array();
$totalReceipt = 0;
foreach ( $contributions as $c ) {
$eligibleAmount = cdntaxreceipts_eligibleAmount($c['contribution_id']);
$receiptContributions[] = array(
'contribution_id' => $c['contribution_id'],
'contribution_amount' => $c['total_amount'],
'receipt_amount' => $eligibleAmount,
'receive_date' => $c['receive_date'],
);
$totalReceipt += $eligibleAmount;
}
// generate a receipt number
$receiptNo = Civi::settings()->get('receipt_prefix') . str_pad($receiptContributions[0]['contribution_id'], 8, 0, STR_PAD_LEFT);
$receipt = array(
'receipt_no' => $receiptNo,
'issued_on' => (int) $_SERVER['REQUEST_TIME'],
'contact_id' => $contactId,
'receipt_amount' => $totalReceipt,
'is_duplicate' => 0,
'issue_type' => 'annual',
'issue_method' => $method,
'receive_date' => $year,
'receipt_status' => 'issued',
'contributions' => $receiptContributions,
'email_tracking_id' => md5(uniqid(rand(), TRUE)),
);
}
return cdntaxreceipts_processTaxReceipt($receipt, $collectedPdf, $mode);
}
/**
* cdntaxreceipts_issueAggregateTaxReceipt()
* Issues an grouped tax receipt for a given contact, year and list of selected contributions.
* Currently does not issue duplicates. Pass only original contribution ids.
*
* @param $contactId
* @param $year
* @param $contributions
* @param $method
* @param null $collectedPdf
* @param bool $previewMode
* @return array
*/
function cdntaxreceipts_issueAggregateTaxReceipt($contactId, $year, $contributions, $method, &$collectedPdf = NULL,
$mode = CDNTAXRECEIPTS_MODE_BACKOFFICE) {
if ( count($contributions) == 0 ) {
return array(FALSE, $method, NULL);
}