private async void Save()
        {
            DateTime now = DateTime.Now;
            CommoditySupplier.CommoditySupplierType = CommoditySupplierType.Individual;
            CommoditySupplier.JoinDate = now;
            CommoditySupplier.Contact.Clear();
            CommoditySupplier.Name = CommodityOwner.FullName;
            CommoditySupplier.BankId =SelectedBank!=null? SelectedBank.Id:Guid.Empty;
            CommoditySupplier.BankBranchId = SelectedBankBranch != null ? SelectedBankBranch.Id : Guid.Empty;
            CommoditySupplier.AccountNo = AccountNumber;
            CommoditySupplier.AccountName = AccountName;
            

            if (!IsValid(CommoditySupplier))
                return;

            CommodityOwner.CommodityOwnerType = SelectedCommodityOwnerType;
            CommodityOwner.MaritalStatus = SelectedMaritalStatus;
            CommodityOwner.DateOfBirth = DateOfBirth;
            if (DateOfBirth.Year < 18)
            {
                MessageBox.Show("Farmer must be over 18 years old.", "Agrimanagr: Farmer Management",
                                MessageBoxButton.OK);
                return;
            }
           
            CommodityOwner._SetStatus(EntityStatus.Active);

            if (!IsValid() || !IsValid(CommodityOwner)) return;

            CommodityProducer.CommodityProducerCentres.Clear();
            CommodityProducer.CommodityProducerCentres.AddRange(AssignedCentresList.Select(n => n.Centre));
            if (!IsValid() || !IsValid(CommodityProducer)) return;

            using (var c = NestedContainer)
            {
                string responseMsg = "";
                _proxy = Using<IDistributorServiceProxy>(c);
                ResponseBool response = await _proxy.CommoditySupplierAddAsync(CommoditySupplier);
                responseMsg += response.ErrorInfo + "\n";

                string log = string.Format("Created commodity supplier: {0}; Code: {1}; And Type {2}",
                                           CommoditySupplier.Name,
                                           CommoditySupplier.CostCentreCode, CommoditySupplier.CommoditySupplierType);
                Using<IAuditLogWFManager>(c).AuditLogEntry("Manage Commodity Suppliers", log);


                if (response.Success)
                {ContactsList.Clear();
                    var coResponse = await SaveCommodityOwner();
                    if (coResponse.ErrorInfo != "") responseMsg += coResponse.ErrorInfo + "\n";

                    var cpResponse = await SaveCommodityProducer();
                    if (cpResponse.ErrorInfo != "") responseMsg += cpResponse.ErrorInfo + "\n";
                    var enve = new VMContactItem();
                    var contact = new Contact(Guid.NewGuid());
                    contact.BusinessPhone = CommodityOwner.BusinessNumber;
                    contact.ContactClassification=ContactClassification.PrimaryContact;
                    contact.ContactOwnerMasterId = CommoditySupplier.Id;
                    contact.DateOfBirth = CommodityOwner.DateOfBirth;
                    contact.Email = CommodityOwner.Email;
                    contact.Fax = CommodityOwner.FaxNumber;
                    contact.Firstname = CommodityOwner.FirstName;
                    contact.HomePhone = CommodityOwner.PhoneNumber;
                    contact.JobTitle = "Farmer";
                    contact.Lastname = CommodityOwner.LastName;
                    contact.MStatus =CommodityOwner.MaritalStatus;
                    contact.PhysicalAddress = CommodityOwner.PhysicalAddress;
                    contact.PostalAddress = CommodityOwner.PostalAddress;
                    contact.MobilePhone = CommodityOwner.PhoneNumber;
                    contact.ContactType = SelectedContactOwnerType;
                    
                    enve.IsDirty = true;
                    enve.IsNew = true;
                    enve.Contact = contact;
                    ContactsList.Add(enve);

                    bool success = await SaveContacts(CommoditySupplier);

                    if (!coResponse.Success || !cpResponse.Success || !success)
                        response.Success = false;
                }

                MessageBox.Show(responseMsg, "Agrimanagr: Manage Farmer", MessageBoxButton.OK,
                                MessageBoxImage.Information);

                if (response.Success)
                    SendNavigationRequestMessage(
                        new Uri("views/admin/commodityowners/listcommodityowners.xaml", UriKind.Relative));
            }
        }
        private async void Save()
        {
              var commodityOwner =new CommodityOwner(CommodityOwnerId);
              var commoditySupplier=new CommoditySupplier(Id);
              var  commodityProducer = new CommodityProducer(CommodityProducerId);
              var contact = new Contact(ContactId);

         using (var c = NestedContainer)
            {
                commodityOwner.Code = OwnerCode;
                if (string.IsNullOrEmpty(commodityOwner.Code))
                {
                    MessageBox.Show("Farmer Code is a Required Field", "Agrimanagr: Farmer Management",
                                        MessageBoxButton.OK);
                    return;

                }
                commodityOwner.Surname = Surname;
                commodityOwner.FirstName = FirstName;
                commodityOwner.LastName = MiddleName;
                commodityOwner.IdNo = IdNumber;
                commodityOwner.PinNo = PinNumber;
                //CommodityOwner.Gender =;
                commodityOwner.PhysicalAddress = PhysicalAddress;
                commodityOwner.PostalAddress = PostalAddress;
                commodityOwner.Email = Email;
                commodityOwner.PhoneNumber = PhoneNumber;
                commodityOwner.BusinessNumber = BusinessNumber;
                commodityOwner.FaxNumber = FaxNumber;
                commodityOwner.OfficeNumber = OfficeNumber;
                commodityOwner.Description = Description;
                commodityOwner.DateOfBirth = DateOfBirth;
                commodityOwner.CommoditySupplier = commoditySupplier;
                commodityOwner._Status = EntityStatus.Active;
                if (DateTime.Now.Year - DateOfBirth.Year < 18)
                    {
                        MessageBox.Show("Farmer must be over 18 years old.", "Agrimanagr: Farmer Management",
                                        MessageBoxButton.OK);
                        return;
                    }
            
                commodityOwner.MaritalStatus = SelectedMaritalStatus;
                commodityOwner.CommodityOwnerType = SelectedCommodityOwnerType;

                commoditySupplier.JoinDate = DateTime.Now.Date;
                commoditySupplier.AccountName = AccountName;
                commoditySupplier.AccountNo = AccountNumber;

                commoditySupplier._Status = EntityStatus.Active;
                commoditySupplier.PinNo = PinNumber;
                commoditySupplier.BankId = SelectedBank.Id;
                commoditySupplier.BankBranchId = SelectedBankBranch.Id;
                commoditySupplier.CommoditySupplierType = SelectedCommoditySupplierType;
                commoditySupplier.CostCentreCode = CostCenterCode;
                commoditySupplier.Name = AccountFullName;
                    var config = Using<IConfigService>(c).Load();
                    commoditySupplier.ParentCostCentre = new CostCentreRef {Id = config.CostCentreId};

                if (!IsValid(commoditySupplier))
                    return;

                commodityProducer.CommoditySupplier = commoditySupplier;
                commodityProducer._SetStatus(EntityStatus.Active);
                commodityOwner.BusinessNumber = BusinessNumber;
               if (!IsValid(commodityOwner)) return;

                commodityProducer.CommodityProducerCentres.Clear();
             
             
                commodityProducer.CommodityProducerCentres.AddRange(AssignedCentresList.Select(n => n.Centre));
                commodityProducer.Code = FarmCode;
                if (string.IsNullOrEmpty(commodityProducer.Code))
                {
                    MessageBox.Show("Farm Code is a Required Field", "Agrimanagr: Farmer Management",
                                        MessageBoxButton.OK);
                    return;
                }
                commodityProducer.Acrage = Acerage;
                commodityProducer.Name = FarmName;
                commodityProducer._Status = EntityStatus.Active;
                commodityProducer.RegNo = FarmRegistrationNo;
                commodityProducer.PhysicalAddress = FarmPhysicalAddress;
                commodityProducer.Description = FarmDescription;
                commodityProducer.CommoditySupplier = commoditySupplier;


                contact.BusinessPhone = BusinessNumber;
                contact.Email = Email;
                contact.ContactOwnerMasterId = commoditySupplier.Id;
                contact.Firstname = AccountFullName;
                contact.PostalAddress = PostalAddress;
                contact.MobilePhone = PhoneNumber;
                contact.PhysicalAddress = PhysicalAddress;
                contact.ContactOwnerType=ContactOwnerType.CommoditySupplier;

                if (!IsValid() || !IsValid(commodityProducer)) return;


                   string responseMsg = "";
                   _proxy = Using<IDistributorServiceProxy>(c);
                   var mapper = Using<IMasterDataToDTOMapping>(c);
                    var commoditySupplierdto = mapper.Map(commoditySupplier);
                    ResponseBool response = await _proxy.CommoditySupplierAddAsync(commoditySupplierdto);
                    responseMsg += response.ErrorInfo + "\n";

                    string log = string.Format("Created commodity supplier: {0}; Code: {1}; And Type {2}",
                                               commoditySupplier.Name,
                                               commoditySupplier.CostCentreCode, commoditySupplier.CommoditySupplierType);
                    Using<IAuditLogWFManager>(c).AuditLogEntry("Manage Commodity Suppliers", log);


                    if (response.Success)
                    {
                        ContactsList.Clear();
                        var coResponse = await SaveCommodityOwner(commodityOwner);
                        if (coResponse.ErrorInfo != "") responseMsg += coResponse.ErrorInfo + "\n";

                        var contactResponse = await SaveSupplierContact(contact);
                        if (contactResponse.ErrorInfo != "") responseMsg += contactResponse.ErrorInfo + "\n";

                        var cpResponse = await SaveCommodityProducer(commodityProducer);
                        if (cpResponse.ErrorInfo != "") responseMsg += cpResponse.ErrorInfo + "\n";

                       

                        if (!coResponse.Success || !cpResponse.Success)
                            response.Success = false;
                    }
                    if (response.Success)
                    {
                          responseMsg = "Farmer Successfully Added ";
                        MessageBox.Show(responseMsg, "Agrimanagr: Manage Farmer", MessageBoxButton.OK,
                                    MessageBoxImage.Information);

                         SendNavigationRequestMessage(
                            new Uri("views/admin/Supplier/ListOfSuppliers.xaml", UriKind.Relative));
                    }
                    else
                    {
                        MessageBox.Show(responseMsg, "Agrimanagr: Manage Farmer", MessageBoxButton.OK,
                                    MessageBoxImage.Information);
                    }

            
                       
            }
        }