Example #1
0
        public IEnumerable <LedgerList> GetPersonPendingBills(int LedgerHeaderId, int LedgerAccountId, string ReferenceType, string term, int Limit)
        {
            LedgerHeader Ledger = new LedgerHeaderService(_unitOfWork).Find(LedgerHeaderId);

            var Settings = new LedgerSettingService(_unitOfWork).GetLedgerSettingForDocument(Ledger.DocTypeId, Ledger.DivisionId, Ledger.SiteId);

            IEnumerable <LedgerList> PendingBillList = new List <LedgerList>();

            if (!string.IsNullOrEmpty(Settings.SqlProcReferenceNo))
            {
                SqlParameter SqlParameterLedgerAccountId = new SqlParameter("@LedgerAccountId", LedgerAccountId);
                SqlParameter SqlParameterReferenceType   = new SqlParameter("@ReferenceType", ReferenceType);
                SqlParameter SqlParameterLimit           = new SqlParameter("@Limit", Limit);
                SqlParameter SqlParameterTerm            = new SqlParameter("@Term", term);

                PendingBillList = db.Database.SqlQuery <LedgerList>("" + Settings.SqlProcReferenceNo + " @LedgerAccountId, @ReferenceType, @Limit, @Term", SqlParameterLedgerAccountId, SqlParameterReferenceType, SqlParameterLimit, SqlParameterTerm).ToList();
            }

            return(PendingBillList);
        }
Example #2
0
        public IQueryable <ComboBoxResult> GetLedgerIds_Adusted(int?Id, string Nature, int filter3, string term)
        {
            var Header = new LedgerHeaderService(_unitOfWork).Find(filter3);

            int CurrentSiteId     = (int)System.Web.HttpContext.Current.Session["SiteId"];
            int CurrentDivisionId = (int)System.Web.HttpContext.Current.Session["DivisionId"];

            string DivId  = "|" + CurrentDivisionId.ToString() + "|";
            string SiteId = "|" + CurrentSiteId.ToString() + "|";

            var Settings = new LedgerSettingService(_unitOfWork).GetLedgerSettingForDocument(Header.DocTypeId, Header.DivisionId, Header.SiteId);

            string[] ContraAccGroups = null;
            if (!string.IsNullOrEmpty(Settings.filterLedgerAccountGroupLines))
            {
                ContraAccGroups = Settings.filterLedgerAccountGroupLines.Split(",".ToCharArray());
            }
            else
            {
                ContraAccGroups = new string[] { "NA" };
            }

            string[] ExcludeContraAccGroups = null;
            if (!string.IsNullOrEmpty(Settings.filterExcludeLedgerAccountGroupLines))
            {
                ExcludeContraAccGroups = Settings.filterExcludeLedgerAccountGroupLines.Split(",".ToCharArray());
            }
            else
            {
                ExcludeContraAccGroups = new string[] { "NA" };
            }


            SqlParameter SqlParameterLedgerAccountId = new SqlParameter("@LedgerAccountId", Id);
            SqlParameter SqlParameterNature          = new SqlParameter("@Nature", Nature);


            var PendingLedgerViewModel = db.Database.SqlQuery <PendingLedgerViewModel>("" + ConfigurationManager.AppSettings["DataBaseSchema"] + ".sp_GetLedgerToAdjust @LedgerAccountId, @Nature", SqlParameterLedgerAccountId, SqlParameterNature).ToList().AsQueryable();


            return(from p in PendingLedgerViewModel
                   where (string.IsNullOrEmpty(term) ? 1 == 1 : p.LedgerHeaderDocNo.ToLower().Contains(term.ToLower()) ||
                          string.IsNullOrEmpty(term) ? 1 == 1 : p.PartyDocNo.ToLower().Contains(term.ToLower()) ||
                          string.IsNullOrEmpty(term) ? 1 == 1 : p.LedgerAccountName.ToLower().Contains(term.ToLower()))
                   select new ComboBoxResult
            {
                id = p.LedgerId.ToString(),
                text = p.LedgerHeaderDocNo,
                AProp1 = p.LedgerAccountName,
                AProp2 = "Party Doc No : " + p.PartyDocNo + ", Party Doc Date : " + p.PartyDocDate,
                TextProp1 = "Balance Amount : " + p.BalanceAmount,
                TextProp2 = "Bill Amount : " + p.BillAmount
            });


            //return (from p in PendingLedgerViewModel
            //        join A in db.LedgerAccount on p.LedgerAccountId equals A.LedgerAccountId into LedgerAccountTable
            //        from LedgerAccountTab in LedgerAccountTable.DefaultIfEmpty()
            //        where (string.IsNullOrEmpty(term) ? 1 == 1 : p.LedgerHeaderDocNo.ToLower().Contains(term.ToLower())
            //        || string.IsNullOrEmpty(term) ? 1 == 1 : p.PartyDocNo.ToLower().Contains(term.ToLower())
            //        || string.IsNullOrEmpty(term) ? 1 == 1 : p.LedgerAccountName.ToLower().Contains(term.ToLower()))
            //        && (string.IsNullOrEmpty(Settings.filterLedgerAccountGroupLines) ? 1 == 1 : ContraAccGroups.Contains(LedgerAccountTab.LedgerAccountGroupId.ToString()))
            //        && (string.IsNullOrEmpty(Settings.filterExcludeLedgerAccountGroupLines) ? 1 == 1 : !ExcludeContraAccGroups.Contains(LedgerAccountTab.LedgerAccountGroupId.ToString()))
            //        && LedgerAccountTab.IsActive == true
            //        select new ComboBoxResult
            //        {
            //            id = p.LedgerId.ToString(),
            //            text = p.LedgerHeaderDocNo,
            //            AProp1 = p.LedgerAccountName,
            //            AProp2 = "Party Doc No : " + p.PartyDocNo + ", Party Doc Date : " + p.PartyDocDate,
            //            TextProp1 = "Balance Amount : " + p.BalanceAmount,
            //            TextProp2 = "Bill Amount : " + p.BillAmount
            //        }).ToList().AsQueryable();
        }
