Refactor getTargetItems method to include customer and department parameters for improved item normalization
This commit is contained in:
@@ -40,11 +40,15 @@ class orderBookingRoute
|
||||
$reference = self::getTargetReference(); // String | Null
|
||||
$po = self::getTargetPo(); // String | Null
|
||||
$pickup = self::getTargetPickup(); // Bool | Null
|
||||
$items = self::getTargetItems(); // Array of order_items_o objects
|
||||
$this->requireOrderBookingCreateAccess(
|
||||
(int)$customer_number->customer_number->value(),
|
||||
(int)$department->id
|
||||
);
|
||||
$items = self::getTargetItems(
|
||||
true,
|
||||
$customer_number,
|
||||
(int)$department->id
|
||||
); // Array of order_items_o objects
|
||||
/**
|
||||
* Input data
|
||||
*/
|
||||
@@ -257,7 +261,6 @@ class orderBookingRoute
|
||||
$reference = self::getTargetReference(false); // String | Null
|
||||
$po = self::getTargetPo(false); // String | Null
|
||||
$pickup = self::getTargetPickup(false); // Bool | Null
|
||||
$items = self::getTargetItems(false); // Array of order_items_o objects
|
||||
$order_id_was_set = self::isParametersSet(['order_id']);
|
||||
$order_id = self::getTargetOrderId(false); // Int | Null
|
||||
/** Authentication */
|
||||
@@ -284,6 +287,11 @@ class orderBookingRoute
|
||||
$ownGuard,
|
||||
'You do not have permission to edit this order booking.'
|
||||
);
|
||||
$items = self::getTargetItems(
|
||||
false,
|
||||
$customer_number ?: (new users_o())->getUserByCustomerNumber((int)$object->customer_number->value()),
|
||||
$department !== null ? (int)$department->id : (int)$object->department->value()
|
||||
); // Array of order_items_o objects
|
||||
/**
|
||||
* Update the object
|
||||
*/
|
||||
@@ -796,7 +804,11 @@ class orderBookingRoute
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private function getTargetItems(bool $required = true): array|null {
|
||||
private function getTargetItems(
|
||||
bool $required = true,
|
||||
?users_o $customer = null,
|
||||
?int $department_id = null
|
||||
): array|null {
|
||||
global $response;
|
||||
$parameter = 'items';
|
||||
$error = 'Invalid items';
|
||||
@@ -811,11 +823,52 @@ class orderBookingRoute
|
||||
foreach ($items as $key => $item) {
|
||||
self::requireType($item, self::type_array());
|
||||
self::requireValidItem((array)$item, $key);
|
||||
$items[$key]['name'] = (new products_o())->select((int)$item['id'])->name->value();
|
||||
$items[$key] = $this->normalizeBookingItem((array)$item, $customer, $department_id);
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private function normalizeBookingItem(array $item, ?users_o $customer, ?int $department_id): array
|
||||
{
|
||||
global $response;
|
||||
|
||||
$product = (new products_o())->select((int)$item['id']);
|
||||
if (!$product->exists()) {
|
||||
$response->error('Invalid item id', 400);
|
||||
}
|
||||
|
||||
$price = (int)$product->price->value();
|
||||
if ($department_id !== null) {
|
||||
$priceResolution = $product->getDepartmentPriceResolution($department_id);
|
||||
$price = (int)$priceResolution['price'];
|
||||
if (
|
||||
$customer !== null
|
||||
&& $customer->exists()
|
||||
&& !products_o::priceResolutionIsCustomMissing($priceResolution)
|
||||
) {
|
||||
$price = $customer->applyProductCustomerPricing(
|
||||
(int)$product->id,
|
||||
$price,
|
||||
true,
|
||||
$department_id
|
||||
);
|
||||
}
|
||||
} elseif ($customer !== null && $customer->exists()) {
|
||||
$price = $customer->applyProductCustomerPricing((int)$product->id, $price);
|
||||
}
|
||||
|
||||
return [
|
||||
...$item,
|
||||
'id' => (int)$product->id,
|
||||
'name' => (string)$product->name->value(),
|
||||
'quantity' => (int)$item['quantity'],
|
||||
'price' => $price,
|
||||
];
|
||||
}
|
||||
|
||||
private function cleanReg(string $reg): string
|
||||
{
|
||||
// Remove all non-alphanumeric characters
|
||||
|
||||
Reference in New Issue
Block a user