public async Task <ApiResult <PagedResult <OrdersDto> > > GetOrdersPagingAsync(int pageIndex, int pageSize) { var orders = await(from o in _context.Orders join customer in _context.Customers on o.CustomerId equals customer.Id into CustomersGroup from c in CustomersGroup.DefaultIfEmpty() join employee in _context.Employees on o.EmployeeId equals employee.Id into EmployeesGroup from e in EmployeesGroup.DefaultIfEmpty() join ps in _context.PaymentStatuses on o.PaymentStatusId equals ps.Id join ts in _context.TransactionStatuses on o.TransactionStatusId equals ts.Id orderby o.DateCreated descending where o.TransactionStatusId != GlobalProperties.WaitingTransactionId let hasPaid = _context.ReceiptVouchers.Where(x => x.OrderId == o.Id).Sum(x => x.Received) select new OrdersDto() { OrderId = o.Id, CustomerName = string.IsNullOrEmpty(c.Name)?o.CustomerName:c.Name, EmployeeName = e.Name, DateCreated = o.DateCreated, PaymentStatusName = ps.Name, TransactionStatusName = ts.Name, CustomerHasPaid = o.TotalAmount }).GetPagedAsync(pageIndex, pageSize); return(new ApiResult <PagedResult <OrdersDto> >(HttpStatusCode.OK, orders)); }
public async Task <ApiResult <OrderDto> > GetOrderAsync(string orderId) { var hasPaid = _context.ReceiptVouchers.Where(x => x.OrderId == orderId).Sum(x => x.Received); var orderDetails = await(from o in _context.Orders join customer in _context.Customers on o.CustomerId equals customer.Id into CustomerGroup from c in CustomerGroup.DefaultIfEmpty() join ts in _context.TransactionStatuses on o.TransactionStatusId equals ts.Id join ps in _context.PaymentStatuses on o.PaymentStatusId equals ps.Id join branch in _context.Branches on o.BranchId equals branch.Id into BranchGroup from b in BranchGroup.DefaultIfEmpty() join employee in _context.Employees on o.EmployeeId equals employee.Id into EmployeesGroup from e in EmployeesGroup.DefaultIfEmpty() where o.Id == orderId select new OrderDto() { Id = o.Id, BranchName = b.Name, CustomerName = string.IsNullOrEmpty(c.Name)?o.CustomerName:c.Name, DateCreated = o.DateCreated, Description = o.Description, DiscountDescription = o.DiscountDescription, DiscountType = o.DiscountType, DiscountValue = o.DiscountValue, EmployeeName = e.Name, CustomerAddress = o.CustomerAddress, CustomerPhone = o.CustomerPhone, PaymentStatusName = ps.Name, TransactionStatusName = ts.Name, OrderDetailsDtos = (from od in _context.OrderDetails join product in _context.Products on od.ProductId equals product.Id into ProductsGroup from p in ProductsGroup.DefaultIfEmpty() where od.OrderId == o.Id select new OrderDetailsDto() { Id = od.Id, ProductId = od.ProductId, Quantity = od.Quantity, ServiceName = od.ServiceName, UnitPrice = od.UnitPrice, ProductName = p.Name }).ToList(), TotalAmount = o.TotalAmount, RestAmount = o.TotalAmount - hasPaid, CustomerId = c.Id }).SingleOrDefaultAsync(); if (orderDetails == null) { return(new ApiResult <OrderDto>(HttpStatusCode.NotFound, $"Không tìm thấy đơn hàng có mã: {orderId}")); } return(new ApiResult <OrderDto>(HttpStatusCode.OK, orderDetails)); }
public async Task <ApiResult <SupplierDto> > GetSupplierAsync(string supplierId) { var checkSupplier = await _context.Suppliers.FindAsync(supplierId); if (checkSupplier == null) { return(new ApiResult <SupplierDto>(HttpStatusCode.NotFound, $"Không tìm thấy nhà cung cấp có mã: {supplierId}")); } var totalPurchaseOrder = await(from po in _context.PurchaseOrders where po.SupplierId == supplierId && po.TransactionStatusId != GlobalProperties.CancelTransactionId select po.TotalAmount).SumAsync(); var totalReceiptVoucher = await(from rv in _context.ReceiptVouchers where rv.SupplierId == supplierId select rv.Received).SumAsync(); var totalPaymentVoucher = await(from pv in _context.PaymentVouchers where pv.SupplierId == supplierId select pv.Paid).SumAsync(); var supplier = await(from s in _context.Suppliers join employee in _context.Employees on s.EmployeeId equals employee.Id into EmployeesGroup from e in EmployeesGroup.DefaultIfEmpty() where s.Id == supplierId && s.IsDelete == false let totalDebt = (from po in _context.PurchaseOrders join pod in _context.PurchaseOrderDetails on po.Id equals pod.PurchaseOrderId where po.SupplierId == s.Id select pod.UnitPrice * pod.Quantity).Sum() - (from po in _context.PurchaseOrders join pv in _context.PaymentVouchers on po.Id equals pv.PurchaseOrderId where po.SupplierId == s.Id select pv.Paid).Sum() + (from rv in _context.ReceiptVouchers where rv.SupplierId == supplierId select rv.Received).Sum() select new SupplierDto() { Id = s.Id, Address = s.Address, Description = s.Description, Email = s.Email, Fax = s.Fax, Name = s.Name, Website = s.Website, EmployeeName = e.Name, PhoneNumber = s.PhoneNumber, TotalDebt = totalPurchaseOrder + totalReceiptVoucher - totalPaymentVoucher }).SingleOrDefaultAsync(); return(new ApiResult <SupplierDto>(HttpStatusCode.OK, supplier)); }