-
Notifications
You must be signed in to change notification settings - Fork 28
/
cdntaxreceipts.functions.inc
884 lines (745 loc) · 33.4 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
<?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 = CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'receipt_pdftemplate');
if (!empty($pdf_template_file)) {
if (is_null($this->_tplIdx)) {
$pdf_template_file = CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'receipt_pdftemplate');
$this->setSourceFile($pdf_template_file);
$this->_tplIdx = $this->importPage(1);
}
$this->useTemplate($this->_tplIdx);
}
}
function Footer() {}
}
const CDNTAX_SETTINGS = 'CDNTaxReceipts';
/**
* 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, $previewMode = FALSE ) {
// determine the send method
require_once('CRM/Contact/BAO/Contact.php');
list($displayname, $email) = CRM_Contact_BAO_Contact::getContactDetails($receipt['contact_id']);
// generate the PDF file
list($pdf_file, $user_friendly) = cdntaxreceipts_generateFormattedReceipt( $receipt, $collectedPdf, $previewMode );
// form a mailParams array to pass to the CiviCRM mail utility
$attachment = array(
'fullPath' => $pdf_file,
'mime_type' => 'application/pdf',
'cleanName' => $user_friendly,
);
$email_message = CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'email_message');
$org_name = CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'org_name');
$email_subject_admin = CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'email_subject');
$email_subject = $email_subject_admin . " " . $receipt['receipt_no'];
$email_from = CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'email_from');
$email_archive = CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'email_archive');
$mailparams = array(
'from' => $org_name . ' <' . $email_from . '> ',
'toName' => $displayname,
'attachments' => array( $attachment ),
'subject' => $email_subject,
'text' => $email_message,
'returnPath' => $email_from,
);
// if method is email, we send to contact and BCC email archive.
// if method is print, we send to email archive only.
if ( $receipt['issue_method'] == 'email' ) {
$mailparams['toEmail'] = $email;
$mailparams['bcc'] = $email_archive;
}
else {
$mailparams['toEmail'] = $email_archive;
}
if ( $previewMode ) {
$ret = TRUE;
}
else {
$ret = CRM_Utils_Mail::send( $mailparams );
if ( $ret == TRUE ) {
// we have successfully processed. Log the receipt.
cdntaxreceipts_log($receipt);
}
}
if ( $receipt['issue_method'] == 'email' ) {
unlink($pdf_file); // file is no longer needed
return array( $ret, 'email', NULL );
}
else {
if ( isset($collectedPdf) ) {
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( $ret, 'print', $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, $previewMode = FALSE ) {
$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_2 = implode(' ', $parts);
$address_line_3 = isset($address['country']) ? $address['country'] : '';
list($displayname, $email) = CRM_Contact_BAO_Contact::getContactDetails($receipt['contact_id']);
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 = "This is your Official Receipt for income tax purposes.";
$config = CRM_Core_Config::singleton();
$pdf_file = $config->customFileUploadDir . 'Receipt-' . $receipt['receipt_no'] . '.pdf';
$user_friendly = 'Receipt-' . $receipt['receipt_no'] . '.pdf';
//KG
//$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, TRUE, 'UTF-8', FALSE);
$pdf = new PDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, TRUE, 'UTF-8', FALSE);
$pdf->Open();
$pdf->SetAuthor(CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, '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(
"preview_mode" => $previewMode,
"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_2" => $address_line_2,
"address_line_3" => $address_line_3,
"inkind_values" => isset($receipt['inkind_values']) ? $receipt['inkind_values'] : array(),
"receipt_contributions" => $receipt['contributions'],
);
$output_files = array($pdf);
if ( isset($collectedPdf) && ( $receipt['issue_method'] == 'print' || $previewMode ) ) {
$output_files[] = $collectedPdf;
}
foreach ( $output_files as $f ) {
$f->AddPage();
if ($receipt['is_duplicate']) {
// print duplicate copy
cdntaxreceipts_createPDF($f, $pdf_variables);
}
elseif (! $receipt['is_duplicate']) {
// print original AND duplicate copy
$pdf_variables["mymargin_top"] = $mymargin_top;
$pdf_variables["is_duplicate"] = FALSE;
cdntaxreceipts_createPDF($f, $pdf_variables);
$pdf_variables["mymargin_top"] = $mymargin_top + 94;
$pdf_variables["is_duplicate"] = TRUE;
cdntaxreceipts_createPDF($f, $pdf_variables);
$pdf_variables["mymargin_top"] = $mymargin_top + 94*2;
$pdf_variables["is_duplicate"] = TRUE;
cdntaxreceipts_createPDF($f, $pdf_variables);
}
}
// 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_createPDF(&$pdf, $pdf_variables) {
// Extract variables
$preview_mode = $pdf_variables["preview_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_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'];
// Middle center section
if ( $preview_mode ) {
$pdf->Image($pdf_img_files_path . 'preview_mode.png', $mymargin_left + 65, $mymargin_top, '', 45);
}
else if ( $is_duplicate ) {
$pdf->Image($pdf_img_files_path . 'duplicate_copy.png', $mymargin_left + 65, $mymargin_top, '', 45);
}
// Top left section
$pdf_template_file = CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'receipt_pdftemplate');
if (!empty($pdf_template_file)) {
}
else {
$pdf->Image(CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'receipt_logo', NULL, $pdf_img_files_path . 'your-logo.png'), $mymargin_left, $mymargin_top, '', 30);
}
// Top right section
$pdf->SetFont('Helvetica', '', 8);
$pdf->SetY($mymargin_top);
$pdf->Write(10, CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'org_name'), '', 0, 'R', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetY($mymargin_top + 4);
$pdf->Write(10, CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'org_address_line1'), '', 0, 'R', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetY($mymargin_top + 8);
$pdf->Write(10, CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'org_address_line2'), '', 0, 'R', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetY($mymargin_top + 12);
if ( CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'org_fax' ) == '' ) {
$pdf->Write(10, 'Tel: ' . CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'org_tel'), '', 0, 'R', TRUE, 0, FALSE, FALSE, 0);
}
else {
$pdf->Write(10, 'Tel: ' . CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'org_tel') . '; Fax: ' . CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'org_fax'), '', 0, 'R', TRUE, 0, FALSE, FALSE, 0);
}
$pdf->SetFont('Helvetica', 'I', 8);
$pdf->SetY($mymargin_top + 16);
$pdf->Write(10, 'Email: ' . CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'org_email') . '; Website: ' . CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'org_web'), '', 0, 'R', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetY($mymargin_top + 20);
$pdf->Write(10, 'Charitable Registration ' . CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'org_charitable_no'), '', 0, 'R', TRUE, 0, FALSE, FALSE, 0);
// Right section
$x_detailscolumn = 120;
$y_detailscolumnstart = 27;
$pdf_template_file = CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'receipt_pdftemplate');
if (!empty($pdf_template_file)) {
}
else {
$background_image = CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'receipt_watermark');
if ( $background_image ) $pdf->Image(CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'receipt_watermark'), $mymargin_left + $x_detailscolumn, $mymargin_top + $y_detailscolumnstart, '', 50);
}
$pdf->SetXY($mymargin_left + $x_detailscolumn, $mymargin_top + $y_detailscolumnstart + 6);
$pdf->SetFont('Helvetica', 'B', 8);
$pdf->Write(10, "Issue Date: " . $issued_on);
$pdf->SetXY($mymargin_left + $x_detailscolumn, $mymargin_top + $y_detailscolumnstart + 12);
$pdf->Write(10, "Received on: " . $display_date);
$pdf->SetXY($mymargin_left + $x_detailscolumn, $mymargin_top + $y_detailscolumnstart + 18);
$pdf->Write(10, "Eligible Gift Amount: $" . number_format($amount, 2));
$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: " . $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, "Source: " . implode(" ", $source_funds_words));
$source_funds_count = count($source_funds_words);
$source_funds_nextline = array_splice(explode(" ", $source_funds), $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, "Receipt No: " . $receipt_number);
$pdf->SetFont('Helvetica', 'B', 8);
$pdf->SetY($mymargin_top + $y_detailscolumnstart + 10);
$pdf->Write(10, "Received from: ", '', 0, 'L', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetFont('Helvetica', 'B', 10);
$pdf->SetY($mymargin_top + $y_detailscolumnstart + 22);
$pdf->Write(10, strtoupper($displayname), '', 0, 'L', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetFont('Helvetica', '', 10);
$pdf->SetY($mymargin_top + $y_detailscolumnstart + 26);
$pdf->Write(10, strtoupper($address_line_1), '', 0, 'L', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetY($mymargin_top + $y_detailscolumnstart + 30);
$pdf->Write(10, strtoupper($address_line_2), '', 0, 'L', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetY($mymargin_top + $y_detailscolumnstart + 34);
$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 = 'Donation in-kind: ' . $inkind_values[0];
if (!empty($inkind_values[3])) {
if ($inkind_values[3] < $amount) {
$inkind_str = 'Donation in-kind: ' . $inkind_values[0] . ' - ' . 'Cost: ' . $inkind_values[3];
}
else
{
$inkind_str = 'Donation in-kind: ' . $inkind_values[0];
}
}
$pdf->SetFont('Helvetica', '', 8);
$pdf->SetY($mymargin_top + 74);
$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, "Appraiser: " . $appraiser);
}
else {
$appraiser_words = explode(" ", substr($appraiser, 0, 36));
$appraiser_lastbit = array_pop($appraiser_words);
$pdf->Write(10, "Appraiser: " . implode(" ", $appraiser_words));
$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
$pdf->SetFont('Helvetica', 'B', 8);
$pdf->SetY($mymargin_top + 78);
$pdf->Write(10, $line_1, '', 0, 'L', TRUE, 0, FALSE, FALSE, 0);
$pdf->SetFont('Helvetica', '', 8);
$pdf->SetY($mymargin_top + 82, 0, 'L', TRUE, 0, FALSE, FALSE, 0);
$pdf->Write(10, 'Canadian Revenue Agency: www.cra-arc.gc.ca/charities');
// Bottom center section
$pdf->SetFont('Helvetica', 'B', 8);
$pdf->SetXY($mymargin_left + 92, $mymargin_top + 82);
if ( $issue_type != 'annual' ) {
$pdf->Write(10, 'Thank you!');
}
else {
$pdf->Write(10, ts("Thank you - %1!", array( 1 => $display_year)));
}
// Bottom right section
$signature = CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'receipt_authorized_signature_text');
if ( $signature == '' ) {
$signature = ts('Authorized Signature');
}
$sig_offset = strlen($signature) - 20;
$pdf_template_file = CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'receipt_pdftemplate');
if (!empty($pdf_template_file)) {
}
else {
$pdf->Image(CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'receipt_signature', NULL, $pdf_img_files_path . 'authorized-signature.png'), $mymargin_left + 137, $mymargin_top + 76, '', 15);
}
$pdf->Line($mymargin_left + 136, $mymargin_top + 87, $mymargin_left + 186, $mymargin_top + 87);
$pdf->SetXY($mymargin_left + 148 - $sig_offset, $mymargin_top + 84);
$pdf->SetFont("Helvetica", "I", 7);
$pdf->Write(10, $signature);
// Line at the bottom
$pdf->Line($mymargin_left, $mymargin_top + 93, 198, $mymargin_top + 93, '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 contribtion type exists. if not, create it.
$dao = _cdntaxreceipts_get_contribution_dao();
$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',
'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, $previewMode = FALSE ) {
// This function basically needs to set up a $receipt[] array and pass it to
// processTaxReceipt() which does the hard work
// 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, $previewMode);
}
}
// determine the send method
list( $method, $email ) = cdntaxreceipts_sendMethodForContact($contribution->contact_id);
// generate a receipt number
$receipt_no = CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'receipt_prefix') . str_pad($contribution->id, 8, 0, STR_PAD_LEFT);
// 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' => $contribution->total_amount - $contribution->non_deductible_amount,
'is_duplicate' => $is_duplicate,
'issue_type' => 'single',
'issue_method' => $method,
'receive_date' => $contribution->receive_date,
'source' => $contribution->source,
'inkind_values' => $inkind_values,
);
$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, $previewMode);
}
function cdntaxreceipts_eligibleForReceipt( $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 = $contribution->total_amount - $contribution->non_deductible_amount;
// 2. check if the contribution is of a Type that is indeed Tax receiptable.
$contributionType = _cdntaxreceipts_get_type_for_contribution($contribution);
// 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, 'cdntaxreceipts_eligible');
$hookOk = TRUE;
if (is_array($results)) {
foreach ( $results as $result ) {
if ( $result == FALSE ) {
$hookOk = FALSE;
}
}
}
if ( $contributionType->is_deductible && $contributionStatus == '1' && $contribution->is_test == 0 && $deductibleAmount > 0 && $hookOk ) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* 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, $previewMode = FALSE ) {
// 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);
$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 ) {
$receiptContributions[] = array(
'contribution_id' => $c['contribution_id'],
'contribution_amount' => $c['total_amount'],
'receipt_amount' => $c['total_amount'] - $c['non_deductible_amount'],
'receive_date' => $c['receive_date'],
);
$totalReceipt += $c['total_amount'] - $c['non_deductible_amount'];
}
// generate a receipt number
$receiptNo = CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, '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,
'contributions' => $receiptContributions,
);
}
return cdntaxreceipts_processTaxReceipt($receipt, $collectedPdf, $previewMode);
}
/* determines the send method for the specified contact
* returns array, [0] = 'email' or 'print', [1] = email address or NULL
*/
function cdntaxreceipts_sendMethodForContact( $contactId ) {
$global_email = CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, 'enable_email', NULL, TRUE);
if ( ! $global_email ) {
return array('print', NULL);
}
require_once 'CRM/Contact/BAO/Contact.php';
list($displayname, $email, $doNotEmail, $onHold) = CRM_Contact_BAO_Contact::getContactDetails($contactId);
if ( isset($email) ) {
if ( ! $doNotEmail && ! $onHold ) {
return array('email', $email);
}
}
return array('print', NULL);
}
function cdntaxreceipts_sendMethodForContribution( $contributionId ) {
$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" );
}
return cdntaxreceipts_sendMethodForContact( $contribution->contact_id );
}
function cdntaxreceipts_openCollectedPDF() {
static $pdf;
if ( ! isset($pdf) ) {
//KG
//$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, TRUE, 'UTF-8', FALSE);
$pdf = new PDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, TRUE, 'UTF-8', FALSE);
$pdf->Open();
$pdf->SetAuthor(CRM_Core_BAO_Setting::getItem(CDNTAX_SETTINGS, '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);
}
return $pdf;
}
function cdntaxreceipts_sendCollectedPDF(&$pdf, $filename) {
if ( $pdf->getNumPages() > 0 ) {
$pdf->Output($filename, 'D');
CRM_Utils_System::civiExit();
}
else {
$pdf->Close();
}
}
/**
* Isolate version checking code
* @param $contribution
* @return CRM_Contribute_DAO_ContributionType|CRM_Financial_DAO_FinancialType
*/
function _cdntaxreceipts_get_type_for_contribution($contribution) {
// Require once lines are only needed if the civicrm.settings.php does not have the autoloader line
require_once 'CRM/Utils/VersionCheck.php';
$version_checker = CRM_Utils_VersionCheck::singleton();
$majorVersion = floatval(substr($version_checker->localVersion, 0, 3));
if ($majorVersion >= 4.3) {
require_once 'CRM/Financial/DAO/FinancialType.php';
$contributionType = new CRM_Financial_DAO_FinancialType();
$contributionType->id = $contribution->financial_type_id;
if ( ! $contributionType->find( TRUE ) ) {
CRM_Core_Error::fatal( "CDNTaxReceipts: Could not find corresponding contribution type." );
}
}
else { // Assume 4.2 or lower
require_once 'CRM/Contribute/DAO/ContributionType.php';
$contributionType = new CRM_Contribute_DAO_ContributionType();
$contributionType->id = $contribution->contribution_type_id;
if ( ! $contributionType->find( TRUE ) ) {
CRM_Core_Error::fatal( "CDNTaxReceipts: Could not find corresponding contribution type." );
}
}
return $contributionType;
}
function _cdntaxreceipts_get_contribution_dao() {
// Require once lines are only needed if the civicrm.settings.php does not have the autoloader line
require_once 'CRM/Utils/VersionCheck.php';
$version_checker = CRM_Utils_VersionCheck::singleton();
$majorVersion = floatval(substr($version_checker->localVersion, 0, 3));
if ($majorVersion >= 4.3) {
require_once 'CRM/Financial/DAO/FinancialType.php';
$contributionType = new CRM_Financial_DAO_FinancialType();
}
else { // Assume 4.2 or lower
require_once 'CRM/Contribute/DAO/ContributionType.php';
$contributionType = new CRM_Contribute_DAO_ContributionType();
}
return $contributionType;
}
/**
* Get the correct contact address. Get the billing address followed by the is_primary as a fallback.
* @param $contact_id
* @return array|mixed|null address : Contact address fixed with named country and state_province
*/
function cdntaxreceipts_getAddress($contact_id) {
$address = NULL;
// get Address information via contact
$params = array(
'version' => 3,
'contact_id' => $contact_id,
'is_billing' => 1,
);
$address_results = civicrm_api('Address', 'get', $params);
if ( $address_results['is_error'] == 0) {
$address = array_shift($address_results['values']);
}
if (!isset($address)) {
$params = array(
'version' => 3,
'contact_id' => $contact_id,
'is_primary' => 1,
);
$address_results = civicrm_api('Address', 'get', $params);
if ( $address_results['is_error'] == 0) {
$address = array_shift($address_results['values']);
}
}
$address = isset($address) ? $address : array();
// add actual names for province and country instead of just having ids
require_once 'CRM/Core/BAO/Address.php';
CRM_Core_BAO_Address::fixAddress($address);
return $address;
}