public ActionResult GetDetails(List <HG_OrderItem> CompletedItems)
        {
            BalanceStatement              bObj          = new BalanceStatement();
            List <BalanceStatement>       jObjList      = new List <BalanceStatement>();
            List <HG_Orders>              OrdersDetails = new List <HG_Orders>();
            List <HG_OrganizationDetails> OrgList       = new List <HG_OrganizationDetails>();
            List <HG_Orders>              OrdrList      = new List <HG_Orders>();
            int    LastEntryNo = 0;
            double LastBalance = 0.00;

            List <PaytmResn> Onlinepayment = new List <PaytmResn>();

            double totalAmount = 0.00;

            HG_Orders ord = new HG_Orders().GetOne(CompletedItems[0].OID);


            if (ord.PaymentStatus == 1 || ord.PaymentStatus == 2)
            {
                for (int i = 0; i < CompletedItems.Count; i++)
                {
                    OrdrList = new HG_Orders().GetAll(CompletedItems[i].OrgId, Status: 3);
                    HG_Orders Ord = OrdrList.Find(x => x.OID == CompletedItems[i].OID);

                    totalAmount += Ord.DeliveryCharge;
                    totalAmount += CompletedItems[i].Count * CompletedItems[i].Price;
                }
                Ledger LedgerDetails = Ledger.GetAllList().Where(x => x.DebtorType == 1 &&
                                                                 x.OrgId == CompletedItems[0].OrgId).FirstOrDefault();

                bObj.Date    = DateTime.Now;
                bObj.Amount  = totalAmount;
                bObj.OrgId   = LedgerDetails.OrgId;
                bObj.OrderId = CompletedItems[0].OID;
                BalanceStatement TotalBalance = BalanceStatement.GetAllForBalanceCalculation(CompletedItems[0].OrgId).Last();
                bObj.Balance = TotalBalance.Balance + totalAmount;

                bObj.isCash = true;
                BalanceStatement Obj          = new BalanceStatement();
                HG_Orders        od           = new HG_Orders().GetOne(bObj.OrderId);
                Ledger           LedgerDetail = Ledger.GetAllList().Where(x => x.DebtorType == 1 &&
                                                                          x.OrgId == bObj.OrgId).FirstOrDefault();



                if (od.PaymentStatus == 1 || od.PaymentStatus == 2)
                {
                    double amt = (bObj.Amount * LedgerDetails.MarginOnCash) / 100;
                    Obj.CRAmount  = (amt) + ((amt * LedgerDetails.TaxOnAboveMargin) / 100);
                    Obj.TaxOnCash = LedgerDetails.TaxOnAboveMargin;
                }
                else if (od.PaymentStatus == 3)
                {
                    double amt = (bObj.Amount * LedgerDetails.MarginOnline) / 100;
                    Obj.CRAmount    = (amt) + ((amt * LedgerDetails.TaxOnAboveMarginOnline) / 100);
                    Obj.TaxOnOnline = LedgerDetails.TaxOnAboveMarginOnline;
                }
                Obj.Date = bObj.Date;

                // Obj.Amount = Amount;
                BalanceStatement TBalance = BalanceStatement.GetAllForBalanceCalculation(bObj.OrgId).Last();
                Obj.Balance   = TBalance.Balance - Obj.CRAmount;
                Obj.OrgId     = bObj.OrgId;
                Obj.OrderId   = bObj.OrderId;
                Obj.Narration = "Commission of Order No." + bObj.OrderId;
                Obj.SaveCRValue();
            }
            else if (ord.PaymentStatus == 3)
            {
                double OAmount = 0.00;
                Onlinepayment = PaytmResn.GetAll();
                PaytmResn paytmTxn = Onlinepayment.Find(x => x.OID == CompletedItems[0].OID);

                totalAmount += ord.DeliveryCharge;
                totalAmount += Convert.ToDouble(paytmTxn.PaidAmount);


                Ledger LedgerDetails = Ledger.GetAllList().Where(x => x.DebtorType == 1 &&
                                                                 x.OrgId == CompletedItems[0].OrgId).FirstOrDefault();

                bObj.Date    = DateTime.Now;
                bObj.Amount  = totalAmount;
                bObj.OrgId   = LedgerDetails.OrgId;
                bObj.OrderId = CompletedItems[0].OID;
                BalanceStatement TotalBalance = BalanceStatement.GetAllForBalanceCalculation(CompletedItems[0].OrgId).Last();
                bObj.Balance = TotalBalance.Balance + totalAmount;

                bObj.Narration = "Online Payment of Order No." + CompletedItems[0].OID;
                bObj.isCash    = false;
                bObj.SaveCRValue();

                BalanceStatement Obj          = new BalanceStatement();
                HG_Orders        od           = new HG_Orders().GetOne(bObj.OrderId);
                Ledger           LedgerDetail = Ledger.GetAllList().Where(x => x.DebtorType == 1 &&
                                                                          x.OrgId == bObj.OrgId).FirstOrDefault();



                if (od.PaymentStatus == 1 || od.PaymentStatus == 2)
                {
                    double amt = (bObj.Amount * LedgerDetails.MarginOnCash) / 100;
                    Obj.CRAmount  = (amt) + ((amt * LedgerDetails.TaxOnAboveMargin) / 100);
                    Obj.TaxOnCash = LedgerDetails.TaxOnAboveMargin;
                }
                else if (od.PaymentStatus == 3)
                {
                    double amt = (bObj.Amount * LedgerDetails.MarginOnline) / 100;
                    Obj.CRAmount    = (amt) + ((amt * LedgerDetails.TaxOnAboveMarginOnline) / 100);
                    Obj.TaxOnOnline = LedgerDetails.TaxOnAboveMarginOnline;

                    if (ord.PaymentStatus == 3 && ord.Status == "4")
                    {
                        double RefundAmt           = bObj.Amount - OAmount;
                        double GetRefundAmt        = (RefundAmt * 2) / 100;
                        double GetRefundAmtwithTax = GetRefundAmt + ((GetRefundAmt * LedgerDetails.TaxOnAboveMarginOnline) / 100);
                        Obj.CRAmount = Obj.CRAmount + GetRefundAmtwithTax;
                    }
                }
                Obj.Date = bObj.Date;

                // Obj.Amount = Amount;
                BalanceStatement TBalance = BalanceStatement.GetAllForBalanceCalculation(bObj.OrgId).Last();
                Obj.Balance   = TBalance.Balance - Obj.CRAmount;
                Obj.OrgId     = bObj.OrgId;
                Obj.OrderId   = bObj.OrderId;
                Obj.Narration = "Commission of Order No." + bObj.OrderId;
                Obj.SaveCRValue();



                //===================================================================================================================
                Receipt ReceiptEntry = new Receipt();

                ReceiptEntry.BalanceStatementId = bObj.BID;
                ReceiptEntry.Date       = bObj.Date;
                ReceiptEntry.Amount     = bObj.Amount;
                ReceiptEntry.Particular = "Online Payment of Order No." + CompletedItems[0].OID;
                try
                {
                    LastEntryNo = Receipt.GetAllList(0, 0).Select(s => s.EntryNo).Last();
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
                if (LastEntryNo > 0)
                {
                    ReceiptEntry.EntryNo = LastEntryNo + 1;
                }
                else
                {
                    ReceiptEntry.EntryNo = 1;
                }

                ReceiptEntry.OrgId = bObj.OrgId;
                string Customer           = "Customer";
                string Paytm              = "Paytm";
                string Bank               = "BANK";
                string CurrentLiabilities = "Current Liabilities";

                Ledger CutomerLedger = Ledger.GetAllList().Where(x => x.Name.ToLower() == Customer.ToLower()).FirstOrDefault();


                Ledger Paytmledger = Ledger.GetAllList().Where(x => x.Name.ToLower() == Paytm.ToLower()).FirstOrDefault();

                ReceiptEntry.CRLedgerId = CutomerLedger.ID;
                ReceiptEntry.DRLedgerId = Paytmledger.ID;

                Group BankGroup = Group.GetAll().Where(x => x.Name.ToLower() == Bank.ToLower()).FirstOrDefault();


                Group CurrentLiabilitiesGroup = Group.GetAll().Where(x => x.Name.ToLower() == CurrentLiabilities.ToLower()).FirstOrDefault();

                ReceiptEntry.CRGroupId = CurrentLiabilitiesGroup.ID;
                ReceiptEntry.DRGroupId = BankGroup.ID;
                try
                {
                    LastBalance = Receipt.GetAllList(0, 0).Select(s => s.Balance).Last();
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
                if (LastBalance > 0)
                {
                    ReceiptEntry.Balance = LastBalance + ReceiptEntry.Amount;
                }
                else
                {
                    ReceiptEntry.Balance = ReceiptEntry.Amount;
                }

                ReceiptEntry.Save();
            }
            return(Json(new { data = bObj }, JsonRequestBehavior.AllowGet));
        }
        // Insert Into Balance Stement After Registration from Calaculation start Date
        public ActionResult InsertIntoBalanceStementAfterRegistration(DateTime CalaculationStartFrom, int OrgId)
        {
            BalanceStatement bObj = new BalanceStatement();


            List <BalanceStatement>       jObjList      = new List <BalanceStatement>();
            List <HG_Orders>              OrdersDetails = new List <HG_Orders>();
            List <HG_OrganizationDetails> OrgList       = new List <HG_OrganizationDetails>();
            List <HG_Orders>              OrdrList      = new List <HG_Orders>();

            OrdrList = new HG_Orders().GetAll(OrgId, Status: 3);
            List <PaytmResn> Onlinepayment = new List <PaytmResn>();
            int    LastEntryNo             = 0;
            double LastBalance             = 0.00;

            foreach (var ord in OrdrList)
            {
                double totalAmt = 0.00;
                List <HG_OrderItem> orderitemlist = new HG_OrderItem().GetAll(ord.OID);
                orderitemlist = orderitemlist.FindAll(x => x.Status == 3 && x.OrderDate.Date >= CalaculationStartFrom.Date);//only completed items


                if (ord.PaymentStatus == 1 || ord.PaymentStatus == 2)
                {
                    for (int i = 0; i < orderitemlist.Count; i++)
                    {
                        OrdrList = new HG_Orders().GetAll(orderitemlist[i].OrgId, Status: 3);
                        HG_Orders Ord = OrdrList.Find(x => x.OID == orderitemlist[i].OID);

                        totalAmt += Ord.DeliveryCharge;
                        totalAmt += orderitemlist[i].Count * orderitemlist[i].Price;
                    }
                    BalanceStatement Obj          = new BalanceStatement();
                    HG_Orders        od           = new HG_Orders().GetOne(ord.OID);
                    Ledger           LedgerDetail = Ledger.GetAllList().Where(x => x.DebtorType == 1 &&
                                                                              x.OrgId == OrgId).FirstOrDefault();

                    if (od.PaymentStatus == 1 || od.PaymentStatus == 2)
                    {
                        double amt = (totalAmt * LedgerDetail.MarginOnCash) / 100;
                        Obj.CRAmount  = (amt) + ((amt * LedgerDetail.TaxOnAboveMargin) / 100);
                        Obj.TaxOnCash = LedgerDetail.TaxOnAboveMargin;
                    }

                    Obj.Date = ord.Create_Date;

                    // Obj.Amount = Amount;
                    BalanceStatement TBalance = BalanceStatement.GetAllForBalanceCalculation(OrgId).Last();
                    Obj.Balance   = TBalance.Balance - Obj.CRAmount;
                    Obj.OrgId     = OrgId;
                    Obj.OrderId   = ord.OID;
                    Obj.Narration = "Commission of Order No." + ord.OID;
                    Obj.SaveCRValue();
                }
                else if (ord.PaymentStatus == 3)
                {
                    Onlinepayment = PaytmResn.GetAll();
                    PaytmResn paytmTxn = Onlinepayment.Find(x => x.OID == ord.OID);

                    totalAmt += ord.DeliveryCharge;
                    totalAmt += Convert.ToDouble(paytmTxn.PaidAmount);


                    if (Onlinepayment.Count > 0)
                    {
                        Ledger LedgerDetails = Ledger.GetAllList().Where(x => x.DebtorType == 1 &&
                                                                         x.OrgId == OrgId).FirstOrDefault();

                        bObj.Date    = ord.Create_Date;
                        bObj.Amount  = totalAmt;
                        bObj.OrgId   = LedgerDetails.OrgId;
                        bObj.OrderId = orderitemlist[0].OID;

                        BalanceStatement TotalBalance = BalanceStatement.GetAllForBalanceCalculation(OrgId).Last();
                        bObj.Balance = TotalBalance.Balance + totalAmt;

                        bObj.Narration = "Online Payment of Order No." + orderitemlist[0].OID;
                        bObj.isCash    = false;

                        bObj.SaveCRValue();

                        //===============================================================================

                        BalanceStatement Obj          = new BalanceStatement();
                        HG_Orders        od           = new HG_Orders().GetOne(bObj.OrderId);
                        Ledger           LedgerDetail = Ledger.GetAllList().Where(x => x.DebtorType == 1 &&
                                                                                  x.OrgId == OrgId).FirstOrDefault();

                        if (od.PaymentStatus == 3)
                        {
                            double amt = (bObj.Amount * LedgerDetails.MarginOnline) / 100;
                            Obj.CRAmount    = (amt) + ((amt * LedgerDetails.TaxOnAboveMarginOnline) / 100);
                            Obj.TaxOnOnline = LedgerDetails.TaxOnAboveMarginOnline;

                            if (ord.PaymentStatus == 3 && ord.Status == "4")
                            {
                                double RefundAmt           = bObj.Amount - totalAmt;
                                double GetRefundAmt        = (RefundAmt * 2) / 100;
                                double GetRefundAmtwithTax = GetRefundAmt + ((GetRefundAmt * LedgerDetails.TaxOnAboveMarginOnline) / 100);
                                Obj.CRAmount = Obj.CRAmount + GetRefundAmtwithTax;
                            }
                        }
                        Obj.Date = bObj.Date;

                        // Obj.Amount = Amount;
                        BalanceStatement TBalance = BalanceStatement.GetAllForBalanceCalculation(OrgId).Last();
                        Obj.Balance   = TBalance.Balance - Obj.CRAmount;
                        Obj.OrgId     = OrgId;
                        Obj.OrderId   = bObj.OrderId;
                        Obj.Narration = "Commission of Order No." + bObj.OrderId;
                        Obj.SaveCRValue();
                    }
                }
            }
            return(Json(new { data = bObj }, JsonRequestBehavior.AllowGet));
        }