using StockManMVC.Models; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web.Configuration; using System.Web.Mvc; 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 .Include(s => s.ItemFlux) .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) || s.ItemFlux.Any(a => a.CodDoc.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 }); } 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 }); } 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, int? page, int? pageSize, string enabFR) { if (page == null) page = 1; if (pageSize == null) pageSize = 10; ViewBag.page = page; ViewBag.pageSize = pageSize; ViewBag.BaseUrl = System.Web.Configuration.WebConfigurationManager.AppSettings["BaseUrl"]; if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Item item = db.Item.Find(id); if (item == null) { return HttpNotFound(); } // !!!FIXME!!! modificare x abilitare SOLO con ruoli // 2017.01.03 aggiunto controllo x permesso a resettare INTERA catena flussi + stock... SE in URL si trova comando enabFR=enabFR_key (da webconfig)... ViewBag.DisplFR = "disabled invisible zeroWidth"; bool enableAll = false; bool.TryParse(WebConfigurationManager.AppSettings["enabFR_all"], out enableAll); if (enabFR != null || enableAll) { string enabFR_key = WebConfigurationManager.AppSettings["enabFR_key"]; if (enabFR == enabFR_key || enableAll) ViewBag.DisplFR = ""; } // restituisco vista parziale 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/ResetAll/5 public ActionResult ResetAll(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Item item = db.Item.Find(id); if (item == null) { return HttpNotFound(); } // se ha trovato l'item ORA RESETTA TUTTO sia in stock che in flussi (toglie registrazione di tutti i movimenti) // chiamo stored x consolidamento flussi (da movimenti a giacenza confermata) using (var ctx = new StockManEntities()) { // esegue stored procedure come function... ctx.stp_ItemResetAll(id); } // restituisco view! //return PartialView("_ItemDetail", item); return RedirectToAction("Details", "Items", new { ID = id, StockItemID = id }); } // GET: Items/Create (mode=std|full, opzionale) public ActionResult Create(string mode) { if (mode == null) { mode = "full"; } ViewBag.ItemFamilyID = new SelectList(db.ItemFamily, "ID", "Descr"); if (mode == "std") { return PartialView(); } else { 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,UM")] Item item) { if (item.CodExt == null) item.CodExt = ""; if (item.DescrExt == null) item.DescrExt = ""; if (item.UM == null) item.UM = ""; 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); } } // 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,UM")] Item item) { if (item.CodExt == null) item.CodExt = ""; if (item.DescrExt == null) item.DescrExt = ""; if (ModelState.IsValid) { db.Entry(item).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Details", "Items", new { ID = item.ID, StockItemID = 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, 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(); } if (mode == "std") { return PartialView("_Delete", item); } else { return View("Delete", 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); } } }