async Task SaveSession()
        {
            try
            {
                InvoiceTypeCode = (NodeEnum.InvoiceType) await NodeContext.Invoice_tbTypes.Where(t => t.InvoiceType == InvoiceType).Select(t => t.InvoiceTypeCode).FirstAsync();

                AccountCode = await NodeContext.Org_tbOrgs.Where(o => o.AccountName == OrganisationName).Select(o => o.AccountCode).FirstAsync();

                CashCode = await NodeContext.Cash_tbCodes.Where(c => c.CashDescription == CashDescription).Select(c => c.CashCode).FirstAsync();

                TaxCode = await NodeContext.App_tbTaxCodes.Where(c => c.TaxDescription == TaxDescription).Select(c => c.TaxCode).FirstAsync();
            }
            catch
            {
            }
        }
        public async Task <IActionResult> OnGetAsync(string accountCode, string cashCode, string taxCode)
        {
            try
            {
                var organisationNames = from t in NodeContext.Org_AccountLookup
                                        orderby t.AccountName
                                        select t.AccountName;

                OrganisationNames = new SelectList(await organisationNames.ToListAsync());

                var profile = new Profile(NodeContext);

                if (!string.IsNullOrEmpty(accountCode))
                {
                    AccountCode = accountCode;
                }
                else if (string.IsNullOrEmpty(AccountCode))
                {
                    AccountCode = await profile.CompanyAccountCode;
                }

                OrganisationName = await NodeContext.Org_tbOrgs.Where(o => o.AccountCode == AccountCode).Select(o => o.AccountName).FirstOrDefaultAsync();

                var cashDescriptions = from t in NodeContext.Cash_CodeLookup
                                       where t.CashTypeCode < (short)NodeEnum.CashType.Bank
                                       orderby t.CashDescription
                                       select t.CashDescription;

                CashDescriptions = new SelectList(await cashDescriptions.ToListAsync());

                if (!string.IsNullOrEmpty(cashCode))
                {
                    CashCodes code = new(NodeContext, cashCode);
                    CashCode = cashCode;
                    TaxCode  = code.TaxCode;
                }
                else if (string.IsNullOrEmpty(CashCode))
                {
                    CashCode = await NodeContext.Cash_CodeLookup
                               .Where(c => c.CashTypeCode < (short)NodeEnum.CashType.Bank)
                               .OrderBy(c => c.CashCode)
                               .Select(c => c.CashCode)
                               .FirstAsync();
                }

                CashDescription = await NodeContext.Cash_tbCodes.Where(c => c.CashCode == CashCode).Select(c => c.CashDescription).FirstOrDefaultAsync();

                var taxDescriptions = from t in NodeContext.App_TaxCodes
                                      orderby t.TaxDescription
                                      select t.TaxDescription;

                TaxDescriptions = new SelectList(await taxDescriptions.ToListAsync());

                if (!string.IsNullOrEmpty(taxCode))
                {
                    TaxCode = taxCode;
                }
                else if (string.IsNullOrEmpty(TaxCode))
                {
                    TaxCode = await NodeContext.App_tbTaxCodes
                              .Where(t => t.TaxTypeCode == (short)NodeEnum.TaxType.VAT)
                              .OrderBy(t => t.TaxCode)
                              .Select(t => t.TaxCode)
                              .FirstAsync();
                }

                TaxDescription = await NodeContext.App_tbTaxCodes.Where(t => t.TaxCode == TaxCode).Select(t => t.TaxDescription).FirstOrDefaultAsync();

                InvoiceTypes = new SelectList(await NodeContext.Invoice_tbTypes.OrderBy(t => t.InvoiceTypeCode).Select(t => t.InvoiceType).ToListAsync());
                InvoiceType  = await NodeContext.Invoice_tbTypes.Where(t => t.InvoiceTypeCode == (short)InvoiceTypeCode).Select(t => t.InvoiceType).FirstAsync();

                Invoice_Entry = new()
                {
                    UserId          = await profile.UserId(UserManager.GetUserId(User)),
                    AccountCode     = AccountCode,
                    InvoiceTypeCode = (short)InvoiceTypeCode,
                    CashCode        = CashCode,
                    TaxCode         = TaxCode,
                    InvoicedOn      = DateTime.Today
                };

                await SetViewData();

                return(Page());
            }
            catch (Exception e)
            {
                NodeContext.ErrorLog(e);
                throw;
            }
        }
        public async Task <IActionResult> OnPostAsync()
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(Page());
                }

                NodeEnum.InvoiceStatus invoiceStatus = (NodeEnum.InvoiceStatus) await NodeContext.Invoice_tbStatuses
                                                       .Where(t => t.InvoiceStatus == InvoiceStatus)
                                                       .Select(t => t.InvoiceStatusCode).FirstAsync();

                NodeEnum.InvoiceType invoiceType = (NodeEnum.InvoiceType) await NodeContext.Invoice_tbTypes
                                                   .Where(t => t.InvoiceType == InvoiceType)
                                                   .Select(t => t.InvoiceTypeCode).FirstAsync();



                bool periodRebuild = false;

                FinancialPeriods periods            = new(NodeContext);
                DateTime         previousInvoicedOn = await NodeContext.Invoice_tbInvoices.Where(i => i.InvoiceNumber == Invoice_tbInvoice.InvoiceNumber)
                                                      .Select(i => i.InvoicedOn).FirstAsync();

                if (previousInvoicedOn != Invoice_tbInvoice.InvoicedOn)
                {
                    DateTime activePeriod = periods.ActiveStartOn;
                    periodRebuild = (previousInvoicedOn < activePeriod || Invoice_tbInvoice.InvoicedOn < activePeriod);
                }

                bool orgRebuild = ((NodeEnum.InvoiceType)Invoice_tbInvoice.InvoiceTypeCode != invoiceType) || ((NodeEnum.InvoiceStatus)Invoice_tbInvoice.InvoiceStatusCode != invoiceStatus) || periodRebuild;

                Invoice_tbInvoice.InvoiceStatusCode = (short)invoiceStatus;
                Invoice_tbInvoice.InvoiceTypeCode   = (short)invoiceType;

                NodeContext.Attach(Invoice_tbInvoice).State = EntityState.Modified;

                try
                {
                    await NodeContext.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!await NodeContext.Invoice_tbInvoices.AnyAsync(e => e.AccountCode == Invoice_tbInvoice.InvoiceNumber))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                Invoices invoices = new(NodeContext, Invoice_tbInvoice.InvoiceNumber);

                if (await invoices.Accept())
                {
                    if (periodRebuild)
                    {
                        periodRebuild = await periods.Generate();
                    }

                    if (orgRebuild)
                    {
                        Orgs orgs = new(NodeContext, Invoice_tbInvoice.AccountCode);
                        orgRebuild = await orgs.Rebuild();
                    }
                }

                RouteValueDictionary route = new();
                route.Add("InvoiceNumber", Invoice_tbInvoice.InvoiceNumber);

                return(RedirectToPage("./Index", route));
            }
            catch (Exception e)
            {
                NodeContext.ErrorLog(e);
                throw;
            }
        }