public ActionResult Create(PotentialFormModel potentialFormModel)
        {
            try
            {
                Potential potential = AutoMapper.Mapper.Map<PotentialFormModel, Potential>(potentialFormModel);

                potential.LeadSource = ((LeadSourceEnum)potentialFormModel.LeadSource).ToString();
                potential.SalesStage = ((SalesStageEnum)potentialFormModel.SalesStage).ToString();
                potential.Type = ((TypeEnum)potentialFormModel.Type).ToString();

                if (ModelState.IsValid)
                {
                    if (potential.ID == 0)
                    {
                        potentialService.CreatePotential(potential);
                    }
                    else
                    {
                        potentialService.EditPotential(potential);
                    }
                    ViewBag.successMessage = "Potential Saved Successfully";
                    ViewBag.errorMessage = "";
                }
                else
                {
                    string validationErrors = string.Join(",", ModelState.Values.Where(E => E.Errors.Count > 0)
                    .SelectMany(E => E.Errors).Select(E => E.ErrorMessage).ToArray());

                    ViewBag.successMessage = "";
                    ViewBag.errorMessage = validationErrors;
                }
                potentialFormModel = getPotentialDropDownValues(potentialFormModel);
            }
            catch (Exception ex)
            {
                ViewBag.successMessage = "";
                ViewBag.errorMessage = string.Format("Error in Potential Save : {0}", ex.Message);
                potentialFormModel = getPotentialDropDownValues(potentialFormModel);
            }

            return View("PotentialCreate", potentialFormModel);
        }
        public ActionResult Create(long Id = 0)
        {
            ViewBag.SuccessMessage = "";

            PotentialFormModel potentialformmodel = new PotentialFormModel();

            if (Id != 0)
            {
                Potential potential = potentialService.GetPotential(Id);

                potentialformmodel = AutoMapper.Mapper.Map<Potential, PotentialFormModel>(potential);
                if(!string.IsNullOrEmpty(potential.LeadSource))
                    potentialformmodel.LeadSource = (int) Enum.Parse(typeof(LeadSourceEnum), potential.LeadSource);
                if (!string.IsNullOrEmpty(potential.Type))
                    potentialformmodel.Type = (int)Enum.Parse(typeof(TypeEnum), potential.Type);
                if(!string.IsNullOrEmpty(potential.SalesStage))
                    potentialformmodel.SalesStage = (int)Enum.Parse(typeof(SalesStageEnum), potential.SalesStage);
            }

            potentialformmodel = getPotentialDropDownValues(potentialformmodel);

            return View("PotentialCreate", potentialformmodel);
        }
        public ActionResult Details(PotentialFormModel potentialformmodel, string command)
        {
            if (command == "Create Invoice")
            {
                Invoice invoice = new Invoice()
                {
                    InvoiceNo = "Inv0001",
                    CustomerNo = "Inv0001",
                    Account = potentialformmodel.Account,
                    AssignedTo = potentialformmodel.AssignedTo,
                    AssignedToUserGroup = potentialformmodel.AssignedToUserGroup == UserGroup.group ? "group" : "user",
                    CreatedOn = DateTime.Now,
                    ModifiedOn = DateTime.Now,
                    CreatedBy = Session["UserID"].ToString(),
                    ModifiedBy = Session["UserID"].ToString()

                };

                invoiceService.CreateInvoice(invoice);

                return RedirectToAction("List", "Invoice");
            }
            else if (command == "Create Quote")
            {
                Quote quote = new Quote()
                {
                    Subject = potentialformmodel.PotentialName,
                    Account = potentialformmodel.Account,
                    Potential = potentialformmodel.ID,
                    AssignedTo = potentialformmodel.AssignedTo,
                    AssignedToUserGroup = potentialformmodel.AssignedToUserGroup == UserGroup.group ? "group" : "user",
                    CreatedOn = DateTime.Now,
                    ModifiedOn = DateTime.Now,
                    CreatedBy = Session["UserID"].ToString(),
                    ModifiedBy = Session["UserID"].ToString(),
                };

                quoteService.CreateQuote(quote);

                return RedirectToAction("List", "Quote");
            }

            return RedirectToAction("List", "Potential");
        }
        private PotentialFormModel getPotentialDropDownValues(PotentialFormModel potentialFormModel)
        {
            IEnumerable<LeadSourceEnum> sourcetypes = Enum.GetValues(typeof(LeadSourceEnum))
                                                     .Cast<LeadSourceEnum>();

            potentialFormModel.LeadSourceList = from src in sourcetypes
                                                select new SelectListItem
                                                {
                                                    Text = src.ToString(),
                                                    Value = ((int)src).ToString()
                                                };

            IEnumerable<SalesStageEnum> statustypes = Enum.GetValues(typeof(SalesStageEnum))
                                                      .Cast<SalesStageEnum>();

            potentialFormModel.SalesStageList = from src in statustypes
                                                select new SelectListItem
                                                {
                                                    Text = src.ToString(),
                                                    Value = ((int)src).ToString()
                                                };

            if (potentialFormModel.AssignedToUserGroup == UserGroup.group)
            {
                var groups = groupService.GetGroups();

                List<SelectListItem> grpItems = new List<SelectListItem>();

                grpItems.AddRange(groups.OrderBy(grp => grp.Name)
                .Select(grp =>
               new SelectListItem
               {
                   Selected = (grp.ID == potentialFormModel.AssignedTo),
                   Text = grp.Name,
                   Value = grp.ID.ToString()
               }));

                potentialFormModel.GrpUserList = grpItems;
            }
            else
            {
                potentialFormModel.AssignedToUserGroup = UserGroup.user;

                var users = userService.GetUsers();

                List<SelectListItem> items = new List<SelectListItem>();
                items.AddRange(users.OrderBy(user => user.Name)
                 .Select(user =>
                new SelectListItem
                {
                    Selected = (user.ID == potentialFormModel.AssignedTo),
                    Text = user.Name,
                    Value = user.ID.ToString()
                }));

                potentialFormModel.GrpUserList = items;
            }

            IEnumerable<TypeEnum> typeNames = Enum.GetValues(typeof(TypeEnum))
                                                    .Cast<TypeEnum>();

            potentialFormModel.TypeList = from src in typeNames
                                                select new SelectListItem
                                                {
                                                    Text = src.ToString(),
                                                    Value = ((int)src).ToString()
                                                };

            var campaigns = campaignService.GetCampaigns();

            List<SelectListItem> campaignsitems = new List<SelectListItem>();
            campaignsitems.AddRange(campaigns.OrderBy(campaign => campaign.CampaignName)
             .Select(campaign =>
            new SelectListItem
            {
                Selected = (campaign.ID == potentialFormModel.AssignedTo),
                Text = campaign.CampaignName,
                Value = campaign.ID.ToString()
            }));

            potentialFormModel.CampaignSourceList = campaignsitems;

            var accounts = accountService.GetAccounts();

            List<SelectListItem> accountsitems = new List<SelectListItem>();
            accountsitems.AddRange(accounts.OrderBy(account => account.AccountName)
             .Select(account =>
            new SelectListItem
            {
                Selected = (account.ID == potentialFormModel.AssignedTo),
                Text = account.AccountName,
                Value = account.ID.ToString()
            }));

            potentialFormModel.AccountNameList = accountsitems;

            return potentialFormModel;
        }
        public ActionResult Details(long Id)
        {
            PotentialFormModel potentialDetails = new PotentialFormModel();

            Potential potential = potentialService.GetPotential(Id);

            potentialDetails = AutoMapper.Mapper.Map<Potential, PotentialFormModel>(potential);

            if (potentialDetails.AssignedToUserGroup == UserGroup.group)
            {
                potentialDetails.AssignedToName = groupService.GetGroup(potentialDetails.AssignedTo) != null ? groupService.GetGroup(potentialDetails.AssignedTo).Name : string.Empty;
            }
            else
            {
                potentialDetails.AssignedToName = userService.GetUser(potentialDetails.AssignedTo) != null ? userService.GetUser(potentialDetails.AssignedTo).Name : string.Empty;
            }

            potentialDetails.LeadSourceName = potential.LeadSource;
            potentialDetails.SalesStageName = potential.SalesStage;
            potentialDetails.TypeName = potential.Type;

            potentialDetails.CampaignSourceName = campaignService.GetCampaign(potentialDetails.CampaignSource) != null ? campaignService.GetCampaign(potentialDetails.CampaignSource).CampaignName : string.Empty;

            potentialDetails.AccountName = accountService.GetAccount(potentialDetails.Account) != null ? accountService.GetAccount(potentialDetails.Account).AccountName : string.Empty;

            potentialDetails.ExpectedCloseDateDisplay = potential.ExpectedCloseDate.Value.ToShortDateString();

            return View("PotentialDetails", potentialDetails);
        }