This repository has been archived by the owner on Feb 21, 2019. It is now read-only.
forked from emaijala/MLInvoice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_reminder_fees.php
116 lines (105 loc) · 4.46 KB
/
add_reminder_fees.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
<?php
/*******************************************************************************
MLInvoice: web-based invoicing application.
Copyright (C) 2010-2015 Ere Maijala
This program is free software. See attached LICENSE.
*******************************************************************************/
/*******************************************************************************
MLInvoice: web-pohjainen laskutusohjelma.
Copyright (C) 2010-2015 Ere Maijala
Tämä ohjelma on vapaa. Lue oheinen LICENSE.
*******************************************************************************/
require_once 'localize.php';
require_once 'datefuncs.php';
require_once 'miscfuncs.php';
function addReminderFees($intInvoiceId)
{
$strAlert = '';
$strQuery = 'SELECT inv.due_date, inv.state_id, inv.print_date ' .
'FROM {prefix}invoice inv ' . 'WHERE inv.id = ?';
$intRes = mysqli_param_query($strQuery, [
$intInvoiceId
]);
if ($row = mysqli_fetch_assoc($intRes)) {
$intStateId = $row['state_id'];
$strDueDate = dateConvDBDate2Date($row['due_date']);
$strPrintDate = $row['print_date'];
} else {
return $GLOBALS['locRecordNotFound'];
}
$intDaysOverdue = floor((time() - strtotime($strDueDate)) / 60 / 60 / 24);
if ($intDaysOverdue <= 0) {
$strAlert = addslashes($GLOBALS['locInvoiceNotOverdue']);
} elseif ($intStateId == 3 || $intStateId == 4) {
$strAlert = addslashes($GLOBALS['locWrongStateForReminderFee']);
} else {
// Update invoice state
if ($intStateId == 1 || $intStateId == 2)
$intStateId = 5;
elseif ($intStateId == 5)
$intStateId = 6;
mysqli_param_query('UPDATE {prefix}invoice SET state_id=? where id=?',
[
$intStateId,
$intInvoiceId
]);
// Add reminder fee
if (getSetting('invoice_notification_fee')) {
// Remove old fee from same day
mysqli_param_query(
'UPDATE {prefix}invoice_row SET deleted=1 WHERE invoice_id=? AND reminder_row=2 AND row_date = ?',
[
$intInvoiceId,
date('Ymd')
]);
$strQuery = 'INSERT INTO {prefix}invoice_row (invoice_id, description, pcs, price, row_date, vat, vat_included, order_no, reminder_row) ' .
'VALUES (?, ?, 1, ?, ?, 0, 0, -2, 2)';
mysqli_param_query($strQuery,
[
$intInvoiceId,
$GLOBALS['locReminderFeeDesc'],
getSetting('invoice_notification_fee'),
date('Ymd')
]);
}
// Add penalty interest
$penaltyInterest = getSetting('invoice_penalty_interest');
if ($penaltyInterest) {
// Remove old penalty interest
mysqli_param_query(
'UPDATE {prefix}invoice_row SET deleted=1 WHERE invoice_id=? AND reminder_row=1',
[
$intInvoiceId
]);
// Add new interest
$intTotSumVAT = 0;
$strQuery = 'SELECT ir.pcs, ir.price, ir.discount, ir.vat, ir.vat_included, ir.reminder_row ' .
'FROM {prefix}invoice_row ir ' .
'WHERE ir.deleted=0 AND ir.invoice_id=?';
$intRes = mysqli_param_query($strQuery,
[
$intInvoiceId
]);
while ($row = mysqli_fetch_assoc($intRes)) {
if ($row['reminder_row']) {
continue;
}
list ($rowSum, $rowVAT, $rowSumVAT) = calculateRowSum($row['price'],
$row['pcs'], $row['vat'], $row['vat_included'], $row['discount']);
$intTotSumVAT += $rowSumVAT;
}
$intPenalty = $intTotSumVAT * $penaltyInterest / 100 * $intDaysOverdue /
360;
$strQuery = 'INSERT INTO {prefix}invoice_row (invoice_id, description, pcs, price, discount, row_date, vat, vat_included, order_no, reminder_row) ' .
'VALUES (?, ?, 1, ?, 0, ?, 0, 0, -1, 1)';
mysqli_param_query($strQuery,
[
$intInvoiceId,
$GLOBALS['locPenaltyInterestDesc'],
$intPenalty,
date('Ymd')
]);
}
}
return $strAlert;
}