forked from emaijala/MLInvoice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoice.php
executable file
·141 lines (118 loc) · 4.96 KB
/
invoice.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
<?php
/*******************************************************************************
MLInvoice: web-based invoicing application.
Copyright (C) 2010-2017 Ere Maijala
Portions based on:
PkLasku : web-based invoicing software.
Copyright (C) 2004-2008 Samu Reinikainen
This program is free software. See attached LICENSE.
*******************************************************************************/
/*******************************************************************************
MLInvoice: web-pohjainen laskutusohjelma.
Copyright (C) 2010-2017 Ere Maijala
Perustuu osittain sovellukseen:
PkLasku : web-pohjainen laskutusohjelmisto.
Copyright (C) 2004-2008 Samu Reinikainen
Tämä ohjelma on vapaa. Lue oheinen LICENSE.
*******************************************************************************/
require_once 'sessionfuncs.php';
sesVerifySession();
require_once 'vendor/autoload.php';
require_once 'sqlfuncs.php';
require_once 'translator.php';
require_once 'pdf.php';
require_once 'datefuncs.php';
require_once 'miscfuncs.php';
$intInvoiceId = getRequest('id', false);
$printTemplate = getRequest('template', 1);
$dateOverride = getRequest('date', false);
if (!is_string($dateOverride) || !ctype_digit($dateOverride)
|| strlen($dateOverride) != 8
) {
$dateOverride = false;
}
if (!$intInvoiceId) {
die('Id missing');
}
$res = mysqli_param_query(
'SELECT filename, parameters, output_filename from {prefix}print_template WHERE id=?',
[$printTemplate]
);
if (!$row = mysqli_fetch_row($res)) {
die('Could not find print template');
}
$printTemplateFile = $row[0];
$printParameters = $row[1];
$printOutputFileName = $row[2];
$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=?';
$intRes = mysqli_param_query($strQuery, [$intInvoiceId]);
$invoiceData = mysqli_fetch_assoc($intRes);
if (!$invoiceData) {
die('Could not find invoice data');
}
if (isOffer($intInvoiceId)) {
$invoiceData['invoice_no'] = $intInvoiceId;
}
$strQuery = 'SELECT * FROM {prefix}company WHERE id=?';
$intRes = mysqli_param_query($strQuery, [$invoiceData['company_id']]);
$recipientData = mysqli_fetch_assoc($intRes);
if (!empty($recipientData)) {
if (!empty($recipientData['company_id'])) {
$recipientData['vat_id'] = createVATID($recipientData['company_id']);
} else {
$recipientData['vat_id'] = '';
}
}
$recipientContactData = [];
$strQuery = 'SELECT * FROM {prefix}company_contact WHERE company_id=? AND deleted=0'
. ' ORDER BY id';
$intRes = mysqli_param_query($strQuery, [$invoiceData['company_id']]);
while ($contact = mysqli_fetch_assoc($intRes)) {
$recipientContactData[] = $contact;
}
$strQuery = 'SELECT * FROM {prefix}base WHERE id=?';
$intRes = mysqli_param_query($strQuery, [$invoiceData['base_id']]);
$senderData = mysqli_fetch_assoc($intRes);
if (!$senderData) {
die('Could not find invoice sender data');
}
$senderData['vat_id'] = createVATID($senderData['company_id']);
$queryParams = [$intInvoiceId];
$where = 'ir.invoice_id=? AND ir.deleted=0';
if ($dateOverride) {
$where .= ' AND row_date=?';
$queryParams[] = $dateOverride;
}
$strQuery = '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, 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";
$intRes = mysqli_param_query($strQuery, $queryParams);
$invoiceRowData = [];
while ($row = mysqli_fetch_assoc($intRes)) {
$invoiceRowData[] = $row;
}
sleep(3);
if (sesWriteAccess()) {
mysqli_param_query(
'UPDATE {prefix}invoice SET print_date=? where id=?',
[
date('Ymd'),
$intInvoiceId
]
);
}
$printer = instantiateInvoicePrinter(trim($printTemplateFile));
$printer->init(
$intInvoiceId, $printParameters, $printOutputFileName, $senderData,
$recipientData, $invoiceData, $invoiceRowData, $recipientContactData,
$dateOverride
);
$printer->printInvoice();