private BagTransaction GetBagDues(int partyId)
        {
            var            comRcvBag  = context.BagTransactions.Where(bb => bb.partyId == partyId).Sum(ss => ss.comRcvBag);
            var            comSentBag = context.BagTransactions.Where(bb => bb.partyId == partyId).Sum(ss => ss.comSentBag);
            var            bagDues    = comRcvBag ?? 0 - comSentBag ?? 0;
            BagTransaction bt         = new BagTransaction();

            bt.comRcvBag  = comRcvBag ?? 0;
            bt.comSentBag = comSentBag ?? 0;
            bt.bagDues    = bagDues;
            return(bt);
        }
Exemple #2
0
        public BagTransaction GetRemainingBag(int partyId)
        {
            var results = from sack in context.BagTransactions
                          join pr in context.tblParties on sack.partyId equals pr.ID
                          where sack.partyId == partyId && (sack.rcvPrice == 0 || sack.rcvPrice == null) && (sack.sentPrice == 0 || sack.sentPrice == null)
                          orderby sack.date descending, sack.ID descending
                select new
            {
                sack.ID,
                sack.partyId,
                pr.name,
                sack.comRcvBag,
                sack.comSentBag,
                sack.bagDues,
                sack.date
            };
            int bgdue = 0;

            foreach (var item in results)
            {
                bgdue += item.comRcvBag ?? 0 - item.comSentBag ?? 0;
            }
            var pric = (from sack in context.BagTransactions
                        join pr in context.tblParties on sack.partyId equals pr.ID
                        where sack.partyId == partyId && (sack.rcvPrice > 0 || sack.sentPrice > 0)
                        orderby sack.date descending, sack.ID descending
                        select sack);


            double price = 0;

            foreach (var item in pric)
            {
                price += item.comRcvBag ?? 0 * item.rcvPrice ?? 0 - item.comSentBag ?? 0 * item.sentPrice ?? 0;
            }

            //var item = results.FirstOrDefault();
            BagTransaction objSackInfo = new BagTransaction();

            //if (item == null && price==null)
            //{
            //    return objSackInfo;
            //}
            //objSackInfo.ID = item==null?price.ID: item.ID;
            //objSackInfo.partyId = item==null?price.partyId: item.partyId;
            //objSackInfo.partyName = item==null?price.partyName: item.name;
            objSackInfo.priceDues = price;
            //objSackInfo.date =item==null?price.date:item.date;
            objSackInfo.bagDues = bgdue;
            return(objSackInfo);
        }
Exemple #3
0
        public BagTransaction SaveBag(BagTransaction objBag)
        {
            long maxId = context.BagTransactions.Select(s => s.ID).DefaultIfEmpty(0).Max();

            var results = from sack in context.BagTransactions
                          where sack.partyId == objBag.partyId.Value && sack.date <= objBag.date && (sack.rcvPrice == 0 || sack.rcvPrice == null) && (sack.sentPrice == 0 || sack.sentPrice == null)
                          orderby sack.date descending, sack.ID descending
            select sack;
            var prevDue = results.FirstOrDefault();

            var pric = (from sack in context.BagTransactions
                        where sack.partyId == objBag.partyId.Value && sack.date <= objBag.date && (sack.rcvPrice > 0 || sack.sentPrice > 0)
                        orderby sack.date descending, sack.ID descending
                        select sack);
            var prevprice = pric.FirstOrDefault();

            if (objBag.sentPrice > 0)
            {
                int bag = objBag.comSentBag > 0 ? objBag.comSentBag.Value : 1;
                objBag.priceDues = prevprice.priceDues - objBag.sentPrice * bag;

                var nextprices = from sack in context.BagTransactions
                                 where sack.partyId == objBag.partyId.Value && sack.date > objBag.date && (sack.rcvPrice > 0 || sack.sentPrice > 0)
                                 select sack;
                foreach (var item in nextprices)
                {
                    item.priceDues -= objBag.sentPrice * bag;
                }
            }
            else
            {
                objBag.bagDues = prevDue.bagDues - objBag.comSentBag;

                var nextBags = from sack in context.BagTransactions
                               where sack.partyId == objBag.partyId.Value && sack.date > objBag.date && (sack.rcvPrice == 0 || sack.rcvPrice == null) && (sack.sentPrice == 0 || sack.sentPrice == null)
                               select sack;
                foreach (var item in nextBags)
                {
                    item.bagDues -= objBag.comSentBag;
                }
            }

            objBag.ID = ++maxId;
            context.BagTransactions.Add(objBag);
            return(context.SaveChanges() > 0 ? objBag : null);
        }
