public int ReturnAsset(int assetID, string data) { AssignAssetSave info; try { info = Newtonsoft.Json.JsonConvert.DeserializeObject <AssignAssetSave>(data); } catch (Exception e) { return(-1); } LoginUser loginUser = TSAuthentication.GetLoginUser(); Asset o = Assets.GetAsset(loginUser, assetID); //Location 1=assigned (shipped), 2=warehouse, 3=junkyard o.Location = "2"; o.AssignedTo = null; DateTime now = DateTime.UtcNow; o.DateModified = now; o.ModifierID = loginUser.UserID; o.Collection.Save(); AssetAssignmentsView assetAssignmentsView = new AssetAssignmentsView(loginUser); assetAssignmentsView.LoadByAssetID(assetID); AssetHistory assetHistory = new AssetHistory(loginUser); AssetHistoryItem assetHistoryItem = assetHistory.AddNewAssetHistoryItem(); assetHistoryItem.AssetID = assetID; assetHistoryItem.OrganizationID = loginUser.OrganizationID; assetHistoryItem.ActionTime = DateTime.UtcNow; assetHistoryItem.ActionDescription = "Item returned to warehouse on " + info.DateShipped.Month.ToString() + "/" + info.DateShipped.Day.ToString() + "/" + info.DateShipped.Year.ToString(); assetHistoryItem.ShippedFrom = assetAssignmentsView[0].ShippedTo; assetHistoryItem.ShippedFromRefType = assetAssignmentsView[0].RefType; assetHistoryItem.ShippedTo = loginUser.OrganizationID; assetHistoryItem.RefType = (int)ReferenceType.Organizations; assetHistoryItem.TrackingNumber = info.TrackingNumber; assetHistoryItem.ShippingMethod = info.ShippingMethod; assetHistoryItem.ReferenceNum = info.ReferenceNumber; assetHistoryItem.Comments = info.Comments; assetHistoryItem.DateCreated = now; assetHistoryItem.Actor = loginUser.UserID; assetHistoryItem.DateModified = now; assetHistoryItem.ModifierID = loginUser.UserID; assetHistory.Save(); AssetAssignments assetAssignments = new AssetAssignments(loginUser); foreach (AssetAssignmentsViewItem assetAssignmentViewItem in assetAssignmentsView) { assetAssignments.DeleteFromDB(assetAssignmentViewItem.AssetAssignmentsID); } ActionLogs.AddActionLog(loginUser, ActionLogType.Update, ReferenceType.Assets, assetID, "Returned asset."); return(assetID); }
public static string ReturnAsset(RestCommand command, int assetID) { Asset asset = Assets.GetAsset(command.LoginUser, assetID); if (asset == null || asset.OrganizationID != command.Organization.OrganizationID) { throw new RestException(HttpStatusCode.Unauthorized); } if (asset.Location != "1") { throw new RestException(HttpStatusCode.BadRequest, "Only assigned assets can be returned."); } AssetAssignmentsView assetAssignmentsView = new AssetAssignmentsView(command.LoginUser); assetAssignmentsView.LoadByAssetID(assetID); AssetHistory assetHistory = new AssetHistory(command.LoginUser); AssetHistoryItem assetHistoryItem = assetHistory.AddNewAssetHistoryItem(); //Html specification does not allow body being send in the DELETE method. //Nevertheless, is relevant as in addition to deleting the assignments we are also adding a history record indicating shipping data. //If no body is sent an exception will be thrown as the Shipping Date is required by the webapp. try { assetHistoryItem.ReadFromXml(command.Data, true); } catch (Exception) { throw new RestException(HttpStatusCode.BadRequest, "Please include a request body with an <AssetHistoryItem> node including at least an <ActionDescription> including the Date Shipped and a <ShippingMethod> node with a valid value."); } ValidateReturn(assetHistoryItem); //Update Asset. asset.Location = "2"; asset.AssignedTo = null; DateTime now = DateTime.UtcNow; asset.DateModified = now; asset.ModifierID = command.LoginUser.UserID; asset.Collection.Save(); //Add history record. assetHistoryItem.AssetID = assetID; assetHistoryItem.OrganizationID = command.LoginUser.OrganizationID; assetHistoryItem.ActionTime = now; assetHistoryItem.ShippedFrom = assetAssignmentsView[0].ShippedTo; assetHistoryItem.ShippedFromRefType = assetAssignmentsView[0].RefType; assetHistoryItem.ShippedTo = command.LoginUser.OrganizationID; assetHistoryItem.RefType = (int)ReferenceType.Organizations; assetHistoryItem.DateCreated = now; assetHistoryItem.Actor = command.LoginUser.UserID; assetHistoryItem.DateModified = now; assetHistoryItem.ModifierID = command.LoginUser.UserID; assetHistory.Save(); //Delete assignments AssetAssignments assetAssignments = new AssetAssignments(command.LoginUser); foreach (AssetAssignmentsViewItem assetAssignmentViewItem in assetAssignmentsView) { assetAssignments.DeleteFromDB(assetAssignmentViewItem.AssetAssignmentsID); } return(AssetHistoryView.GetAssetHistoryViewItem(command.LoginUser, assetHistoryItem.HistoryID).GetXml("AssetHistoryItem", true)); }