Refactor getOrdersWithPossibleDuplicates and InvoicingPeriodRoute: simplify duplicate order detection, enhance customer transaction handling, and streamline object construction for improved efficiency.
This commit is contained in:
@@ -952,6 +952,10 @@ class orders_o extends db
|
||||
}
|
||||
|
||||
/**
|
||||
* Get orders with possible duplicates in a date range
|
||||
* @param string $dateFrom The start date of the date range (inclusive) "Y-m-d H:i:s" format
|
||||
* @param string $dateTo The end date of the date range (inclusive) "Y-m-d H:i:s" format
|
||||
* @return array An array of possible duplicate orders, keyed by registration number.
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getOrdersWithPossibleDuplicates(string $dateFrom, string $dateTo): array
|
||||
@@ -980,45 +984,42 @@ class orders_o extends db
|
||||
'created_at' => (string)$row['created_at'],
|
||||
];
|
||||
// Add the order to the list
|
||||
$orders[$tmp['reg_1']][] = $tmp;
|
||||
$orders[$tmp['reg_1']][] = [
|
||||
'id' => $tmp['id'],
|
||||
'reg_1' => $tmp['reg_1'],
|
||||
'created_at' => $tmp['created_at'],
|
||||
'object' => (new orders_o())->select((int)$tmp['id'])
|
||||
];
|
||||
}
|
||||
// Filter out orders with more than one entry (possible duplicates)
|
||||
// More than one order with the same reg_1, consider it a possible duplicate
|
||||
$possibleDuplicates = array_filter($orders, function ($orderList) {
|
||||
return count($orderList) > 1; // Keep only those with more than one order
|
||||
});
|
||||
if (empty($possibleDuplicates)) {
|
||||
return []; // No possible duplicates found
|
||||
}
|
||||
// Loop through the possible duplicates and check if they are within 24 hours of each other
|
||||
foreach ( $possibleDuplicates as $reg_1 => $orderList ) {
|
||||
// Sort the orders by created_at date
|
||||
usort($orderList, function ($a, $b) {
|
||||
return strtotime($a['created_at']) - strtotime($b['created_at']);
|
||||
});
|
||||
// Check if any two orders are within 24 hours of each other
|
||||
for ( $i = 0; $i < count($orderList) - 1; $i++ ) {
|
||||
$firstOrder = $orderList[$i];
|
||||
$secondOrder = $orderList[$i + 1];
|
||||
if (strtotime($secondOrder['created_at']) - strtotime($firstOrder['created_at']) <= 86400) { // 86400 seconds in a day
|
||||
// Mark them as possible duplicates
|
||||
$possibleDuplicates[$reg_1][$i]['is_duplicate'] = true;
|
||||
$possibleDuplicates[$reg_1][$i + 1]['is_duplicate'] = true;
|
||||
// Filter out orders with more than one entry for the same registration number (in a 24 hour period)
|
||||
$possibleDuplicates = [];
|
||||
// Loop through the registration numbers
|
||||
foreach ( $orders as $reg_1 => $orderList ) {
|
||||
// If there are more than one order for the same registration number, add it to the possible duplicates
|
||||
if (count($orderList) > 1) {
|
||||
// Loop through the orders and check if they are within 24 hours of each other
|
||||
$filteredOrders = [];
|
||||
foreach ( $orderList as $order ) {
|
||||
// Check if the order is within 24 hours of the first order
|
||||
if (empty($filteredOrders)) {
|
||||
$filteredOrders[] = $order; // Add the first order
|
||||
} else {
|
||||
// Check if the order is within 24 hours of the first order
|
||||
$firstOrderTime = strtotime($filteredOrders[0]['created_at']);
|
||||
$currentOrderTime = strtotime($order['created_at']);
|
||||
if ($currentOrderTime - $firstOrderTime <= 86400) { // 86400 seconds = 24 hours
|
||||
$filteredOrders[] = $order; // Add the order to the filtered list
|
||||
}
|
||||
}
|
||||
}
|
||||
// If there are more than one order in the filtered list, add it to the possible duplicates
|
||||
if (count($filteredOrders) > 1) {
|
||||
$possibleDuplicates[$reg_1] = $filteredOrders;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Return the possible duplicates orders_o objects
|
||||
$resultOrders = [];
|
||||
foreach ( $possibleDuplicates as $reg_1 => $orderList ) {
|
||||
foreach ( $orderList as $order ) {
|
||||
// Create a new orders_o object and select the order by id
|
||||
$orderObject = new orders_o();
|
||||
$orderObject->select((int)$order['id']);
|
||||
// Add the order object to the result
|
||||
$resultOrders[] = $orderObject;
|
||||
}
|
||||
}
|
||||
return $resultOrders;
|
||||
// Return the possible duplicates
|
||||
return $possibleDuplicates;
|
||||
}
|
||||
|
||||
public function setTemporaryNetAmount(float $amount): void
|
||||
|
||||
@@ -439,26 +439,32 @@ class InvoicingPeriodRoute
|
||||
// Get orders with the same reg_1, that has been created within 24 hours of each other
|
||||
$orders = (new orders_o())->getOrdersWithPossibleDuplicates($dateFrom, $dateTo);
|
||||
// Get the customer numbers from the orders
|
||||
$customer_numbers = array_map(function ($order) {
|
||||
return (int)$order->customer_id->value();
|
||||
}, $orders);
|
||||
$tmp_customer_arr = [];
|
||||
// Remove duplicates from the customer numbers
|
||||
$customer_numbers = array_unique($customer_numbers);
|
||||
$possible_duplicates = [];
|
||||
/** @var int $customer_number */
|
||||
foreach ( $customer_numbers as $customer_number ) {
|
||||
$possible_duplicate = self::getCustomerFromList((int)$customer_number, $customersWithTransactions);
|
||||
// Set the requires_action to true, as these are potential duplicates
|
||||
$possible_duplicate['requires_action'] = true;
|
||||
$possible_duplicate['transactions'] = [];
|
||||
// Add the transactions to the possible duplicate
|
||||
foreach ( $orders as $order ) {
|
||||
if ((int)$order->customer_id->value() === $customer_number) {
|
||||
$possible_duplicate['transactions'][] = self::constructTransactionObject($order);
|
||||
}
|
||||
foreach ( $orders as $order ) {
|
||||
// Get the customer number from the order
|
||||
$customer_number = (int)$order[0]['object']->customer_id->value();
|
||||
// Check if the customer number is already in the array
|
||||
if (isset($tmp_customer_arr[$customer_number])) {
|
||||
continue;
|
||||
}
|
||||
// Add the possible duplicate to the list
|
||||
$possible_duplicates[] = $possible_duplicate;
|
||||
// Add the customer number to the array
|
||||
$tmp_customer_arr[$customer_number] = true;
|
||||
// Get the customer from the list of customers with transactions
|
||||
$customer = self::getCustomerFromList($customer_number, $customersWithTransactions);
|
||||
// Add the customer to the possible duplicates array
|
||||
$possible_duplicates[] = self::constructCustomerObject(
|
||||
$customer_number,
|
||||
(new \objects\users_o())->getCustomerName($customer_number) ?? 'Unknown Customer',
|
||||
array_map(function ($transaction) {
|
||||
// Construct the transaction object from the order
|
||||
return $transaction['object'];
|
||||
}, $order),
|
||||
true, // Requires action because there are possible duplicates
|
||||
$customer['id'] ?? null // Use the id from the customer object if available
|
||||
);
|
||||
}
|
||||
return $possible_duplicates;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user