public IActionResult CancelInvoice(SaleBill salBill, IEnumerable <SaleBillItem> SaleBillItem_List)
        {
            string CompId = User.Claims.Where(r => r.Type == "CompanyID").FirstOrDefault().Value;
            int    CompID = Convert.ToInt32(CompId);

            salBill.SB_B_1    = true;
            salBill.CreatedBy = User.Identity.Name;
            salBill.CompanyID = CompID;
            int total = _context.SaleBillItem.Where(r => r.CompanyID == CompID).Where(r => r.SaleBillID == salBill.SaleBillID).Count();

            for (int i = 0; i < total; i++)
            {
                SaleBillItem SBI = new SaleBillItem();
                SBI = _context.SaleBillItem.Where(r => r.SaleBillID == salBill.SaleBillID).Where(r => r.SaleBillExtraBool == false).FirstOrDefault();
                SBI.SaleBillExtraBool = true;
                _context.Update(SBI);
                _context.SaveChanges();
            }

            _context.Update(salBill);
            _context.SaleTransaction.RemoveRange(_context.SaleTransaction.Where(x => x.SaleBillID == salBill.SaleBillID));
            _context.CashFlow.RemoveRange(_context.CashFlow.Where(x => x.SaleBillID == salBill.SaleBillID));
            _context.SaveChanges();

            // Decreasing Item Quantity from ITEM Table
            decimal StockQuantity, final;

            foreach (var item in SaleBillItem_List)
            {
                Item itemObj = new Item();
                itemObj       = _context.Item.Where(r => r.ItemID == item.ItemID).SingleOrDefault();
                StockQuantity = itemObj.Quantity;
                final         = StockQuantity + item.Quantity;

                itemObj.Quantity = final;
                _context.Update(itemObj);
                _context.SaveChanges();
            }
            // Decreasing Item Quantity from ITEM Table

            return(RedirectToAction("Index"));
        }
        public async Task <IActionResult> Edit(SaleBill saleBill, IEnumerable <SaleBillItem> SaleBillItem_List, IEnumerable <SaleTransaction> SaleTransactionList)
        {
            int SalBillID = saleBill.SaleBillID;

            string CompId = User.Claims.Where(r => r.Type == "CompanyID").FirstOrDefault().Value;
            int    CompID = Convert.ToInt32(CompId);

            // Main Sale Bill
            saleBill.CreatedBy = User.Identity.Name;
            saleBill.CompanyID = CompID;

            _context.Update(saleBill);
            await _context.SaveChangesAsync();

            _context.SaleTransaction.RemoveRange(_context.SaleTransaction.Where(x => x.SaleBillID == saleBill.SaleBillID));
            _context.SaleBillItem.RemoveRange(_context.SaleBillItem.Where(x => x.SaleBillID == saleBill.SaleBillID));
            _context.CashFlow.RemoveRange(_context.CashFlow.Where(x => x.SaleBillID == saleBill.SaleBillID));
            _context.SaveChanges();

            // Sale Items List Entry

            var TransactionList_Result = SaleTransactionList.ToList();

            TransactionList_Result.RemoveAll(x => x.SaleTransactionID == 777777);
            TransactionList_Result.ForEach(x => x.SaleTransactionID = 0);

            foreach (var item in TransactionList_Result)
            {
                SaleTransaction transaction = new SaleTransaction();
                transaction.CompanyID  = CompID;
                transaction.Amount     = item.Amount;
                transaction.BankID     = item.BankID;
                transaction.Cheque     = item.Cheque;
                transaction.Date       = item.Date;
                transaction.Mode       = item.Mode;
                transaction.Paid       = item.Paid;
                transaction.SaleBillID = SalBillID;
                _context.SaleTransaction.Add(transaction);
                await _context.SaveChangesAsync();
            }

            var ItemList_Result = SaleBillItem_List.ToList();

            ItemList_Result.RemoveAll(x => x.SaleBillExtraInt_1 == 777777);

            foreach (var item in ItemList_Result)
            {
                SaleBillItem salBillItem = new SaleBillItem();

                if (item.SaleBillExtraInt_2 != 0)
                {
                    // Increase Quantity of Items in Stock (Item Table)
                    Item    itemObj = new Item();
                    decimal OldBillQuantity, StockQuantity, final;

                    OldBillQuantity = item.SaleBillExtraInt; // Old Quantity

                    itemObj       = _context.Item.Where(r => r.ItemID == item.SaleBillExtraInt_2).SingleOrDefault();
                    StockQuantity = itemObj.Quantity;
                    final         = StockQuantity + OldBillQuantity;

                    itemObj.Quantity = final;
                    _context.Update(itemObj);
                    await _context.SaveChangesAsync();

                    // Increase Quantity of Items in Stock (Item Table)
                }
                salBillItem.ItemID     = item.ItemID;
                salBillItem.SaleBillID = SalBillID;
                salBillItem.SalePrice  = item.SalePrice;
                salBillItem.Quantity   = item.Quantity;
                salBillItem.CompanyID  = CompID;

                _context.SaleBillItem.Add(salBillItem);
                await _context.SaveChangesAsync();

                // Increase Quantity of Items in Stock (Item Table)

                // Decrease Quantity of Items in Stock (Item Table)
                Item    itemObj1 = new Item();
                decimal Old_Quantity, New_Quantity;

                itemObj1     = _context.Item.Where(r => r.ItemID == item.ItemID).SingleOrDefault();
                Old_Quantity = itemObj1.Quantity;
                New_Quantity = Old_Quantity - item.Quantity;

                itemObj1.Quantity = New_Quantity;
                _context.Update(itemObj1);
                await _context.SaveChangesAsync();

                // Decrease Quantity of Items in Stock (Item Table)
            }
            // Sale Items List Entry

            if (saleBill.PartiesID == null)
            {
                CashFlow CF = new CashFlow();
                CF.Credit     = saleBill.NetAmount;
                CF.SaleBillID = SalBillID;
                CF.CompanyID  = CompID;

                // Cash Sale 03-0001-0001
                int MainAccID = _context.MainAccount.Where(r => r.CompanyID == CompID).Where(r => r.MainAccountNumber == "03").FirstOrDefault().MainAccountID;
                int SubAccID  = _context.SubAccount.Where(r => r.CompanyID == CompID).Where(r => r.MainAccountID == MainAccID).Where(r => r.SubAccountNumber == "0001").FirstOrDefault().SubAccountID;
                int TraAccID  = _context.TransactionAccount.Where(r => r.CompanyID == CompID).Where(r => r.SubAccountID == SubAccID).Where(r => r.TransactionAccountNumber == "0001").FirstOrDefault().TransactionAccountID;

                CF.MainAccountID        = MainAccID;
                CF.SubAccountID         = SubAccID;
                CF.TransactionAccountID = TraAccID;
                CF.VoucherType          = "Sale Bill Voucher";
                CF.DateCreation         = DateTime.Now.Date;

                _context.CashFlow.Add(CF);
                await _context.SaveChangesAsync();

                CashFlow CF1 = new CashFlow();
                CF1.Debit      = saleBill.NetAmount;
                CF1.SaleBillID = SalBillID;
                CF1.CompanyID  = CompID;

                // Cash In Hand  01-0001-0001
                int MainAccID_1 = _context.MainAccount.Where(r => r.CompanyID == CompID).Where(r => r.MainAccountNumber == "01").FirstOrDefault().MainAccountID;
                int SubAccID_1  = _context.SubAccount.Where(r => r.CompanyID == CompID).Where(r => r.MainAccountID == MainAccID_1).Where(r => r.SubAccountNumber == "0001").FirstOrDefault().SubAccountID;
                int TraAccID_1  = _context.TransactionAccount.Where(r => r.CompanyID == CompID).Where(r => r.SubAccountID == SubAccID_1).Where(r => r.TransactionAccountNumber == "0001").FirstOrDefault().TransactionAccountID;

                CF1.MainAccountID        = MainAccID_1;
                CF1.SubAccountID         = SubAccID_1;
                CF1.TransactionAccountID = TraAccID_1;
                CF1.VoucherType          = "Sale Bill Voucher";
                CF1.DateCreation         = DateTime.Now.Date;

                _context.CashFlow.Add(CF1);
                await _context.SaveChangesAsync();
            }
            else
            {
                CashFlow CF2 = new CashFlow();
                CF2.Debit      = saleBill.NetAmount;
                CF2.SaleBillID = SalBillID;
                CF2.CompanyID  = CompID;
                CF2.PartiesID  = saleBill.PartiesID;
                // Party Debit
                int?PartyTransID = _context.Parties.Where(r => r.CompanyID == CompID).Where(r => r.PartiesID == saleBill.PartiesID).FirstOrDefault().TransactionAccountID;
                int PartMainAcc1 = _context.MainAccount.Where(r => r.CompanyID == CompID).Where(r => r.MainAccountNumber == "00").FirstOrDefault().MainAccountID;
                int PartSubAcc1  = _context.SubAccount.Where(r => r.CompanyID == CompID).Where(r => r.SubAccountNumber == "0000").FirstOrDefault().SubAccountID;

                CF2.MainAccountID        = PartMainAcc1;
                CF2.SubAccountID         = PartSubAcc1;
                CF2.TransactionAccountID = (int)PartyTransID;
                CF2.VoucherType          = "Sale Bill Voucher";
                CF2.DateCreation         = DateTime.Now.Date;

                _context.CashFlow.Add(CF2);
                await _context.SaveChangesAsync();

                CashFlow CF3 = new CashFlow();
                CF3.Credit     = saleBill.NetAmount;
                CF3.SaleBillID = SalBillID;
                CF3.CompanyID  = CompID;
                CF3.PartiesID  = saleBill.PartiesID;
                // Sale Credit 03-0001-0002
                int MainAccID = _context.MainAccount.Where(r => r.CompanyID == CompID).Where(r => r.MainAccountNumber == "03").FirstOrDefault().MainAccountID;
                int SubAccID  = _context.SubAccount.Where(r => r.CompanyID == CompID).Where(r => r.MainAccountID == MainAccID).Where(r => r.SubAccountNumber == "0001").FirstOrDefault().SubAccountID;
                int TraAccID  = _context.TransactionAccount.Where(r => r.CompanyID == CompID).Where(r => r.SubAccountID == SubAccID).Where(r => r.TransactionAccountNumber == "0002").FirstOrDefault().TransactionAccountID;

                CF3.MainAccountID        = MainAccID;
                CF3.SubAccountID         = SubAccID;
                CF3.TransactionAccountID = TraAccID;
                CF3.VoucherType          = "Sale Bill Voucher";
                CF3.DateCreation         = DateTime.Now.Date;

                _context.CashFlow.Add(CF3);
                await _context.SaveChangesAsync();

                if (SaleTransactionList.FirstOrDefault().Paid == true)
                {
                    CashFlow CF4 = new CashFlow();
                    CF4.Credit     = SaleTransactionList.FirstOrDefault().Amount;
                    CF4.SaleBillID = SalBillID;
                    CF4.CompanyID  = CompID;
                    CF4.PartiesID  = saleBill.PartiesID;
                    // Party Credit
                    int?PartyTransID_1 = _context.Parties.Where(r => r.CompanyID == CompID).Where(r => r.PartiesID == saleBill.PartiesID).FirstOrDefault().TransactionAccountID;
                    int PartMainAcc1_2 = _context.MainAccount.Where(r => r.CompanyID == CompID).Where(r => r.MainAccountNumber == "00").FirstOrDefault().MainAccountID;
                    int PartSubAcc1_2  = _context.SubAccount.Where(r => r.CompanyID == CompID).Where(r => r.SubAccountNumber == "0000").FirstOrDefault().SubAccountID;

                    CF4.MainAccountID        = PartMainAcc1_2;
                    CF4.SubAccountID         = PartSubAcc1_2;
                    CF4.TransactionAccountID = (int)PartyTransID_1;
                    CF4.VoucherType          = "Sale Bill Voucher";
                    CF4.DateCreation         = DateTime.Now.Date;

                    _context.CashFlow.Add(CF4);
                    await _context.SaveChangesAsync();

                    if (SaleTransactionList.FirstOrDefault().Mode == Models.PaymentMode.Cash) // Mode 0 Means ==> Cash
                    {
                        CashFlow CF5 = new CashFlow();
                        CF5.Debit      = SaleTransactionList.FirstOrDefault().Amount;
                        CF5.SaleBillID = SalBillID;
                        CF5.CompanyID  = CompID;
                        CF5.PartiesID  = saleBill.PartiesID;
                        // Cash in Hand Debit 01-0001-0001
                        int MainAccID_2 = _context.MainAccount.Where(r => r.CompanyID == CompID).Where(r => r.MainAccountNumber == "01").FirstOrDefault().MainAccountID;
                        int SubAccID_2  = _context.SubAccount.Where(r => r.CompanyID == CompID).Where(r => r.MainAccountID == MainAccID_2).Where(r => r.SubAccountNumber == "0001").FirstOrDefault().SubAccountID;
                        int TraAccID_2  = _context.TransactionAccount.Where(r => r.CompanyID == CompID).Where(r => r.SubAccountID == SubAccID_2).Where(r => r.TransactionAccountNumber == "0001").FirstOrDefault().TransactionAccountID;

                        CF5.MainAccountID        = MainAccID_2;
                        CF5.SubAccountID         = SubAccID_2;
                        CF5.TransactionAccountID = TraAccID_2;
                        CF5.VoucherType          = "Sale Bill Voucher";
                        CF5.DateCreation         = DateTime.Now.Date;

                        _context.CashFlow.Add(CF5);
                        await _context.SaveChangesAsync();
                    }
                    else if (SaleTransactionList.FirstOrDefault().Mode == Models.PaymentMode.Cheque) // Mode 0 Means ==> Cheque
                    {
                        CashFlow CF6 = new CashFlow();
                        CF6.Debit      = SaleTransactionList.FirstOrDefault().Amount;
                        CF6.SaleBillID = SalBillID;
                        CF6.CompanyID  = CompID;
                        CF6.PartiesID  = saleBill.PartiesID;
                        // Bank Debit
                        int?bankID      = SaleTransactionList.FirstOrDefault().BankID;
                        int transBankID = _context.Bank.Where(r => r.CompanyID == CompID).Where(r => r.BankID == bankID).FirstOrDefault().TransactionAccountID;
                        int MainAccID_3 = _context.MainAccount.Where(r => r.CompanyID == CompID).Where(r => r.MainAccountNumber == "01").FirstOrDefault().MainAccountID;
                        int SubAccID_3  = _context.SubAccount.Where(r => r.CompanyID == CompID).Where(r => r.MainAccountID == MainAccID_3).Where(r => r.SubAccountNumber == "0002").FirstOrDefault().SubAccountID;

                        CF6.MainAccountID        = MainAccID_3;
                        CF6.SubAccountID         = SubAccID_3;
                        CF6.TransactionAccountID = transBankID;
                        CF6.VoucherType          = "Sale Bill Voucher";
                        CF6.DateCreation         = DateTime.Now.Date;

                        _context.CashFlow.Add(CF6);
                        await _context.SaveChangesAsync();
                    }
                }
            }
            // Sale Transaction Entry

            return(RedirectToAction("Index"));
        }