Example #1
0
        /// <summary>
        /// Private method which consumes data from the database via the ServiceLayer and processes it into the format required for the home page
        /// </summary>
        /// <returns>Dictionary containing the customerId as the key, and a CustomerViewModel object as the value</returns>
        private Dictionary <int, CustomerViewModel> GetCustomers()
        {
            var customers = CustomerAccessMethods.GetAllCustomers();
            var invoices  = InvoiceAccessMethods.GetAllInvoices();

            var customerDataList = new Dictionary <int, CustomerViewModel>();

            Parallel.ForEach(customers, (customer) =>
            {
                var customerToAdd = new CustomerViewModel
                {
                    Name = customer.Value.Name
                };

                var customerInvoices = GetInvoicesForCustomer(customer.Key, invoices);

                var mostRecentInvoice = GetMostRecentInvoiceForCustomer(customerInvoices);
                customerToAdd.MostRecentInvoiceRef          = mostRecentInvoice.InvoiceId;
                customerToAdd.MostRecentInvoiceAmount       = mostRecentInvoice.Value;
                customerToAdd.NumberOfOutstandingInvoices   = GetNumberOfOutstandingInvoicesForCustomer(customerInvoices);
                customerToAdd.TotalOfAllOutstandingInvoices = GetAmountOwedOnOutstandingInvoicesForCustomer(customerInvoices);
                customerToAdd.TotalOfAllPaidInvoices        = GetAmountPaidOnInvoices(customerInvoices);

                lock (customerDataList)
                {
                    customerDataList.Add(customer.Key, customerToAdd);
                }
            });

            return(customerDataList);
        }