Example #1
0
        private static void ValidateReturn(AssetHistoryItem assetHistoryItem)
        {
            //The DateShipped must exist
            //if (assetHistoryItem.
            //There is no DateShipped column. Is embedded in description. So we won't be able to validate.
            //Lets at least check that the description is not null
            if (String.IsNullOrEmpty(assetHistoryItem.ActionDescription))
            {
                throw new RestException(HttpStatusCode.BadRequest, "Please provide an action description indicating when the asset was shipped.");
            }

            //The Shipping method must exist and be one of the existing values
            if (
                String.IsNullOrEmpty(assetHistoryItem.ShippingMethod) ||
                (
                    assetHistoryItem.ShippingMethod.ToLower() != "fedex" &&
                    assetHistoryItem.ShippingMethod.ToLower() != "ups" &&
                    assetHistoryItem.ShippingMethod.ToLower() != "usps" &&
                    assetHistoryItem.ShippingMethod.ToLower() != "other"
                )
                )
            {
                throw new RestException(HttpStatusCode.BadRequest, "Please provide either FedEx, UPS, USPS or Other as ShippingMethod value.");
            }
        }
Example #2
0
        public static string GetAssetHistoryItem(RestCommand command, int historyID)
        {
            AssetHistoryItem assetHistoryItem = AssetHistory.GetAssetHistoryItem(command.LoginUser, historyID);

            if (assetHistoryItem.OrganizationID != command.Organization.OrganizationID)
            {
                throw new RestException(HttpStatusCode.Unauthorized);
            }
            return(assetHistoryItem.GetXml("AssetHistoryItem", true));
        }
