Example #1
0
        public static string GetAssetHistoryViewItem(RestCommand command, int historyID)
        {
            AssetHistoryViewItem assetHistoryViewItem = AssetHistoryView.GetAssetHistoryViewItem(command.LoginUser, historyID);

            if (assetHistoryViewItem.OrganizationID != command.Organization.OrganizationID)
            {
                throw new RestException(HttpStatusCode.Unauthorized);
            }
            return(assetHistoryViewItem.GetXml("AssetHistoryViewItem", true));
        }
Example #2
0
        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));
        }
        public static string JunkAsset(RestCommand command, int assetID)
        {
            Asset asset = Assets.GetAsset(command.LoginUser, assetID);

            if (asset.OrganizationID != command.Organization.OrganizationID)
            {
                throw new RestException(HttpStatusCode.Unauthorized);
            }
            if (asset.Location == "1")
            {
                throw new RestException(HttpStatusCode.BadRequest, "Please return an asset to the Warehouse before sending it to the Junkyard.");
            }
            if (asset.Location == "3")
            {
                throw new RestException(HttpStatusCode.BadRequest, "This asset is already in the Junkyard.");
            }
            asset.Location   = "3";
            asset.AssignedTo = null;
            DateTime now = DateTime.UtcNow;

            asset.DateModified = now;
            asset.ModifierID   = command.LoginUser.UserID;
            asset.Collection.Save();

            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 we are not really deleting and we need to add a delete comment.
            //If no body is sent an exception will be thrown. We ignore it to allow sending to junkyard without a comment.
            try
            {
                assetHistoryItem.ReadFromXml(command.Data, true);
            }
            catch (Exception)
            {
            }

            assetHistoryItem.AssetID            = assetID;
            assetHistoryItem.OrganizationID     = command.LoginUser.OrganizationID;
            assetHistoryItem.ActionTime         = now;
            assetHistoryItem.ActionDescription  = "Asset assigned to Junkyard via API.";
            assetHistoryItem.ShippedFrom        = -1;
            assetHistoryItem.ShippedFromRefType = -1;
            assetHistoryItem.ShippedTo          = -1;
            assetHistoryItem.RefType            = -1;
            assetHistoryItem.TrackingNumber     = string.Empty;
            assetHistoryItem.ShippingMethod     = string.Empty;
            assetHistoryItem.ReferenceNum       = string.Empty;
            //This is handled by the ReadFromXml
            //assetHistoryItem.Comments = comments;

            assetHistoryItem.DateCreated  = now;
            assetHistoryItem.Actor        = command.LoginUser.UserID;
            assetHistoryItem.DateModified = now;
            assetHistoryItem.ModifierID   = command.LoginUser.UserID;

            assetHistory.Save();


            return(AssetHistoryView.GetAssetHistoryViewItem(command.LoginUser, assetHistoryItem.HistoryID).GetXml("AssetHistoryItem", true));
        }