Example #3
0
        public string LedgerPost(IEnumerable <LedgerViewModel> LedgerViewModel)
        {
            string ErrorText = "";

            var ledgertemp = (from L in LedgerViewModel
                              group new { L } by new { L.DocHeaderId } into Result
                              select new
            {
                DocHeadewrId = Result.Key.DocHeaderId,
                TotalDrAmt = Result.Sum(i => i.L.AmtDr),
                TotalCrAmt = Result.Sum(i => i.L.AmtCr)
            }).FirstOrDefault();

            if (ledgertemp.TotalCrAmt != ledgertemp.TotalDrAmt)
            {
                ErrorText = "Debit and credit side amounts are not equal.";
                return(ErrorText);
            }

            //Get Data from Ledger VIew Model and post in ledger header model for insertion or updation
            LedgerHeader LedgerHeader = (from L in LedgerViewModel
                                         select new LedgerHeader
            {
                DocHeaderId = L.DocHeaderId,
                DocTypeId = L.DocTypeId,
                DocDate = L.DocDate,
                DocNo = L.DocNo,
                DivisionId = L.DivisionId,
                SiteId = L.SiteId,
                LedgerAccountId = L.HeaderLedgerAccountId,
                CreditDays = L.CreditDays,
                Narration = L.HeaderNarration,
                Remark = L.Remark,
                Status = L.Status,
                CreatedBy = L.CreatedBy,
                CreatedDate = L.CreatedDate,
                ModifiedBy = L.ModifiedBy,
                ModifiedDate = L.ModifiedDate
            }).FirstOrDefault();

            //For Checking that this Doc Header Id already exist in Ledger Header
            var temp = new LedgerHeaderService(_unitOfWork).FindByDocHeader(LedgerHeader.DocHeaderId, LedgerHeader.DocTypeId, LedgerHeader.SiteId, LedgerHeader.DivisionId);

            //if record is found in Ledger Header Table then it will edit it if data is not found in ledger header table than it will add a new record.
            if (temp == null)
            {
                new LedgerHeaderService(_unitOfWork).Create(LedgerHeader);

                LedgerHeader H = new LedgerHeader();

                H.DocHeaderId     = LedgerHeader.DocHeaderId;
                H.DocTypeId       = LedgerHeader.DocTypeId;
                H.DocDate         = LedgerHeader.DocDate;
                H.DocNo           = LedgerHeader.DocNo;
                H.DivisionId      = LedgerHeader.DivisionId;
                H.SiteId          = LedgerHeader.SiteId;
                H.LedgerAccountId = LedgerHeader.LedgerAccountId;
                H.CreditDays      = LedgerHeader.CreditDays;
                H.Narration       = LedgerHeader.Narration;
                H.Remark          = LedgerHeader.Remark;
                H.Status          = LedgerHeader.Status;
                H.CreatedBy       = LedgerHeader.CreatedBy;
                H.CreatedDate     = LedgerHeader.CreatedDate;
                H.ModifiedBy      = LedgerHeader.ModifiedBy;
                H.ModifiedDate    = LedgerHeader.ModifiedDate;
            }
            else
            {
                DeleteLedgerForLedgerHeader(LedgerHeader.LedgerHeaderId);

                temp.DocHeaderId     = LedgerHeader.DocHeaderId;
                temp.DocTypeId       = LedgerHeader.DocTypeId;
                temp.DocDate         = LedgerHeader.DocDate;
                temp.DocNo           = LedgerHeader.DocNo;
                temp.DivisionId      = LedgerHeader.DivisionId;
                temp.SiteId          = LedgerHeader.SiteId;
                temp.LedgerAccountId = LedgerHeader.LedgerAccountId;
                temp.CreditDays      = LedgerHeader.CreditDays;
                temp.Narration       = LedgerHeader.Narration;
                temp.Remark          = LedgerHeader.Remark;
                temp.Status          = LedgerHeader.Status;
                temp.CreatedBy       = LedgerHeader.CreatedBy;
                temp.CreatedDate     = LedgerHeader.CreatedDate;
                temp.ModifiedBy      = LedgerHeader.ModifiedBy;
                temp.ModifiedDate    = LedgerHeader.ModifiedDate;

                new LedgerHeaderService(_unitOfWork).Update(temp);
            }

            IEnumerable <Ledger> LedgerList = (from L in LedgerViewModel
                                               group new { L } by new { L.LedgerAccountId, L.ContraLedgerAccountId, L.CostCenterId } into Result
                                               select new Ledger
            {
                LedgerAccountId = Result.Key.LedgerAccountId,
                ContraLedgerAccountId = Result.Key.ContraLedgerAccountId,
                CostCenterId = Result.Key.CostCenterId,
                AmtDr = Result.Sum(i => i.L.AmtDr),
                AmtCr = Result.Sum(i => i.L.AmtCr),
                Narration = Result.Max(i => i.L.Narration)
            }).ToList();

            foreach (Ledger item in LedgerList)
            {
                if (temp != null)
                {
                    item.LedgerHeaderId = temp.LedgerHeaderId;
                }

                Create(item);
            }

            return(ErrorText);
        }
