-
Notifications
You must be signed in to change notification settings - Fork 20
/
copy_invoice.php
executable file
·214 lines (199 loc) · 6.77 KB
/
copy_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
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
<?php
/**
* Logo handling
*
* PHP version 8
*
* Copyright (C) Ere Maijala 2010-2021
*
* 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 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 'htmlfuncs.php';
require_once 'sqlfuncs.php';
require_once 'sessionfuncs.php';
initDbConnection();
sesVerifySession();
require_once 'translator.php';
require_once 'datefuncs.php';
require_once 'miscfuncs.php';
require_once 'settings.php';
if (!sesWriteAccess()) {
echo htmlPageStart();
?>
<body>
<div class="container-fluid">
<div class="form_container">
<?php echo Translator::translate('NoAccess') . "\n"?>
</div>
</div>
</body>
</html>
<?php
return;
}
$intInvoiceId = getPostOrQuery('id', false);
$boolRefund = getPostOrQuery('refund', false);
$strFunc = getPostOrQuery('func', '');
$strList = getPostOrQuery('list', '');
$isOffer = !getPostOrQuery('invoice', false) && isOffer($intInvoiceId);
if ($intInvoiceId) {
if ($boolRefund) {
$strQuery = 'UPDATE {prefix}invoice ' . 'SET state_id = 4 '
. 'WHERE {prefix}invoice.id = ?';
dbParamQuery($strQuery, [$intInvoiceId]);
}
$strQuery = 'SELECT * ' . 'FROM {prefix}invoice '
. 'WHERE {prefix}invoice.id = ?';
$rows = dbParamQuery($strQuery, [$intInvoiceId]);
if (!$rows) {
echo htmlPageStart();
?>
<body>
<div class="container-fluid">
<div class="form_container">
<?php echo Translator::translate('RecordNotFound')?>
</div>
</div>
</body>
</html>
<?php
return;
}
$invoiceData = $rows[0];
$paymentDays = getPaymentDays($invoiceData['company_id']);
unset($invoiceData['id']);
unset($invoiceData['invoice_no']);
$invoiceData['deleted'] = 0;
if (!$boolRefund) {
unset($invoiceData['ref_number']);
if (!empty($invoiceData['company_id'])) {
$rows = dbParamQuery(
'SELECT default_ref_number FROM {prefix}company WHERE id=?',
[$invoiceData['company_id']]
);
$invoiceData['ref_number'] = isset($rows[0])
? $rows[0]['default_ref_number'] : null;
}
if (!empty($invoiceData['base_id'])) {
$rows = dbParamQuery(
'SELECT invoice_default_info FROM {prefix}base WHERE id=?',
[$invoiceData['base_id']]
);
$invoiceData['info'] = isset($rows[0])
? $rows[0]['invoice_default_info'] : null;
}
}
$invoiceData['invoice_date'] = date('Ymd');
$invoiceData['due_date'] = date(
'Ymd', mktime(0, 0, 0, date('m'), date('d') + $paymentDays, date('Y'))
);
$invoiceData['payment_date'] = null;
if ($isOffer) {
$invoiceData['state_id'] = getInitialOfferState();
} else {
$invoiceData['state_id'] = 1;
}
$invoiceData['archived'] = false;
$invoiceData['refunded_invoice_id'] = $boolRefund ? $intInvoiceId : null;
if ($boolRefund) {
$invoiceData['interval_type'] = 0;
$invoiceData['next_interval_date'] = null;
}
switch ($invoiceData['interval_type']) {
// Month
case 2:
$invoiceData['next_interval_date'] = date(
'Ymd', mktime(0, 0, 0, date('m') + 1, date('d'), date('Y'))
);
break;
// Year
case 3:
$invoiceData['next_interval_date'] = date(
'Ymd', mktime(0, 0, 0, date('m'), date('d'), date('Y') + 1)
);
break;
// 2 to 6 months
case 4:
case 5:
case 6:
case 7:
case 8:
$invoiceData['next_interval_date'] = date(
'Ymd',
mktime(
0, 0, 0, date('m') + $invoiceData['interval_type'] - 2,
date('d'), date('Y')
)
);
break;
}
dbQueryCheck('SET AUTOCOMMIT = 0');
dbQueryCheck('BEGIN');
try {
if ($invoiceData['interval_type'] > 0) {
// Reset interval type of the original invoice
$strQuery = 'UPDATE {prefix}invoice ' . 'SET interval_type = 0 ' .
'WHERE {prefix}invoice.id = ?';
dbParamQuery($strQuery, [$intInvoiceId], 'exception');
}
$strQuery = 'INSERT INTO {prefix}invoice(' .
implode(', ', array_keys($invoiceData)) . ') ' . 'VALUES (' .
str_repeat('?, ', count($invoiceData) - 1) . '?)';
dbParamQuery($strQuery, array_values($invoiceData), 'exception');
$intNewId = mysqli_insert_id($dblink);
if (!$intNewId) {
die('Could not get ID of the new invoice');
}
$newRowDate = date('Ymd');
$strQuery = 'SELECT * ' . 'FROM {prefix}invoice_row ' .
'WHERE deleted=0 AND invoice_id=?';
$rows = dbParamQuery($strQuery, [$intInvoiceId], 'exception');
foreach ($rows as $row) {
if ($boolRefund) {
$row['pcs'] = -$row['pcs'];
if ($row['partial_payment']) {
$row['price'] = -$row['price'];
}
} elseif ($row['reminder_row']) {
continue;
}
unset($row['id']);
$row['invoice_id'] = $intNewId;
if (getSetting('invoice_update_row_dates_on_copy')) {
$row['row_date'] = $newRowDate;
}
// Update product stock balance
if (!$isOffer && $row['product_id'] !== null) {
updateProductStockBalance(null, $row['product_id'], $row['pcs']);
}
$strQuery = 'INSERT INTO {prefix}invoice_row(' .
implode(', ', array_keys($row)) . ') ' . 'VALUES (' .
str_repeat('?, ', count($row) - 1) . '?)';
dbParamQuery($strQuery, $row, 'exception');
}
} catch (Exception $e) {
dbQueryCheck('ROLLBACK');
dbQueryCheck('SET AUTOCOMMIT = 1');
die($e->getMessage());
}
dbQueryCheck('COMMIT');
dbQueryCheck('SET AUTOCOMMIT = 1');
}
header("Location: index.php?func=$strFunc&list=$strList&form=invoice&id=$intNewId");