Exemple #1
0
        public ActionResult Edit(Asset asset)
        {
            if (ModelState.IsValid)
            {
                var selectedAsset = _assetRepository.Get(asset.Id);

                if (selectedAsset != null)
                {
                    // Log new Changes in Allocation Table
                    if (selectedAsset.State != asset.State || selectedAsset.AllocatedEmployeeId != asset.AllocatedEmployeeId)
                    {
                        var assetAllocation = new AssetAllocation
                        {
                            State = asset.State,
                            AllocatedEmployeeId   = asset.AllocatedEmployeeId,
                            AllocatedOn           = DateTime.UtcNow,
                            AllocatedByEmployeeId = WebUser.Id,
                            AssetId = selectedAsset.Id
                        };

                        _assetAllocationRepository.Create(assetAllocation);
                        _unitOfWork.Commit();

                        // Asset State Changed
                        #if !DEBUG
                        _emailComposerService.AssetStateChanged(selectedAsset.Id);
                        #endif
                    }

                    // Update main Asset

                    selectedAsset.SerialNumber        = asset.SerialNumber;
                    selectedAsset.Title               = asset.Title;
                    selectedAsset.Description         = asset.Description;
                    selectedAsset.Specifications      = asset.Specifications;
                    selectedAsset.Brand               = asset.Brand;
                    selectedAsset.Cost                = asset.Cost;
                    selectedAsset.ModelNumber         = asset.ModelNumber;
                    selectedAsset.IsBrandNew          = asset.IsBrandNew;
                    selectedAsset.State               = asset.State;
                    selectedAsset.PurchaseDate        = asset.PurchaseDate;
                    selectedAsset.WarrantyExpiryDate  = asset.WarrantyExpiryDate;
                    selectedAsset.AssetCategoryId     = asset.AssetCategoryId;
                    selectedAsset.DepartmentId        = asset.DepartmentId;
                    selectedAsset.VendorId            = asset.VendorId;
                    selectedAsset.AllocatedEmployeeId = asset.AllocatedEmployeeId;

                    _assetRepository.Update(selectedAsset);
                    _unitOfWork.Commit();

                    return(RedirectToAction("Index"));
                }
            }

            return(View(asset));
        }
Exemple #2
0
        public JsonResult Update(AssetModel assetModel)
        {
            ApiResult <Asset> apiResult;

            if (ModelState.IsValid)
            {
                if (assetModel.Id > 0)
                {
                    apiResult = TryExecute(() =>
                    {
                        var selectedAsset = _assetRepository.Get(assetModel.Id);
                        if (selectedAsset != null)
                        {
                            var employee = _employeeRepository.GetBy(u => u.UserId == WebUser.Id);

                            // Log new Changes in Allocation Table
                            if (selectedAsset.State != assetModel.State || selectedAsset.AllocatedEmployeeId != assetModel.AllocatedEmployeeId)
                            {
                                var assetAllocation = new AssetAllocation
                                {
                                    State = assetModel.State,
                                    AllocatedEmployeeId   = assetModel.AllocatedEmployeeId,
                                    AllocatedOn           = DateTime.UtcNow,
                                    AllocatedByEmployeeId = employee.Id,
                                    AssetId = selectedAsset.Id
                                };

                                _assetAllocationRepository.Create(assetAllocation);
                                _unitOfWork.Commit();
                            }
                            // Update main Asset

                            selectedAsset.SerialNumber        = assetModel.SerialNumber;
                            selectedAsset.Title               = assetModel.Title;
                            selectedAsset.Description         = assetModel.Description;
                            selectedAsset.Specifications      = assetModel.Specifications;
                            selectedAsset.Brand               = assetModel.Brand;
                            selectedAsset.Cost                = assetModel.Cost;
                            selectedAsset.TagNumber           = assetModel.TagNumber;
                            selectedAsset.ModelNumber         = assetModel.ModelNumber;
                            selectedAsset.IsBrandNew          = assetModel.IsBrandNew;
                            selectedAsset.State               = assetModel.State;
                            selectedAsset.PurchaseDate        = assetModel.PurchaseDate;
                            selectedAsset.WarrantyExpiryDate  = assetModel.WarrantyExpiryDate;
                            selectedAsset.AssetCategoryId     = assetModel.AssetCategoryId;
                            selectedAsset.DepartmentId        = assetModel.DepartmentId;
                            selectedAsset.VendorId            = assetModel.VendorId;
                            selectedAsset.AllocatedEmployeeId = assetModel.AllocatedEmployeeId;

                            _assetRepository.Update(selectedAsset);
                            _unitOfWork.Commit();
                        }

                        // Asset State Changed
#if !DEBUG
                        _emailComposerService.AssetStateChanged(selectedAsset.Id);
#endif

                        return(selectedAsset);
                    }, "Asset updated sucessfully");
                }
                else
                {
                    apiResult = TryExecute(() =>
                    {
                        var asset = new Asset
                        {
                            Title               = assetModel.Title,
                            SerialNumber        = assetModel.SerialNumber,
                            Description         = assetModel.Description,
                            Specifications      = assetModel.Specifications,
                            Brand               = assetModel.Brand,
                            Cost                = assetModel.Cost,
                            ModelNumber         = assetModel.ModelNumber,
                            IsBrandNew          = assetModel.IsBrandNew,
                            State               = assetModel.State,
                            PurchaseDate        = assetModel.PurchaseDate,
                            WarrantyExpiryDate  = assetModel.WarrantyExpiryDate,
                            AssetCategoryId     = assetModel.AssetCategoryId,
                            DepartmentId        = assetModel.DepartmentId,
                            VendorId            = assetModel.VendorId,
                            AllocatedEmployeeId = assetModel.AllocatedEmployeeId,
                            Id = assetModel.Id
                        };

                        _assetRepository.Create(asset);
                        _unitOfWork.Commit();

                        // Update Tag Number
                        var selectedAsset = _assetRepository.Get(asset.Id);
                        if (selectedAsset != null)
                        {
                            selectedAsset.TagNumber = $"LA{asset.Id.ToString("D" + 6)}";
                            _assetRepository.Update(selectedAsset);
                            _unitOfWork.Commit();
                        }
#if !DEBUG
                        if (selectedAsset != null)
                        {
                            _emailComposerService.AssetStateChanged(selectedAsset.Id);
                        }
#endif
                        return(asset);
                    }, "Asset created sucessfully");
                }
            }
            else
            {
                apiResult = ApiResultFromModelErrors <Asset>();
            }

            return(Json(apiResult, JsonRequestBehavior.AllowGet));
        }