[HttpPatch("[action]")] //Espero los Id de las ordenes que cambiaran de estado a preventa o cobrado public async Task <IActionResult> ChangeOrderState(ChangeOrderDetailDto orderToChange) { if (orderToChange.DetailsId != null && orderToChange.DetailsId.Any(x => x != 0)) { var orders = await _unitOfWork.OrderRepository.GetByRangeId(orderToChange.DetailsId); var incompletedOrders = new List <Order>(); var incompletedDetails = new List <OrderDetail>(); switch (orderToChange.Status) { //Preventa case 2: orders.ForEach(x => { incompletedDetails = x.OrderDetails.Where(y => !y.Status.Equals("Entregado")).ToList(); if (x.Status.Equals("Abierta") && incompletedDetails.Count == 0) { x.Status = "Preventa"; x.Tip = orderToChange.Tip; } else { incompletedOrders.Add(x); } }); if (incompletedDetails.Count >= 1) { return(BadRequest(new BadRequestObjectResult(new { errors = "Not all the details are in state 'Entregado'", incompletedDetails }))); } if (orders.Any(x => x.Status.Equals("Abierta"))) { return(BadRequest(new BadRequestObjectResult(new{ errors = "Any of the Orders are not in state Abierta", incompletedOrders }))); } _unitOfWork.OrderRepository.UpdateRange(orders); await _unitOfWork.Commit(); await _orderHub.Clients.All.PreSaleOrders(orders); return(Ok(orders)); //Cobrado case 3: orders.ForEach(x => { incompletedDetails = x.OrderDetails.Where(y => !y.Status.Equals("Entregado")).ToList(); if (x.Status.Equals("Preventa") && incompletedDetails.Count == 0) { x.Status = "Cobrado"; } }); if (incompletedDetails.Count >= 1) { return(BadRequest(new BadRequestObjectResult(new { errors = "Not all the details are in state 'Entregado'", incompletedDetails }))); } if (orders.Any(x => x.Status.Equals("Preventa"))) { return(BadRequest(new BadRequestObjectResult(new { errors = "Any of the Orders are not in state Preventa", orders }))); } _unitOfWork.OrderRepository.UpdateRange(orders); await _unitOfWork.Commit(); await _orderHub.Clients.All.ChargedOrders(orders); var tablesId = orders.Select(x => x.TableId).ToList(); var tables = await _unitOfWork.TableRepository.GetByIdRange(tablesId); tables.ForEach(x => { x.CustomerId = null; x.State = 0; x.Description = x.Description; x.CustomerLastName = null; x.CustomerName = null; }); _unitOfWork.TableRepository.UpdateRange(tables); await _unitOfWork.Commit(); await _orderHub.Clients.All.FreeTable(tables); return(Ok()); case 4: orders.ForEach(x => { x.Status = "Anulado"; x.OrderDetails.ForEach(y => { y.Status = "Anulado"; }); }); return(Ok(orders)); default: return(BadRequest(new BadRequestObjectResult("Invalid Status Provided"))); } } return(BadRequest(new BadRequestObjectResult("Null Value Detected in details"))); }
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"))); }