Exemple #4
0
 public JsonResult SaveBag(BagTransaction objBag)
 {
     if (Session["role"] == null)
     {
         Session["userId"] = null;
         Session["role"]   = null;
         return(Json(null, JsonRequestBehavior.AllowGet));
     }
     else if (Session["role"].ToString() == "Admin" || Session["role"].ToString() == "Super Admin")
     {
         return(Json(duePaymentRepository.SaveBag(objBag), JsonRequestBehavior.AllowGet));
     }
     else
     {
         Session["userId"] = null;
         Session["role"]   = null;
         return(Json(null, JsonRequestBehavior.AllowGet));
     }
 }
        public ReportViewModel GetReportViewModel(List <object> objLst, int partyId, string fromDate, string toDate)
        {
            var reportViewModel = new ReportViewModel()
            {
                LeftMainTitle = "Union Group",
                LeftSubTitle  = "IT",
                Name          = "Sales Report",
                //ReportDate = DateTime.Now,
                ReportLogo = "~/Content/logo.jpg",
                //ReportTitle = "Retailerwise Sales Report",
                ReportLanguage = "en-US",
                //UserNamPrinting = UserPrinting,
                Format           = ReportViewModel.ReportFormat.PDF,
                ViewAsAttachment = false
            };
            tblParty party   = context.tblParties.Where(pp => pp.ID == partyId).FirstOrDefault();
            string   pname   = party.name ?? "N/A";
            string   area    = party.area ?? "N/A";
            string   contact = party.contactNo != null?party.contactNo.ToString() : "N/A";

            double         dues    = GetDues(partyId, Convert.ToDateTime(fromDate), Convert.ToDateTime(toDate));
            double         prevDue = GetDuesOnDate(partyId, Convert.ToDateTime(fromDate));
            BagTransaction bagDue  = GetBagDues(partyId);

            //adding the dataset information to the report view model object
            reportViewModel.ReportDataSets.Add(new ReportViewModel.ReportDataSet()
            {
                DataSetData = objLst, DatasetName = "DataSet_GetPaddyInfo"
            });
            reportViewModel.ReportParams.Add(new ReportParameter("dues", dues.ToString()));
            reportViewModel.ReportParams.Add(new ReportParameter("prevDue", prevDue.ToString()));
            reportViewModel.ReportParams.Add(new ReportParameter("comRcvBag", bagDue.comRcvBag.ToString()));
            reportViewModel.ReportParams.Add(new ReportParameter("comSentBag", bagDue.comSentBag.ToString()));
            reportViewModel.ReportParams.Add(new ReportParameter("bagDues", bagDue.bagDues.ToString()));
            reportViewModel.ReportParams.Add(new ReportParameter("partyName", pname));
            reportViewModel.ReportParams.Add(new ReportParameter("address", area));
            reportViewModel.ReportParams.Add(new ReportParameter("phone", contact));
            reportViewModel.ReportParams.Add(new ReportParameter("startDate", fromDate));
            reportViewModel.ReportParams.Add(new ReportParameter("endDate", toDate));
            return(reportViewModel);
        }
        // just try to avoid opening values
        public long SavePaddy(tblBuy paddyInfo)
        {
            try
            {
                long maxId = context.tblBuys.Select(p => p.ID).DefaultIfEmpty(0).Max();
                paddyInfo.ID = ++maxId;
                if (paddyInfo.amount > 0 || paddyInfo.labourCostPerBag > 0 || paddyInfo.transportCost > 0)
                {
                    #region costing source
                    long             cstId   = context.tblCostingSources.Select(i => i.ID).DefaultIfEmpty(0).Max();
                    tblCostingSource costObj = new tblCostingSource();
                    costObj.ID             = ++cstId;
                    costObj.sourceName     = "ধান"; // shoul be come from commonelement
                    costObj.srcDescId      = 23;    // should be come from commonelemnt
                    costObj.srcDescription = "ধান ক্রয় বাবদ খরচ";
                    // costObj.amount =  paddyInfo.amount + labCost* paddyInfo.noOfBag+ paddyInfo.transportCost ?? 0;
                    costObj.labourCostPerBag = paddyInfo.labourCostPerBag ?? 0;
                    if (paddyInfo.transportCostInclude)
                    {
                        costObj.transportCost = paddyInfo.transportCost ?? 0;
                    }
                    else
                    {
                        costObj.transportCost = 0;
                    }

                    costObj.amount  = paddyInfo.amount;
                    costObj.date    = paddyInfo.date;
                    costObj.partyId = paddyInfo.partyId;
                    costObj.buyId   = paddyInfo.ID;
                    context.tblCostingSources.Add(costObj);

                    #endregion
                }

                #region save dues
                long   maxdueId = context.tblDues.Select(i => i.ID).DefaultIfEmpty(0).Max();
                tblDue objDue   = new tblDue();
                objDue.ID      = ++maxdueId;
                objDue.partyId = paddyInfo.partyId;
                objDue.buyId   = paddyInfo.ID;
                objDue.date    = paddyInfo.date;

                //tblDue dueItem = context.tblDues.Where(bb => bb.date <= paddyInfo.date && bb.partyId == paddyInfo.partyId).OrderByDescending(oo => oo.date).ThenByDescending(ii => ii.ID).FirstOrDefault();
                //List<tblDue> dueItems = context.tblDues.Where(bb => bb.date > paddyInfo.date && bb.partyId == paddyInfo.partyId).ToList();
                double?totalPr = paddyInfo.price * paddyInfo.quantityPerBag / 40 * paddyInfo.noOfBag;
                var    due     = totalPr - paddyInfo.amount;
                objDue.amount = due;
                //if (dueItem == null)
                //{
                //    objDue.openingBalance = objDue.amount;
                //}
                //else
                //{
                //    objDue.openingBalance = dueItem.openingBalance + objDue.amount;
                //}
                //if (dueItems != null)
                //{
                //    foreach (var item in dueItems)
                //    {
                //        item.openingBalance += objDue.amount;
                //    }
                //}
                context.tblDues.Add(objDue);
                #endregion

                context.tblBuys.Add(paddyInfo);

                #region stock balance
                STK_Balance padStk = context.STK_Balance.Where(ss => ss.stockId == paddyInfo.stockId && ss.productId == paddyInfo.productId && ss.sackWeight == paddyInfo.quantityPerBag).FirstOrDefault();
                if (padStk != null)
                {
                    padStk.sackQuantity += paddyInfo.noOfBag;
                }
                else
                {
                    STK_Balance newStk   = new STK_Balance();
                    int         maxbalId = context.STK_Balance.Select(p => p.ID).DefaultIfEmpty(0).Max();
                    newStk.ID           = ++maxbalId;
                    newStk.productId    = paddyInfo.productId;
                    newStk.stockId      = paddyInfo.stockId;
                    newStk.sackWeight   = paddyInfo.quantityPerBag;
                    newStk.sackQuantity = paddyInfo.noOfBag;
                    context.STK_Balance.Add(newStk);
                }
                #endregion

                #region stock transaction
                long maxprdstkId = context.PaddyTransactions.Select(p => p.ID).DefaultIfEmpty(0).Max();
                long laststkId   = context.PaddyTransactions.Where(s => s.stockId == paddyInfo.stockId && s.prodId == paddyInfo.productId && s.bagWeight == paddyInfo.quantityPerBag).Select(l => l.ID).DefaultIfEmpty(0).Max();
                var  lastTrans   = context.PaddyTransactions.Where(ll => ll.ID == laststkId).FirstOrDefault();

                PaddyTransaction objStkTrans = new PaddyTransaction();
                objStkTrans.ID           = maxprdstkId + 1;
                objStkTrans.date         = paddyInfo.date;
                objStkTrans.rcvQty       = paddyInfo.noOfBag;
                objStkTrans.releaseQty   = 0;
                objStkTrans.stockId      = paddyInfo.stockId;
                objStkTrans.prodId       = paddyInfo.productId;
                objStkTrans.buyId        = paddyInfo.ID;
                objStkTrans.bagWeight    = paddyInfo.quantityPerBag;
                objStkTrans.openingStock = lastTrans == null ? paddyInfo.noOfBag : lastTrans.openingStock + paddyInfo.noOfBag;
                context.PaddyTransactions.Add(objStkTrans);
                #endregion

                #region BagTransactions
                long maxBagId = context.BagTransactions.Select(b => b.ID).DefaultIfEmpty(0).Max();
                if (paddyInfo.bagPrice > 0)
                {
                    BagTransaction        prevItem  = context.BagTransactions.Where(bb => bb.date <= paddyInfo.date && bb.partyId == paddyInfo.partyId && (bb.rcvPrice > 0 || bb.sentPrice > 0)).OrderByDescending(oo => oo.date).ThenByDescending(ii => ii.ID).FirstOrDefault();
                    List <BagTransaction> nextItems = context.BagTransactions.Where(bb => bb.date > paddyInfo.date && bb.partyId == paddyInfo.partyId && (bb.rcvPrice > 0 || bb.sentPrice > 0)).ToList();
                    //int bagCnt = paddyInfo.noOfBag > 0 ? paddyInfo.noOfBag : 1;
                    double?curPriceDue = prevItem == null ? paddyInfo.noOfBag * paddyInfo.bagPrice : prevItem.priceDues + paddyInfo.noOfBag * paddyInfo.bagPrice;

                    BagTransaction bagTrans = new BagTransaction();
                    bagTrans.ID         = ++maxBagId;
                    bagTrans.partyId    = paddyInfo.partyId;
                    bagTrans.rcvId      = paddyInfo.ID;
                    bagTrans.comRcvBag  = paddyInfo.noOfBag;
                    bagTrans.rcvPrice   = paddyInfo.bagPrice;
                    bagTrans.comSentBag = 0;
                    bagTrans.sentPrice  = 0;
                    bagTrans.priceDues  = curPriceDue;
                    bagTrans.bagDues    = 0;
                    bagTrans.date       = paddyInfo.date;
                    context.BagTransactions.Add(bagTrans);

                    if (nextItems != null)
                    {
                        foreach (var item in nextItems)
                        {
                            item.priceDues += curPriceDue;
                        }
                    }
                }
                else
                {
                    BagTransaction        prevItem  = context.BagTransactions.Where(bb => bb.date <= paddyInfo.date && bb.partyId == paddyInfo.partyId && (bb.rcvPrice == 0 || bb.rcvPrice == null) && (bb.sentPrice == 0 || bb.sentPrice == null)).OrderByDescending(oo => oo.date).ThenByDescending(ii => ii.ID).FirstOrDefault();
                    List <BagTransaction> nextItems = context.BagTransactions.Where(bb => bb.date > paddyInfo.date && bb.partyId == paddyInfo.partyId && (bb.rcvPrice == 0 || bb.rcvPrice == null) && (bb.sentPrice == 0 || bb.sentPrice == null)).ToList();
                    int?curBagDue = prevItem == null ? paddyInfo.noOfBag : prevItem.bagDues + paddyInfo.noOfBag;

                    BagTransaction bagTrans = new BagTransaction();
                    bagTrans.ID         = ++maxBagId;
                    bagTrans.partyId    = paddyInfo.partyId;
                    bagTrans.rcvId      = paddyInfo.ID;
                    bagTrans.comRcvBag  = paddyInfo.noOfBag;
                    bagTrans.rcvPrice   = 0;
                    bagTrans.comSentBag = 0;
                    bagTrans.sentPrice  = 0;
                    bagTrans.priceDues  = 0;
                    bagTrans.bagDues    = curBagDue;
                    bagTrans.date       = paddyInfo.date;
                    context.BagTransactions.Add(bagTrans);

                    if (nextItems != null)
                    {
                        foreach (var item in nextItems)
                        {
                            item.bagDues += curBagDue;
                        }
                    }
                }

                #endregion

                return(context.SaveChanges() > 0 ? paddyInfo.ID : 0);
            }
            catch (Exception exc)
            {
                throw new Exception(exc.Message);
            }
        }
        public long EditPaddyInfo(tblBuy paddyInfo)
        {
            var orgPaddy = context.tblBuys.Where(ss => ss.ID == paddyInfo.ID).FirstOrDefault();

            #region edit paddy stock
            STK_Balance padStk = context.STK_Balance.Where(ss => ss.stockId == orgPaddy.stockId && ss.productId == orgPaddy.productId && ss.sackWeight == orgPaddy.quantityPerBag).FirstOrDefault();
            if (padStk != null)
            {
                padStk.sackQuantity -= orgPaddy.noOfBag;
            }

            STK_Balance curStk = context.STK_Balance.Where(ss => ss.stockId == paddyInfo.stockId && ss.productId == paddyInfo.productId && ss.sackWeight == paddyInfo.quantityPerBag).FirstOrDefault();
            if (curStk != null)
            {
                curStk.sackQuantity += paddyInfo.noOfBag;
            }
            else
            {
                STK_Balance newStk   = new STK_Balance();
                int         maxbalId = context.STK_Balance.Select(p => p.ID).DefaultIfEmpty(0).Max();
                newStk.ID           = ++maxbalId;
                newStk.productId    = paddyInfo.productId;
                newStk.stockId      = paddyInfo.stockId;
                newStk.sackWeight   = paddyInfo.quantityPerBag;
                newStk.sackQuantity = paddyInfo.noOfBag;
                context.STK_Balance.Add(newStk);
            }
            #endregion

            #region edit costing source
            var orgCostSrc = context.tblCostingSources.Where(ii => ii.buyId == paddyInfo.ID).FirstOrDefault();
            if (orgCostSrc != null)
            {
                orgCostSrc.amount = paddyInfo.amount;
                orgCostSrc.date   = paddyInfo.date;
                if (paddyInfo.transportCostInclude)
                {
                    orgCostSrc.transportCost = paddyInfo.transportCost;
                }
                else
                {
                    orgCostSrc.transportCost = 0;
                }
                orgCostSrc.labourCostPerBag = paddyInfo.labourCostPerBag;
                orgCostSrc.partyId          = paddyInfo.partyId;
            }
            else if (orgCostSrc == null && (paddyInfo.amount > 0 || paddyInfo.transportCost > 0))
            {
                long             cstId  = context.tblCostingSources.Select(i => i.ID).DefaultIfEmpty(0).Max();
                tblCostingSource cstObj = new tblCostingSource();
                cstObj.ID               = ++cstId;
                cstObj.sourceName       = "ধান"; // shoul be come from commonelement
                cstObj.srcDescId        = 23;    // should be come from commonelemnt
                cstObj.srcDescription   = "ধান ক্রয় বাবদ খরচ";
                cstObj.amount           = paddyInfo.amount;
                cstObj.date             = paddyInfo.date;
                cstObj.buyId            = paddyInfo.ID;
                cstObj.partyId          = paddyInfo.partyId;
                cstObj.labourCostPerBag = paddyInfo.labourCostPerBag ?? 0;
                if (paddyInfo.transportCostInclude)
                {
                    cstObj.transportCost = paddyInfo.transportCost ?? 0;
                }
                else
                {
                    cstObj.transportCost = 0;
                }
                context.tblCostingSources.Add(cstObj);
            }
            #endregion

            #region edit stock transaction

            var orgStkTrans = context.PaddyTransactions.Where(ss => ss.buyId == orgPaddy.ID).FirstOrDefault();
            if (orgStkTrans.stockId == paddyInfo.stockId)
            {
                double diff = orgStkTrans.rcvQty.Value - paddyInfo.noOfBag;
                orgStkTrans.openingStock = orgStkTrans.openingStock - orgStkTrans.rcvQty.Value;

                orgStkTrans.date         = paddyInfo.date;
                orgStkTrans.rcvQty       = paddyInfo.noOfBag;
                orgStkTrans.releaseQty   = 0;
                orgStkTrans.stockId      = paddyInfo.stockId;
                orgStkTrans.prodId       = paddyInfo.productId;
                orgStkTrans.openingStock = orgStkTrans.openingStock + paddyInfo.noOfBag;
                // here may be change for different stock
                var nextStkTrans = context.PaddyTransactions.Where(ss => ss.ID > orgStkTrans.ID && ss.prodId == orgStkTrans.prodId && ss.stockId == orgStkTrans.stockId && ss.bagWeight == orgStkTrans.bagWeight);// && ss.date>=orgStkTrans.date
                foreach (var item in nextStkTrans)
                {
                    item.openingStock -= diff;
                }
            }
            else
            {
                var nextStkTrans = context.PaddyTransactions.Where(ss => ss.ID > orgStkTrans.ID && ss.prodId == orgStkTrans.prodId && ss.stockId == orgStkTrans.stockId && ss.bagWeight == orgStkTrans.bagWeight);// && ss.date >= orgStkTrans.date
                foreach (var item in nextStkTrans)
                {
                    item.openingStock -= orgStkTrans.rcvQty.Value;
                }
                context.PaddyTransactions.Remove(orgStkTrans);

                long maxprdstkId = context.PaddyTransactions.Select(p => p.ID).DefaultIfEmpty(0).Max();
                long laststkId   = context.PaddyTransactions.Where(s => s.stockId == paddyInfo.stockId && s.prodId == paddyInfo.productId && s.bagWeight == paddyInfo.quantityPerBag).Select(l => l.ID).DefaultIfEmpty(0).Max();
                var  lastTrans   = context.PaddyTransactions.Where(ll => ll.ID == laststkId).FirstOrDefault();

                PaddyTransaction objStkTrans = new PaddyTransaction();
                objStkTrans.ID           = maxprdstkId + 1;
                objStkTrans.date         = paddyInfo.date;
                objStkTrans.rcvQty       = paddyInfo.noOfBag;
                objStkTrans.releaseQty   = 0;
                objStkTrans.stockId      = paddyInfo.stockId;
                objStkTrans.prodId       = paddyInfo.productId;
                objStkTrans.buyId        = paddyInfo.ID;
                objStkTrans.bagWeight    = paddyInfo.quantityPerBag;
                objStkTrans.openingStock = lastTrans == null ? paddyInfo.noOfBag : lastTrans.openingStock + paddyInfo.noOfBag;
                context.PaddyTransactions.Add(objStkTrans);
            }
            #endregion

            #region edit tblbuy
            orgPaddy.productId   = paddyInfo.productId;
            orgPaddy.noOfBag     = paddyInfo.noOfBag;
            orgPaddy.truckNumber = paddyInfo.truckNumber;
            //orgPaddy.tra nsportCost = paddyInfo.transportCost;
            //orgPaddy.labourCostPerBag = paddyInfo.labourCostPerBag;
            //orgPaddy.amount = paddyInfo.amount;
            orgPaddy.partyId        = paddyInfo.partyId;
            orgPaddy.quantityPerBag = paddyInfo.quantityPerBag;
            orgPaddy.stockId        = paddyInfo.stockId;
            orgPaddy.unit           = paddyInfo.unit;
            orgPaddy.price          = paddyInfo.price;
            #endregion

            #region edit dues
            tblDue orgDue  = context.tblDues.Where(d => d.buyId == orgPaddy.ID).FirstOrDefault();
            double?totalPr = paddyInfo.price * paddyInfo.quantityPerBag / 40 * paddyInfo.noOfBag;
            var    due     = totalPr - paddyInfo.amount;
            orgDue.amount = due;

            //tblDue dueItem = context.tblDues.Where(bb => bb.date <= paddyInfo.date && bb.partyId == paddyInfo.partyId && bb.ID < orgDue.ID).OrderByDescending(oo => oo.date).ThenByDescending(ii => ii.ID).FirstOrDefault();
            //List<tblDue> dueItems = context.tblDues.Where(bb => bb.date > paddyInfo.date && bb.partyId == paddyInfo.partyId).ToList();
            //if (dueItem == null)
            //{
            //    orgDue.openingBalance = orgDue.amount;
            //}
            //else
            //{
            //    orgDue.openingBalance = dueItem.openingBalance + orgDue.amount;
            //}
            //if (dueItems != null)
            //{
            //    foreach (var item in dueItems)
            //    {
            //        item.openingBalance += orgDue.amount;
            //    }
            //}

            #endregion

            #region edit sackinfo
            BagTransaction orgItem = context.BagTransactions.Where(bb => bb.rcvId == orgPaddy.ID).FirstOrDefault();
            //remove if section when all price of previous paddy has been applied
            if (orgItem == null)
            {
                #region BagTransactions
                long maxBagId = context.BagTransactions.Select(b => b.ID).DefaultIfEmpty(0).Max();
                if (paddyInfo.bagPrice > 0)
                {
                    BagTransaction        prevItem  = context.BagTransactions.Where(bb => bb.date <= paddyInfo.date && bb.partyId == paddyInfo.partyId && (bb.rcvPrice > 0 || bb.sentPrice > 0)).OrderByDescending(oo => oo.date).ThenByDescending(ii => ii.ID).FirstOrDefault();
                    List <BagTransaction> nextItems = context.BagTransactions.Where(bb => bb.date > paddyInfo.date && bb.partyId == paddyInfo.partyId && (bb.rcvPrice > 0 || bb.sentPrice > 0)).ToList();
                    //int bagCnt = paddyInfo.noOfBag > 0 ? paddyInfo.noOfBag : 1;
                    double?curPriceDue = prevItem == null ? paddyInfo.noOfBag * paddyInfo.bagPrice : prevItem.priceDues + paddyInfo.noOfBag * paddyInfo.bagPrice;

                    BagTransaction bagTrans = new BagTransaction();
                    bagTrans.ID         = ++maxBagId;
                    bagTrans.partyId    = paddyInfo.partyId;
                    bagTrans.rcvId      = paddyInfo.ID;
                    bagTrans.comRcvBag  = paddyInfo.noOfBag;
                    bagTrans.rcvPrice   = paddyInfo.bagPrice;
                    bagTrans.comSentBag = 0;
                    bagTrans.sentPrice  = 0;
                    bagTrans.priceDues  = curPriceDue;
                    bagTrans.bagDues    = 0;
                    bagTrans.date       = paddyInfo.date;
                    context.BagTransactions.Add(bagTrans);

                    if (nextItems != null)
                    {
                        foreach (var item in nextItems)
                        {
                            item.priceDues += curPriceDue;
                        }
                    }
                }
                else
                {
                    BagTransaction        prevItem  = context.BagTransactions.Where(bb => bb.date <= paddyInfo.date && bb.partyId == paddyInfo.partyId && (bb.rcvPrice == 0 || bb.rcvPrice == null) && (bb.sentPrice == 0 || bb.sentPrice == null)).OrderByDescending(oo => oo.date).ThenByDescending(ii => ii.ID).FirstOrDefault();
                    List <BagTransaction> nextItems = context.BagTransactions.Where(bb => bb.date > paddyInfo.date && bb.partyId == paddyInfo.partyId && (bb.rcvPrice == 0 || bb.rcvPrice == null) && (bb.sentPrice == 0 || bb.sentPrice == null)).ToList();
                    int?curBagDue = prevItem == null ? paddyInfo.noOfBag : prevItem.bagDues + paddyInfo.noOfBag;

                    BagTransaction bagTrans = new BagTransaction();
                    bagTrans.ID         = ++maxBagId;
                    bagTrans.partyId    = paddyInfo.partyId;
                    bagTrans.rcvId      = paddyInfo.ID;
                    bagTrans.comRcvBag  = paddyInfo.noOfBag;
                    bagTrans.rcvPrice   = 0;
                    bagTrans.comSentBag = 0;
                    bagTrans.sentPrice  = 0;
                    bagTrans.priceDues  = 0;
                    bagTrans.bagDues    = curBagDue;
                    bagTrans.date       = paddyInfo.date;
                    context.BagTransactions.Add(bagTrans);

                    if (nextItems != null)
                    {
                        foreach (var item in nextItems)
                        {
                            item.bagDues += curBagDue;
                        }
                    }
                }

                #endregion
            }
            else
            {
                orgItem.rcvPrice   = orgItem.rcvPrice ?? 0;
                paddyInfo.bagPrice = paddyInfo.bagPrice ?? 0;
                if (orgItem.rcvPrice > 0 && paddyInfo.bagPrice > 0)
                {
                    List <BagTransaction> nextItems = context.BagTransactions.Where(bb => bb.date >= paddyInfo.date && bb.partyId == paddyInfo.partyId && (bb.rcvPrice > 0 || bb.sentPrice > 0)).ToList(); // && bb.ID>orgItem.ID
                    double?prevPriceDue             = orgItem.priceDues - orgItem.rcvPrice * orgItem.comRcvBag;
                    double?curPriceDue = prevPriceDue + paddyInfo.noOfBag * paddyInfo.bagPrice;
                    if (nextItems != null)
                    {
                        foreach (var item in nextItems)
                        {
                            item.priceDues = item.priceDues - orgItem.priceDues + curPriceDue;
                        }
                    }
                    orgItem.partyId   = paddyInfo.partyId;
                    orgItem.comRcvBag = paddyInfo.noOfBag;
                    orgItem.rcvPrice  = paddyInfo.bagPrice;
                    orgItem.priceDues = curPriceDue;
                    orgItem.date      = paddyInfo.date;
                }
                else if ((orgItem.rcvPrice == 0 || orgItem.rcvPrice == null) && (paddyInfo.bagPrice == 0 || paddyInfo.bagPrice == null))
                {
                    List <BagTransaction> nextItems = context.BagTransactions.Where(bb => bb.date >= paddyInfo.date && bb.partyId == paddyInfo.partyId && (bb.rcvPrice == 0 || bb.rcvPrice == null) && (bb.sentPrice == 0 || bb.sentPrice == null)).ToList(); //&& bb.ID > orgItem.ID
                    int?prevBagDue = orgItem.bagDues - orgItem.comRcvBag;
                    int?curBagDue  = prevBagDue + paddyInfo.noOfBag;
                    if (nextItems != null)
                    {
                        foreach (var item in nextItems)
                        {
                            item.bagDues = item.bagDues - orgItem.bagDues + curBagDue;
                        }
                    }
                    orgItem.partyId   = paddyInfo.partyId;
                    orgItem.comRcvBag = paddyInfo.noOfBag;
                    orgItem.bagDues   = curBagDue;
                    orgItem.date      = paddyInfo.date;
                }
                else if (orgItem.rcvPrice > 0 && (paddyInfo.bagPrice == 0 || paddyInfo.bagPrice == null))
                {
                    List <BagTransaction> nextItemsWithPric    = context.BagTransactions.Where(bb => bb.date >= paddyInfo.date && bb.partyId == paddyInfo.partyId && (bb.rcvPrice > 0 || bb.sentPrice > 0)).ToList();                                                    //&& bb.ID > orgItem.ID
                    List <BagTransaction> nextItemsWithoutPric = context.BagTransactions.Where(bb => bb.date >= paddyInfo.date && bb.partyId == paddyInfo.partyId && (bb.rcvPrice == 0 || bb.rcvPrice == null) && (bb.sentPrice == 0 || bb.sentPrice == null)).ToList(); //&& bb.ID > orgItem.ID
                    int?prevBagDue = orgItem.bagDues - orgItem.comRcvBag;
                    int?curBagDue  = prevBagDue + paddyInfo.noOfBag;
                    if (nextItemsWithPric != null)
                    {
                        foreach (var item in nextItemsWithPric)
                        {
                            item.priceDues = item.priceDues - orgItem.priceDues;
                        }
                    }
                    if (nextItemsWithoutPric != null)
                    {
                        foreach (var item in nextItemsWithoutPric)
                        {
                            item.bagDues = item.bagDues + paddyInfo.noOfBag;
                        }
                    }

                    orgItem.partyId   = paddyInfo.partyId;
                    orgItem.comRcvBag = paddyInfo.noOfBag;
                    orgItem.rcvPrice  = 0;
                    orgItem.priceDues = 0;
                    orgItem.bagDues   = curBagDue;
                    orgItem.date      = paddyInfo.date;
                }
                else if ((orgItem.rcvPrice == 0 || orgItem.rcvPrice == null) && paddyInfo.bagPrice > 0)
                {
                    List <BagTransaction> nextItemsWithPric    = context.BagTransactions.Where(bb => bb.date >= paddyInfo.date && bb.partyId == paddyInfo.partyId && (bb.rcvPrice > 0 || bb.sentPrice > 0)).ToList();                                                    // && bb.ID > orgItem.ID
                    List <BagTransaction> nextItemsWithoutPric = context.BagTransactions.Where(bb => bb.date >= paddyInfo.date && bb.partyId == paddyInfo.partyId && (bb.rcvPrice == 0 || bb.rcvPrice == null) && (bb.sentPrice == 0 || bb.sentPrice == null)).ToList(); // && bb.ID > orgItem.ID
                    double?prevPriceDue = orgItem.priceDues - orgItem.rcvPrice * orgItem.comRcvBag;
                    double?curPriceDue  = prevPriceDue + paddyInfo.noOfBag * paddyInfo.bagPrice;
                    if (nextItemsWithPric != null)
                    {
                        foreach (var item in nextItemsWithPric)
                        {
                            item.priceDues = item.priceDues + paddyInfo.noOfBag * paddyInfo.bagPrice;
                        }
                    }
                    if (nextItemsWithoutPric != null)
                    {
                        foreach (var item in nextItemsWithoutPric)
                        {
                            item.bagDues = item.bagDues - orgItem.comRcvBag;
                        }
                    }

                    orgItem.partyId   = paddyInfo.partyId;
                    orgItem.comRcvBag = paddyInfo.noOfBag;
                    orgItem.rcvPrice  = paddyInfo.bagPrice;
                    orgItem.priceDues = curPriceDue;
                    orgItem.bagDues   = 0;
                    orgItem.date      = paddyInfo.date;
                }
            }
            #endregion

            return(context.SaveChanges() > 0 ? paddyInfo.ID : 0);
        }