public async Task <ApiResult <ReportCustomerSupplierDebtDto> > GetAllCustomerDebtAsync(int pageIndex, int pageSize) { var customers = from c in _context.Customers join employee in _context.Employees on c.EmployeeId equals employee.Id into EmployeeGroup from e in EmployeeGroup.DefaultIfEmpty() join appuser in _context.AppUsers on c.AppUserId equals appuser.Id into AppUserGroup from au in AppUserGroup.DefaultIfEmpty() join ct in _context.CustomerTypes on c.CustomerTypeId equals ct.Id let customerDebt = (_context.Orders.Where(x => x.CustomerId == c.Id && x.TransactionStatusId != GlobalProperties.CancelTransactionId && x.TransactionStatusId != GlobalProperties.WaitingTransactionId) .Sum(x => (decimal?)x.TotalAmount) ?? 0) + (_context.PaymentVouchers.Where(x => x.CustomerId == c.Id) .Sum(x => (decimal?)x.Paid) ?? 0) - (_context.ReceiptVouchers.Where(x => x.CustomerId == c.Id) .Sum(x => (decimal?)x.Received) ?? 0) where (int)customerDebt != 0 select new CustomersSuppliersForReportDto() { Id = c.Id, Name = c.Name, PhoneNumber = c.PhoneNumber, TotalDebt = customerDebt, Address = c.Address, Email = au.Email, }; decimal totalDebt = 0; foreach (var c in await customers.ToListAsync()) { totalDebt += c.TotalDebt; } var reportCustomer = new ReportCustomerSupplierDebtDto() { Targets = await customers.GetPagedAsync(pageIndex, pageSize), TotalDebt = totalDebt }; return(new ApiResult <ReportCustomerSupplierDebtDto>(HttpStatusCode.OK, reportCustomer)); }
public async Task <ApiResult <ReportCustomerSupplierDebtDto> > GetAllSupplierDebtAsync(int pageIndex, int pageSize) { var suppliers = await Task.Run(() => (from s in _context.Suppliers join e in _context.Employees on s.EmployeeId equals e.Id let totalDebt = (_context.PurchaseOrders.Where(x => x.SupplierId == s.Id && x.TransactionStatusId != GlobalProperties.CancelTransactionId) .Sum(x => (decimal?)x.TotalAmount) ?? 0) - (_context.PaymentVouchers.Where(x => x.SupplierId == s.Id) .Sum(x => (decimal?)x.Paid) ?? 0) + (_context.ReceiptVouchers.Where(x => x.SupplierId == s.Id) .Sum(x => (decimal?)x.Received) ?? 0) where (int)totalDebt != 0 select new CustomersSuppliersForReportDto() { Id = s.Id, Name = s.Name, PhoneNumber = s.PhoneNumber, TotalDebt = totalDebt, Address = s.Address, Email = s.Email })); decimal totalDebt = 0; foreach (var s in await suppliers.ToListAsync()) { totalDebt += s.TotalDebt; } var reportSupplier = new ReportCustomerSupplierDebtDto() { Targets = await suppliers.GetPagedAsync(pageIndex, pageSize), TotalDebt = totalDebt }; return(new ApiResult <ReportCustomerSupplierDebtDto>(HttpStatusCode.OK, reportSupplier)); }