Introduce methods to generate and retrieve unique external IDs for collected order invoices, ensuring UUID-based uniqueness. Update user route actions to include new functionalities for editing user roles and passwords.
266 lines
9.2 KiB
PHP
266 lines
9.2 KiB
PHP
<?php
|
|
|
|
namespace objects;
|
|
|
|
use classes\db;
|
|
use classes\object_property;
|
|
use Exception;
|
|
use traits\db_object_t;
|
|
|
|
class collected_order_invoices_o extends db
|
|
{
|
|
use db_object_t;
|
|
|
|
public object_property $customer_number;
|
|
public object_property $name;
|
|
public object_property $notes;
|
|
public object_property $processor;
|
|
public object_property $external_id;
|
|
public object_property $created_at;
|
|
public object_property $updated_at;
|
|
public object_property $closed_at;
|
|
public array $processors = [
|
|
1 => 'E-conomic',
|
|
2 => 'Stripe',
|
|
];
|
|
|
|
/**
|
|
* Get the external id of the invoice collection
|
|
* @throws Exception If the request was not successful
|
|
* @throws Exception If the UUID could not be generated
|
|
* @throws Exception If the external id is not unique
|
|
*/
|
|
public function getExternalId(): string
|
|
{
|
|
if (empty($this->external_id->value())) {
|
|
$this->external_id->set(self::generateExternalId());
|
|
}
|
|
return $this->external_id->value();
|
|
}
|
|
|
|
/**
|
|
* Generate a unique external id (UUID)
|
|
* @return string The generated UUID
|
|
* @throws Exception If the UUID could not be generated
|
|
* @throws Exception If the request was not successful
|
|
*/
|
|
public static function generateExternalId(): string
|
|
{
|
|
$uuid = bin2hex(random_bytes(16));
|
|
return substr($uuid, 0, 8) . '-' . substr($uuid, 8, 4) . '-' . substr($uuid, 12, 4) . '-' . substr($uuid, 16, 4) . '-' . substr($uuid, 20);
|
|
}
|
|
|
|
public function structure(): void
|
|
{
|
|
$this->setTable('collected_order_invoices');
|
|
}
|
|
|
|
/**
|
|
* Create a new collected order invoice
|
|
* @param int $customer_number The E-conomic customer number
|
|
* @param string|null $name The name of the invoice (optional)
|
|
* @param string|null $notes The notes for the invoice (optional)
|
|
* @param int|null $processor The processor id (optional)
|
|
* @return self The created object
|
|
* @throws Exception If the object was not created successfully
|
|
*/
|
|
public function add(int $customer_number, ?string $name = null, ?string $notes = null, ?int $processor = null): self
|
|
{
|
|
global /** @var db $db */
|
|
$db;
|
|
// Sanitize the input
|
|
$customer_number = $db->escape_string($customer_number);
|
|
if (!empty($name)) {
|
|
$name = $db->escape_string($name);
|
|
}
|
|
if (!empty($notes)) {
|
|
$notes = $db->escape_string($notes);
|
|
}
|
|
if (!empty($processor)) {
|
|
$processor = $db->escape_string($processor);
|
|
}
|
|
// Require the customer number to be of a valid customer
|
|
self::requireValidCustomer($customer_number);
|
|
// Add the object
|
|
$tmp_id = self::add_object([
|
|
'customer_number' => (int)$customer_number,
|
|
'name' => $name,
|
|
'notes' => $notes
|
|
]);
|
|
self::select((int)$tmp_id);
|
|
self::requireSelected();
|
|
// Set the processor
|
|
if (!empty($processor)) {
|
|
$this->processor->set($processor);
|
|
}
|
|
self::objectChanged();
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Require the E-conomic customer number to be a valid customer
|
|
* @throws Exception If the customer number is not a valid customer
|
|
*/
|
|
private static function requireValidCustomer(string $customer_number): void
|
|
{
|
|
$customers = new users_o();
|
|
$customers->select($customer_number);
|
|
$customers->requireSelected();
|
|
}
|
|
|
|
public function objectChanged(): void
|
|
{
|
|
//TODO: Add cache invalidation
|
|
}
|
|
|
|
/**
|
|
* List all collected order invoices for a customer
|
|
* @param int $customer_number The E-conomic customer number
|
|
* @return array The list of collected order invoices
|
|
* @throws Exception If the request was not successful
|
|
*/
|
|
public function getCustomerInvoiceCollections(int $customer_number): array
|
|
{
|
|
$collections = self::getFieldsWhere(
|
|
[
|
|
'customer_number' => $customer_number,
|
|
'deleted_at' => null
|
|
],
|
|
['id', 'name', 'notes', 'processor', 'external_id', 'created_at', 'updated_at', 'closed_at']
|
|
);
|
|
// Parse the results
|
|
$result = [];
|
|
foreach ( $collections as $collection ) {
|
|
$this->id = $collection['id'];
|
|
self::getObjectProperties();
|
|
self::requireSelected();
|
|
$result[] = self::asArray();
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public function getObjectProperties(): void
|
|
{
|
|
$this->customer_number = new object_property($this->table, $this->id, 'customer_number', 'int', false);
|
|
$this->name = new object_property($this->table, $this->id, 'name', 'string', false);
|
|
$this->notes = new object_property($this->table, $this->id, 'notes', 'string', false);
|
|
$this->processor = new object_property($this->table, $this->id, 'processor', 'int', false);
|
|
$this->external_id = new object_property($this->table, $this->id, 'external_id', 'string', false);
|
|
$this->created_at = new object_property($this->table, $this->id, 'created_at', 'string', false);
|
|
$this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'string', false);
|
|
$this->closed_at = new object_property($this->table, $this->id, 'closed_at', 'string', false);
|
|
}
|
|
|
|
public function asArray(): array
|
|
{
|
|
return [
|
|
'id' => (int)$this->id,
|
|
'customer_number' => (int)$this->customer_number->value(),
|
|
'name' => (string)$this->name->value(),
|
|
'notes' => (string)$this->notes->value(),
|
|
'processor' => (int)$this->processor->value(),
|
|
'external_id' => (string)$this->external_id->value(),
|
|
'created_at' => (string)$this->created_at->value(),
|
|
'updated_at' => (string)$this->updated_at->value(),
|
|
'closed_at' => (string)$this->closed_at->value(),
|
|
'orders' => $this->getOrders()
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get the orders in the invoice collection
|
|
* @param bool $count If the count of orders should be returned instead of the orders
|
|
* @return array|int The list of orders in the invoice collection, or the count of orders if $count is true
|
|
* @throws Exception If the request was not successful
|
|
*/
|
|
public function getOrders(bool $count = false): array|int
|
|
{
|
|
$orders = new orders_o();
|
|
$order_ids = $orders->getFieldsWhere(
|
|
[
|
|
'invoice_collection_id' => $this->id,
|
|
'deleted_at' => null
|
|
],
|
|
['id']
|
|
);
|
|
if ($count) {
|
|
return count($order_ids);
|
|
}
|
|
$result = [];
|
|
foreach ( $order_ids as $order_id ) {
|
|
$orders->select($order_id['id']);
|
|
$orders->requireSelected();
|
|
$result[] = $orders->asArray();
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Check if a customer has an open invoice collection
|
|
* @param int $customer_number The E-conomic customer number
|
|
* @return bool If the customer has an open invoice collection
|
|
*/
|
|
public function hasOpenInvoiceCollection(int $customer_number): bool
|
|
{
|
|
$collections = self::getFieldsWhere(
|
|
[
|
|
'customer_number' => $customer_number,
|
|
'closed_at' => null,
|
|
],
|
|
['id']
|
|
);
|
|
return !empty($collections);
|
|
}
|
|
|
|
/**
|
|
* Get the open invoice collections for a customer
|
|
* @param int $customer_number The E-conomic customer number
|
|
* @return array The open invoice collections
|
|
* @throws Exception If the request was not successful
|
|
*/
|
|
public function getOpenInvoiceCollections(int $customer_number): array
|
|
{
|
|
$collections = self::getFieldsWhere(
|
|
[
|
|
'customer_number' => $customer_number,
|
|
'closed_at' => null,
|
|
],
|
|
['id', 'name', 'notes', 'processor', 'external_id', 'created_at', 'updated_at', 'closed_at']
|
|
);
|
|
// Parse the results
|
|
$result = [];
|
|
foreach ( $collections as $collection ) {
|
|
$this->id = $collection['id'];
|
|
self::getObjectProperties();
|
|
self::requireSelected();
|
|
$result[] = self::asArray();
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* @param int $customer_number The E-conomic customer number
|
|
* @return self The latest open invoice collection
|
|
* @throws Exception If no open invoice collections were found
|
|
* @throws Exception If the request was not successful
|
|
*/
|
|
public function getLatestOpenInvoiceCollection(int $customer_number): self
|
|
{
|
|
$collections = self::getFieldsWhere(
|
|
[
|
|
'customer_number' => $customer_number,
|
|
'closed_at' => null,
|
|
],
|
|
['id', 'name', 'notes', 'processor', 'external_id', 'created_at', 'updated_at', 'closed_at'],
|
|
);
|
|
if (empty($collections)) {
|
|
throw new Exception('No open invoice collections found');
|
|
}
|
|
// Parse the results
|
|
$this->id = $collections[0]['id'];
|
|
self::getObjectProperties();
|
|
self::requireSelected();
|
|
return $this;
|
|
}
|
|
|
|
} |