Files
ayanova7/source/ri/ri/Controllers/PartController.cs
2018-06-29 19:47:36 +00:00

383 lines
14 KiB
C#

using System;
using System.Web.Mvc;
using GZTW.AyaNova.BLL;
using ri.util;
namespace ri.Controllers
{
public class PartController : Controller
{
#region Edit
//
// GET: /Part/Create
// Required intermediary route to avoid trouble with dupes created
//as new object ID needs to be 'cleaned'' from url with redirection
public ActionResult Create()
{
return RedirectToAction("Edit", new { id = AyaBizUtils.NewObjectGuid });
}
//
// GET: /Part/Edit/5
public ActionResult Edit(Guid id)
{
Part c = null;
if (id == AyaBizUtils.NewObjectGuid)
c = Part.NewItem();
else
c = Part.GetItem(id);
//serve up a 404 if not found
//Needs a custom 404 page, maybe return a custom view or something
if (c == null) return HttpNotFound();
formInit(c);
return View(c);
}
#endregion edit
#region Update
//
// POST: /Part/Edit/5
[HttpPost]
public ActionResult Edit(Guid id, FormCollection f)
{
Part c = null;
try
{
//retrieve the Part in question
c = Part.GetItem(id);
if (c == null) return HttpNotFound();
//update all the fields from collection
c.Active = ay.parseBool("Active", f);
c.AlternativeWholesalerID = ay.parseGuid("AlternativeWholesalerID", f);
c.AlternativeWholesalerNumber = f["AlternativeWholesalerNumber"];
c.Cost = ay.parseDecimal("Cost", f);
c.ManufacturerID = ay.parseGuid("ManufacturerID", f);
c.ManufacturerNumber = f["ManufacturerNumber"];
c.Name = f["Name"];
c.Notes = ay.ParseMultiLineText("Notes", f);
c.PartAssemblyID = ay.parseGuid("PartAssemblyID", f);
c.PartCategoryID = ay.parseGuid("PartCategoryID", f);
c.PartNumber = f["PartNumber"];
c.Retail = ay.parseDecimal("Retail", f);
c.TrackSerialNumber = ay.parseBool("TrackSerialNumber", f);
c.UnitOfMeasureID = ay.parseGuid("UnitOfMeasureID", f);
c.UPC = f["UPC"];
c.WholesalerID = ay.parseGuid("WholesalerID", f);
c.WholesalerNumber = f["WholesalerNumber"];
//handle update
if (!c.IsDirty) return RedirectToAction("Index");
if (c.IsSavable)
{
//all ok
c.Save();
formInit(c);
return View(c);
}
else
{
//Broken rules
ayBrokenRules rulesError = new ayBrokenRules(c);
ViewBag.err = rulesError;
formInit(c);
return View(c);
}
}
catch (Exception ex)
{
ay.setGeneralError(TempData, ex);
formInit(c);
return View(c);
}
}
#endregion update
#region Duplicate
//
// GET: /Part/Duplicate/5
public ActionResult Duplicate(Guid id)
{
Part c = Part.GetItem(id);
Part b = c.Duplicate();
b = (Part)b.Save();
return RedirectToAction("Edit", "Part", new { id = b.ID });
}
#endregion duplicate
#region Custom fields
//
// GET: /Part/CustomFields/5
public ActionResult CustomFields(Guid id)
{
Part c = null;
if (id == AyaBizUtils.NewObjectGuid)
throw new System.NotSupportedException("PartController->CustomFields route id==new object id!");
else
c = Part.GetItem(id);
//set rights
util.ay.setViewBagRights(ViewBag, RootObjectTypes.Part);
if (c == null) return HttpNotFound();
return View(c);
}
//
// POST: /Part/Edit/5
[HttpPost]
[ValidateInput(false)]
public ActionResult CustomFields(Guid id, FormCollection f)
{
Part c = null;
try
{
//retrieve the Part in question
c = Part.GetItem(id);
if (c == null) return HttpNotFound();
ay.getCustomFields(c, "Part", f);
//handle update
if (!c.IsDirty) return RedirectToAction("Edit", new { id = id });
if (c.IsSavable)
{
//all ok
c.Save();
//set rights
util.ay.setViewBagRights(ViewBag, RootObjectTypes.Part);
return View(c);
//return RedirectToAction("Edit", new { id = id });
}
else
{
//Broken rules
ayBrokenRules rulesError = new ayBrokenRules(c);
ViewBag.err = rulesError;
return View(c);
}
}
catch (Exception ex)
{
ay.setGeneralError(TempData, ex);
return View(c);
}
}
#endregion custom fields
#region Delete
//
// GET: /Part/Delete/5
public ActionResult Delete(Guid id, string ayf)
{
try
{
Part.DeleteItem(id);
return Redirect(ayf);
}
catch (Exception ex)
{
ay.setGeneralError(TempData, util.ay.GetSQLError(ex));
return RedirectToAction("Index", "Home");
}
}
#endregion delete
#region Inventory list
// GET: /Part/Inventory/5
public ActionResult Inventory(Guid id)
{
ViewBag.ItemId = id;
ViewBag.titleName = NameFetcher.GetItem(RootObjectTypes.Part, id, true).RecordName;
ViewBag.ayAdjustUrl = ay.genBaseSiteUrl(Url) + "Part/Adjust?id=" + id;
ViewBag.ayCanAdjust =
(AyaBizUtils.HasRight(RootObjectTypes.Part,SecurityLevelTypes.ReadWrite)) &&
(AyaBizUtils.HasRight(RootObjectTypes.PartInventoryAdjustment, SecurityLevelTypes.ReadWrite));
return View();
}
#endregion edit
#region inventory adjustment
private Part setUpAdjust(Guid partId)
{
util.ay.setViewBagRights(ViewBag, RootObjectTypes.PartInventoryAdjustment);
Part c = Part.GetItem(partId);
if (c == null) return c;
ViewBag.aySerialized = c.TrackSerialNumber;
ViewBag.ayPartId = c.ID;
ViewBag.titleName = PartPickList.GetOnePart(partId)[0].DisplayName(AyaBizUtils.GlobalSettings.DefaultPartDisplayFormat);
return c;
}
public ActionResult Adjust(Guid id)
{
ViewBag.fromSavedAdjust = 0;
//util.ay.setViewBagRights(ViewBag, RootObjectTypes.PartInventoryAdjustment);
//Part c = Part.GetItem(id);
//ViewBag.aySerialized = c.TrackSerialNumber;
//ViewBag.ayPartId = c.ID;
//ViewBag.titleName = PartPickList.GetOnePart(id)[0].DisplayName(AyaBizUtils.GlobalSettings.DefaultPartDisplayFormat);
Part c = setUpAdjust(id);
//serve up a 404 if not found
//Needs a custom 404 page, maybe return a custom view or something
if (c == null) return HttpNotFound();
formInit(c);
return View(c);
}
[HttpPost]
public ActionResult Adjust(Guid id, FormCollection f)
{
try
{
PartInventoryAdjustment p = PartInventoryAdjustment.NewItem();
p.DateAdjusted = ay.ParseDateToDbValue("DateAdjusted", f);
p.ReasonForAdjustment = ay.ParseMultiLineText("ReasonForAdjustment", f);
PartInventoryAdjustmentItem i= p.Items.Add(p);
i.PartID = ay.parseGuid("ayPartId", f);
i.PartWarehouseID = ay.parseGuid("PartWarehouseID", f);
i.QuantityAdjustment = ay.parseDecimal("QuantityAdjustment", f);
//is it serialized?
if (i.SerialNumbers.Count > 0)//i.serialnumbers collection is automatically created if the part is serialized
{
decimal snQuantity = Math.Abs(i.QuantityAdjustment);
if (i.QuantityAdjustment < 0)
{
//parse out existing sn's
string[] snIDArray = f["snlist"].Split(',');
for (int x = 0; x < snQuantity; x++)
{
PartSerial ps = i.SerialNumbers[x];
ps.PartID = i.PartID;
ps.PartWarehouseID = i.PartWarehouseID;
//adjustment item expects sn list to actually contain the Guid of the
//existing partserial being returned, it will handle fixing that up
//automatically
ps.SerialNumber = snIDArray[x];
}
}
else if (i.QuantityAdjustment > 0)
{
//parse out text boxes
for (int x = 0; x < snQuantity; x++)
{
PartSerial ps = i.SerialNumbers[x];
ps.PartID = i.PartID;
ps.PartWarehouseID = i.PartWarehouseID;
ps.SerialNumber = f["EDSN_" + x.ToString()];
}
}
} //if serialized
if (p.IsSavable)
{
//all ok
p.Save();
//return RedirectToAction("Adjust", new { id = id });
ViewBag.fromSavedAdjust = 1;
}
else
{
//Broken rules
ayBrokenRules rulesError = new ayBrokenRules(p);
ViewBag.err = rulesError;
ViewBag.fromSavedAdjust = 0;
//return RedirectToAction("Inventory", new { id = id });
}
Part c = setUpAdjust(id);
//serve up a 404 if not found
//Needs a custom 404 page, maybe return a custom view or something
if (c == null) return HttpNotFound();
formInit(c);
return View(c);
}
catch (Exception ex)
{
ay.setGeneralError(TempData, util.ay.GetSQLError(ex));
return RedirectToAction("Inventory", new { id = id });
}
}
#endregion inventory adjustment
#region Menu and rights
private void formInit(Part c)
{
//set rights
util.ay.setViewBagRights(ViewBag, RootObjectTypes.Part);
string baseurl = ay.genBaseSiteUrl(Url);
string sId = c.ID.ToString();
string sType = ((int)RootObjectTypes.Part).ToString();
#region Generate menu
ayMenu m = new ayMenu();
m.title = "O.Part";
m.icon = "icon-Part";
if (c.CanWiki)
{
m.menuItems.Add(new ayMenuItem("navitem", "O.WikiPage", ay.WikiUrl(RootObjectTypes.Part, c.ID, Url), "icon-WikiPage"));
m.menuItems.Add(new ayMenuItem("navitem", "O.AyaFile", ay.WikiFileUrl(RootObjectTypes.Part, c.ID, Url), "icon-WikiFile"));
}
//PART INVENTORY
if (AyaBizUtils.GlobalSettings.UseInventory)
{
m.menuItems.Add(new ayMenuItem("navitem", "PartByWarehouseInventory.Label.List", baseurl + "Part/Inventory?id=" + sId, "icon-PartByWarehouseInventory"));
}
if (ay.customFieldsAvailable("Part"))
{
m.menuItems.Add(new ayMenuItem("navitem", "ObjectCustomField.Label.CustomGrid", baseurl + "Part/CustomFields?id=" + sId, "icon-CustomFields"));
}
//---COMMAND ITEMS---
m.menuItems.Add(new ayMenuItem("divider", "", "", ""));
//CREATE
if (ViewBag.ayCanEdit)
m.menuItems.Add(new ayMenuItem("navitem", "UI.Toolbar.New", baseurl + "Part/Create", "icon-Add"));//UI.Toolbar.New
//DUPLICATE
if (c.CanDuplicate)
m.menuItems.Add(new ayMenuItem("navitem", "UI.Command.Duplicate", baseurl + "Part/Duplicate?id=" + sId, "icon-Duplicate", "", "ay-set-ret-url"));
//RECORD HISTORY
m.menuItems.Add(new ayMenuItem("scriptitem", "UI.Command.RecordHistory", "alert('" +
ay.RecordHistoryText(c.Creator, c.Modifier, c.Created, c.Modified) +
"')", "icon-RecordHistory"));
//DELETE
if (ViewBag.ayCanDelete)
{
m.menuItems.Add(new ayMenuItem("navitem", "UI.Command.Delete", baseurl + "Part/Delete?id=" + sId, "icon-Delete", ay.PromptForDelete(), "ay-set-ret-url"));
}
ViewBag.ayMenu = m;
#endregion generate menu
}
#endregion
//eoc
}
}