mirror of
https://github.com/itflow-org/itflow
synced 2026-03-01 03:14:52 +00:00
Add functions for retrieving setting values and
calculating taxes
This commit is contained in:
@@ -778,3 +778,59 @@ function roundToNearest15($time) {
|
|||||||
// Return the decimal hours
|
// Return the decimal hours
|
||||||
return number_format($decimalHours, 2);
|
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;
|
||||||
|
}
|
||||||
@@ -4,16 +4,18 @@ require_once "inc_all_reports.php";
|
|||||||
|
|
||||||
validateAccountantRole();
|
validateAccountantRole();
|
||||||
|
|
||||||
if (isset($_GET['year'])) {
|
$year = isset($_GET['year']) ? intval($_GET['year']) : date('Y');
|
||||||
$year = intval($_GET['year']);
|
|
||||||
} else {
|
$view = isset($_GET['view']) ? $_GET['view'] : 'quarterly';
|
||||||
$year = date('Y');
|
$company_currency = getSettingValue($mysqli, 'company_currency');
|
||||||
}
|
|
||||||
|
|
||||||
//GET unique years from expenses, payments and revenues
|
//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_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");
|
|||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<!-- View Selection Dropdown -->
|
||||||
|
<select onchange="this.form.submit()" class="form-control" name="view">
|
||||||
|
<option value="monthly" <?php if ($view == 'monthly') echo "selected"; ?>>Monthly</option>
|
||||||
|
<option value="quarterly" <?php if ($view == 'quarterly') echo "selected"; ?>>Quarterly</option>
|
||||||
</select>
|
</select>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<div class="table-responsive-sm">
|
<div class="table-responsive-sm">
|
||||||
<table class="table table-sm">
|
<table class="table table-sm">
|
||||||
<thead class="text-dark">
|
<thead class="text-dark">
|
||||||
<tr>
|
<tr>
|
||||||
<th>Tax</th>
|
<th>Tax</th>
|
||||||
<th class="text-right">Jan-Mar</th>
|
<?php
|
||||||
<th class="text-right">Apr-Jun</th>
|
if ($view == 'monthly') {
|
||||||
<th class="text-right">Jul-Sep</th>
|
for ($i = 1; $i <= 12; $i++) {
|
||||||
<th class="text-right">Oct-Dec</th>
|
echo "<th class='text-right'>" . date('M', mktime(0, 0, 0, $i, 10)) . "</th>";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo "<th class='text-right'>Jan-Mar</th>";
|
||||||
|
echo "<th class='text-right'>Apr-Jun</th>";
|
||||||
|
echo "<th class='text-right'>Jul-Sep</th>";
|
||||||
|
echo "<th class='text-right'>Oct-Dec</th>";
|
||||||
|
}
|
||||||
|
?>
|
||||||
<th class="text-right">Total</th>
|
<th class="text-right">Total</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<?php
|
<?php
|
||||||
while ($row = mysqli_fetch_array($sql_tax)) {
|
while ($row = mysqli_fetch_array($sql_tax)) {
|
||||||
$tax_id = intval($row['tax_id']);
|
echo "<tr>";
|
||||||
$tax_name = nullable_htmlentities($row['tax_name']);
|
echo "<td>" . $row['tax_name'] . "</td>";
|
||||||
|
|
||||||
|
if ($view == 'monthly') {
|
||||||
|
for ($i = 1; $i <= 12; $i++) {
|
||||||
|
$monthly_tax = getMonthlyTax($row['tax_name'], $i, $year, $mysqli);
|
||||||
|
echo "<td class='text-right'>" . numfmt_format_currency($currency_format, $monthly_tax, $company_currency) . "</td>";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for ($q = 1; $q <= 4; $q++) {
|
||||||
|
$quarterly_tax = getQuarterlyTax($row['tax_name'], $q, $year, $mysqli);
|
||||||
|
echo "<td class='text-right'>" . numfmt_format_currency($currency_format, $quarterly_tax, $company_currency) . "</td>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate total for row and echo bold
|
||||||
|
$total_tax = getTotalTax($row['tax_name'], $year, $mysqli);
|
||||||
|
echo "<td class='text-right text-bold'>" . numfmt_format_currency($currency_format, $total_tax, $company_currency) . "</td>";
|
||||||
|
echo "</tr>";
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
<td><?php echo $tax_name; ?></td>
|
<th>Total</th>
|
||||||
|
<?php
|
||||||
<?php
|
if ($view == 'monthly') {
|
||||||
|
for ($i = 1; $i <= 12; $i++) {
|
||||||
$tax_collected_quarter_one = 0;
|
$monthly_tax = getMonthlyTax($row['tax_name'], $i, $year, $mysqli);
|
||||||
|
echo "<th class='text-right'>" . numfmt_format_currency($currency_format, $monthly_tax, $company_currency) . "</th>";
|
||||||
for($month = 1; $month<=3; $month++) {
|
}
|
||||||
|
} else {
|
||||||
$sql_tax_collected = mysqli_query(
|
for ($q = 1; $q <= 4; $q++) {
|
||||||
$mysqli,
|
$quarterly_tax = getQuarterlyTax($row['tax_name'], $q, $year, $mysqli);
|
||||||
"SELECT SUM(item_tax) AS tax_collected_for_month
|
echo "<th class='text-right'>" . numfmt_format_currency($currency_format, $quarterly_tax, $company_currency) . "</th>";
|
||||||
FROM invoices, invoice_items
|
}
|
||||||
WHERE item_invoice_id = invoice_id
|
}
|
||||||
AND invoice_status LIKE 'Paid'
|
|
||||||
AND item_tax_id = $tax_id
|
|
||||||
AND YEAR(invoice_date) = $year AND MONTH(invoice_date) = $month"
|
|
||||||
);
|
|
||||||
|
|
||||||
$row = mysqli_fetch_array($sql_tax_collected);
|
|
||||||
$tax_collected_for_month = floatval($row['tax_collected_for_month']);
|
|
||||||
|
|
||||||
$tax_collected_quarter_one = $tax_collected_quarter_one + $tax_collected_for_month;
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
<td></td>
|
||||||
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $tax_collected_quarter_one, $session_company_currency); ?></td>
|
|
||||||
|
|
||||||
<?php
|
|
||||||
|
|
||||||
$tax_collected_quarter_two = 0;
|
|
||||||
|
|
||||||
for($month = 4; $month <= 6; $month ++) {
|
|
||||||
|
|
||||||
$sql_tax_collected = mysqli_query(
|
|
||||||
$mysqli,
|
|
||||||
"SELECT SUM(item_tax) AS tax_collected_for_month
|
|
||||||
FROM invoices, invoice_items
|
|
||||||
WHERE item_invoice_id = invoice_id
|
|
||||||
AND invoice_status LIKE 'Paid'
|
|
||||||
AND item_tax_id = $tax_id
|
|
||||||
AND YEAR(invoice_date) = $year AND MONTH(invoice_date) = $month"
|
|
||||||
);
|
|
||||||
|
|
||||||
$row = mysqli_fetch_array($sql_tax_collected);
|
|
||||||
$tax_collected_for_month = floatval($row['tax_collected_for_month']);
|
|
||||||
|
|
||||||
$tax_collected_quarter_two = $tax_collected_quarter_two + $tax_collected_for_month;
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
||||||
|
|
||||||
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $tax_collected_quarter_two, $session_company_currency); ?></td>
|
|
||||||
|
|
||||||
<?php
|
|
||||||
|
|
||||||
$tax_collected_quarter_three = 0;
|
|
||||||
|
|
||||||
for($month = 7; $month <= 9; $month ++) {
|
|
||||||
|
|
||||||
$sql_tax_collected = mysqli_query(
|
|
||||||
$mysqli,
|
|
||||||
"SELECT SUM(item_tax) AS tax_collected_for_month
|
|
||||||
FROM invoices, invoice_items
|
|
||||||
WHERE item_invoice_id = invoice_id
|
|
||||||
AND invoice_status LIKE 'Paid'
|
|
||||||
AND item_tax_id = $tax_id
|
|
||||||
AND YEAR(invoice_date) = $year AND MONTH(invoice_date) = $month"
|
|
||||||
);
|
|
||||||
|
|
||||||
$row = mysqli_fetch_array($sql_tax_collected);
|
|
||||||
$tax_collected_for_month = floatval($row['tax_collected_for_month']);
|
|
||||||
|
|
||||||
$tax_collected_quarter_three = $tax_collected_quarter_three + $tax_collected_for_month;
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
||||||
|
|
||||||
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $tax_collected_quarter_three, $session_company_currency); ?></td>
|
|
||||||
|
|
||||||
<?php
|
|
||||||
|
|
||||||
$tax_collected_quarter_four = 0;
|
|
||||||
|
|
||||||
for($month = 10; $month <= 12; $month ++) {
|
|
||||||
|
|
||||||
$sql_tax_collected = mysqli_query(
|
|
||||||
$mysqli,
|
|
||||||
"SELECT SUM(item_tax) AS tax_collected_for_month
|
|
||||||
FROM invoices, invoice_items
|
|
||||||
WHERE item_invoice_id = invoice_id
|
|
||||||
AND invoice_status LIKE 'Paid'
|
|
||||||
AND item_tax_id = $tax_id
|
|
||||||
AND YEAR(invoice_date) = $year AND MONTH(invoice_date) = $month"
|
|
||||||
);
|
|
||||||
|
|
||||||
$row = mysqli_fetch_array($sql_tax_collected);
|
|
||||||
$tax_collected_for_month = floatval($row['tax_collected_for_month']);
|
|
||||||
|
|
||||||
$tax_collected_quarter_four = $tax_collected_quarter_four + $tax_collected_for_month;
|
|
||||||
}
|
|
||||||
|
|
||||||
$total_tax_collected_four_quarters = $tax_collected_quarter_one + $tax_collected_quarter_two + $tax_collected_quarter_three + $tax_collected_quarter_four;
|
|
||||||
|
|
||||||
?>
|
|
||||||
|
|
||||||
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $tax_collected_quarter_four, $session_company_currency); ?></td>
|
|
||||||
|
|
||||||
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $total_tax_collected_four_quarters, $session_company_currency); ?></td>
|
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
<?php
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
||||||
|
|
||||||
<tr>
|
|
||||||
<th>Total Taxes<br><br><br></th>
|
|
||||||
<?php
|
|
||||||
|
|
||||||
$tax_collected_total_quarter_one = 0;
|
|
||||||
|
|
||||||
for($month = 1; $month <= 3; $month ++) {
|
|
||||||
|
|
||||||
$sql_tax_collected = mysqli_query(
|
|
||||||
$mysqli,
|
|
||||||
"SELECT SUM(item_tax) AS tax_collected_for_month
|
|
||||||
FROM invoices, invoice_items
|
|
||||||
WHERE item_invoice_id = invoice_id
|
|
||||||
AND invoice_status LIKE 'Paid'
|
|
||||||
AND YEAR(invoice_date) = $year AND MONTH(invoice_date) = $month"
|
|
||||||
);
|
|
||||||
|
|
||||||
$row = mysqli_fetch_array($sql_tax_collected);
|
|
||||||
$tax_collected_for_month = floatval($row['tax_collected_for_month']);
|
|
||||||
|
|
||||||
$tax_collected_total_quarter_one = $tax_collected_total_quarter_one + $tax_collected_for_month;
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
||||||
|
|
||||||
<th class="text-right"><?php echo numfmt_format_currency($currency_format, $tax_collected_total_quarter_one, $session_company_currency); ?></th>
|
|
||||||
|
|
||||||
<?php
|
|
||||||
|
|
||||||
$tax_collected_total_quarter_two = 0;
|
|
||||||
|
|
||||||
for($month = 4; $month <= 6; $month ++) {
|
|
||||||
|
|
||||||
$sql_tax_collected = mysqli_query(
|
|
||||||
$mysqli,
|
|
||||||
"SELECT SUM(item_tax) AS tax_collected_for_month
|
|
||||||
FROM invoices, invoice_items
|
|
||||||
WHERE item_invoice_id = invoice_id
|
|
||||||
AND invoice_status LIKE 'Paid'
|
|
||||||
AND YEAR(invoice_date) = $year AND MONTH(invoice_date) = $month"
|
|
||||||
);
|
|
||||||
|
|
||||||
$row = mysqli_fetch_array($sql_tax_collected);
|
|
||||||
$tax_collected_for_month = floatval($row['tax_collected_for_month']);
|
|
||||||
|
|
||||||
$tax_collected_total_quarter_two = $tax_collected_total_quarter_two + $tax_collected_for_month;
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
||||||
|
|
||||||
<th class="text-right"><?php echo numfmt_format_currency($currency_format, $tax_collected_total_quarter_two, $session_company_currency); ?></th>
|
|
||||||
|
|
||||||
<?php
|
|
||||||
|
|
||||||
$tax_collected_total_quarter_three = 0;
|
|
||||||
|
|
||||||
for($month = 7; $month <= 9; $month ++) {
|
|
||||||
|
|
||||||
$sql_tax_collected = mysqli_query(
|
|
||||||
$mysqli,
|
|
||||||
"SELECT SUM(item_tax) AS tax_collected_for_month
|
|
||||||
FROM invoices, invoice_items
|
|
||||||
WHERE item_invoice_id = invoice_id
|
|
||||||
AND invoice_status LIKE 'Paid'
|
|
||||||
AND YEAR(invoice_date) = $year AND MONTH(invoice_date) = $month"
|
|
||||||
);
|
|
||||||
|
|
||||||
$row = mysqli_fetch_array($sql_tax_collected);
|
|
||||||
$tax_collected_for_month = floatval($row['tax_collected_for_month']);
|
|
||||||
|
|
||||||
$tax_collected_total_quarter_three = $tax_collected_total_quarter_three + $tax_collected_for_month;
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
||||||
|
|
||||||
<th class="text-right"><?php echo numfmt_format_currency($currency_format, $tax_collected_total_quarter_three, $session_company_currency); ?></th>
|
|
||||||
|
|
||||||
<?php
|
|
||||||
|
|
||||||
$tax_collected_total_quarter_four = 0;
|
|
||||||
|
|
||||||
for($month = 10; $month <= 12; $month ++) {
|
|
||||||
|
|
||||||
$sql_tax_collected = mysqli_query(
|
|
||||||
$mysqli,
|
|
||||||
"SELECT SUM(item_tax) AS tax_collected_for_month
|
|
||||||
FROM invoices, invoice_items
|
|
||||||
WHERE item_invoice_id = invoice_id
|
|
||||||
AND invoice_status LIKE 'Paid'
|
|
||||||
AND YEAR(invoice_date) = $year AND MONTH(invoice_date) = $month"
|
|
||||||
);
|
|
||||||
|
|
||||||
$row = mysqli_fetch_array($sql_tax_collected);
|
|
||||||
$tax_collected_for_month = floatval($row['tax_collected_for_month']);
|
|
||||||
|
|
||||||
$tax_collected_total_quarter_four = $tax_collected_total_quarter_four + $tax_collected_for_month;
|
|
||||||
}
|
|
||||||
|
|
||||||
$tax_collected_total_all_four_quarters = $tax_collected_total_quarter_one + $tax_collected_total_quarter_two + $tax_collected_total_quarter_three + $tax_collected_total_quarter_four;
|
|
||||||
|
|
||||||
?>
|
|
||||||
|
|
||||||
<th class="text-right"><?php echo numfmt_format_currency($currency_format, $tax_collected_total_quarter_four, $session_company_currency); ?></th>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<th class="text-right"><?php echo numfmt_format_currency($currency_format, $tax_collected_total_all_four_quarters, $session_company_currency); ?></th>
|
|
||||||
</tr>
|
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user