public List <STK_Balance> GetPaddyTransferInfos() { var results = from bal in context.STK_Balance join stk in context.STK_tblStock on bal.stockId equals stk.ID join prod in context.tblProducts on bal.productId equals prod.ID where prod.parentId == 2054 select new { stk.stockName, prod.productName, bal.sackWeight, bal.sackQuantity }; List <STK_Balance> balanceList = new List <STK_Balance>(); foreach (var items in results) { STK_Balance objStkBalance = new STK_Balance(); objStkBalance.stockName = items.stockName; objStkBalance.productName = items.productName; objStkBalance.sackWeight = items.sackWeight; objStkBalance.sackQuantity = items.sackQuantity; balanceList.Add(objStkBalance); } return(balanceList); }
public bool EditProdStock(STK_Balance prodStock) { #region stock balance var orgStk = context.STK_Balance.Where(ss => ss.ID == prodStock.ID).FirstOrDefault(); #region stock transaction long maxprdstkId = context.STK_Transaction.Select(p => p.ID).DefaultIfEmpty(0).Max(); long laststkId = context.STK_Transaction.Where(s => s.stockId == prodStock.stockId && s.prodId == prodStock.productId).Select(l => l.ID).DefaultIfEmpty(0).Max(); STK_Transaction objStkTrans = new STK_Transaction(); objStkTrans.ID = maxprdstkId + 1; objStkTrans.date = prodStock.date; objStkTrans.rcvQty = prodStock.sackQuantity; objStkTrans.sellQty = 0; objStkTrans.stockId = prodStock.stockId.Value; objStkTrans.prodId = prodStock.productId.Value; objStkTrans.operation = 2; var lastTrans = context.STK_Transaction.Where(ll => ll.ID == laststkId).FirstOrDefault(); objStkTrans.openingStock = lastTrans.openingStock - orgStk.sackQuantity == null?0:orgStk.sackQuantity + prodStock.sackQuantity == null?0:prodStock.sackQuantity.Value; context.STK_Transaction.Add(objStkTrans); #endregion orgStk.productId = prodStock.productId; orgStk.sackQuantity = prodStock.sackQuantity; orgStk.stockId = prodStock.stockId; orgStk.date = prodStock.date; #endregion return(context.SaveChanges() > 0); }
public bool DeleteParticleStock(STK_Balance particleStk) { var orgStk = context.STK_Balance.Where(ss => ss.ID == particleStk.ID).FirstOrDefault(); context.STK_Balance.Remove(orgStk); return(context.SaveChanges() > 0); }
public bool SaveProdStock(STK_Balance prodStk) { #region stock transaction long maxprdstkId = context.STK_Transaction.Select(p => p.ID).DefaultIfEmpty(0).Max(); long laststkId = context.STK_Transaction.Where(s => s.stockId == prodStk.stockId && s.prodId == prodStk.productId).Select(l => l.ID).DefaultIfEmpty(0).Max(); STK_Transaction objStkTrans = new STK_Transaction(); objStkTrans.ID = maxprdstkId + 1; objStkTrans.date = prodStk.date; objStkTrans.rcvQty = prodStk.sackQuantity; objStkTrans.sellQty = 0; objStkTrans.stockId = prodStk.stockId.Value; objStkTrans.prodId = prodStk.productId.Value; objStkTrans.operation = 1; var lastTrans = context.STK_Transaction.Where(ll => ll.ID == laststkId).FirstOrDefault(); objStkTrans.openingStock = lastTrans == null ? prodStk.sackQuantity.Value : lastTrans.openingStock + prodStk.sackQuantity.Value; context.STK_Transaction.Add(objStkTrans); #endregion #region stock balance int maxId = context.STK_Balance.Select(p => p.ID).DefaultIfEmpty(0).Max(); var existStk = context.STK_Balance.Where(ss => ss.productId == prodStk.productId && ss.stockId == prodStk.stockId).FirstOrDefault(); if (existStk == null) { prodStk.ID = ++maxId; context.STK_Balance.Add(prodStk); } else { existStk.sackQuantity += prodStk.sackQuantity; } #endregion return(context.SaveChanges() > 0); }
public bool SaveParticleStock(STK_Balance particleStk) { int maxId = context.STK_Balance.Select(p => p.ID).DefaultIfEmpty(0).Max(); particleStk.ID = ++maxId; context.STK_Balance.Add(particleStk); return(context.SaveChanges() > 0); }
public long SavePaddyTransfer(STK_Balance objStkBalance) { var transferFrom = context.STK_Balance.Where( ss => ss.stockId == objStkBalance.stockId && ss.productId == objStkBalance.productId && ss.sackWeight == objStkBalance.sackWeight).FirstOrDefault(); transferFrom.sackQuantity = transferFrom.sackQuantity - objStkBalance.sackQuantity; if (transferFrom.sackQuantity < 0) { return(0); } var transferTo = context.STK_Balance.Where( ss => ss.stockId == objStkBalance.targetStockId && ss.productId == objStkBalance.productId && ss.sackWeight == objStkBalance.sackWeight).FirstOrDefault(); if (transferTo != null) { transferTo.sackQuantity += objStkBalance.sackQuantity; } else { var maxId = context.STK_Balance.Select(ss => ss.ID).DefaultIfEmpty(0).Max(); STK_Balance stockTo = new STK_Balance(); stockTo.ID = ++maxId; stockTo.stockId = objStkBalance.targetStockId; stockTo.sackWeight = objStkBalance.sackWeight; context.STK_Balance.Add(stockTo); } #region paddy transaction var maxTransId = context.PaddyTransactions.Select(ss => ss.ID).DefaultIfEmpty(0).Max(); PaddyTransaction exstRlsStk = context.PaddyTransactions.Where(pp => pp.prodId == objStkBalance.productId && pp.stockId == objStkBalance.stockId && pp.bagWeight == objStkBalance.sackWeight).FirstOrDefault(); PaddyTransaction objTransRls = new PaddyTransaction(); objTransRls.ID = ++maxTransId; objTransRls.date = objStkBalance.date; objTransRls.bagWeight = objStkBalance.sackWeight; objTransRls.stockId = objStkBalance.stockId.Value; objTransRls.prodId = objStkBalance.productId.Value; objTransRls.releaseQty = objStkBalance.sackQuantity; objTransRls.openingStock = exstRlsStk == null ? 0 : exstRlsStk.openingStock - objStkBalance.sackQuantity.Value; context.PaddyTransactions.Add(objTransRls); PaddyTransaction exstRcvStk = context.PaddyTransactions.Where(pp => pp.prodId == objStkBalance.productId && pp.stockId == objStkBalance.targetStockId && pp.bagWeight == objStkBalance.sackWeight).FirstOrDefault(); PaddyTransaction objTransRcv = new PaddyTransaction(); objTransRcv.ID = ++maxTransId; objTransRcv.date = objStkBalance.date; objTransRcv.bagWeight = objStkBalance.sackWeight; objTransRcv.stockId = objStkBalance.targetStockId; objTransRcv.prodId = objStkBalance.productId.Value; objTransRcv.rcvQty = objStkBalance.sackQuantity; objTransRcv.openingStock = exstRcvStk == null ? 0 : exstRcvStk.openingStock + objStkBalance.sackQuantity.Value; objTransRcv.serial = objStkBalance.serial; objTransRcv.millCost = objStkBalance.millCost; objTransRcv.fromStock = objStkBalance.stockId; context.PaddyTransactions.Add(objTransRcv); #endregion return(context.SaveChanges() > 0 ? objTransRcv.ID : 0); }
public bool EditParticleStock(STK_Balance particleStk) { var orgStk = context.STK_Balance.Where(ss => ss.ID == particleStk.ID).FirstOrDefault(); orgStk.productId = particleStk.productId; orgStk.sackQuantity = particleStk.sackQuantity; orgStk.stockId = particleStk.stockId; return(context.SaveChanges() > 0); }
public JsonResult SaveParticleStock(STK_Balance particleStk) { 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(sellRepository.SaveParticleStock(particleStk), JsonRequestBehavior.AllowGet)); } else { Session["userId"] = null; Session["role"] = null; return(Json(null, JsonRequestBehavior.AllowGet)); } }
//[Authorize(Roles = "Admin,Super Admin")] public JsonResult Save(STK_Balance objStkBalance) { 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(paddyTransferRepo.SavePaddyTransfer(objStkBalance), JsonRequestBehavior.AllowGet)); } else { Session["userId"] = null; Session["role"] = null; return(Json(null, JsonRequestBehavior.AllowGet)); } }
public JsonResult EditParticleStock([System.Web.Http.FromBody] STK_Balance particleStk) { if (Session["role"] == null) { Session["userId"] = null; Session["role"] = null; return(Json(false, JsonRequestBehavior.AllowGet)); } else if (Session["role"].ToString() == "Super Admin") { return(Json(sellRepository.EditParticleStock(particleStk), JsonRequestBehavior.AllowGet)); } else { Session["userId"] = null; Session["role"] = null; return(Json(false, JsonRequestBehavior.AllowGet)); } }
public JsonResult SaveProdStock(STK_Balance prodStk) { if (Session["role"] == null) { Session["userId"] = null; Session["role"] = null; return(Json(false, JsonRequestBehavior.AllowGet)); } else if (Session["role"].ToString() == "Super Admin") { return(Json(stkRepository.SaveProdStock(prodStk), JsonRequestBehavior.AllowGet)); } else { Session["userId"] = null; Session["role"] = null; return(Json(false, JsonRequestBehavior.AllowGet)); } }
public List <STK_Balance> GetProdStocks() { var prodstks = context.STK_Balance.Join(context.tblProducts, stk => stk.productId, prod => prod.ID, (stk, prod) => new { ID = stk.ID, productId = stk.productId, sackQuantity = stk.sackQuantity, stockId = stk.stockId, productName = prod.productName }).Join(context.STK_tblStock, ricep => ricep.stockId, stk => stk.ID, (ricep, stk) => new { ID = ricep.ID, productId = ricep.productId, sackQuantity = ricep.sackQuantity, stockId = ricep.stockId, productName = ricep.productName, stockName = stk.stockName }).ToList(); List <STK_Balance> rLst = new List <STK_Balance>(); foreach (var x in prodstks) { STK_Balance objR = new STK_Balance(); objR.ID = x.ID; objR.productId = x.productId; objR.sackQuantity = x.sackQuantity; objR.stockId = x.stockId; objR.productName = x.productName; objR.stockName = x.stockName; rLst.Add(objR); } return(rLst); }
public List <STK_Balance> GetParticleStock() { var particlestks = context.STK_Balance.Join(context.tblProducts, particle => particle.productId, prod => prod.ID, (particle, prod) => new { ID = particle.ID, productId = particle.productId, sackQuantity = particle.sackQuantity, stockId = particle.stockId, productName = prod.productName }).Join(context.STK_tblStock, particlep => particlep.stockId, stk => stk.ID, (particlep, stk) => new { ID = particlep.ID, productId = particlep.productId, sackQuantity = particlep.sackQuantity, stockId = particlep.stockId, productName = particlep.productName, stockName = stk.stockName }).ToList(); List <STK_Balance> rLst = new List <STK_Balance>(); foreach (var x in particlestks) { STK_Balance objR = new STK_Balance(); objR.ID = x.ID; objR.productId = x.productId; objR.sackQuantity = x.sackQuantity; objR.stockId = x.stockId; objR.productName = x.productName; objR.stockName = x.stockName; rLst.Add(objR); } return(rLst); }
public bool DeletePaddy(int pk) { var orgPaddy = context.tblBuys.Where(ss => ss.ID == pk).FirstOrDefault(); #region edit stock transaction var orgStkTrans = context.PaddyTransactions.Where(ss => ss.buyId == orgPaddy.ID).FirstOrDefault(); var nextStkTrans = context.PaddyTransactions.Where(ss => ss.ID > orgStkTrans.ID && ss.prodId == orgStkTrans.prodId && ss.stockId == orgStkTrans.stockId && ss.bagWeight == orgStkTrans.bagWeight); foreach (var item in nextStkTrans) { item.openingStock -= orgStkTrans.rcvQty.Value; } context.PaddyTransactions.Remove(orgStkTrans); #endregion #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();//&& ss.sackWeight == riceInfo.quantity padStk.sackQuantity -= orgPaddy.noOfBag; #endregion #region delete costing source var orgCstSrc = context.tblCostingSources.Where(ii => ii.buyId == orgPaddy.ID).FirstOrDefault(); if (orgCstSrc != null) { context.tblCostingSources.Remove(orgCstSrc); } #endregion #region delete sackInfo var orgSack = context.BagTransactions.Where(k => k.rcvId == pk).FirstOrDefault(); if (orgSack != null) { if (orgSack.rcvPrice > 0) { List <BagTransaction> nextItems = context.BagTransactions.Where(ss => ss.date >= orgPaddy.date && ss.partyId == orgPaddy.partyId && ss.ID > orgSack.ID && ss.rcvPrice > 0).ToList(); if (nextItems != null) { foreach (var item in nextItems) { item.priceDues -= orgSack.rcvPrice * orgSack.comRcvBag; } } } else { List <BagTransaction> nextItems = context.BagTransactions.Where(ss => ss.date >= orgPaddy.date && ss.partyId == orgPaddy.partyId && ss.ID > orgSack.ID && (ss.rcvPrice == 0 || ss.rcvPrice == null)).ToList(); if (nextItems != null) { foreach (var item in nextItems) { item.bagDues -= orgSack.comRcvBag; } } } context.BagTransactions.Remove(orgSack); } #endregion #region delete dues tblDue orgDue = context.tblDues.Where(d => d.buyId == orgPaddy.ID).FirstOrDefault(); //List<tblDue> dueItems = context.tblDues.Where(bb => bb.date >= orgDue.date && bb.partyId == orgDue.partyId && bb.ID > orgDue.ID).ToList(); //if (dueItems != null) //{ // foreach (var item in dueItems) // { // item.openingBalance -= orgDue.amount; // } //} context.tblDues.Remove(orgDue); #endregion #region delete tblbuy context.tblBuys.Remove(orgPaddy); #endregion return(context.SaveChanges() > 0); }
// 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); }
public tblSell SaveHusk(tblSell huskInfo) { try { long maxId = context.tblSells.Select(p => p.ID).DefaultIfEmpty(0).Max(); huskInfo.ID = ++maxId; if (huskInfo.paidAmount > 0) { #region income source long incId = context.tblIncomeSources.Select(i => i.ID).DefaultIfEmpty(0).Max(); tblIncomeSource incomeObj = new tblIncomeSource(); incomeObj.ID = ++incId; incomeObj.partyId = huskInfo.partyId; incomeObj.sourceName = "তুষ"; // shoul be come from commonelement incomeObj.srcDescId = 21; // should be come from commonelemnt incomeObj.description = "তুষ বিক্রয় বাবদ আয়"; incomeObj.amount = huskInfo.paidAmount; incomeObj.date = huskInfo.date; context.tblIncomeSources.Add(incomeObj); #endregion huskInfo.incSrcId = incomeObj.ID; } #region save payable double?totalPr = 0; if (huskInfo.transportCostInclude) { totalPr = huskInfo.noOfBag * huskInfo.unitPrice * huskInfo.quantity + huskInfo.transportCost; } else { totalPr = huskInfo.noOfBag * huskInfo.unitPrice * huskInfo.quantity; if (huskInfo.transportCost > 0) { #region costing source tblCostingSource objTblCostingSource = new tblCostingSource(); long maxCost = context.tblCostingSources.Select(i => i.ID).DefaultIfEmpty(0).Max(); objTblCostingSource.ID = ++maxCost; objTblCostingSource.partyId = huskInfo.partyId; objTblCostingSource.amount = Convert.ToDouble(huskInfo.transportCost); objTblCostingSource.sourceName = "তুষ"; objTblCostingSource.srcDescription = "তুষ বিক্রয়ের পরিবহন খরচ"; objTblCostingSource.srcDescId = 21; objTblCostingSource.date = huskInfo.date; objTblCostingSource.sellId = huskInfo.ID; context.tblCostingSources.Add(objTblCostingSource); #endregion } } if (totalPr > 0) { long maxpayId = context.tblPayables.Select(i => i.ID).DefaultIfEmpty(0).Max(); tblPayable objTblPayable = new tblPayable(); objTblPayable.ID = ++maxpayId; objTblPayable.partyId = huskInfo.partyId; objTblPayable.date = huskInfo.date; objTblPayable.sellId = huskInfo.ID; var lastPayable = context.tblPayables.Where(p => p.partyId == huskInfo.partyId && p.isActive == 1).FirstOrDefault(); var loan = totalPr - huskInfo.paidAmount; objTblPayable.amount = loan; if (lastPayable != null) { lastPayable.isActive = 0; objTblPayable.openingBalance = objTblPayable.amount + lastPayable.openingBalance; } else { objTblPayable.openingBalance = objTblPayable.amount; } objTblPayable.isActive = 1; context.tblPayables.Add(objTblPayable); } #endregion context.tblSells.Add(huskInfo); #region substract husk from stock STK_Balance huskStk = context.STK_Balance.Where(ss => ss.stockId == huskInfo.stockId && ss.productId == huskInfo.productId).FirstOrDefault();//&& ss.sackWeight==huskInfo.quantity if (huskStk == null) { var maxStkBalId = context.STK_Balance.Select(p => p.ID).DefaultIfEmpty(0).Max(); STK_Balance stkBal = new STK_Balance(); stkBal.ID = ++maxStkBalId; stkBal.productId = huskInfo.productId; stkBal.stockId = huskInfo.stockId; stkBal.sackQuantity = -huskInfo.noOfBag; context.STK_Balance.Add(stkBal); } else { huskStk.sackQuantity -= huskInfo.noOfBag; } #endregion #region stock transaction long maxprdstkId = context.STK_Transaction.Select(p => p.ID).DefaultIfEmpty(0).Max(); //long laststkId = context.STK_Transaction.Where(s => s.stockId == huskInfo.stockId && s.prodId == huskInfo.productId).Select(l => l.ID).DefaultIfEmpty(0).Max(); STK_Transaction objStkTrans = new STK_Transaction(); objStkTrans.ID = maxprdstkId + 1; objStkTrans.date = huskInfo.date; objStkTrans.rcvQty = 0; objStkTrans.sellQty = huskInfo.noOfBag; objStkTrans.stockId = huskInfo.stockId.Value; objStkTrans.prodId = huskInfo.productId; objStkTrans.operation = 1; objStkTrans.sellId = huskInfo.ID; if (huskStk == null) { objStkTrans.openingStock = -huskInfo.noOfBag; } else { objStkTrans.openingStock = huskStk.sackQuantity.Value; } //var lastTrans = context.STK_Transaction.Where(ll => ll.ID == laststkId).FirstOrDefault(); //objStkTrans.openingStock = lastTrans == null ? 0 - huskInfo.noOfBag : lastTrans.openingStock - huskInfo.noOfBag; context.STK_Transaction.Add(objStkTrans); #endregion tblSell newSell = new tblSell(); newSell.ID = huskInfo.ID; newSell.incSrcId = huskInfo.incSrcId; return(context.SaveChanges() > 0 ? newSell : null); } catch (Exception exc) { throw new Exception(exc.Message); } }
public bool DeleteHuskSell(int pk) { var orgHuskSell = context.tblSells.Where(ss => ss.ID == pk).FirstOrDefault(); #region edit stock STK_Balance huskStk = context.STK_Balance.Where(ss => ss.stockId == orgHuskSell.stockId && ss.productId == orgHuskSell.productId).FirstOrDefault();//&& ss.sackWeight == particleInfo.quantity huskStk.sackQuantity += orgHuskSell.noOfBag; #endregion #region edit stock transaction var delStkTrans = context.STK_Transaction.Where(ss => ss.sellId == orgHuskSell.ID).FirstOrDefault(); var nextStkTrans = context.STK_Transaction.Where(ss => ss.ID > delStkTrans.ID && ss.prodId == delStkTrans.prodId && ss.stockId == delStkTrans.stockId); foreach (var item in nextStkTrans) { item.openingStock += orgHuskSell.noOfBag; } context.STK_Transaction.Remove(delStkTrans); #endregion #region delete tblPayable var orgPayable = context.tblPayables.Where(ss => ss.sellId == pk).FirstOrDefault(); if (orgPayable != null) { double?difference = orgPayable.amount; var nextPayables = context.tblPayables.Where(pp => pp.partyId == orgPayable.partyId && pp.ID > orgPayable.ID); if (nextPayables.Count() <= 0) { var lastPayable = context.tblPayables.Where(pp => pp.partyId == orgPayable.partyId && pp.ID < orgPayable.ID).OrderByDescending(pp => pp.ID).FirstOrDefault(); if (lastPayable != null) { lastPayable.isActive = 1; } } foreach (var item in nextPayables) { item.openingBalance = item.openingBalance - difference; } context.tblPayables.Remove(orgPayable); } #endregion #region delete costing source List <tblCostingSource> objCostingSource = context.tblCostingSources.Where(cc => cc.sellId == pk).ToList(); if (objCostingSource != null) { foreach (var item in objCostingSource) { context.tblCostingSources.Remove(item); } } #endregion // delete income source var orgIncomeSrc = context.tblIncomeSources.Where(ii => ii.ID == orgHuskSell.incSrcId).FirstOrDefault(); if (orgIncomeSrc != null) { context.tblIncomeSources.Remove(orgIncomeSrc); } //delete tblsell context.tblSells.Remove(orgHuskSell); return(context.SaveChanges() > 0); }
public tblSell EditHuskInfo(tblSell huskInfo) { try { // into payable tabel column amount is 'how many amount is added with opening balance, not paidamount' var orgHuskSell = context.tblSells.Where(ss => ss.ID == huskInfo.ID).FirstOrDefault(); #region edit husk stock STK_Balance orgStk = context.STK_Balance.Where(ss => ss.stockId == orgHuskSell.stockId && ss.productId == orgHuskSell.productId).FirstOrDefault();// && ss.sackWeight == particleInfo.quantity orgStk.sackQuantity += orgHuskSell.noOfBag; STK_Balance newStk = context.STK_Balance.Where(ss => ss.stockId == huskInfo.stockId && ss.productId == huskInfo.productId).FirstOrDefault();// && ss.sackWeight == particleInfo.quantity if (newStk == null) { STK_Balance stkBal = new STK_Balance(); var maxStkBal = context.STK_Balance.Select(p => p.ID).DefaultIfEmpty(0).Max(); stkBal.ID = ++maxStkBal; stkBal.productId = huskInfo.productId; stkBal.stockId = huskInfo.stockId; stkBal.sackQuantity = -huskInfo.noOfBag; context.STK_Balance.Add(stkBal); } else { newStk.sackQuantity -= huskInfo.noOfBag; } #endregion double?totalPr = 0; if (huskInfo.transportCostInclude) { totalPr = huskInfo.noOfBag * huskInfo.unitPrice * huskInfo.quantity + huskInfo.transportCost; } else { totalPr = huskInfo.noOfBag * huskInfo.unitPrice * huskInfo.quantity; } #region costing source tblCostingSource objCostingSource = context.tblCostingSources.Where(cc => cc.sellId == huskInfo.ID).FirstOrDefault(); if (huskInfo.transportCostInclude && objCostingSource != null) { context.tblCostingSources.Remove(objCostingSource); } else if (!huskInfo.transportCostInclude && objCostingSource == null) { tblCostingSource newCost = new tblCostingSource(); long maxCost = context.tblCostingSources.Select(i => i.ID).DefaultIfEmpty(0).Max(); newCost.ID = ++maxCost; newCost.amount = Convert.ToDouble(huskInfo.transportCost); newCost.partyId = huskInfo.partyId; newCost.sourceName = "তুষ"; newCost.srcDescription = "তুষ বিক্রয়ে পরিবহন খরচ"; newCost.srcDescId = 21; newCost.date = huskInfo.date; newCost.sellId = huskInfo.ID; context.tblCostingSources.Add(newCost); } else if (!huskInfo.transportCostInclude && objCostingSource != null) { objCostingSource.amount = Convert.ToDouble(huskInfo.transportCost); objCostingSource.date = huskInfo.date; } #endregion #region edit or insert income source var orgIncomeSrc = context.tblIncomeSources.Where(ii => ii.ID == huskInfo.incSrcId).FirstOrDefault(); if (orgIncomeSrc != null) { orgIncomeSrc.amount = huskInfo.paidAmount; orgIncomeSrc.date = huskInfo.date; orgIncomeSrc.partyId = huskInfo.partyId; } else if (orgIncomeSrc == null && huskInfo.paidAmount > 0) { long incId = context.tblIncomeSources.Select(i => i.ID).DefaultIfEmpty(0).Max(); tblIncomeSource incomeObj = new tblIncomeSource(); incomeObj.ID = ++incId; incomeObj.sourceName = "তুষ"; // shoul be come from commonelement incomeObj.srcDescId = 21; // should be come from commonelemnt incomeObj.description = "তুষ বিক্রয় বাবদ আয়"; incomeObj.amount = huskInfo.paidAmount; incomeObj.date = huskInfo.date; incomeObj.partyId = huskInfo.partyId; context.tblIncomeSources.Add(incomeObj); orgHuskSell.incSrcId = incomeObj.ID; } #endregion #region edit payable var orgPayable = context.tblPayables.Where(ss => ss.sellId == huskInfo.ID).FirstOrDefault(); //long lastpayId = context.tblPayables.Where(s => s.partyId == particleInfo.partyId).Select(l => l.ID).DefaultIfEmpty(0).Max(); //var lastPayable = context.tblPayables.Where(pp => pp.ID == lastpayId).FirstOrDefault(); if (orgPayable != null) { double?difference = totalPr - huskInfo.paidAmount - orgPayable.amount; orgPayable.openingBalance = orgPayable.openingBalance + difference; orgPayable.amount = totalPr - huskInfo.paidAmount; var nextPayables = context.tblPayables.Where(pp => pp.partyId == huskInfo.partyId && pp.ID > orgPayable.ID); foreach (var item in nextPayables) { item.openingBalance = item.openingBalance + difference; } } else if (orgPayable == null && totalPr > 0) { long maxpayId = context.tblPayables.Select(i => i.ID).DefaultIfEmpty(0).Max(); tblPayable objTblPayable = new tblPayable(); objTblPayable.ID = ++maxpayId; objTblPayable.partyId = huskInfo.partyId; objTblPayable.date = huskInfo.date; objTblPayable.sellId = huskInfo.ID; var lastPayable = context.tblPayables.Where(p => p.partyId == huskInfo.partyId && p.isActive == 1).FirstOrDefault(); var loan = totalPr - huskInfo.paidAmount; objTblPayable.amount = loan; if (lastPayable != null) { lastPayable.isActive = 0; objTblPayable.openingBalance = objTblPayable.amount + lastPayable.openingBalance; } else { objTblPayable.openingBalance = objTblPayable.amount; } objTblPayable.isActive = 1; context.tblPayables.Add(objTblPayable); } #endregion #region edit stock transaction var orgStkTrans = context.STK_Transaction.Where(ss => ss.sellId == orgHuskSell.ID).FirstOrDefault(); double diff = orgStkTrans.sellQty.Value - huskInfo.noOfBag; orgStkTrans.openingStock = orgStkTrans.openingStock + orgStkTrans.sellQty.Value; orgStkTrans.date = huskInfo.date; orgStkTrans.rcvQty = 0; orgStkTrans.sellQty = huskInfo.noOfBag; orgStkTrans.stockId = huskInfo.stockId.Value; orgStkTrans.prodId = huskInfo.productId; orgStkTrans.operation = 2; orgStkTrans.openingStock = orgStkTrans.openingStock - huskInfo.noOfBag; var nextStkTrans = context.STK_Transaction.Where(ss => ss.ID > orgStkTrans.ID && ss.prodId == orgStkTrans.prodId && ss.stockId == orgStkTrans.stockId); foreach (var item in nextStkTrans) { item.openingStock += diff; } #endregion #region edit tblsell orgHuskSell.productId = huskInfo.productId; orgHuskSell.productName = huskInfo.productName; orgHuskSell.noOfBag = huskInfo.noOfBag; orgHuskSell.paidAmount = huskInfo.paidAmount; orgHuskSell.partyId = huskInfo.partyId; orgHuskSell.partyName = huskInfo.partyName; orgHuskSell.quantity = huskInfo.quantity; orgHuskSell.stockId = huskInfo.stockId; orgHuskSell.stockName = huskInfo.stockName; orgHuskSell.unit = huskInfo.unit; orgHuskSell.unitPrice = huskInfo.unitPrice; orgHuskSell.transportCost = huskInfo.transportCost; #endregion tblSell newSell = new tblSell(); newSell.ID = huskInfo.ID; newSell.incSrcId = huskInfo.incSrcId; return(context.SaveChanges() > 0 ? newSell : null); } catch (Exception ex) { throw new Exception(ex.Message); } }