Esempio n. 1
0
        private async Task <IWrappedResponse> CancelAction(Rules.Order.Cancel.MainRule rule)
        {
            var order   = rule.Context.Rules.OrderResourceRule.Context.Resource;
            var request = rule.Context.Request;

            if (IsDplEmployee)
            {
                var dplNote = Mapper.Map <Olma.EmployeeNote>(request.DplNote);
                order.DplNotes.Add(dplNote);

                order.Status = OrderStatus.Cancelled;
            }
            else
            {
                order.Status = OrderStatus.CancellationRequested;
            }

            _olmaOrderRepo.Save();

            var orderCancelSyncRequest = Mapper.Map <Olma.Order, OrderCancelSyncRequest>(order);

            orderCancelSyncRequest.IsApproved = IsDplEmployee;
            // TODO generate string from all emplyoeenote fields
            orderCancelSyncRequest.Note = IsDplEmployee ? request.DplNote.Text : request.Note;
            var ordersSyncRequest = new OrdersSyncRequest
            {
                OrderCancelSyncRequests = new List <OrderCancelSyncRequest>
                {
                    orderCancelSyncRequest
                }
            };
            var syncResult = await _synchronizationsService.SendOrdersAsync(ordersSyncRequest);

            if (syncResult.ResultType == ResultType.Ok)
            {
                order.SyncDate = DateTime.UtcNow;
            }

            _olmaOrderRepo.Save();

            var responseOrder = Mapper.Map <Order>(order);

            return(Updated(responseOrder));
        }
        private async Task <IWrappedResponse> CancelAction(Rules.LoadCarrierReceipts.Cancel.MainRule rule)
        {
            var id                 = rule.Context.Parent.Id;
            var request            = rule.Context.Parent.Request;
            var loadCarrierReceipt = rule.Context.LoadCarrierReceipt;

            if (loadCarrierReceipt.Document != null)
            {
                loadCarrierReceipt.Document.CancellationReason = request.Reason;
                loadCarrierReceipt.Document.StateId            = 255;
            }

            var syncDate = DateTime.UtcNow;

            foreach (var postingRequest in loadCarrierReceipt.PostingRequests)
            {
                postingRequest.Status   = PostingRequestStatus.Canceled;
                postingRequest.SyncDate = syncDate;
            }

            var rollbackFulfillmentSyncRequest = false;

            if (loadCarrierReceipt.OrderLoadDetail != null)
            {
                loadCarrierReceipt.OrderLoadDetail.Status = OrderLoadStatus.TransportPlanned;
                loadCarrierReceipt.OrderLoadDetail.ActualFulfillmentDateTime = null;
                loadCarrierReceipt.OrderLoadDetail.LoadCarrierReceiptId      = null;
                loadCarrierReceipt.OrderLoadDetailId = null;
                rollbackFulfillmentSyncRequest       = true;
            }

            var response = _olmaLoadCarrierReceiptRepo
                           .Update <Olma.LoadCarrierReceipt, Olma.LoadCarrierReceipt, LoadCarrierReceipt>(id,
                                                                                                          loadCarrierReceipt);

            await _synchronizationsService.SendPostingRequestsAsync(new PostingRequestsSyncRequest
            {
                PostingRequestCancelSyncRequests = new List <PostingRequestCancelSyncRequest>
                {
                    new PostingRequestCancelSyncRequest
                    {
                        RefLtmsTransactionId =
                            loadCarrierReceipt.PostingRequests.First().RefLtmsTransactionId,
                        Reason = request.Reason
                    }
                }
            });

            if (rollbackFulfillmentSyncRequest)
            {
                await _synchronizationsService.SendOrdersAsync(new OrdersSyncRequest
                {
                    OrderRollbackFulfillmentSyncRequests = new List <OrderRollbackFulfillmentSyncRequest>
                    {
                        new OrderRollbackFulfillmentSyncRequest
                        {
                            DigitalCode = loadCarrierReceipt.DigitalCode
                        }
                    }
                });
            }

            return(response);
        }
        private async Task <IWrappedResponse> CreateAction(Rules.OrderGroup.Create.MainRule rule)
        {
            Olma.OrderGroup orderGroup = new Olma.OrderGroup()
            {
                Orders = new List <Olma.Order>()
            };

            // create orders for order group
            switch (rule.Context.OrderGroupsCreateRequest.QuantityType)
            {
            case OrderQuantityType.Load:
            {
                for (var i = 0; i < rule.Context.OrderGroupsCreateRequest.NumberOfLoads; i++)
                {
                    Olma.Order order = Mapper.Map <Olma.Order>(rule.Context.OrderGroupsCreateRequest);
                    order.Status = OrderStatus.Pending;
                    orderGroup.Orders.Add(order);
                }

                break;
            }

            case OrderQuantityType.LoadCarrierQuantity:
            {
                Olma.Order order = Mapper.Map <Olma.Order>(rule.Context.OrderGroupsCreateRequest);
                order.Status = OrderStatus.Pending;
                orderGroup.Orders.Add(order);
                break;
            }

            case OrderQuantityType.Stacks:
            {
                Olma.Order order = Mapper.Map <Olma.Order>(rule.Context.OrderGroupsCreateRequest);
                order.Status = OrderStatus.Pending;
                orderGroup.Orders.Add(order);
                break;
            }

            default:
                throw new ArgumentOutOfRangeException($"Unknown QuantityType: {rule.Context.OrderGroupsCreateRequest.QuantityType}");
            }

            bool transactionRolledBack = false;
            IWrappedResponse <OrderMatch> orderMatchServiceResponse = null;

            _olmaDbContext.Database.CreateExecutionStrategy().Execute(operation: () =>
            {
                using (_olmaDbContext.Database.BeginTransaction())
                {
                    _olmaOrderGroupRepo.Create(orderGroup);
                    _olmaOrderGroupRepo.Save();

                    // this needs to happen after order is created in db as otherwise no orderGroupid exists
                    if (rule.Context.OrderGroupsCreateRequest.MatchLmsOrderGroupRowGuid.HasValue)
                    {
                        var order = orderGroup.Orders.Single();
                        var orderMatchQuantity = this.CalculateOrderMatchQuantities(order, order.StackHeightMax);

                        var orderMatchesCreateRequest = Mapper.Map <OrderMatchesCreateRequest>(order);
                        orderMatchesCreateRequest     = Mapper.Map(orderMatchQuantity, orderMatchesCreateRequest);

                        if (rule.Context.OrderGroupsCreateRequest.Type == OrderType.Demand)
                        {
                            orderMatchesCreateRequest.SupplyOrderRowGuid = rule.Context.OrderGroupsCreateRequest.MatchLmsOrderGroupRowGuid.Value;
                            orderMatchesCreateRequest.DemandOrderRowGuid = order.RefLmsOrderRowGuid;
                        }
                        else
                        {
                            orderMatchesCreateRequest.DemandOrderRowGuid = rule.Context.OrderGroupsCreateRequest.MatchLmsOrderGroupRowGuid.Value;
                            orderMatchesCreateRequest.SupplyOrderRowGuid = order.RefLmsOrderRowGuid;
                        }

                        orderMatchServiceResponse = (IWrappedResponse <OrderMatch>)_orderMatchesService.Create(orderMatchesCreateRequest).Result;

                        if (orderMatchServiceResponse.ResultType != ResultType.Created)
                        {
                            _olmaDbContext.Database.RollbackTransaction();
                            transactionRolledBack = true;
                        }
                        else
                        {
                            _olmaDbContext.Database.CommitTransaction();
                        }
                    }
                    else
                    {
                        _olmaDbContext.Database.CommitTransaction();
                    }
                }
            });

            if (transactionRolledBack)
            {
                return(orderMatchServiceResponse);
            }

            var orders = _olmaDbContext.Orders.Where(i => i.GroupId == orderGroup.Id)
                         .Include(og => og.LoadingLocation).ThenInclude(loc => loc.Address)
                         .Include(og => og.LoadingLocation.BusinessHours)
                         .Include(og => og.LoadingLocation.CustomerDivision).ThenInclude(cd => cd.Customer)
                         .Include(og => og.CreatedBy).ThenInclude(u => u.Person)
                         .Include(og => og.LoadCarrier)
                         .Include(og => og.BaseLoadCarrier)
                         .Include(og => og.PostingAccount).ThenInclude(pa => pa.CustomerDivisions)
                         .ThenInclude(cd => cd.Customer);

            DateTime syncDate = DateTime.UtcNow;

            foreach (var order in orders)
            {
                order.OrderNumber = await _numberSequencesService.GetProcessNumber(ProcessType.Order, order.Id);

                var orderCreateSyncRequest = Mapper.Map <Olma.Order, OrderCreateSyncRequest>(order);
                if (orderMatchServiceResponse != null)
                {
                    switch (order.Type)
                    {
                    case OrderType.Demand:
                        orderCreateSyncRequest.RefLmsAvailabilityRowGuid          = orderMatchServiceResponse.Data.RefLmsAvailabilityRowGuid;
                        orderCreateSyncRequest.RefLmsPermanentAvailabilityRowGuid = orderMatchServiceResponse.Data.RefLmsPermanentAvailabilityRowGuid;
                        break;

                    case OrderType.Supply:
                        orderCreateSyncRequest.RefLmsDeliveryRowGuid          = orderMatchServiceResponse.Data.RefLmsDeliveryRowGuid;
                        orderCreateSyncRequest.RefLmsPermanentDeliveryRowGuid = orderMatchServiceResponse.Data.RefLmsPermanentDeliveryRowGuid;
                        break;
                    }

                    orderCreateSyncRequest.DigitalCode = orderMatchServiceResponse.Data.DigitalCode;

                    orderCreateSyncRequest.LoadCarrierQuantity     = orderMatchServiceResponse.Data.LoadCarrierQuantity;
                    orderCreateSyncRequest.BaseLoadCarrierQuantity = orderMatchServiceResponse.Data.BaseLoadCarrierQuantity;
                }

                var ordersSyncRequest = new OrdersSyncRequest
                {
                    OrderCreateSyncRequests = new List <OrderCreateSyncRequest>
                    {
                        orderCreateSyncRequest
                    }
                };
                var syncResult = await _synchronizationsService.SendOrdersAsync(ordersSyncRequest);

                if (syncResult.ResultType == ResultType.Ok)
                {
                    order.SyncDate ??= syncDate;
                }
            }

            _olmaDbContext.SaveChanges();

            var result = Mapper.Map <Olma.OrderGroup, OrderGroup>(orderGroup);

            return(new WrappedResponse <OrderGroup>
            {
                ResultType = ResultType.Created,
                Data = result
            });
        }
