public SelectList GetAllCentres(CommoditySupplierViewModel vm)
        {

            List<SelectListItem> selectList = new List<SelectListItem>
                                                  {
                                                      new SelectListItem
                                                          {
                                                              Selected = true,
                                                              Text = "-- Select a centre --",
                                                              Value = Guid.Empty.ToString()
                                                          }
                                                  };

            var assignedCentres = new List<Centre>();
            if (vm.AssignedFarmCentres != null && vm.AssignedFarmCentres.Any())
            {
                assignedCentres.AddRange(vm.AssignedFarmCentres);
            }
            if (vm.HubId == Guid.Empty)
            {
                CommoditySupplier supplier = _commoditySupplierRepository.GetById(vm.CommoditySupplierId) as CommoditySupplier;
                if (supplier != null)
                    vm.HubId = supplier.ParentCostCentre.Id;
            }
            _centreRepository.GetAll()
                .OrderBy(n => n.Name)
                .ToList().ForEach(n => selectList.Add(new SelectListItem() { Text = n.Name, Value = n.Id.ToString() }));

            return new SelectList(selectList, "Value", "Text");
        }
 public void SetUpNew(CommoditySupplierViewModel vm)
 {
     if (vm.AssignedFarmCentres == null)
         vm.AssignedFarmCentres = new List<Centre>();
     vm.UnAsignedCentresList = GetAllCentres(vm);
 }
        public ActionResult CreateCommoditySupplier(CommoditySupplierViewModel vm)
        {
            
            ViewBag.CommoditySupplierTypeList = _commoditySupplierViewModelBuilder.CommoditySupplierType();
            ViewBag.ParentCostCentreList = _commoditySupplierViewModelBuilder.ParentCostCentre();
            ViewBag.Banks = _commoditySupplierViewModelBuilder.GetBanks();
            ViewBag.GenderList = _commodityOwnerViewModelBuilder.Gender();
            ViewBag.MaritalStatusList = _commodityOwnerViewModelBuilder.MaritalStatus();
            ViewBag.CommodityOwnerTypeList = _commodityOwnerViewModelBuilder.CommodityOwnerType();
            ViewBag.UnassignedCentresList = _centreRepository.GetAll().Select(r => new { r.Id, r.Name }).ToDictionary(d => d.Id, d => d.Name);
            LoadBranches(vm.BankId);

            if (!ModelState.IsValid)
            {
                return View(vm);
            }

            var allocatedCenter = _centreRepository.GetById(vm.SelectedCentreId);
            try
            {
                using (var tran = new TransactionScope())
                {
                    var commoditySupplierDto = new CommoditySupplierDTO
                    {
                            MasterId = vm.CommoditySupplierId,
                            Name = vm.Name,
                            AccountName = vm.AccountName,
                            AccountNo = vm.AccountNo,
                            PinNo = vm.PinNo,
                            BankId = vm.BankId,
                            BankBranchId = vm.BankBranchId,
                            CommoditySupplierTypeId = vm.CommoditySupplierType,
                            CostCentreCode = vm.CostCentreCode,
                            ParentCostCentreId = vm.ParentCostCentreId,
                        
                        };
                    _commoditySupplierViewModelBuilder.Save(commoditySupplierDto);
                    var commodityOwnerViewModel = new CommodityOwnerViewModel
                        {
                            Id=vm.CommodityOwnerId,
                            Code=vm.OwnerCode,
                            CommodityOwnerType=vm.CommodityOwnerType,
                            DateOfBirth=vm.DateOfBirth,
                            BusinessNumber=vm.BusinessNumber,
                            Description=vm.Description,
                            Email=vm.Email,
                            FaxNumber=vm.FaxNumber,
                            FirstName=vm.FirstName,
                            LastName=vm.LastName,
                            Surname=vm.Surname,
                            Gender=vm.Gender,
                            IdNo=vm.IdNo,
                            MaritalStatus=vm.MaritalStatus,
                            OfficeNumber=vm.OfficeNumber,
                            PhoneNumber=vm.PhoneNumber,
                            PhysicalAddress=vm.PhysicalAddress,
                            PinNo=vm.OwnerPinNo,
                            PostalAddress=vm.PostalAddress,
                            CommoditySupplier=vm.CommoditySupplierId
                        };

                    _commodityOwnerViewModelBuilder.Save(commodityOwnerViewModel);
                    var commodityProducerViewModel = new CommodityProducerViewModel
                        {
                            Id=vm.CommodityProducerId,
                            Code=vm.FarmCode,
                            Acrage=vm.Acrage,
                            Name=vm.FarmName,
                            RegNo=vm.RegNo,
                            PhysicalAddress=vm.FarmPhysicalAddress,
                            Description=vm.FarmDescription,
                            CommoditySupplierId=vm.CommoditySupplierId,
                            AssignedFarmCentres = new List<Centre>(){allocatedCenter},

                        };
                    _commodityProducerViewModelBuilder.Save(commodityProducerViewModel);

                    AssignCenter(vm.SelectedCentreId, vm.CommodityProducerId);

                    TempData["msg"] = "Commodity supplier Successfully Created";
                    tran.Complete();
                }


                return RedirectToAction("ListCommoditySuppliers");
            }
            catch (DomainValidationException ve)
            {
                ValidationSummary.DomainValidationErrors(ve, ModelState);
                _log.Debug("Failed to create commodity suppliers " + ve.Message);
                _log.Error("Failed to create commodity suppliers" + ve.ToString());

                return View(vm);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                _log.Debug("Failed to create Commodity suppliers " + ex.Message);
                _log.Error("Failed to create Commodity suppliers" + ex.ToString());

                return View(vm);
            }

        }
        public ActionResult AssignCentre(CommoditySupplierViewModel m)
        {

            ViewBag.CommoditySupplierTypeList = _commoditySupplierViewModelBuilder.CommoditySupplierType();
            ViewBag.ParentCostCentreList = _commoditySupplierViewModelBuilder.ParentCostCentre();
            ViewBag.Banks = _commoditySupplierViewModelBuilder.GetBanks();
            ViewBag.GenderList = _commodityOwnerViewModelBuilder.Gender();
            ViewBag.MaritalStatusList = _commodityOwnerViewModelBuilder.MaritalStatus();
            ViewBag.CommodityOwnerTypeList = _commodityOwnerViewModelBuilder.CommodityOwnerType();

            if (m != null)
            {
                LoadBranches(m.BankId);

             //  m.AssignedFarmCentres = LoadAssignedCenters(m.CommodityProducerId, m.SelectedCentreId);

                return View("CreateCommoditySupplier", m);
            }
            return View("CreateCommoditySupplier");
        }
        public ActionResult CreateCommoditySupplier()
        {
            ViewBag.Banks = _commoditySupplierViewModelBuilder.GetBanks();
            ViewBag.CommoditySupplierTypeList = _commoditySupplierViewModelBuilder.CommoditySupplierType();
            ViewBag.ParentCostCentreList = _commoditySupplierViewModelBuilder.ParentCostCentre();
            ViewBag.GenderList = _commodityOwnerViewModelBuilder.Gender();
            ViewBag.MaritalStatusList = _commodityOwnerViewModelBuilder.MaritalStatus();
            ViewBag.CommodityOwnerTypeList = _commodityOwnerViewModelBuilder.CommodityOwnerType();
            ViewBag.UnassignedCentresList = _centreRepository.GetAll().Select(r => new { r.Id, r.Name }).ToDictionary(d => d.Id, d => d.Name);

            var model = new CommoditySupplierViewModel();
            model.CommoditySupplierId = Guid.NewGuid();
            model.CommodityOwnerId = Guid.NewGuid();
            model.CommodityProducerId = Guid.NewGuid();
            _commoditySupplierViewModelBuilder.SetUpNew(model);
            return View(model);
        }