Example #3
0
        public static string AddAssetAssignment(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 == "3")
            {
                throw new RestException(HttpStatusCode.Forbidden, "Junkyard assets cannot be assigned. Please move it to the Warehouse before assigning it.");
            }

            AssetHistory     assetHistory     = new AssetHistory(command.LoginUser);
            AssetHistoryItem assetHistoryItem = assetHistory.AddNewAssetHistoryItem();

            assetHistoryItem.FullReadFromXml(command.Data, true);

            ValidateAssignment(command.LoginUser, assetHistoryItem);

            DateTime now = DateTime.UtcNow;

            assetHistoryItem.AssetID            = assetID;
            assetHistoryItem.OrganizationID     = command.LoginUser.OrganizationID;
            assetHistoryItem.ActionTime         = now;
            assetHistoryItem.ShippedFrom        = command.LoginUser.OrganizationID;
            assetHistoryItem.ShippedFromRefType = (int)ReferenceType.Organizations;
            assetHistoryItem.DateCreated        = now;
            assetHistoryItem.Actor        = command.LoginUser.UserID;
            assetHistoryItem.DateModified = now;
            assetHistoryItem.ModifierID   = command.LoginUser.UserID;
            assetHistory.Save();

            AssetAssignments assetAssignments = new AssetAssignments(command.LoginUser);
            AssetAssignment  assetAssignment  = assetAssignments.AddNewAssetAssignment();

            assetAssignment.HistoryID = assetHistoryItem.HistoryID;
            assetAssignments.Save();

            asset.Location   = "1";
            asset.AssignedTo = assetHistoryItem.ShippedTo;
            asset.Collection.Save();

            return(AssetAssignmentsView.GetAssetAssignmentsViewItem(command.LoginUser, assetAssignment.AssetAssignmentsID).GetXml("AssetAssignment", true));
        }
        public static string CreateAsset(RestCommand command)
        {
            DateTime now    = DateTime.UtcNow;
            Assets   assets = new Assets(command.LoginUser);
            Asset    asset  = assets.AddNewAsset();

            asset.OrganizationID = command.Organization.OrganizationID;
            asset.FullReadFromXml(command.Data, true);
            if (asset.ProductID == null)
            {
                throw new RestException(HttpStatusCode.BadRequest, "Product required");
            }
            // For consistency all assets are created in the warehouse.
            asset.Location = "2";
            // This is normally not necessary, but as the CreatorID is defined as a null field in this table it is needed.
            asset.CreatorID     = command.LoginUser.UserID;
            asset.NeedsIndexing = true;
            asset.DateCreated   = now;
            asset.DateModified  = now;
            asset.Collection.Save();
            asset.UpdateCustomFieldsFromXml(command.Data);

            string           description = String.Format("Asset {0} created via API.", GetAssetReference(asset));
            AssetHistory     history     = new AssetHistory(command.LoginUser);
            AssetHistoryItem historyItem = history.AddNewAssetHistoryItem();

            historyItem.OrganizationID    = command.Organization.OrganizationID;
            historyItem.Actor             = command.LoginUser.UserID;
            historyItem.AssetID           = asset.AssetID;
            historyItem.ActionTime        = DateTime.UtcNow;
            historyItem.ActionDescription = "Asset created via API.";
            historyItem.ShippedFrom       = 0;
            historyItem.ShippedTo         = 0;
            historyItem.TrackingNumber    = string.Empty;
            historyItem.ShippingMethod    = string.Empty;
            historyItem.ReferenceNum      = string.Empty;
            historyItem.Comments          = string.Empty;
            historyItem.DateCreated       = now;
            historyItem.DateModified      = now;

            history.Save();

            return(AssetsView.GetAssetsViewItem(command.LoginUser, asset.AssetID).GetXml("Asset", true));
        }
        public int JunkAsset(int assetID, string comments)
        {
            LoginUser loginUser = TSAuthentication.GetLoginUser();
            Asset     o         = Assets.GetAsset(loginUser, assetID);

            //Location 1=assigned (shipped), 2=warehouse, 3=junkyard
            o.Location   = "3";
            o.AssignedTo = null;
            DateTime now = DateTime.UtcNow;

            o.DateModified = now;
            o.ModifierID   = loginUser.UserID;
            o.Collection.Save();

            AssetHistory     assetHistory     = new AssetHistory(loginUser);
            AssetHistoryItem assetHistoryItem = assetHistory.AddNewAssetHistoryItem();

            assetHistoryItem.AssetID            = assetID;
            assetHistoryItem.OrganizationID     = loginUser.OrganizationID;
            assetHistoryItem.ActionTime         = DateTime.UtcNow;
            assetHistoryItem.ActionDescription  = "Asset assigned to Junkyard";
            assetHistoryItem.ShippedFrom        = -1;
            assetHistoryItem.ShippedFromRefType = -1;
            assetHistoryItem.ShippedTo          = -1;
            assetHistoryItem.RefType            = -1;
            assetHistoryItem.TrackingNumber     = string.Empty;
            assetHistoryItem.ShippingMethod     = string.Empty;
            assetHistoryItem.ReferenceNum       = string.Empty;
            assetHistoryItem.Comments           = comments;

            assetHistoryItem.DateCreated  = now;
            assetHistoryItem.Actor        = loginUser.UserID;
            assetHistoryItem.DateModified = now;
            assetHistoryItem.ModifierID   = loginUser.UserID;

            assetHistory.Save();

            ActionLogs.AddActionLog(loginUser, ActionLogType.Update, ReferenceType.Assets, assetID, "Junked asset.");

            return(assetID);
        }
        public int SaveAsset(string data)
        {
            NewAssetSave info;

            try
            {
                info = Newtonsoft.Json.JsonConvert.DeserializeObject <NewAssetSave>(data);
            }
            catch (Exception e)
            {
                return(-1);
            }

            LoginUser loginUser = TSAuthentication.GetLoginUser();
            Assets    assets    = new Assets(loginUser);
            Asset     asset     = assets.AddNewAsset();

            asset.OrganizationID     = TSAuthentication.OrganizationID;
            asset.Name               = info.Name;
            asset.ProductID          = info.ProductID;
            asset.ProductVersionID   = info.ProductVersionID;
            asset.SerialNumber       = info.SerialNumber;
            asset.WarrantyExpiration = DataUtils.DateToUtc(TSAuthentication.GetLoginUser(), info.WarrantyExpiration);
            asset.Notes              = info.Notes;
            //Location 1=assigned (shipped), 2=warehouse, 3=junkyard
            asset.Location = "2";

            asset.DateCreated  = DateTime.UtcNow;
            asset.DateModified = DateTime.UtcNow;
            asset.CreatorID    = loginUser.UserID;
            asset.ModifierID   = loginUser.UserID;

            asset.Collection.Save();

            string description = String.Format("Created asset {0} ", GetAssetReference(asset));

            ActionLogs.AddActionLog(TSAuthentication.GetLoginUser(), ActionLogType.Insert, ReferenceType.Assets, asset.AssetID, description);

            foreach (CustomFieldSaveInfo field in info.Fields)
            {
                CustomValue customValue = CustomValues.GetValue(TSAuthentication.GetLoginUser(), field.CustomFieldID, asset.AssetID);
                if (field.Value == null)
                {
                    customValue.Value = "";
                }
                else
                {
                    if (customValue.FieldType == CustomFieldType.DateTime)
                    {
                        customValue.Value = ((DateTime)field.Value).ToString();
                        //DateTime dt;
                        //if (DateTime.TryParse(((string)field.Value), System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out dt))
                        //{
                        //    customValue.Value = dt.ToUniversalTime().ToString();
                        //}
                    }
                    else
                    {
                        customValue.Value = field.Value.ToString();
                    }
                }

                customValue.Collection.Save();
            }

            AssetHistory     history     = new AssetHistory(loginUser);
            AssetHistoryItem historyItem = history.AddNewAssetHistoryItem();

            historyItem.OrganizationID    = loginUser.OrganizationID;
            historyItem.Actor             = loginUser.UserID;
            historyItem.AssetID           = asset.AssetID;
            historyItem.ActionTime        = DateTime.UtcNow;
            historyItem.ActionDescription = "Asset created.";
            historyItem.ShippedFrom       = 0;
            historyItem.ShippedTo         = 0;
            historyItem.TrackingNumber    = string.Empty;
            historyItem.ShippingMethod    = string.Empty;
            historyItem.ReferenceNum      = string.Empty;
            historyItem.Comments          = string.Empty;

            history.Save();

            return(asset.AssetID);
        }
        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 string AssignAsset(int assetID, string data)
        {
            AssignAssetSave info;

            try
            {
                info = Newtonsoft.Json.JsonConvert.DeserializeObject <AssignAssetSave>(data);
            }
            catch (Exception e)
            {
                return("error");
            }

            LoginUser loginUser = TSAuthentication.GetLoginUser();
            Asset     o         = Assets.GetAsset(loginUser, assetID);

            //Location 1=assigned (shipped), 2=warehouse, 3=junkyard
            o.Location   = "1";
            o.AssignedTo = info.RefID;
            DateTime now = DateTime.UtcNow;

            o.DateModified = now;
            o.ModifierID   = loginUser.UserID;
            o.Collection.Save();

            AssetHistory     assetHistory     = new AssetHistory(loginUser);
            AssetHistoryItem assetHistoryItem = assetHistory.AddNewAssetHistoryItem();

            assetHistoryItem.AssetID            = assetID;
            assetHistoryItem.OrganizationID     = loginUser.OrganizationID;
            assetHistoryItem.ActionTime         = DateTime.UtcNow;
            assetHistoryItem.ActionDescription  = "Asset Shipped on " + info.DateShipped.Month.ToString() + "/" + info.DateShipped.Day.ToString() + "/" + info.DateShipped.Year.ToString();
            assetHistoryItem.ShippedFrom        = loginUser.OrganizationID;
            assetHistoryItem.ShippedFromRefType = (int)ReferenceType.Organizations;
            assetHistoryItem.ShippedTo          = info.RefID;
            assetHistoryItem.TrackingNumber     = info.TrackingNumber;
            assetHistoryItem.ShippingMethod     = info.ShippingMethod;
            assetHistoryItem.ReferenceNum       = info.ReferenceNumber;
            assetHistoryItem.Comments           = info.Comments;

            assetHistoryItem.DateCreated  = now;
            assetHistoryItem.Actor        = loginUser.UserID;
            assetHistoryItem.RefType      = info.RefType;
            assetHistoryItem.DateModified = now;
            assetHistoryItem.ModifierID   = loginUser.UserID;

            assetHistory.Save();

            AssetAssignments assetAssignments = new AssetAssignments(loginUser);
            AssetAssignment  assetAssignment  = assetAssignments.AddNewAssetAssignment();

            assetAssignment.HistoryID = assetHistoryItem.HistoryID;

            assetAssignments.Save();

            string description = String.Format("Assigned asset to {0}.", info.AssigneeName);

            ActionLogs.AddActionLog(loginUser, ActionLogType.Update, ReferenceType.Assets, assetID, description);

            AssetsView assetsView = new AssetsView(loginUser);

            assetsView.LoadByAssetID(assetID);

            StringBuilder productVersionNumberDisplayName = new StringBuilder();

            if (!string.IsNullOrEmpty(assetsView[0].ProductVersionNumber))
            {
                productVersionNumberDisplayName.Append(" - " + assetsView[0].ProductVersionNumber);
            }

            StringBuilder serialNumberDisplayValue = new StringBuilder();

            if (string.IsNullOrEmpty(assetsView[0].SerialNumber))
            {
                serialNumberDisplayValue.Append("Empty");
            }
            else
            {
                serialNumberDisplayValue.Append(assetsView[0].SerialNumber);
            }

            StringBuilder warrantyExpirationDisplayValue = new StringBuilder();

            if (assetsView[0].WarrantyExpiration == null)
            {
                warrantyExpirationDisplayValue.Append("Empty");
            }
            else
            {
                warrantyExpirationDisplayValue.Append(((DateTime)assetsView[0].WarrantyExpiration).ToString(GetDateFormatNormal()));
            }

            return(string.Format(@"<div class='list-group-item'>
                            <a href='#' id='{0}' class='assetLink'><h4 class='list-group-item-heading'>{1}</h4></a>
                            <div class='row'>
                                <div class='col-xs-8'>
                                    <p class='list-group-item-text'>{2}{3}</p>
                                </div>
                            </div>
                            <div class='row'>
                                <div class='col-xs-8'>
                                    <p class='list-group-item-text'>SN: {4} - Warr. Exp.: {5}</p>
                                </div>
                            </div>
                            </div>"

                                 , assetID
                                 , assetsView[0].DisplayName
                                 , assetsView[0].ProductName
                                 , productVersionNumberDisplayName
                                 , serialNumberDisplayValue
                                 , warrantyExpirationDisplayValue));
        }
