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.

abstract_models.py 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. from decimal import Decimal as D
  2. from django.conf import settings
  3. from django.db import models
  4. from django.utils.translation import ugettext_lazy as _
  5. from django.utils.importlib import import_module as django_import_module
  6. from oscar.core.loading import import_module
  7. import_module('partner.wrappers', ['DefaultWrapper'], locals())
  8. # Cache the partners for quicklookups
  9. default_wrapper = DefaultWrapper()
  10. partner_wrappers = {}
  11. for partner, class_str in settings.OSCAR_PARTNER_WRAPPERS.items():
  12. bits = class_str.split('.')
  13. class_name = bits.pop()
  14. module_str = '.'.join(bits)
  15. module = django_import_module(module_str)
  16. partner_wrappers[partner] = getattr(module, class_name)()
  17. def get_partner_wrapper(partner_name):
  18. """
  19. Returns the appropriate partner wrapper given the partner name
  20. """
  21. return partner_wrappers.get(partner_name, default_wrapper)
  22. class AbstractPartner(models.Model):
  23. """
  24. Fulfillment partner
  25. """
  26. name = models.CharField(max_length=128, unique=True)
  27. # A partner can have users assigned to it. These can be used
  28. # to provide authentication for webservices etc.
  29. users = models.ManyToManyField('auth.User', related_name="partners", blank=True, null=True)
  30. class Meta:
  31. verbose_name_plural = 'Fulfillment partners'
  32. abstract = True
  33. permissions = (
  34. ("can_edit_stock_records", "Can edit stock records"),
  35. ("can_view_stock_records", "Can view stock records"),
  36. ("can_edit_product_range", "Can edit product range"),
  37. ("can_view_product_range", "Can view product range"),
  38. ("can_edit_order_lines", "Can edit order lines"),
  39. ("can_view_order_lines", "Can view order lines"),
  40. )
  41. def __unicode__(self):
  42. return self.name
  43. class AbstractStockRecord(models.Model):
  44. """
  45. A basic stock record.
  46. This links a product to a partner, together with price and availability
  47. information. Most projects will need to subclass this object to add custom
  48. fields such as lead_time, report_code, min_quantity.
  49. """
  50. product = models.OneToOneField('catalogue.Product', related_name="stockrecord")
  51. partner = models.ForeignKey('partner.Partner')
  52. # The fulfilment partner will often have their own SKU for a product, which
  53. # we store here.
  54. partner_sku = models.CharField(_("Partner SKU"), max_length=128, blank=True)
  55. # Price info:
  56. # We deliberately don't store tax information to allow each project
  57. # to subclass this model and put its own fields for convey tax.
  58. price_currency = models.CharField(max_length=12, default=settings.OSCAR_DEFAULT_CURRENCY)
  59. # This is the base price for calculations - tax should be applied
  60. # by the appropriate method. We don't store it here as its calculation is
  61. # highly domain-specific. It is NULLable because some items don't have a fixed
  62. # price.
  63. price_excl_tax = models.DecimalField(decimal_places=2, max_digits=12, blank=True, null=True)
  64. # Retail price for this item
  65. price_retail = models.DecimalField(decimal_places=2, max_digits=12, blank=True, null=True)
  66. # Cost price is optional as not all partner supply it
  67. cost_price = models.DecimalField(decimal_places=2, max_digits=12, blank=True, null=True)
  68. # Stock level information
  69. num_in_stock = models.PositiveIntegerField(default=0, blank=True, null=True)
  70. # The amount of stock allocated to orders but not fed back to the master
  71. # stock system. A typical stock update process will set the num_in_stock
  72. # variable to a new value and reset num_allocated to zero
  73. num_allocated = models.IntegerField(default=0, blank=True, null=True)
  74. # Date information
  75. date_created = models.DateTimeField(auto_now_add=True)
  76. date_updated = models.DateTimeField(auto_now=True, db_index=True)
  77. class Meta:
  78. abstract = True
  79. def allocate(self, quantity):
  80. """
  81. Record a stock allocation.
  82. We don't alter the num_in_stock variable as it is assumed that this
  83. will be set by a batch "stock update" process.
  84. """
  85. self.num_allocated = int(self.num_allocated)
  86. self.num_allocated += quantity
  87. self.save()
  88. def consume_allocation(self, quantity):
  89. if quantity > self.num_allocated:
  90. raise ValueError('No more than %d units can be consumed' % self.num_allocated)
  91. self.num_allocated -= quantity
  92. self.num_in_stock -= quantity
  93. self.save()
  94. def cancel_allocation(self, quantity):
  95. if quantity > self.num_allocated:
  96. raise ValueError('No more than %d units can be cancelled' % self.num_allocated)
  97. self.num_allocated -= quantity
  98. self.save()
  99. def set_discount_price(self, price):
  100. """
  101. A setter method for setting a new price.
  102. This is called from within the "discount" app, which is responsible
  103. for applying fixed-discount offers to products. We use a setter method
  104. so that this behaviour can be customised in projects.
  105. """
  106. self.price_excl_tax = price
  107. self.save()
  108. # Price retrieval methods - these default to no tax being applicable
  109. # These are intended to be overridden.
  110. @property
  111. def is_available_to_buy(self):
  112. """
  113. Return whether this stockrecord allows the product to be purchased
  114. """
  115. return get_partner_wrapper(self.partner.name).is_available_to_buy(self)
  116. def is_purchase_permitted(self, user=None, quantity=1):
  117. """
  118. Return whether this stockrecord allows the product to be purchased by a
  119. specific user and quantity
  120. """
  121. return get_partner_wrapper(self.partner.name).is_purchase_permitted(self, user, quantity)
  122. @property
  123. def net_stock_level(self):
  124. """
  125. Return the effective number in stock
  126. """
  127. if self.num_in_stock is None:
  128. return 0
  129. if self.num_allocated is None:
  130. return self.num_in_stock
  131. return self.num_in_stock - self.num_allocated
  132. @property
  133. def availability_code(self):
  134. """
  135. Return an item's availability as a code for use in CSS
  136. """
  137. return get_partner_wrapper(self.partner.name).availability_code(self)
  138. @property
  139. def availability(self):
  140. """
  141. Return an item's availability as a string
  142. """
  143. return get_partner_wrapper(self.partner.name).availability(self)
  144. @property
  145. def dispatch_date(self):
  146. """
  147. Return the estimated dispatch date for a line
  148. """
  149. return get_partner_wrapper(self.partner.name).dispatch_date(self)
  150. @property
  151. def lead_time(self):
  152. return get_partner_wrapper(self.partner.name).lead_time(self)
  153. @property
  154. def price_incl_tax(self):
  155. """
  156. Return a product's price including tax.
  157. This defaults to the price_excl_tax as tax calculations are
  158. domain specific. This class needs to be subclassed and tax logic
  159. added to this method.
  160. """
  161. if self.price_excl_tax is None:
  162. return D('0.00')
  163. return self.price_excl_tax + self.price_tax
  164. @property
  165. def price_tax(self):
  166. """
  167. Return a product's tax value
  168. """
  169. return get_partner_wrapper(self.partner.name).calculate_tax(self)
  170. def __unicode__(self):
  171. if self.partner_sku:
  172. return "%s (%s): %s" % (self.partner.name, self.partner_sku, self.product.title)
  173. else:
  174. return "%s: %s" % (self.partner.name, self.product.title)