//add public ActionResult Add(string reason, string bit) { try { int localid_ = Convert.ToInt32(TempData.Peek("local")); int retailid_ = Convert.ToInt32(TempData.Peek("type")); // ViewBag.localid = localid_; if (localid_ == 0 || retailid_ == 0) { throw new Exception("No items In Table To Return"); } using (linqDBContext db = new linqDBContext()) { var v = (from a in db.tblSubInventories join c in db.tblItems on a.itemId equals c.id join d in db.issuedRetailers on a.issueRetailId equals d.Id where d.localId == localid_ && d.retailId == retailid_ select new { id = a.id, issuedid = a.issueRetailId, itmid = c.id, small = a.small, medium = a.medium, large = a.large, xlarge = a.xLarge }); int firstTime = 0; int counter = 0; foreach (var item in v) { tblSubInventory tsi = (from a in dc.tblSubInventories where a.itemId == item.itmid && a.issueRetailId == item.issuedid select a).FirstOrDefault(); tblreturnHistoryDetail trd = new tblreturnHistoryDetail(); if (firstTime == 0) { var chk = (from a in dc.tblreturnHistoryDetails select a).ToList(); if (chk.Count == 0) { counter = 0; } else { counter = chk.Last().RID.Value; } counter++; firstTime = 1; } trd.RID = counter; trd.issueRetailId = item.issuedid; trd.itmId = item.itmid; trd.small = item.small; trd.medium = item.medium; trd.large = item.large; trd.xlarge = item.xlarge; trd.date = DateTimeOffset.Now.Date.ToShortDateString(); dc.tblreturnHistoryDetails.Add(trd); dc.SaveChanges(); tsi.small -= item.small; tsi.medium -= item.medium; tsi.large -= item.large; tsi.xLarge -= item.xlarge; // dc.tblSubInventories.Remove(tsi); dc.SaveChanges(); } // now group the ReturnHistorydetail data Category wise and save it ito Returnhistory Table tblReturnHistory Rh; var xx = (from a in dc.tblreturnHistoryDetails where a.RID == counter join b in dc.tblItems on a.itmId equals b.id select new { isu = a.issueRetailId, cat = b.mainCategory, small = a.small, med = a.medium, large = a.large, xlarge = a.xlarge, reason = a.reason }).GroupBy(x => x.cat); foreach (var item in xx) { int getcat = item.First().cat.Value; Rh = new tblReturnHistory(); int S = 0, M = 0, L = 0, XL = 0; foreach (var list in item) { S += (int)list.small; M += (int)list.med; L += (int)list.large; XL += (int)list.xlarge; Rh.issueRetailId = list.isu; } Rh.small = S; Rh.medium = M; Rh.large = L; Rh.xLarge = XL; Rh.category_Id = getcat; Rh.date = DateTimeOffset.Now.Date; if (bit.Equals("True")) { // if checkbox is cheked he want to return to main inventory then... Rh.save_to_main_inventory = true; // tblMainInventry tmi; var tmi = (from a in dc.tblMainInventries where a.mainCatId == getcat select a).FirstOrDefault(); if (tmi != null) { tmi.small += S; tmi.medium += M; tmi.large += L; tmi.xLarge += XL; db.Entry(tmi).State = EntityState.Modified; db.SaveChanges(); } else { tmi = new tblMainInventry(); tmi.mainCatId = getcat; tmi.small = S; tmi.medium = M; tmi.large = L; tmi.xLarge = XL; db.tblMainInventries.Add(tmi); db.SaveChanges(); } } else { //if checkbox is uncheked then items will save into return inventory Rh.save_to_main_inventory = false; //tblReturnInventory tri; var tri = (from a in dc.tblReturnInventories where a.mainCatId == getcat select a).FirstOrDefault(); if (tri != null) { tri.small += S; tri.medium += M; tri.large += L; tri.xLarge += XL; } else { tri = new tblReturnInventory(); tri.mainCatId = getcat; tri.small = S; tri.medium = M; tri.large = L; tri.xLarge = XL; db.tblReturnInventories.Add(tri); } } db.tblReturnHistories.Add(Rh); db.SaveChanges(); } return(Json(new JsonResult() { Data = "Success" }, JsonRequestBehavior.AllowGet)); } } catch (Exception x) { return(Json(new { status = "error", Data = x.Message }, JsonRequestBehavior.AllowGet)); } }