private async void LoadVatReport()
        {
            DateTime FromDate, ToDate;

            if (txtDateFrm.Text == string.Empty)
            {
                FromDate = DateTime.Today;
                FromDate = FromDate.AddDays(1 - FromDate.Day); // first day in current month
            }
            else
            {
                FromDate = localFromDate = txtDateFrm.DateTime.Date;
            }
            if (txtDateTo.Text == string.Empty)
            {
                ToDate = DateTime.Today.AddMonths(1);
                ToDate = ToDate.AddDays(-ToDate.Day); // last day in current month
            }
            else
            {
                ToDate = localToDate = txtDateTo.DateTime.Date;
            }

            busyIndicator.IsBusy = true;

            var    qapi    = api;
            var    country = qapi.CompanyEntity._CountryId;
            var    rapi    = new ReportAPI(qapi);
            string journal = cmbJournal.Text;
            var    vatTask = rapi.VatCodeSum(FromDate, ToDate, journal);

            SQLCache accounts = qapi.GetCache(typeof(Uniconta.DataModel.GLAccount)) ?? await qapi.LoadCache(typeof(Uniconta.DataModel.GLAccount));

            SQLCache vats = qapi.GetCache(typeof(Uniconta.DataModel.GLVat)) ?? await qapi.LoadCache(typeof(Uniconta.DataModel.GLVat));

            SQLCache vattypes = qapi.GetCache(typeof(Uniconta.DataModel.GLVatType)) ?? await qapi.LoadCache(typeof(Uniconta.DataModel.GLVatType));

            FinancialBalance[] sumVat = await vatTask;
            if (sumVat == null || accounts == null || vats == null || vattypes == null)
            {
                busyIndicator.IsBusy = false;
                UtilDisplay.ShowErrorCode(rapi.LastError);
                return;
            }

            Array.Sort(sumVat, new AccountSorter());

            VatReportLine v;
            var           lst = new List <VatReportLine>(sumVat.Length);

            int  decm       = 2;
            bool RoundTo100 = false;

            if (!qapi.CompanyEntity.HasDecimals)
            {
                CalculatedVAT.HasDecimals = PostedVAT.HasDecimals = WithoutVAT.HasDecimals = Accumulated.HasDecimals = false;
                RoundTo100 = true;
                decm       = 0;
            }

            var countrySelected = (CountryCode)Math.Max(0, vatCountry.SelectedIndex);

            var       AccsFound = new HashSet <int>();
            GLAccount Account;
            GLVat     Vat;
            int       i;
            bool      HasOffsetType0 = false, HasOffsetType1 = false;
            int       PrevAccount = 0;

            for (i = 0; (i < sumVat.Length); i++)
            {
                var sum = sumVat[i];
                Account = (GLAccount)accounts.Get(sum.AccountRowId);
                if (Account == null) // || Account._SystemAccount == (byte)SystemAccountTypes.SalesTaxOffset)
                {
                    continue;
                }
                //if (sum._Debit == 0 && sum._Credit == 0)
                //    continue;

                Vat = (GLVat)vats.Get(sum.VatRowId);
                if (Vat == null)
                {
                    continue;
                }

                var acStr = Account._Account;
                v = new VatReportLine();
                v.AmountWithVat = sum.Debit;
                v._PostedVAT    = sum.Credit;
                v._BaseVAT      = sum.AmountBase;

                v.AccountNumber = acStr;
                v.Account       = Account;
                v.AccountIsVat  = Account._SystemAccount == (byte)SystemAccountTypes.SalesTaxPayable || Account._SystemAccount == (byte)SystemAccountTypes.SalesTaxReceiveable ? (byte)1 : (byte)0;

                if (Vat._Account == acStr || Vat._OffsetAccount == acStr)
                {
                    if (Vat._Account == acStr)
                    {
                        v.AccountIsVat    = 1;
                        v.IsOffsetAccount = 0;
                    }
                    else
                    {
                        v.AccountIsVat    = 1;
                        v.IsOffsetAccount = 1;
                        if (Vat._VatType == 0)
                        {
                            HasOffsetType0 = true;
                        }
                        else
                        {
                            HasOffsetType1 = true;
                        }
                    }
                }
                else
                {
                    foreach (var va in (IEnumerable <GLVat>)vats.GetNotNullArray)
                    {
                        if (va._Account == acStr)
                        {
                            v.AccountIsVat    = 1;
                            v.IsOffsetAccount = 0;
                            break;
                        }
                        if (va._OffsetAccount == acStr)
                        {
                            v.AccountIsVat    = 1;
                            v.IsOffsetAccount = 1;
                            if (va._VatType == 0)
                            {
                                HasOffsetType0 = true;
                            }
                            else
                            {
                                HasOffsetType1 = true;
                            }
                            // do not break in case we foind the same account as _Account
                        }
                    }
                }

                v.Vat     = Vat;
                v.VatType = (byte)Vat._VatType;
                if (v.IsOffsetAccount == 1 && Vat._OffsetVatOperation != null)
                {
                    v._VatOperation = (byte)vattypes.Get(Vat._OffsetVatOperation).RowId;
                }
                else
                {
                    v._VatOperation = sum.VatOperation != 0 ? sum.VatOperation : Vat._VatOperationCode;
                }
                v._CalculatedVAT = Vat.VatAmount1(v.AmountWithVat, FromDate, RoundTo100, GLVatCalculationMethod.Netto);

                v.FromDate = FromDate;
                v.CalcRate(RoundTo100);

                var vattp = (GLVatType)vattypes.Get(v._VatOperation);
                if (vattp != null)
                {
                    v.VatOpr       = vattp;
                    v.vatOperation = vattp._Code;
                    v.BaseVatText  = vattp._Name;
                }
                lst.Add(v);

                if (PrevAccount != sum.AccountRowId)
                {
                    PrevAccount = sum.AccountRowId;
                    AccsFound.Add(sum.AccountRowId);
                }
            }

            foreach (var acc in (GLAccount[])accounts.GetNotNullArray)
            {
                if (acc._Vat != null)
                {
                    Vat = (GLVat)vats.Get(acc._Vat);
                    if (Vat == null)
                    {
                        continue;
                    }
                    if (countrySelected != 0 && countrySelected != (Vat._VatCountry != 0 ? Vat._VatCountry : country))
                    {
                        continue;
                    }
                    AccsFound.Add(acc.RowId);
                }
            }

            List <int> AccLst = AccsFound.ToList();

            AccsFound.Clear();

            FinancialBalance[] AccTotals = await rapi.GenerateTotal(AccLst, FromDate, ToDate, journal, null, 0, true, false);

            SQLCacheTemplate <FinancialBalance> AccLookup = null;

            if (AccTotals != null && AccTotals.Length > 0)
            {
                AccLookup = new SQLCacheTemplate <FinancialBalance>(AccTotals, false);
            }

            lst.Sort(new VatAccountSort());

            FinancialBalance AccTotal;
            int           AccountRowId = 0;
            double        AmountDif    = 0d;
            VatReportLine vDif         = new VatReportLine();

            for (i = lst.Count; (--i >= 0);)
            {
                v = lst[i];
                if (v.Account.RowId != AccountRowId)
                {
                    if (AmountDif > 0.005d || AmountDif < -0.005d)
                    {
                        vDif.AmountWithout = GLVat.Round(AmountDif);
                        lst.Add(vDif);
                        vDif = new VatReportLine();
                    }

                    AccountRowId = v.Account.RowId;
                    AccTotal     = AccLookup?.Get(AccountRowId);
                    if (AccTotal != null)
                    {
                        AmountDif = (AccTotal._Debit - AccTotal._Credit) / 100d;
                    }
                    else
                    {
                        AmountDif = 0d;
                    }

                    AccsFound.Add(AccountRowId);
                }
                AmountDif           -= v.AmountWithVat;
                vDif.Account         = v.Account;
                vDif.AccountNumber   = v.AccountNumber;
                vDif.VatType         = v.VatType;
                vDif.AccountIsVat    = v.AccountIsVat;
                vDif.IsOffsetAccount = v.IsOffsetAccount;
            }

            if (AmountDif > 0.005d || AmountDif < -0.005d)
            {
                vDif.AmountWithout = GLVat.Round(AmountDif);
                lst.Add(vDif);
            }

            if (AccLookup != null)
            {
                // Add account, that has a VAT-code but no transactios with VAT.
                for (i = 0; (i < AccTotals.Length); i++)
                {
                    AccTotal = AccTotals[i];
                    if (!AccsFound.Contains(AccTotal.RowId) && (AccTotal._Debit - AccTotal._Credit) != 0)
                    {
                        Account = (GLAccount)accounts.Get(AccTotal.RowId);
                        if (Account?._Vat == null)
                        {
                            continue;
                        }
                        Vat = (GLVat)vats.Get(Account._Vat);
                        if (Vat == null)
                        {
                            continue;
                        }

                        vDif = new VatReportLine();
                        vDif.AmountWithout = (AccTotal._Debit - AccTotal._Credit) / 100d;
                        vDif.Account       = Account;
                        vDif.AccountNumber = Account._Account;
                        vDif.VatType       = (byte)Vat._VatType;
                        lst.Add(vDif);
                    }
                }
            }

            if (countrySelected != 0)
            {
                for (i = lst.Count; (--i >= 0);)
                {
                    Vat = (GLVat)vats.Get(lst[i].VatCode);
                    if (Vat != null)
                    {
                        if (countrySelected != (Vat._VatCountry != 0 ? Vat._VatCountry : country))
                        {
                            lst.RemoveAt(i);
                            continue;
                        }
                    }
                }
            }

            vDif = null;

            for (int k = 0; (k < 4); k++)
            {
                string helptext;
                if (k == 0)
                {
                    helptext = "sales";
                }
                else if (k == 1)
                {
                    helptext = "buy";
                }
                else if (k == 2)
                {
                    helptext = "sales vat";
                }
                else
                {
                    helptext = "buy vat";
                }

                for (int n = 0; n < 3; n++)
                {
                    vDif              = new VatReportLine();
                    vDif.helptext     = helptext;
                    vDif.Order        = (n > 0) ? n : -1; // header, total, empty
                    vDif.VatType      = (byte)(k % 2);
                    vDif.AccountIsVat = (byte)(k / 2);
                    vDif.isvatotal    = (k >= 2 && n == 1);
                    lst.Add(vDif);

                    if (vDif.AccountIsVat == 1)
                    {
                        if ((HasOffsetType0 && vDif.VatType == 0) || (HasOffsetType1 && vDif.VatType == 1))
                        {
                            vDif                 = new VatReportLine();
                            vDif.helptext        = "import vat";
                            vDif.Order           = (n > 0) ? n : -1; // header, total, empty
                            vDif.VatType         = (byte)(k % 2);
                            vDif.AccountIsVat    = 1;
                            vDif.IsOffsetAccount = 1;
                            vDif.isvatotal       = true;
                            lst.Add(vDif);
                        }
                    }
                }
            }
            vDif = null;

            lst.Sort(new VatTotalsSort());
            double d1 = 0d, d2 = 0d, d3 = 0d, d4 = 0d;

            double[] VatOperationValues = new double[256];
            double[] VatOperationBases  = new double[256];

            double[] VatValues          = new double[256];
            double[] VatBases           = new double[256];

            VatSumOperationReport[] vatReportSum = new VatSumOperationReport[127];

            for (i = 0; (i < lst.Count); i++)
            {
                v = lst[i];

                if (v.Order == 1) // total
                {
                    v.AmountWithVat = Math.Round(d1, decm);
                    var AmountWithout = Math.Round(d2, decm);
                    v.AmountWithout  = AmountWithout;
                    v._CalculatedVAT = Math.Round(d3, decm);
                    v._PostedVAT     = Math.Round(d4, decm);
                    d1 = d2 = d3 = d4 = 0d;

                    if (country == CountryCode.Denmark && AmountWithout != 0 && v.AccountIsVat == 1)
                    {
                        if (v.IsOffsetAccount == 0)
                        {
                            // here we sum up the amount without vatcodes
                            if (v.VatType == 0)
                            {
                                var sumLine = CreateSum(vatReportSum, 33);
                                sumLine._Amount -= AmountWithout;
                            }
                            else
                            {
                                var sumLine = CreateSum(vatReportSum, 23);
                                sumLine._Amount += AmountWithout;
                                sumLine          = CreateSum(vatReportSum, 31);
                                sumLine._Amount += AmountWithout;
                            }
                        }
                        else
                        {
                            if (v.VatType == 1)
                            {
                                var sumLine = CreateSum(vatReportSum, 34);
                                sumLine._Amount -= AmountWithout;
                            }
                        }
                    }
                }
                else
                {
                    d1 += v.AmountWithVat;
                    d2 += v.AmountWithout;
                    d3 += v._CalculatedVAT;
                    d4 += v._PostedVAT;

                    if (v.AccountIsVat == 1)
                    {
                        if (v.IsOffsetAccount == 0)
                        {
                            VatOperationValues[v._VatOperation] += v.AmountWithVat;
                            if (v.Vat != null)
                            {
                                VatValues[v.Vat.RowId] += v.AmountWithVat;
                            }
                        }
                    }
                    else
                    {
                        VatOperationBases[v._VatOperation] += v.AmountWithVat;
                        if (v.Vat != null)
                        {
                            VatBases[v.Vat.RowId] += v.AmountWithVat;
                        }
                    }
                }
            }

            vDif      = new VatReportLine();
            vDif.Text = string.Format("{0}: {1}, {2}", Uniconta.ClientTools.Localization.lookup("FinalVatStatus"), Uniconta.ClientTools.Localization.lookup("AmountBase"), Uniconta.ClientTools.Localization.lookup("VATamount"));
            lst.Add(vDif);

            foreach (var c in (GLVatType[])vattypes.GetNotNullArray)
            {
                if (c == null)
                {
                    continue;
                }

                vDif = new VatReportLine();
                vDif.vatOperation  = c._Code;
                vDif._Rate         = c._Pct1;
                vDif.Text          = string.Concat(c._Code, ", ", c._Name);
                vDif.AmountWithVat = VatOperationBases[c._RowNo];
                vDif._PostedVAT    = VatOperationValues[c._RowNo];
                vDif.AccountIsVat  = 1;
                lst.Add(vDif);

                int pos = c._Position1;
                if (pos > 0)
                {
                    UpdateSum(vatReportSum, vDif, pos, c._Position2, c._Position3);
                }
                pos = c._Position4;
                if (pos > 0)
                {
                    UpdateSum(vatReportSum, vDif, pos, c._Position5, c._Position6);
                }
                pos = c._Position7;
                if (pos > 0)
                {
                    UpdateSum(vatReportSum, vDif, pos, c._Position8, c._Position9);
                }
                pos = c._Position10;
                if (pos > 0)
                {
                    UpdateSum(vatReportSum, vDif, pos, c._Position11, c._Position12);
                }
                pos = c._Position13;
                if (pos > 0)
                {
                    UpdateSum(vatReportSum, vDif, pos, c._Position14, c._Position15);
                }
            }

            List <VatReportLine> vatlst = new List <VatReportLine>();

            this.vatlst = vatlst;

            for (int k = 0; (k < 2); k++)
            {
                vDif      = new VatReportLine();
                vDif.Text = string.Empty;
                lst.Add(vDif);

                vDif              = new VatReportLine();
                vDif.VatType      = (byte)k;
                vDif.Order        = -1;
                vDif.AccountIsVat = 1;
                lst.Add(vDif);

                foreach (var c in (GLVat[])vats.GetNotNullArray)
                {
                    if (c != null && (int)c._VatType == k)
                    {
                        if (countrySelected != 0 && countrySelected != (c._VatCountry != 0 ? c._VatCountry : country))
                        {
                            continue;
                        }

                        vDif               = new VatReportLine();
                        vDif.Vat           = c;
                        vDif.Text          = string.Concat(c._Vat, ", ", c._Name);
                        vDif.AmountWithVat = VatBases[c.RowId];
                        vDif._PostedVAT    = VatValues[c.RowId];
                        vDif.AccountIsVat  = 1;
                        vDif.FromDate      = FromDate;
                        vDif.CalcRate(RoundTo100);
                        lst.Add(vDif);
                        vatlst.Add(vDif);
                    }
                }
            }

            dgVatReport.ItemsSource = lst;
            if (lst.Count > 0)
            {
                SetMenuItem();
            }

            if (country == CountryCode.Denmark)
            {
                AccLst.Clear();
                foreach (var acc in (GLAccount[])accounts.GetNotNullArray)
                {
                    if (acc._SystemAccount == (byte)SystemAccountTypes.OtherTax)
                    {
                        AccLst.Add(acc.RowId);
                    }
                }
                if (AccLst != null && AccLst.Count > 0)
                {
                    AccTotals = await rapi.GenerateTotal(AccLst, FromDate, ToDate);

                    if (AccTotals != null && AccTotals.Length > 0)
                    {
                        var otherTaxList = new ReportDataDenmark[AccTotals.Length];
                        i = 14;
                        foreach (var acTot in AccTotals)
                        {
                            var Acc = accounts.Get(acTot.AccountRowId);
                            var lin = new VatSumOperationReport()
                            {
                                _Text   = Acc.KeyName,
                                _Amount = (acTot._Debit - acTot._Credit) / 100d,
                                _Line   = i
                            };
                            vatReportSum[i] = lin;
                            i++;
                            if (i == 19)
                            {
                                break;
                            }
                        }
                    }
                }
            }

            for (i = vatReportSumSize + 1; (--i >= 1);)
            {
                if (vatReportSum[i] == null)
                {
                    vatReportSum[i] = new VatSumOperationReport()
                    {
                        _Line = i
                    }
                }
            }
            ;

            this.vatReportSum = vatReportSum.Where(r => r != null).ToList();

            dgVatReport.Visibility = Visibility.Visible;
            busyIndicator.IsBusy   = false;
        }
        public VatReportDenmark(CrudAPI api, List <VatSumOperationReport> vatSumOperationLst, DateTime fromDate, DateTime toDate)
        {
            InitializeComponent();
            ReportDataDenmark data = new ReportDataDenmark();

            data.VatPeriode = string.Format("Periode: {0} - {1}", fromDate.ToShortDateString(), toDate.ToShortDateString());

            var vatArray = new double[100];

            data.VatArray = vatArray;
            var OtherTaxName = new string[10];

            data.OtherTaxName = OtherTaxName;

            foreach (var rec in vatSumOperationLst)
            {
                if (rec == null)
                {
                    continue;
                }

                var idx = rec._Line;
                if (idx < 14)
                {
                    vatArray[idx] += rec._AmountBase;
                }
                else if (idx >= 14 && idx <= 19)
                {
                    OtherTaxName[idx - 13] = rec._Text;
                    vatArray[idx]          = rec._Amount;
                    vatArray[31]          += rec._Amount;
                }
                else
                {
                    vatArray[idx] += rec._Amount;
                }

                /*
                 * 1-10 totals at the buttom
                 * 14-18 other tax
                 *
                 * 51 TotalAmountUdgÄende
                 * 52 TotalAmountIndgÄende
                 */
            }

            vatArray[32] = vatArray[33] + vatArray[34] + vatArray[35];
            vatArray[30] = vatArray[32] - vatArray[31];

            data.CompanyInfo  = api.CompanyEntity._Name;
            data.CompanyRegNr = string.Format("Cvr.nr: {0}", api.CompanyEntity._Id);

            VatDenmarkReportHeader sourcedata = new VatDenmarkReportHeader();
            //sourcedata.exportUri = CorasauDataGrid.GetExportServiceConnection(api);
            VatPrintBaseModuleDenmark printbase = new VatPrintBaseModuleDenmark();

            printbase.DetailTemplate          = (DataTemplate)this.Resources["detailTemplate"];
            printbase.ReportHeaderDataContext = (VatDenmarkReportHeader)sourcedata;
            printbase.DetailData  = data;
            custPrint.DataContext = printbase;
        }