Merge pull request #2 from copenhagentruckwash/development
Add support for E-conomic departments and fix formatting issues
This commit is contained in:
@@ -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'));
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ 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
|
||||
{
|
||||
@@ -39,17 +40,22 @@ 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;
|
||||
// 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' 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
|
||||
|
||||
@@ -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,8 +46,13 @@ 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);
|
||||
}
|
||||
|
||||
// Add the department
|
||||
(new departments_o())->create($data['name'], $data['description']);
|
||||
// Log the incident
|
||||
@@ -74,11 +78,20 @@ 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);
|
||||
}
|
||||
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']);
|
||||
(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
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user