public async Task <ApiResult <IEnumerable <CustomerDto> > > SearchCustomerAsync(string customerName) { var customers = await(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 customertype in _context.CustomerTypes on c.CustomerTypeId equals customertype.Id into CustomerTypeGroup from ct in CustomerTypeGroup.DefaultIfEmpty() 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 c.Name.ToLower().Contains(customerName.ToLower()) && c.IsDelete == false select new CustomerDto() { Address = c.Address, Description = c.Description, Dob = c.Dob, Fax = c.Fax, Gender = c.Gender, Id = c.Id, Name = c.Name, Website = c.Website, EmployeeName = e.Name, IsDelete = c.IsDelete, UserName = au.UserName, PhoneNumber = c.PhoneNumber, CustomerTypeName = ct.Name, TotalDebt = customerDebt, Email = string.IsNullOrEmpty(c.Email)?au.Email:c.Email }).ToListAsync(); return(new ApiResult <IEnumerable <CustomerDto> >() { Code = HttpStatusCode.OK, ResultObj = customers }); }
public async Task <ApiResult <CustomerDto> > GetCustomerAsync(string customerId) { var totalAmountOrders = await _context.Orders.Where(x => x.CustomerId == customerId && x.TransactionStatusId != GlobalProperties.CancelTransactionId && x.TransactionStatusId != GlobalProperties.WaitingTransactionId).SumAsync(x => x.TotalAmount); var totalPaymentVouchers = await _context.PaymentVouchers.Where(x => x.CustomerId == customerId).SumAsync(x => x.Paid); var totalReceiptVouchers = await _context.ReceiptVouchers.Where(x => x.CustomerId == customerId).SumAsync(x => x.Received); var customer = await(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 customertype in _context.CustomerTypes on c.CustomerTypeId equals customertype.Id into CustomerTypeGroup from ct in CustomerTypeGroup.DefaultIfEmpty() where c.Id == customerId && c.IsDelete == false select new CustomerDto() { Address = c.Address, Description = c.Description, Dob = c.Dob, Fax = c.Fax, Gender = c.Gender, Id = c.Id, Name = c.Name, Website = c.Website, EmployeeName = e.Name, PhoneNumber = c.PhoneNumber, IsDelete = c.IsDelete, UserName = au.UserName, CustomerTypeName = ct.Name, TotalDebt = totalAmountOrders + totalPaymentVouchers - totalReceiptVouchers }).SingleOrDefaultAsync(); if (customer == null) { return(new ApiResult <CustomerDto>(HttpStatusCode.NotFound, $"Không tìm thấy khách hàng có mã: {customerId}")); } return(new ApiResult <CustomerDto>(HttpStatusCode.OK, customer)); }