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.

methods.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. from decimal import Decimal as D
  2. from django.utils.translation import ugettext_lazy as _
  3. from oscar.apps.shipping.base import ShippingMethod
  4. class Free(ShippingMethod):
  5. """
  6. Simple method for free shipping
  7. """
  8. code = 'free-shipping'
  9. name = _('Free shipping')
  10. def basket_charge_incl_tax(self):
  11. return D('0.00')
  12. def basket_charge_excl_tax(self):
  13. return D('0.00')
  14. class NoShippingRequired(Free):
  15. """
  16. This is a special shipping method that indicates that no shipping is
  17. actually required (eg for digital goods).
  18. """
  19. code = 'no-shipping-required'
  20. name = _('No shipping required')
  21. class FixedPrice(ShippingMethod):
  22. code = 'fixed-price-shipping'
  23. name = _('Fixed price shipping')
  24. def __init__(self, charge_incl_tax, charge_excl_tax=None):
  25. self.charge_incl_tax = charge_incl_tax
  26. if not charge_excl_tax:
  27. charge_excl_tax = charge_incl_tax
  28. self.charge_excl_tax = charge_excl_tax
  29. def basket_charge_incl_tax(self):
  30. return self.charge_incl_tax
  31. def basket_charge_excl_tax(self):
  32. return self.charge_excl_tax
  33. class OfferDiscount(ShippingMethod):
  34. """
  35. Wrapper class that applies a discount to an existing shipping method's
  36. charges
  37. """
  38. def __init__(self, method, offer):
  39. self.method = method
  40. self.offer = offer
  41. @property
  42. def is_discounted(self):
  43. # We check to see if the discount is non-zero. It is possible to have
  44. # zero shipping already in which case this the offer does not lead to
  45. # any further discount.
  46. return self.get_discount()['discount'] > 0
  47. @property
  48. def discount(self):
  49. return self.get_discount()['discount']
  50. @property
  51. def code(self):
  52. return self.method.code
  53. @property
  54. def name(self):
  55. return self.method.name
  56. @property
  57. def description(self):
  58. return self.method.description
  59. def get_discount(self):
  60. # Return a 'discount' dictionary in the same form as that used by the
  61. # OfferApplications class
  62. parent_charge = self.method.basket_charge_incl_tax()
  63. return {
  64. 'offer': self.offer,
  65. 'result': None,
  66. 'name': self.offer.name,
  67. 'description': '',
  68. 'voucher': self.offer.get_voucher(),
  69. 'freq': 1,
  70. 'discount': self.offer.shipping_discount(parent_charge)}
  71. def basket_charge_incl_tax_before_discount(self):
  72. return self.method.basket_charge_incl_tax()
  73. def basket_charge_excl_tax_before_discount(self):
  74. return self.method.basket_charge_excl_tax()
  75. def basket_charge_incl_tax(self):
  76. parent_charge = self.method.basket_charge_incl_tax()
  77. discount = self.offer.shipping_discount(parent_charge)
  78. return parent_charge - discount
  79. def basket_charge_excl_tax(self):
  80. # Adjust tax exclusive rate using the ratio of the two tax inclusive
  81. # charges.
  82. parent_charge_excl_tax = self.method.basket_charge_excl_tax()
  83. parent_charge_incl_tax = self.method.basket_charge_incl_tax()
  84. charge_incl_tax = self.basket_charge_incl_tax()
  85. if parent_charge_incl_tax == 0:
  86. return D('0.00')
  87. return parent_charge_excl_tax * (charge_incl_tax /
  88. parent_charge_incl_tax)