oldhowl

Untitled

Oct 16th, 2020
504
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Order : DomainEntity<int>, IAuditableEntity, IAggregateRoot
  2.     {
  3.         #region Columns
  4.         public object AuditableIdentifier => Id;
  5.         public DateTimeOffset Created { get; private set; }
  6.         public int CustomerId { get; private set; }
  7.         public Customer Customer { get; private set; }
  8.  
  9.         public int SellerId { get; private set; }
  10.         public Seller Seller { get; private set; }
  11.  
  12.         private readonly List<OrderItem> _items = new List<OrderItem>();
  13.         public IReadOnlyCollection<OrderItem> Items => _items;
  14.  
  15.         public int? InvoiceId { get; private set; }
  16.         public Invoice Invoice { get; private set; }
  17.  
  18.         public int? PartnerReferralId { get; private set; }
  19.  
  20.         public bool IsHiddenForCustomer { get; private set; }
  21.        
  22.         public Refund Refund { get; private set; }
  23.         public int? RefundId { get; private set; }
  24.  
  25.         #region VO
  26.  
  27.         public PaymentInfo PaymentInfo { get; private set; }
  28.  
  29.         #endregion
  30.  
  31.         #endregion
  32.  
  33.  
  34.         //EF default ctor
  35.         private Order()
  36.         {
  37.         }
  38.  
  39.  
  40.         public Order(
  41.             int customerId,
  42.             int sellerId,
  43.             List<OrderItem> orderItems,
  44.             decimal vatRate,
  45.             decimal vatAmount,
  46.             OrderDiscount discount,
  47.             OrderRequisites requisites,
  48.             PaymentType paymentType,
  49.             int? partnerReferralId = null,
  50.             IDateTime created = null)
  51.         {
  52.             var totalPrice = orderItems.Select(x => x.Price).Aggregate((m1, m2) => m1 + m2);
  53.             _items = orderItems;
  54.             CustomerId = customerId;
  55.             SellerId = sellerId;
  56.             PartnerReferralId = partnerReferralId;
  57.             Created = created?.Now ?? DateTimeOffset.Now;
  58.  
  59.             PaymentInfo = new PaymentInfo(Created, paymentType, requisites, totalPrice, discount,
  60.                 new VatInfo(vatRate, vatAmount));
  61.  
  62.  
  63.             PublishEvent(new OrderCreatedEvent(CustomerId, new NewRequisitesDto()
  64.             {
  65.                 Address = requisites.Address,
  66.                 City = requisites.City,
  67.                 Email = requisites.Email,
  68.                 Name = requisites.Name,
  69.                 Surname = requisites.Surname,
  70.                 BirthDate = requisites.BirthDate,
  71.                 CompanyName = requisites.CompanyName,
  72.                 CountryIso = requisites.CountryId,
  73.                 EntityType = requisites.EntityType,
  74.                 PhoneCode = requisites.PhoneCode,
  75.                 PhoneNumber = requisites.PhoneNumber,
  76.                 RegistrationNumber = requisites.RegistrationNumber,
  77.                 VatCode = requisites.VatCode,
  78.                 VatNumber = requisites.VatNumber,
  79.                 ZipCode = requisites.ZipCode
  80.             }));
  81.         }
  82.  
  83.  
  84.         public OrderStatus OrderStatus
  85.         {
  86.             get
  87.             {
  88.                 if (PaymentInfo.IsPayed && !RefundId.HasValue)
  89.                     return OrderStatus.Payed;
  90.  
  91.                 if (RefundId.HasValue)
  92.                     return OrderStatus.PaymentRefund;
  93.  
  94.                 if (!PaymentInfo.PayDate.HasValue)
  95.                     return OrderStatus.WaitingPayment;
  96.  
  97.                 throw new OrderStatusException($"Impossible order status, check order {Id}");
  98.             }
  99.         }
  100.  
  101.         public void HideForCustomer()
  102.         {
  103.             IsHiddenForCustomer = true;
  104.         }
  105.  
  106.         public void ResolveTransactionId(string transactionId)
  107.         {
  108.             if (PaymentInfo.PaymentType != PaymentType.Bank && string.IsNullOrEmpty(transactionId))
  109.                 throw new InvalidTransactionIdException(Id, transactionId, PaymentInfo.TransactionId);
  110.  
  111.             PaymentInfo.ResolveTransactionId(string.IsNullOrEmpty(transactionId) ? $"bank:{Id}" : transactionId);
  112.         }
  113.  
  114.         public void Payed(
  115.             Func<Money, string> moneyToText,
  116.             BankRequsites ownerBankRequisites,
  117.             string transactionId,
  118.             PaymentType paymentType,
  119.             PaymentProcessingType paymentProcessingType,
  120.             DateTimeOffset paymentDate)
  121.         {
  122.             if (PaymentInfo.PayDate != null)
  123.                 throw new OrderAlreadyPayedException(Id, PaymentInfo.PayDate.Value);
  124.  
  125.             if (transactionId != PaymentInfo.TransactionId)
  126.                 throw new InvalidTransactionIdException(Id,
  127.                     transactionId,
  128.                     PaymentInfo.TransactionId);
  129.  
  130.  
  131.             var payDate = PaymentInfo.Payed(transactionId, paymentType, paymentProcessingType, paymentDate);
  132.  
  133.             Invoice = new Invoice(
  134.                 PaymentInfo,
  135.                 moneyToText?.Invoke(PaymentInfo.TotalPrice),
  136.                 ownerBankRequisites,
  137.                 _items.Select(x => x.ToInvoiceItem()));
  138.  
  139.  
  140.             if (!Customer.ReferralStatus.ReferralPartnerId.HasValue && PartnerReferralId.HasValue)
  141.                 Customer.ReferralStatus.ChangeCustomerReferralPartnerId(PartnerReferralId, paymentDate.DateTime);
  142.  
  143.             PublishEvent(
  144.                 new OrderPayedEvent(
  145.                     Id,
  146.                     PaymentInfo.TotalPrice,
  147.                     payDate,
  148.                     Customer,
  149.                     Items.ToList()));
  150.         }
  151.  
  152.         public void AddRefund(int initiatorId)
  153.         {
  154.             // if (transactionId != PaymentInfo.TransactionId)
  155.             //     throw new InvalidTransactionIdException(Id,
  156.             //         transactionId,
  157.             //         PaymentInfo.TransactionId);
  158.  
  159.             if (!PaymentInfo.IsPayed)
  160.                 throw new OrderNotPayedForRefundException(Id);
  161.  
  162.             Refund = new Refund(initiatorId);
  163.  
  164.             this.PublishEvent(new OrderRefundedEvent(Id, CustomerId, Items.Select(x => x.Id).ToArray()));
  165.         }
  166.     }
RAW Paste Data