public ItemResultObject SaveItemDetails(ItemDetails itemDetails) { using (StageBitzDB dataContext = new StageBitzDB()) { ItemTypesBL itemTypesBL = new ItemTypesBL(dataContext); ItemResultObject returnObj = itemTypesBL.SaveItemDetails(itemDetails, false, true); return(returnObj); } }
/// <summary> /// Saves the item details. /// </summary> /// <param name="itemDetails">The item details.</param> /// <param name="skipConcurrencyCheck">if set to <c>true</c> [skip concurrency check].</param> /// <param name="shouldCommit">if set to <c>true</c> [should commit].</param> /// <returns></returns> public ItemResultObject SaveItemDetails(ItemDetails itemDetails, bool skipConcurrencyCheck = false, bool shouldCommit = false) { ItemResultObject itemResultObject = new ItemResultObject(); ItemBriefBL itemBriefBL = new ItemBriefBL(DataContext); InventoryBL inventoryBL = new InventoryBL(DataContext); ProjectBL projectBL = new ProjectBL(DataContext); LocationBL locationBL = new LocationBL(DataContext); CompanyBL companyBL = new CompanyBL(DataContext); int itemId = itemDetails.ItemId; int companyId = 0; int projectId = 0; Data.ItemBrief itemBrief = itemBriefBL.GetItemBrief(itemDetails.ItemBriefId); Data.Item item = inventoryBL.GetItem(itemId); if (itemDetails.ItemId > 0) { companyId = item.CompanyId.Value; } else if (itemBrief != null && item == null) { companyId = itemBrief.Project.CompanyId; projectId = itemBrief.ProjectId; item = new Item(); item.CompanyId = companyId; } bool isInventoryPage = itemDetails.RelatedTable == "Company"; string relatedTable = "Company"; int relatedId = companyId; if (itemDetails.RelatedTable == "Project" && projectId > 0) { relatedTable = itemDetails.RelatedTable; relatedId = projectId; } if (!projectBL.ShouldStopProcessing(relatedTable, relatedId, itemDetails.UserId)) { ErrorCodes errorCode; if (itemDetails.ItemBriefId > 0 && !inventoryBL.CanEditIteminItemBrief(itemDetails.ItemBriefId, itemDetails.ItemId)) { itemResultObject.Status = "CONCURRENCY"; itemResultObject.ErrorCode = (int)ErrorCodes.NoEditPermissionForItemInItemBrief; itemResultObject.Message = "No Write Permission"; return(itemResultObject); } else if (relatedTable == "Company" && itemId > 0 && inventoryBL.CheckPermissionsForItemDetailsPage(itemDetails.UserId, itemId, companyId, out errorCode, false) == null) { itemResultObject.Status = "CONCURRENCY"; itemResultObject.ErrorCode = (int)errorCode; itemResultObject.Message = "No Write Permission"; return(itemResultObject); } else if (!skipConcurrencyCheck && itemId > 0 && itemDetails.LastUpdatedDate != item.LastUpdatedDate) { itemResultObject.Status = "NOTOK"; itemResultObject.Message = "Can not update"; return(itemResultObject); } item.Name = itemDetails.Name; item.Description = itemDetails.Description; if (itemDetails.IsEditableToAdminOnly && item.Quantity != itemDetails.Quantity)//Commit if changes exist { bool canSaveQty = true; if (item.Quantity > itemDetails.Quantity)//Only when reduces Units { int maxBookedQty = inventoryBL.GetMaxBookedQuantityForAllDuration(itemDetails.ItemId); if (maxBookedQty > itemDetails.Quantity) { canSaveQty = false; } } if (canSaveQty) { item.Quantity = itemDetails.Quantity; } else { itemResultObject.Status = "CONCURRENCY"; itemResultObject.ErrorCode = (int)ErrorCodes.QuantityUpdateFailed; itemResultObject.Message = "Can not reduce quantity"; return(itemResultObject); } } item.LastUpdatedByUserId = itemDetails.UserId; item.LastUpdatedDate = Utils.Now; if (isInventoryPage) { if (companyBL.HasEditPermissionForInventoryStaff(companyId, itemDetails.UserId, itemDetails.LocationId)) { item.CreatedFor = itemDetails.CreatedFor; if (itemDetails.LocationId.HasValue) { Data.Location location = locationBL.GetLocation(itemDetails.LocationId.Value); if (location == null) { itemResultObject.Status = "CONCURRENCY"; itemResultObject.ErrorCode = (int)ErrorCodes.InventoryLocationDeleted; itemResultObject.Message = "Item Location not available"; return(itemResultObject); } } item.LocationId = itemDetails.LocationId; } else { itemResultObject.Status = "CONCURRENCY"; itemResultObject.ErrorCode = (int)ErrorCodes.NoEditPermissionForInventory; itemResultObject.Message = "No Edit Permission For Item In Inventory"; return(itemResultObject); } } if (itemDetails.ItemTypeId.HasValue) { item.ItemTypeId = itemDetails.ItemTypeId; } //Update ItemBooking if (itemBrief != null) { ItemBooking itemBooking = inventoryBL.GetItemBookingByItemID(itemId, itemBrief.ItemBriefId, "ItemBrief"); itemBooking.Quantity = itemDetails.Quantity.Value; itemBooking.LastUpdateDate = Utils.Now; itemBooking.LastUpdatedBy = itemDetails.UserId; } if (itemDetails.ItemValues != null) { foreach (ValuesInfo iValue in itemDetails.ItemValues) { if (iValue.Id == 0) { //It is an Insert ItemValue iValueNew = new ItemValue(); iValueNew.ItemId = item.ItemId; iValueNew.FieldId = iValue.FieldId; iValueNew.Value = iValue.Value; iValueNew.FieldOptionId = iValue.FieldOptionId; DataContext.ItemValues.AddObject(iValueNew); } else { //Update ItemValue itemValue = GetItemFieldValueById(iValue.Id); if (itemValue != null) { if (iValue.Value == string.Empty && iValue.FieldOptionId == null) { DataContext.ItemValues.DeleteObject(itemValue); } else { itemValue.FieldOptionId = iValue.FieldOptionId; itemValue.Value = iValue.Value; } } } } } if (shouldCommit) { DataContext.SaveChanges(); } itemResultObject.Status = "OK"; return(itemResultObject); } else { itemResultObject.Status = "STOPPROCESS"; return(itemResultObject); } }