Add getOrderItemObjects and getProduct methods, and enhance Slack notifications in invoicing routes

- Introduce `getOrderItemObjects` in `orders_o` to retrieve item objects for an order.
- Add `getProduct` in `order_items_o` for product retrieval by item.
- Enhance Slack notification logic in invoicing routes for better error reporting and fixed pricing summaries.
- Temporarily comment out permission checks in certain invoicing endpoints for debugging purposes.
This commit is contained in:
Jeppe Bundgaard
2026-02-02 12:57:27 +01:00
parent c591439240
commit 08077df618
3 changed files with 66 additions and 2 deletions
@@ -339,4 +339,12 @@ class order_items_o extends db
}
return $items;
}
/**
* @throws Exception
*/
public function getProduct(): products_o
{
return (new products_o())->select((int)$this->product_id->value());
}
}
+17
View File
@@ -1549,4 +1549,21 @@ class orders_o extends db
$row = $result->fetch_assoc();
return (int)$row['wash_count'];
}
/**
* @throws Exception
* @retuns order_items_o[]
*/
public function getOrderItemObjects(): array
{
self::requireSelected();
$order_items = $this->getOrderItems((int)$this->id);
$order_item_objects = [];
foreach ( $order_items as $item ) {
$order_item = new order_items_o();
$order_item->select((int)$item['id']);
$order_item_objects[] = $order_item;
}
return $order_item_objects;
}
}
@@ -3,9 +3,11 @@
namespace routes;
use classes\authentication;
use classes\slack;
use Exception;
use objects\customer_vehicles_o;
use objects\logs_o;
use objects\order_items_o;
use objects\orders_o;
use objects\products_o;
use objects\users_o;
@@ -78,7 +80,7 @@ class InvoicingPeriodRoute
$this->get('/superuser/invoicing/period/distribution/fixed-pricing', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('superuser_invoicing_period');
// $this->requirePermission('superuser_invoicing_period');
self::requireParameters([
'dateFrom',
'dateTo',
@@ -100,7 +102,7 @@ class InvoicingPeriodRoute
$this->get('/superuser/invoicing/period/distribution/wash-subscriptions', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('superuser_invoicing_period');
//$this->requirePermission('superuser_invoicing_period');
self::requireParameters([
'dateFrom',
'dateTo',
@@ -295,6 +297,24 @@ class InvoicingPeriodRoute
$sum_of_distribution = array_sum($customer['meta']['subscription']['subscription_price_department_distribution']);
$difference = $customer['meta']['subscription']['subscription_total'] - $sum_of_distribution;
if (abs($difference) > 0.01) {
// Debug info
$debug_info = [
'customer_number' => $customer['customer_number'],
'subscription_total' => $customer['meta']['subscription']['subscription_total'],
'sum_of_distribution' => $sum_of_distribution,
'difference' => $difference,
];
// Send slack alert
$message = "Subscription price distribution mismatch for customer " . $customer['customer_number'] . " (" . (new users_o())->getCustomerName((int)$customer['customer_number']) . ")\n";
$message .= "Subscription total: " . $customer['meta']['subscription']['subscription_total'] . "\n";
$message .= "Distribution total: " . $sum_of_distribution . "\n";
$message .= "Difference: " . $difference . "\n";
$distribution_details = "Distribution details:\n";
$distribution_details .= print_r($customer['meta']['subscription']['subscription_price_department_distribution'], true);
$message .= $distribution_details . "\n";
$message .= "Debug info: " . print_r($debug_info, true);
// Describe what to check
(new slack())->send_message($message, 'Subscription Price Distribution Mismatch');
// Throw an error
throw new Exception('Subscription price distribution does not equal total subscription price for customer ' . $customer['customer_number'] . '. Difference: ' . $difference);
}
@@ -571,6 +591,25 @@ class InvoicingPeriodRoute
$collective_results = self::parseTheDepartmentIdsToDepartmentNames($collective_results);
// Include the collective results in the response
$response->add_include('collective_fixed_pricing_results', $collective_results);
// Send slack message with the collective results
$slack_message = "Fixed Pricing Invoicing Period Summary:\n";
$slack_message .= "Total Fixed Price: " . number_format($collective_results['total_fixed_price'], 2) . " DKK\n";
$slack_message .= "Total Original Price: " . number_format($collective_results['total_original_price'], 2) . " DKK\n";
$slack_message .= "Department Totals:\n";
$tmp_sum = 0;
foreach ( $collective_results['total_department_totals_parsed'] as $department_name => $amount ) {
$slack_message .= "- " . $department_name . ": " . number_format($amount, 2) . " DKK\n";
$tmp_sum += $amount;
}
$slack_message .= "Total Department Totals: " . number_format($tmp_sum, 2) . " DKK\n";
$slack_message .= "Relative Department Totals:\n";
$tmp_sum = 0;
foreach ( $collective_results['total_department_totals_relative_parsed'] as $department_name => $amount ) {
$slack_message .= "- " . $department_name . ": " . number_format($amount, 2) . " DKK\n";
$tmp_sum += $amount;
}
$slack_message .= "Total Relative Department Totals: " . number_format($tmp_sum, 2) . " DKK\n";
(new slack())->send_message($slack_message, 'Fixed Pricing Invoicing Period Summary');
return $fixed_pricing;
}