Files
api/services/nginx/app/objects/customer_vehicles_o.php
T
Jepp9350 29e76b58d8 Fix and enhance order processing with improved data handling
Set proper timestamps for collected invoices and added `getLastOrderId` for vehicles. Streamlined order fetching logic by fixing parameter usage, refining customer and order item retrieval, and improving type casting in multiple methods.
2025-04-29 15:22:26 +02:00

197 lines
7.0 KiB
PHP

<?php
namespace objects;
use classes\db;
use classes\object_property;
use Exception;
use traits\db_object_t;
class customer_vehicles_o extends db
{
use db_object_t;
public object_property $customer_id; // The id of the customer
public object_property $type; // The type of the vehicle
public object_property $reg; // The registration number of the vehicle
public object_property $wash_subscription; // The wash subscription of the vehicle (boolean)
public object_property $notes; // The notes of the vehicle
public object_property $deleted_at; // The timestamp of when the record was "deleted"
/**
* @param array $getAddons [db_object_t child object]
* @return array
*/
private static function transformObjectsToArray(array $getAddons): array
{
return array_map(function ($addon) {
return $addon->asArray();
}, $getAddons);
}
/**
* Convert the object to an array
* @return array
* @throws Exception If the object is not selected
*/
public function asArray(): array
{
self::requireSelected();
$wash_subscription = (bool)$this->wash_subscription->value();
$addons = self::transformObjectsToArray(
self::getAddons()
);
$available_addons = self::transformObjectsToArray(
self::getAvailableAddons()
);
$last_order_id = self::getLastOrderId();
return [
'id' => (int)$this->id,
'user_id' => (int)(new users_o())->getUserIdFromEconomic((int)$this->customer_id->value()),
'customer_id' => (int)$this->customer_id->value(),
'customer_name' => (string)(new users_o())->getCustomerName((int)$this->customer_id->value()),
'type' => (int)$this->type->value(),
'reg' => (string)$this->reg->value(),
'wash_subscription' => $wash_subscription,
'addons' => [
'enabled' => count($addons),
'available' => count($available_addons),
'list' => $addons,
],
'last_order_id' => ($last_order_id ? (int)$last_order_id : null),
];
}
/**
* Get the addons for the vehicle
* @return array{customer_vehicles_addons_o}
* @throws Exception If the object is not selected
*/
public function getAddons(): array
{
self::requireSelected();
$addons = (new customer_vehicles_addons_o())->getFieldsWhere([
'vehicle_id' => (int)$this->id,
], [
'id',
]);
return array_map(function ($addon) {
return (new customer_vehicles_addons_o())->select($addon['id']);
}, $addons);
}
/**
* Get the available addons for the vehicle
* @return array{product_options_o}
* @throws Exception If the object is not selected
*/
public function getAvailableAddons(): array
{
self::requireSelected();
$available_addons = (new product_options_o())->getProductOptions((int)$this->type->value());
// Filter the addons, to only show the ones that has "subscription_allowed" set to true
$available_addons = array_filter($available_addons, function ($addon) {
return (bool)$addon['product']['subscription_allowed'];
});
return array_map(function ($addon) {
return (new product_options_o())->select((int)$addon['id']);
}, $available_addons);
}
/**
* Get the last order id for the vehicle
* @return int|null
* @throws Exception If the object is not selected
* @throws Exception If the there is an error selecting the order
*/
private function getLastOrderId(): ?int
{
self::requireSelected();
$orders_o = (new orders_o());
$orders = $orders_o->getFieldsWhere([
'reg_1' => (string)$this->reg->value(),
'deleted_at' => null,
], [
'id',
]);
if (count($orders) > 0) {
$last_order = array_pop($orders);
return (int)$last_order['id'];
}
return null;
}
/**
* Add the default addons to the vehicle
* @return void
* @throws Exception If the object is not selected
* @throws Exception If the there is an error adding the addon
* @throws Exception If the there is an error selecting the addon
* @throws Exception If the there is an error selecting the vehicle
*/
public function addDefaultAddons(): void
{
self::requireSelected();
// If any of the available addons have the product id in the $automatic_addon_product_ids array, add it to the vehicle
$automatic_addon_product_ids = [
23, // Spot Free- Varevogn
24, // Spot Free- Lastbil
];
$available_addons = self::getAvailableAddons();
/** @var product_options_o $addon */
foreach ( $available_addons as $addon ) {
//echo 'Vehicle id: ' . $this->id . PHP_EOL;
//echo 'Addon id: ' . $addon->id . PHP_EOL;
//echo 'Primary product id: ' . $addon->product_id->value() . PHP_EOL;
//echo 'Addon product id: ' . $addon->option_id->value() . PHP_EOL;
//echo 'Is in array: ' . (in_array((int)$addon->option_id->value(), $automatic_addon_product_ids) ? 'true' : 'false') . PHP_EOL;
//print_r($available_addons);
if (in_array((int)$addon->option_id->value(), $automatic_addon_product_ids)) {
// Add the addon to the vehicle
(new customer_vehicles_addons_o())->add(
(int)$this->id,
(int)$addon->id,
(int)1,
);
}
}
}
/**
* @throws Exception If the creation of the object fails
*/
public function add(int $customer_number, int $type, string $reg, bool $subscription): void
{
global $db, $response;
$tmp_id = self::add_object([
'customer_id' => (int)$customer_number,
'type' => (int)$type,
'reg' => (string)$reg,
'wash_subscription' => (bool)$subscription ? 1 : 0,
]);
self::select((int)$tmp_id);
self::objectChanged();
self::addDefaultAddons();
}
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_vehicles');
}
public function getObjectProperties(): void
{
$this->customer_id = new object_property($this->table, $this->id, 'customer_id', 'int', true);
$this->type = new object_property($this->table, $this->id, 'type', 'int', true);
$this->reg = new object_property($this->table, $this->id, 'reg', 'string', true);
$this->wash_subscription = new object_property($this->table, $this->id, 'wash_subscription', 'bool', false);
$this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'timestamp', false);
}
}