diff --git a/services/nginx/app/objects/customer_fixed_pricing_o.php b/services/nginx/app/objects/customer_fixed_pricing_o.php new file mode 100644 index 00000000..7459f2d8 --- /dev/null +++ b/services/nginx/app/objects/customer_fixed_pricing_o.php @@ -0,0 +1,121 @@ + (int)$this->id, + 'customer_number' => (int)$this->customer_number->value(), + 'price' => (int)$this->price->value(), + 'description' => (string)$this->description->value(), + 'created_at' => (string)$this->created_at->value(), + 'updated_at' => (string)$this->updated_at->value(), + ]; + } + + + /** + * @throws Exception If the creation of the object fails + */ + public function add(int $customer_number, int $price, string $description = ''): void + { + global $db, $response; + $tmp_id = self::add_object([ + 'customer_number' => (int)$customer_number, + 'price' => (int)$price, + 'description' => (string)$description, + ]); + self::select((int)$tmp_id); + self::objectChanged(); + } + + public function objectChanged(): void + { + // Since the customer_vehicles object is not cached, there is no need to invalidate the cache + } + + public function structure(): void + { + $this->setTable('customer_fixed_pricing'); + } + + public function getObjectProperties(): void + { + $this->customer_number = new object_property($this->table, $this->id, 'customer_number', 'int', true); + $this->price = new object_property($this->table, $this->id, 'price', 'int', true); + $this->description = new object_property($this->table, $this->id, 'description', 'string', true); + $this->created_at = new object_property($this->table, $this->id, 'created_at', 'timestamp', true); + $this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'timestamp', true); + } + + /** + * Get the customer_fixed_pricing object by customer number + * @param int $customer_number + * @return bool Returns true if the customer_fixed_pricing object exists, false otherwise. + */ + public function doesUserHaveFixedPricing(int $customer_number): bool + { + return $this->getIdByCustomerNumber($customer_number) !== null; + } + + /** + * Get the ID of the customer_fixed_pricing object by customer number + * @param int $customer_number + * @return int|null Returns the ID of the customer_fixed_pricing object, if it exists. Returns null, if it does not exist. + */ + public function getIdByCustomerNumber(int $customer_number): ?int + { + $matches = self::getFieldsWhere([ + 'customer_number' => (int)$customer_number, + ], [ + 'id' + ]); + if (count($matches) > 0) { + // There is a match, so return the ID + return (int)$matches[0]['id']; + } else { + // There's no match, so return null + return null; + } + } + + /** + * Get the customer_fixed_pricing object by customer number + * @param int $customer_number + * @return customer_fixed_pricing_o|null Returns the customer_fixed_pricing object, if it exists. Returns null, if it does not exist. + * @throws Exception If the user does not have fixed pricing, or if the object is not selected successfully. + * @see doesUserHaveFixedPricing() + * @see getIdByCustomerNumber() + */ + public function selectByCustomerNumber(int $customer_number): ?customer_fixed_pricing_o + { + $this->select($this->getIdByCustomerNumber($customer_number)); + if ($this->exists()) { + return $this; + } else { + throw new Exception('Unable to select fixed pricing item by customer_number, no item found'); + } + } +} \ No newline at end of file diff --git a/services/nginx/app/routes/customerFixedPricingRoute.php b/services/nginx/app/routes/customerFixedPricingRoute.php new file mode 100644 index 00000000..5d6af848 --- /dev/null +++ b/services/nginx/app/routes/customerFixedPricingRoute.php @@ -0,0 +1,149 @@ +get('/customer/pricing/fixed', function () { + // Require the user to be logged in + global $response; + self::requirePermission('get_customer_fixed_pricing'); + $user = (new authentication())->get_user(); + if (!$user) { + (new logs_o())->add('customer_fixed_pricing', 'global', 0, $user->id, 'CUSTOMER_GET_FIXED_PRICING', 'User not logged in'); + $response->error('Invalid session', 400); + } + // Require the customer number parameter + self::requireParameters(['customer_number']); + // Check if the customer number is valid + $customer_number = (int)self::getParameter('customer_number'); + self::requireMinLength('customer_number', 1); + self::requireMaxLength('customer_number', 255); + self::requireType($customer_number, self::type_int()); + self::requireMinValue($customer_number, 1); + // Check if the customer number exists + $customer = (new users_o())->getUserByCustomerNumber((int)$customer_number); + if (!$customer->exists()) { + (new logs_o())->add('customer_fixed_pricing', 'global', 0, $user->id, 'CUSTOMER_GET_FIXED_PRICING', 'Customer not found'); + $response->error('Customer not found', 400); + } + // Get the fixed price + $customer_fixed_pricing_o = new customer_fixed_pricing_o(); + $fixed_pricing_object_id = $customer_fixed_pricing_o->getIdByCustomerNumber((int)$customer_number); + if (empty($fixed_pricing_object_id)) { + // Return "Not found" + $response->error('This customer does not have a fixed price', 400); + } + // Select the object + $customer_fixed_pricing_o->select((int)$fixed_pricing_object_id); + // Log the action + (new logs_o())->add('customer_fixed_pricing', 'global', 0, $user->id, 'CUSTOMER_GET_FIXED_PRICING', 'Fixed price retrieved'); + // Return success + $response->success($customer_fixed_pricing_o->asArray()); + }, + [ + 'get_customer_fixed_pricing' => 'Get a fixed price for a customer', + ] + ); + $this->post('/customer/pricing/fixed', function () { + // Require the user to be logged in + global $response; + self::requirePermission('add_customer_fixed_pricing'); + $user = (new authentication())->get_user(); + if (!$user) { + (new logs_o())->add('customer_fixed_pricing', 'global', 0, $user->id, 'CUSTOMER_ADD_FIXED_PRICING', 'User not logged in'); + $response->error('Invalid session', 400); + } + // Require the customer number, price and description parameters + self::requireParameters(['customer_number', 'price', 'description']); + // Check if the customer number is valid + $customer_number = (int)self::getParameter('customer_number'); + self::requireMinLength('customer_number', 1); + self::requireMaxLength('customer_number', 255); + self::requireType($customer_number, self::type_int()); + self::requireMinValue($customer_number, 1); + // Check if the price is valid + $price = (int)self::getParameter('price'); + self::requireMinLength('price', 1); + self::requireMaxLength('price', 255); + self::requireType($price, self::type_int()); + self::requireMinValue($price, 1); + // Check if the description is valid + $description = (string)self::getParameter('description'); + self::requireMinLength('description', 1); + self::requireMaxLength('description', 255); + self::requireType($description, self::type_string()); + // Check if the customer number exists + $customer = (new users_o())->getUserByCustomerNumber((int)$customer_number); + if (!$customer->exists()) { + (new logs_o())->add('customer_fixed_pricing', 'global', 0, $user->id, 'CUSTOMER_ADD_FIXED_PRICING', 'Customer not found'); + $response->error('Customer not found', 400); + } + // Check if the customer already has a fixed price + $customer_fixed_pricing_o = new customer_fixed_pricing_o(); + $fixed_pricing_object_id = $customer_fixed_pricing_o->getIdByCustomerNumber((int)$customer_number); + if (!empty($fixed_pricing_object_id)) { + // Return "Already exists" + $response->error('This customer already has a fixed price', 400); + } + // Add the fixed price + $customer_fixed_pricing_o->add((int)$customer_number, (int)$price, (string)$description); + // Log the action + (new logs_o())->add('customer_fixed_pricing', 'global', 0, $user->id, 'CUSTOMER_ADD_FIXED_PRICING', 'Fixed price added'); + // Return success + $response->success($customer_fixed_pricing_o->asArray()); + }, + [ + 'add_customer_fixed_pricing' => 'Add a fixed price for a customer (This is a global action, should only be permitted superusers)', + ] + ); + + $this->delete('/customer/pricing/fixed', function () { + // Require the user to be logged in + global $response; + self::requirePermission('delete_customer_fixed_pricing'); + $user = (new authentication())->get_user(); + if (!$user) { + (new logs_o())->add('customer_fixed_pricing', 'global', 0, $user->id, 'CUSTOMER_DELETE_FIXED_PRICING', 'User not logged in'); + $response->error('Invalid session', 400); + } + // Require the customer number parameter + self::requireParameters(['customer_number']); + // Check if the customer number is valid + $customer_number = (int)self::getParameter('customer_number'); + self::requireMinLength('customer_number', 1); + self::requireMaxLength('customer_number', 255); + self::requireType($customer_number, self::type_int()); + self::requireMinValue($customer_number, 1); + // Check if the customer number exists + $customer = (new users_o())->getUserByCustomerNumber((int)$customer_number); + if (!$customer->exists()) { + (new logs_o())->add('customer_fixed_pricing', 'global', 0, $user->id, 'CUSTOMER_DELETE_FIXED_PRICING', 'Customer not found'); + $response->error('Customer not found', 400); + } + // Check if the customer already has a fixed price + $customer_fixed_pricing_o = new customer_fixed_pricing_o(); + $fixed_pricing_object = $customer_fixed_pricing_o->selectByCustomerNumber((int)$customer_number); + // Delete the fixed price + $fixed_pricing_object->delete(); + // Log the action + (new logs_o())->add('customer_fixed_pricing', 'global', 0, $user->id, 'CUSTOMER_DELETE_FIXED_PRICING', 'Fixed price deleted'); + // Return success + $response->success('Fixed price deleted'); + }, + [ + 'delete_customer_fixed_pricing' => 'Delete a fixed price for a customer (This is a global action, should only be permitted superusers)', + ] + ); + } +} \ No newline at end of file