public CwTransportRegistration(TMJournalLineClientLocal tmJournalLine, CrudAPI _crudApi, double mileageTotal, bool returning, bool _addMileage = false)
 {
     crudApi     = _crudApi;
     journalline = tmJournalLine;
     Returning   = returning;
     intializeProperties();
     this.DataContext = this;
     InitializeComponent();
     lePayType.api = leProject.api = crudApi;
     this.Title    = string.Format("{0} {1}", Uniconta.ClientTools.Localization.lookup("Mileage"), Uniconta.ClientTools.Localization.lookup("ProjectRegistration"));
     employee      = tmJournalLine.EmployeeRef;
     company       = crudApi.CompanyEntity;
     if (journalline.RowId != 0 && journalline._RegistrationType == RegistrationType.Mileage)
     {
         chkReturning.IsEnabled = false;
     }
     else
     {
         debtor = tmJournalLine.ProjectRef?.Debtor;
         txtProjectName.Text = tmJournalLine.ProjectRef?._Name;
         installation        = tmJournalLine.ProjectRef?.InstallationRef;
     }
     addMileage        = _addMileage;
     this.mileageTotal = mileageTotal;
     LoadControls();
     this.DataContext = this;
 }
        public CwTransportRegistration(TMJournalLineClientLocal tmJournalLine, CrudAPI _crudApi)
        {
            crudApi     = _crudApi;
            journalline = tmJournalLine;
            Returning   = true;
            company     = crudApi.CompanyEntity;
            employee    = tmJournalLine.EmployeeRef;
            intializeProperties();
            this.DataContext = this;
            InitializeComponent();
            lePayType.api = leProject.api = leProjectTask.api = leWorkSpace.api = crudApi;
            this.Title    = string.Format("{0} {1}", Uniconta.ClientTools.Localization.lookup("Mileage"), Uniconta.ClientTools.Localization.lookup("ProjectRegistration"));

            payrollCache = crudApi.GetCache(typeof(Uniconta.DataModel.EmpPayrollCategory));
            projectCache = crudApi.GetCache(typeof(Uniconta.DataModel.Project));

            debtor = tmJournalLine.ProjectRef?.Debtor;
            txtProjectName.Text = tmJournalLine.ProjectRef?._Name;
            installation        = tmJournalLine.ProjectRef?.InstallationRef;

            if (!company.ProjectTask)
            {
                lblProjectTask.Visibility = Visibility.Collapsed;
                leProjectTask.Visibility  = Visibility.Collapsed;
            }

            LoadControls();

            lePayType.cacheFilter = new MileagePayrollFilter(payrollCache);
            SetProjectTask();
        }
        private void leProject_EditValueChanged(object sender, DevExpress.Xpf.Editors.EditValueChangedEventArgs e)
        {
            string id       = Convert.ToString(e.NewValue);
            var    projects = crudApi.CompanyEntity.GetCache(typeof(Uniconta.DataModel.Project));
            var    project  = (Uniconta.DataModel.Project)projects?.Get(id) as ProjectClient;

            if (project != null)
            {
                txtProjectName.Text = project._Name;
                debtor       = project.Debtor;
                installation = project.InstallationRef;
                if (journalline.RowId != 0 && journalline._RegistrationType == RegistrationType.Mileage)
                {
                    addEmptyAddress = false;
                }
                LoadControls();
                if (!txtMonHrs.IsReadOnly)
                {
                    txtMonHrs.Text = "0";
                    Day1           = 0d;
                }
                if (!txtTueHrs.IsReadOnly)
                {
                    txtTueHrs.Text = "0";
                    Day2           = 0d;
                }
                if (!txtWedHrs.IsReadOnly)
                {
                    txtWedHrs.Text = "0";
                    Day3           = 0d;
                }
                if (!txtThuHrs.IsReadOnly)
                {
                    txtThuHrs.Text = "0";
                    Day4           = 0d;
                }
                if (!txtFriHrs.IsReadOnly)
                {
                    txtFriHrs.Text = "0";
                    Day5           = 0d;
                }
                if (!txtSatHrs.IsReadOnly)
                {
                    txtSatHrs.Text = "0";
                    Day6           = 0d;
                }
                if (!txtSunHrs.IsReadOnly)
                {
                    txtSunHrs.Text = "0";
                    Day7           = 0d;
                }
                CalculateTotal();
                CalulateMileage();
            }
        }
        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"));
            }
        }