public ActionResult AddNewVendor(int id, WorkgroupVendor workgroupVendor, bool skipAddress)
        {
            ViewBag.StepNumber = 9;
            var workgroup = _workgroupRepository.GetNullableById(id);

            if (workgroup == null)
            {
                Message = "Workgroup not found.";
                this.RedirectToAction <WorkgroupController>(a => a.Index(false));
            }

            if (skipAddress)
            {
                workgroupVendor.Line1       = "N/A";
                workgroupVendor.City        = "N/A";
                workgroupVendor.State       = "CA";
                workgroupVendor.Zip         = "95616";
                workgroupVendor.CountryCode = "US";
            }

            var workgroupVendorToCreate = new WorkgroupVendor();

            _workgroupService.TransferValues(workgroupVendor, ref workgroupVendorToCreate);

            workgroupVendorToCreate.Workgroup = workgroup;

            ModelState.Clear();
            workgroupVendorToCreate.TransferValidationMessagesTo(ModelState);

            if (ModelState.IsValid)
            {
                _workgroupVendorRepository.EnsurePersistent(workgroupVendorToCreate);

                Message = "WorkgroupVendor Created Successfully";

                return(this.RedirectToAction(a => a.Vendors(id)));
            }

            WorkgroupVendorViewModel viewModel;


            //var vendor = _vendorRepository.GetNullableById(workgroupVendor.VendorId);
            //var vendorAddress = _vendorAddressRepository.Queryable.FirstOrDefault(a => a.Vendor == vendor && a.TypeCode == workgroupVendor.VendorAddressTypeCode);
            viewModel = WorkgroupVendorViewModel.Create(_vendorRepository, workgroupVendorToCreate, null, null, true);



            return(View(viewModel));
        }
Esempio n. 2
0
        public JsonNetResult AddKfsVendor(int workgroupId, string vendorId, string addressTypeCode)
        {
            var    workgroup     = _repositoryFactory.WorkgroupRepository.GetById(workgroupId);
            var    vendor        = _vendorRepository.Queryable.Single(a => a.Id == vendorId);
            var    vendorAddress = _vendorAddressRepository.Queryable.First(a => a.Vendor == vendor && a.TypeCode == addressTypeCode);
            var    added         = true;
            var    duplicate     = false;
            var    wasInactive   = false;
            string errorMessage  = null;

            // just make sure user has access to the workgroup
            Check.Require(_securityService.HasWorkgroupAccess(workgroup), Resources.NoAccess_Workgroup);

            var workgroupVendor = new WorkgroupVendor
            {
                Workgroup             = workgroup,
                VendorId              = vendor.Id,
                VendorAddressTypeCode = vendorAddress.TypeCode
                                        //,
                                        //Name = vendor.Name,
                                        //Line1 = vendorAddress.Line1,
                                        //Line2 = vendorAddress.Line2,
                                        //Line3 = vendorAddress.Line3,
                                        //City = vendorAddress.City,
                                        //State = vendorAddress.State,
                                        //Zip = vendorAddress.Zip,
                                        //CountryCode = vendorAddress.CountryCode
            };
            var workgroupVendorToCreate = new WorkgroupVendor();

            _workgroupService.TransferValues(workgroupVendor, ref workgroupVendorToCreate); //Central location for values that get transferred (Fax, state, etc.)
            workgroupVendorToCreate.Workgroup = workgroup;


            if (!_repositoryFactory.WorkgroupVendorRepository.Queryable
                .Any(a => a.Workgroup.Id == workgroupId &&
                     a.VendorId == workgroupVendorToCreate.VendorId &&
                     a.VendorAddressTypeCode == workgroupVendorToCreate.VendorAddressTypeCode))
            {
                //doesn't find any
                var tempModelState = new ModelStateDictionary();
                workgroupVendorToCreate.TransferValidationMessagesTo(tempModelState);
                if (!tempModelState.IsValid)
                {
                    if (tempModelState.ContainsKey("WorkgroupVendor.Email"))
                    {
                        workgroupVendorToCreate.Email = null;
                        errorMessage = "Warning, Email removed. KFS Vendor's Email was invalid";
                    }
                }

                try
                {
                    _repositoryFactory.WorkgroupVendorRepository.EnsurePersistent(workgroupVendorToCreate);
                }
                catch (Exception)
                {
                    errorMessage = "An Error occurred while trying to add that vendor to your workgroup.";
                }

                added       = true;
                duplicate   = false;
                wasInactive = false;
            }
            else
            {
                //found one
                var inactiveVendor = _repositoryFactory.WorkgroupVendorRepository.Queryable
                                     .FirstOrDefault(a => a.Workgroup.Id == workgroupId &&
                                                     a.VendorId == workgroupVendorToCreate.VendorId &&
                                                     a.VendorAddressTypeCode == workgroupVendorToCreate.VendorAddressTypeCode &&
                                                     !a.IsActive);
                if (inactiveVendor != null)
                {
                    // we found one this is disabled, activate it and return it
                    inactiveVendor.IsActive = true;
                    _repositoryFactory.WorkgroupVendorRepository.EnsurePersistent(inactiveVendor);
                    added       = false;
                    duplicate   = false;
                    wasInactive = true;
                    return(new JsonNetResult(new { id = inactiveVendor.Id, name = inactiveVendor.Name, added, duplicate, wasInactive, errorMessage }));
                }
                // there was an active duplicate, return the first one.
                workgroupVendorToCreate = _repositoryFactory.WorkgroupVendorRepository.Queryable
                                          .First(a => a.Workgroup.Id == workgroupId &&
                                                 a.VendorId == workgroupVendorToCreate.VendorId &&
                                                 a.VendorAddressTypeCode == workgroupVendorToCreate.VendorAddressTypeCode &&
                                                 a.IsActive);
                added       = false;
                duplicate   = true;
                wasInactive = false;
            }

            return(new JsonNetResult(new { id = workgroupVendorToCreate.Id, name = workgroupVendorToCreate.Name, added, duplicate, wasInactive, errorMessage }));
        }
