public ActionResult Create(PurchaseOrderFormModel purchaseOrdermodel)
        {
            try
            {
                PurchaseOrder purchaseOrder = AutoMapper.Mapper.Map<PurchaseOrderFormModel, PurchaseOrder>(purchaseOrdermodel);
                purchaseOrder.Carrier = ((CarrierEnum)purchaseOrdermodel.Carrier).ToString();
                purchaseOrder.Status = ((StatusEnum)purchaseOrdermodel.Status).ToString();

                if (ModelState.IsValid)
                {
                    if (purchaseOrder.ID == 0)
                    {
                        purchaseOrderService.CreatePurchaseOrder(purchaseOrder);
                    }
                    else
                    {
                        purchaseOrderService.EditPurchaseOrder(purchaseOrder);
                    }
                    ViewBag.successMessage = "PurchaseOrder 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;
                }
                purchaseOrdermodel = getDropDownValues(purchaseOrdermodel);
            }
            catch (Exception ex)
            {
                ViewBag.successMessage = "";
                ViewBag.errorMessage = string.Format("Error in PurchaseOrder Save : {0}", ex.Message);
            }

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

            PurchaseOrderFormModel purchaseOrderFormModel = new PurchaseOrderFormModel();

            if(Id != 0)
            {
                PurchaseOrder purchaseOrder = purchaseOrderService.GetPurchaseOrder(Id);
                purchaseOrderFormModel = AutoMapper.Mapper.Map<PurchaseOrder, PurchaseOrderFormModel>(purchaseOrder);

                if (!string.IsNullOrEmpty(purchaseOrder.Status))
                    purchaseOrderFormModel.Status = (int)Enum.Parse(typeof(StatusEnum), purchaseOrder.Status);
                if (!string.IsNullOrEmpty(purchaseOrder.Carrier))
                    purchaseOrderFormModel.Carrier = (int)Enum.Parse(typeof(CarrierEnum), purchaseOrder.Carrier);

            }

            purchaseOrderFormModel = getDropDownValues(purchaseOrderFormModel);

            return View("PurchaseOrderCreate", purchaseOrderFormModel);
        }
        private PurchaseOrderFormModel getDropDownValues(PurchaseOrderFormModel purchaseOrderFormModel)
        {
            IEnumerable<CarrierEnum> sourcetypes = Enum.GetValues(typeof(CarrierEnum))
                                                     .Cast<CarrierEnum>();

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

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

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

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

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

                purchaseOrderFormModel.GrpUserList = items;
            }

            var contacts = contactService.GetContacts();

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

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

            purchaseOrderFormModel.ContactList = contactItems;

            var vendors = vendorService.GetVendors();

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

            vendoritems.AddRange(vendors.OrderBy(x => x.VendorName)
            .Select(x => new SelectListItem
            {
               Selected = (x.ID == purchaseOrderFormModel.Vendor),
               Text = x.VendorName,
               Value = x.ID.ToString()
            }
            ));

            purchaseOrderFormModel.VendorList = vendoritems;

            return purchaseOrderFormModel;
        }
        public ActionResult Details(long Id)
        {
            PurchaseOrderFormModel purchaseOrderDetails = new PurchaseOrderFormModel();

            PurchaseOrder purchaseOrder = purchaseOrderService.GetPurchaseOrder(Id);

            purchaseOrderDetails = AutoMapper.Mapper.Map<PurchaseOrder, PurchaseOrderFormModel>(purchaseOrder);

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

            purchaseOrderDetails.CarrierName = purchaseOrder.Carrier;
            purchaseOrderDetails.StatusName = purchaseOrder.Status;

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

            if (purchaseOrderDetails.Vendor != null)
                purchaseOrderDetails.VendorName = vendorService.GetVendor(long.Parse(purchaseOrderDetails.Vendor.ToString())) != null ? vendorService.GetVendor(long.Parse(purchaseOrderDetails.Vendor.ToString())).VendorName : string.Empty;

            return View("PurchaseOrderDetails", purchaseOrderDetails);
        }