public ActionResult Create(SalesOrderFormModel salesOrdermodel)
        {
            try
            {
                SalesOrder salesOrder = AutoMapper.Mapper.Map<SalesOrderFormModel, SalesOrder>(salesOrdermodel);
                salesOrder.Carrier = ((CarrierEnum)salesOrdermodel.Carrier).ToString();
                salesOrder.Status = ((StatusEnum)salesOrdermodel.Status).ToString();

                if (ModelState.IsValid)
                {
                    if (salesOrder.ID == 0)
                    {
                        salesOrderService.CreateSalesOrder(salesOrder);
                    }
                    else
                    {
                        salesOrderService.EditSalesOrder(salesOrder);
                    }
                    ViewBag.successMessage = "SalesOrder 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;
                }
                salesOrdermodel = getdropdownValues(salesOrdermodel);
            }
            catch (Exception ex)
            {
                ViewBag.successMessage = "";
                ViewBag.errorMessage = string.Format("Error in SalesOrder Save : {0}", ex.Message);
                salesOrdermodel = getdropdownValues(salesOrdermodel);
            }

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

            SalesOrderFormModel salesOrderFormModel = new SalesOrderFormModel();

            if(Id != 0)
            {
                SalesOrder salesOrder = salesOrderService.GetSalesOrder(Id);

                salesOrderFormModel = AutoMapper.Mapper.Map<SalesOrder, SalesOrderFormModel>(salesOrder);

                if (!string.IsNullOrEmpty(salesOrder.Status))
                    salesOrderFormModel.Status = (int)Enum.Parse(typeof(StatusEnum), salesOrder.Status);
                if (!string.IsNullOrEmpty(salesOrder.Carrier))
                    salesOrderFormModel.Carrier = (int)Enum.Parse(typeof(CarrierEnum), salesOrder.Carrier);
            }

            salesOrderFormModel = getdropdownValues(salesOrderFormModel);

            return View("SalesOrderCreate", salesOrderFormModel);
        }
        private SalesOrderFormModel getdropdownValues(SalesOrderFormModel salesOrderFormModel)
        {
            IEnumerable<CarrierEnum> sourcetypes = Enum.GetValues(typeof(CarrierEnum))
                                                     .Cast<CarrierEnum>();

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

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

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

            if (salesOrderFormModel.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 == salesOrderFormModel.AssignedTo),
                   Text = grp.Name,
                   Value = grp.ID.ToString()
               }));

                salesOrderFormModel.GrpUserList = grpItems;
            }
            else
            {
                salesOrderFormModel.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 == salesOrderFormModel.AssignedTo),
                    Text = user.Name,
                    Value = user.ID.ToString()
                }));

                salesOrderFormModel.GrpUserList = items;
            }

            var accounts = accountService.GetAccounts();

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

            salesOrderFormModel.AccountList = accountsitems;

            var contacts = contactService.GetContacts();

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

            contactItems.AddRange(contacts.OrderBy(x => x.FirstName)
                .Select(x => new SelectListItem
                {
                    Selected = (x.ID == salesOrderFormModel.Contact),
                    Text = x.FirstName,
                    Value = x.ID.ToString()
                }
            ));

            salesOrderFormModel.ContactList = contactItems;

            var potentials = potentialService.GetPotentials();

            List<SelectListItem> potentialItems = new List<SelectListItem>();
            potentialItems.AddRange(potentials.OrderBy(x => x.PotentialName)
             .Select(x =>
            new SelectListItem
            {
                Selected = (x.ID == salesOrderFormModel.Potential),
                Text = x.PotentialName,
                Value = x.ID.ToString()
            }));

            salesOrderFormModel.PotentialList = potentialItems;

            var quotes = quoteService.GetQuotes();

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

            quoteItems.AddRange(quotes.OrderBy(x => x.Subject)
                .Select(x =>
                new SelectListItem
                {
                    Selected = (x.ID == salesOrderFormModel.Quote),
                    Text = x.Subject,
                    Value = x.ID.ToString()
                }));

            salesOrderFormModel.QuoteList = quoteItems;

            return salesOrderFormModel;
        }
        public ActionResult Details(long Id)
        {
            SalesOrderFormModel salesOrderDetails = new SalesOrderFormModel();

            SalesOrder salesOrder = salesOrderService.GetSalesOrder(Id);

            salesOrderDetails = AutoMapper.Mapper.Map<SalesOrder, SalesOrderFormModel>(salesOrder);

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

            salesOrderDetails.CarrierName = salesOrder.Carrier;
            salesOrderDetails.StatusName = salesOrder.Status;

            if (salesOrderDetails.Account != null)
                salesOrderDetails.AccountName = accountService.GetAccount(long.Parse(salesOrderDetails.Account.ToString())) != null ? accountService.GetAccount(long.Parse(salesOrderDetails.Account.ToString())).AccountName : string.Empty;

            if (salesOrderDetails.Contact != null)
                salesOrderDetails.ContactName = contactService.GetContact(long.Parse(salesOrderDetails.Contact.ToString())) != null ? contactService.GetContact(long.Parse(salesOrderDetails.Contact.ToString())).FirstName : string.Empty;

            if (salesOrderDetails.Potential != null)
                salesOrderDetails.PotentialName = potentialService.GetPotential(long.Parse(salesOrderDetails.Potential.ToString())) != null ? potentialService.GetPotential(long.Parse(salesOrderDetails.Potential.ToString())).PotentialName : string.Empty;

            if (salesOrderDetails.Quote != null)
                salesOrderDetails.QuoteName = quoteService.GetQuote(long.Parse(salesOrderDetails.Quote.ToString())) != null ? quoteService.GetQuote(long.Parse(salesOrderDetails.Quote.ToString())).Subject : string.Empty;

            return View("SalesOrderDetails", salesOrderDetails);
        }