public async Task SendOrderReciveNotifcation(IEnumerable <Order> orders) { var moneyPlacedes = await _uintOfWork.Repository <MoenyPlaced>().GetAll(); var outSideCompny = moneyPlacedes.First(c => c.Id == (int)MoneyPalcedEnum.OutSideCompany).Name; List <Notfication> totalNotfications = new List <Notfication>(); List <Notfication> detailNotifications = new List <Notfication>(); foreach (Order order in orders) { if (order.OrderStateId != (int)OrderStateEnum.Finished && order.OrderplacedId != (int)OrderplacedEnum.Way) { var clientNotigaction = totalNotfications.Where(c => c.ClientId == order.ClientId && c.OrderPlacedId == order.OrderplacedId && c.MoneyPlacedId == order.MoenyPlacedId).FirstOrDefault(); if (clientNotigaction == null) { int moenyPlacedId = order.MoenyPlacedId; if (moenyPlacedId == (int)MoneyPalcedEnum.WithAgent) { moenyPlacedId = (int)MoneyPalcedEnum.OutSideCompany; } clientNotigaction = new Notfication() { ClientId = order.ClientId, OrderPlacedId = order.OrderplacedId, MoneyPlacedId = moenyPlacedId, IsSeen = false, OrderCount = 1 }; totalNotfications.Add(clientNotigaction); } else { clientNotigaction.OrderCount++; } } var moneyPlacedName = order.MoenyPlaced.Name; if (order.MoenyPlacedId == (int)MoneyPalcedEnum.WithAgent) { moneyPlacedName = outSideCompny; } detailNotifications.Add(new Notfication() { Note = $"الطلب {order.Code} اصبح {order.Orderplaced.Name} و موقع المبلغ {order.MoenyPlaced.Name}", ClientId = order.ClientId }); } await _uintOfWork.AddRange(detailNotifications); await _uintOfWork.AddRange(totalNotfications); var unionNotification = totalNotfications.Union(detailNotifications); var grouping = unionNotification.GroupBy(c => c.ClientId); foreach (var item in grouping) { var key = item.Key.ToString(); var notficationDtos = item.Select(c => _mapper.Map <NotificationDto>(c)); await _notificationHub.AllNotification(key.ToString(), notficationDtos.ToArray()); } }
public async Task <ErrorResponse <string, IEnumerable <string> > > ReceiptOfTheStatusOfTheDeliveredShipment(IEnumerable <ReceiptOfTheStatusOfTheDeliveredShipmentWithCostDto> receiptOfTheStatusOfTheDeliveredShipmentWithCostDtos) { var moneyPlacedes = await _uintOfWork.Repository <MoenyPlaced>().GetAll(); var orderPlacedes = await _uintOfWork.Repository <OrderPlaced>().GetAll(); var outSideCompny = moneyPlacedes.First(c => c.Id == (int)MoneyPalcedEnum.OutSideCompany).Name; var response = new ErrorResponse <string, IEnumerable <string> >(); if (!receiptOfTheStatusOfTheDeliveredShipmentWithCostDtos.All(c => c.OrderplacedId == OrderplacedEnum.Way || c.OrderplacedId == OrderplacedEnum.Delivered || c.OrderplacedId == OrderplacedEnum.PartialReturned)) { var wrongData = receiptOfTheStatusOfTheDeliveredShipmentWithCostDtos.Where(c => !(c.OrderplacedId == OrderplacedEnum.Way || c.OrderplacedId == OrderplacedEnum.Delivered || c.OrderplacedId == OrderplacedEnum.PartialReturned)); var worngDataIds = wrongData.Select(c => c.Id); var worngOrders = await _uintOfWork.Repository <Order>().GetAsync(c => worngDataIds.Contains(c.Id)); List <string> errors = new List <string>(); foreach (var item in receiptOfTheStatusOfTheDeliveredShipmentWithCostDtos) { string code = worngOrders.Where(c => c.Id == item.Id).FirstOrDefault()?.Code; errors.Add($"لا يمكن وضع حالة الشحنة {OrderPlacedEnumToString(item.OrderplacedId)} للشحنة ذات الرقم : {code}"); } response.Errors = errors; return(response); } List <Notfication> notfications = new List <Notfication>(); List <Notfication> addednotfications = new List <Notfication>(); var ids = new HashSet <int>(receiptOfTheStatusOfTheDeliveredShipmentWithCostDtos.Select(c => c.Id)); var orders = await _uintOfWork.Repository <Order>().GetAsync(c => ids.Contains(c.Id)); var repatedOrders = orders.Where(order => receiptOfTheStatusOfTheDeliveredShipmentWithCostDtos.Any(r => r.EqualToOrder(order))).ToList(); orders = orders.Except(repatedOrders).ToList(); var exptedOrdersIds = repatedOrders.Select(c => c.Id); receiptOfTheStatusOfTheDeliveredShipmentWithCostDtos = receiptOfTheStatusOfTheDeliveredShipmentWithCostDtos.Where(c => !exptedOrdersIds.Contains(c.Id)); if (!receiptOfTheStatusOfTheDeliveredShipmentWithCostDtos.Any()) { return(new ErrorResponse <string, IEnumerable <string> >()); } List <OrderLog> logs = new List <OrderLog>(); foreach (var item in receiptOfTheStatusOfTheDeliveredShipmentWithCostDtos) { var order = orders.First(c => c.Id == item.Id); logs.Add(order); order.MoenyPlacedId = (int)item.MoenyPlacedId; order.OrderplacedId = (int)item.OrderplacedId; order.Note = item.Note; if (order.DeliveryCost != item.DeliveryCost) { if (order.OldDeliveryCost == null) { order.OldDeliveryCost = order.DeliveryCost; } } order.DeliveryCost = item.DeliveryCost; order.AgentCost = item.AgentCost; order.SystemNote = "ReceiptOfTheStatusOfTheDeliveredShipment"; if (order.IsClientDiliverdMoney) { switch (order.OrderplacedId) { case (int)OrderplacedEnum.Delivered: { if (decimal.Compare(order.Cost, item.Cost) != 0) { if (order.OldCost == null) { order.OldCost = order.Cost; } order.Cost = item.Cost; } var payForClient = order.ShouldToPay(); if (decimal.Compare(payForClient, (order.ClientPaied ?? 0)) != 0) { order.OrderStateId = (int)OrderStateEnum.ShortageOfCash; if (order.MoenyPlacedId == (int)MoneyPalcedEnum.Delivered) { order.MoenyPlacedId = (int)MoneyPalcedEnum.InsideCompany; } } else { order.OrderStateId = (int)OrderStateEnum.Finished; } } break; case (int)OrderplacedEnum.PartialReturned: { if (order.OldCost == null) { order.OldCost = order.Cost; } order.Cost = item.Cost; order.OrderStateId = (int)OrderStateEnum.ShortageOfCash; } break; } } else { if (order.Cost != item.Cost) { if (order.OldCost == null) { order.OldCost = order.Cost; } order.Cost = item.Cost; } } if (order.MoenyPlacedId == (int)MoneyPalcedEnum.InsideCompany) { order.ApproveAgentEditOrderRequests.Clear(); } order.MoenyPlaced = moneyPlacedes.First(c => c.Id == order.MoenyPlacedId); order.Orderplaced = orderPlacedes.First(c => c.Id == order.OrderplacedId); } await _uintOfWork.BegeinTransaction(); try { await _uintOfWork.UpdateRange(orders); await _uintOfWork.AddRange(logs); await _notificationService.SendOrderReciveNotifcation(orders); var receiptOfTheOrderStatus = new ReceiptOfTheOrderStatus { CreatedOn = DateTime.UtcNow, RecvierId = _userService.AuthoticateUserId() }; var receiptOfTheOrderStatusDetalis = new List <ReceiptOfTheOrderStatusDetali>(); foreach (var order in orders) { receiptOfTheOrderStatusDetalis.Add(new ReceiptOfTheOrderStatusDetali() { OrderCode = order.Code, ClientId = order.ClientId, Cost = order.Cost, AgentCost = order.AgentCost, AgentId = (int)order.AgentId, MoneyPlacedId = order.MoenyPlacedId, OrderPlacedId = order.OrderplacedId, OrderStateId = order.OrderStateId, }); } receiptOfTheOrderStatus.ReceiptOfTheOrderStatusDetalis = receiptOfTheOrderStatusDetalis; await _uintOfWork.Add(receiptOfTheOrderStatus); await _treasuryService.IncreaseAmountByOrderFromAgent(receiptOfTheOrderStatus); await _uintOfWork.Commit(); return(response); } catch (Exception ex) { await _uintOfWork.RoleBack(); return(new ErrorResponse <string, IEnumerable <string> >("حدث خطأ ما ")); } }