public async Task <IWrappedResponse> Create(PostingRequestsCreateRequest request)
        {
            var olmaPostingRequests = new List <Olma.PostingRequest>();

            foreach (var postingRequestPosition in request.Positions)
            {
                var olmaPostingRequest = Mapper.Map <Olma.PostingRequest>(request);
                olmaPostingRequest.LoadCarrierId       = postingRequestPosition.LoadCarrierId;
                olmaPostingRequest.LoadCarrierQuantity = postingRequestPosition.LoadCarrierQuantity;
                olmaPostingRequest.SyncDate            = DateTime.UtcNow;

                _olmaPostingRequestRepo.Create(olmaPostingRequest);
                _olmaPostingRequestRepo.Save();

                var postingRequest = olmaPostingRequest;
                olmaPostingRequest = _olmaPostingRequestRepo.FindByCondition(pr => pr.Id == postingRequest.Id)
                                     .Include(lc => lc.LoadCarrier)
                                     .Include(pa => pa.PostingAccount)
                                     .FirstOrDefault();

                if (olmaPostingRequest == null)
                {
                    return(Failed <IEnumerable <PostingRequest> >());
                }

                if (string.IsNullOrEmpty(request.ReferenceNumber))
                {
                    olmaPostingRequest.ReferenceNumber =
                        await _numberSequencesService.GetProcessNumber(ProcessType.PostingRequest,
                                                                       olmaPostingRequest.Id);
                }

                olmaPostingRequests.Add(olmaPostingRequest);
            }

            var postingRequestsSyncRequest = new PostingRequestsSyncRequest
            {
                PostingRequestCreateSyncRequests = new List <PostingRequestCreateSyncRequest>()
            };

            var postingRequestCreateSyncRequest =
                new PostingRequestCreateSyncRequest
            {
                IsSortingRequired = olmaPostingRequests.Select(i => i.IsSortingRequired).FirstOrDefault(),
                Positions         = olmaPostingRequests.Select(pr => new PostingRequestPosition
                {
                    RefLtmsPalletId       = pr.LoadCarrier.RefLtmsPalletId,
                    LoadCarrierQuantity   = pr.LoadCarrierQuantity,
                    RefLtmsBookingRowGuid = pr.RowGuid
                }),
                CreditRefLtmsAccountId =
                    olmaPostingRequests.Select(pr => pr.DestinationRefLtmsAccountId).FirstOrDefault(),
                DebitRefLtmsAccountId =
                    olmaPostingRequests.Select(pr => pr.SourceRefLtmsAccountId).FirstOrDefault(),
                ReferenceNumber          = olmaPostingRequests.Select(pr => pr.ReferenceNumber).FirstOrDefault(),
                IssuedByRefLtmsAccountId = olmaPostingRequests.Select(pr => pr.PostingAccount.RefLtmsAccountId)
                                           .FirstOrDefault(),
                Note               = olmaPostingRequests.Select(pr => pr.Note).FirstOrDefault(),
                RefLtmsProcessId   = olmaPostingRequests.Select(pr => pr.RefLtmsProcessId).FirstOrDefault(),
                RefLtmsProcessType =
                    (RefLtmsProcessType)olmaPostingRequests.Select(pr => pr.RefLtmsProcessTypeId)
                    .FirstOrDefault(),
                RefLtmsTransactionId = request.RefLtmsTransactionRowGuid ?? olmaPostingRequests.Select(pr => pr.RefLtmsTransactionId).FirstOrDefault(),
                DocumentFileName     = request.DocumentFileName,
                IsSelfService        = request.IsSelfService,
                DigitalCode          = request.DigitalCode,
                DeliveryNoteNumber   = request.DeliveryNoteNumber,
                PickUpNoteNumber     = request.PickUpNoteNumber,
                RefLmsBusinessTypeId = request.RefLmsBusinessTypeId
            };

            var result =
                Mapper.Map <IEnumerable <Olma.PostingRequest>, IEnumerable <PostingRequest> >(olmaPostingRequests);

            postingRequestsSyncRequest.PostingRequestCreateSyncRequests.Add(postingRequestCreateSyncRequest);
            var syncResult = await _synchronizationsService.SendPostingRequestsAsync(postingRequestsSyncRequest);

            // Switch (override) Type and loadCarrierQuantity sign when quantity is negative to support Split Bookings
            // PostingRequests have to have positive quantities all the time
            if (olmaPostingRequests.Any(r => r.LoadCarrierQuantity < 0))
            {
                foreach (var postingRequest in olmaPostingRequests.Where(r => r.LoadCarrierQuantity < 0))
                {
                    postingRequest.LoadCarrierQuantity = Math.Abs(postingRequest.LoadCarrierQuantity);
                    postingRequest.Type = PostingRequestType.Credit;
                }
                _olmaPostingRequestRepo.Save();
            }

            if (syncResult.ResultType != ResultType.Ok)
            {
                return(new WrappedResponse
                {
                    ResultType = ResultType.Failed
                });
            }
            return(Created(result));
        }
        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);
        }