Example #9
0
        private static void ValidateAssignment(LoginUser loginUser, AssetHistoryItem assetHistoryItem)
        {
            //To validate the assignment the following must be true
            //The ShippedTo value and record must exist and belong to the processing organization
            if (assetHistoryItem.ShippedTo == null)
            {
                throw new RestException(HttpStatusCode.BadRequest, "A valid OrganizationID or ContactID is required as a ShippedTo value.");
            }
            else if (assetHistoryItem.RefType == null)
            {
                throw new RestException(HttpStatusCode.BadRequest, "A 9 or a 32 value is required for the RefType field to assign to either a customer or a contact respectively.");
            }
            else if ((ReferenceType)assetHistoryItem.RefType == ReferenceType.Organizations)
            {
                Organization assignedCustomer = Organizations.GetOrganization(loginUser, (int)assetHistoryItem.ShippedTo);
                if (assignedCustomer == null || assignedCustomer.ParentID != loginUser.OrganizationID)
                {
                    throw new RestException(HttpStatusCode.BadRequest, "A valid OrganizationID or ContactID is required as a ShippedTo value.");
                }
            }
            else if ((ReferenceType)assetHistoryItem.RefType == ReferenceType.Contacts)
            {
                User assignedContact = Users.GetUser(loginUser, (int)assetHistoryItem.ShippedTo);
                if (assignedContact == null)
                {
                    throw new RestException(HttpStatusCode.BadRequest, "A valid OrganizationID or ContactID is required as a ShippedTo value.");
                }
                else
                {
                    Organization assignedContactOrganization = Organizations.GetOrganization(loginUser, (int)assignedContact.OrganizationID);
                    if (assignedContactOrganization == null || assignedContactOrganization.ParentID != loginUser.OrganizationID)
                    {
                        throw new RestException(HttpStatusCode.BadRequest, "A valid OrganizationID or ContactID is required as a ShippedTo value.");
                    }
                }
            }
            else
            {
                throw new RestException(HttpStatusCode.BadRequest, "A 9 or a 32 value is required for the RefType field to assign to either a customer or a contact respectively.");
            }

            //The DateShipped must exist
            //if (assetHistoryItem.
            //There is no DateShipped column. Is embedded in description. So we won't be able to validate.
            //Lets at least check that the description is not null
            if (String.IsNullOrEmpty(assetHistoryItem.ActionDescription))
            {
                throw new RestException(HttpStatusCode.BadRequest, "Please provide an action description indicating when the asset was shipped.");
            }

            //The Shipping method must exist and be one of the existing values
            if (
                String.IsNullOrEmpty(assetHistoryItem.ShippingMethod) ||
                (
                    assetHistoryItem.ShippingMethod.ToLower() != "fedex" &&
                    assetHistoryItem.ShippingMethod.ToLower() != "ups" &&
                    assetHistoryItem.ShippingMethod.ToLower() != "usps" &&
                    assetHistoryItem.ShippingMethod.ToLower() != "other"
                )
                )
            {
                throw new RestException(HttpStatusCode.BadRequest, "Please provide either FedEx, UPS, USPS or Other as ShippingMethod value.");
            }
        }
