|
| 1 | +using Grand.Business.Core.Interfaces.Checkout.Orders; |
| 2 | +using Grand.Business.Core.Interfaces.Customers; |
| 3 | +using Grand.Business.Core.Utilities.Checkout; |
| 4 | +using Grand.Data; |
| 5 | +using Grand.Domain.Customers; |
| 6 | +using Grand.Domain.Orders; |
| 7 | +using Grand.Domain.Payments; |
| 8 | + |
| 9 | +namespace Grand.Business.Checkout.Services.Orders; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Company credit service |
| 13 | +/// </summary> |
| 14 | +public class CompanyCreditService : ICompanyCreditService |
| 15 | +{ |
| 16 | + #region Ctor |
| 17 | + |
| 18 | + public CompanyCreditService(IRepository<Order> orderRepository, ICustomerService customerService) |
| 19 | + { |
| 20 | + _orderRepository = orderRepository; |
| 21 | + _customerService = customerService; |
| 22 | + } |
| 23 | + |
| 24 | + #endregion |
| 25 | + |
| 26 | + #region Fields |
| 27 | + |
| 28 | + private readonly IRepository<Order> _orderRepository; |
| 29 | + private readonly ICustomerService _customerService; |
| 30 | + |
| 31 | + #endregion |
| 32 | + |
| 33 | + #region Utilities |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Gets the company's orders bought on credit that are not paid yet |
| 37 | + /// </summary> |
| 38 | + private IQueryable<Order> UnpaidCreditOrders(string companyOwnerId) |
| 39 | + { |
| 40 | + return _orderRepository.Table.Where(o => |
| 41 | + o.OwnerId == companyOwnerId && |
| 42 | + o.PaymentDueDateUtc != null && |
| 43 | + !o.Deleted && |
| 44 | + o.OrderStatusId != (int)OrderStatusSystem.Cancelled && |
| 45 | + (o.PaymentStatusId == PaymentStatus.Pending || |
| 46 | + o.PaymentStatusId == PaymentStatus.Authorized || |
| 47 | + o.PaymentStatusId == PaymentStatus.PartiallyPaid)); |
| 48 | + } |
| 49 | + |
| 50 | + #endregion |
| 51 | + |
| 52 | + #region Methods |
| 53 | + |
| 54 | + public virtual async Task<CompanyCredit> GetCompanyCredit(Customer customer) |
| 55 | + { |
| 56 | + ArgumentNullException.ThrowIfNull(customer); |
| 57 | + |
| 58 | + var companyOwnerId = OrderApprovalRules.CompanyId(customer); |
| 59 | + var owner = string.IsNullOrEmpty(customer.OwnerId) |
| 60 | + ? customer |
| 61 | + : await _customerService.GetCustomerById(customer.OwnerId); |
| 62 | + |
| 63 | + if (owner is not { Active: true, Deleted: false } || |
| 64 | + !owner.CreditLimit.HasValue || !owner.PaymentTermDays.HasValue) |
| 65 | + return CompanyCredit.None(companyOwnerId); |
| 66 | + |
| 67 | + var outstanding = await GetOutstandingAmount(companyOwnerId); |
| 68 | + var hasOverdue = await HasOverdue(companyOwnerId, DateTime.UtcNow); |
| 69 | + |
| 70 | + return new CompanyCredit(companyOwnerId, owner.CreditLimit, owner.PaymentTermDays, outstanding, |
| 71 | + hasOverdue); |
| 72 | + } |
| 73 | + |
| 74 | + public virtual async Task<double> GetOutstandingAmount(string companyOwnerId) |
| 75 | + { |
| 76 | + if (string.IsNullOrEmpty(companyOwnerId)) |
| 77 | + return 0; |
| 78 | + |
| 79 | + var query = UnpaidCreditOrders(companyOwnerId) |
| 80 | + .Select(o => new { o.OrderTotal, o.PaidAmount, o.CurrencyRate }); |
| 81 | + var orders = await _orderRepository.ToListAsync(query); |
| 82 | + |
| 83 | + // the same conversion to the primary currency as the order reports use |
| 84 | + return orders.Sum(o => (o.OrderTotal - o.PaidAmount) / (o.CurrencyRate > 0 ? o.CurrencyRate : 1)); |
| 85 | + } |
| 86 | + |
| 87 | + public virtual async Task<bool> HasOverdue(string companyOwnerId, DateTime utcNow) |
| 88 | + { |
| 89 | + if (string.IsNullOrEmpty(companyOwnerId)) |
| 90 | + return false; |
| 91 | + |
| 92 | + //the due date is a calendar day: overdue once it has passed, as in PaymentExtensions.IsPaymentOverdue |
| 93 | + var today = utcNow.Date; |
| 94 | + return await _orderRepository.AnyAsync( |
| 95 | + UnpaidCreditOrders(companyOwnerId).Where(o => o.PaymentDueDateUtc < today)); |
| 96 | + } |
| 97 | + |
| 98 | + #endregion |
| 99 | +} |
0 commit comments