Ejemplo n.º 1
0
        public void Update(UpdateAssetInput input)
        {
            var @entity = _assetRepository.Get(input.Id);
            var listAssetCustomFields = _assetManager.GetAssetCustomFields(input.Id, input.CompanyName).ToList();
            var listAssetToolKits     = _assetManager.GetAssetToolAsset(input.Id, input.CompanyName).ToList();

            if (@entity == null)
            {
                throw new UserFriendlyException("No se pudo encontrar el artículo, fue borrado o no existe.");
            }
            if (_assetManager.AssetExist(@entity.Code, input.Id, input.CompanyName))
            {
                throw new UserFriendlyException("Existe un Artículo con el mismo Código.");
            }

            @entity.Name         = input.Name;
            @entity.Description  = input.Description;
            @entity.Code         = input.Code;
            @entity.Brand        = input.Brand;
            @entity.Series       = input.Series;
            @entity.ModelStr     = input.ModelStr;
            @entity.Plate        = input.Plate;
            @entity.Barcode      = input.Barcode;
            @entity.PurchaseDate = input.PurchaseDate;
            @entity.IsAToolInKit = input.IsAToolInKit;
            @entity.SetDepretiation(input.AdmissionDate, input.DepreciationMonthsQty);
            @entity.AssetType            = input.AssetType;
            @entity.CategoryId           = input.CategoryId;
            @entity.LastModificationTime = _dateTime.Now;
            @entity.LastModifierUserId   = input.LastModifierUserId;


            IList <CustomField> newCustomFields    = new List <CustomField>();
            IList <CustomField> updateCustomFields = new List <CustomField>();
            IList <CustomField> deleteCustomFields = new List <CustomField>();

            IList <ToolAsset> newToolKits    = new List <ToolAsset>();
            IList <ToolAsset> updateToolKits = new List <ToolAsset>();
            IList <ToolAsset> deleteToolKits = new List <ToolAsset>();

            foreach (var item in input.CustomFieldsDto)
            {
                if (listAssetCustomFields.Exists(a => a.Id == item.Id))
                {
                    CustomField updateCustomField = listAssetCustomFields.FirstOrDefault(a => a.Id == item.Id);
                    if (item.Delete == 1)
                    {
                        if (item.Saved == 1)
                        {
                            updateCustomField.IsDeleted = true;
                            deleteCustomFields.Add(updateCustomField);
                        }
                    }
                    else if (item.Update == 1)
                    {
                        updateCustomField.Name = item.Name;
                        updateCustomField.SetValue(item.CustomFieldType, item.Value);
                        updateCustomField.LastModificationTime = _dateTime.Now;
                        updateCustomField.LastModifierUserId   = input.CreatorGuidId;
                        updateCustomField.CompanyName          = input.CompanyName;
                        updateCustomFields.Add(updateCustomField);
                    }
                }
                else
                {
                    var @createDetail = CustomField.Create(item.Name, item.CustomFieldType, @entity.Id, item.Value, item.GetStringValue(), item.GetDateValue(), item.GetIntValue(), item.GetDoubleValue(), input.CreatorGuidId, _dateTime.Now, input.CompanyName);
                    newCustomFields.Add(@createDetail);
                }
            }

            foreach (var item in input.DetailAssetToolKitsDto)
            {
                if (listAssetToolKits.Exists(a => a.Code.Equals(item.Code)))
                {
                    ToolAsset updateToolKit = listAssetToolKits.FirstOrDefault(a => a.Id == item.Id);
                    if (item.Delete == 1)
                    {
                        if (item.Saved == 1)
                        {
                            updateToolKit.IsDeleted = true;
                            deleteToolKits.Add(updateToolKit);
                        }
                    }
                    else if (item.Update == 1)
                    {
                        updateToolKit.Name    = item.Name;
                        updateToolKit.Quatity = item.Quatity;
                        updateToolKit.LastModificationTime = _dateTime.Now;
                        updateToolKit.LastModifierUserId   = input.CreatorGuidId;
                        updateToolKit.CompanyName          = input.CompanyName;
                        updateToolKits.Add(updateToolKit);
                    }
                }
                else
                {
                    var entityTool = ToolAsset.Create(item.Name, item.Name, item.Code, @entity.Id, input.CreatorGuidId, _dateTime.Now, item.Quatity, input.CompanyName);
                    newToolKits.Add(entityTool);
                }
            }

            if (HasFile(input.Image))
            {
                string folderPath = "~/AssetsImages/";
                string fileName   = input.Image.FileName;//input.Id + ".png"

                var mappedPath = HostingEnvironment.MapPath(folderPath) + fileName;
                var directory  = new DirectoryInfo(HostingEnvironment.MapPath(folderPath));
                if (directory.Exists == false)
                {
                    directory.Create();
                }
                string folderPathRelative = "../../AssetsImages/";
                input.ImagePath = folderPathRelative + fileName;
                input.Image.SaveAs(mappedPath);

                @entity.SetImage(input.ImagePath);
            }
            _assetManager.Update(@entity, newCustomFields, deleteCustomFields, updateCustomFields, newToolKits, deleteToolKits, updateToolKits);
        }