/// <summary> /// Get the Customers who have the highest average invoices /// </summary> /// <returns>List of customers if succeeded otherwise null</returns> public List <ProfitableCustomerDTO> GetMostProfitableCustomers() { List <Customer> customers; List <ProfitableCustomerDTO> profitableCustomersDTO = null; try { customers = _unitOfWork.Customer.GetMostProfitableCustomers(); } catch (Exception e) { _logger.Log($"{Messages.messageFor[MessageType.GeneralDbFaild]} Execption details: {e.Message}"); throw new FaildToConnectDbExeption(Messages.messageFor[MessageType.GeneralDbFaild]); } if (customers != null) { profitableCustomersDTO = new List <ProfitableCustomerDTO>(); foreach (var customer in customers) { ProfitableCustomerDTO customerDTO = CreateProfitableCustomerModel(customer); profitableCustomersDTO.Add(customerDTO); } } return(profitableCustomersDTO); }
/// <summary> /// Create profitable customer model /// </summary> /// <param name="customer">Customer model</param> /// <returns>ProfitableCustomerDTO model</returns> private ProfitableCustomerDTO CreateProfitableCustomerModel(Customer customer) { ProfitableCustomerDTO customerDTO = new ProfitableCustomerDTO(); customerDTO.FirstName = customer.FirstName; customerDTO.LastName = customer.LastName; customerDTO.IdentityCard = customer.IdentityCard; customerDTO.LastMonthProfit = customer.Lines.Sum(p => p.Payments.Where(x => x.Date.Year == DateTime.Now.Year && x.Date.Month == DateTime.Now.Month).Sum(q => q.LineTotalPrice)); return(customerDTO); }