Esempio n. 3
0
        public ActionResult AddVendor(int workgroupId, WorkgroupVendor vendor)
        {
            var workgroup = _repositoryFactory.WorkgroupRepository.GetById(workgroupId);

            Check.Require(_securityService.HasWorkgroupAccess(workgroup));

            var modelState = new ModelStateDictionary();
            vendor.Workgroup = workgroup;
            vendor.TransferValidationMessagesTo(modelState);
            if (!modelState.IsValid)
            {
                return Json(new {success = false});
            }

            workgroup.AddVendor(vendor);

            _repositoryFactory.WorkgroupRepository.EnsurePersistent(workgroup);

            return Json(new { id = vendor.Id, success = true });
        }
        public ActionResult AddKfsVendor(int id, WorkgroupVendor workgroupVendor)
        {
            ViewBag.StepNumber = 9;
            var workgroup = _workgroupRepository.GetNullableById(id);

            if (workgroup == null)
            {
                Message = "Workgroup not found.";
                this.RedirectToAction <WorkgroupController>(a => a.Index(false));
            }

            var workgroupVendorToCreate = new WorkgroupVendor();

            _workgroupService.TransferValues(workgroupVendor, ref workgroupVendorToCreate);

            workgroupVendorToCreate.Workgroup = workgroup;

            ModelState.Clear();
            //workgroupVendorToCreate.TransferValidationMessagesTo(ModelState);
            if (string.IsNullOrWhiteSpace(workgroupVendorToCreate.VendorId))
            {
                ModelState.AddModelError("WorkgroupVendor.VendorId", "Please select a Kfs Vendor");
            }
            if (string.IsNullOrWhiteSpace(workgroupVendorToCreate.VendorAddressTypeCode))
            {
                ModelState.AddModelError("WorkgroupVendor.VendorAddressTypeCode", "Please select a Vendor Address");
            }
            if (ModelState.IsValid)
            {
                workgroupVendorToCreate.TransferValidationMessagesTo(ModelState);
                if (ModelState.ContainsKey("WorkgroupVendor.Email"))
                {
                    //Message = "Vendor Email is invalid";
                    workgroupVendorToCreate.Email = null;
                    ModelState.Clear();
                    //workgroupVendorToCreate.TransferValidationMessagesTo(ModelState); Note: Not checking this again so it *WILL* throw an exception that can be investigated.
                }
            }

            if (ModelState.IsValid)
            {
                if (_workgroupVendorRepository.Queryable
                    .Any(a => a.Workgroup.Id == id &&
                         a.VendorId == workgroupVendorToCreate.VendorId &&
                         a.VendorAddressTypeCode == workgroupVendorToCreate.VendorAddressTypeCode &&
                         a.IsActive))
                {
                    Message = "KFS vendor has already been added";
                    return(this.RedirectToAction(a => a.Vendors(id)));
                }
                var inactiveKfsVendor = _workgroupVendorRepository.Queryable
                                        .FirstOrDefault(a => a.Workgroup.Id == id &&
                                                        a.VendorId == workgroupVendorToCreate.VendorId &&
                                                        a.VendorAddressTypeCode == workgroupVendorToCreate.VendorAddressTypeCode &&
                                                        !a.IsActive);
                if (inactiveKfsVendor != null)
                {
                    inactiveKfsVendor.IsActive = true;
                    _workgroupVendorRepository.EnsurePersistent(inactiveKfsVendor);
                    Message = "KFS vendor added back. It was previously deleted from this workgroup.";
                    return(this.RedirectToAction(a => a.Vendors(id)));
                }

                _workgroupVendorRepository.EnsurePersistent(workgroupVendorToCreate);

                Message = "WorkgroupVendor Created Successfully";

                return(this.RedirectToAction(a => a.Vendors(id)));
            }

            ErrorMessage = "Please correct errors before continuing, of skip this step.";
            WorkgroupVendorViewModel viewModel;


            viewModel = WorkgroupVendorViewModel.Create(_vendorRepository, workgroupVendorToCreate, newVendor: true);

            return(View(viewModel));
        }