Add handling for fixed pricing and tank cleaning transactions with action checks and enhanced customer retrieval

This commit is contained in:
Jepp9350
2025-06-02 22:04:57 +02:00
parent 27ca81ba8f
commit 9774b589cc
3 changed files with 217 additions and 1 deletions
+71
View File
@@ -582,4 +582,75 @@ class orders_o extends db
]);
return (bool)$count;
}
public function getFixedPricingTransactions(int $customer_number, false $asArray, string|null $dateFrom = null, string|null $dateTo = null): array
{
// Get the fixed pricing transactions for a customer
return self::listObjectsWithPagination(
1,
100,
null,
[
'customer_id' => $customer_number,
'deleted_at' => null,
'cashier_id' => (new collected_order_invoices_o())->economic_wash_subscription_user_id,
...(!empty($dateFrom) ? ['created_at-date_from' => $dateFrom] : []),
...(!empty($dateTo) ? ['created_at-date_to' => $dateTo] : []),
],
[
'id' => 'DESC',
],
function ($object) use ($asArray) {
$res = (new orders_o())->select($object['id']);
if ($asArray) {
return $res->asArray();
}
return $res;
}
);
}
/**
* @throws Exception
*/
public function getTankCleaningTransactions(int $customer_number, false $asArray, string $dateFrom, string $dateTo): array
{
// Get the tank cleaning transactions for a customer
return self::listObjectsWithPagination(
1,
100,
null,
[
'customer_id' => $customer_number,
'deleted_at' => null,
...(!empty($dateFrom) ? ['created_at-date_from' => $dateFrom] : []),
...(!empty($dateTo) ? ['created_at-date_to' => $dateTo] : []),
],
[
'id' => 'DESC',
],
function ($object) use ($asArray) {
$res = (new orders_o())->select($object['id']);
if ($asArray) {
return $res->asArray();
}
return $res;
}
);
}
/**
* @throws Exception
*/
public function isBooked(): bool
{
// Check the invoice collection has been booked
if ((int)$this->invoice_collection_id->value() > 0) {
$invoice_collection = (new collected_order_invoices_o())->select((int)$this->invoice_collection_id->value());
return $invoice_collection->isBooked();
} else {
// If there is no invoice collection, the order is not booked
return false;
}
}
}
+41
View File
@@ -1135,4 +1135,45 @@ class users_o extends db
return $customer_numbers;
}
public function getCustomersWithFixedPricing(): array
{
global $db;
// Get all customers with fixed pricing
$sql = "SELECT DISTINCT customer_number FROM customer_fixed_pricing";
$result = $db->query($sql);
$customer_numbers = [];
while ($row = $result->fetch_assoc()) {
$customer_numbers[] = (int)$row['customer_number'];
}
return $customer_numbers;
}
public function getCustomersWithTankCleaning(): array
{
global $db;
// Get all customers with tank cleaning
$sql = "SELECT DISTINCT user_id FROM customer_attributes WHERE attribute = 'onlyTankCleaning'";
$result = $db->query($sql);
$customer_numbers = [];
while ($row = $result->fetch_assoc()) {
$customer_numbers[] = (int)$row['user_id'];
}
// Get the customer numbers from the user IDs
$customer_numbers = array_unique($customer_numbers);
// Convert user IDs to customer numbers
return self::getCustomerNumbersFromUserIds($customer_numbers);
}
private static function getCustomerNumbersFromUserIds(array $user_ids): array
{
global $db;
$customer_numbers = (new users_o)->getFieldsWhere(
['id' => $user_ids],
['customer_number']
);
return array_map(function ($user) {
return (int)$user['customer_number'];
}, $customer_numbers);
}
}
@@ -72,7 +72,8 @@ class InvoicingPeriodRoute
'dateTo' => $dateTo,
'types' => [
'vehicle_subscriptions' => self::getVehicleSubscriptions($dateFrom, $dateTo),
'fixed_pricing' => [],
'fixed_pricing' => self::getFixedPricing($dateFrom, $dateTo),
'tank_cleaning' => self::getTankCleaning($dateFrom, $dateTo),
],
];
}
@@ -91,6 +92,7 @@ class InvoicingPeriodRoute
'customer_number' => $customer_number,
'customer_name' => (new \objects\users_o())->getCustomerName($customer_number),
'transactions' => [],
'requires_action' => true,
];
// Check if the customer has a vehicle subscription order within the date range
$orders = (new \objects\orders_o())->getWashSubscriptionTransactions($customer_number, false, $dateFrom, $dateTo);
@@ -102,9 +104,111 @@ class InvoicingPeriodRoute
'id' => $order->id,
];
}
// If there are transactions, set requires_action to false
if (count($tmp_subscription['transactions']) > 0) {
// Check if there's any transaction that has not been booked yet
$requires_action = false;
foreach ( $tmp_subscription['transactions'] as $transaction ) {
$order = (new orders_o())->select((int)$transaction['id']);
if (!$order->isBooked()) {
$requires_action = true;
break;
}
}
$tmp_subscription['requires_action'] = $requires_action;
}
// Add the subscription to the list if it has transactions
$subscriptions[] = $tmp_subscription;
}
return $subscriptions;
}
/**
* @throws \Exception
*/
private static function getFixedPricing(string $dateFrom, string $dateTo): array
{
// Get all customers with fixed pricing
$customer_numbers = (new \objects\users_o())->getCustomersWithFixedPricing();
$fixed_pricing = [];
/** @var int $customer_number */
foreach ( $customer_numbers as $customer_number ) {
$tmp_fixed_pricing = [
'customer_number' => $customer_number,
'customer_name' => (new \objects\users_o())->getCustomerName($customer_number),
'transactions' => [],
'requires_action' => true,
];
// Check if the customer has a fixed pricing order within the date range
$orders = (new \objects\orders_o())->getFixedPricingTransactions($customer_number, false, $dateFrom, $dateTo);
/** @var orders_o $order */
foreach ( $orders as $order ) {
// Add the order to the fixed pricing transactions
$tmp_fixed_pricing['transactions'][] = [
'id' => $order->id,
];
}
// If there are transactions, set requires_action to false
if (count($tmp_fixed_pricing['transactions']) > 0) {
// Check if there's any transaction that has not been booked yet
$requires_action = false;
foreach ( $tmp_fixed_pricing['transactions'] as $transaction ) {
$order = (new orders_o())->select((int)$transaction['id']);
if (!$order->isBooked()) {
$requires_action = true;
break;
}
}
// Set requires_action based on the transactions
$tmp_fixed_pricing['requires_action'] = $requires_action;
}
// Add the fixed pricing to the list if it has transactions
$fixed_pricing[] = $tmp_fixed_pricing;
}
return $fixed_pricing;
}
/**
* @throws \Exception
*/
private static function getTankCleaning(string $dateFrom, string $dateTo): array
{
// Get all customers with tank cleaning
$customer_numbers = (new \objects\users_o())->getCustomersWithTankCleaning();
$tank_cleaning = [];
/** @var int $customer_number */
foreach ( $customer_numbers as $customer_number ) {
$tmp_tank_cleaning = [
'customer_number' => $customer_number,
'customer_name' => (new \objects\users_o())->getCustomerName($customer_number),
'transactions' => [],
'requires_action' => false,
];
// Check if the customer has a tank cleaning order within the date range
$orders = (new \objects\orders_o())->getTankCleaningTransactions($customer_number, false, $dateFrom, $dateTo);
/** @var orders_o $order */
foreach ( $orders as $order ) {
// Add the order to the tank cleaning transactions
$tmp_tank_cleaning['transactions'][] = [
'id' => $order->id,
];
}
// If there are transactions, set requires_action to false
if (count($tmp_tank_cleaning['transactions']) > 0) {
// Check if there's any transaction that has not been booked yet
$requires_action = false;
foreach ( $tmp_tank_cleaning['transactions'] as $transaction ) {
$order = (new orders_o())->select($transaction['id']);
if (!$order->isBooked()) {
$requires_action = true;
break;
}
}
$tmp_tank_cleaning['requires_action'] = $requires_action;
}
// Add the tank cleaning to the list if it has transactions
$tank_cleaning[] = $tmp_tank_cleaning;
}
return $tank_cleaning;
}
}