Example #4
0
        public string LedgerPost(LedgerViewModel LedgerViewModel_New, LedgerViewModel LedgerViewModel_Old)
        {
            string       ErrorText = "";
            LedgerHeader LedgerHeader;


            if (LedgerViewModel_New != null)
            {
                LedgerHeader = new LedgerHeaderService(_unitOfWork).FindByDocHeader(LedgerViewModel_New.DocHeaderId, LedgerViewModel_New.DocTypeId, LedgerViewModel_New.SiteId, LedgerViewModel_New.DivisionId);

                if (LedgerHeader == null)
                {
                    LedgerHeader H = new LedgerHeader();

                    H.DocHeaderId     = LedgerHeader.DocHeaderId;
                    H.DocTypeId       = LedgerHeader.DocTypeId;
                    H.DocDate         = LedgerHeader.DocDate;
                    H.DocNo           = LedgerHeader.DocNo;
                    H.DivisionId      = LedgerHeader.DivisionId;
                    H.SiteId          = LedgerHeader.SiteId;
                    H.LedgerAccountId = LedgerHeader.LedgerAccountId;
                    H.CreditDays      = LedgerHeader.CreditDays;
                    H.Narration       = LedgerHeader.Narration;
                    H.Remark          = LedgerHeader.Remark;
                    H.Status          = LedgerHeader.Status;
                    H.CreatedBy       = LedgerHeader.CreatedBy;
                    H.CreatedDate     = LedgerHeader.CreatedDate;
                    H.ModifiedBy      = LedgerHeader.ModifiedBy;
                    H.ModifiedDate    = LedgerHeader.ModifiedDate;

                    new LedgerHeaderService(_unitOfWork).Create(H);
                }
                else
                {
                    LedgerHeader.DocHeaderId  = LedgerViewModel_New.DocHeaderId;
                    LedgerHeader.DocTypeId    = LedgerViewModel_New.DocTypeId;
                    LedgerHeader.DocDate      = LedgerViewModel_New.DocDate;
                    LedgerHeader.DocNo        = LedgerViewModel_New.DocNo;
                    LedgerHeader.DivisionId   = LedgerViewModel_New.DivisionId;
                    LedgerHeader.SiteId       = LedgerViewModel_New.SiteId;
                    LedgerHeader.Remark       = LedgerViewModel_New.Remark;
                    LedgerHeader.Status       = LedgerViewModel_New.Status;
                    LedgerHeader.CreatedBy    = LedgerViewModel_New.CreatedBy;
                    LedgerHeader.CreatedDate  = LedgerViewModel_New.CreatedDate;
                    LedgerHeader.ModifiedBy   = LedgerViewModel_New.ModifiedBy;
                    LedgerHeader.ModifiedDate = LedgerViewModel_New.ModifiedDate;

                    new LedgerHeaderService(_unitOfWork).Update(LedgerHeader);
                }
            }
            else
            {
                LedgerHeader = new LedgerHeaderService(_unitOfWork).FindByDocHeader(LedgerViewModel_Old.DocHeaderId, LedgerViewModel_Old.DocTypeId, LedgerViewModel_Old.SiteId, LedgerViewModel_Old.DivisionId);
            }

            if (LedgerViewModel_Old != null)
            {
                Ledger Ledger_Old = Find(LedgerHeader.LedgerHeaderId, LedgerViewModel_Old.LedgerAccountId, LedgerViewModel_Old.ContraLedgerAccountId, LedgerViewModel_Old.CostCenterId);

                Ledger_Old.AmtCr = Ledger_Old.AmtCr - LedgerViewModel_Old.AmtCr;
                Ledger_Old.AmtDr = Ledger_Old.AmtDr - LedgerViewModel_Old.AmtDr;

                if (Ledger_Old.AmtCr == 0 && Ledger_Old.AmtDr == 0)
                {
                    Delete(Ledger_Old);
                }
                else
                {
                    Update(Ledger_Old);
                }
            }

            if (LedgerViewModel_New != null)
            {
                Ledger Ledger_New;

                if (LedgerHeader != null)
                {
                    Ledger_New = Find(LedgerHeader.LedgerHeaderId, LedgerViewModel_New.LedgerAccountId, LedgerViewModel_New.ContraLedgerAccountId, LedgerViewModel_New.CostCenterId);
                }
                else
                {
                    Ledger_New = null;
                }

                if (Ledger_New == null)
                {
                    Ledger L = new Ledger();

                    L.LedgerAccountId       = LedgerViewModel_New.LedgerAccountId;
                    L.ContraLedgerAccountId = LedgerViewModel_New.ContraLedgerAccountId;
                    L.CostCenterId          = LedgerViewModel_New.CostCenterId;
                    L.AmtDr      = LedgerViewModel_New.AmtDr;
                    L.AmtCr      = LedgerViewModel_New.AmtCr;
                    L.Narration  = LedgerViewModel_New.Narration;
                    L.ContraText = LedgerViewModel_New.ContraText;

                    if (LedgerHeader != null)
                    {
                        L.LedgerHeaderId = LedgerHeader.LedgerHeaderId;
                    }

                    Create(L);
                }
                else
                {
                    Ledger_New.AmtDr = Ledger_New.AmtDr + LedgerViewModel_New.AmtDr;
                    Ledger_New.AmtCr = Ledger_New.AmtCr + LedgerViewModel_New.AmtCr;

                    Update(Ledger_New);
                }
            }

            return(ErrorText);
        }
        public void LedgerPosting(ref LedgerHeaderViewModel LedgerHeaderViewModel, IEnumerable <CalculationHeaderCharge> HeaderTable, IEnumerable <CalculationLineCharge> LineTable)
        {
            int PersonAccountId = 6612;
            int LedgerHeaderId  = 0;

            if (LedgerHeaderViewModel.LedgerHeaderId == 0)
            {
                LedgerHeader LedgerHeader = new LedgerHeader();

                LedgerHeader.DocHeaderId  = LedgerHeaderViewModel.DocHeaderId;
                LedgerHeader.DocTypeId    = LedgerHeaderViewModel.DocTypeId;
                LedgerHeader.ProcessId    = LedgerHeaderViewModel.ProcessId;
                LedgerHeader.DocDate      = LedgerHeaderViewModel.DocDate;
                LedgerHeader.DocNo        = LedgerHeaderViewModel.DocNo;
                LedgerHeader.DivisionId   = LedgerHeaderViewModel.DivisionId;
                LedgerHeader.SiteId       = LedgerHeaderViewModel.SiteId;
                LedgerHeader.Narration    = LedgerHeaderViewModel.Narration;
                LedgerHeader.Remark       = LedgerHeaderViewModel.Remark;
                LedgerHeader.CreatedBy    = LedgerHeaderViewModel.CreatedBy;
                LedgerHeader.CreatedDate  = DateTime.Now.Date;
                LedgerHeader.ModifiedBy   = LedgerHeaderViewModel.ModifiedBy;
                LedgerHeader.ModifiedDate = DateTime.Now.Date;

                new LedgerHeaderService(_unitOfWork).Create(LedgerHeader);
            }
            else
            {
                LedgerHeader LedgerHeader = new LedgerHeaderService(_unitOfWork).Find((int)LedgerHeaderViewModel.LedgerHeaderId);

                LedgerHeader.DocHeaderId  = LedgerHeaderViewModel.DocHeaderId;
                LedgerHeader.DocTypeId    = LedgerHeaderViewModel.DocTypeId;
                LedgerHeader.ProcessId    = LedgerHeaderViewModel.ProcessId;
                LedgerHeader.DocDate      = LedgerHeaderViewModel.DocDate;
                LedgerHeader.DocNo        = LedgerHeaderViewModel.DocNo;
                LedgerHeader.DivisionId   = LedgerHeaderViewModel.DivisionId;
                LedgerHeader.SiteId       = LedgerHeaderViewModel.SiteId;
                LedgerHeader.Narration    = LedgerHeaderViewModel.Narration;
                LedgerHeader.Remark       = LedgerHeaderViewModel.Remark;
                LedgerHeader.ModifiedBy   = LedgerHeaderViewModel.ModifiedBy;
                LedgerHeader.ModifiedDate = DateTime.Now.Date;

                new LedgerHeaderService(_unitOfWork).Update(LedgerHeader);

                IEnumerable <Ledger> LedgerList = new LedgerService(_unitOfWork).FindForLedgerHeader(LedgerHeader.LedgerHeaderId);

                var LedgerAdjDrList = (from H in db.LedgerHeader
                                       join L in db.Ledger on H.LedgerHeaderId equals L.LedgerHeaderId into LedgerTable
                                       from LedgerTab in LedgerTable.DefaultIfEmpty()
                                       join La in db.LedgerAdj on LedgerTab.LedgerId equals La.DrLedgerId into LedgerAdjTable
                                       from LedgerAdjTab in LedgerAdjTable.DefaultIfEmpty()
                                       where H.LedgerHeaderId == LedgerHeader.LedgerHeaderId && LedgerAdjTab.LedgerAdjId != null
                                       select LedgerAdjTab).ToList();

                foreach (LedgerAdj item in LedgerAdjDrList)
                {
                    new LedgerAdjService(_unitOfWork).Delete(item.LedgerAdjId);
                }

                var LedgerAdjCrList = (from H in db.LedgerHeader
                                       join L in db.Ledger on H.LedgerHeaderId equals L.LedgerHeaderId into LedgerTable
                                       from LedgerTab in LedgerTable.DefaultIfEmpty()
                                       join La in db.LedgerAdj on LedgerTab.LedgerId equals La.CrLedgerId into LedgerAdjTable
                                       from LedgerAdjTab in LedgerAdjTable.DefaultIfEmpty()
                                       where H.LedgerHeaderId == LedgerHeader.LedgerHeaderId && LedgerAdjTab.LedgerAdjId != null
                                       select LedgerAdjTab).ToList();

                foreach (LedgerAdj item in LedgerAdjCrList)
                {
                    new LedgerAdjService(_unitOfWork).Delete(item.LedgerAdjId);
                }


                foreach (Ledger item in LedgerList)
                {
                    new LedgerService(_unitOfWork).Delete(item);
                }

                LedgerHeaderId = LedgerHeader.LedgerHeaderId;
            }


            IEnumerable <LedgerPostingViewModel> LedgerHeaderAmtDr = from H in HeaderTable
                                                                     join A in db.LedgerAccount on H.PersonID equals A.PersonId into LedgerAccountTable
                                                                     from LedgerAccountTab in LedgerAccountTable.DefaultIfEmpty()
                                                                     where H.LedgerAccountDrId != null && H.Amount != 0 && H.Amount != null
                                                                     select new LedgerPostingViewModel
            {
                LedgerAccountId       = (int)(H.LedgerAccountDrId == PersonAccountId ? LedgerAccountTab.LedgerAccountId : H.LedgerAccountDrId),
                ContraLedgerAccountId = (H.LedgerAccountCrId == PersonAccountId ? LedgerAccountTab.LedgerAccountId : H.LedgerAccountCrId),
                CostCenterId          = H.CostCenterId,
                AmtDr = Math.Abs((H.Amount > 0 ? H.Amount : 0) ?? 0),
                AmtCr = Math.Abs((H.Amount < 0 ? H.Amount : 0) ?? 0)
            };

            IEnumerable <LedgerPostingViewModel> LedgerHeaderAmtCr = from H in HeaderTable
                                                                     join A in db.LedgerAccount on H.PersonID equals A.PersonId into LedgerAccountTable
                                                                     from LedgerAccountTab in LedgerAccountTable.DefaultIfEmpty()
                                                                     where H.LedgerAccountCrId != null && H.Amount != 0 && H.Amount != null
                                                                     select new LedgerPostingViewModel
            {
                LedgerAccountId       = (int)(H.LedgerAccountCrId == PersonAccountId ? LedgerAccountTab.LedgerAccountId : H.LedgerAccountCrId),
                ContraLedgerAccountId = (H.LedgerAccountDrId == PersonAccountId ? LedgerAccountTab.LedgerAccountId : H.LedgerAccountDrId),
                CostCenterId          = H.CostCenterId,
                AmtCr = Math.Abs((H.Amount > 0 ? H.Amount : 0) ?? 0),
                AmtDr = Math.Abs((H.Amount < 0 ? H.Amount : 0) ?? 0)
            };

            IEnumerable <LedgerPostingViewModel> LedgerLineAmtDr = from L in LineTable
                                                                   join A in db.LedgerAccount on L.PersonID equals A.PersonId into LedgerAccountTable
                                                                   from LedgerAccountTab in LedgerAccountTable.DefaultIfEmpty()
                                                                   where L.LedgerAccountDrId != null && L.Amount != 0 && L.Amount != null
                                                                   select new LedgerPostingViewModel
            {
                LedgerAccountId       = (int)(L.LedgerAccountDrId == PersonAccountId ? LedgerAccountTab.LedgerAccountId : L.LedgerAccountDrId),
                ContraLedgerAccountId = (L.LedgerAccountCrId == PersonAccountId ? LedgerAccountTab.LedgerAccountId : L.LedgerAccountCrId),
                CostCenterId          = L.CostCenterId,
                AmtDr = Math.Abs((L.Amount > 0 ? L.Amount : 0) ?? 0),
                AmtCr = Math.Abs((L.Amount < 0 ? L.Amount : 0) ?? 0)
            };

            IEnumerable <LedgerPostingViewModel> LedgerLineAmtCr = from L in LineTable
                                                                   join A in db.LedgerAccount on L.PersonID equals A.PersonId into LedgerAccountTable
                                                                   from LedgerAccountTab in LedgerAccountTable.DefaultIfEmpty()
                                                                   where L.LedgerAccountCrId != null && L.Amount != 0 && L.Amount != null
                                                                   select new LedgerPostingViewModel
            {
                LedgerAccountId       = (int)(L.LedgerAccountCrId == PersonAccountId ? LedgerAccountTab.LedgerAccountId : L.LedgerAccountCrId),
                ContraLedgerAccountId = (L.LedgerAccountDrId == PersonAccountId ? LedgerAccountTab.LedgerAccountId : L.LedgerAccountDrId),
                CostCenterId          = L.CostCenterId,
                AmtCr = Math.Abs((L.Amount > 0 ? L.Amount : 0) ?? 0),
                AmtDr = Math.Abs((L.Amount < 0 ? L.Amount : 0) ?? 0)
            };


            IEnumerable <LedgerPostingViewModel> LedgerCombind = LedgerHeaderAmtDr.Union(LedgerHeaderAmtCr).Union(LedgerLineAmtDr).Union(LedgerLineAmtCr);

            //IEnumerable<LedgerPostingViewModel> LedgerPost = from L in LedgerCombind
            //                                                 group new { L } by new { L.LedgerAccountId, L.ContraLedgerAccountId, L.CostCenterId } into Result
            //                                                 select new LedgerPostingViewModel
            //                                                 {
            //                                                     LedgerAccountId = Result.Key.LedgerAccountId,
            //                                                     ContraLedgerAccountId = Result.Key.ContraLedgerAccountId,
            //                                                     CostCenterId = Result.Key.CostCenterId,
            //                                                     AmtDr = Result.Sum(i => i.L.AmtDr),
            //                                                     AmtCr = Result.Sum(i => i.L.AmtCr)
            //                                                 };


            IEnumerable <LedgerPostingViewModel> LedgerPost1 = from L in LedgerCombind
                                                               join Ca in db.LedgerAccount on L.ContraLedgerAccountId equals Ca.LedgerAccountId into ContraLedgerAccountTable
                                                               from ContraLedgerAccountTab in ContraLedgerAccountTable.DefaultIfEmpty()
                                                               join Cag in db.LedgerAccountGroup on ContraLedgerAccountTab.LedgerAccountGroupId equals Cag.LedgerAccountGroupId into ContraLedgerAccountGroupTable
                                                               from ContraLedgerAccountGroupTab in ContraLedgerAccountGroupTable.DefaultIfEmpty()
                                                               group new { L, ContraLedgerAccountGroupTab } by new { L.LedgerAccountId, L.ContraLedgerAccountId, L.CostCenterId } into Result
                                                 select new LedgerPostingViewModel
            {
                LedgerAccountId              = Result.Key.LedgerAccountId,
                ContraLedgerAccountId        = Result.Key.ContraLedgerAccountId,
                ContraLedgerAccountWeightage = Result.Max(i => i.ContraLedgerAccountGroupTab.Weightage ?? 0),
                CostCenterId = Result.Key.CostCenterId,
                AmtDr        = Result.Sum(i => i.L.AmtDr),
                AmtCr        = Result.Sum(i => i.L.AmtCr)
            };


            IEnumerable <LedgerPostingViewModel> LedgerPost2 = from L in LedgerPost1
                                                               group new { L } by new { L.LedgerAccountId, L.CostCenterId } into Result
                                                 select new LedgerPostingViewModel
            {
                LedgerAccountId = Result.Key.LedgerAccountId,
                ContraLedgerAccountWeightage = Result.Max(i => i.L.ContraLedgerAccountWeightage ?? 0),
                CostCenterId = Result.Key.CostCenterId,
                AmtDr        = Result.Sum(i => i.L.AmtDr) - Result.Sum(i => i.L.AmtCr) > 0 ? Result.Sum(i => i.L.AmtDr) - Result.Sum(i => i.L.AmtCr) : 0,
                AmtCr        = Result.Sum(i => i.L.AmtCr) - Result.Sum(i => i.L.AmtDr) > 0 ? Result.Sum(i => i.L.AmtCr) - Result.Sum(i => i.L.AmtDr) : 0,
            };

            IEnumerable <LedgerPostingViewModel> LedgerPost = from L2 in LedgerPost2
                                                              join L1 in LedgerPost1 on new { A1 = L2.LedgerAccountId, A2 = L2.CostCenterId, A3 = L2.ContraLedgerAccountWeightage } equals new { A1 = L1.LedgerAccountId, A2 = L1.CostCenterId, A3 = L1.ContraLedgerAccountWeightage } into LedgerPost1Table
            from LedgerPost1Tab in LedgerPost1Table.DefaultIfEmpty()
            group new { LedgerPost1Tab, L2 } by new { L2.LedgerAccountId, L2.CostCenterId } into Result
                                                 select new LedgerPostingViewModel
            {
                LedgerAccountId       = Result.Key.LedgerAccountId,
                ContraLedgerAccountId = Result.Max(i => i.LedgerPost1Tab.ContraLedgerAccountId),
                CostCenterId          = Result.Key.CostCenterId,
                AmtDr = Result.Max(i => i.L2.AmtDr),
                AmtCr = Result.Max(i => i.L2.AmtCr)
            };


            var temp = (from L in LedgerPost
                        group L by 1 into Result
                        select new
            {
                AmtDr = Result.Sum(i => i.AmtDr),
                AmtCr = Result.Sum(i => i.AmtCr)
            }).FirstOrDefault();


            if (temp != null)
            {
                if (temp.AmtDr != temp.AmtCr)
                {
                    throw new Exception("Debit amount and credit amount is not equal.Check account posting.");
                }
            }



            foreach (LedgerPostingViewModel item in LedgerPost)
            {
                Ledger Ledger = new Ledger();

                if (LedgerHeaderId != 0)
                {
                    Ledger.LedgerHeaderId = LedgerHeaderId;
                }
                Ledger.LedgerAccountId       = item.LedgerAccountId;
                Ledger.ContraLedgerAccountId = item.ContraLedgerAccountId;


                var TempCostCenter = (from C in db.CostCenter
                                      where C.CostCenterId == item.CostCenterId && C.LedgerAccountId == item.LedgerAccountId
                                      select new { CostCenterId = C.CostCenterId }).FirstOrDefault();

                if (TempCostCenter != null)
                {
                    Ledger.CostCenterId = item.CostCenterId;
                }



                Ledger.AmtDr     = item.AmtDr * (LedgerHeaderViewModel.ExchangeRate ?? 1);
                Ledger.AmtCr     = item.AmtCr * (LedgerHeaderViewModel.ExchangeRate ?? 1);
                Ledger.Narration = "";

                new LedgerService(_unitOfWork).Create(Ledger);
            }
        }