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 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);
        }