using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using StockManMVC.Models; namespace StockManMVC.Controllers { public class ItemsController : Controller { private StockManEntities db = new StockManEntities(); /// /// Selezione /// /// /// public static IEnumerable Select(string SearchVal) { StockManEntities db = new StockManEntities(); var answ = db.Item.Where(s => s.Descr.Contains(SearchVal) || s.DescrExt.Contains(SearchVal) || s.ItemFamilyID.Contains(SearchVal) || s.ItemFamily.Descr.Contains(SearchVal) || s.CodInt.Contains(SearchVal) || s.CodExt.Contains(SearchVal) ); return answ; } /// /// Ricerca con selezione parametri /// /// /// /// /// public static IEnumerable Order(string SearchVal, string OrderBy, string Sort) { IEnumerable answ = Select(SearchVal); switch (OrderBy) { case "codint": if (Sort == "dsc") { answ = answ.OrderByDescending(x => x.CodInt); } else { answ = answ.OrderBy(x => x.CodInt); } break; case "codext": if (Sort == "dsc") { answ = answ.OrderByDescending(x => x.CodExt); } else { answ = answ.OrderBy(x => x.CodExt); } break; case "descrext": if (Sort == "dsc") { answ = answ.OrderByDescending(x => x.DescrExt); } else { answ = answ.OrderBy(x => x.DescrExt); } break; case "family": if (Sort == "dsc") { answ = answ.OrderByDescending(x => x.ItemFamilyID); } else { answ = answ.OrderBy(x => x.ItemFamilyID); } break; case "qtapend": if (Sort == "dsc") { answ = answ.OrderByDescending(x => x.QtaPend); } else { answ = answ.OrderBy(x => x.QtaPend); } break; case "currvalue": if (Sort == "dsc") { answ = answ.OrderByDescending(x => x.CurrValue); } else { answ = answ.OrderBy(x => x.CurrValue); } break; case "totval": if (Sort == "dsc") { answ = answ.OrderByDescending(x => x.CurrValue * x.QtaPend); } else { answ = answ.OrderBy(x => x.CurrValue * x.QtaPend); } break; case "descr": default: if (Sort == "dsc") { answ = answ.OrderByDescending(x => x.Descr); } else { answ = answ.OrderBy(x => x.Descr); } break; } return answ; //return Select(SearchVal).OrderBy(x => x.Descr); } /// /// Ricerca con paginazione /// /// /// /// /// /// /// public static IEnumerable SelectPage(string SearchVal, string OrderBy, string Sort, int startRowIndex, int maximumRows) { return Order(SearchVal, OrderBy, Sort).Skip(startRowIndex * maximumRows).Take(maximumRows); } /// /// Elenco x famiglia ordine qta desc /// /// /// /// /// public ActionResult ListByFamily(string FamilyID) { if (FamilyID == null) FamilyID = ""; var item = db.Item.Where(s => s.ItemFamilyID == FamilyID).OrderByDescending(x=>x.QtaPend); return PartialView("ListByFamily", item); } // GET: Items/Consolidate/5?caller=callingController public ActionResult Consolidate(int? id, string caller) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } // chiamo stored x consolidamento flussi (da movimenti a giacenza confermata) using (var ctx = new StockManEntities()) { // esegue stored procedure come function, SE id == 0 processa TUTTI... int rowMod = ctx.stp_ItemConsolidate(id); } // se non specificati rimando a /Items/Details if (caller == "") caller = "Items"; // rimanda alla view principale da cui è stata fatta la chiamata... if (caller == "Items") { return RedirectToAction("Details", "Items", new { ID = id, StockItemID = id, FluxItemID = id }); } else { return RedirectToAction("Index", "vItemFlux2Save"); } } // GET: Items/ShrinkHistory/5?caller=callingController public ActionResult ShrinkHistory(int? id, string caller) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } // chiamo stored x consolidamento flussi (da movimenti a giacenza confermata) using (var ctx = new StockManEntities()) { // esegue stored procedure come function, SE id == 0 processa TUTTI... int rowMod = ctx.stp_ItemShrinkHist(id); } // se non specificati rimando a /Items/Details if (caller == "") caller = "Items"; // rimanda alla view principale da cui è stata fatta la chiamata... if (caller == "Items") { return RedirectToAction("Details", "Items", new { ID = id, StockItemID = id, FluxItemID = id }); } else { return RedirectToAction("Index", "vItemFlux2Save"); } } /// /// calcolo num risultati /// /// /// public static int SelectCount(string SearchVal) { return Select(SearchVal).Count(); } // GET: Items public ActionResult Index(int? id) { var item = db.Item.Include(i => i.ItemFamily); if (id == null) id = 0; ViewBag.ItemID = (int)id; return View(item.ToList()); } public ActionResult Grid() { var item = db.Item.Include(i => i.ItemFamily); return View(item.ToList()); } // GET: Items/Details/5 public ActionResult Details(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Item item = db.Item.Find(id); if (item == null) { return HttpNotFound(); } return View(item); } // GET: Items/CurrItemDetail/5 public ActionResult CurrItemDetail(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Item item = db.Item.Find(id); if (item == null) { return HttpNotFound(); } return PartialView("_ItemDetail", item); } // GET: Items/Create public ActionResult Create() { ViewBag.ItemFamilyID = new SelectList(db.ItemFamily, "ID", "Descr"); return View(); } // POST: Items/Create // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "ID,Descr,CodInt,ItemFamilyID,CodExt,DescrExt,QtaMin,QtaBatch,CurrValue")] Item item) { if (ModelState.IsValid) { db.Item.Add(item); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.ItemFamilyID = new SelectList(db.ItemFamily, "ID", "Descr", item.ItemFamilyID); return View(item); } // GET: Items/Duplicate/5 public ActionResult Duplicate(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } int newId = 0; // ora chiamo stored e recupero nuovo ID del record duplicato using (var ctx = new StockManEntities()) { // esegue stored procedure come function, recuperando nuovo ID creato... var newItem = ctx.stp_ItemDuplicate(id).ToList(); newId = newItem[0].ID; } // rimando in editing! return RedirectToAction("Edit", "Items", new { ID = newId, mode = "full" }); } // GET: Items/Edit/5 public ActionResult Edit(int? id, string mode) { if (mode == null) { mode = "std"; } if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Item item = db.Item.Find(id); if (item == null) { return HttpNotFound(); } ViewBag.ItemFamilyID = new SelectList(db.ItemFamily, "ID", "Descr", item.ItemFamilyID); ViewBag.mode = mode; if (mode == "std") { return PartialView("_Edit", item); } else { return View("Edit", item); } } #if false // GET: Items/Edit/5 public ActionResult EditFull(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Item item = db.Item.Find(id); if (item == null) { return HttpNotFound(); } ViewBag.ItemFamilyID = new SelectList(db.ItemFamily, "ID", "Descr", item.ItemFamilyID); return View("Edit", item); } #endif // POST: Items/Edit/5 // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include = "ID,Descr,CodInt,ItemFamilyID,CodExt,DescrExt,QtaMin,QtaBatch,CurrValue")] Item item) { if (ModelState.IsValid) { db.Entry(item).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Details", "Items", new { ID = item.ID, StockItemID = item.ID, FluxItemID = item.ID }); } ViewBag.ItemFamilyID = new SelectList(db.ItemFamily, "ID", "Descr", item.ItemFamilyID); return View("Edit", item); } // GET: Items/Delete/5 public ActionResult Delete(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Item item = db.Item.Find(id); if (item == null) { return HttpNotFound(); } return View(item); } // POST: Items/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(int id) { Item item = db.Item.Find(id); db.Item.Remove(item); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } }