Add water usage calculation to department daily reports
- Introduced `getTransactionsOnDateWaterUsage` method in `department_daily_reports_o` to calculate water usage based on transactions for a given date or date range. - Updated department daily reports routes to include `water_usage` in the output.
This commit is contained in:
@@ -560,4 +560,59 @@ class department_daily_reports_o extends db
|
||||
}
|
||||
return (int)$amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function calculates how much water has been used on the transactions on a given date
|
||||
* That is calculated by getting the water usage report on the earliest report before the given date
|
||||
* and the water usage report on the latest report on or before the given date
|
||||
* The difference between the two is the water usage for the given date
|
||||
* @note This does not count products from removed orders, only products from orders that are still active
|
||||
* @param string $date The date to get the report for (YYYY-MM-DD)
|
||||
* @param int $department_id The id of the department
|
||||
* @param string|null $date_to (Optional) The end date to get the report for (YYYY-MM-DD). If provided, the report will be for the date range between $date and $date_to
|
||||
* @return int The amount of water used on the given date
|
||||
*/
|
||||
public function getTransactionsOnDateWaterUsage(string $date, int $department_id, string $date_to = null): int
|
||||
{
|
||||
global /** @var db $db */
|
||||
$db;
|
||||
// If the date_to is null, set it to the date
|
||||
if ($date_to === null) {
|
||||
$date_to = $date; // Making the report for an entire day
|
||||
}
|
||||
$conn = $db->conn();
|
||||
// Get the earliest report before the given date
|
||||
$stmt = $conn->prepare(
|
||||
'SELECT water_usage FROM department_daily_reports o
|
||||
WHERE o.created_at < ? AND o.department_id = ? AND o.water_usage > 0 ORDER BY o.created_at DESC LIMIT 1'
|
||||
);
|
||||
if ($stmt) {
|
||||
$stmt->bind_param('si', $date, $department_id); // Bind parameters
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result(); // Get the result set from the statement
|
||||
$data = $result->fetch_assoc(); // Fetch the result as an associative array
|
||||
$earliest_usage = $data ? (int)$data['water_usage'] : 0;
|
||||
$stmt->close(); // Close the statement
|
||||
} else {
|
||||
// Handle query preparation error
|
||||
die('Query preparation failed: ' . $conn->error);
|
||||
}
|
||||
// Get the report closest to the date_to, but not after
|
||||
$stmt = $conn->prepare(
|
||||
'SELECT water_usage FROM department_daily_reports o
|
||||
WHERE o.created_at <= ? AND o.department_id = ? AND o.water_usage > 0 ORDER BY o.created_at DESC LIMIT 1'
|
||||
);
|
||||
if ($stmt) {
|
||||
$stmt->bind_param('si', $date_to, $department_id); // Bind parameters
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result(); // Get the result set from the statement
|
||||
$data = $result->fetch_assoc(); // Fetch the result as an associative array
|
||||
$latest_usage = $data ? (int)$data['water_usage'] : 0;
|
||||
$stmt->close(); // Close the statement
|
||||
} else {
|
||||
// Handle query preparation error
|
||||
die('Query preparation failed: ' . $conn->error);
|
||||
}
|
||||
return $latest_usage - $earliest_usage;
|
||||
}
|
||||
}
|
||||
@@ -506,6 +506,13 @@ class departmentDailyReportsRoute
|
||||
(int)self::getParameter('department_id'),
|
||||
$date_to
|
||||
),
|
||||
'water_usage' =>
|
||||
(new department_daily_reports_o())
|
||||
->getTransactionsOnDateWaterUsage(
|
||||
(string)self::getParameter('date'),
|
||||
(int)self::getParameter('department_id'),
|
||||
$date_to
|
||||
),
|
||||
'date' => (string)self::getParameter('date'),
|
||||
'department_id' => (int)self::getParameter('department_id'),
|
||||
'date_to' => $date_to,
|
||||
|
||||
Reference in New Issue
Block a user