Skip to content

Commit

Permalink
Merge pull request #1 from dalrankov/hotfix/1.0.1
Browse files Browse the repository at this point in the history
Hotfix/1.0.1 Fix decimal rounding and formatting
  • Loading branch information
dalrankov authored Feb 9, 2023
2 parents d93fe47 + a03d145 commit fefe6b3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
24 changes: 24 additions & 0 deletions Extensions/DecimalExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Globalization;

namespace OpenMicroFiscal.Extensions;

public static class DecimalExtensions
{
public static decimal RoundTo(this decimal value, int decimals)
{
var roundedValue = Math.Round(value, decimals, MidpointRounding.AwayFromZero);
var formattedRoundedValue = roundedValue.ToFormattedString(decimals);
return decimal.Parse(formattedRoundedValue, CultureInfo.InvariantCulture);
}

public static decimal IncreaseBy(this decimal value, decimal percentage)
{
return value + value * 0.01M * percentage;
}

public static string ToFormattedString(this decimal value, int decimals)
{
const char zeroChar = '0';
return value.ToString($"{zeroChar}.{new string(zeroChar, decimals)}", CultureInfo.InvariantCulture);
}
}
34 changes: 16 additions & 18 deletions InvoiceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,29 @@ public async Task<CreateInvoiceResult> CreateInvoiceAsync(
var invoiceItems = createInvoiceRequest.Items
.Select(item =>
{
const decimal defaultVatPercentage = 21.00M;
const decimal defaultVatPercentage = 21.0000M;
var resultItem = new Item
{
Name = item.Name,
Unit = item.Unit,
UnitPrice = item.UnitPrice,
UnitPrice = item.UnitPrice.RoundTo(4),
Quantity = item.Quantity,
TotalPriceBeforeVat = item.UnitPrice * item.Quantity,
VatPercentage = item.VatPercentage ?? defaultVatPercentage
VatPercentage = item.VatPercentage?.RoundTo(4) ?? defaultVatPercentage
};
var unitPriceAfterVat = item.UnitPrice + item.UnitPrice * 0.01M * resultItem.VatPercentage;
resultItem.UnitPriceAfterVat = Math.Round(unitPriceAfterVat, 2, MidpointRounding.AwayFromZero);
resultItem.TotalPriceAfterVat = resultItem.UnitPriceAfterVat * item.Quantity;
resultItem.TotalVatAmount = resultItem.TotalPriceAfterVat - resultItem.TotalPriceBeforeVat;
resultItem.UnitPriceAfterVat = resultItem.UnitPrice.IncreaseBy(resultItem.VatPercentage).RoundTo(4);
resultItem.TotalPriceBeforeVat = (resultItem.UnitPrice * resultItem.Quantity).RoundTo(4);
resultItem.TotalPriceAfterVat = (resultItem.UnitPriceAfterVat * resultItem.Quantity).RoundTo(4);
resultItem.TotalVatAmount = (resultItem.TotalPriceAfterVat - resultItem.TotalPriceBeforeVat).RoundTo(4);
return resultItem;
})
.ToList();

var totalPrice = invoiceItems.Sum(i => i.TotalPriceAfterVat);
var totalVatAmount = invoiceItems.Sum(i => i.TotalVatAmount);
var totalPriceWithoutVat = invoiceItems.Sum(i => i.TotalPriceBeforeVat);
var totalPrice = invoiceItems.Sum(i => i.TotalPriceAfterVat).RoundTo(2);
var totalVatAmount = invoiceItems.Sum(i => i.TotalVatAmount).RoundTo(2);
var totalPriceWithoutVat = invoiceItems.Sum(i => i.TotalPriceBeforeVat).RoundTo(2);

var currentDateTime = DateTime.UtcNow.WithoutSeconds();

Expand Down Expand Up @@ -125,9 +123,9 @@ public async Task<CreateInvoiceResult> CreateInvoiceAsync(
.GroupBy(item => item.VatPercentage)
.Select(vatPercentageGroup => new SameTax
{
VatPercentage = vatPercentageGroup.Key,
VatAmount = vatPercentageGroup.Sum(x => x.TotalVatAmount),
PriceBeforeVat = vatPercentageGroup.Sum(x => x.TotalPriceBeforeVat),
VatPercentage = vatPercentageGroup.Key.RoundTo(2),
VatAmount = vatPercentageGroup.Sum(x => x.TotalVatAmount).RoundTo(2),
PriceBeforeVat = vatPercentageGroup.Sum(x => x.TotalPriceBeforeVat).RoundTo(2),
TotalItems = vatPercentageGroup.Sum(x => x.Quantity)
})
.ToList()
Expand Down Expand Up @@ -171,7 +169,7 @@ public async Task<CreateInvoiceResult> CreateInvoiceAsync(
["bu"] = _settings.IssuerBusinessUnitCode,
["cr"] = _settings.IssuerEnuCode,
["sw"] = _settings.IssuerSoftwareCode,
["prc"] = totalPrice
["prc"] = totalPrice.ToFormattedString(2)
};

var queryString = string.Join("&", urlQueryParams.Select(p => $"{p.Key}={p.Value}"));
Expand Down Expand Up @@ -201,7 +199,7 @@ public async Task<CreateInvoiceResult> CreateInvoiceAsync(
_settings.IssuerBusinessUnitCode,
_settings.IssuerEnuCode,
_settings.IssuerSoftwareCode,
totalPrice);
totalPrice.ToFormattedString(2));

var iicSignatureBytes = certificate
.GetRSAPrivateKey()!
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Registarski podaci softvera:

- Proizvođač/Održavaoc: `NOVI ALGORITAM DOO`
- Naziv: `OpenMicroFiscal`
- Verzija: `1.0.0`
- Identifikator: `cg193xb896`
- Verzija: `1.0.1`
- Identifikator: `lf281wm877`

> ❗Napomena: Kompanija NOVI ALGORITAM DOO ne upravlja direktno nijednom pokrenutom instancom ovog softvera nigdje na internetu
> niti odgovara za rad softvera u produkcionom okruženju.
Expand Down

0 comments on commit fefe6b3

Please sign in to comment.