//DELETE public ActionResult Delete(int id) { try { using (linqDBContext db = new linqDBContext()) { var data = db.tblLots.Single(x => x.id == id); tblMainInventry mainInventry = (from us in db.tblMainInventries where us.mainCatId == data.mainCatId select us).FirstOrDefault(); //applying deletion to main inventry first mainInventry.pacA -= data.packets_A; mainInventry.pacB -= data.packets_B; mainInventry.small -= data.small; mainInventry.medium -= data.medium; mainInventry.large -= data.large; mainInventry.xLarge -= data.xLarge; db.tblLots.Remove(data); db.SaveChanges(); return Json(new { status = "success", Data = "done" }, JsonRequestBehavior.AllowGet); } } catch (Exception x) { return Json(new { status = "error", Data = x.Message }, JsonRequestBehavior.AllowGet); } }
//EDIT public ActionResult Edit(string id_, string cat, string name, string pacA, string pacB, string small, string med, string large, string xLarge) { try { int catId = Convert.ToInt32(cat); int id = Convert.ToInt32(id_); int uPacA, uPacB, uSmall, uMed, uLarge, uxLarge; using (linqDBContext db = new linqDBContext()) { tblLot query = (from us in db.tblLots where us.id == id select us).FirstOrDefault(); tblMainInventry mainInventry= (from us in db.tblMainInventries where us.mainCatId == catId select us).FirstOrDefault(); if (query != null) { //Calculating difference bw current values and updated values uPacA = Convert.ToInt32(pacA) > 0 ? Convert.ToInt32(pacA) - (int)(query.packets_A) : (int)(query.packets_A); uPacB = Convert.ToInt32(pacB) > 0 ? Convert.ToInt32(pacB) - (int)(query.packets_B) : (int)(query.packets_B); uSmall = Convert.ToInt32(small) > 0 ? Convert.ToInt32(small) - (int)(query.small) : (int)(query.small); uMed = Convert.ToInt32(med) > 0 ? Convert.ToInt32(med) - (int)(query.medium) : (int)(query.medium); uLarge = Convert.ToInt32(large) > 0 ? Convert.ToInt32(large) - (int)(query.large) : (int)(query.large); uxLarge = Convert.ToInt32(xLarge) > 0 ? Convert.ToInt32(xLarge) - (int)(query.xLarge) : (int)(query.xLarge); //Applying Changes to main inventry mainInventry.pacA += uPacA; mainInventry.pacB += uPacB; mainInventry.small += uSmall; mainInventry.medium += uMed; mainInventry.large += uLarge; mainInventry.xLarge += uxLarge; //apply changes to lot entry query.mainCatId = catId; query.lotName = name; query.packets_A =Convert.ToInt32(pacA); query.packets_B = Convert.ToInt32( pacB); query.small = Convert.ToInt32(small); query.medium = Convert.ToInt32 (med); query.large = Convert.ToInt32(large); query.xLarge = Convert.ToInt32(xLarge); db.SaveChanges(); return Json(new JsonResult() { Data = "Success" }, JsonRequestBehavior.AllowGet); } else { return Json(new JsonResult() { Data = "Not found!" }, JsonRequestBehavior.AllowGet); } } } catch (Exception x) { return Json(new { status = "error", Data = x.Message }, JsonRequestBehavior.AllowGet); } }
//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)); } }
//ADD public ActionResult Add(string cat, string name, string pacA, string pacB, string small, string med, string large, string xLarge) { try { int assignedID = 0; using (linqDBContext db = new linqDBContext()) { tblLot lt = new tblLot(); lt.mainCatId =Convert.ToInt32(cat); lt.lotName = name; lt.packets_A = Convert.ToInt32(pacA); lt.packets_B = Convert.ToInt32 (pacB); lt.small = Convert.ToInt32(small); lt.medium = Convert.ToInt32 (med); lt.large = Convert.ToInt32( large); lt.xLarge = Convert.ToInt32 (xLarge); db.tblLots.Add(lt); db.SaveChanges(); assignedID = lt.id; //update main inventry //var mainCatId = (from a in dc.tblLots // where a.id == lt.mainCatId // select a.mainCatId).ToList().First().Value; //int chkCatId = Convert.ToInt32(lt.mainCatId); var chkqry = (from b in dc.tblMainInventries where b.mainCatId == lt.mainCatId select b).FirstOrDefault(); if (chkqry == null) { tblMainInventry mI = new tblMainInventry(); mI.mainCatId = lt.mainCatId; mI.pacA = lt.packets_A; mI.pacB = lt.packets_B; mI.small = lt.small; mI.medium = lt.medium; mI.large = lt.large; mI.xLarge = lt.xLarge; dc.tblMainInventries.Add(mI); dc.SaveChanges(); } else { //update main invenrty chkqry.pacA += lt.packets_A; chkqry.pacB += lt.packets_B; chkqry.small += lt.small; chkqry.medium += lt.medium; chkqry.large += lt.large; chkqry.xLarge += lt.xLarge; dc.SaveChanges(); } } return Json(new JsonResult() { Data = assignedID }, JsonRequestBehavior.AllowGet); } catch (ApplicationException m) { return Json(new { status = "error", Data = m.Message }, JsonRequestBehavior.AllowGet); } catch (Exception x) { return Json(new { status = "error", Data = x.Message }, JsonRequestBehavior.AllowGet); } }
//edit public ActionResult Edit(string id_, string cat, string local, string lot, string localname, string pacA, string pacB, string small, string med, string large, string xLarge, string date, string isFromReturn) { try { int catId = Convert.ToInt32(cat); int id = Convert.ToInt32(id_); int loc = Convert.ToInt32(local); int lotid = Convert.ToInt32(lot); int uPacA, uPacB, uSmall, uMed, uLarge, uxLarge; using (linqDBContext db = new linqDBContext()) { var query = (from us in db.tblIssues where us.id == id select us).FirstOrDefault(); var issuedret = (from us in db.issuedRetailers where us.Id == query.issuedRetailId select us).FirstOrDefault(); var getLotId = (from us in db.tblLots where us.id == query.lotId select us).FirstOrDefault().mainCatId; tblMainInventry mainInventry = (from us in db.tblMainInventries where us.mainCatId == getLotId select us).FirstOrDefault(); if (query != null) { int totalS = 0, totalM = 0, totalL = 0, totalXL = 0, tPacA = 0, tPacB = 0; //Previous Qtys try { var ts = (from a in db.tblIssues where a.lotId == lotid && a.id != id select a.small.Value).Sum(); if (ts > 0) { totalS = ts; } } catch (Exception xz) { } try { var tm = (from a in db.tblIssues where a.lotId == lotid && a.id != id select a.medium.Value).Sum(); if (tm > 0) { totalM = tm; } } catch (Exception xz) { } try { var tl = (from a in db.tblIssues where a.lotId == lotid && a.id != id select a.large.Value).Sum(); if (tl > 0) { totalL = tl; } } catch (Exception xz) { } try { var tXL = (from a in db.tblIssues where a.lotId == lotid && a.id != id select a.xLarge.Value).Sum(); if (tXL > 0) { totalL = tXL; } } catch (Exception xz) { } try { var tA = (from a in db.tblIssues where a.lotId == lotid && a.id != id select a.packetA.Value).Sum(); if (tA > 0) { tPacA = tA; } } catch (Exception xz) { } try { var tB = (from a in db.tblIssues where a.lotId == lotid && a.id != id select a.packetB.Value).Sum(); if (tB > 0) { tPacB = tB; } } catch (Exception xz) { } //Check lot var lotQuery = (from us in db.tblLots where us.id == lotid select us).FirstOrDefault(); if (lotQuery != null) { if (Convert.ToInt32(pacA) + tPacA > lotQuery.packets_A) { throw new Exception("Cannot add more than " + (lotQuery.packets_A - tPacA) + " A packets."); } if (Convert.ToInt32(pacB) + tPacB > lotQuery.packets_B) { throw new Exception("Cannot add more than " + (lotQuery.packets_B - tPacB) + " B packets."); } if (Convert.ToInt32(small) + totalS > lotQuery.small) { throw new Exception("Cannot add more than " + (lotQuery.small - totalS) + " small items."); } if (Convert.ToInt32(med) + totalM > lotQuery.medium) { throw new Exception("Cannot add more than " + (lotQuery.medium - totalM) + " medium items."); } if (Convert.ToInt32(large) + totalL > lotQuery.large) { throw new Exception("Cannot add more than " + (lotQuery.large - totalL) + " large items."); } if (Convert.ToInt32(xLarge) + totalXL > lotQuery.xLarge) { throw new Exception("Cannot add more than " + (lotQuery.xLarge - totalXL) + " XL items."); } //applying changes to main inventry uPacA = Convert.ToInt32(pacA) > 0 ? Convert.ToInt32(pacA) - (int)(query.packetA) : (int)(query.packetA); uPacB = Convert.ToInt32(pacB) > 0 ? Convert.ToInt32(pacB) - (int)(query.packetB) : (int)(query.packetB); uSmall = Convert.ToInt32(small) > 0 ? Convert.ToInt32(small) - (int)(query.small) : (int)(query.small); uMed = Convert.ToInt32(med) > 0 ? Convert.ToInt32(med) - (int)(query.medium) : (int)(query.medium); uLarge = Convert.ToInt32(large) > 0 ? Convert.ToInt32(large) - (int)(query.large) : (int)(query.large); uxLarge = Convert.ToInt32(xLarge) > 0 ? Convert.ToInt32(xLarge) - (int)(query.xLarge) : (int)(query.xLarge); //Applying Changes to main inventry if (mainInventry.pacA >= uPacA) { mainInventry.pacA -= uPacA; } else { throw new Exception("Inventory have only " + mainInventry.pacA + " item(s) for packet A"); } if (mainInventry.pacB >= uPacB) { mainInventry.pacB -= uPacB; } else { throw new Exception("Inventory have only " + mainInventry.pacB + " item(s) for packet B"); } if (mainInventry.small >= uSmall) { mainInventry.small -= uSmall; } else { throw new Exception("Inventory have only " + mainInventry.small + " item(s) for small"); } if (mainInventry.medium >= uMed) { mainInventry.medium -= uMed; } else { throw new Exception("Inventory have only " + mainInventry.medium + " item(s) for medium"); } if (mainInventry.large >= uLarge) { mainInventry.large -= uLarge; } else { throw new Exception("Inventory have only " + mainInventry.large + " item(s) for large"); } if (mainInventry.xLarge >= uxLarge) { mainInventry.xLarge -= uxLarge; } else { throw new Exception("Inventory have only " + mainInventry.xLarge + " item(s) for xLarge"); } mainInventry.pacB -= uPacB; mainInventry.small -= uSmall; mainInventry.medium -= uMed; mainInventry.large -= uLarge; mainInventry.xLarge -= uxLarge; //applying changes to issue entry issuedret.retailId = catId; issuedret.localId = loc; issuedret.localName = localname; query.lotId = lotid; query.packetA = Convert.ToInt32(pacA); query.packetB = Convert.ToInt32(pacB); query.small = Convert.ToInt32(small); query.medium = Convert.ToInt32(med); query.large = Convert.ToInt32(large); query.xLarge = Convert.ToInt32(xLarge); query.issueDate = Convert.ToDateTime(date).Date; if (isFromReturn == "0") { query.isFromReturn = false; } else { query.isFromReturn = true; } db.SaveChanges(); } return(Json(new JsonResult() { Data = "Success" }, JsonRequestBehavior.AllowGet)); } else { return(Json(new JsonResult() { Data = "error" }, JsonRequestBehavior.AllowGet)); } } } catch (Exception x) { return(Json(new { status = "error", Data = x.Message }, JsonRequestBehavior.AllowGet)); } }