public ActionResult Create(long Id = 0)
        {
            ViewBag.SuccessMessage = "";

            InvoiceFormModel invoiceFormModel = new InvoiceFormModel();

            if(Id != 0)
            {
                Invoice invoice = invoiceService.GetInvoice(Id);
                invoiceFormModel = AutoMapper.Mapper.Map<Invoice, InvoiceFormModel>(invoice);

                if(!string.IsNullOrEmpty(invoice.Status))
                    invoiceFormModel.Status = (int)Enum.Parse(typeof(StatusEnum), invoice.Status);
            }

            invoiceFormModel = getDropDownValues(invoiceFormModel);

            return View("InvoiceCreate", invoiceFormModel);
        }
        public ActionResult Create(InvoiceFormModel invoicemodel)
        {
            try
            {
                Invoice invoice = AutoMapper.Mapper.Map<InvoiceFormModel, Invoice>(invoicemodel);
                invoice.Status = ((StatusEnum)invoicemodel.Status).ToString();

                if (ModelState.IsValid)
                {
                    if (invoice.ID == 0)
                    {
                        invoiceService.CreateInvoice(invoice);
                    }
                    else
                    {
                        invoiceService.EditInvoice(invoice);
                    }
                    ViewBag.successMessage = "Invoice 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;
                }
                invoicemodel = getDropDownValues(invoicemodel);
            }
            catch (Exception ex)
            {
                ViewBag.successMessage = "";
                ViewBag.errorMessage = string.Format("Error in Invoice Save : {0}", ex.Message);
                invoicemodel = getDropDownValues(invoicemodel);
            }

            return View("InvoiceCreate", invoicemodel);
        }
        private InvoiceFormModel getDropDownValues(InvoiceFormModel invoiceFormModel)
        {
            IEnumerable<StatusEnum> statustypes = Enum.GetValues(typeof(StatusEnum))
                                                   .Cast<StatusEnum>();

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

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

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

                invoiceFormModel.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 == invoiceFormModel.AssignedTo),
                Text = account.AccountName,
                Value = account.ID.ToString()
            }));

            invoiceFormModel.AccountList = accountsitems;

            var contacts = contactService.GetContacts();

            List<SelectListItem> contactsitems = new List<SelectListItem>();
            contactsitems.AddRange(contacts.OrderBy(cont => cont.FirstName)
                .Select(cont =>
                new SelectListItem
                {
                    Selected = (cont.ID == invoiceFormModel.Contact),
                    Text = cont.FirstName,
                    Value = cont.ID.ToString()
                }
                ));

            invoiceFormModel.ContactList = contactsitems;

            var salesorders = salesOrderService.GetSalesOrders();

            List<SelectListItem> salesorderitems = new List<SelectListItem>();
            salesorderitems.AddRange(salesorders.OrderBy(x => x.Subject)
                .Select(x =>
                new SelectListItem
                {
                    Selected = (x.ID == invoiceFormModel.SalesOrder),
                    Text = x.Subject,
                    Value = x.ID.ToString()
                }
                ));

            invoiceFormModel.SalesOrderList = salesorderitems;

            return invoiceFormModel;
        }
        public ActionResult Details(long Id)
        {
            InvoiceFormModel invoiceDetails = new InvoiceFormModel();

            Invoice invoice = invoiceService.GetInvoice(Id);

            invoiceDetails = AutoMapper.Mapper.Map<Invoice, InvoiceFormModel>(invoice);

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

            invoiceDetails.StatusName = invoice.Status;

            if(invoiceDetails.SalesOrder != null)
                invoiceDetails.SalesOrderName = salesOrderService.GetSalesOrder(long.Parse(invoiceDetails.SalesOrder.ToString())) != null ? salesOrderService.GetSalesOrder(long.Parse(invoiceDetails.SalesOrder.ToString())).Subject : string.Empty;

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

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

            return View("InvoiceDetails", invoiceDetails);
        }