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

223 lines
8.6 KiB
C#

using System;
using System.Web.Mvc;
using GZTW.AyaNova.BLL;
using ri.util;
using CSLA;
namespace ri.Controllers
{
public class ScheduledUserController : Controller
{
#region List
//
// GET: /ScheduledUser/
public ActionResult Index(Guid id)
{
Workorder c = Workorder.GetWorkorderByRelativeNoMRU(RootObjectTypes.WorkorderItem, id);
if (c == null) return HttpNotFound();
formInit(c);
ViewBag.newItemUrl = ay.genBaseSiteUrl(Url) + "ScheduledUser/Create?Id=" + id.ToString();
return View(c.WorkorderItems[id].ScheduledUsers);
}
#endregion list
#region Create
//
// GET: /ScheduledUser/Create
public ActionResult Create(Guid id)
{
Workorder c = null;
try
{
c = Workorder.GetWorkorderByRelativeNoMRU(RootObjectTypes.WorkorderItem, id);
if (c == null) return HttpNotFound();
WorkorderItemScheduledUser i = c.WorkorderItems[id].ScheduledUsers.Add(c.WorkorderItems[id]);
c.Save();
return RedirectToAction("Edit", new { id = i.ID });
}
catch (Exception ex)
{
ay.setGeneralError(TempData, ex);
formInit(c);
return RedirectToAction("Index", "Home");
}
}
#endregion create
#region Edit
//
// GET: /ScheduledUser/Edit/5
public ActionResult Edit(Guid id)
{
Workorder w = Workorder.GetWorkorderByRelativeNoMRU(RootObjectTypes.WorkorderItemScheduledUser, id);
ViewBag.WorkorderID = w.ID;
ViewBag.WorkorderItemID = w.GetWorkorderItemIDFromDescendant(RootObjectTypes.WorkorderItemScheduledUser, id);
WorkorderItemScheduledUser c = w.WorkorderItems[ViewBag.WorkorderItemID].ScheduledUsers[id];
if (c == null) return HttpNotFound();
formInit(w, c);
return View(c);
}
#endregion edit
#region Update
//
// POST: /ScheduledUser/Edit/5
[HttpPost]
public ActionResult Edit(Guid id, FormCollection f)
{
WorkorderItemScheduledUser c = null;
Workorder w = null;
try
{
w = Workorder.GetWorkorderByRelativeNoMRU(RootObjectTypes.WorkorderItemScheduledUser, id);
ViewBag.WorkorderID = w.ID;
ViewBag.WorkorderItemID = w.GetWorkorderItemIDFromDescendant(RootObjectTypes.WorkorderItemScheduledUser, id);
c = w.WorkorderItems[ViewBag.WorkorderItemID].ScheduledUsers[id];
//update all the fields from collection
c.StartDate = ay.ParseDateToDbValue("StartDate", f);
c.StopDate = ay.ParseDateToDbValue("StopDate", f);
//autocomplete stop if empty
if ((!(c.StartDate is System.DBNull)) && c.StopDate is System.DBNull && (AyaBizUtils.GlobalSettings.LaborSchedUserDfltTimeSpan != 0))
c.StopDate = ((DateTime)c.StartDate).AddMinutes(AyaBizUtils.GlobalSettings.LaborSchedUserDfltTimeSpan);//case 816
c.UserID = ay.parseGuid("UserID", f);
c.EstimatedQuantity = ay.parseDecimal("EstimatedQuantity", f);
c.ServiceRateID = ay.parseGuid("ServiceRateID", f);
//handle update
if (w.IsDirty)
{
if (w.IsSavable)
w.Save();
else
{
//Broken rules
ayBrokenRules rulesError = new ayBrokenRules(c);
ViewBag.err = rulesError;
}
}
}
catch (Exception ex)
{
ay.setGeneralError(TempData, ex);
}
formInit(w,c);
return View(c);
}
#endregion update
#region Delete
//
// GET: /ScheduledUser/Delete/5
public ActionResult Delete(Guid id, string ayf)
{
try
{
Workorder w = Workorder.GetWorkorderByRelativeNoMRU(RootObjectTypes.WorkorderItemScheduledUser, id);
if (w == null) return HttpNotFound();
ViewBag.WorkorderID = w.ID;
Guid woItemId = w.GetWorkorderItemIDFromDescendant(RootObjectTypes.WorkorderItemScheduledUser, id);
w.WorkorderItems[woItemId].ScheduledUsers.Remove(id);
w.Save();
return Redirect(ayf);
}
catch (Exception ex)
{
ay.setGeneralError(TempData, util.ay.GetSQLError(ex));
return RedirectToAction("Index");
}
}
#endregion delete
#region Convert to labor
//
// GET: /ScheduledUser/Convert
public ActionResult Convert(Guid id)//in this case it's the wisu id
{
Workorder c = null;
try
{
c = Workorder.GetWorkorderByRelativeNoMRU(RootObjectTypes.WorkorderItemScheduledUser, id);
if (c == null) return HttpNotFound();
Guid woItemId=c.GetWorkorderItemIDFromDescendant(RootObjectTypes.WorkorderItemScheduledUser, id);
Guid mNewWorkorderItemLaborId = c.CreateLaborFromScheduledUser(woItemId,id);
//Note: new wo item labor ID can potentially be Guid empty if that method
//can't find one of the id's listed above. About the only way I can conceive of that happening
//is if someone else deletes the record between the start of this method call and the end of it
//so no point in handling that rare case here, let it bomb instead the sequence will be descriptive
//for support if it ever comes up
c.Save();
return RedirectToAction("Edit", "Labor", new { id = mNewWorkorderItemLaborId });
}
catch (Exception ex)
{
ay.setGeneralError(TempData, ex);
formInit(c);
return RedirectToAction("Index", "Home");
}
}
#endregion convert to labor
#region Menu and rights
private void formInit(Workorder w, WorkorderItemScheduledUser c=null)
{
//set rights
util.ay.setViewBagRights(ViewBag, w.WorkorderItemChildEffectiveRights(RootObjectTypes.WorkorderItemScheduledUser));
if (c != null)
{
//generate menu
#region Generate menu
ayMenu m = new ayMenu();
m.title = "O.WorkorderItemScheduledUser";
m.icon = "icon-WorkorderItemScheduledUser";
string baseurl = ay.genBaseSiteUrl(Url);
string cId = c.ID.ToString();
string sType = ((int)RootObjectTypes.WorkorderItemScheduledUser).ToString();
//RECORD HISTORY
m.menuItems.Add(new ayMenuItem("scriptitem", "UI.Command.RecordHistory", "alert('" +
ay.RecordHistoryText(c.Creator, c.Modifier, c.Created, c.Modified) +
"')", "icon-RecordHistory"));
//---COMMAND ITEMS---
m.menuItems.Add(new ayMenuItem("divider", "", "", ""));
//Convert to labour
if (ViewBag.ayCanEdit)
m.menuItems.Add(new ayMenuItem("navitem", "Workorder.Label.Command.ConvertScheduledUserToLabor", baseurl + "ScheduledUser/Convert?id=" + cId, "icon-ConvertScheduledToLabor"));
//DELETE
if (ViewBag.ayCanDelete)
{
m.menuItems.Add(new ayMenuItem("navitem", "UI.Command.Delete", baseurl + "ScheduledUser/Delete?id=" + cId, "icon-Delete", ay.PromptForDelete(), "ay-set-ret-url"));
}
//CREATE
if (ViewBag.ayCanEdit)
m.menuItems.Add(new ayMenuItem("navitem", "UI.Toolbar.New", baseurl + "ScheduledUser/Create?id="+c.WorkorderItemID.ToString(), "icon-Add"));//UI.Toolbar.New
ViewBag.ayMenu = m;
#endregion generate menu
}
}
#endregion
//eoc
}
}