private async Task <WrapperListCustomerVM> SetHistoryOverview(WrapperListCustomerVM vm, string FactoryId)
        {
            var data = new GetDataListHistory();

            //Task<WrapperCustomerHistory>[] listOftask = new Task<WrapperCustomerHistory>[vm.ListOfData.Count];
            WrapperCustomerHistory[] listOftask = new WrapperCustomerHistory[vm.ListOfData.Count];
            for (int i = 0; i < vm.ListOfData.Count; i++)
            {
                CustomerVM temp = vm.ListOfData.ElementAt(i);
                data.ClientId   = temp.CustomerId;
                data.PageNumber = -1;
                data.PageSize   = -1;
                data.FactoryId  = FactoryId;
                listOftask[i]   = await GetCustomerHistory(data);

                int len = listOftask[i].ListOfData.Count - 1;
                vm.ListOfData.ElementAt(i).PaidAmount       = listOftask[i].ListOfData[len].PaidAmount;
                vm.ListOfData.ElementAt(i).RecievableAmount = listOftask[i].ListOfData[len].RecievableAmount;
                vm.ListOfData.ElementAt(i).RecievedAmount   = listOftask[i].ListOfData[len].RecievedAmount;
                vm.ListOfData.ElementAt(i).PayableAmount    = listOftask[i].ListOfData[len].PayableAmount;
            }

            //            await Task.WhenAll(listOftask);

            //for (int i = 0; i < vm.ListOfData.Count; i++)
            //{
            //    //CustomerHistory te = GetCustomerHistoryOverview(listOftask[i].Result);
            //    CustomerHistory te = GetCustomerHistoryOverview(listOftask[i]);
            //    vm.ListOfData.ElementAt(i).PaidAmount = te.PaidAmount;
            //    vm.ListOfData.ElementAt(i).RecievableAmount = te.RecievableAmount;
            //    vm.ListOfData.ElementAt(i).RecievedAmount = te.RecievedAmount;
            //    vm.ListOfData.ElementAt(i).PayableAmount = te.PayableAmount;
            //}
            return(vm);
        }
        public CustomerHistory GetCustomerHistoryOverview(WrapperCustomerHistory list)
        {
            CustomerHistory history = new CustomerHistory();

            for (int i = 0; i < list.ListOfData.Count; i++)
            {
                CustomerHistory temp = list.ListOfData.ElementAt(i);
                if (temp.Type == "InvoiceItem")
                {
                    //  history.RecievableAmount += temp.RecievableAmount;
                    //  history.RecievedAmount += temp.RecievedAmount;
                }
                else if (temp.Type == "SaleItem")
                {
                }
                else if (temp.Type == "IncomeItem")
                {
                    history.RecievedAmount += temp.RecievedAmount;
                }
                else if (temp.Type == "ExpenseItem")
                {
                    history.PaidAmount += temp.PaidAmount;
                }
                else if (temp.Type == "PayableItem")
                {
                    history.PayableAmount += temp.PayableAmount;
                }
                else if (temp.Type == "RecievableItem")
                {
                    history.RecievableAmount += temp.RecievableAmount;
                }
            }
            return(history);
        }
        public async Task <WrapperCustomerHistory> GetCustomerHistory(GetDataListHistory customerVM)
        {
            WrapperCustomerHistory custHist       = new WrapperCustomerHistory();
            WrapperCustomerHistory returnCustHist = new WrapperCustomerHistory();
            // Sales -- Sales Invoice Generated
            // Invoice
            // Income -- Payment Invoice Generated
            Task <List <Sales> > listSalesT =
                _repositoryWrapper
                .Sales
                .FindAll()
                .Where(x => x.FactoryId == customerVM.FactoryId &&
                       x.ClientId == customerVM.ClientId)
                .Include(x => x.Item)
                .ThenInclude(x => x.ItemCategory)
                .ToListAsync();

            Task <List <Invoice> > listInvoiceT =
                _repositoryWrapper
                .Invoice
                .FindAll()
                .Where(x => x.FactoryId == customerVM.FactoryId &&
                       x.ClientId == customerVM.ClientId)
                .Include(x => x.InvoiceType)
                .Where(x => x.InvoiceType.Name == TypeInvoice.Sales.ToString())
                .ToListAsync();

            Task <List <Income> > listIncomeT =
                _repositoryWrapper
                .Income
                .FindAll()
                .Where(x => x.FactoryId == customerVM.FactoryId &&
                       x.ClientId == customerVM.ClientId)
                .Include(x => x.IncomeType)
                //.Where(x => x.IncomeType.Name == TypeIncome.ClientPaymentRecieved.ToString())
                .ToListAsync();


            Task <List <Expense> > listExpenseT =
                _repositoryWrapper
                .Expense
                .FindAll()
                .Where(x => x.FactoryId == customerVM.FactoryId &&
                       x.ClientId == customerVM.ClientId)
                .Include(x => x.ExpenseType)
                //.Where(x => x.IncomeType.Name == TypeIncome.ClientPaymentRecieved.ToString())
                .ToListAsync();


            Task <List <Payable> > listPayableT =
                _repositoryWrapper
                .Payable
                .FindAll()
                .Where(x => x.FactoryId == customerVM.FactoryId &&
                       x.ClientId == customerVM.ClientId)
                .ToListAsync();

            Task <List <Recievable> > listRecievableT =
                _repositoryWrapper
                .Recievable
                .FindAll()
                .Where(x => x.FactoryId == customerVM.FactoryId &&
                       x.ClientId == customerVM.ClientId)
                .ToListAsync();



            await Task.WhenAll(listSalesT, listInvoiceT, listIncomeT, listExpenseT, listPayableT, listRecievableT);

            List <CustomerHistory> custHistInvoice = _utilService.Mapper.Map <List <Invoice>, List <CustomerHistory> >(listInvoiceT.Result.ToList());
            List <CustomerHistory> custHistIncome  = _utilService.Mapper.Map <List <Income>, List <CustomerHistory> >(listIncomeT.Result.ToList());
            List <CustomerHistory> custHistSales   = _utilService.Mapper.Map <List <Sales>, List <CustomerHistory> >(listSalesT.Result.ToList());
            List <CustomerHistory> custHistExpense = _utilService.Mapper.Map <List <Expense>, List <CustomerHistory> >(listExpenseT.Result.ToList());


            List <CustomerHistory> custHistPayable    = _utilService.Mapper.Map <List <Payable>, List <CustomerHistory> >(listPayableT.Result.ToList());
            List <CustomerHistory> custHistRecievable = _utilService.Mapper.Map <List <Recievable>, List <CustomerHistory> >(listRecievableT.Result.ToList());



            custHist.ListOfData.AddRange(custHistInvoice);
            custHist.ListOfData.AddRange(custHistIncome);
            custHist.ListOfData.AddRange(custHistExpense);
            custHist.ListOfData.AddRange(custHistPayable);
            custHist.ListOfData.AddRange(custHistRecievable);
            custHist.ListOfData = custHist.ListOfData.OrderBy(x => x.OccurranceDate).ToList();
            for (int i = 0; i < custHist.ListOfData.Count(); i++)
            {
                IEnumerable <CustomerHistory> tempList = new List <CustomerHistory>();
                if (custHist.ListOfData.ElementAt(i).Type == "InvoiceItem")
                {
                    tempList = custHistSales.Where(x => x.InvoiceId == custHist.ListOfData.ElementAt(i).InvoiceId);
                    returnCustHist.ListOfData.AddRange(tempList);
                    returnCustHist.ListOfData.Add(custHist.ListOfData.ElementAt(i));
                }
                else
                {
                    returnCustHist.ListOfData.Add(custHist.ListOfData.ElementAt(i));
                }
            }
            var staffHist = GetCustomerHistoryOverview(returnCustHist);


            if (customerVM.PageSize != -1)
            {
                returnCustHist.ListOfData = returnCustHist.ListOfData
                                            .Skip((customerVM.PageNumber - 1) * customerVM.PageSize)
                                            .Take(customerVM.PageSize)
                                            .ToList();
            }

            returnCustHist.ListOfData.Add(staffHist);
            return(returnCustHist);
        }