public async Task <IActionResult> InventoryAdjustment(InventoryAdjustmentDto inventoryAdjustment) { if (!ModelState.IsValid) { return(BadRequest(inventoryAdjustment)); } var inventoryTransaction = new InventorySupplyTransaction { Date = DateTime.Now, SupplyId = inventoryAdjustment.SupplyId, Qty = inventoryAdjustment.Qty, TransactionType = inventoryAdjustment.Qty >= 0?0:1, TransactionNumber = "Ajuste de Inventario", Comments = inventoryAdjustment.Comments }; await _unitOfWork.InventorySupplyTransactionRepository.InsertTask(inventoryTransaction); await _unitOfWork.Commit(); return(Ok(inventoryTransaction)); }
public async Task <IActionResult> ChangeDetailState(ChangeOrderDetailDto orderDetails) { if (orderDetails.DetailsId != null && orderDetails.DetailsId.Any(x => x != 0)) { var details = await _unitOfWork.OrderDetailRepository.GetByRangeId(orderDetails.DetailsId); var detailsWithDifferentState = new List <OrderDetail>(); switch (orderDetails.Status) { case 2: //todo aqui debo de hacer el ajuste de inventario,disminuir el inventario details.ForEach(x => { if (x.Status.Equals("En Cola")) { x.Status = "En Proceso"; } else { detailsWithDifferentState.Add(x); } }); if (detailsWithDifferentState.Count >= 1) { return(BadRequest(new { errors = "Only send details with state 'En Cola'", detailsWithDifferentState })); } _unitOfWork.OrderDetailRepository.UpdateRange(details); await _unitOfWork.Commit(); var inventoryTransactions = new List <InventorySupplyTransaction>(); details.ForEach(x => { if (x.ComboId != null) { x.Combo.ComboDetails.ForEach(y => { y.Dish.DishSupplies.ForEach(async z => { var inventorySupplyTransaction = new InventorySupplyTransaction { TransactionType = 1, TransactionNumber = await GetOrderNumber(x.OrderId), Date = DateTime.Now, ExpirationDate = DateTime.Now, Qty = -z.Qty * y.Qty * x.Qty, SupplyId = z.SupplyId, SupplyQty = z.Qty, DishId = y.DishId, DishQty = y.Qty, ComboId = x.ComboId, ComboQty = x.Qty, OrderId = x.OrderId }; inventoryTransactions.Add(inventorySupplyTransaction); }); }); } else { x.Dish.DishSupplies.ForEach(async y => { var inventorySupplyTransaction = new InventorySupplyTransaction { TransactionType = 1, TransactionNumber = await GetOrderNumber(x.OrderId), Date = DateTime.Now, ExpirationDate = DateTime.Now, Qty = -y.Qty * x.Qty, SupplyId = y.SupplyId, SupplyQty = y.Qty, DishId = x.DishId, DishQty = x.Qty, OrderId = x.OrderId }; inventoryTransactions.Add(inventorySupplyTransaction); }); } }); inventoryTransactions = inventoryTransactions.OrderBy(x => x.OrderId).ToList(); await _unitOfWork.InventorySupplyTransactionRepository.InsertRangeTask(inventoryTransactions); await _unitOfWork.Commit(); await _orderHub.Clients.All.DetailsInProcess(details); return(Ok(details)); case 3: details.ForEach(x => { if (x.Status.Equals("En Proceso")) { x.Status = "Finalizado"; } else { detailsWithDifferentState.Add(x); } }); if (detailsWithDifferentState.Count >= 1) { return(BadRequest(new { errors = "Only send details with state 'En Proceso'", detailsWithDifferentState })); } _unitOfWork.OrderDetailRepository.UpdateRange(details); await _unitOfWork.Commit(); await _orderHub.Clients.All.DetailsFinished(details); var orderIds = details.Select(x => x.OrderId).ToList(); var orders = await _unitOfWork.OrderRepository.GetByRangeIdNoIncludes(orderIds); var notifications = details.Select(x => new NotificationOrderDto { OrderDetail = x, Title = $"¡ORDEN NUMERO {orders.Where(y => y.Id == x.OrderId).Select(y => y.OrderNumber).FirstOrDefault()} LISTA PARA ENTREGAR!", Token = orders.Where(y => y.Id == x.OrderId).Select(y => y.NotificationToken).FirstOrDefault() }).ToList(); await _expoServices.SendPushNotification(notifications); return(Ok(details)); case 4: details.ForEach(x => { if (x.Status.Equals("Finalizado")) { x.Status = "Entregado"; } else { detailsWithDifferentState.Add(x); } }); if (detailsWithDifferentState.Count >= 1) { return(BadRequest(new { errors = "Only send details with state 'Finalizado'", detailsWithDifferentState })); } _unitOfWork.OrderDetailRepository.UpdateRange(details); await _unitOfWork.Commit(); await _orderHub.Clients.All.DetailsDelivered(details); return(Ok(details)); default: return(BadRequest(new BadRequestObjectResult("Invalid Status Provided"))); } } return(BadRequest(new BadRequestObjectResult("Null Value Detected in details"))); }