public   ContactViewModel Map(Contact cont)
        {
            //if (cont == null)
            //    return null;
            //else
            //{
                var contTypes = _contactTypeRepository.GetAll().ToDictionary(n=>n.Id,n=>n.Name);
                var contOwner = _contactRepository.GetAll().OrderBy(n => n.ContactOwnerType).Distinct().ToDictionary(n=>n.Id,n=>n.ContactOwnerType);
                Dictionary<int, string> cl = EnumHelper.EnumToList<ContactClassification>()
                               .ToDictionary(n => (int)n, n => n.ToString());
                ContactViewModel contactVM = new ContactViewModel();
               
                    contactVM.Id = cont.Id;
                    contactVM.BusinessPhone = cont.BusinessPhone;
                    contactVM.Firstname = cont.Firstname;
                    contactVM.Lastname = cont.Lastname;
                    contactVM.Fax = cont.Fax;
                    contactVM.MobilePhone = cont.MobilePhone;
                    contactVM.PostalAddress = cont.PostalAddress;
                    contactVM.PhysicalAddress = cont.PhysicalAddress;
                    contactVM.Email = cont.Email;
                    contactVM.HomePhone = cont.HomePhone ;
                    contactVM.City = cont.City;
                    contactVM.CostCentre = cont.ContactOwnerMasterId;
                    contactVM.HomeTown = cont.HomeTown;
                    contactVM.IsActive = cont._Status == EntityStatus.Active ? true : false;
                    contactVM.JobTitle = cont.JobTitle;
                    contactVM.ChildrenNames = cont.ChildrenNames;
                     contactVM.Company=cont.Company;
                     contactVM.WorkExtPhone=cont.WorkExtPhone;
                     contactVM.SpouseName = cont.SpouseName;
                     contactVM.ContactOwner = (int)cont.ContactOwnerType;
                     contactVM.ContactOwnerList = new SelectList(contOwner, "Key", "Value", (int)cont.ContactOwnerType);
                     contactVM.ContactTypeList = new SelectList(contTypes, "Key", "Value", cont.ContactType == null ? Guid.Empty : cont.ContactType.Id);
                     contactVM.Fullnames = cont.Firstname + " " + cont.Lastname;
                     if (cont.DateOfBirth.HasValue)
                         contactVM.DateofBirth = cont.DateOfBirth.Value;
                if (cont.MStatus != null)
                {
                    contactVM.MaritalStatus = cont.MStatus.ToString();
                    // contactVM.MaritalStatusId = _maritalStatusRepository.GetById(cont.MStatus.Id).Id;
                    //contactVM.MaritalStatus = _maritalStatusRepository.GetById(cont.MStatus.Id).MStatus;
                }

                if (cont.ContactType != null)
                {
                    contactVM.ContactTypeId = _contactTypeRepository.GetById(cont.ContactType.Id).Id;
                    contactVM.ContactTypeName = _contactTypeRepository.GetById(cont.ContactType.Id).Name;
                }
                    contactVM.ClassificationList=new SelectList(cl,"Key","Value");
                    return contactVM;
            //}
        }
        public ActionResult ListContacts(bool? showInactive, Guid? CostCentre, int? ContactOwner, int itemsperpage = 10, int page = 1, string searchParam = "")
        {
            ViewBag.contactOwnerList = AcceptedContactOwnerTypes();// _contactModelViewBuilder.ContactOwner();
            ViewBag.contactOwner = null;
            ViewBag.searchParam = searchParam;
            try
            {
                if (itemsperpage != null)
                {
                    ViewModelBase.ItemsPerPage = itemsperpage;
                }
                

                ViewBag.CostCentre = CostCentre;
                bool showinactive = false;
                if (showInactive != null)
                    showinactive = (bool)showInactive;

                ViewBag.showInactive = showinactive;
                ViewBag.ContactsFor = "All";
                if (TempData["msg"] != null)
                {
                    ViewBag.msg = TempData["msg"].ToString();
                    TempData["msg"] = null;
                }
                //var ls = _contactModelViewBuilder.Query(showinactive);
                //var ls = _contactModelViewBuilder.GetAll(showinactive);
                //int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
                //return View(ls.ToPagedList(currentPageIndex, ViewModelBase.ItemsPerPage));

                var currentPageIndex = page - 1 < 0 ? 0 : page - 1;
                var take = itemsperpage;
                var skip = currentPageIndex * take;
                var query = new QueryStandard { SupplierId = CostCentre, Name = searchParam, ShowInactive = showinactive, Skip = skip, Take = take };
                var ls = _contactModelViewBuilder.Query(query);
                var total = ls.Count;
                var data = ls.Data.ToList();


                return View(data.ToPagedList(currentPageIndex, take, total));
          
            }
            catch (Exception ex)
            {
                ViewBag.msg = ex.Message;
                var errorContactList = new ContactViewModel();
                errorContactList.ErrorText = ex.Message;
                return View();
            }
        }
 public ActionResult EditContact(ContactViewModel cvm)
 {
     ViewBag.ContactClassificationList = _contactModelViewBuilder.ContactClassification();
     ViewBag.MaritalStatusList = _contactModelViewBuilder.GetMarialStatus();
     ViewBag.CostCentreList = _contactModelViewBuilder.CostCentre();
     ViewBag.contactTypeList = _contactModelViewBuilder.GetContactTypes();
     try
     {
         _contactModelViewBuilder.save(cvm);
         _auditLogViewModelBuilder.AddAuditLog(this.User.Identity.Name, "Edit", "Contact", DateTime.Now);
         TempData["msg"] = "Contact Successfully Edited";
         return RedirectToAction("ListContacts", new {CostCentre=cvm.CostCentre });
     }
     catch (DomainValidationException dve)
     {
         ValidationSummary.DomainValidationErrors(dve, ModelState);
         ViewBag.msg = dve.Message;
         return View();
     }
     catch (Exception exx)
     {
         ViewBag.msg = exx.Message;
         return View();
     }
 }
        public ActionResult CreateContact(ContactViewModel cvm)
        {
            ViewBag.CostCentreList = _contactModelViewBuilder.CostCentre();
            ViewBag.MaritalStatusList = _contactModelViewBuilder.GetMarialStatus();
            ViewBag.ContactClassificationList = _contactModelViewBuilder.ContactClassification();
            ViewBag.contactTypeList = _contactModelViewBuilder.GetContactTypes();
            ViewBag.contactOwnerList = AcceptedContactOwnerTypes();// _contactModelViewBuilder.ContactOwner();
            try
            {
                if (cvm.Firstname == null)
                {
                    ModelState.AddModelError("Contact", "Firstname is required");
                    ContactViewModel cvm2 = new ContactViewModel
                    {
                        CostCentre = Guid.NewGuid(),
                        CostCentreName = "",
                        ContactFor = ""
                    };
                    return View(cvm2);
                }
                else
                {
                    cvm.Id = Guid.NewGuid();
                    _contactModelViewBuilder.save(cvm);
                    ViewBag.CostCentreList = _contactModelViewBuilder.CostCentre();
                    _auditLogViewModelBuilder.AddAuditLog(this.User.Identity.Name, "Create", "Contact", DateTime.Now);
                    TempData["msg"] = "Contact Successfully Created";

                    return RedirectToAction("ListContacts", new { CostCentre = cvm.CostCentre, CostCentreName = cvm.CostCentreName, ContactFor = cvm.ContactFor });
                }
            }
            catch (DomainValidationException dve)
            {
                
                ValidationSummary.DomainValidationErrors(dve,ModelState);
                ContactViewModel cvm2 = new ContactViewModel
                {
                    CostCentre = Guid.NewGuid(),
                    CostCentreName = "",
                    ContactFor = ""
                };
                return View(cvm2);
            }
            catch (Exception exx)
            {
                ViewBag.msg = exx.Message;
                ContactViewModel cvm2 = new ContactViewModel
                {
                    CostCentre = Guid.NewGuid(),
                    CostCentreName = "",
                    ContactFor = ""  
                };
                return View(cvm2);
            }
        }
 public ActionResult CreateContact(Guid? CostCentre, string CostCentreName, string ContactFor)
 {
     ViewBag.CostCentreList = _contactModelViewBuilder.CostCentre();
     ViewBag.MaritalStatusList = _contactModelViewBuilder.GetMarialStatus();
     ViewBag.ContactClassificationList = _contactModelViewBuilder.ContactClassification();
     ViewBag.contactTypeList = _contactModelViewBuilder.GetContactTypes();
     ViewBag.contactOwnerList = AcceptedContactOwnerTypes();//_contactModelViewBuilder.ContactOwner();
     try
     {
         if (CostCentre.HasValue)
         {
             ContactViewModel cvm = new ContactViewModel
             {
                 CostCentre = CostCentre.Value,
                 CostCentreName = CostCentreName,
                 ContactFor = ContactFor
             };
             //return View("CreateContact",_contactModelViewBuilder.Get(0));
             return View(cvm);
         }
         else
         {
             ContactViewModel cvm2 = new ContactViewModel
            {
                CostCentre = Guid.NewGuid(),
                CostCentreName = "",
                ContactFor = ""
            };
             return View("CreateContact",cvm2);
         }
     }
     catch (Exception ex)
     {
         return View(ex.Message);
     }
 }
        public void save(ContactViewModel contactViewModel)
        {

            Contact cont = new Contact(contactViewModel.Id);
           // {
                cont.Firstname = contactViewModel.Firstname;
                cont.Lastname = contactViewModel.Lastname;
                cont.BusinessPhone = contactViewModel.BusinessPhone;
                cont.Fax = contactViewModel.Fax;
                cont.MobilePhone = contactViewModel.MobilePhone;
                cont.PhysicalAddress = contactViewModel.PhysicalAddress;
                cont.PostalAddress = contactViewModel.PostalAddress;
                cont.Company = contactViewModel.Company;
                cont.Email = contactViewModel.Email;
                cont.City = contactViewModel.City;
                cont.ContactClassification = contactViewModel.Classification;
                cont.ContactOwnerMasterId = contactViewModel.CostCentre;
                if (contactViewModel.ContactFor == "User")
                {
                    cont.ContactOwnerType = Distributr.Core.Domain.Master.CostCentreEntities.ContactOwnerType.User;
                }
                if (contactViewModel.ContactFor == "Distributor")
                {
                    cont.ContactOwnerType = Distributr.Core.Domain.Master.CostCentreEntities.ContactOwnerType.Distributor;
                }
                if (contactViewModel.ContactFor == "Outlet")
                {
#if(KEMSA)

                    cont.ContactOwnerType = Distributr.Core.Domain.Master.CostCentreEntities.ContactOwnerType.HealthFacility;
#else
                    cont.ContactOwnerType = Distributr.Core.Domain.Master.CostCentreEntities.ContactOwnerType.Outlet;
#endif
                }
                else
                {
                    cont.ContactOwnerType = (ContactOwnerType)contactViewModel.ContactOwner;
                }
            cont.ContactOwnerMasterId = contactViewModel.CostCentre;
                cont.HomePhone = contactViewModel.HomePhone;
                cont.HomeTown = contactViewModel.HomeTown;
                cont.DateOfBirth = contactViewModel.DateofBirth;
            cont.MStatus = contactViewModel.MaritalStatusId;
               cont.ContactType = _contactTypeRepository.GetById(contactViewModel.ContactTypeId);
                cont.SpouseName = contactViewModel.SpouseName;
                cont.WorkExtPhone = contactViewModel.WorkExtPhone;
                cont.ChildrenNames = contactViewModel.ChildrenNames;
                cont.JobTitle = contactViewModel.JobTitle;
            //};
         
               _contactRepository.Save(cont);
           
        }
        public ActionResult ListContacts(bool? showInactive, Guid? CostCentre, int? page, string srcParam, string srch, string CostCentreID, string ContactOwner, int? itemsperpage)
        {
            ViewBag.contactOwnerList = AcceptedContactOwnerTypes();// _contactModelViewBuilder.ContactOwner();
            string command = srch;
            Guid ccId = Guid.Empty;
            int ccType = 0;
            if (ContactOwner != "")
            {
                int.TryParse(ContactOwner, out ccType);
            }
            if (CostCentreID != "")
            {
                Guid.TryParse(CostCentreID, out ccId);
                //ccId = Guid.Parse(CostCentreID.ToString());
            }
            try
            {
                if (itemsperpage != null)
                {
                    ViewModelBase.ItemsPerPage = itemsperpage.Value;
                }
                ViewBag.CostCentre = CostCentre;
                bool showinactive = false;
                if (showInactive != null)
                    showinactive = (bool)showInactive;

                ViewBag.showInactive = showinactive;

                if (TempData["msg"] != null)
                {
                    ViewBag.msg = TempData["msg"].ToString();
                    TempData["msg"] = null;
                }
                if (command == "Search")
                {
                    var ls = _contactModelViewBuilder.SearchContacts(srcParam, showinactive);
                    int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
                    return View(ls.ToPagedList(currentPageIndex, ViewModelBase.ItemsPerPage));
                }
                if (command == "Filter")
                {
                    var ls = _contactModelViewBuilder.GetContactsByCostCentre(ccId, showinactive);
                    string cont = GetCostCentreUser(ccType, ccId);
                    ViewBag.ContactsFor = GetCostCentreUser(ccType, ccId);
                    int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
                    return View(ls.ToPagedList(currentPageIndex, ViewModelBase.ItemsPerPage));
                }
                else
                    return RedirectToAction("ListContacts", new { srcParam = "", showinactive = showInactive, srch = "Search" });
            }
            catch (Exception ex)
            {
                ViewBag.msg = ex.Message;
                var errorContactList = new ContactViewModel();
                errorContactList.ErrorText = ex.Message;
                return View(errorContactList);
            }
        }