You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

results.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from decimal import Decimal as D
  2. class OfferApplications(object):
  3. """
  4. A collection of offer applications and the discounts that they give.
  5. Each offer application is stored as a dict which has fields for:
  6. * The offer that led to the successful applicatio
  7. * The result instance
  8. * The number of times the offer was successfully applied
  9. """
  10. def __init__(self):
  11. self.applications = {}
  12. def __iter__(self):
  13. return self.applications.values().__iter__()
  14. def __len__(self):
  15. return len(self.applications)
  16. def add(self, offer, result):
  17. if offer.id not in self.applications:
  18. self.applications[offer.id] = {
  19. 'offer': offer,
  20. 'result': result,
  21. 'name': offer.name,
  22. 'description': result.description,
  23. 'voucher': offer.get_voucher(),
  24. 'freq': 0,
  25. 'discount': D('0.00')}
  26. self.applications[offer.id]['discount'] += result.discount
  27. self.applications[offer.id]['freq'] += 1
  28. @property
  29. def offer_discounts(self):
  30. """
  31. Return basket discounts from offers (but not voucher offers)
  32. """
  33. discounts = []
  34. for application in self.applications.values():
  35. if not application['voucher'] and application['discount'] > 0:
  36. discounts.append(application)
  37. return discounts
  38. @property
  39. def voucher_discounts(self):
  40. """
  41. Return basket discounts from vouchers.
  42. """
  43. discounts = []
  44. for application in self.applications.values():
  45. if application['voucher'] and application['discount'] > 0:
  46. discounts.append(application)
  47. return discounts
  48. @property
  49. def shipping_discounts(self):
  50. """
  51. Return shipping discounts
  52. """
  53. discounts = []
  54. for application in self.applications.values():
  55. if application['result'].affects_shipping:
  56. discounts.append(application)
  57. return discounts
  58. @property
  59. def grouped_voucher_discounts(self):
  60. """
  61. Return voucher discounts aggregated up to the voucher level.
  62. This is different to the voucher_discounts property as a voucher can
  63. have multiple offers associated with it.
  64. """
  65. voucher_discounts = {}
  66. for application in self.voucher_discounts:
  67. voucher = application['voucher']
  68. if voucher.code not in voucher_discounts:
  69. voucher_discounts[voucher.code] = {
  70. 'voucher': voucher,
  71. 'discount': application['discount'],
  72. }
  73. else:
  74. voucher_discounts[voucher.code] += application.discount
  75. return voucher_discounts.values()
  76. @property
  77. def post_order_actions(self):
  78. """
  79. Return successful offer applications which didn't lead to a discount
  80. """
  81. applications = []
  82. for application in self.applications.values():
  83. if application['result'].affects_post_order:
  84. applications.append(application)
  85. return applications
  86. @property
  87. def offers(self):
  88. """
  89. Return a dict of offers that were successfully applied
  90. """
  91. return dict([(a['offer'].id, a['offer']) for a in
  92. self.applications.values()])