Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ codeunit 13916 "Export XRechnung Document"
EDocumentService: Record "E-Document Service";
FeatureTelemetry: Codeunit "Feature Telemetry";
PEPPOLMgt: Codeunit "PEPPOL Management";
TypeHelper: Codeunit "Type Helper";
FeatureNameTok: Label 'E-document XRechnung Format', Locked = true;
StartEventNameTok: Label 'E-document XRechnung export started', Locked = true;
EndEventNameTok: Label 'E-document XRechnung export completed', Locked = true;
XmlNamespaceCBC: Text;
XmlNamespaceCAC: Text;
AlwaysIncludeTwoDecimalPlacesForAmountFields: Boolean;

trigger OnRun();
begin
Expand All @@ -70,6 +72,7 @@ codeunit 13916 "Export XRechnung Document"
RecordRef.SetTable(SalesInvoiceHeader);

FindEDocumentService(RecordExportBuffer."Electronic Document Format");
CheckShouldIncludeTwoDecimalPlacesForAmountFields();
RecordExportBuffer."File Content".CreateOutStream(FileOutStream, TextEncoding::UTF8);
CreateXML(SalesInvoiceHeader, FileOutStream);
RecordExportBuffer.Modify();
Expand All @@ -87,6 +90,7 @@ codeunit 13916 "Export XRechnung Document"
RecordRef.SetTable(SalesCrMemoHeader);

FindEDocumentService(RecordExportBuffer."Electronic Document Format");
CheckShouldIncludeTwoDecimalPlacesForAmountFields();
RecordExportBuffer."File Content".CreateOutStream(FileOutStream, TextEncoding::UTF8);
CreateXML(SalesCrMemoHeader, FileOutStream);
RecordExportBuffer.Modify();
Expand All @@ -104,6 +108,7 @@ codeunit 13916 "Export XRechnung Document"
RecordRef.SetTable(ServiceInvoiceHeader);

FindEDocumentService(RecordExportBuffer."Electronic Document Format");
CheckShouldIncludeTwoDecimalPlacesForAmountFields();
RecordExportBuffer."File Content".CreateOutStream(FileOutStream, TextEncoding::UTF8);
CreateXML(ServiceInvoiceHeader, FileOutStream);
RecordExportBuffer.Modify();
Expand All @@ -121,6 +126,7 @@ codeunit 13916 "Export XRechnung Document"
RecordRef.SetTable(ServiceCrMemoHeader);

