public void SubmitChanges(CommodityPurchaseNote document)
        {
            int sequence = 0;
            var envelope = new CommandEnvelope();
            envelope.Initialize(document);
            
            List<DocumentCommand> commandsToExecute = document.GetDocumentCommandsToExecute();

            CreateCommand createCommand = commandsToExecute.OfType<CreateCommand>().FirstOrDefault();
            if (createCommand != null)
                envelope.CommandsList.Add(new CommandEnvelopeItem(++sequence, createCommand));
                //_commandRouter.RouteDocumentCommand(createCommand);
            List<AfterCreateCommand> lineItemCommands = commandsToExecute.OfType<AfterCreateCommand>().ToList();
            foreach (var item in lineItemCommands)
            {
                envelope.CommandsList.Add(new CommandEnvelopeItem(++sequence, item));
              //  _commandRouter.RouteDocumentCommand(item);

            }
            ConfirmCommodityPurchaseCommand confirmCommand = commandsToExecute.OfType<ConfirmCommodityPurchaseCommand>().FirstOrDefault();
            if (confirmCommand != null)
            {
                envelope.CommandsList.Add(new CommandEnvelopeItem(++sequence, confirmCommand));

               // _commandRouter.RouteDocumentCommand(confirmCommand);
                //_notifyService.SubmitCommodityPurchase(document);
            }
            _commandEnvelopeRouter.RouteCommandEnvelope(envelope);
            _auditLogWFManager.AuditLogEntry("Commodity Purchase", "Created and confirmed commodity purchase note " + document.DocumentReference + "; id " + document.Id + " with " + document.LineItems.Count + " line items");

        }
        private SourcingDocument Map(tblSourcingDocument tbldoc)
        {
            SourcingDocument doc = null;
            doc = new CommodityPurchaseNote(tbldoc.Id);
            _Map(tbldoc, doc);
            CommodityPurchaseNote purchaseNote = doc as CommodityPurchaseNote;
            purchaseNote.CommodityOwner = _commodityOwnerRepository.GetById(tbldoc.CommodityOwnerId);
            if (tbldoc.CommodityProducerId != null)
                purchaseNote.CommodityProducer = _commodityProducerRepository.GetById(tbldoc.CommodityProducerId.Value);
            if (tbldoc.DocumentOnBehalfOfCostCentreId != null)
                purchaseNote.CommoditySupplier =
                    _costCentreRepository.GetById(tbldoc.DocumentOnBehalfOfCostCentreId.Value) as CommoditySupplier;
            purchaseNote.DeliveredBy = tbldoc.DeliveredBy;
            purchaseNote._SetLineItems(tbldoc.tblSourcingLineItem.Select(MapCommodityPurchaseLineItem).ToList());
            doc = purchaseNote;
            return doc;

        }
        public void LoadReceipt(CommodityPurchaseNote purchaseNote)
        {
            if (purchaseNote !=null)
            {
                ReceiptLineItemsList.Clear();
                Reset(); 
                
                using (IContainer cont = NestedContainer)
                {
                    var _costCentreRepo = Using<ICostCentreRepository>(cont);
                    var costCentre = _costCentreRepo.GetById(purchaseNote.DocumentIssuerCostCentre.ParentCostCentre.Id);
                    CompanyName = costCentre.Name;
                }

                if (purchaseNote.DocumentIssuerCostCentre != null)
                {
                    HubName = purchaseNote.DocumentIssuerCostCentre.Name;
                }
                if (purchaseNote.CommodityOwner != null)
                {

                    FarmerName = purchaseNote.CommodityOwner.FullName;
                    FamerNo = purchaseNote.CommodityOwner.Code;

                    TelNo = purchaseNote.CommodityOwner.PhoneNumber;
                    FaxNo = purchaseNote.CommodityOwner.FaxNumber;
                    FarmerEmail = purchaseNote.CommodityOwner.Email;
                }

                TotalWeight = purchaseNote.LineItems.Sum(n => n.Weight);

                DeliveredBy = purchaseNote.DeliveredBy;

                ServedByUserName = purchaseNote.DocumentIssuerUser.Username;

                ReceiptNo = purchaseNote.DocumentReference;
                try
                {

                    using (var c = NestedContainer)
                    {
                        var contact =
                            Using<IContactRepository>(c).GetByContactsOwnerId(purchaseNote.DocumentIssuerCostCentre.Id);

                        if(contact !=null)
                        {
                            if(contact.Any())
                                CompanyEmail = contact.FirstOrDefault().Email;
                        }
                       
                    }
                }catch
                {
                }
               

                foreach (var item in purchaseNote.LineItems)
                {
                    var commodityItem = new CommodyLineItemViewModel()
                    {
                        Id = item.Id,
                        Commodity = item.Commodity,
                        CommodityGrade = item.CommodityGrade,
                        ContainerNo = item.ContainerNo,
                        ContainerType = item.ContainerType,
                        Description = item.Description,
                        GrossWeight = (item.Weight+item.TareWeight+item.ContainerType.BubbleSpace),
                        NetWeight = item.Weight,
                        BubbleSpace =item.ContainerType.BubbleSpace,
                        TareWeight =item.TareWeight
                        
                    };
                    ReceiptLineItemsList.Add(commodityItem);

                }
                

            }
        }