CommodityOwnerViewModel Map(CommodityOwner commodityOwner)
 {
     CommodityOwnerViewModel commodityOwnerVM = new CommodityOwnerViewModel();
     commodityOwnerVM.Id = commodityOwner.Id;
     commodityOwnerVM.Code = commodityOwner.Code;
     commodityOwnerVM.Surname = commodityOwner.Surname;
     commodityOwnerVM.FirstName = commodityOwner.FirstName;
     commodityOwnerVM.LastName = commodityOwner.LastName;
     commodityOwnerVM.IdNo = commodityOwner.IdNo;
     commodityOwnerVM.PinNo = commodityOwner.PinNo;
     commodityOwnerVM.Gender = (int)commodityOwner.Gender;
     commodityOwnerVM.PhysicalAddress = commodityOwner.PhysicalAddress;
     commodityOwnerVM.PostalAddress = commodityOwner.PostalAddress;
     commodityOwnerVM.Email = commodityOwner.Email;
     commodityOwnerVM.PhoneNumber = commodityOwner.PhoneNumber;
     commodityOwnerVM.BusinessNumber = commodityOwner.BusinessNumber;
     commodityOwnerVM.FaxNumber = commodityOwner.FaxNumber;
     commodityOwnerVM.OfficeNumber = commodityOwner.OfficeNumber;
     commodityOwnerVM.Description = commodityOwner.Description;
     commodityOwnerVM.DateOfBirth = DateTime.Parse(commodityOwner.DateOfBirth.ToString());
     commodityOwnerVM.MaritalStatus = (int) commodityOwner.MaritalStatus;//commodityOwner.MaritalStatus.Id;
     commodityOwnerVM.CommodityOwnerType = commodityOwner.CommodityOwnerType.Id;
     commodityOwnerVM.CommoditySupplier = commodityOwner.CommoditySupplier.Id;
     commodityOwnerVM.IsActive = (int)commodityOwner._Status;
     return commodityOwnerVM;
 }
 private void EditFarmer(CommodityOwner commodityOwner)
 {
     Messenger.Default.Send(new EditCommodityOwnerMessage
         {
             SupplierId=SupplierId,
             CommodityOwnerId=commodityOwner.Id
         });
     SendNavigationRequestMessage(new Uri("/Views/Admin/Owner/CommodityOwner.xaml", UriKind.Relative));
 }
 public bool AddCommodityOwner(CommodityOwner commodityOwnerToEdit, out CommodityOwner commodityOwnerReturned)
 {
     _vm = DataContext as EditCommodityOwnerModalViewModel;
     _vm.Load(commodityOwnerToEdit);
     _vm.CloseDialog += (s, e) => this.Close();
     ShowDialog();
     commodityOwnerReturned = _vm.CommodityOwner;
     return _vm.DialogResult; ;
 }
       public CommodityPurchaseNote Create(CostCentre documentIssuerCostCentre, 
           Guid documentIssueCostCentreApplicationId,
           CostCentre documentRecipientCostCentre,
           User documentIssuerUser, string documentReference, 
           Guid documentParentId, 
           string deliveredBy, 
           CommodityProducer commodityProducer,
           CommoditySupplier commoditySupplier, 
           CommodityOwner owner,
           DateTime documentDate,
           DateTime documentDateIssued, string description,string note)
       {
           Guid id = Guid.NewGuid();
           var doc = DocumentPrivateConstruct<CommodityPurchaseNote>(id);
           if (documentParentId == null || documentParentId == Guid.Empty)
               doc.DocumentParentId = id;
           else
               doc.DocumentParentId = documentParentId;

           doc.DeliveredBy = deliveredBy;
           doc.CommodityOwner = owner;
           doc.CommodityProducer = commodityProducer;
           doc.CommoditySupplier = commoditySupplier;
           doc.DocumentIssuerCostCentre = documentIssuerCostCentre;
           doc.DocumentIssuerCostCentreApplicationId = documentIssueCostCentreApplicationId;
           doc.DocumentRecipientCostCentre = documentRecipientCostCentre;
           doc.DocumentReference = documentReference;
           doc.DocumentIssuerUser = documentIssuerUser;
           doc.Description = description;
           doc.Note = note;
           doc.DocumentDate = documentDate;
           doc.DocumentDateIssued = documentDateIssued;

           Map(doc, documentIssuerCostCentre, documentIssueCostCentreApplicationId, documentRecipientCostCentre, documentIssuerUser, documentReference, null, null);
           SetDefaultDates(doc);
           doc.EnableAddCommands();
           return doc;
       }
        protected override void LoadPage(Page page)
        {
            Guid commodityOwnerId = PresentationUtility.ParseIdFromUrl(page.NavigationService.CurrentSource);
            ContactsList.Clear();
            using (var c = NestedContainer)
            {
                if (commodityOwnerId == Guid.Empty)
                {
                    PageTitle = "Add Farmer";
                    CommoditySupplier = Using<CostCentreFactory>(c).CreateCostCentre(Guid.NewGuid(), CostCentreType.CommoditySupplier,
                                                                     Using<ICostCentreRepository>(c).GetById(
                                                                         GetConfigParams().CostCentreId)) as
                        CommoditySupplier;
                    CommodityOwner = new CommodityOwner(Guid.NewGuid()) {CommoditySupplier = CommoditySupplier};
                    CommodityProducer = new CommodityProducer(Guid.NewGuid())
                                            {
                                                CommoditySupplier = CommoditySupplier,
                                                CommodityProducerCentres = new List<Centre>()
                                            };
                }
                else
                {
                    PageTitle = "Edit Farmer";
                    var commodityOwner = Using<ICommodityOwnerRepository>(c).GetById(commodityOwnerId);
                    CommodityOwner = commodityOwner.DeepClone<CommodityOwner>();
                    CommoditySupplier = commodityOwner.CommoditySupplier.DeepClone<CommoditySupplier>();
                    CommodityProducer =
                        Using<ICommodityProducerRepository>(c).GetAll().FirstOrDefault(
                            n => n.CommoditySupplier.Id == CommoditySupplier.Id);

                }
                Setup();
            }
        }
        private async Task<ResponseBool> SaveCommodityOwner(CommodityOwner commodityOwner)
        {
            //if (commodityOwner == null) throw new ArgumentNullException("commodityOwner");

            var response = new ResponseBool { Success = false };
            var logs = new List<string>();

           // CommodityOwnerItem itemToSave = Map(commodityOwner);
            string log = string.Format("Created farmer: {0}; Code: {1}; In Account {2}",
                                       commodityOwner.FullName,
                                       commodityOwner.Code, commodityOwner.CommoditySupplier.Name);
            logs.Add(log);
            using (var c = NestedContainer)
            {
                var mapper = Using<IMasterDataToDTOMapping>(c);
                var commodityOwnerDto = mapper.Map(commodityOwner);
                response = await _proxy.CommodityOwnerAddAsync(commodityOwnerDto);
                if (response.Success)
                    logs.ForEach(n => Using<IAuditLogWFManager>(c).AuditLogEntry("Farmer Management", n));
            }
            return response;
        }
        private async Task<ImportValidationResultInfo> ValidateEntityeAsync(CommodityOwner entity)
        {
            return await Task.Run(() =>
            {
                var res = _commodityOwnerRepository.Validate(entity);
                return new ImportValidationResultInfo()
                {
                    Results = res.Results,
                    // Description = "Row-" + count,
                    Entity = entity
                };
            });

        }
 private ValidationResultInfo Validate(CommodityOwner commodityOwner)
 {
     return _commodityOwnerRepository.Validate(commodityOwner);
 }
        private void SelectAccount()
        {
            using (var container = NestedContainer)
            {
                var selected = Using<IAgriItemsLookUp>(container).SelectCommoditySupplier();

                SelectedAccount = selected;
                if (selected == null)
                {
                    SelectedAccount = new CommoditySupplier(Guid.Empty) { AccountName = "--Select Account---" };

                    FarmerVisibility = "Collapsed";
                }
                else
                {

                    SelectedAccountName = SelectedAccount.Name;

                    if (SelectedAccount.CommoditySupplierType == CommoditySupplierType.Cooperative)
                    {
                        if (SelectedFarmer != null && SelectedFarmer.Id != SelectedAccount.Id)
                        {
                            SelectedFarmer = new CommodityOwner(Guid.Empty) { FirstName = "--Select Farmer---" };
                            SelectedFarmerName = "--Select Farmer---";
                        }
                        FarmerVisibility = "Visible";
                    }
                    else
                    {
                        SelectedFarmer = Using<ICommodityOwnerRepository>(container).GetBySupplier(SelectedAccount.Id).FirstOrDefault();
                        FarmerVisibility = "Collapsed";
                    }

                }
            }
        }
 public HttpResponseMessage CommodityOwnerListAdd(List<CommodityOwnerItem> commodityOwnerItemsList)
 {
     var response = new ResponseBool { Success = false };
     try
     {
         foreach (var commodityOwnerItem in commodityOwnerItemsList)
         {
             var commodityOwner = new CommodityOwner(commodityOwnerItem.MasterId)
                                      {
                                          Surname = commodityOwnerItem.Surname,
                                          _Status = (EntityStatus) commodityOwnerItem.StatusId,
                                          PostalAddress = commodityOwnerItem.PostalAddress,
                                          PinNo = commodityOwnerItem.PinNo,
                                          PhysicalAddress = commodityOwnerItem.PhysicalAddress,
                                          OfficeNumber = commodityOwnerItem.OfficeNumber,
                                          BusinessNumber = commodityOwnerItem.BusinessNumber,
                                          PhoneNumber = commodityOwnerItem.PhoneNumber,
                                          IdNo = commodityOwnerItem.IdNo,
                                          LastName = commodityOwnerItem.LastName,
                                          MaritalStatus =
                                              (MaritalStatas) commodityOwnerItem.MaritalStatasMasterId,
                                          Code = commodityOwnerItem.Code,
                                          CommodityOwnerType =
                                              _commodityOwnerTypeRepository.GetById(
                                                  commodityOwnerItem.CommodityOwnerTypeMasterId),
                                          CommoditySupplier =
                                              _commoditySupplierRepository.GetById(
                                                  commodityOwnerItem.CommoditySupplierMasterId) as
                                              CommoditySupplier,
                                          DateOfBirth = commodityOwnerItem.DateOfBirth,
                                          Description = commodityOwnerItem.Description,
                                          Email = commodityOwnerItem.Email,
                                          FaxNumber = commodityOwnerItem.FaxNumber,
                                          FirstName = commodityOwnerItem.FirstName,
                                          Gender = (Gender) commodityOwnerItem.GenderId,
                                      };
             _commodityOwnerRepository.Save(commodityOwner);
         }
         response.Success = true;
         response.ErrorInfo = "Commodity producer successfully added.";
     }
     catch (DomainValidationException dve)
     {
         string errorMsg = dve.ValidationResults.Results.Aggregate("Error: Invalid commodity owner fields.\n",
                                                                   (current, msg) =>
                                                                   current + ("\t- " + msg.ErrorMessage + "\n"));
         response.ErrorInfo = errorMsg;
         response.Success = false;
         _log.Error(errorMsg, dve);
     }
     catch (Exception ex) //any other
     {
         response.ErrorInfo = "Error: An error occurred when saving the entity.\n" + ex.ToString();
         response.Success = false;
         _log.Error("Error: An error occurred when saving the entity.", ex);
     }
     return Request.CreateResponse(HttpStatusCode.OK, response);
 }
        protected override void LoadPage(Page page)
        {
            Guid oommodityProducerId = PresentationUtility.ParseIdFromUrl(page.NavigationService.CurrentSource);
            using (var c = NestedContainer)
            {
                if (oommodityProducerId == Guid.Empty)
                {
                    CommodityOwner = new CommodityOwner(Guid.NewGuid());
                }
                else
                {
                    var commodityProducer = Using<ICommodityOwnerRepository>(c).GetById(oommodityProducerId);
                    CommodityOwner = commodityProducer.Clone<CommodityOwner>();
                    PageTitle = "Edit Commodity Owner";
                    DateOfBirth = CommodityOwner.DateOfBirth;
                }
                Setup();

                if (CommodityOwner._Status == EntityStatus.New) return;
                SelectedCommoditySupplier = CommoditySuppliersList.FirstOrDefault(n => n.Id == CommodityOwner.CommoditySupplier.Id);
                SelectedMaritalStatus = CommodityOwner.MaritalStatus;
                SelectedCommodityOwnerType = CommodityOwnerTypesList.FirstOrDefault(n => n.Id == CommodityOwner.CommodityOwnerType.Id);
            }
        }
 public CommodityOwnerDTO Map(CommodityOwner commodityOwner)
 {
     if (commodityOwner == null) return null;
     var commodityOwnerDto = Mapper.Map<CommodityOwner, CommodityOwnerDTO>(commodityOwner);
     return commodityOwnerDto;
 }
        private async void Save()
        {
            CommodityOwner commodityOwner;
            string responseMsg = string.Empty;
            var response = new ResponseBool {Success = false};


            if (DateTime.Now.Year - DateOfBirth.Year < 18)
            {
                MessageBox.Show("Farmer must be over 18 years old.", "Agrimanagr: Farmer Management",
                                MessageBoxButton.OK);
                return;
            }

            if (string.IsNullOrEmpty(Surname) || string.IsNullOrEmpty(FirstName) || string.IsNullOrEmpty(IdNumber) || string.IsNullOrEmpty(PinNumber) || string.IsNullOrEmpty(SelectedCommodityOwnerTypePropertyName) || string.IsNullOrEmpty(PostalAddress) || string.IsNullOrEmpty(Email) || string.IsNullOrEmpty(PhoneNumber) || string.IsNullOrEmpty(BusinessNumber) || string.IsNullOrEmpty(PhysicalAddress))
            {
                MessageBox.Show("Please fill in all the mandatory fields.", "Agrimanagr: Farmer Management",
                                MessageBoxButton.OK);
                return;
            }
                using (var c = NestedContainer)
                {
                    commodityOwner = Using<ICommodityOwnerRepository>(c).GetById(Id);
                    if(commodityOwner==null)
                    {
                        commodityOwner = new CommodityOwner(Id);
                    }
                    commodityOwner.Surname = Surname;
                    commodityOwner.FirstName = FirstName;
                    commodityOwner.Code = Code;
                    commodityOwner.IdNo = IdNumber;
                    commodityOwner.PinNo = PinNumber;
                    commodityOwner.MaritalStatus = SelectedMaritalStatus;
                    commodityOwner.CommodityOwnerType = SelectedCommodityOwnerType;
                    commodityOwner.PhysicalAddress = PhysicalAddress;
                    commodityOwner.PhoneNumber = PhoneNumber;
                    commodityOwner.BusinessNumber = BusinessNumber;
                    commodityOwner.OfficeNumber = OfficeNumber;
                    commodityOwner.FaxNumber = FaxNumber;
                    commodityOwner.Email = Email;
                   
                    commodityOwner.DateOfBirth = DateOfBirth;
                    commodityOwner.Description = Description;
                    commodityOwner.PostalAddress = PostalAddress;
                    commodityOwner._Status = EntityStatus.Active;
                
                var commoditySupplier = Using<ICommoditySupplierRepository>(c).GetById(SupplierId) as CommoditySupplier;
                if (commoditySupplier != null)
                {
                    commodityOwner.CommoditySupplier = commoditySupplier;
                }


                //if (IsValid(commodityOwner))
                //{
                    _proxy = Using<IDistributorServiceProxy>(c);
                    response = await SaveCommodityOwner(commodityOwner);
                    if (response.ErrorInfo != "")
                        responseMsg += response.ErrorInfo + "\n";
                //}

                if (response.Success&& string.IsNullOrEmpty(response.ErrorInfo))
                {
                     responseMsg = "Farmer Added Successfully!";
                    MessageBox.Show(responseMsg, "Agrimanager :" + PageTitle, MessageBoxButton.OK,
                               MessageBoxImage.Information);
                    SendNavigationRequestMessage(new Uri("Views/Admin/Owner/ListingMemberCommodityOwner.xaml", UriKind.Relative));
                }
                else
                {
                    MessageBox.Show(responseMsg, "Agrimanager :" + PageTitle, MessageBoxButton.OK,
                               MessageBoxImage.Information);
                }
                   
            }
        }
  private void ViewLastTransaction(CommodityOwner farmer)
 {
     using (var c = NestedContainer)
     {
         var commodityPurchaseNote =
             Using<ICommodityPurchaseRepository>(c).GetLastTransactionByFarmerId(farmer.Id);
         if (commodityPurchaseNote == null)
         {
             MessageBox.Show(farmer.FullName+ " has no transactions to show", "Agrimanagr Info", MessageBoxButton.OK);
             
         }
         else
         {
             Using<IReceiptDocumentPopUp>(c).ShowReceipt(commodityPurchaseNote);
         }
     }
 }
 private void PurchaseCommodity(CommodityOwner obj)
 {
     Messenger.Default.Send<ViewModelMessage>(new ViewModelMessage { Id = obj.Id});
     //var navHelper = UserControlNavigationHelper.FindParentByType<>();
     const string uri = "/views/CommodityPurchase/EditPurchase.xaml";
     NavigateCommand.Execute(uri);
 }
        void ClearViewModel()
        {

            DocumentReference = string.Empty;
            Notes = "";
            SelectedAccountName = "--Select Account---"; ;
            SelectedFarmerName = "--Select Farmer---";
            DocumentId = Guid.Empty;
            SelectedAccount = new CommoditySupplier(Guid.Empty) { AccountName = "--Select Account---" };
            SelectedFarmer = new CommodityOwner(Guid.Empty) { FirstName = "--Select Farmer---" };
            DocumentIssuerUser = null;
            DocumentParentId = Guid.Empty;
            SelectedCommodity = new Commodity(Guid.Empty) { Name = "--Select Commodity---" }; ;
            SelectedGrade = new CommodityGrade(Guid.Empty) { Name = "--Select Grade---" };
            DocumentIssuerCostCentre = null;
            DocumentRecipientCostCentre = null;
            DriverName = "";
            RegistrationNumber = "";
            Weight = decimal.Zero;
            LineItems.Clear();
            FarmersList.Clear();
            CommodityList.Clear();
            GradeList.Clear();
            FarmerVisibility = "Collapsed";
            WeighTypeProp = WeighType.Manual;
            // MakeWeightextBoxReadOnly = false;
        }
        private void SelectFarmer()
        {
            using (var container = NestedContainer)
            {
                var selected = Using<IAgriItemsLookUp>(container).SelectFarmersBySupplier(SelectedAccount.Id);

                SelectedFarmer = selected;
                if (selected == null)
                {
                    SelectedFarmer = new CommodityOwner(Guid.Empty) { FirstName = "--Select Farmer---" };
                    SelectedFarmerName = "--Select Farmer---";
                }
                else
                {

                    SelectedFarmerName = SelectedFarmer.FullName;
                }
            }
        }
 public void Load(CommodityOwner commodityOwner)
 {
     using (var c = NestedContainer)
     {
         if (commodityOwner.Id == Guid.Empty)
         {
             CommodityOwner = new CommodityOwner(Guid.NewGuid()){CommoditySupplier = commodityOwner.CommoditySupplier};
             PageTitle = "Add Farmer to Account " + CommodityOwner.CommoditySupplier.Name;
         }
         else
         {
             CommodityOwner = commodityOwner.Clone<CommodityOwner>();
             PageTitle = "Edit Farmer for Account " + CommodityOwner.CommoditySupplier.Name;
         }
         Setup();
     }
 }
 protected Guid AddCommodityOwner(string bussNu, string code, string lastName, string idNo, string offNu, string pin, Guid cotId,  Guid supplierId, string surname, string firstname)
 {
     CommodityOwner farmer = new CommodityOwner(Guid.NewGuid())
                                 {
                                     BusinessNumber = bussNu,
                                     Code = code,
                                     LastName = lastName,
                                     IdNo = idNo,
                                     MaritalStatus =MaritalStatas.Unknown /*_maritalStatusRepository.GetById( ms)*/,
                                     OfficeNumber = offNu,
                                     PhoneNumber = offNu,
                                     PhysicalAddress = "Kitale",
                                     PinNo = pin,
                                     PostalAddress = "209",
                                     Surname = surname,
                                     FirstName = firstname, 
                                     Description = "Farmer",
                                     CommodityOwnerType = _commodityOwnerTypeRepository.GetById(cotId),
                                     Email = "*****@*****.**",
                                     _Status = EntityStatus.Active,
                                     CommoditySupplier = _commoditySupplierRepository.GetById(supplierId) as CommoditySupplier
                                 };
     return _commodityOwnerRepository.Save(farmer);
 }
 CommodityOwnerItem Map(CommodityOwner co)
 {
     var item = new CommodityOwnerItem
                    {
                        BusinessNumber = co.BusinessNumber,
                        Code = co.Code,
                        CommodityOwnerTypeMasterId = co.CommodityOwnerType.Id,
                        CommoditySupplierMasterId = co.CommoditySupplier.Id,
                        DateCreated = co._DateCreated,
                        DateLastUpdated = co._DateLastUpdated,
                        DateOfBirth = co.DateOfBirth,
                        Description = co.Description,
                        Email = co.Email,
                        FaxNumber = co.FaxNumber,
                        FirstName = co.FirstName,
                        GenderId = (int) co.Gender,
                        IdNo = co.IdNo,
                        LastName = co.LastName,
                        MaritalStatasMasterId = (int) co.MaritalStatus,
                        MasterId = co.Id,
                        OfficeNumber = co.OfficeNumber,
                        PhoneNumber = co.PhoneNumber,
                        PhysicalAddress = co.PhysicalAddress,
                        PinNo = co.PinNo,
                        PostalAddress = co.PostalAddress,
                        StatusId = (int) co._Status,
                        Surname = co.Surname
                    };
     return item;
 }
 private void AssertCommodityOwner(CommodityOwner commodityOwnerType, CommodityOwner savedCommodityOwnerType)
 {
     Assert.AreEqual(commodityOwnerType.Code, savedCommodityOwnerType.Code);
     Assert.AreEqual(commodityOwnerType.FirstName, savedCommodityOwnerType.FirstName);
     Assert.AreEqual(commodityOwnerType.Surname, savedCommodityOwnerType.Surname);
     Assert.AreEqual(commodityOwnerType.LastName, savedCommodityOwnerType.LastName);
     Assert.AreEqual(commodityOwnerType.Description, savedCommodityOwnerType.Description);
     Assert.AreEqual(commodityOwnerType.IdNo, savedCommodityOwnerType.IdNo);
     Assert.AreEqual(commodityOwnerType.PinNo, savedCommodityOwnerType.PinNo);
     Assert.AreEqual(commodityOwnerType.DateOfBirth, savedCommodityOwnerType.DateOfBirth);
     Assert.AreEqual(commodityOwnerType.MaritalStatus, savedCommodityOwnerType.MaritalStatus);
     Assert.AreEqual(commodityOwnerType.Gender, savedCommodityOwnerType.Gender);
     Assert.AreEqual(commodityOwnerType.PhysicalAddress, savedCommodityOwnerType.PhysicalAddress);
     Assert.AreEqual(commodityOwnerType.PostalAddress, savedCommodityOwnerType.PostalAddress);
     Assert.AreEqual(commodityOwnerType.Email, savedCommodityOwnerType.Email);
     Assert.AreEqual(commodityOwnerType.PhoneNumber, savedCommodityOwnerType.PhoneNumber);
     Assert.AreEqual(commodityOwnerType.BusinessNumber, savedCommodityOwnerType.BusinessNumber);
     Assert.AreEqual(commodityOwnerType.FaxNumber, savedCommodityOwnerType.FaxNumber);
     Assert.AreEqual(commodityOwnerType.OfficeNumber, savedCommodityOwnerType.OfficeNumber);
     Assert.AreEqual(commodityOwnerType.CommodityOwnerType.Code, savedCommodityOwnerType.CommodityOwnerType.Code);
     Assert.AreEqual(commodityOwnerType.CommoditySupplier.Id, savedCommodityOwnerType.CommoditySupplier.Id);
     Assert.AreEqual(commodityOwnerType._Status, savedCommodityOwnerType._Status);
 }
        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);
                    }

            
                       
            }
        }