FindEDocumentService(RecordExportBuffer."Electronic Document Format");
CheckShouldIncludeTwoDecimalPlacesForAmountFields();
RecordExportBuffer."File Content".CreateOutStream(FileOutStream, TextEncoding::UTF8);
CreateXML(ServiceCrMemoHeader, FileOutStream);
RecordExportBuffer.Modify();
Expand Down Expand Up @@ -856,8 +862,8 @@ codeunit 13916 "Export XRechnung Document"
AllowanceChargeElement.Add(XmlElement.Create('ChargeIndicator', XmlNamespaceCBC, 'false'));
AllowanceChargeElement.Add(XmlElement.Create('AllowanceChargeReason', XmlNamespaceCBC, AllowanceChargeReason));
AllowanceChargeElement.Add(XmlElement.Create('MultiplierFactorNumeric', XmlNamespaceCBC, FormatFiveDecimal(MultiplierFactorNumeric)));
AllowanceChargeElement.Add(XmlElement.Create('Amount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(Amount)));
AllowanceChargeElement.Add(XmlElement.Create('BaseAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(BaseAmount)));
AllowanceChargeElement.Add(XmlElement.Create('Amount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(Amount, AlwaysIncludeTwoDecimalPlacesForAmountFields)));
AllowanceChargeElement.Add(XmlElement.Create('BaseAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(BaseAmount, AlwaysIncludeTwoDecimalPlacesForAmountFields)));
if InsertTaxCat then
InsertTaxCategory(AllowanceChargeElement, 'TaxCategory', TaxCategory, Percent);
RootXMLNode.Add(AllowanceChargeElement);
Expand All @@ -868,8 +874,8 @@ codeunit 13916 "Export XRechnung Document"
TaxSubtotalElement: XmlElement;
begin
TaxSubtotalElement := XmlElement.Create('TaxSubtotal', XmlNamespaceCAC);
TaxSubtotalElement.Add(XmlElement.Create('TaxableAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(TaxableAmount)));
TaxSubtotalElement.Add(XmlElement.Create('TaxAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(TaxAmount)));
TaxSubtotalElement.Add(XmlElement.Create('TaxableAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(TaxableAmount, AlwaysIncludeTwoDecimalPlacesForAmountFields)));
TaxSubtotalElement.Add(XmlElement.Create('TaxAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(TaxAmount, AlwaysIncludeTwoDecimalPlacesForAmountFields)));
InsertTaxCategory(TaxSubtotalElement, 'TaxCategory', TaxCategory, VATPercentage);
RootElement.Add(TaxSubtotalElement);
end;
Expand All @@ -890,7 +896,7 @@ codeunit 13916 "Export XRechnung Document"
TaxTotalElement: XmlElement;
begin
TaxTotalElement := XmlElement.Create('TaxTotal', XmlNamespaceCAC);
TaxTotalElement.Add(XmlElement.Create('TaxAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(GetTotalTaxAmount(SalesInvLine))));
TaxTotalElement.Add(XmlElement.Create('TaxAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(GetTotalTaxAmount(SalesInvLine), AlwaysIncludeTwoDecimalPlacesForAmountFields)));

if SalesInvLine.FindSet() then
repeat
Expand All @@ -916,7 +922,7 @@ codeunit 13916 "Export XRechnung Document"
TaxTotalElement: XmlElement;
begin
TaxTotalElement := XmlElement.Create('TaxTotal', XmlNamespaceCAC);
TaxTotalElement.Add(XmlElement.Create('TaxAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(GetTotalTaxAmount(SalesCrMemoLine))));
TaxTotalElement.Add(XmlElement.Create('TaxAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(GetTotalTaxAmount(SalesCrMemoLine), AlwaysIncludeTwoDecimalPlacesForAmountFields)));

if SalesCrMemoLine.FindSet() then
repeat
Expand All @@ -942,12 +948,12 @@ codeunit 13916 "Export XRechnung Document"
LegalMonetaryTotalElement: XmlElement;
begin
LegalMonetaryTotalElement := XmlElement.Create('LegalMonetaryTotal', XmlNamespaceCAC);
LegalMonetaryTotalElement.Add(XmlElement.Create('LineExtensionAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesInvLine.FieldName(Amount)) + LineAmounts.Get(SalesInvLine.FieldName("Inv. Discount Amount")))));
LegalMonetaryTotalElement.Add(XmlElement.Create('TaxExclusiveAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesInvLine.FieldName(Amount)))));
LegalMonetaryTotalElement.Add(XmlElement.Create('TaxInclusiveAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesInvLine.FieldName("Amount Including VAT")))));
LegalMonetaryTotalElement.Add(XmlElement.Create('LineExtensionAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesInvLine.FieldName(Amount)) + LineAmounts.Get(SalesInvLine.FieldName("Inv. Discount Amount")), AlwaysIncludeTwoDecimalPlacesForAmountFields)));
LegalMonetaryTotalElement.Add(XmlElement.Create('TaxExclusiveAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesInvLine.FieldName(Amount)), AlwaysIncludeTwoDecimalPlacesForAmountFields)));
LegalMonetaryTotalElement.Add(XmlElement.Create('TaxInclusiveAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesInvLine.FieldName("Amount Including VAT")), AlwaysIncludeTwoDecimalPlacesForAmountFields)));
if LineAmounts.Get(SalesInvLine.FieldName("Inv. Discount Amount")) > 0 then
LegalMonetaryTotalElement.Add(XmlElement.Create('AllowanceTotalAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesInvLine.FieldName("Inv. Discount Amount")))));
LegalMonetaryTotalElement.Add(XmlElement.Create('PayableAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesInvLine.FieldName("Amount Including VAT")))));
LegalMonetaryTotalElement.Add(XmlElement.Create('AllowanceTotalAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesInvLine.FieldName("Inv. Discount Amount")), AlwaysIncludeTwoDecimalPlacesForAmountFields)));
LegalMonetaryTotalElement.Add(XmlElement.Create('PayableAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesInvLine.FieldName("Amount Including VAT")), AlwaysIncludeTwoDecimalPlacesForAmountFields)));
RootXMLNode.Add(LegalMonetaryTotalElement);
end;

Expand All @@ -956,12 +962,12 @@ codeunit 13916 "Export XRechnung Document"
LegalMonetaryTotalElement: XmlElement;
begin
LegalMonetaryTotalElement := XmlElement.Create('LegalMonetaryTotal', XmlNamespaceCAC);
LegalMonetaryTotalElement.Add(XmlElement.Create('LineExtensionAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesCrMemoLine.FieldName(Amount)) + LineAmounts.Get(SalesCrMemoLine.FieldName("Inv. Discount Amount")))));
LegalMonetaryTotalElement.Add(XmlElement.Create('TaxExclusiveAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesCrMemoLine.FieldName(Amount)))));
LegalMonetaryTotalElement.Add(XmlElement.Create('TaxInclusiveAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesCrMemoLine.FieldName("Amount Including VAT")))));
LegalMonetaryTotalElement.Add(XmlElement.Create('LineExtensionAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesCrMemoLine.FieldName(Amount)) + LineAmounts.Get(SalesCrMemoLine.FieldName("Inv. Discount Amount")), AlwaysIncludeTwoDecimalPlacesForAmountFields)));
LegalMonetaryTotalElement.Add(XmlElement.Create('TaxExclusiveAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesCrMemoLine.FieldName(Amount)), AlwaysIncludeTwoDecimalPlacesForAmountFields)));
LegalMonetaryTotalElement.Add(XmlElement.Create('TaxInclusiveAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesCrMemoLine.FieldName("Amount Including VAT")), AlwaysIncludeTwoDecimalPlacesForAmountFields)));
if LineAmounts.Get(SalesCrMemoLine.FieldName("Inv. Discount Amount")) > 0 then
LegalMonetaryTotalElement.Add(XmlElement.Create('AllowanceTotalAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesCrMemoLine.FieldName("Inv. Discount Amount")))));
LegalMonetaryTotalElement.Add(XmlElement.Create('PayableAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesCrMemoLine.FieldName("Amount Including VAT")))));
LegalMonetaryTotalElement.Add(XmlElement.Create('AllowanceTotalAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesCrMemoLine.FieldName("Inv. Discount Amount")), AlwaysIncludeTwoDecimalPlacesForAmountFields)));
LegalMonetaryTotalElement.Add(XmlElement.Create('PayableAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(LineAmounts.Get(SalesCrMemoLine.FieldName("Amount Including VAT")), AlwaysIncludeTwoDecimalPlacesForAmountFields)));
RootXMLNode.Add(LegalMonetaryTotalElement);
end;

Expand Down Expand Up @@ -1028,7 +1034,7 @@ codeunit 13916 "Export XRechnung Document"
ExcludeVAT(SalesInvLine, Currency."Amount Rounding Precision");
InvoiceLineElement.Add(XmlElement.Create('ID', XmlNamespaceCBC, Format(SalesInvLine."Line No.")));
InvoiceLineElement.Add(XmlElement.Create('InvoicedQuantity', XmlNamespaceCBC, XmlAttribute.Create('unitCode', GetUoMCode(SalesInvLine."Unit of Measure Code")), FormatDecimalUnlimited(SalesInvLine.Quantity)));
InvoiceLineElement.Add(XmlElement.Create('LineExtensionAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(SalesInvLine.Amount + SalesInvLine."Inv. Discount Amount")));
InvoiceLineElement.Add(XmlElement.Create('LineExtensionAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(SalesInvLine.Amount + SalesInvLine."Inv. Discount Amount", AlwaysIncludeTwoDecimalPlacesForAmountFields)));
if SalesInvLine."Shipment Date" <> 0D then
InsertInvoicePeriod(InvoiceLineElement, SalesInvLine."Shipment Date", SalesInvLine."Shipment Date");
InsertOrderLineReference(InvoiceLineElement, SalesInvLine."Line No.");
Expand Down Expand Up @@ -1067,7 +1073,7 @@ codeunit 13916 "Export XRechnung Document"
ExcludeVAT(SalesCrMemoLine, Currency."Amount Rounding Precision");
CrMemoLineElement.Add(XmlElement.Create('ID', XmlNamespaceCBC, Format(SalesCrMemoLine."Line No.")));
CrMemoLineElement.Add(XmlElement.Create('CreditedQuantity', XmlNamespaceCBC, XmlAttribute.Create('unitCode', GetUoMCode(SalesCrMemoLine."Unit of Measure Code")), FormatDecimalUnlimited(SalesCrMemoLine.Quantity)));
CrMemoLineElement.Add(XmlElement.Create('LineExtensionAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(SalesCrMemoLine.Amount + SalesCrMemoLine."Inv. Discount Amount")));
CrMemoLineElement.Add(XmlElement.Create('LineExtensionAmount', XmlNamespaceCBC, XmlAttribute.Create('currencyID', CurrencyCode), FormatDecimal(SalesCrMemoLine.Amount + SalesCrMemoLine."Inv. Discount Amount", AlwaysIncludeTwoDecimalPlacesForAmountFields)));
InsertOrderLineReference(CrMemoLineElement, SalesCrMemoLine."Line No.");
if SalesCrMemoLine."Shipment Date" <> 0D then
InsertInvoicePeriod(CrMemoLineElement, SalesCrMemoLine."Shipment Date", SalesCrMemoLine."Shipment Date");
Expand Down Expand Up @@ -1317,6 +1323,13 @@ codeunit 13916 "Export XRechnung Document"
if EDocumentService.FindLast() then;
OnAfterFindEDocumentService(EDocumentService, EDocumentFormat);
end;

local procedure CheckShouldIncludeTwoDecimalPlacesForAmountFields()
begin
AlwaysIncludeTwoDecimalPlacesForAmountFields := false;
OnCheckShouldIncludeTwoDecimalPlacesForAmountFields(AlwaysIncludeTwoDecimalPlacesForAmountFields);
end;

#region CommonFunctions
procedure FormatDate(VarDate: Date): Text[20];
begin
Expand All @@ -1327,7 +1340,18 @@ codeunit 13916 "Export XRechnung Document"

procedure FormatDecimal(VarDecimal: Decimal): Text[30];
begin
exit(Format(Round(VarDecimal, 0.01), 0, 9));
exit(FormatDecimal(VarDecimal, false));
end;

procedure FormatDecimal(VarDecimal: Decimal; IncludeDecimalPlaces: Boolean): Text[30];
var
DecimalRounded: Decimal;
begin
DecimalRounded := Round(VarDecimal, 0.01);
if IncludeDecimalPlaces then
exit(Format(DecimalRounded, 0, TypeHelper.GetXMLAmountFormatWithTwoDecimalPlaces()))
else
exit(Format(DecimalRounded, 0, 9));
end;

procedure FormatDecimalUnlimited(VarDecimal: Decimal): Text
Expand Down Expand Up @@ -1565,4 +1589,13 @@ codeunit 13916 "Export XRechnung Document"
local procedure OnInsertAttachmentOnAfterSetFilters(TableNo: Integer; DocumentNo: Code[20]; var DocumentAttachment: Record "Document Attachment")
begin
end;

/// <summary>
/// Use this event to always include two decimal places for amount fields
/// </summary>
/// <param name="AlwaysIncludeTwoDecimalPlacesForAmountFields">Set to true to force all amount fields to include two decimal places (e.g. 1.10 instead of 1.1)</param>
[IntegrationEvent(false, false)]
local procedure OnCheckShouldIncludeTwoDecimalPlacesForAmountFields(var AlwaysIncludeTwoDecimalPlacesForAmountFields: Boolean)
begin
end;
}
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,36 @@ codeunit 13918 "XRechnung XML Document Tests"
end;
#endregion

#region TwoDecimalPlaces
[Test]
procedure FormatDecimalWithTwoDecimalPlacesFlagReturnsTrailingZero();
begin
// [SCENARIO] FormatDecimal with IncludeDecimalPlaces = true always formats amount with exactly two decimal places
Initialize();

// [WHEN/THEN] A value with one significant decimal place gets the trailing zero
Assert.AreEqual('1.10', ExportXRechnungDocument.FormatDecimal(1.1, true), 'FormatDecimal(1.1, true) should return ''1.10''');
// [WHEN/THEN] A whole number gets two decimal zeros
Assert.AreEqual('1.00', ExportXRechnungDocument.FormatDecimal(1, true), 'FormatDecimal(1, true) should return ''1.00''');
// [WHEN/THEN] A value with two decimal places is unchanged
Assert.AreEqual('1.23', ExportXRechnungDocument.FormatDecimal(1.23, true), 'FormatDecimal(1.23, true) should return ''1.23''');
end;

[Test]
procedure FormatDecimalWithoutTwoDecimalPlacesFlagNoTrailingZero();
begin
// [SCENARIO] FormatDecimal with IncludeDecimalPlaces = false uses the default format without trailing zeros
Initialize();

// [WHEN/THEN] A value with one significant decimal place has no trailing zero
Assert.AreEqual('1.1', ExportXRechnungDocument.FormatDecimal(1.1, false), 'FormatDecimal(1.1, false) should return ''1.1''');
// [WHEN/THEN] A whole number has no decimal places
Assert.AreEqual('1', ExportXRechnungDocument.FormatDecimal(1, false), 'FormatDecimal(1, false) should return ''1''');
// [WHEN/THEN] A value with two decimal places is unchanged
Assert.AreEqual('1.23', ExportXRechnungDocument.FormatDecimal(1.23, false), 'FormatDecimal(1.23, false) should return ''1.23''');
end;
#endregion

local procedure CreateAndPostSalesDocument(DocumentType: Enum "Sales Document Type"; LineType: Enum "Sales Line Type"; InvoiceDiscount: Boolean): Code[20];
var
SalesHeader: Record "Sales Header";
Expand Down Expand Up @@ -2632,9 +2662,9 @@ codeunit 13918 "XRechnung XML Document Tests"
CompanyInformation."SWIFT Code" := LibraryUtility.GenerateGUID();
CompanyInformation."E-Mail" := LibraryUtility.GenerateRandomEmail();
CompanyInformation.Modify();

GeneralLedgerSetup.Get();

EDocumentService.DeleteAll();
EDocumentService.Get(LibraryEdocument.CreateService("E-Document Format"::XRechnung, "Service Integration"::"No Integration"));
Commit();
Expand Down
Loading