459 lines
19 KiB
C#
459 lines
19 KiB
C#
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 ItemFluxesController : Controller
|
|
{
|
|
private StockManEntities db = new StockManEntities();
|
|
|
|
/// <summary>
|
|
/// Selezione
|
|
/// </summary>
|
|
/// <param name="ItemID"></param>
|
|
/// <param name="SearchVal"></param>
|
|
/// <returns></returns>
|
|
public static IEnumerable<ItemFlux> Select(int? ItemID, string SearchVal)
|
|
{
|
|
StockManEntities db = new StockManEntities();
|
|
if (ItemID == null)
|
|
{
|
|
ItemID = 0;
|
|
}
|
|
var answ = db.ItemFlux.Where(s => s.ItemID == ItemID
|
|
&& (
|
|
s.Note.Contains(SearchVal)
|
|
|| s.Location.Descr.Contains(SearchVal)
|
|
|| s.Location1.Descr.Contains(SearchVal)
|
|
|| s.Operator.LastName.Contains(SearchVal)
|
|
|| s.Operator.FirstName.Contains(SearchVal)
|
|
|| s.MovType.Descr.Contains(SearchVal)
|
|
));
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// Ricerca con selezione parametri
|
|
/// </summary>
|
|
/// <param name="ItemID"></param>
|
|
/// <param name="SearchVal"></param>
|
|
/// <param name="OrderBy"></param>
|
|
/// <param name="Sort"></param>
|
|
/// <returns></returns>
|
|
public static IEnumerable<ItemFlux> Order(int? ItemID, string SearchVal, string OrderBy, string Sort)
|
|
{
|
|
IEnumerable<ItemFlux> answ = Select(ItemID, SearchVal);
|
|
switch (OrderBy)
|
|
{
|
|
case "note":
|
|
if (Sort == "dsc")
|
|
{
|
|
answ = answ.OrderByDescending(x => x.Note);
|
|
}
|
|
else
|
|
{
|
|
answ = answ.OrderBy(x => x.Note);
|
|
}
|
|
break;
|
|
case "Posizione":
|
|
if (Sort == "dsc")
|
|
{
|
|
answ = answ.OrderByDescending(x => x.Location.Descr);
|
|
}
|
|
else
|
|
{
|
|
answ = answ.OrderBy(x => x.Location.Descr);
|
|
}
|
|
break;
|
|
case "Tipo":
|
|
if (Sort == "dsc")
|
|
{
|
|
answ = answ.OrderByDescending(x => x.MovType.Descr);
|
|
}
|
|
else
|
|
{
|
|
answ = answ.OrderBy(x => x.MovType.Descr);
|
|
}
|
|
break;
|
|
case "Riferimento":
|
|
if (Sort == "dsc")
|
|
{
|
|
answ = answ.OrderByDescending(x => x.Location1.Descr);
|
|
}
|
|
else
|
|
{
|
|
answ = answ.OrderBy(x => x.Location1.Descr);
|
|
}
|
|
break;
|
|
case "Operatore":
|
|
if (Sort == "dsc")
|
|
{
|
|
answ = answ.OrderByDescending(x => x.Operator.LastName);
|
|
}
|
|
else
|
|
{
|
|
answ = answ.OrderBy(x => x.Operator.LastName);
|
|
}
|
|
break;
|
|
case "dtMov":
|
|
default:
|
|
if (Sort == "dsc")
|
|
{
|
|
answ = answ.OrderByDescending(x => x.ID);
|
|
}
|
|
else
|
|
{
|
|
answ = answ.OrderBy(x => x.ID);
|
|
}
|
|
break;
|
|
}
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// Ricerca con paginazione
|
|
/// </summary>
|
|
/// <param name="ItemID"></param>
|
|
/// <param name="SearchVal"></param>
|
|
/// <param name="OrderBy"></param>
|
|
/// <param name="Sort"></param>
|
|
/// <param name="startRowIndex"></param>
|
|
/// <param name="maximumRows"></param>
|
|
/// <returns></returns>
|
|
public static IEnumerable<ItemFlux> SelectPage(int? ItemID, string SearchVal, string OrderBy, string Sort, int startRowIndex, int maximumRows)
|
|
{
|
|
return Order(ItemID, SearchVal, OrderBy, Sort).Skip(startRowIndex * maximumRows).Take(maximumRows);
|
|
}
|
|
/// <summary>
|
|
/// calcolo num risultati
|
|
/// </summary>
|
|
/// <param name="ItemID"></param>
|
|
/// <param name="SearchVal"></param>
|
|
/// <returns></returns>
|
|
public static int SelectCount(int? ItemID, string SearchVal)
|
|
{
|
|
return Select(ItemID, SearchVal).Count();
|
|
}
|
|
|
|
// GET: ItemFluxes
|
|
public ActionResult Index()
|
|
{
|
|
var itemFlux = db.ItemFlux.Include(i => i.Location).Include(i => i.MovType).Include(i => i.Item).Include(i => i.Operator).Include(i => i.Location1);
|
|
return View(itemFlux.ToList());
|
|
}
|
|
|
|
// GET: ItemFluxes/Details/5
|
|
public ActionResult Details(int? id)
|
|
{
|
|
if (id == null)
|
|
{
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|
}
|
|
ItemFlux itemFlux = db.ItemFlux.Find(id);
|
|
if (itemFlux == null)
|
|
{
|
|
return HttpNotFound();
|
|
}
|
|
return View(itemFlux);
|
|
}
|
|
|
|
// GET: ItemFluxes/Create
|
|
public ActionResult Create()
|
|
{
|
|
ViewBag.LocationID = new SelectList(db.Location, "ID", "Descr");
|
|
ViewBag.MovTypeID = new SelectList(db.MovType, "ID", "Descr");
|
|
ViewBag.ItemID = new SelectList(db.Item, "ID", "Descr");
|
|
ViewBag.OperatorID = new SelectList(db.Operator, "ID", "CodExt");
|
|
ViewBag.ExtLocationID = new SelectList(db.Location, "ID", "Descr");
|
|
return View();
|
|
}
|
|
|
|
// POST: ItemFluxes/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,ItemID,LocationID,MovTypeID,ExtLocationID,dtMov,Qta,TotValue,UnitVal,Note,dtExport,OperatorID")] ItemFlux itemFlux)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
db.ItemFlux.Add(itemFlux);
|
|
db.SaveChanges();
|
|
return RedirectToAction("Index");
|
|
}
|
|
|
|
ViewBag.LocationID = new SelectList(db.Location, "ID", "Descr", itemFlux.LocationID);
|
|
ViewBag.MovTypeID = new SelectList(db.MovType, "ID", "Descr", itemFlux.MovTypeID);
|
|
ViewBag.ItemID = new SelectList(db.Item, "ID", "Descr", itemFlux.ItemID);
|
|
ViewBag.OperatorID = new SelectList(db.Operator, "ID", "CodExt", itemFlux.OperatorID);
|
|
ViewBag.ExtLocationID = new SelectList(db.Location, "ID", "Descr", itemFlux.ExtLocationID);
|
|
return View(itemFlux);
|
|
}
|
|
|
|
// GET: ItemFluxes/Create with params preselected
|
|
public ActionResult CreatePrecompiled(int ItemID, string MovTypeID)
|
|
{
|
|
ViewBag.LocationID = new SelectList(db.Location.Where(o => o.LocType.IsStock), "ID", "Descr");
|
|
ViewBag.MovTypeID = new SelectList(db.MovType, "ID", "Descr", MovTypeID);
|
|
ViewBag.ItemID = new SelectList(db.Item, "ID", "Descr", ItemID);
|
|
ViewBag.enabEditVal = true;
|
|
// preparo query degli scaffali ammissibili
|
|
// recupero SOLO gli scaffali ammissibili
|
|
var query = db.Location.Where(o => o.LocType.IsStock).Join(db.ItemStock.Where(s => s.ItemID == ItemID),
|
|
l => l.ID,
|
|
s => s.LocationID,
|
|
(l, s) => new { l.ID, l.Descr });
|
|
// in base al tipo di mov richiesto determino cosa mostrare...
|
|
switch (MovTypeID)
|
|
{
|
|
case "CAR":
|
|
// per partire scelgo la posizione di default ric ordini
|
|
string stdLocationId = "RIC-ORD";
|
|
// cerco se ci sia una posizione occupata dall'item in stock..
|
|
if (db.ItemStock.Where(s => s.ItemID == ItemID).Count() > 0)
|
|
{
|
|
stdLocationId = db.ItemStock.Where(s => s.ItemID == ItemID).ToList()[0].LocationID;
|
|
}
|
|
ViewBag.LocationID = new SelectList(db.Location.Where(o => o.LocType.IsStock), "ID", "Descr", stdLocationId);
|
|
ViewBag.ExtLocationID = new SelectList(db.Location.Where(o => o.LocType.IsFor), "ID", "Descr");
|
|
break;
|
|
case "SCAR":
|
|
if (query.Count() > 0)
|
|
{
|
|
ViewBag.LocationID = new SelectList(query, "ID", "Descr");
|
|
}
|
|
ViewBag.ExtLocationID = new SelectList(db.Location.Where(o => o.LocType.IsCli), "ID", "Descr");
|
|
ViewBag.enabEditVal = false;
|
|
break;
|
|
case "MOV":
|
|
ViewBag.ExtLocationID = new SelectList(db.Location.Where(o => o.LocType.IsStock), "ID", "Descr");
|
|
ViewBag.enabEditVal = false;
|
|
break;
|
|
case "OFOR":
|
|
ViewBag.LocationID = new SelectList(db.Location.Where(o => o.LocType.IsStock), "ID", "Descr", "RIC-ORD");
|
|
ViewBag.ExtLocationID = new SelectList(db.Location.Where(o => o.LocType.IsFor), "ID", "Descr");
|
|
break;
|
|
case "RETT":
|
|
if (query.Count() > 0)
|
|
{
|
|
ViewBag.LocationID = new SelectList(query, "ID", "Descr");
|
|
}
|
|
ViewBag.ExtLocationID = new SelectList(db.Location, "ID", "Descr");
|
|
ViewBag.enabEditVal = false;
|
|
break;
|
|
case "ND":
|
|
default:
|
|
ViewBag.ExtLocationID = new SelectList(db.Location, "ID", "Descr");
|
|
break;
|
|
}
|
|
|
|
// aggiungo valori default...
|
|
|
|
ItemFlux itemFlux = new ItemFlux();
|
|
itemFlux.dtMov = DateTime.Now.Date;
|
|
// alore propongo corrente
|
|
return PartialView("_CreatePrecompiled", itemFlux);
|
|
}
|
|
|
|
// POST: ItemFluxes/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 CreatePrecompiled([Bind(Include = "ID,ItemID,LocationID,MovTypeID,ExtLocationID,dtMov,Qta,TotValue,UnitVal,CodDoc,Note")] ItemFlux itemFlux)
|
|
{
|
|
// aggiungo campi mancanti...
|
|
if (itemFlux.dtMov == null) itemFlux.dtMov = DateTime.Now;
|
|
if (itemFlux.Note == null) itemFlux.Note = "";
|
|
if (itemFlux.CodDoc == null) itemFlux.CodDoc = "";
|
|
itemFlux.dtExport = null;
|
|
string currOpID = "ND"; // default
|
|
try
|
|
{
|
|
var trovato = db.Operator.SingleOrDefault(s => s.CodExt == User.Identity.Name || s.CodExt.Contains(User.Identity.Name) || s.ID == User.Identity.Name);
|
|
Operator currOp = (Operator)trovato;
|
|
currOpID = currOp.ID;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
}
|
|
itemFlux.OperatorID = currOpID;
|
|
itemFlux.ModOperatorID = currOpID;
|
|
|
|
// verifico Qta: SE CAR > 0, se SCAR < 0, se MOV / rett libero...
|
|
switch (itemFlux.MovTypeID)
|
|
{
|
|
case "CAR":
|
|
itemFlux.Qta = Math.Abs(itemFlux.Qta);
|
|
break;
|
|
case "SCAR":
|
|
itemFlux.Qta = -Math.Abs(itemFlux.Qta);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
db.ItemFlux.Add(itemFlux);
|
|
db.SaveChanges();
|
|
// rimando a pag esterna...
|
|
return RedirectToAction("Details", "Items", new { ID = itemFlux.ItemID, StockItemID = itemFlux.ItemID, FluxItemID = itemFlux.ItemID });
|
|
//return RedirectToAction("Index");
|
|
}
|
|
|
|
ViewBag.LocationID = new SelectList(db.Location, "ID", "Descr", itemFlux.LocationID);
|
|
ViewBag.MovTypeID = new SelectList(db.MovType, "ID", "Descr", itemFlux.MovTypeID);
|
|
ViewBag.ItemID = new SelectList(db.Item, "ID", "Descr", itemFlux.ItemID);
|
|
ViewBag.OperatorID = new SelectList(db.Operator, "ID", "CodExt", itemFlux.OperatorID);
|
|
ViewBag.ExtLocationID = new SelectList(db.Location, "ID", "Descr", itemFlux.ExtLocationID);
|
|
|
|
return View("CreatePrecompiled", itemFlux);
|
|
}
|
|
|
|
// GET: ItemFluxes/ConvOrder/5 converte da ORDINE a CARICO e manda in edit...
|
|
public ActionResult ConvOrder(int? id)
|
|
{
|
|
if (id == null)
|
|
{
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|
}
|
|
ItemFlux itemFlux = db.ItemFlux.Find(id);
|
|
if (itemFlux == null)
|
|
{
|
|
return HttpNotFound();
|
|
}
|
|
// converto ordine in carico!
|
|
if (itemFlux.MovTypeID == "OFOR")
|
|
{
|
|
itemFlux.MovTypeID = "CAR";
|
|
db.SaveChanges();
|
|
// rileggo dettaglio!
|
|
}
|
|
return RedirectToAction("Details", "Items", new { ID = itemFlux.ItemID, StockItemID = itemFlux.ItemID, FluxItemID = itemFlux.ItemID });
|
|
}
|
|
|
|
// GET: ItemFluxes/Edit/5
|
|
public ActionResult Edit(int? id)
|
|
{
|
|
if (id == null)
|
|
{
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|
}
|
|
ItemFlux itemFlux = db.ItemFlux.Find(id);
|
|
if (itemFlux == null)
|
|
{
|
|
return HttpNotFound();
|
|
}
|
|
ViewBag.LocationID = new SelectList(db.Location, "ID", "Descr", itemFlux.LocationID);
|
|
ViewBag.MovTypeID = new SelectList(db.MovType, "ID", "Descr", itemFlux.MovTypeID);
|
|
ViewBag.ItemID = new SelectList(db.Item, "ID", "Descr", itemFlux.ItemID);
|
|
ViewBag.OperatorID = new SelectList(db.Operator, "ID", "CodExt", itemFlux.OperatorID);
|
|
ViewBag.ExtLocationID = new SelectList(db.Location, "ID", "Descr", itemFlux.ExtLocationID);
|
|
return View(itemFlux);
|
|
}
|
|
|
|
// POST: ItemFluxes/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,LocationID,ExtLocationID,dtMov,Qta,TotValue,UnitVal,CodDoc,Note,dtExport")] ItemFlux itemFlux)
|
|
//public ActionResult Edit([Bind(Include = "ID,ItemID,LocationID,MovTypeID,ExtLocationID,dtMov,Qta,TotValue,UnitVal,CodDoc,Note,dtExport,OperatorID")] ItemFlux itemFlux)
|
|
{
|
|
// recupero vecchi valori...
|
|
ItemFlux itemFluxOld = db.ItemFlux.Find(itemFlux.ID);
|
|
// fix operatore modifica
|
|
string currOpIDMod = "ND"; // default
|
|
try
|
|
{
|
|
var trovato = db.Operator.SingleOrDefault(s => s.CodExt == User.Identity.Name || s.CodExt.Contains(User.Identity.Name) || s.ID == User.Identity.Name);
|
|
Operator currOp = (Operator)trovato;
|
|
currOpIDMod = currOp.ID;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
}
|
|
itemFlux.ModOperatorID = currOpIDMod;
|
|
// imposto valori vecchi x campi "bloccati" (articolo, tipo movimento, operatore iniziale...)
|
|
itemFlux.ItemID = itemFluxOld.ItemID;
|
|
itemFlux.MovTypeID = itemFluxOld.MovTypeID;
|
|
if (itemFlux.OperatorID == null) itemFlux.OperatorID = itemFluxOld.OperatorID;
|
|
// continuo
|
|
if (ModelState.IsValid)
|
|
{
|
|
// verifico: se è un carico --> positivo, scarico --> negativo!
|
|
switch (itemFlux.MovTypeID)
|
|
{
|
|
case "CAR":
|
|
itemFlux.Qta = Math.Abs(itemFlux.Qta);
|
|
break;
|
|
case "SCAR":
|
|
itemFlux.Qta = -Math.Abs(itemFlux.Qta);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
// sblocco vecchio item x non fare conflitto
|
|
db.Entry(itemFluxOld).State = EntityState.Detached;
|
|
// indico che devo salvare nuovo!
|
|
db.Entry(itemFlux).State = EntityState.Modified;
|
|
db.SaveChanges();
|
|
|
|
// rimando pag dettaglio...
|
|
return RedirectToAction("Details", "Items", new { ID = itemFlux.ItemID, StockItemID = itemFlux.ItemID, FluxItemID = itemFlux.ItemID });
|
|
//return RedirectToAction("Index");
|
|
}
|
|
ViewBag.LocationID = new SelectList(db.Location, "ID", "Descr", itemFlux.LocationID);
|
|
ViewBag.MovTypeID = new SelectList(db.MovType, "ID", "Descr", itemFlux.MovTypeID);
|
|
ViewBag.ItemID = new SelectList(db.Item, "ID", "Descr", itemFlux.ItemID);
|
|
ViewBag.OperatorID = new SelectList(db.Operator, "ID", "CodExt", itemFlux.OperatorID);
|
|
ViewBag.ExtLocationID = new SelectList(db.Location, "ID", "Descr", itemFlux.ExtLocationID);
|
|
|
|
// rimando pag dettaglio...
|
|
return View(itemFlux);
|
|
}
|
|
|
|
// GET: ItemFluxes/Delete/5
|
|
public ActionResult Delete(int? id)
|
|
{
|
|
if (id == null)
|
|
{
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|
}
|
|
ItemFlux itemFlux = db.ItemFlux.Find(id);
|
|
if (itemFlux == null)
|
|
{
|
|
return HttpNotFound();
|
|
}
|
|
return View(itemFlux);
|
|
}
|
|
|
|
// POST: ItemFluxes/Delete/5
|
|
[HttpPost, ActionName("Delete")]
|
|
[ValidateAntiForgeryToken]
|
|
public ActionResult DeleteConfirmed(int id)
|
|
{
|
|
ItemFlux itemFlux = db.ItemFlux.Find(id);
|
|
db.ItemFlux.Remove(itemFlux);
|
|
db.SaveChanges();
|
|
//return RedirectToAction("Index");
|
|
return RedirectToAction("Details", "Items", new { ID = itemFlux.ItemID, StockItemID = itemFlux.ItemID, FluxItemID = itemFlux.ItemID });
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
db.Dispose();
|
|
}
|
|
base.Dispose(disposing);
|
|
}
|
|
}
|
|
}
|