From 0ba926abb46505aeda6d92958e79302241f48a00 Mon Sep 17 00:00:00 2001 From: Jepp9350 <2jepp9350@gmail.com> Date: Mon, 6 Jan 2025 11:52:05 +0100 Subject: [PATCH 1/3] Added economic_department_id to departments. --- objects/departments_o.php | 10 ++++++---- routes/departmentsRoute.php | 34 ++++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/objects/departments_o.php b/objects/departments_o.php index 0e525e28..489b6ae7 100644 --- a/objects/departments_o.php +++ b/objects/departments_o.php @@ -12,20 +12,21 @@ class departments_o extends db public object_property $name; public object_property $description; + public object_property $economic_department_id; // The id of the department in the economic system (Can be null) public function structure(): void { $this->setTable('departments'); } - public function create(string $name, string $description): void + public function create(string $name, string $description, ?int $economic_department_id): void { global $db; // Avoid SQL injection $name = $db->escape_string($name); $description = $db->escape_string($description); // Create a new record in the database - $sql = "INSERT INTO $this->table (name, description) VALUES ('$name', '$description')"; + $sql = "INSERT INTO $this->table (name, description, economic_department_id) VALUES ('$name', '$description', $economic_department_id)"; $db->query($sql); // Get the id of the new record @@ -39,9 +40,10 @@ class departments_o extends db { $this->name = new object_property($this->table, $this->id, 'name', 'string', true); $this->description = new object_property($this->table, $this->id, 'description', 'string', false); + $this->economic_department_id = new object_property($this->table, $this->id, 'economic_department_id', 'int', false); } - public function edit(int $id, string $name, string $description): void + public function edit(int $id, string $name, string $description, ?int $economic_department_id): void { global $db; $this->id = $id; @@ -49,7 +51,7 @@ class departments_o extends db $name = $db->escape_string($name); $description = $db->escape_string($description); // Update the record in the database - $sql = "UPDATE $this->table SET name = '$name', description = '$description' WHERE id = $this->id"; + $sql = "UPDATE $this->table SET name = '$name', description = '$description', economic_department_id = $economic_department_id WHERE id = $this->id"; $db->query($sql); // Set the values of the object properties diff --git a/routes/departmentsRoute.php b/routes/departmentsRoute.php index 355fc557..67dac24d 100644 --- a/routes/departmentsRoute.php +++ b/routes/departmentsRoute.php @@ -5,7 +5,6 @@ namespace routes; use classes\authentication; use objects\departments_o; use objects\logs_o; -use objects\users_o; use traits\route_t; class departmentsRoute @@ -47,10 +46,19 @@ class departmentsRoute // Get the post data $data = json_decode(file_get_contents('php://input'), true); // Check if the required fields are set - if (!isset($data['name'])) { $response->error('Name is required', 400); } - if (!isset($data['description'])) { $response->error('Description is required', 400); } + if (!isset($data['name'])) { + $response->error('Name is required', 400); + } + if (!isset($data['description'])) { + $response->error('Description is required', 400); + } + $economic_department_id = null; + if (isset($data['economic_department_id'])) { + $economic_department_id = (int)$data['economic_department_id']; + } + // Add the department - (new departments_o())->create($data['name'], $data['description']); + (new departments_o())->create($data['name'], $data['description'], $economic_department_id); // Log the incident (new logs_o())->add('departments', 'global', 1, $user->id, 'ADD_DEPARTMENT', 'Successfully added a department ' . $data['name']); // Return a success message @@ -74,11 +82,21 @@ class departmentsRoute // Get the post data $data = json_decode(file_get_contents('php://input'), true); // Check if the required fields are set - if (!isset($data['id'])) { $response->error('ID is required', 400); } - if (!isset($data['name'])) { $response->error('Name is required', 400); } - if (!isset($data['description'])) { $response->error('Description is required', 400); } + if (!isset($data['id'])) { + $response->error('ID is required', 400); + } + if (!isset($data['name'])) { + $response->error('Name is required', 400); + } + if (!isset($data['description'])) { + $response->error('Description is required', 400); + } + $economic_department_id = null; + if (isset($data['economic_department_id'])) { + $economic_department_id = (int)$data['economic_department_id']; + } // Update the department - (new departments_o())->edit($data['id'], $data['name'], $data['description']); + (new departments_o())->edit($data['id'], $data['name'], $data['description'], $economic_department_id); // Log the incident (new logs_o())->add('departments', $data['id'], 1, $user->id, 'EDIT_DEPARTMENT', 'Successfully updated a department ' . $data['name']); // Return a success message From 16c1716cd0c134bd38b1119521c12c522a4364c4 Mon Sep 17 00:00:00 2001 From: Jepp9350 <2jepp9350@gmail.com> Date: Mon, 6 Jan 2025 12:20:29 +0100 Subject: [PATCH 2/3] Fixed formatting --- objects/departments_o.php | 12 ++++++++---- routes/departmentsRoute.php | 13 ++++--------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/objects/departments_o.php b/objects/departments_o.php index 489b6ae7..4af29656 100644 --- a/objects/departments_o.php +++ b/objects/departments_o.php @@ -19,14 +19,14 @@ class departments_o extends db $this->setTable('departments'); } - public function create(string $name, string $description, ?int $economic_department_id): void + public function create(string $name, string $description): void { global $db; // Avoid SQL injection $name = $db->escape_string($name); $description = $db->escape_string($description); // Create a new record in the database - $sql = "INSERT INTO $this->table (name, description, economic_department_id) VALUES ('$name', '$description', $economic_department_id)"; + $sql = "INSERT INTO $this->table (name, description) VALUES ('$name', '$description')"; $db->query($sql); // Get the id of the new record @@ -43,15 +43,19 @@ class departments_o extends db $this->economic_department_id = new object_property($this->table, $this->id, 'economic_department_id', 'int', false); } - public function edit(int $id, string $name, string $description, ?int $economic_department_id): void + public function edit(int $id, string $name, string $description, int $economic_department_id): void { global $db; $this->id = $id; // Avoid SQL injection $name = $db->escape_string($name); $description = $db->escape_string($description); + // If the economic_department_id is 0, set it to null + if ($economic_department_id === 0) { + $economic_department_id = null; + } // Update the record in the database - $sql = "UPDATE $this->table SET name = '$name', description = '$description', economic_department_id = $economic_department_id WHERE id = $this->id"; + $sql = "UPDATE $this->table SET name = '$name', description = '$description', economic_department_id = " . ($economic_department_id ?? 'NULL') . " WHERE id = $this->id"; $db->query($sql); // Set the values of the object properties diff --git a/routes/departmentsRoute.php b/routes/departmentsRoute.php index 67dac24d..b40863e3 100644 --- a/routes/departmentsRoute.php +++ b/routes/departmentsRoute.php @@ -52,13 +52,9 @@ class departmentsRoute if (!isset($data['description'])) { $response->error('Description is required', 400); } - $economic_department_id = null; - if (isset($data['economic_department_id'])) { - $economic_department_id = (int)$data['economic_department_id']; - } // Add the department - (new departments_o())->create($data['name'], $data['description'], $economic_department_id); + (new departments_o())->create($data['name'], $data['description']); // Log the incident (new logs_o())->add('departments', 'global', 1, $user->id, 'ADD_DEPARTMENT', 'Successfully added a department ' . $data['name']); // Return a success message @@ -91,12 +87,11 @@ class departmentsRoute if (!isset($data['description'])) { $response->error('Description is required', 400); } - $economic_department_id = null; - if (isset($data['economic_department_id'])) { - $economic_department_id = (int)$data['economic_department_id']; + if (!isset($data['economic_department_id'])) { + $response->error('Economic department ID is required', 400); } // Update the department - (new departments_o())->edit($data['id'], $data['name'], $data['description'], $economic_department_id); + (new departments_o())->edit($data['id'], $data['name'], $data['description'], (int)$data['economic_department_id']); // Log the incident (new logs_o())->add('departments', $data['id'], 1, $user->id, 'EDIT_DEPARTMENT', 'Successfully updated a department ' . $data['name']); // Return a success message From 36537fcc1edd117ec1ee4db8f235ad299fd98256 Mon Sep 17 00:00:00 2001 From: Jepp9350 <2jepp9350@gmail.com> Date: Mon, 6 Jan 2025 13:36:16 +0100 Subject: [PATCH 3/3] Added support for E-conomic departments. --- .../draft/economic_invoice_draft_mo.php | 19 +++++++++++++++++-- routes/economicInvoiceRoute.php | 5 +++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/modules/economic/invoices/draft/economic_invoice_draft_mo.php b/modules/economic/invoices/draft/economic_invoice_draft_mo.php index eeb69750..661eca3f 100644 --- a/modules/economic/invoices/draft/economic_invoice_draft_mo.php +++ b/modules/economic/invoices/draft/economic_invoice_draft_mo.php @@ -10,11 +10,11 @@ class economic_invoice_draft_mo extends economicInvoicesDrafts protected array $recipient; // Recipient of the invoice (Includes name, address, zip, city, and (array)vatZone) protected array $lines; // Lines of the invoice (Includes product, quantity, unitNetPrice, discountPercentage, and (array)vatRate) - public function addLine(string $productNumber, string $description, float $quantity, float $unitNetPrice, float $discountPercentage): void + public function addLine(string $productNumber, string $description, float $quantity, float $unitNetPrice, float $discountPercentage, int $economic_department_id): void { // We then add a 0 in front of the product number to make sure it's the right one, made by FlexPOS. // Add a line to the invoice - $this->lines[] = [ + $line = [ 'product' => [ 'productNumber' => $productNumber, ], @@ -23,6 +23,14 @@ class economic_invoice_draft_mo extends economicInvoicesDrafts 'discountPercentage' => $discountPercentage, 'description' => $description, ]; + // If the department is set, add it to the line + if ($economic_department_id) { + $line['departmentalDistribution'] = [ + 'departmentalDistributionNumber' => $economic_department_id, + 'DistributionType' => 'Department' + ]; + } + $this->lines[] = $line; } public function addLineTEXT(string $text): void @@ -108,4 +116,11 @@ class economic_invoice_draft_mo extends economicInvoicesDrafts $url = '/invoices/booked/' . $param . '/pdf'; return $this->send_request($url, 'GET'); } + + public function getInvoiceDraft(int $int): object + { + // Get the invoice draft + $url = '/invoices/drafts/' . $int; + return json_decode($this->send_request($url, 'GET')); + } } \ No newline at end of file diff --git a/routes/economicInvoiceRoute.php b/routes/economicInvoiceRoute.php index 919ada37..159e0725 100644 --- a/routes/economicInvoiceRoute.php +++ b/routes/economicInvoiceRoute.php @@ -276,6 +276,10 @@ class economicInvoiceRoute if ($order_item['price'] !== $order_item['product']['price']) $line .= ', Discount: ' . ($order_item['price'] - $order_item['product']['price']) . ' DKK (~' . $discountPercentage . '%)'; + // Get the department + $department = $order->getDepartmentByOrderId($order->id); + $economic_department_id = $department['economic_department_id']; + // Add the line to the invoice $economic_invoice_draft->addLine( (string)$order_item['product']['economic_product_id'], @@ -283,6 +287,7 @@ class economicInvoiceRoute (int)$quantity, (int)$order_item['price'], 0, // Since we can't be specific about the discount, we set it to 0. (The API has a limit of 2 decimals, and that's not enough for our needs) + (int)$economic_department_id ?? 0 ); } } \ No newline at end of file