Example #10
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));
        }
        public static string UpdateAsset(RestCommand command, int assetID)
        {
            Asset asset = Assets.GetAsset(command.LoginUser, assetID);

            if (asset == null || asset.OrganizationID != command.Organization.OrganizationID)
            {
                throw new RestException(HttpStatusCode.Unauthorized);
            }
            string originalLocation = asset.Location;

            asset.FullReadFromXml(command.Data, false);
            if (asset.ProductID == null)
            {
                throw new RestException(HttpStatusCode.BadRequest, "Product required");
            }

            if (
                String.IsNullOrEmpty(asset.Location.Trim()) ||
                (
                    asset.Location != "1" &&
                    asset.Location != "2" &&
                    asset.Location != "3"
                )
                )
            {
                asset.Location = originalLocation;
            }
            //Location can not be changed via the WebApp. The only change we are allowing is to bring an asset back to the Warehouse from the Junkyard.
            else if (asset.Location == "1" && originalLocation != "1")
            {
                throw new RestException(HttpStatusCode.BadRequest, "Please use a POST against the /assets/{id}/assignments/ URL to assign an asset.");
            }
            else if (asset.Location == "2" && originalLocation == "1")
            {
                throw new RestException(HttpStatusCode.BadRequest, "Please use a DELETE against the /assets/{id}/assignments/ URL to return an asset.");
            }
            else if (asset.Location == "3" && originalLocation == "1")
            {
                throw new RestException(HttpStatusCode.BadRequest, "Please return an asset to the Warehouse before sending it to the Junkyard.");
            }
            else if (asset.Location == "3" && originalLocation == "2")
            {
                throw new RestException(HttpStatusCode.BadRequest, "Please use a DELETE against the /assets/{id}/ URL to assign an asset to the Junkyard.");
            }

            asset.Collection.Save();
            asset.UpdateCustomFieldsFromXml(command.Data);

            if (asset.Location == "2" && originalLocation == "3")
            {
                DateTime         now              = DateTime.UtcNow;
                AssetHistory     assetHistory     = new AssetHistory(command.LoginUser);
                AssetHistoryItem assetHistoryItem = assetHistory.AddNewAssetHistoryItem();

                assetHistoryItem.AssetID            = assetID;
                assetHistoryItem.OrganizationID     = command.LoginUser.OrganizationID;
                assetHistoryItem.ActionTime         = DateTime.UtcNow;
                assetHistoryItem.ActionDescription  = "Restore asset from 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;

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

                assetHistory.Save();
            }

            return(AssetsView.GetAssetsViewItem(command.LoginUser, assetID).GetXml("Asset", true));
        }