public BatchDocumentCommandEnvelopeRoutingResponse GetNextCommandEnvelopes(Guid costCentreApplicationId, Guid costCentreId,
            List<Guid> lastDeliveredEnvelopeIds, int batchSize)
        {
            //if (lastDeliveredEnvelopeIds != null && lastDeliveredEnvelopeIds.Count > 0)
            //    _envelopeRouteOnRequestCostcentreRepository.MarkEnvelopesAsDelivered(lastDeliveredEnvelopeIds, costCentreApplicationId, costCentreId);

            List<CommandEnvelope> envelopes = _envelopeRouteOnRequestCostcentreRepository.GetUnDeliveredEnvelopesByDestinationCostCentreApplicationId
                (costCentreApplicationId, costCentreId, batchSize, true);
            var responce = new BatchDocumentCommandEnvelopeRoutingResponse();
            if (envelopes == null || envelopes.Count == 0)
            {
                responce.Envelopes = new List<CommandEnvelopeWrapper>();
                responce.ErrorInfo = "No Pending Download";
            }
            else
            {
                foreach (var envelope in envelopes)
                {   var wrapper = new CommandEnvelopeWrapper
                    {
                       
                        DocumentType = ((DocumentType)envelope.DocumentTypeId).ToString(),
                        EnvelopeArrivedAtServerTick=envelope.EnvelopeArrivedAtServerTick,
                        Envelope =envelope
                    };

                    responce.Envelopes.Add(wrapper);
                   
                }
             
                responce.ErrorInfo = "Success";
             

            }
            return responce;
        }
        public HttpResponseMessage GetNextDocumentCommandEnvelopes(EnvelopeRoutingRequest request)
        {
            string pullType = "Inventory";
            _log.InfoFormat("GetNextDocumentCommandEnvelopes  from CCAPPId : {0} with batchSize : {1}", request.CostCentreApplicationId, request.BatchSize);
            Guid ccid = _costCentreApplicationService.GetCostCentreFromCostCentreApplicationId(request.CostCentreApplicationId);
            BatchDocumentCommandEnvelopeRoutingResponse response = null;
            List<Guid> lastBatchEnvelopesIds = request.DeliveredEnvelopeIds;
            try
            {
                if (_costCentreApplicationService.IsCostCentreActive(request.CostCentreApplicationId))
                {
                    response = _commandEnvelopeRouterResponseBuilder.GetNextInventoryCommandEnvelopes(request.CostCentreApplicationId, ccid,
                                                                                lastBatchEnvelopesIds, request.BatchSize);
                    if (response.Envelopes.Count == 0)
                    {
                        pullType = "Other";
                        response=_commandEnvelopeRouterResponseBuilder.GetNextCommandEnvelopes(request.CostCentreApplicationId, ccid,
                                                                                lastBatchEnvelopesIds, request.BatchSize);
                    }
                   
                }
                else
                {
                    response = new BatchDocumentCommandEnvelopeRoutingResponse { ErrorInfo = "Inactive CostCentre Application Id" };
                }

            }
            catch (Exception ex)
            {
                response = new BatchDocumentCommandEnvelopeRoutingResponse { ErrorInfo = "Failed to get next document command" };
                _log.InfoFormat("ERROR GetNextDocumentCommandEnvelopes   from CCAPPId : {0} with batchsize : {1}", request.CostCentreApplicationId, request.BatchSize);
                _log.Error(ex);
            }

            AuditCCHit(ccid, "GetNextDocumentCommandEnvelopes", JsonConvert.SerializeObject(response.Envelopes.Select(s=>new {s.DocumentType,s.Envelope.Id})), pullType + " OK " + JsonConvert.SerializeObject(request));

            
            return Request.CreateResponse(HttpStatusCode.OK, response);
        }