-
Notifications
You must be signed in to change notification settings - Fork 20
/
invoice_printer_base.php
2838 lines (2615 loc) · 87.9 KB
/
invoice_printer_base.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
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
/**
* Invoice printer abstract base class
*
* PHP version 8
*
* Copyright (C) Samu Reinikainen 2004-2008
* Copyright (C) Ere Maijala 2010-2019.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category MLInvoice
* @package MLInvoice\Base
* @author Samu Reinikainen <not-available@ajassa.fi>
* @author Ere Maijala <ere@labs.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://labs.fi/mlinvoice.eng.php
*/
require_once 'translator.php';
require_once 'settings.php';
require_once 'pdf.php';
/**
* Invoice printer abstract base class
*
* @category MLInvoice
* @package MLInvoice\Base
* @author Samu Reinikainen <not-available@ajassa.fi>
* @author Ere Maijala <ere@labs.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://labs.fi/mlinvoice.eng.php
*/
abstract class InvoicePrinterBase
{
protected $pdf = null;
protected $invoiceId = null;
protected $printStyle = '';
protected $printLanguage = 'fi';
protected $senderData = null;
protected $recipientData = null;
protected $recipientContactData = null;
protected $invoiceData = null;
protected $invoiceRowData = null;
protected $separateStatement = false;
protected $readOnlySafe = false;
protected $refNumber = '';
protected $barcode = '';
protected $totalSum = 0;
protected $totalVAT = 0;
protected $totalSumVAT = 0;
protected $discountedRows = false;
protected $groupedVATs = [];
protected $recipientMaxY = 0;
protected $invoiceRowMaxY = 270;
protected $invoiceRowMaxYFirstPage = 185;
protected $senderAddressX = 0;
protected $senderAddressY = 0;
protected $recipientAddressX = 0;
protected $recipientAddressY = 0;
protected $partialPayments = 0;
protected $dateOverride = false;
protected $roundRowPrices = false;
/**
* Whether a separate invoice statement is allowed
*
* @var bool
*/
protected $allowSeparateStatement = true;
/**
* Invoice type data
*
* @var array
*/
protected $invoiceTypeData = null;
/**
* Left of the main content
*
* @value int
*/
protected $left = 10;
/**
* Width of the main content
*
* @value int
*/
protected $width = 190;
/**
* Autofit column padding for rows
*
* @value int
*/
protected $columnPadding = 3;
/**
* Left coordinate of the info array
*
* @param int
*/
protected $infoLeft = 115;
/**
* Info headings column width
*
* @param int
*/
protected $infoHeadingsWidth = 40;
/**
* Info text column width
*
* @param int
*/
protected $infoTextWidth = 48;
/**
* Bottom margin for auto page break
*
* @var int
*/
protected $autoPageBreakMargin = 19;
/**
* Bottom margin for auto page break on first page
*
* @var int
*/
protected $autoPageBreakMarginFirstPage = 19;
/**
* Column definitions in the printout. This includes all possible columns and
* may be modified in the constructor or init method. Do not remove items from
* the array.
*
* Keys in the array:
* heading The heading (to be translated)
* valuemethod A method to retrieve the cell content. Current row is given as
* parameter to the method.
* visible Whether the column is visible
* align Alignment of the cell ('L', 'C' or 'R')
* width Column width in mm or 'fill' to use maximum left from other
* columns
* autofir If true, width will be adjusted to fit the actual content
* maxheight Maximum height of the cell
*
* @param array
*/
protected $columnDefs = [
'sequence' => [
'heading' => 'RowSequenceNumber',
'valuemethod' => 'getRowSequenceNumber',
'visible' => true,
'align' => 'L',
'width' => 2,
'autofit' => true,
'maxheight' => 0
],
'description' => [
'heading' => 'RowName',
'valuemethod' => 'getRowDescription',
'visible' => true,
'align' => 'L',
'width' => 'fill',
'maxheight' => 0
],
'date' => [
'heading' => 'RowDate',
'valuemethod' => 'getRowDate',
'visible' => true,
'align' => 'L',
'width' => 20,
'autofit' => true
],
'pieces' => [
'heading' => 'RowPieces',
'valuemethod' => 'getRowPieces',
'visible' => true,
'align' => 'R',
'width' => 20,
'autofit' => true
],
'type' => [
'heading' => '',
'valuemethod' => 'getRowItemType',
'visible' => true,
'align' => 'L',
'width' => 10,
'autofit' => true
],
'price' => [
'heading' => 'RowPrice',
'valuemethod' => 'getRowPrice',
'visible' => true,
'align' => 'R',
'width' => 20,
'autofit' => true
],
'discount' => [
'heading' => 'RowDiscount',
'valuemethod' => 'getRowDiscount',
'visible' => true,
'align' => 'R',
'width' => 20,
'autofit' => true
],
'totalvatless' => [
'heading' => 'RowTotalVATLess',
'valuemethod' => 'getRowTotalVATLess',
'visible' => true,
'align' => 'R',
'width' => 20,
'autofit' => true
],
'vatpercent' => [
'heading' => 'RowVATPercent',
'valuemethod' => 'getRowVATPercent',
'visible' => true,
'align' => 'R',
'width' => 15,
'autofit' => true
],
'vat' => [
'heading' => 'RowTax',
'valuemethod' => 'getRowVAT',
'visible' => true,
'align' => 'R',
'width' => 20,
'autofit' => true
],
'total' => [
'heading' => 'RowTotal',
'valuemethod' => 'getRowTotal',
'visible' => true,
'align' => 'R',
'width' => 20,
'autofit' => true
]
];
/**
* Any print parameters apart from the first three that have a special meaning
*
* @var array
*/
protected $printParams = [];
/**
* Print template ID
*
* @var int
*/
protected $printTemplateId;
/**
* Whether the print request is made by an authenticated user
*
* @var bool
*/
protected $authenticated;
/**
* Whether to include bank information in footer
*
* @var bool
*/
protected $includeBankInFooter = false;
/**
* Attachments
*
* @var array
*/
protected $attachments = [];
/**
* Whether to print virtual barcode
*
* @var bool
*/
protected $printVirtualBarcode = false;
/**
* Output file name
*
* @var string
*/
protected $outputFileName = '';
/**
* Constructor
*/
public function __construct()
{
}
/**
* Check if the printout is safe to use for read-only user permissions
*
* @return bool
*/
public function getReadOnlySafe()
{
return $this->readOnlySafe;
}
/**
* Initialize printing
*
* @param int $invoiceId Invoice ID
* @param string $printParameters Print control parameters
* @param string $outputFileName File name template
* @param int $dateOverride Date override for invoice date
* @param int $printTemplateId Print template ID
* @param bool $authenticated Whether the user is authenticated
*
* @return void
*/
public function init($invoiceId, $printParameters, $outputFileName,
$dateOverride, $printTemplateId, $authenticated
) {
$this->printTemplateId = $printTemplateId;
$this->authenticated = $authenticated;
if (0 !== $invoiceId) {
$strQuery = 'SELECT inv.*, ref.invoice_no as refunded_invoice_no, delivery_terms.name as delivery_terms,'
. ' delivery_method.name as delivery_method, invoice_state.name as invoice_state,'
. ' invoice_state.invoice_open as invoice_open, invoice_state.invoice_unpaid as invoice_unpaid '
. 'FROM {prefix}invoice inv '
. 'LEFT OUTER JOIN {prefix}invoice ref ON ref.id = inv.refunded_invoice_id '
. 'LEFT OUTER JOIN {prefix}delivery_terms as delivery_terms ON delivery_terms.id = inv.delivery_terms_id '
. 'LEFT OUTER JOIN {prefix}delivery_method as delivery_method ON delivery_method.id = inv.delivery_method_id '
. 'LEFT OUTER JOIN {prefix}invoice_state as invoice_state ON invoice_state.id = inv.state_id '
. 'WHERE inv.id=?';
$rows = dbParamQuery($strQuery, [$invoiceId]);
if (!$rows) {
if ($authenticated) {
die('Could not find invoice data');
}
return;
}
$invoiceData = $rows[0];
if (isOffer($invoiceId)) {
$invoiceData['invoice_no'] = $invoiceId;
}
$recipientData = getCompany($invoiceData['company_id']);
if ($recipientData) {
if (!empty($recipientData['company_id'])) {
$recipientData['vat_id'] = createVATID($recipientData['company_id']);
} else {
$recipientData['vat_id'] = '';
}
$strQuery = 'SELECT * FROM {prefix}company_contact WHERE company_id=?'
. ' AND deleted=0 ORDER BY id';
$recipientContactData = dbParamQuery($strQuery, [$invoiceData['company_id']]);
}
$strQuery = 'SELECT * FROM {prefix}base WHERE id=?';
$rows = dbParamQuery($strQuery, [$invoiceData['base_id']]);
if (!$rows) {
if ($authenticated) {
die('Could not find invoice sender data');
}
return;
}
$senderData = $rows[0];
$senderData['vat_id'] = createVATID($senderData['company_id']);
$queryParams = [$invoiceId];
$where = 'ir.invoice_id=? AND ir.deleted=0';
if ($dateOverride) {
$where .= ' AND row_date=?';
$queryParams[] = $dateOverride;
}
$strQuery = <<<EOT
SELECT pr.product_name, pr.product_code, pr.price_decimals,
pr.barcode1, pr.barcode1_type, pr.barcode2, pr.barcode2_type,
ir.description, ir.pcs, ir.price, IFNULL(ir.discount, 0) as discount,
IFNULL(ir.discount_amount, 0) as discount_amount, ir.row_date, ir.vat,
ir.vat_included, ir.reminder_row, ir.partial_payment, ir.order_no, rt.name type
FROM {prefix}invoice_row ir
LEFT OUTER JOIN {prefix}row_type rt ON rt.id = ir.type_id
LEFT OUTER JOIN {prefix}product pr ON ir.product_id = pr.id
WHERE $where ORDER BY ir.order_no, row_date, pr.product_name DESC,
ir.description DESC
EOT;
$invoiceRowData = dbParamQuery($strQuery, $queryParams);
$invoiceTypeData = getInvoiceType($invoiceData['type_id']);
$this->attachments = getInvoiceAttachments($invoiceId, true);
} else {
$invoiceData = [];
$invoiceRowData = [];
$senderData = [];
$invoiceTypeData = [];
$this->attachments = [];
}
if (empty($recipientData)) {
$recipientData = [
'company_name' => '',
'company_id' => '',
'vat_id' => '',
'customer_no' => '',
'street_address' => '',
'zip_code' => '',
'city' => '',
'billing_address' => '',
'email' => ''
];
$recipientContactData = [];
}
$this->dateOverride = $dateOverride;
$this->invoiceId = $invoiceId;
$parameters = explode(',', $printParameters);
$this->printStyle = $parameters[0];
$this->printLanguage = $parameters[1] ?? 'fi';
$this->printVirtualBarcode = isset($parameters[2]) ? ($parameters[2] == 'Y')
: false;
// Rest of the parameters are key=value style
if (count($parameters) > 3) {
$this->printParams = parse_ini_string(
implode("\n", array_slice($parameters, 3))
);
}
$this->outputFileName = $outputFileName;
$this->senderData = $senderData;
$this->recipientData = $recipientData;
$this->invoiceData = $invoiceData;
$this->invoiceRowData = $invoiceRowData;
$this->recipientContactData = $recipientContactData;
$this->invoiceTypeData = $invoiceTypeData;
Translator::setActiveLanguage('non-default', $this->printLanguage);
$this->totalSum = 0;
$this->totalVAT = 0;
$this->totalSumVAT = 0;
$this->discountedRows = false;
$this->partialPayments = 0;
$this->groupedVATs = [];
$sequence = 1;
foreach ($this->invoiceRowData as $key => &$row) {
$row['sequence'] = $sequence++;
if ($row['partial_payment']) {
$this->partialPayments -= $row['price'];
continue;
}
if ($row['partial_payment']) {
$rowSum = $rowSumVAT = $row['price'];
$rowVAT = 0;
} else {
[$rowSum, $rowVAT, $rowSumVAT] = calculateRowSum($row);
if ($row['vat_included']) {
$row['price'] /= (1 + $row['vat'] / 100);
}
}
$row['rowsum'] = $rowSum;
$row['rowvat'] = $rowVAT;
$row['rowsumvat'] = $rowSumVAT;
$this->totalSum += $rowSum;
$this->totalVAT += $rowVAT;
$this->totalSumVAT += $rowSumVAT;
$discount = (float)$row['discount'];
$discountAmount = (float)$row['discount_amount'];
if ($discount || $discountAmount) {
$this->discountedRows = true;
}
// Create array grouped by the VAT base
$vat = str_pad(
number_format($row['vat'], 2, '', ''), 5, '0', STR_PAD_LEFT
);
if (isset($this->groupedVATs[$vat])) {
$this->groupedVATs[$vat]['totalsum'] += $rowSum;
$this->groupedVATs[$vat]['totalvat'] += $rowVAT;
$this->groupedVATs[$vat]['totalsumvat'] += $rowSumVAT;
} else {
$this->groupedVATs[$vat]['vat'] = $row['vat'];
$this->groupedVATs[$vat]['totalsum'] = $rowSum;
$this->groupedVATs[$vat]['totalvat'] = $rowVAT;
$this->groupedVATs[$vat]['totalsumvat'] = $rowSumVAT;
}
}
ksort($this->groupedVATs);
$this->separateStatement = ($this->printStyle == 'invoice') &&
getSetting('invoice_separate_statement') == 1;
$this->allowSeparateStatement = ($this->printStyle == 'invoice') &&
getSetting('invoice_separate_statement') == 0;
$this->refNumber = isset($invoiceData['ref_number'])
? formatRefNumber($invoiceData['ref_number']) : '';
// barcode
/*
* 1 Barcode version, this is version 4 or 5
* 1 Currency (1=FIM, 2=EURO)
* 16 IBAN without leading country code
* 6 Euros
* 2 Cents
* 3 Spares, contain zeros
* 20 Reference Number
* 6 Due Date. Format is YYMMDD.
*/
$this->barcode = '';
$paymentAmount = $this->totalSumVAT - $this->partialPayments;
if ($paymentAmount > 0) {
$tmpRefNumber = str_replace(' ', '', $this->refNumber);
$IBAN = str_replace(' ', '', substr($senderData['bank_iban'], 2));
if (ctype_digit($tmpRefNumber) == 0
|| (strncmp($tmpRefNumber, 'RF', 2) == 0
&& ctype_digit(substr($tmpRefNumber, 2) == 0))
) {
error_log(
'Empty or invalid reference number "' . $tmpRefNumber
. '", barcode not created'
);
} elseif (strlen($IBAN) != 16) {
error_log(
'IBAN length invalid (should be 16 numbers without leading'
. ' country code and spaces), barcode not created'
);
} elseif (strlen($invoiceData['due_date']) != 8) {
error_log(
'Invalid due date \'' . $invoiceData['due_date']
. '\' - barcode not created'
);
} elseif ($paymentAmount >= 1000000) {
error_log('Invoice total too large, barcode not created');
} else {
$tmpSum = miscRound2Decim($paymentAmount, 2, '', '');
$tmpSum = str_repeat('0', 8 - strlen($tmpSum)) . $tmpSum;
$tmpDueDate = substr($invoiceData['due_date'], 2);
if (strncmp($tmpRefNumber, 'RF', 2) == 0) {
$checkDigits = substr($tmpRefNumber, 2, 2);
$tmpRefNumber = substr($tmpRefNumber, 4);
$tmpRefNumber = $checkDigits .
str_repeat('0', 21 - strlen($tmpRefNumber)) . $tmpRefNumber;
$this->barcode = '5' . $IBAN . $tmpSum . $tmpRefNumber .
$tmpDueDate;
} else {
$tmpRefNumber = str_repeat('0', 20 - strlen($tmpRefNumber)) .
$tmpRefNumber;
$this->barcode = '4' . $IBAN . $tmpSum . '000' . $tmpRefNumber .
$tmpDueDate;
}
}
}
$this->senderAddressX = 10 + getSetting('invoice_address_x_offset', 0);
$this->senderAddressY = 20 + getSetting('invoice_address_y_offset', 0);
$this->recipientAddressX = 10 +
getSetting('invoice_recipient_address_x_offset', 0);
$this->recipientAddressY = 40 +
getSetting('invoice_recipient_address_y_offset', 0);
if ($this->printStyle === 'invoice') {
$this->autoPageBreakMarginFirstPage = $this->printVirtualBarcode
? 120 : 115;
}
if (!getSetting('invoice_show_sequential_number')) {
$this->columnDefs['sequence']['visible'] = false;
}
if (!getSetting('invoice_show_row_date')) {
$this->columnDefs['date']['visible'] = false;
}
if (!$this->discountedRows) {
$this->columnDefs['discount']['visible'] = false;
}
if (getSetting('invoice_row_description_first_line_only', false)) {
$this->columnDefs['description']['maxheight'] = 5;
}
if ('invoice' !== $this->printStyle) {
$this->invoiceRowMaxYFirstPage = $this->invoiceRowMaxY;
}
}
/**
* Set invoice data
*
* @param array $data Invoice data
*
* @return void
*/
public function setInvoiceData($data)
{
$this->invoiceData = $data;
}
/**
* Set sender data
*
* @param array $data Sender data
*
* @return void
*/
public function setSenderData($data)
{
$this->senderData = $data;
}
/**
* Set recipient data
*
* @param array $data Recipient data
* @param array $contactData Recipient contact data
*
* @return void
*/
public function setRecipientData($data, $contactData)
{
$this->recipientData = $data;
$this->recipientContactData = $contactData;
}
/**
* Main method for printing
*
* @return void
*/
public function printInvoice()
{
if (ob_get_contents()) {
echo "\nData has already been output, cannot continue printing\n";
return;
} elseif (headers_sent()) {
echo "\nHeaders have already been sent, cannot continue printing\n";
return;
}
$result = $this->createPrintout();
foreach ($result['headers'] as $header => $value) {
header("$header: $value");
}
echo $result['data'];
}
/**
* Create the printout and return headers and data
*
* @return array Associative array with headers and data
*/
public function createPrintout()
{
if (!empty($this->invoiceData['ref_number'])
&& strlen($this->invoiceData['ref_number']) < 4
) {
error_log('Reference number too short, will not be displayed');
$this->invoiceData['ref_number'] = '';
}
if ('dispatch' === $this->printStyle || !$this->senderData['vat_registered']
) {
$this->columnDefs['totalvatless']['visible'] = false;
$this->columnDefs['vatpercent']['visible'] = false;
$this->columnDefs['vat']['visible'] = false;
}
if ('dispatch' === $this->printStyle) {
$this->columnDefs['price']['visible'] = false;
$this->columnDefs['discount']['visible'] = false;
$this->columnDefs['total']['visible'] = false;
}
$this->initPDF();
$this->printSender();
$this->printRecipient();
$this->printInfo();
$this->printSeparatorLine();
$this->printForeword();
if ($this->printStyle == 'invoice') {
$this->printForm();
}
$savePdf = clone $this->pdf;
if (!$this->separateStatement) {
if ($this->printRows() || !$this->allowSeparateStatement) {
$this->printSummary();
} else {
$this->pdf = $savePdf;
$this->printSeparateStatementMessage();
$this->separateStatement = true;
}
} else {
$this->printSeparateStatementMessage();
}
$this->printAfterword();
if ($this->printStyle == 'invoice'
&& !$this->separateStatement
&& $this->allowSeparateStatement
) {
if ($this->pdf->getY() > $this->invoiceRowMaxYFirstPage
|| $this->pdf->getPage() > 1
) {
$this->pdf = $savePdf;
$this->separateStatement = true;
$this->printSeparateStatementMessage();
$this->printAfterword();
}
}
if ($this->separateStatement) {
$this->printRows();
$this->printSummary();
}
return [
'filename' => $this->getPrintOutFileName(),
'headers' => $this->getHttpHeaders(),
'data' => $this->getPdfData()
];
}
/**
* Get a title for the current print style
*
* @return string
*/
public function getHeaderTitle()
{
if ($this->printStyle == 'dispatch') {
return $this->translate('DispatchNoteHeader');
} elseif ($this->printStyle == 'receipt') {
return $this->translate('ReceiptHeader');
} elseif ($this->invoiceData['state_id'] == 5) {
return $this->translate('FirstReminderHeader');
} elseif ($this->invoiceData['state_id'] == 6) {
return $this->translate('SecondReminderHeader');
} elseif ($this->invoiceData['refunded_invoice_no'] || $this->totalSum < 0) {
return $this->translate('CreditInvoiceHeader');
}
return $this->translate('InvoiceHeader');
}
/**
* Initialize the PDF
*
* @return void
*/
protected function initPDF()
{
$pdf = new PDF('P', 'mm', 'A4', _CHARSET_ == 'UTF-8', _CHARSET_, false);
$pdf->AddPage();
$pdf->SetAutoPageBreak(
true, $this->autoPageBreakMargin, $this->autoPageBreakMarginFirstPage
);
$pdf->footerLeft = $this->getFooterLeftColumn();
$pdf->footerCenter = $this->getFooterCenterColumn();
$pdf->footerRight = $this->getFooterRightColumn();
$pdf->headerLeftPos = $pdf->footerLeftPos = $this->left;
$pdf->headerRightPos = $pdf->footerRightPos = $this->left + $this->width
- $pdf->headerRightWidth;
$pdf->markdown = getSetting('printout_markdown');
$this->pdf = $pdf;
}
/**
* Print sender's logo and/or address
*
* @return void
*/
protected function printSender()
{
$pdf = $this->pdf;
$senderData = $this->senderData;
if (isset($senderData['logo_filedata'])) {
if (!isset($senderData['logo_top'])) {
$senderData['logo_top'] = $pdf->GetY() + 5;
}
if (!isset($senderData['logo_left'])) {
$senderData['logo_left'] = $pdf->GetX();
}
if (!isset($senderData['logo_width']) || $senderData['logo_width'] == 0
) {
$senderData['logo_width'] = 80;
}
$pdf->Image(
'@' . $senderData['logo_filedata'],
$senderData['logo_left'], $senderData['logo_top'],
$senderData['logo_width'], 0, '', '', 'N', false, 300, '', false,
false, 0, true
);
}
if (!isset($senderData['logo_filedata'])
|| getSetting('invoice_print_senders_logo_and_address')
) {
$width = getSetting('invoice_address_max_width');
$address = $senderData['street_address'] . "\n" . $senderData['zip_code']
. ' ' . $senderData['city'] . "\n" . $senderData['country'];
$pdf->SetTextColor(125);
$pdf->SetFont('Helvetica', 'B', 10);
$pdf->SetY($this->senderAddressY);
$pdf->setX($this->senderAddressX);
$pdf->multiCellMD($width, 5, $senderData['name'], 'L');
$pdf->SetFont('Helvetica', '', 10);
$pdf->setX($this->senderAddressX);
$pdf->multiCellMD($width, 5, $address, 'L');
}
}
/**
* Print recipient's contact information
*
* @return void
*/
protected function printRecipient()
{
$pdf = $this->pdf;
$recipientData = $this->recipientData;
$width = getSetting('invoice_address_max_width');
$pdf->SetTextColor(0);
$pdf->SetFont('Helvetica', '', 12);
$pdf->SetY($this->recipientAddressY);
$pdf->setX($this->recipientAddressX);
$pdf->multiCellMD($width, 5, $this->getRecipientName(), 'L');
$contact = $this->getContactPerson();
if (!empty($contact['contact_person'])
&& getSetting('invoice_show_recipient_contact_person')
) {
$pdf->setX($this->recipientAddressX);
$pdf->multiCellMD($width, 5, $contact['contact_person'], 'L');
}
$pdf->setX($this->recipientAddressX);
$pdf->multiCellMD($width, 5, $this->getRecipientAddress(), 'L');
if (!empty($recipientData['email'])
&& getSetting('invoice_show_recipient_email')
) {
$link = '<a style="color: black; text-decoration: none" href="mailto:'
. htmlentities($recipientData['email'])
. '">' . htmlentities($recipientData['email']) . '</a>';
$pdf->WriteHTMLCell(
$width,
5,
$this->recipientAddressX,
$pdf->GetY() + 4,
$link,
0,
1,
false,
true,
'L'
);
}
$this->recipientMaxY = $pdf->GetY();
}
/**
* Print info headers
*
* @return void
*/
protected function printInfo()
{
$pdf = $this->pdf;
$pdf->SetXY($this->infoLeft + $this->infoHeadingsWidth, 10);
$pdf->SetFont('Helvetica', 'B', 12);
$pdf->Cell($this->infoTextWidth, 5, $this->getHeaderTitle(), 0, 1, 'L');
$pdf->SetFont('Helvetica', '', 10);
$pdf->SetXY($this->infoLeft, $pdf->GetY() + 5);
$data = $this->getInfoArray();
$this->printInfoArray($data);
}
/**
* Gather an array of information to print
*
* @param bool $bankInfo Whether to include recipient bank information
*
* @return array
*/
protected function getInfoArray($bankInfo = false)
{
if ($this->printStyle == 'dispatch') {
$locStr = 'DispatchNote';
} elseif ($this->printStyle == 'receipt') {
$locStr = 'Receipt';
} else {
$locStr = 'Invoice';
}
$invoiceData = $this->invoiceData;
$recipientData = $this->recipientData;
$senderData = $this->senderData;
$data = [];
if ($recipientData['customer_no'] != 0) {
$data['CustomerNumber'] = $recipientData['customer_no'];
}
if ($recipientData['company_id']) {
$data['ClientVATID'] = $recipientData['company_id'];
}
$data["{$locStr}Number"] = $invoiceData['invoice_no'];
$strInvoiceDate = ($this->dateOverride)
? $this->formatDate($this->dateOverride)
: $this->formatDate($invoiceData['invoice_date']);
$data["{$locStr}Date"] = $strInvoiceDate;
$strDueDate = $this->formatDate($invoiceData['due_date']);
if ($this->printStyle == 'invoice') {
$data['DueDate'] = $strDueDate;
$paymentDays = round(
dbDate2UnixTime($invoiceData['due_date']) / 3600 / 24 -
dbDate2UnixTime($invoiceData['invoice_date']) / 3600 / 24
);
if ($paymentDays < 0) {
// This shouldn't happen, but try to be safe...
$paymentDays = getPaymentDays($invoiceData['company_id']);
}
$data['TermsOfPayment'] = [
'value' => $this->getTermsOfPayment($paymentDays),
'type' => 'multicell'
];
$data['PeriodForComplaints'] = $this->getPeriodForComplaints();
$data['PenaltyInterest'] = $this->formatNumber(
getSetting('invoice_penalty_interest'), 1, true
) . ' %';
if ($bankInfo) {
$data['RecipientBankAccount'] = $senderData['bank_iban'];
$data['RecipientBankBIC'] = $senderData['bank_swiftbic'];
}
if ($this->refNumber) {
$data['InvoiceRefNr'] = $this->refNumber;
}
}
if ($invoiceData['reference']) {
$data['YourReference'] = [
'value' => $invoiceData['reference'],
'type' => 'multicell'
];
}
if (($this->printStyle == 'invoice'
&& getSetting('invoice_show_delivery_info_in_invoice'))
|| ($this->printStyle == 'dispatch'
&& getSetting('dispatch_note_show_delivery_info'))