/nopCommerce_1.60/Libraries/Nop.BusinessLogic/Orders/RecurringPayment.cs

http://glavzmey.codeplex.com · C# · 263 lines · 166 code · 28 blank · 69 comment · 18 complexity · ca1e5ed51d1e355a401b6d28c0e0edf2 MD5 · raw file

  1. //------------------------------------------------------------------------------
  2. // The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
  3. // You may obtain a copy of the License at http://www.nopCommerce.com/License.aspx.
  4. //
  5. // Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
  6. // See the License for the specific language governing rights and limitations under the License.
  7. //
  8. // The Original Code is nopCommerce.
  9. // The Initial Developer of the Original Code is NopSolutions.
  10. // All Rights Reserved.
  11. //
  12. // Contributor(s): _______.
  13. //------------------------------------------------------------------------------
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Text;
  17. using NopSolutions.NopCommerce.BusinessLogic.CustomerManagement;
  18. using NopSolutions.NopCommerce.BusinessLogic.Products;
  19. using NopSolutions.NopCommerce.Common;
  20. using NopSolutions.NopCommerce.BusinessLogic.Payment;
  21. namespace NopSolutions.NopCommerce.BusinessLogic.Orders
  22. {
  23. /// <summary>
  24. /// Represents a recurring payment
  25. /// </summary>
  26. public partial class RecurringPayment : BaseEntity
  27. {
  28. #region Ctor
  29. /// <summary>
  30. /// Creates a new instance of the RecurringPayment class
  31. /// </summary>
  32. public RecurringPayment()
  33. {
  34. }
  35. #endregion
  36. #region Properties
  37. /// <summary>
  38. /// Gets or sets the recurring payment identifier
  39. /// </summary>
  40. public int RecurringPaymentId { get; set; }
  41. /// <summary>
  42. /// Gets or sets the initial order identifier
  43. /// </summary>
  44. public int InitialOrderId { get; set; }
  45. /// <summary>
  46. /// Gets or sets the cycle length
  47. /// </summary>
  48. public int CycleLength { get; set; }
  49. /// <summary>
  50. /// Gets or sets the cycle period
  51. /// </summary>
  52. public int CyclePeriod { get; set; }
  53. /// <summary>
  54. /// Gets or sets the total cycles
  55. /// </summary>
  56. public int TotalCycles { get; set; }
  57. /// <summary>
  58. /// Gets or sets the start date
  59. /// </summary>
  60. public DateTime StartDate { get; set; }
  61. /// <summary>
  62. /// Gets or sets a value indicating whether the payment is active
  63. /// </summary>
  64. public bool IsActive { get; set; }
  65. /// <summary>
  66. /// Gets or sets a value indicating whether the entity has been deleted
  67. /// </summary>
  68. public bool Deleted { get; set; }
  69. /// <summary>
  70. /// Gets or sets the date and time of payment creation
  71. /// </summary>
  72. public DateTime CreatedOn { get; set; }
  73. #endregion
  74. #region Custom Properties
  75. /// <summary>
  76. /// Gets the initial order
  77. /// </summary>
  78. public Order InitialOrder
  79. {
  80. get
  81. {
  82. return OrderManager.GetOrderById(this.InitialOrderId);
  83. }
  84. }
  85. /// <summary>
  86. /// Gets the initial customer
  87. /// </summary>
  88. public Customer Customer
  89. {
  90. get
  91. {
  92. Customer customer = null;
  93. Order initialOrder = this.InitialOrder;
  94. if (initialOrder != null)
  95. {
  96. customer = initialOrder.Customer;
  97. }
  98. return customer;
  99. }
  100. }
  101. /// <summary>
  102. /// Gets the recurring payment history
  103. /// </summary>
  104. public RecurringPaymentHistoryCollection RecurringPaymentHistory
  105. {
  106. get
  107. {
  108. return OrderManager.SearchRecurringPaymentHistory(this.RecurringPaymentId, 0);
  109. }
  110. }
  111. /// <summary>
  112. /// Gets the next payment date
  113. /// </summary>
  114. public DateTime? NextPaymentDate
  115. {
  116. get
  117. {
  118. //result
  119. DateTime? result = null;
  120. if (!this.IsActive)
  121. return result;
  122. var historyCollection = this.RecurringPaymentHistory;
  123. if (historyCollection.Count >= this.TotalCycles)
  124. {
  125. return result;
  126. }
  127. //set another value to change calculation method
  128. bool useLatestPayment = false;
  129. if (useLatestPayment)
  130. {
  131. //get latest payment
  132. RecurringPaymentHistory latestPayment = null;
  133. foreach (var historyRecord in historyCollection)
  134. {
  135. if (latestPayment != null)
  136. {
  137. if (historyRecord.CreatedOn >= latestPayment.CreatedOn)
  138. {
  139. latestPayment = historyRecord;
  140. }
  141. }
  142. else
  143. {
  144. latestPayment = historyRecord;
  145. }
  146. }
  147. //calculate next payment date
  148. if (latestPayment != null)
  149. {
  150. switch (this.CyclePeriod)
  151. {
  152. case (int)RecurringProductCyclePeriodEnum.Days:
  153. result = latestPayment.CreatedOn.AddDays((double)this.CycleLength);
  154. break;
  155. case (int)RecurringProductCyclePeriodEnum.Weeks:
  156. result = latestPayment.CreatedOn.AddDays((double)(7 * this.CycleLength));
  157. break;
  158. case (int)RecurringProductCyclePeriodEnum.Months:
  159. result = latestPayment.CreatedOn.AddMonths(this.CycleLength);
  160. break;
  161. case (int)RecurringProductCyclePeriodEnum.Years:
  162. result = latestPayment.CreatedOn.AddYears(this.CycleLength);
  163. break;
  164. default:
  165. throw new NopException("Not supported cycle period");
  166. }
  167. }
  168. else
  169. {
  170. if (this.TotalCycles > 0)
  171. result = this.StartDate;
  172. }
  173. }
  174. else
  175. {
  176. if (historyCollection.Count > 0)
  177. {
  178. switch (this.CyclePeriod)
  179. {
  180. case (int)RecurringProductCyclePeriodEnum.Days:
  181. result = this.StartDate.AddDays((double)this.CycleLength * historyCollection.Count);
  182. break;
  183. case (int)RecurringProductCyclePeriodEnum.Weeks:
  184. result = this.StartDate.AddDays((double)(7 * this.CycleLength) * historyCollection.Count);
  185. break;
  186. case (int)RecurringProductCyclePeriodEnum.Months:
  187. result = this.StartDate.AddMonths(this.CycleLength * historyCollection.Count);
  188. break;
  189. case (int)RecurringProductCyclePeriodEnum.Years:
  190. result = this.StartDate.AddYears(this.CycleLength * historyCollection.Count);
  191. break;
  192. default:
  193. throw new NopException("Not supported cycle period");
  194. }
  195. }
  196. else
  197. {
  198. if (this.TotalCycles > 0)
  199. result = this.StartDate;
  200. }
  201. }
  202. return result;
  203. }
  204. }
  205. /// <summary>
  206. /// Gets the cycles remaining
  207. /// </summary>
  208. public int CyclesRemaining
  209. {
  210. get
  211. {
  212. //result
  213. var historyCollection = this.RecurringPaymentHistory;
  214. int result = this.TotalCycles - historyCollection.Count;
  215. if (result < 0)
  216. result = 0;
  217. return result;
  218. }
  219. }
  220. /// <summary>
  221. /// Gets a recurring payment type
  222. /// </summary>
  223. public RecurringPaymentTypeEnum RecurringPaymentType
  224. {
  225. get
  226. {
  227. Order order = this.InitialOrder;
  228. if (order == null)
  229. return RecurringPaymentTypeEnum.NotSupported;
  230. return PaymentManager.SupportRecurringPayments(order.PaymentMethodId);
  231. }
  232. }
  233. #endregion
  234. }
  235. }