public async Task <IActionResult> PutAsync([FromRoute] string id, [FromBody] CycleDto cycle) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != cycle.Id) { return(BadRequest()); } try { await _db.UpdateCycleAsync(_mapper.Map <Cycle>(cycle)); } catch (DinersNotFoundException) { return(NotFound()); } return(NoContent()); }
public JsonResult UpdateCycle(CycleDto model) { bool success = true; string errorMessage = string.Empty; var now = DateTime.Now; if (model == null || model.Id == 0 || !ModelState.IsValid) { success = false; errorMessage = "Failed to create cycle"; } #region cycle basic details DateTime StartDate = DateTime.ParseExact(model.Details.StartDate, "dd/MM/yyyy HH:mm", null); //DateTime StartDateTime = StartDate.Date.AddHours(model.Details.start_time); // + new TimeSpan(model.Details.start_time, 0, 0); DateTime EndDate = DateTime.ParseExact(model.Details.EndDate, "dd/MM/yyyy HH:mm", null); //DateTime EndDateTime = EndDate.Date.AddHours(model.Details.End_time); //+ new TimeSpan(model.Details.End_time, 0, 0); DateTime ReconciliationDate = DateTime.ParseExact(model.Details.ReconciliationDate, "dd/MM/yyyy HH:mm", null); //DateTime ReconDatetime = ReconciliationDate.Date.AddHours(model.Details.Reconciliation_time);// + new TimeSpan(model.Details.Reconciliation_time, 0, 0); var cycle = UnitOfWork.CyclesBL.GetByID(model.Id); cycle.Name = model.Details.Name; cycle.Type = model.Details.Type ?? 0; cycle.DepartmentId = model.Details.Department; cycle.StartDate = StartDate; cycle.EndDate = EndDate; cycle.ReconciliationDate = ReconciliationDate; UnitOfWork.CyclesBL.Update(cycle); #endregion #region cycle territories if (!string.IsNullOrEmpty(model.Territories)) { var territoryIds = new List <int>(); try { territoryIds = model.Territories .Split(", ", StringSplitOptions.RemoveEmptyEntries) .Select(int.Parse) .ToList(); } catch { } if (territoryIds.Count > 0) { var allTerritories = UnitOfWork.TerritoriesBL.GetAllActive(); var oldTerritories = UnitOfWork.TerritoriesBL.GetAllByCycleId(model.Id); var selectedTerritories = allTerritories.Where(t => territoryIds.Contains(t.TerritoryId)).ToList(); var grantedTerritories = new List <Territories>(); for (int i = 0; i < selectedTerritories.Count; i++) { var territory = selectedTerritories[i]; GetParentTerritories(territory, allTerritories, ref grantedTerritories); } var toRemove = oldTerritories.Except(grantedTerritories) .Select(t => t.TerritoryId) .ToList(); var toAdd = grantedTerritories.Except(oldTerritories) .Select(t => t.TerritoryId) .ToList(); UnitOfWork.CyclesBL.DetachTerritories(model.Id, toRemove); UnitOfWork.CyclesBL.AttachTerritories(model.Id, toAdd); } } else { success = false; errorMessage = "No territories selected"; } #endregion #region cycle products (Hashed) /* * var allProducts = UnitOfWork.ProductBL.GetAllActiveProductsWithDepartments(); * * var inventoryLogs = new List<InventoryLog>(); * var transactionDetails = new List<TransactionDetail>(); * if (model.Products.Count > 0) * { * for (int i = 0; i < model.Products.Count; i++) * { * var item = model.Products[i]; * var product = allProducts.FirstOrDefault(p => p.ProductId == item.Id); * if (product == null) * { * continue; * } * var availableQuanity = Convert.ToInt32(product.InventoryQnty ?? 0); * if (availableQuanity < item.Quantity) * { * success = false; * errorMessage = $"Insufficient quantity for product #{product.ProductId}"; * break; * } * product.InventoryQnty -= item.Quantity; * UnitOfWork.ProductBL.Update(product); * newCycle.CycleProduct.Add(new CycleProduct * { * ProductId = item.Id, * Qunt = item.Quantity, * RemainQunt = item.Quantity, * }); * inventoryLogs.Add(new InventoryLog * { * ProductId = item.Id, * OldQnty = availableQuanity, * NewQnty = availableQuanity - item.Quantity, * Quantity = item.Quantity, * TransactionType = (int)TransactionTypes.Delivery, * UserId = LoggedUserId, * CreatedDate = now, * ActionType = (int)TransactionStatus.NewRequst, * }); * transactionDetails.Add(new TransactionDetail * { * ProductId = item.Id, * TransAmount = item.Quantity, * }); * } * } * else * { * success = false; * errorMessage = "No products selected"; * } * * var transaction = new Transaction * { * TransType = (int)TransactionTypes.Delivery, * Status = (int)TransactionStatus.NewRequst, * CreatedById = LoggedUserId, * CreateDate = now, * InventoryLog = inventoryLogs, * TransactionDetail = transactionDetails, * }; * * newCycle.Transaction.Add(transaction); */ #endregion #region cycle users if (model.Users.Count > 0) { var oldUsers = UnitOfWork.AppUserBL .GetAllByCycleId(model.Id) .Select(u => u.Id) .ToList(); var toRemove = oldUsers.Except(model.Users) .ToList(); var toAdd = model.Users.Except(oldUsers) .ToList(); UnitOfWork.CyclesBL.AttachUsers(model.Id, toAdd); UnitOfWork.CyclesBL.DetachUsers(model.Id, toRemove); } else { success = false; errorMessage = "No users selected"; } #endregion if (success) { if (UnitOfWork.Complete(LoggedUserId) > 0) { return(Json(new { Ok = true, Message = "Cycle updated successfully", })); } else { return(Json(new { Ok = false, Message = "Failed to update cycle", })); } } else { return(Json(new { Ok = false, Message = errorMessage, })); } }
public JsonResult CreateCycle(CycleDto model) { try { bool success = true; string errorMessage = string.Empty; var now = DateTime.Now; if (model == null || !ModelState.IsValid) { success = false; errorMessage = "Failed to create cycle"; } #region cycle basic details DateTime StartDate = DateTime.ParseExact(model.Details.StartDate, "dd/MM/yyyy HH:mm", null); // DateTime StartDateTime = StartDate.Date.AddHours(model.Details.start_time); // + new TimeSpan(model.Details.start_time, 0, 0); DateTime EndDate = DateTime.ParseExact(model.Details.EndDate, "dd/MM/yyyy HH:mm", null); // DateTime EndDateTime = EndDate.Date.AddHours(model.Details.End_time); //+ new TimeSpan(model.Details.End_time, 0, 0); DateTime ReconciliationDate = DateTime.ParseExact(model.Details.ReconciliationDate, "dd/MM/yyyy HH:mm", null); // DateTime ReconDatetime = ReconciliationDate.Date.AddHours(model.Details.Reconciliation_time);// + new TimeSpan(model.Details.Reconciliation_time, 0, 0); var newCycle = new Cycles { Name = model.Details.Name, Type = model.Details.Type ?? 0, DepartmentId = model.Details.Department, // StartDate = DateTime.ParseExact(model.Details.StartDate, "dd/MM/yyyy", null), StartDate = StartDate, // EndDate = DateTime.ParseExact(model.Details.EndDate, "dd/MM/yyyy", null), EndDate = EndDate, //ReconciliationDate = DateTime.ParseExact(model.Details.ReconciliationDate, "dd/MM/yyyy", null), ReconciliationDate = ReconciliationDate, IsActive = true, Status = (int)POPEnums.CycleStatus.Upcoming, // edit by mo salah }; #endregion #region cycle territories if (!string.IsNullOrEmpty(model.Territories)) { var territoryIds = new List <int>(); try { territoryIds = model.Territories .Split(", ", StringSplitOptions.RemoveEmptyEntries) .Select(int.Parse) .ToList(); } catch { } if (territoryIds.Count > 0) { var allTerritories = UnitOfWork.TerritoriesBL.GetAllActive(); var selectedTerritories = allTerritories.Where(t => territoryIds.Contains(t.TerritoryId)).ToList(); var grantedTerritories = new List <Territories>(); for (int i = 0; i < selectedTerritories.Count; i++) { var territory = selectedTerritories[i]; GetParentTerritories(territory, allTerritories, ref grantedTerritories); } newCycle.CycleTerritory = grantedTerritories.Select(t => new CycleTerritory { TerritoryId = t.TerritoryId }).ToList(); } } else { success = false; errorMessage = "No territories selected"; } #endregion #region cycle products var allProducts = UnitOfWork.ProductBL.GetAllActiveProductsWithDepartments(); var inventoryLogs = new List <InventoryLog>(); var transactionDetails = new List <TransactionDetail>(); var notification = new List <Notifications>(); if (model.Products.Count > 0) { for (int i = 0; i < model.Products.Count; i++) { var item = model.Products[i]; var product = allProducts.FirstOrDefault(p => p.ProductId == item.Id); if (product == null) { continue; } var availableQuanity = product.InventoryQnty ?? 0; if (availableQuanity < item.Quantity) { success = false; errorMessage = $"Insufficient quantity for product #{product.ProductId}"; break; } product.InventoryQnty -= item.Quantity; UnitOfWork.ProductBL.Update(product); // request is pending for bbx approval newCycle.CycleProduct.Add(new CycleProduct { ProductId = item.Id, Qunt = 0, //Qunt = item.Quantity, RemainQunt = 0 //RemainQunt = item.Quantity, }); inventoryLogs.Add(new InventoryLog { ProductId = item.Id, OldQnty = availableQuanity, NewQnty = availableQuanity - item.Quantity, Quantity = item.Quantity, TransactionType = (int)TransactionTypes.Delivery, UserId = LoggedUserId, CreatedDate = now, ActionType = (int)InventoryActionType.Decrement, }); transactionDetails.Add(new TransactionDetail { ProductId = item.Id, TransAmount = item.Quantity, }); } } else { success = false; errorMessage = "No products selected"; } #region Add Notification notification.Add(new Notifications { CreatedAt = DateTime.Now, Title = NotificationType.Unconfirmed.ToString(), Notificationtype = (int)NotificationType.Unconfirmed, IsSeen = false, ToUserId = model.BBXUser }); #endregion var transaction = new Transaction { TransType = (int)TransactionTypes.Delivery, Status = (int)TransactionStatus.New_Request, CreatedById = LoggedUserId, CreateDate = now, InventoryLog = inventoryLogs, TransactionDetail = transactionDetails, Notifications = notification, FromUserId = model.BBXUser, // bbx ToUserId = model.HRUser }; newCycle.Transaction.Add(transaction); #endregion #region cycle users if (model.Users.Count > 0) { for (int i = 0; i < model.Users.Count; i++) { var item = model.Users[i]; newCycle.CycleUser.Add(new CycleUser { UserId = item, }); } } else { success = false; errorMessage = "No users selected"; } #endregion if (success) { UnitOfWork.CyclesBL.Add(newCycle); if (UnitOfWork.Complete(LoggedUserId) > 0) { return(Json(new { Ok = true, Message = "Cycle added successfully", })); } else { return(Json(new { Ok = false, Message = "Failed to add cycle", })); } } else { return(Json(new { Ok = false, Message = errorMessage, })); } } catch (Exception e) { return(Json(new { Ok = false, Message = "An error occured , Failed to add cycle", })); } }