mirror of
https://github.com/itflow-org/itflow
synced 2026-09-21 14:11:17 +00:00
Fix Tax Summary double counting tax on partially paid invoices
This commit is contained in:
@@ -86,21 +86,19 @@ $sql_tax = mysqli_query($mysqli, "SELECT `tax_name` FROM `taxes`");
|
|||||||
|
|
||||||
if ($view == 'monthly') {
|
if ($view == 'monthly') {
|
||||||
|
|
||||||
|
// Row total = sum of this tax’s 12 months, accumulated as we go
|
||||||
|
$row_total = 0.0;
|
||||||
for ($i = 1; $i <= 12; $i++) {
|
for ($i = 1; $i <= 12; $i++) {
|
||||||
$monthly_tax = (float) getMonthlyTax($tax_name, $i, $year, $mysqli);
|
$monthly_tax = (float) getMonthlyTax($tax_name, $i, $year, $mysqli);
|
||||||
|
|
||||||
// Accumulate totals
|
// Accumulate totals
|
||||||
$monthly_totals[$i] += $monthly_tax;
|
$monthly_totals[$i] += $monthly_tax;
|
||||||
$grand_total += $monthly_tax;
|
$grand_total += $monthly_tax;
|
||||||
|
$row_total += $monthly_tax;
|
||||||
|
|
||||||
echo "<td class='text-end'>" . numfmt_format_currency($currency_format, $monthly_tax, $company_currency) . "</td>";
|
echo "<td class='text-end'>" . numfmt_format_currency($currency_format, $monthly_tax, $company_currency) . "</td>";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Row total = sum of this tax’s 12 months
|
|
||||||
$row_total = 0.0;
|
|
||||||
for ($i = 1; $i <= 12; $i++) {
|
|
||||||
$row_total += (float) getMonthlyTax($tax_name, $i, $year, $mysqli);
|
|
||||||
}
|
|
||||||
echo "<td class='text-end text-bold'>" . numfmt_format_currency($currency_format, $row_total, $company_currency) . "</td>";
|
echo "<td class='text-end text-bold'>" . numfmt_format_currency($currency_format, $row_total, $company_currency) . "</td>";
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -644,12 +644,20 @@ function checkForUpdates() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getMonthlyTax($tax_name, $month, $year, $mysqli) {
|
function getMonthlyTax($tax_name, $month, $year, $mysqli) {
|
||||||
// SQL to calculate monthly tax
|
// Cash basis - tax is booked to the month the money arrived, in proportion to
|
||||||
$sql = "SELECT SUM(item_tax) AS monthly_tax FROM invoice_items
|
// how much of the invoice that payment covered. Driving off payments (rather
|
||||||
LEFT JOIN invoices ON invoice_items.item_invoice_id = invoices.invoice_id
|
// than invoice_items) counts each payment exactly once, and pre-aggregating
|
||||||
LEFT JOIN payments ON invoices.invoice_id = payments.payment_invoice_id
|
// the line items stops a multi-payment invoice multiplying its own tax.
|
||||||
|
$sql = "SELECT SUM(invoice_tax.tax_total * (payments.payment_amount / invoices.invoice_amount)) AS monthly_tax
|
||||||
|
FROM payments
|
||||||
|
INNER JOIN invoices ON invoices.invoice_id = payments.payment_invoice_id
|
||||||
|
INNER JOIN (SELECT item_invoice_id, SUM(item_tax) AS tax_total
|
||||||
|
FROM invoice_items
|
||||||
|
WHERE item_tax_id = (SELECT tax_id FROM taxes WHERE tax_name = '$tax_name')
|
||||||
|
GROUP BY item_invoice_id) AS invoice_tax
|
||||||
|
ON invoice_tax.item_invoice_id = invoices.invoice_id
|
||||||
WHERE YEAR(payments.payment_date) = $year AND MONTH(payments.payment_date) = $month
|
WHERE YEAR(payments.payment_date) = $year AND MONTH(payments.payment_date) = $month
|
||||||
AND invoice_items.item_tax_id = (SELECT tax_id FROM taxes WHERE tax_name = '$tax_name')";
|
AND invoices.invoice_amount > 0";
|
||||||
$result = mysqli_query($mysqli, $sql);
|
$result = mysqli_query($mysqli, $sql);
|
||||||
$row = mysqli_fetch_assoc($result);
|
$row = mysqli_fetch_assoc($result);
|
||||||
return $row['monthly_tax'] ?? 0;
|
return $row['monthly_tax'] ?? 0;
|
||||||
@@ -660,12 +668,17 @@ function getQuarterlyTax($tax_name, $quarter, $year, $mysqli) {
|
|||||||
$start_month = ($quarter - 1) * 3 + 1;
|
$start_month = ($quarter - 1) * 3 + 1;
|
||||||
$end_month = $start_month + 2;
|
$end_month = $start_month + 2;
|
||||||
|
|
||||||
// SQL to calculate quarterly tax
|
// SQL to calculate quarterly tax - see getMonthlyTax for why it is shaped this way
|
||||||
$sql = "SELECT SUM(item_tax) AS quarterly_tax FROM invoice_items
|
$sql = "SELECT SUM(invoice_tax.tax_total * (payments.payment_amount / invoices.invoice_amount)) AS quarterly_tax
|
||||||
LEFT JOIN invoices ON invoice_items.item_invoice_id = invoices.invoice_id
|
FROM payments
|
||||||
LEFT JOIN payments ON invoices.invoice_id = payments.payment_invoice_id
|
INNER JOIN invoices ON invoices.invoice_id = payments.payment_invoice_id
|
||||||
|
INNER JOIN (SELECT item_invoice_id, SUM(item_tax) AS tax_total
|
||||||
|
FROM invoice_items
|
||||||
|
WHERE item_tax_id = (SELECT tax_id FROM taxes WHERE tax_name = '$tax_name')
|
||||||
|
GROUP BY item_invoice_id) AS invoice_tax
|
||||||
|
ON invoice_tax.item_invoice_id = invoices.invoice_id
|
||||||
WHERE YEAR(payments.payment_date) = $year AND MONTH(payments.payment_date) BETWEEN $start_month AND $end_month
|
WHERE YEAR(payments.payment_date) = $year AND MONTH(payments.payment_date) BETWEEN $start_month AND $end_month
|
||||||
AND invoice_items.item_tax_id = (SELECT tax_id FROM taxes WHERE tax_name = '$tax_name')";
|
AND invoices.invoice_amount > 0";
|
||||||
$result = mysqli_query($mysqli, $sql);
|
$result = mysqli_query($mysqli, $sql);
|
||||||
$row = mysqli_fetch_assoc($result);
|
$row = mysqli_fetch_assoc($result);
|
||||||
return $row['quarterly_tax'] ?? 0;
|
return $row['quarterly_tax'] ?? 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user