static public async void GenerateOIOXml(CrudAPI api, InvoicePostingResult res)
        {
            var Comp     = api.CompanyEntity;
            var InvCache = api.GetCache(typeof(Uniconta.DataModel.InvItem)) ?? await api.LoadCache(typeof(Uniconta.DataModel.InvItem));

            var VatCache = api.GetCache(typeof(Uniconta.DataModel.GLVat)) ?? await api.LoadCache(typeof(Uniconta.DataModel.GLVat));

            //SystemInfo.Visible = true;

            int            countErr   = 0;
            SaveFileDialog saveDialog = null;
            InvoiceAPI     Invapi     = new InvoiceAPI(api);

            var invClient = (DebtorInvoiceClient)res.Header;

            var Debcache = Comp.GetCache(typeof(Debtor)) ?? await api.LoadCache(typeof(Debtor));

            var debtor = (Debtor)Debcache.Get(invClient._DCAccount);

            var layoutGroupCache = api.GetCache(typeof(DebtorLayoutGroup)) ?? await api.LoadCache(typeof(DebtorLayoutGroup));

            var invoiceLines = (InvTransClient[])res.Lines;

            Contact contactPerson = null;

            if (invClient._ContactRef != 0)
            {
                var Contacts = api.GetCache(typeof(Uniconta.DataModel.Contact)) ?? await api.LoadCache(typeof(Uniconta.DataModel.Contact));

                foreach (var contact in (Uniconta.DataModel.Contact[])Contacts.GetRecords)
                {
                    if (contact.RowId == invClient._ContactRef)
                    {
                        contactPerson = contact;
                        break;
                    }
                }
            }

            DebtorOrders.SetDeliveryAdress(invClient, debtor, api);

            Debtor deliveryAccount;

            if (invClient._DeliveryAccount != null)
            {
                deliveryAccount = (Debtor)Debcache.Get(invClient._DeliveryAccount);
            }
            else
            {
                deliveryAccount = null;
            }

            WorkInstallation workInstallation = null;

            if (invClient._Installation != null)
            {
                var workInstallCache = api.GetCache(typeof(Uniconta.DataModel.WorkInstallation)) ?? await api.LoadCache(typeof(Uniconta.DataModel.WorkInstallation));

                workInstallation = (WorkInstallation)workInstallCache.Get(invClient._Installation);
            }

            CreationResult result;

            if (Comp._CountryId == CountryCode.Norway || Comp._CountryId == CountryCode.Netherlands)
            {
                result = EHF.GenerateEHFXML(Comp, debtor, deliveryAccount, invClient, invoiceLines, InvCache, VatCache, null, contactPerson);
            }
            else if (Comp._CountryId == CountryCode.Iceland)
            {
                var paymFormatCache = Comp.GetCache(typeof(DebtorPaymentFormatClientIceland)) ?? await api.LoadCache(typeof(DebtorPaymentFormatClientIceland));

                TableAddOnData[] attachments = await UBL.Iceland.Attachments.CollectInvoiceAttachments(invClient, api);

                result = TS136137.GenerateTS136137XML(Comp, debtor, deliveryAccount, invClient, invoiceLines, InvCache, VatCache, null, contactPerson, paymFormatCache, attachments);
            }
            else
            {
                var attachments = await FromXSDFile.OIOUBL.ExportImport.Attachments.CollectInvoiceAttachments(invClient, api);

                result = Uniconta.API.DebtorCreditor.OIOUBL.GenerateOioXML(Comp, debtor, deliveryAccount, invClient, invoiceLines, InvCache, VatCache, null, contactPerson, attachments, layoutGroupCache, workInstallation);
            }

            bool createXmlFile = true;

            var errorInfo = "";

            if (result.HasErrors)
            {
                countErr++;
                createXmlFile = false;
                foreach (FromXSDFile.OIOUBL.ExportImport.PrecheckError error in result.PrecheckErrors)
                {
                    errorInfo += error.ToString() + "\n";
                }
            }

            var  applFilePath  = string.Empty;
            bool hasUserFolder = false;

            if (result.Document != null && createXmlFile)
            {
                var filename = string.Format("{0}_{1}", Uniconta.ClientTools.Localization.lookup("Invoice"), invClient.InvoiceNumber);

                if (session.User._AppDocPath != string.Empty && Directory.Exists(session.User._AppDocPath))
                {
                    try
                    {
                        applFilePath = string.Format("{0}\\OIOUBL", session.User._AppDocPath);
                        Directory.CreateDirectory(applFilePath);

                        filename      = string.Format("{0}\\{1}.xml", applFilePath, filename);
                        hasUserFolder = true;
                    }
                    catch (Exception ex)
                    {
                        UnicontaMessageBox.Show(ex);
                    }
                }
                else
                {
                    saveDialog          = UtilDisplay.LoadSaveFileDialog;
                    saveDialog.FileName = filename;
                    saveDialog.Filter   = "XML-File | *.xml";
                    bool?dialogResult = saveDialog.ShowDialog();
                    if (dialogResult != true)
                    {
                        return;
                    }

                    filename = saveDialog.FileName;
                }

                result.Document.Save(filename);
                await Invapi.MarkSendInvoiceOIO(invClient);

                if (hasUserFolder)
                {
                    UnicontaMessageBox.Show(string.Format(Uniconta.ClientTools.Localization.lookup("SaveFileMsgOBJ"), 1, Uniconta.ClientTools.Localization.lookup("Invoice"), applFilePath)
                                            , Uniconta.ClientTools.Localization.lookup("Information"));
                }
            }

            if (countErr != 0 && !string.IsNullOrWhiteSpace(errorInfo))
            {
                UnicontaMessageBox.Show(errorInfo, Uniconta.ClientTools.Localization.lookup("Error"));
            }
        }
        public static async void GenerateOIOXml(CrudAPI api, DCInvoice inv)
        {
            var Comp     = api.CompanyEntity;
            var InvCache = api.GetCache(typeof(Uniconta.DataModel.InvItem)) ?? await api.LoadCache(typeof(Uniconta.DataModel.InvItem));

            var VatCache = api.GetCache(typeof(Uniconta.DataModel.GLVat)) ?? await api.LoadCache(typeof(Uniconta.DataModel.GLVat));

            //SystemInfo.Visible = true;

            int            countErr   = 0;
            SaveFileDialog saveDialog = null;

            Uniconta.API.DebtorCreditor.InvoiceAPI Invapi = new Uniconta.API.DebtorCreditor.InvoiceAPI(api);

            var listPropval = new List <PropValuePair>()
            {
                PropValuePair.GenereteWhereElements("InvoiceNumber", inv._InvoiceNumber, CompareOperator.Equal),
                PropValuePair.GenereteWhereElements("DCAccount", inv._DCAccount, CompareOperator.Equal),
                PropValuePair.GenereteWhereElements("Date", inv._Date, CompareOperator.Equal)
            };

            var FindDebInvLocal = await api.Query <DebtorInvoiceClient>(listPropval);

            var invClient = FindDebInvLocal.FirstOrDefault();

            if (invClient == null)
            {
                return;
            }

            var Debcache = Comp.GetCache(typeof(Debtor)) ?? await api.LoadCache(typeof(Debtor));

            var debtor = (Debtor)Debcache.Get(invClient._DCAccount);

            if (debtor == null || !debtor._InvoiceInXML || invClient.SendTimeOIO != DateTime.MinValue)
            {
                if (!debtor._InvoiceInXML)
                {
                    UnicontaMessageBox.Show("Faktura i OIOUBL er ikke sat til denne debitor", Uniconta.ClientTools.Localization.lookup("Warning"));
                }
                return;
            }

            var InvTransInvoiceLines = (DebtorInvoiceLines[])await Invapi.GetInvoiceLines(invClient, new DebtorInvoiceLines()); //TODO:Test fakturering

            var layoutGroupCache = api.GetCache(typeof(DebtorLayoutGroup)) ?? await api.LoadCache(typeof(DebtorLayoutGroup));

            Contact contactPerson = null;

            if (invClient._ContactRef != 0)
            {
                var Contacts = api.GetCache(typeof(Uniconta.DataModel.Contact)) ?? await api.LoadCache(typeof(Uniconta.DataModel.Contact));

                foreach (var contact in (Uniconta.DataModel.Contact[])Contacts.GetRecords)
                {
                    if (contact.RowId == invClient._ContactRef)
                    {
                        contactPerson = contact;
                        break;
                    }
                }
            }

            DebtorOrders.SetDeliveryAdress(invClient, debtor, api);

            Debtor deliveryAccount;

            if (invClient._DeliveryAccount != null)
            {
                deliveryAccount = (Debtor)Debcache.Get(invClient._DeliveryAccount);
            }
            else
            {
                deliveryAccount = null;
            }

            WorkInstallation workInstallation = null;

            if (invClient._Installation != null)
            {
                var workInstallCache = api.GetCache(typeof(Uniconta.DataModel.WorkInstallation)) ?? await api.LoadCache(typeof(Uniconta.DataModel.WorkInstallation));

                workInstallation = (WorkInstallation)workInstallCache.Get(invClient._Installation);
            }

            CreationResult result;

            if (Comp._CountryId == CountryCode.Norway || Comp._CountryId == CountryCode.Netherlands)
            {
                result = EHF.GenerateEHFXML(Comp, debtor, deliveryAccount, invClient, InvTransInvoiceLines, InvCache, VatCache, null, contactPerson);
            }
            else
            {
                var attachments = await FromXSDFile.OIOUBL.ExportImport.Attachments.CollectInvoiceAttachments(invClient, api);

                result = Uniconta.API.DebtorCreditor.OIOUBL.GenerateOioXML(Comp, debtor, deliveryAccount, invClient, InvTransInvoiceLines, InvCache, VatCache, null, contactPerson, attachments, layoutGroupCache, workInstallation);
            }

            bool createXmlFile = true;

            var errorInfo = "";

            if (result.HasErrors)
            {
                countErr++;
                createXmlFile = false;

                foreach (FromXSDFile.OIOUBL.ExportImport.PrecheckError error in result.PrecheckErrors)
                {
                    errorInfo += error.ToString() + "\n";
                }
            }

            if (result.Document != null && createXmlFile)
            {
                string invoice = Uniconta.ClientTools.Localization.lookup("Invoice");
                saveDialog          = Uniconta.ClientTools.Util.UtilDisplay.LoadSaveFileDialog;
                saveDialog.FileName = string.Format("{0}_{1}", invoice, invClient.InvoiceNumber);
                saveDialog.Filter   = "XML-File | *.xml";
                bool?dialogResult = saveDialog.ShowDialog();
                if (dialogResult != true)
                {
                    return;
                }

                var filename = saveDialog.FileName;

                result.Document.Save(filename);
                await Invapi.MarkSendInvoiceOIO(invClient);

                invClient.SendTimeOIO = BasePage.GetSystemDefaultDate();
            }

            //invClient.NotifySystemInfoSet();

            //if (countErr != 0)
            //    UnicontaMessageBox.Show(Uniconta.ClientTools.Localization.lookup(String.Format("Couldn't create file for {0} invoice(s). Please check System info column.", countErr)));

            if (countErr != 0 && !string.IsNullOrWhiteSpace(errorInfo))
            {
                UnicontaMessageBox.Show(errorInfo, Uniconta.ClientTools.Localization.lookup("Error"));
            }
        }