Esempio n. 4
0
        public async Task <IWrappedResponse> Cancel(int id, OrderLoadCancelRequest request)
        {
            #region security

            // TODO add security

            #endregion

            var orderLoad = _olmaOrderLoadRepo.FindByCondition(o => o.Id == id)
                            .Include(i => i.Detail)
                            .Include(i => i.DplNotes)
                            .Include(o => o.Order)
                            .Include(o => o.CreatedBy)
                            .FirstOrDefault();

            if (orderLoad == null)
            {
                return(NotFound <OrderLoad>(id));
            }

            var cancellableStates = new[] { OrderLoadStatus.Pending, OrderLoadStatus.TransportPlanned };
            if (IsDplEmployee)
            {
                cancellableStates = cancellableStates.Append(OrderLoadStatus.CancellationRequested).ToArray();
            }

            if (!cancellableStates.Contains(orderLoad.Detail.Status))
            {
                return(BadRequest <OrderLoad>("OrderLoad is in a state that cannot be cancelled"));
            }

            var status = IsDplEmployee ? OrderLoadStatus.Canceled : OrderLoadStatus.CancellationRequested;
            orderLoad.Detail.Status = status;
            if (IsDplEmployee)
            {
                var dplNote = Mapper.Map <Olma.EmployeeNote>(request.DplNote);
                orderLoad.DplNotes.Add(dplNote);
            }

            _olmaOrderLoadRepo.Save();

            var orderCancelSyncRequest = Mapper.Map <Olma.OrderLoad, OrderCancelSyncRequest>(orderLoad);
            orderCancelSyncRequest.Note = IsDplEmployee ? request.DplNote.Text : request.Note;
            var ordersSyncRequest = new OrdersSyncRequest
            {
                OrderCancelSyncRequests = new List <OrderCancelSyncRequest>
                {
                    orderCancelSyncRequest
                }
            };
            var syncResult = await _synchronizationsService.SendOrdersAsync(ordersSyncRequest);

            if (syncResult.ResultType == ResultType.Ok)
            {
                orderLoad.Order.SyncDate = DateTime.UtcNow;
            }
            _olmaOrderLoadRepo.Save();

            var responseOrderLoad = Mapper.Map <OrderLoad>(orderLoad);
            return(Updated(responseOrderLoad));
        }