Add support for dynamic currency conversion in invoices

Introduced dynamic currency handling in invoice generation by adding methods to set currency and retrieve conversion rates. Adjusted logic to ensure accurate currency conversions when processing orders, draft invoices, and customer data. Default behavior falls back to DKK if no currency is specified.
This commit is contained in:
Jepp9350
2025-03-31 14:42:19 +02:00
parent 67cf1c5880
commit 68ab9bc84c
6 changed files with 89 additions and 11 deletions
@@ -4,6 +4,7 @@ namespace helpers;
use classes\economic;
use Exception;
use objects\currency_conversion_rates_o;
use objects\departments_o;
use objects\orders_o;
@@ -26,15 +27,28 @@ class economic_invoice_draft
* @var object $draft_invoice_data
*/
protected object $draft_invoice_data;
/**
* The currency for the draft invoice
* @var string $currency
* @note The currency is set to DKK by default
*/
protected string $currency = 'DKK';
/**
* The currency conversion rate
* @var float $conversion_rate
* @note The conversion rate is set to 1.0 by default
*/
protected float $conversion_rate = 1.0;
/**
* Construct a new Economic draft invoice object
* @throws Exception if the invoice data is invalid or empty
*/
public function __construct(int $draft_invoice_number)
public function __construct(int $draft_invoice_number, string $currency = 'DKK')
{
$this->setDraftInvoiceNumber($draft_invoice_number);
$this->setCurrency(strtoupper($currency));
$this->fetchDraftInvoiceData();
$this->requireSelected();
}
@@ -49,6 +63,21 @@ class economic_invoice_draft
$this->draft_invoice_number = $draft_invoice_number;
}
/**
* Set the currency for the draft invoice
* @param string $currency The currency to set
* @return void
* @throws Exception If the currency is not valid
* @throws Exception If the conversion rate is not valid
*/
private function setCurrency(string $currency): void
{
// Set the currency for the draft invoice
$this->currency = $currency;
// Get the conversion rate for the currency
$this->conversion_rate = (float)(new currency_conversion_rates_o())->convertTo($this->currency, 1);
}
/**
* Fetch the draft invoice data from the Economic system
* @throws Exception If the request fails
@@ -268,7 +297,7 @@ class economic_invoice_draft
'productNumber' => $productNumber,
],
'quantity' => $quantity,
'unitNetPrice' => $unitNetPrice,
'unitNetPrice' => self::convertCurrency($unitNetPrice),
'discountPercentage' => 0,
'description' => $description,
];
@@ -283,6 +312,20 @@ class economic_invoice_draft
$this->draft_lines[] = $line;
}
private function convertCurrency(float $amount): float
{
// Convert the amount to the correct currency
$converted_value = $amount * self::getConversionRate();
// Round the value to 2 decimal places
return round($converted_value, 2);
}
private function getConversionRate(): float
{
// Get the conversion rate for the currency
return $this->conversion_rate;
}
/**
* Add a discount line to the draft invoice
* @note The lines won't be saved until the addLines() method is called.
@@ -299,7 +342,7 @@ class economic_invoice_draft
'productNumber' => 'TotDiscount',
],
'quantity' => 1,
'unitNetPrice' => -$discount,
'unitNetPrice' => self::convertCurrency(-$discount),
'discountPercentage' => 0,
'description' => 'Rabat',
];