pageextension 50114 "SalesOrderExt" extends "Sales Order"
{
actions
{
modify(Post)
{
trigger OnAfterAction()
var
HttpClient: HttpClient;
HttpResponse: HttpResponseMessage;
RequestContent: HttpContent;
ResponseText: Text;
JsonBody: Text;
EncodedXML: Text;
HttpHeaders: HttpHeaders;
Parts: List of [Text];
Part: Text;
SalesInvoiceHeader: Record "Sales Invoice Header";
SalesInvoiceLine: Record "Sales Invoice Line";
CompanyInfo: Record "Company Information";
CustomerInfo: Record "Customer";
ConnectingDevicesTax_JOD: Record "ConnectingDevicesTax_JOD";
APIResponseLog: Record "API Response Log";
Posted_InvoiceID: Text;
Posted_Document_Date: Date;
Posted_TaxNumberCompany: Text;
Posted_CompanyName: Text;
Posted_TaxNumberCustomer: Text;
Posted_CustomerName: Text;
Posted_TotalDiscountHeader: Decimal;
Posted_TotalInvoice: Decimal;
Posted_TotalBalance: Decimal;
Posted_TotalVat: Decimal;
Posted_CurrencyCode: Text;
Posted_InputNumberSequence: Text;
Posted_RowNumber: Integer;
InvoiceAreaDevelopmentCode: Text;
BARCODE_VALUE_Invoice: Text;
begin
if Rec."Document Type" = Rec."Document Type"::Order then begin
SalesInvoiceHeader.SetRange("Order No.", Rec."No.");
if SalesInvoiceHeader.FindFirst() then begin
Posted_InvoiceID := SalesInvoiceHeader."No.";
Posted_CurrencyCode := SalesInvoiceHeader."Currency Code";
Posted_Document_Date := SalesInvoiceHeader."Document Date";
CompanyInfo.FindFirst();
Posted_TaxNumberCompany := CompanyInfo."VAT Registration No.";
Posted_CompanyName := CompanyInfo.Name;
CustomerInfo.SetRange("No.", SalesInvoiceHeader."Sell-to Customer No.");
if CustomerInfo.FindFirst() then begin
Posted_TaxNumberCustomer := CustomerInfo."VAT Registration No.";
Posted_CustomerName := CustomerInfo.Name;
end;
ConnectingDevicesTax_JOD.SetRange(TypeForTax, 'GeneralSalesInvoice');
if ConnectingDevicesTax_JOD.FindFirst() then begin
Posted_InputNumberSequence := ConnectingDevicesTax_JOD.InputNumberSequence;
Posted_RowNumber := ConnectingDevicesTax_JOD.RowNumber + 1;
ConnectingDevicesTax_JOD.RowNumber := Posted_RowNumber;
ConnectingDevicesTax_JOD.Modify();
end;
CalculateInvoiceTotals(SalesInvoiceHeader, Posted_TotalDiscountHeader, Posted_TotalInvoice, Posted_TotalBalance, Posted_TotalVat);
JsonBody := CreateInvoiceXML(SalesInvoiceHeader, SalesInvoiceLine, Posted_InvoiceID, Posted_Document_Date, Posted_CurrencyCode, Posted_TaxNumberCompany, Posted_CompanyName, Posted_TaxNumberCustomer, Posted_CustomerName);
LogXMLToFile(JsonBody, 'C:\EInvoiceTest\InvoiceXML.txt');
EncodedXML := EncodeToBase64(JsonBody);
Parts := SplitString(EncodedXML, 2048);
foreach Part in Parts do begin
JsonBody := '{' +
'"invoice": "' + Part + '"' +
'}';
RequestContent.WriteFrom(JsonBody);
RequestContent.GetHeaders(HttpHeaders);
HttpHeaders.Clear();
HttpHeaders.Add('Content-Type', 'application/json');
HttpClient.DefaultRequestHeaders.Add('Client-Id', ConnectingDevicesTax_JOD.ClinetID);
HttpClient.DefaultRequestHeaders.Add('Secret-Key', ConnectingDevicesTax_JOD.SecretKey);
if not HttpClient.Post('https://backend.jofotara.gov.jo/core/invoices/', RequestContent, HttpResponse) then
Error('Failed to call the API.');
HttpResponse.Content.ReadAs(ResponseText);
BARCODE_VALUE_Invoice := ResponseText;
LogAPIResponse(APIResponseLog, ResponseText);
Message('Part Sent - Response: %1', ResponseText);
SaveBarcodeValue(Posted_InvoiceID, BARCODE_VALUE_Invoice);
end;
end;
end;
end;
}
}
procedure CalculateInvoiceTotals(SalesInvoiceHeader: Record "Sales Invoice Header"; var Posted_TotalDiscountHeader: Decimal; var Posted_TotalInvoice: Decimal; var Posted_TotalBalance: Decimal; var Posted_TotalVat: Decimal)
var
SalesInvoiceLine: Record "Sales Invoice Line";
begin
SalesInvoiceLine.SetRange("Document No.", SalesInvoiceHeader."No.");
if SalesInvoiceLine.FindSet() then begin
repeat
Posted_TotalDiscountHeader += (SalesInvoiceLine."Line Amount") - SalesInvoiceLine."Line Discount Amount";
Posted_TotalInvoice += SalesInvoiceLine.GetLineAmountInclVAT();
Posted_TotalBalance += (SalesInvoiceLine."Quantity" * SalesInvoiceLine."Unit Price");
Posted_TotalVat += SalesInvoiceLine.GetLineAmountInclVAT() - SalesInvoiceLine."Line Amount";
until SalesInvoiceLine.Next() = 0;
end;
end;
procedure CreateInvoiceXML(SalesInvoiceHeader: Record "Sales Invoice Header"; SalesInvoiceLine: Record "Sales Invoice Line"; Posted_InvoiceID: Text; Posted_Document_Date: Date; Posted_CurrencyCode: Text; Posted_TaxNumberCompany: Text; Posted_CompanyName: Text; Posted_TaxNumberCustomer: Text; Posted_CustomerName: Text): Text
var
XmlText: Text;
begin
XmlText := '<?xml version="1.0" encoding="UTF-8"?>' +
'<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"' +
' xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"' +
' xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">' +
'<cbc:ID>' + Posted_InvoiceID + '</cbc:ID>' +
'<cbc:IssueDate>' + Format(Posted_Document_Date, 0, '<Year4>-<Month,2>-<Day,2>') + '</cbc:IssueDate>' +
'<cbc:InvoiceTypeCode>388</cbc:InvoiceTypeCode>' +
'<cbc:DocumentCurrencyCode>' + Posted_CurrencyCode + '</cbc:DocumentCurrencyCode>' +
'<cbc:TaxCurrencyCode>' + Posted_CurrencyCode + '</cbc:TaxCurrencyCode>' +
'<cac:AccountingSupplierParty>' +
'<cac:Party>' +
'<cac:PartyIdentification>' +
'<cbc:ID>' + Posted_TaxNumberCompany + '</cbc:ID>' +
'</cac:PartyIdentification>' +
'<cac:PartyName>' +
'<cbc:Name>' + Posted_CompanyName + '</cbc:Name>' +
'</cac:PartyName>' +
'</cac:Party>' +
'</cac:AccountingSupplierParty>' +
'<cac:AccountingCustomerParty>' +
'<cac:Party>' +
'<cac:PartyIdentification>' +
'<cbc:ID>' + Posted_TaxNumberCustomer + '</cbc:ID>' +
'</cac:PartyIdentification>' +
'<cac:PartyName>' +
'<cbc:Name>' + Posted_CustomerName + '</cbc:Name>' +
'</cac:PartyName>' +
'</cac:Party>' +
'</cac:AccountingCustomerParty>';
// إضافة بنود الفاتورة (Line Data)
SalesInvoiceLine.SetRange("Document No.", SalesInvoiceHeader."No.");
if SalesInvoiceLine.FindSet() then begin
repeat
XmlText += '<cac:InvoiceLine>' +
'<cbc:ID>' + Format(SalesInvoiceLine."Line No.") + '</cbc:ID>' +
'<cbc:InvoicedQuantity unitCode="PCE">' + Format(SalesInvoiceLine.Quantity) + '</cbc:InvoicedQuantity>' +
'<cbc:LineExtensionAmount currencyID="' + Posted_CurrencyCode + '">' + Format(SalesInvoiceLine."Line Amount") + '</cbc:LineExtensionAmount>' +
'<cac:Item>' +
'<cbc:Name>' + SalesInvoiceLine.Description + '</cbc:Name>' +
'</cac:Item>' +
'<cac:Price>' +
'<cbc:PriceAmount currencyID="' + Posted_CurrencyCode + '">' + Format(SalesInvoiceLine."Unit Price") + '</cbc:PriceAmount>' +
'</cac:Price>' +
'</cac:InvoiceLine>';
until SalesInvoiceLine.Next() = 0;
end;
XmlText += '</Invoice>';
exit(XmlText);
end;
procedure LogAPIResponse(var APIResponseLog: Record "API Response Log"; ResponseText: Text)
begin
APIResponseLog.Init();
APIResponseLog."Response Text" := ResponseText;
APIResponseLog."Date Time" := CurrentDateTime;
APIResponseLog.Insert();
end;
procedure EncodeToBase64(InputText: Text): Text
var
Base64Convert: Codeunit "Base64 Convert";
begin
exit(Base64Convert.ToBase64(InputText));
end;
procedure SplitString(InputText: Text; MaxLength: Integer): List of [Text]
var
Parts: List of [Text];
StartIndex: Integer;
EndIndex: Integer;
begin
StartIndex := 1;
while StartIndex <= StrLen(InputText) do begin
EndIndex := StartIndex + MaxLength - 1;
if EndIndex > StrLen(InputText) then
EndIndex := StrLen(InputText);
Parts.Add(CopyStr(InputText, StartIndex, EndIndex - StartIndex + 1));
StartIndex := EndIndex + 1;
end;
exit(Parts);
end;
procedure LogXMLToFile(XMLContent: Text; FileName: Text)
var
FileMgt: Codeunit "File Management";
TempBlob: Codeunit "Temp Blob";
OutStream: OutStream;
begin
TempBlob.CreateOutStream(OutStream);
OutStream.WriteText(XMLContent);
FileMgt.BLOBExport(TempBlob, FileName, true);
end;
procedure SaveBarcodeValue(RecIID: Text[20]; BARCODE: Text[1000])
var
SaveQRCodeReceived: Record "Save_QR_Code_Received";
begin
if (RecIID = '') or (BARCODE = '') then begin
Error('Record ID or BARCODE is empty.');
end else begin
SaveQRCodeReceived.Init();
SaveQRCodeReceived."ID" := SaveQRCodeReceived.Count() + 1;
SaveQRCodeReceived.Invoice_Type := 'Invoice';
SaveQRCodeReceived."Invoice_NO" := RecIID;
SaveQRCodeReceived.QR_Code_JO_Insert_Date := Today;
SaveQRCodeReceived."QR_Code_JO" := BARCODE;
SaveQRCodeReceived.Insert();
end;
Commit();
end;
}