public new static ContactWrapper GetSample()
 {
     return(new PersonWrapper(0)
     {
         IsPrivate = true,
         IsShared = false,
         IsCompany = false,
         FirstName = "Tadjeddine",
         LastName = "Bachir",
         Company = CompanyWrapper.GetSample(),
         Title = "Programmer",
         About = "",
         Created = ApiDateTime.GetSample(),
         CreateBy = EmployeeWraper.GetSample(),
         CommonData = new List <ContactInfoWrapper>()
         {
             ContactInfoWrapper.GetSample()
         },
         CustomFields = new List <CustomFieldBaseWrapper>()
         {
             CustomFieldBaseWrapper.GetSample()
         },
         ShareType = ShareType.None,
         CanDelete = true,
         CanEdit = true,
         TaskCount = 0,
         HaveLateTasks = false
     });
 }
        public static CompanyWrapper ToCompanyWrapperQuick(Company company)
        {
            var result = new CompanyWrapper(company.ID);

            result.DisplayName = company.GetTitle();
            result.IsPrivate   = CRMSecurity.IsPrivate(company);
            result.IsShared    = company.ShareType == ShareType.ReadWrite || company.ShareType == ShareType.Read;
            result.ShareType   = company.ShareType;

            if (result.IsPrivate)
            {
                result.AccessList = CRMSecurity.GetAccessSubjectTo(company)
                                    .Select(item => EmployeeWraper.Get(item.Key));
            }
            result.Currency = !String.IsNullOrEmpty(company.Currency) ?
                              new CurrencyInfoWrapper(CurrencyProvider.Get(company.Currency)) :
                              null;

            result.SmallFotoUrl  = String.Format("{0}HttpHandlers/filehandler.ashx?action=contactphotoulr&cid={1}&isc={2}&ps=1", PathProvider.BaseAbsolutePath, company.ID, true);
            result.MediumFotoUrl = String.Format("{0}HttpHandlers/filehandler.ashx?action=contactphotoulr&cid={1}&isc={2}&ps=2", PathProvider.BaseAbsolutePath, company.ID, true);
            result.IsCompany     = true;
            result.CanEdit       = CRMSecurity.CanEdit(company);
            //result.CanDelete = CRMSecurity.CanDelete(contact);


            result.CompanyName = company.CompanyName;

            result.CreateBy = EmployeeWraper.Get(company.CreateBy);
            result.Created  = (ApiDateTime)company.CreateOn;
            result.About    = company.About;
            result.Industry = company.Industry;

            return(result);
        }
 public new static PersonWrapper GetSample()
 {
     return(new PersonWrapper(0)
     {
         IsPrivate = false,
         IsCompany = false,
         FirstName = "Tadjeddine",
         LastName = "Bachir",
         Company = CompanyWrapper.GetSample(),
         Title = "Programmer",
         About = "",
         Created = (ApiDateTime)DateTime.UtcNow,
         CreateBy = EmployeeWraper.GetSample()
     });
 }
 public new static PersonWrapper GetSample()
 {
     return(new PersonWrapper(0)
     {
         IsPrivate = true,
         IsShared = false,
         IsCompany = false,
         FirstName = "Tadjeddine",
         LastName = "Bachir",
         Company = CompanyWrapper.GetSample(),
         Title = "Programmer",
         About = "",
         Created = ApiDateTime.GetSample(),
         CreateBy = EmployeeWraper.GetSample(),
         ShareType = ShareType.None
     });
 }
        private ContactWrapper ToContactWrapper(Contact contact)
        {
            ContactWrapper result;

            var person = contact as Person;
            if (person != null)
            {
                var peopleWrapper = new PersonWrapper(person);
                if (person.CompanyID > 0)
                {
                    peopleWrapper.Company = ToContactBaseWrapper(DaoFactory.GetContactDao().GetByID(person.CompanyID));
                }

                result = peopleWrapper;
            }
            else
            {
                var company = contact as Company;
                if (company != null)
                {
                    result = new CompanyWrapper(company);
                    ((CompanyWrapper)result).PersonsCount = DaoFactory.GetContactDao().GetMembersCount(result.ID);
                }
                else throw new ArgumentException();
            }

            if (contact.StatusID > 0)
            {
                var listItem = DaoFactory.GetListItemDao().GetByID(contact.StatusID);
                if (listItem == null) throw new ItemNotFoundException();

                result.ContactStatus = new ContactStatusBaseWrapper(listItem);
            }

            result.TaskCount = DaoFactory.GetTaskDao().GetTasksCount(contact.ID);
            result.HaveLateTasks = DaoFactory.GetTaskDao().HaveLateTask(contact.ID);

            var contactInfos = new List<ContactInfoWrapper>();
            var addresses = new List<Address>();

            var data = DaoFactory.GetContactInfoDao().GetList(contact.ID, null, null, null);

            foreach (var contactInfo in data)
            {
                if (contactInfo.InfoType == ContactInfoType.Address)
                {
                    addresses.Add(new Address(contactInfo));
                }
                else
                {
                    contactInfos.Add(new ContactInfoWrapper(contactInfo));
                }
            }

            result.Addresses = addresses;
            result.CommonData = contactInfos;

            if (contact is Person)
            {
                result.CustomFields = DaoFactory.GetCustomFieldDao()
                                                .GetEnityFields(EntityType.Person, contact.ID, false)
                                                .ConvertAll(item => new CustomFieldBaseWrapper(item)).ToSmartList();
            }
            else
            {
                result.CustomFields = DaoFactory.GetCustomFieldDao()
                                                .GetEnityFields(EntityType.Company, contact.ID, false)
                                                .ConvertAll(item => new CustomFieldBaseWrapper(item)).ToSmartList();
            }

            return result;
        }
        private IEnumerable<ContactWrapper> ToListContactWrapper(List<Contact> itemList)
        {

            if (itemList.Count == 0) return new List<ContactWrapper>();

            var result = new List<ContactWrapper>();

            var personsIDs = new List<int>();
            var companyIDs = new List<int>();
            var contactIDs = new int[itemList.Count];

            var peopleCompanyIDs = new List<int>();

            var peopleCompanyList = new Dictionary<int, ContactBaseWrapper>();

            for (int index = 0; index < itemList.Count; index++)
            {
                var contact = itemList[index];

                if (contact is Company)
                    companyIDs.Add(contact.ID);
                else if (contact is Person)
                {
                    var person = (Person)contact;

                    personsIDs.Add(contact.ID);

                    if (person.CompanyID > 0)
                        peopleCompanyIDs.Add(person.CompanyID);
                }

                contactIDs[index] = itemList[index].ID;
            }

            if (peopleCompanyIDs.Count > 0)
                peopleCompanyList = DaoFactory.GetContactDao().GetContacts(peopleCompanyIDs.ToArray())
                                   .ToDictionary(item => item.ID,
                                                         ToContactBaseWrapper);

            var companiesMembersCount = DaoFactory.GetContactDao().GetMembersCount(companyIDs.Distinct().ToArray());

            var contactStatusIDs = itemList.Select(item => item.StatusID).Distinct().ToArray();
            var contactInfos = new Dictionary<int, List<ContactInfoWrapper>>();

            var haveLateTask = DaoFactory.GetTaskDao().HaveLateTask(contactIDs);
            var contactStatus = DaoFactory.GetListItemDao().GetItems(contactStatusIDs).ToDictionary(item => item.ID,
                                                                                    item => new ContactStatusBaseWrapper(item));

            var personsCustomFields = DaoFactory.GetCustomFieldDao().GetEnityFields(EntityType.Person,
                                                                                    personsIDs.ToArray());

            var companyCustomFields = DaoFactory.GetCustomFieldDao().GetEnityFields(EntityType.Company,
                                                                                    companyIDs.ToArray());


            var customFields = personsCustomFields.Union(companyCustomFields).GroupBy(item => item.EntityID).ToDictionary(
                           item => item.Key, item => item.Select(ToCustomFieldBaseWrapper));


            var addresses = new Dictionary<int, List<Address>>();
            var taskCount = DaoFactory.GetTaskDao().GetTasksCount(contactIDs);

            var contactTags = DaoFactory.GetTagDao().GetEntitiesTags(EntityType.Contact);

            DaoFactory.GetContactInfoDao().GetAll(contactIDs).ForEach(
                 item =>
                 {
                     if (item.InfoType == ContactInfoType.Address)
                     {
                         if (!addresses.ContainsKey(item.ContactID))
                             addresses.Add(item.ContactID, new List<Address>
                                                               {
                                                                   new Address(item)
                                                               });
                         else
                             addresses[item.ContactID].Add(new Address(item));
                     }
                     else
                     {
                         if (!contactInfos.ContainsKey(item.ContactID))
                             contactInfos.Add(item.ContactID, new List<ContactInfoWrapper> { new ContactInfoWrapper(item) });
                         else
                             contactInfos[item.ContactID].Add(new ContactInfoWrapper(item));
                     }
                 }
            );

            foreach (var contact in itemList)
            {

                ContactWrapper contactWrapper;

                if (contact is Person)
                {
                    var people = (Person)contact;

                    var peopleWrapper = new PersonWrapper(people);

                    if (people.CompanyID > 0 && peopleCompanyList.ContainsKey(people.CompanyID))
                    {
                        peopleWrapper.Company = peopleCompanyList[people.CompanyID];
                    }

                    contactWrapper = peopleWrapper;

                }
                else if (contact is Company)
                {
                    contactWrapper = new CompanyWrapper((Company)contact);

                    if (companiesMembersCount.ContainsKey(contactWrapper.ID))
                        ((CompanyWrapper)contactWrapper).PersonsCount = companiesMembersCount[contactWrapper.ID];
                }
                else
                    throw new ArgumentException();

                if (contactTags.ContainsKey(contact.ID))
                    contactWrapper.Tags = contactTags[contact.ID];

                if (addresses.ContainsKey(contact.ID))
                    contactWrapper.Addresses = addresses[contact.ID];

                contactWrapper.CommonData = contactInfos.ContainsKey(contact.ID) ? contactInfos[contact.ID] : new List<ContactInfoWrapper>();

                if (contactStatus.ContainsKey(contact.StatusID))
                    contactWrapper.ContactStatus = contactStatus[contact.StatusID];

                contactWrapper.HaveLateTasks = haveLateTask.ContainsKey(contact.ID) && haveLateTask[contact.ID];

                contactWrapper.CustomFields = customFields.ContainsKey(contact.ID) ? customFields[contact.ID] : new List<CustomFieldBaseWrapper>();

                contactWrapper.TaskCount = taskCount.ContainsKey(contact.ID) ? taskCount[contact.ID] : 0;

                result.Add(contactWrapper);
            }

            return result;

        }
        private IEnumerable<ContactWithTaskWrapper> ToSimpleListContactWrapper(IReadOnlyList<Contact> itemList)
        {
            if (itemList.Count == 0) return new List<ContactWithTaskWrapper>();

            var result = new List<ContactWithTaskWrapper>();

            var personsIDs = new List<int>();
            var companyIDs = new List<int>();
            var contactIDs = new int[itemList.Count];

            var peopleCompanyIDs = new List<int>();

            var peopleCompanyList = new Dictionary<int, ContactBaseWrapper>();

            for (int index = 0; index < itemList.Count; index++)
            {
                var contact = itemList[index];

                if (contact is Company)
                    companyIDs.Add(contact.ID);
                else if (contact is Person)
                {
                    var person = (Person)contact;

                    personsIDs.Add(contact.ID);

                    if (person.CompanyID > 0)
                        peopleCompanyIDs.Add(person.CompanyID);
                }

                contactIDs[index] = itemList[index].ID;
            }

            if (peopleCompanyIDs.Count > 0)
                peopleCompanyList = DaoFactory.GetContactDao().GetContacts(peopleCompanyIDs.ToArray())
                                   .ToDictionary(item => item.ID,
                                                         ToContactBaseWrapper);


            var contactInfos = new Dictionary<int, List<ContactInfoWrapper>>();

            var addresses = new Dictionary<int, List<Address>>();

            DaoFactory.GetContactInfoDao().GetAll(contactIDs).ForEach(
                item =>
                {
                    if (item.InfoType == ContactInfoType.Address)
                    {
                        if (!addresses.ContainsKey(item.ContactID))
                            addresses.Add(item.ContactID, new List<Address>
                                                               {
                                                                   new Address(item)
                                                               });
                        else
                            addresses[item.ContactID].Add(new Address(item));
                    }
                    else
                    {
                        if (!contactInfos.ContainsKey(item.ContactID))
                            contactInfos.Add(item.ContactID, new List<ContactInfoWrapper> { new ContactInfoWrapper(item) });
                        else
                            contactInfos[item.ContactID].Add(new ContactInfoWrapper(item));
                    }
                }
           );

            var nearestTasks = DaoFactory.GetTaskDao().GetNearestTask(contactIDs.ToArray());

            IEnumerable<TaskCategoryBaseWrapper> taskCategories = new List<TaskCategoryBaseWrapper>();

            if (nearestTasks.Any())
            {
                taskCategories = DaoFactory.GetListItemDao().GetItems(ListType.TaskCategory).ConvertAll(item => new TaskCategoryBaseWrapper(item));
            }

            foreach (var contact in itemList)
            {
                ContactWrapper contactWrapper;

                if (contact is Person)
                {
                    var people = (Person)contact;

                    var peopleWrapper = new PersonWrapper(people);

                    if (people.CompanyID > 0 && peopleCompanyList.ContainsKey(people.CompanyID))
                    {
                        peopleWrapper.Company = peopleCompanyList[people.CompanyID];
                    }

                    contactWrapper = peopleWrapper;
                }
                else if (contact is Company)
                {
                    contactWrapper = new CompanyWrapper((Company)contact);
                }
                else
                {
                    throw new ArgumentException();
                }


                contactWrapper.CommonData = contactInfos.ContainsKey(contact.ID) ? contactInfos[contact.ID] : new List<ContactInfoWrapper>();

                TaskBaseWrapper taskWrapper = null;

                if (nearestTasks.ContainsKey(contactWrapper.ID))
                {
                    var task = nearestTasks[contactWrapper.ID];
                    taskWrapper = new TaskBaseWrapper(task);

                    if (task.CategoryID > 0)
                        taskWrapper.Category = taskCategories.First(x => x.ID == task.CategoryID);
                }

                result.Add(new ContactWithTaskWrapper
                    {
                        Contact = contactWrapper,
                        Task = taskWrapper
                    });
            }

            return result;
        }
        private ContactWrapper ToContactWrapper(Contact contact)
        {
            ContactWrapper result;

            if (contact is Person)
            {
                var people = (Person)contact;

                var peopleWrapper = new PersonWrapper(people);

                if (people.CompanyID > 0)
                    peopleWrapper.Company = ToContactBaseWrapper(DaoFactory.GetContactDao().GetByID(people.CompanyID));

                result = peopleWrapper;

            }
            else if (contact is Company)
                result = new CompanyWrapper((Company)contact);
            else
                throw new ArgumentException();

            if (contact.StatusID > 0)
                result.ContactType = GetContactTypeByID(contact.StatusID);

            result.TaskCount = DaoFactory.GetTaskDao().GetTasksCount(contact.ID);
            result.HaveLateTasks = DaoFactory.GetTaskDao().HaveLateTask(contact.ID);

            var contactInfos = new List<ContactInfoWrapper>();
            var addresses = new List<Address>();

            var data = DaoFactory.GetContactInfoDao().GetList(contact.ID, null, null, null);

            foreach (var contactInfo in data)
                if (contactInfo.InfoType == ContactInfoType.Address)
                    addresses.Add(new Address(contactInfo));
                else
                    contactInfos.Add(new ContactInfoWrapper(contactInfo));

            result.Addresses = addresses;
            result.CommonData = contactInfos;

            if (contact is Person)
                result.CustomFields = DaoFactory.GetCustomFieldDao().GetEnityFields(EntityType.Person, contact.ID, false).ConvertAll(item => new CustomFieldWrapper(item)).ToSmartList();
            else
                result.CustomFields = DaoFactory.GetCustomFieldDao().GetEnityFields(EntityType.Company, contact.ID, false).ConvertAll(item => new CustomFieldWrapper(item)).ToSmartList();

            return result;
        }
        public static CompanyWrapper ToCompanyWrapperQuick(Company company)
        {
            var result = new CompanyWrapper(company.ID);

            result.DisplayName = company.GetTitle();
            result.IsPrivate = CRMSecurity.IsPrivate(company);
            result.IsShared = company.ShareType == ShareType.ReadWrite || company.ShareType == ShareType.Read;
            result.ShareType = company.ShareType;

            if (result.IsPrivate)
            {
                result.AccessList = CRMSecurity.GetAccessSubjectTo(company)
                                        .Select(item => EmployeeWraper.Get(item.Key));
            }
            result.Currency = !String.IsNullOrEmpty(company.Currency) ?
                new CurrencyInfoWrapper(CurrencyProvider.Get(company.Currency)) :
                null;

            result.SmallFotoUrl = String.Format("{0}HttpHandlers/filehandler.ashx?action=contactphotoulr&cid={1}&isc={2}&ps=1", PathProvider.BaseAbsolutePath, company.ID, true).ToLower();
            result.MediumFotoUrl = String.Format("{0}HttpHandlers/filehandler.ashx?action=contactphotoulr&cid={1}&isc={2}&ps=2", PathProvider.BaseAbsolutePath, company.ID, true).ToLower();
            result.IsCompany = true;
            result.CanEdit = CRMSecurity.CanEdit(company);
            //result.CanDelete = CRMSecurity.CanDelete(contact);


            result.CompanyName = company.CompanyName;

            result.CreateBy = EmployeeWraper.Get(company.CreateBy);
            result.Created = (ApiDateTime)company.CreateOn;
            result.About = company.About;
            result.Industry = company.Industry;

            return result;
        }