diff --git a/functions.php b/functions.php index 9f1523d7..7c3f19a0 100644 --- a/functions.php +++ b/functions.php @@ -778,3 +778,59 @@ function roundToNearest15($time) { // Return the decimal hours return number_format($decimalHours, 2); } + +// Get the value of a setting from the database +function getSettingValue($mysqli, $setting_name) { + //if starts with config_ then get from config table + if (substr($setting_name, 0, 7) == "config_") { + $sql = mysqli_query($mysqli, "SELECT $setting_name FROM settings"); + $row = mysqli_fetch_array($sql); + return $row[$setting_name]; + } elseif (substr($setting_name, 0, 7) == "company") { + $sql = mysqli_query($mysqli, "SELECT $setting_name FROM companies"); + $row = mysqli_fetch_array($sql); + return $row[$setting_name]; + } else { + return "Cannot Find Setting Name"; + } +} + +function getMonthlyTax($tax_name, $month, $year, $mysqli) { + // SQL to calculate monthly tax + $sql = "SELECT SUM(item_tax) AS monthly_tax FROM invoice_items + LEFT JOIN invoices ON invoice_items.item_invoice_id = invoices.invoice_id + LEFT JOIN payments ON invoices.invoice_id = payments.payment_invoice_id + 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')"; + $result = mysqli_query($mysqli, $sql); + $row = mysqli_fetch_assoc($result); + return $row['monthly_tax'] ?? 0; +} + +function getQuarterlyTax($tax_name, $quarter, $year, $mysqli) { + // Calculate start and end months for the quarter + $start_month = ($quarter - 1) * 3 + 1; + $end_month = $start_month + 2; + + // SQL to calculate quarterly tax + $sql = "SELECT SUM(item_tax) AS quarterly_tax FROM invoice_items + LEFT JOIN invoices ON invoice_items.item_invoice_id = invoices.invoice_id + LEFT JOIN payments ON invoices.invoice_id = payments.payment_invoice_id + 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')"; + $result = mysqli_query($mysqli, $sql); + $row = mysqli_fetch_assoc($result); + return $row['quarterly_tax'] ?? 0; +} + +function getTotalTax($tax_name, $year, $mysqli) { + // SQL to calculate total tax + $sql = "SELECT SUM(item_tax) AS total_tax FROM invoice_items + LEFT JOIN invoices ON invoice_items.item_invoice_id = invoices.invoice_id + LEFT JOIN payments ON invoices.invoice_id = payments.payment_invoice_id + WHERE YEAR(payments.payment_date) = $year + AND invoice_items.item_tax_id = (SELECT tax_id FROM taxes WHERE tax_name = '$tax_name')"; + $result = mysqli_query($mysqli, $sql); + $row = mysqli_fetch_assoc($result); + return $row['total_tax'] ?? 0; +} \ No newline at end of file diff --git a/report_tax_summary.php b/report_tax_summary.php index 41fd0aeb..ea041602 100644 --- a/report_tax_summary.php +++ b/report_tax_summary.php @@ -4,16 +4,18 @@ require_once "inc_all_reports.php"; validateAccountantRole(); -if (isset($_GET['year'])) { - $year = intval($_GET['year']); -} else { - $year = date('Y'); -} +$year = isset($_GET['year']) ? intval($_GET['year']) : date('Y'); + +$view = isset($_GET['view']) ? $_GET['view'] : 'quarterly'; +$company_currency = getSettingValue($mysqli, 'company_currency'); + //GET unique years from expenses, payments and revenues $sql_all_years = mysqli_query($mysqli, "SELECT DISTINCT(YEAR(item_created_at)) AS all_years FROM invoice_items ORDER BY all_years DESC"); -$sql_tax = mysqli_query($mysqli, "SELECT * FROM taxes ORDER BY tax_name ASC"); +$sql_tax = mysqli_query($mysqli, + "SELECT `tax_name` + FROM `taxes`"); ?> @@ -38,254 +40,77 @@ $sql_tax = mysqli_query($mysqli, "SELECT * FROM taxes ORDER BY tax_name ASC"); } ?> + + + + +
| Tax | -Jan-Mar | -Apr-Jun | -Jul-Sep | -Oct-Dec | + " . date('M', mktime(0, 0, 0, $i, 10)) . ""; + } + } else { + echo "Jan-Mar | "; + echo "Apr-Jun | "; + echo "Jul-Sep | "; + echo "Oct-Dec | "; + } + ?>Total | " . $row['tax_name'] . " | "; + if ($view == 'monthly') { + for ($i = 1; $i <= 12; $i++) { + $monthly_tax = getMonthlyTax($row['tax_name'], $i, $year, $mysqli); + echo "" . numfmt_format_currency($currency_format, $monthly_tax, $company_currency) . " | "; + } + } else { + for ($q = 1; $q <= 4; $q++) { + $quarterly_tax = getQuarterlyTax($row['tax_name'], $q, $year, $mysqli); + echo "" . numfmt_format_currency($currency_format, $quarterly_tax, $company_currency) . " | "; + } + } + + // Calculate total for row and echo bold + $total_tax = getTotalTax($row['tax_name'], $year, $mysqli); + echo "" . numfmt_format_currency($currency_format, $total_tax, $company_currency) . " | "; + echo ""; + } + ?>
|---|---|---|---|---|---|---|---|---|---|
| - - Total + " . numfmt_format_currency($currency_format, $monthly_tax, $company_currency) . ""; + } + } else { + for ($q = 1; $q <= 4; $q++) { + $quarterly_tax = getQuarterlyTax($row['tax_name'], $q, $year, $mysqli); + echo " | " . numfmt_format_currency($currency_format, $quarterly_tax, $company_currency) . " | "; + } + } ?> - -- - - - | - - - - | - - - - | - - | + | |||
| Total Taxes |
-
-
- - - - - | - - - - | - - - - | - - - - - | - |