Exemple #23
0
 public bool CommodityOwnerHasProducers(CommodityOwner commodityOwner)
 {
     return _ctx.tblSourcingDocument.Any(n => n.CommodityOwnerId == commodityOwner.Id);
 }
 public void Save(CommodityOwnerViewModel commodityOwnerVM)
 {
     //var age = DateTime.Now.Year - commodityOwnerVM.DateOfBirth.Year;
     //if(age < 18 || age > 120)
     //{
     //    throw new DomainValidationException(new ValidationResultInfo(), "Farmer must be over 17 years old.");
     //}
     CommodityOwner commodityOwner = new CommodityOwner(commodityOwnerVM.Id);
     commodityOwner.Code = commodityOwnerVM.Code;
     commodityOwner.Surname = commodityOwnerVM.Surname;
     commodityOwner.FirstName = commodityOwnerVM.FirstName;
     commodityOwner.LastName = commodityOwnerVM.LastName;
     commodityOwner.IdNo = commodityOwnerVM.IdNo;
     commodityOwner.PinNo = commodityOwnerVM.PinNo;
     commodityOwner.DateOfBirth = commodityOwnerVM.DateOfBirth;
     commodityOwner.MaritalStatus = (MaritalStatas) commodityOwnerVM.MaritalStatus;//_maritalStatusRepository.GetById(commodityOwnerVM.MaritalStatus);
     commodityOwner.Gender = (Gender)commodityOwnerVM.Gender;
     commodityOwner.PhysicalAddress = commodityOwnerVM.PhysicalAddress;
     commodityOwner.PostalAddress = commodityOwnerVM.PostalAddress;
     commodityOwner.Email = commodityOwnerVM.Email;
     commodityOwner.PhoneNumber = commodityOwnerVM.PhoneNumber;
     commodityOwner.BusinessNumber = commodityOwnerVM.BusinessNumber;
     commodityOwner.FaxNumber = commodityOwnerVM.FaxNumber;
     commodityOwner.OfficeNumber = commodityOwnerVM.OfficeNumber;
     commodityOwner.Description = commodityOwnerVM.Description;
     commodityOwner.CommodityOwnerType = _commodityOwnerTypeRepository.GetById(commodityOwnerVM.CommodityOwnerType);
     commodityOwner._Status = EntityStatus.Active;
     commodityOwner.CommoditySupplier = (CommoditySupplier) _commoditySupplierRepository.GetById(commodityOwnerVM.CommoditySupplier);
     _commodityOwnerRepository.Save(commodityOwner);
 }
        public HttpResponseMessage CommodityOwnerAdd(CommodityOwnerItem commodityOwnerItem)
        {
            var response = new ResponseBool { Success = false };
            try
            {
                var commodityOwner = new CommodityOwner(commodityOwnerItem.MasterId)
                                         {
                                             Surname = commodityOwnerItem.Surname,
                                             _Status = (EntityStatus) commodityOwnerItem.StatusId,
                                             PostalAddress = commodityOwnerItem.PostalAddress,
                                             PinNo = commodityOwnerItem.PinNo,
                                             PhysicalAddress = commodityOwnerItem.PhysicalAddress,
                                             OfficeNumber = commodityOwnerItem.OfficeNumber,
                                             BusinessNumber = commodityOwnerItem.BusinessNumber,
                                             PhoneNumber = commodityOwnerItem.PhoneNumber,
                                             IdNo = commodityOwnerItem.IdNo,
                                             LastName = commodityOwnerItem.LastName,
                                             MaritalStatus = (MaritalStatas) commodityOwnerItem.MaritalStatasMasterId,
                                             Code = commodityOwnerItem.Code,
                                             CommodityOwnerType =
                                                 _commodityOwnerTypeRepository.GetById(
                                                     commodityOwnerItem.CommodityOwnerTypeMasterId),
                                             CommoditySupplier =
                                                 _commoditySupplierRepository.GetById(
                                                     commodityOwnerItem.CommoditySupplierMasterId) as CommoditySupplier,
                                             DateOfBirth = commodityOwnerItem.DateOfBirth,
                                             Description = commodityOwnerItem.Description,
                                             Email = commodityOwnerItem.Email,
                                             FaxNumber = commodityOwnerItem.FaxNumber,
                                             FirstName = commodityOwnerItem.FirstName,
                                             Gender = (Gender) commodityOwnerItem.GenderId,
                                         };
                _commodityOwnerRepository.Save(commodityOwner);
                var contact = _contactRepository.GetByContactsOwnerId(commodityOwnerItem.CommoditySupplierMasterId).FirstOrDefault();
                if(contact==null)
                {
                    contact = new Contact(Guid.NewGuid());
                }
                contact.BusinessPhone = commodityOwnerItem.BusinessNumber;
                    contact.ChildrenNames = "";
                    contact.City = "";
                    contact.Company = "";
                    contact.ContactClassification=ContactClassification.PrimaryContact;
                    contact.ContactOwnerMasterId = commodityOwnerItem.CommoditySupplierMasterId;
                    contact.ContactOwnerType=ContactOwnerType.CommoditySupplier;
                    contact.ContactType = _contactTypeRepository.GetById(
                        commodityOwnerItem.CommodityOwnerTypeMasterId);
                    contact.DateOfBirth = commodityOwnerItem.DateOfBirth;
                    contact.Email = commodityOwnerItem.Email;
                    contact.Fax = commodityOwnerItem.FaxNumber;
                    contact.Firstname = commodityOwnerItem.FirstName;
                    contact.HomePhone = commodityOwnerItem.BusinessNumber;
                    contact.HomeTown = "";
                    contact.JobTitle = "Farmer";
                    contact.Lastname = commodityOwnerItem.LastName;
                    contact.MobilePhone = commodityOwnerItem.PhoneNumber;
                    contact.BusinessPhone = commodityOwnerItem.BusinessNumber;
                    contact.PhysicalAddress = "";
                    contact.PostalAddress = commodityOwnerItem.PhysicalAddress;
                    contact.SpouseName = "";
                _contactRepository.Save(contact);

                response.Success = true;
                response.ErrorInfo = "Commodity producer successfully added.";
            }
            catch (DomainValidationException dve)
            {
                string errorMsg = dve.ValidationResults.Results.Aggregate("Error: Invalid commodity owner fields.\n",
                                                                          (current, msg) =>
                                                                          current + ("\t- " + msg.ErrorMessage + "\n"));
                response.ErrorInfo = errorMsg;
                _log.Error(errorMsg, dve);
            }
            catch (Exception ex) //any other
            {
                response.ErrorInfo = "Error: An error occurred when saving the entity.\n" + ex.ToString();
                _log.Error("Error: An error occurred when saving the entity.", ex);
            }
            return Request.CreateResponse(HttpStatusCode.OK, response);
        }