Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

abstract_models.py 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. from decimal import Decimal as D
  2. import warnings
  3. from django.db import models
  4. from django.conf import settings
  5. from django.db.models import get_model
  6. from django.utils.translation import ugettext_lazy as _
  7. from django.utils.importlib import import_module as django_import_module
  8. from oscar.core.compat import AUTH_USER_MODEL
  9. from oscar.core.utils import slugify
  10. from oscar.core.loading import get_class
  11. from oscar.apps.partner.exceptions import InvalidStockAdjustment
  12. DefaultWrapper = get_class('partner.wrappers', 'DefaultWrapper')
  13. # Cache dict of partner_id => availability wrapper instance
  14. partner_wrappers = None
  15. default_wrapper = DefaultWrapper()
  16. def get_partner_wrapper(partner_id):
  17. """
  18. Returns the appropriate partner wrapper given the partner's PK
  19. """
  20. if partner_wrappers is None:
  21. _load_partner_wrappers()
  22. return partner_wrappers.get(partner_id, default_wrapper)
  23. def _load_partner_wrappers():
  24. # Prime cache of partner wrapper dict
  25. global partner_wrappers
  26. partner_wrappers = {}
  27. Partner = get_model('partner', 'Partner')
  28. for code, class_str in settings.OSCAR_PARTNER_WRAPPERS.items():
  29. try:
  30. partner = Partner.objects.get(code=code)
  31. except Partner.DoesNotExist:
  32. continue
  33. else:
  34. module_path, klass = class_str.rsplit('.', 1)
  35. module = django_import_module(module_path)
  36. partner_wrappers[partner.id] = getattr(module, klass)()
  37. class AbstractPartner(models.Model):
  38. """
  39. A fulfillment partner. An individual or company who can fulfil products.
  40. E.g. for physical goods, somebody with a warehouse and means of delivery.
  41. Creating one or more instances of the Partner model is a required step in
  42. setting up an Oscar deployment. Many Oscar deployments will only have one
  43. fulfillment partner.
  44. """
  45. code = models.SlugField(_("Code"), max_length=128, unique=True)
  46. name = models.CharField(_("Name"), max_length=128, null=True, blank=True)
  47. #: A partner can have users assigned to it. These can be used
  48. #: to provide authentication for webservices etc.
  49. users = models.ManyToManyField(
  50. AUTH_USER_MODEL, related_name="partners",
  51. blank=True, null=True, verbose_name=_("Users"))
  52. @property
  53. def display_name(self):
  54. if not self.name:
  55. return self.code
  56. return self.name
  57. def save(self, *args, **kwargs):
  58. if not self.code:
  59. self.code = slugify(self.name)
  60. super(AbstractPartner, self).save(*args, **kwargs)
  61. @property
  62. def primary_address(self):
  63. """
  64. Returns a partners primary address. Usually that will be the
  65. headquarters or similar.
  66. This is a rudimentary implementation that raises an error if there's
  67. more than one address. If you actually want to support multiple
  68. addresses, you will likely need to extend PartnerAddress to have some
  69. field or flag to base your decision on.
  70. """
  71. addresses = self.addresses.all()
  72. if len(addresses) == 0: # intentionally using len() to save queries
  73. return None
  74. elif len(addresses) == 1:
  75. return addresses[0]
  76. else:
  77. raise NotImplementedError(
  78. "Oscar's default implementation of primary_address only "
  79. "supports one PartnerAddress. You need to override the "
  80. "primary_address to look up the right address")
  81. def get_address_for_stockrecord(self, stockrecord):
  82. """
  83. Stock might be coming from different warehouses. Overriding this
  84. function allows selecting the correct PartnerAddress for the record.
  85. That can be useful when determining tax.
  86. """
  87. return self.primary_address
  88. class Meta:
  89. verbose_name = _('Fulfillment partner')
  90. verbose_name_plural = _('Fulfillment partners')
  91. abstract = True
  92. permissions = (
  93. ("can_edit_stock_records", _("Can edit stock records")),
  94. ("can_view_stock_records", _("Can view stock records")),
  95. ("can_edit_product_range", _("Can edit product range")),
  96. ("can_view_product_range", _("Can view product range")),
  97. ("can_edit_order_lines", _("Can edit order lines")),
  98. ("can_view_order_lines", _("Can view order lines"))
  99. )
  100. def __unicode__(self):
  101. return self.name
  102. class AbstractStockRecord(models.Model):
  103. """
  104. A stock record.
  105. This records information about a product from a fulfilment partner, such as
  106. their SKU, the number they have in stock and price information.
  107. Stockrecords are used by 'strategies' to determine availability and pricing
  108. information for the customer.
  109. """
  110. product = models.ForeignKey(
  111. 'catalogue.Product', related_name="stockrecords",
  112. verbose_name=_("Product"))
  113. partner = models.ForeignKey(
  114. 'partner.Partner', verbose_name=_("Partner"),
  115. related_name='stockrecords')
  116. #: The fulfilment partner will often have their own SKU for a product,
  117. #: which we store here. This will sometimes be the same the product's UPC
  118. #: but not always. It should be unique per partner.
  119. #: See also http://en.wikipedia.org/wiki/Stock-keeping_unit
  120. partner_sku = models.CharField(_("Partner SKU"), max_length=128)
  121. # Price info:
  122. price_currency = models.CharField(
  123. _("Currency"), max_length=12, default=settings.OSCAR_DEFAULT_CURRENCY)
  124. # This is the base price for calculations - tax should be applied by the
  125. # appropriate method. We don't store tax here as its calculation is highly
  126. # domain-specific. It is NULLable because some items don't have a fixed
  127. # price but require a runtime calculation (possible from an external
  128. # service).
  129. price_excl_tax = models.DecimalField(
  130. _("Price (excl. tax)"), decimal_places=2, max_digits=12,
  131. blank=True, null=True)
  132. #: Retail price for this item. This is simply the recommended price from
  133. #: the manufacturer. If this is used, it is for display purposes only.
  134. #: This prices is the NOT the price charged to the customer.
  135. price_retail = models.DecimalField(
  136. _("Price (retail)"), decimal_places=2, max_digits=12,
  137. blank=True, null=True)
  138. #: Cost price is the price charged by the fulfilment partner. It is not
  139. #: used (by default) in any price calculations but is often used in
  140. #: reporting so merchants can report on their profit margin.
  141. cost_price = models.DecimalField(
  142. _("Cost Price"), decimal_places=2, max_digits=12,
  143. blank=True, null=True)
  144. #: Number of items in stock
  145. num_in_stock = models.PositiveIntegerField(
  146. _("Number in stock"), blank=True, null=True)
  147. #: The amount of stock allocated to orders but not fed back to the master
  148. #: stock system. A typical stock update process will set the num_in_stock
  149. #: variable to a new value and reset num_allocated to zero
  150. num_allocated = models.IntegerField(
  151. _("Number allocated"), blank=True, null=True)
  152. #: Threshold for low-stock alerts. When stock goes beneath this threshold,
  153. #: an alert is triggered so warehouse managers can order more.
  154. low_stock_threshold = models.PositiveIntegerField(
  155. _("Low Stock Threshold"), blank=True, null=True)
  156. # Date information
  157. date_created = models.DateTimeField(_("Date created"), auto_now_add=True)
  158. date_updated = models.DateTimeField(_("Date updated"), auto_now=True,
  159. db_index=True)
  160. def __unicode__(self):
  161. msg = u"Partner: %s, product: %s" % (
  162. self.partner.display_name, self.product,)
  163. if self.partner_sku:
  164. msg = u"%s (%s)" % (msg, self.partner_sku)
  165. return msg
  166. class Meta:
  167. abstract = True
  168. unique_together = ('partner', 'partner_sku')
  169. verbose_name = _("Stock record")
  170. verbose_name_plural = _("Stock records")
  171. @property
  172. def net_stock_level(self):
  173. """
  174. The effective number in stock (eg available to buy).
  175. This is correct property to show the customer, not the num_in_stock
  176. field as that doesn't account for allocations. This can be negative in
  177. some unusual circumstances
  178. """
  179. if self.num_in_stock is None:
  180. return 0
  181. if self.num_allocated is None:
  182. return self.num_in_stock
  183. return self.num_in_stock - self.num_allocated
  184. # 2-stage stock management model
  185. def allocate(self, quantity):
  186. """
  187. Record a stock allocation.
  188. This normally happens when a product is bought at checkout. When the
  189. product is actually shipped, then we 'consume' the allocation.
  190. """
  191. if self.num_allocated is None:
  192. self.num_allocated = 0
  193. self.num_allocated += quantity
  194. self.save()
  195. allocate.alters_data = True
  196. def is_allocation_consumption_possible(self, quantity):
  197. """
  198. Test if a proposed stock consumption is permitted
  199. """
  200. return quantity <= min(self.num_allocated, self.num_in_stock)
  201. def consume_allocation(self, quantity):
  202. """
  203. Consume a previous allocation
  204. This is used when an item is shipped. We remove the original
  205. allocation and adjust the number in stock accordingly
  206. """
  207. if not self.is_allocation_consumption_possible(quantity):
  208. raise InvalidStockAdjustment(
  209. _('Invalid stock consumption request'))
  210. self.num_allocated -= quantity
  211. self.num_in_stock -= quantity
  212. self.save()
  213. consume_allocation.alters_data = True
  214. def cancel_allocation(self, quantity):
  215. # We ignore requests that request a cancellation of more than the
  216. # amount already allocated.
  217. self.num_allocated -= min(self.num_allocated, quantity)
  218. self.save()
  219. cancel_allocation.alters_data = True
  220. @property
  221. def is_below_threshold(self):
  222. if self.low_stock_threshold is None:
  223. return False
  224. return self.net_stock_level < self.low_stock_threshold
  225. # Stock wrapper methods - deprecated since 0.6
  226. @property
  227. def is_available_to_buy(self):
  228. """
  229. Return whether this stockrecord allows the product to be purchased
  230. """
  231. warnings.warn((
  232. "StockRecord.is_available_to_buy is deprecated and will be "
  233. "removed in 0.7. Use a strategy class to determine availability "
  234. "instead"), DeprecationWarning)
  235. return get_partner_wrapper(self.partner_id).is_available_to_buy(self)
  236. def is_purchase_permitted(self, user=None, quantity=1, product=None):
  237. """
  238. Return whether this stockrecord allows the product to be purchased by a
  239. specific user and quantity
  240. """
  241. warnings.warn((
  242. "StockRecord.is_purchase_permitted is deprecated and will be "
  243. "removed in 0.7. Use a strategy class to determine availability "
  244. "instead"), DeprecationWarning)
  245. return get_partner_wrapper(
  246. self.partner_id).is_purchase_permitted(
  247. self, user, quantity, product)
  248. @property
  249. def availability_code(self):
  250. """
  251. Return an product's availability as a code for use in CSS to add icons
  252. to the overall availability mark-up. For example, "instock",
  253. "unavailable".
  254. """
  255. warnings.warn((
  256. "StockRecord.availability_code is deprecated and will be "
  257. "removed in 0.7. Use a strategy class to determine availability "
  258. "instead"), DeprecationWarning)
  259. return get_partner_wrapper(self.partner_id).availability_code(self)
  260. @property
  261. def availability(self):
  262. """
  263. Return a product's availability as a string that can be displayed to
  264. the user. For example, "In stock", "Unavailable".
  265. """
  266. warnings.warn((
  267. "StockRecord.availability is deprecated and will be "
  268. "removed in 0.7. Use a strategy class to determine availability "
  269. "instead"), DeprecationWarning)
  270. return get_partner_wrapper(self.partner_id).availability(self)
  271. def max_purchase_quantity(self, user=None):
  272. """
  273. Return an item's availability as a string
  274. :param user: (optional) The user who wants to purchase
  275. """
  276. warnings.warn((
  277. "StockRecord.max_purchase_quantity is deprecated and will be "
  278. "removed in 0.7. Use a strategy class to determine availability "
  279. "instead"), DeprecationWarning)
  280. return get_partner_wrapper(
  281. self.partner_id).max_purchase_quantity(self, user)
  282. @property
  283. def dispatch_date(self):
  284. """
  285. Return the estimated dispatch date for a line
  286. """
  287. warnings.warn((
  288. "StockRecord.dispatch_date is deprecated and will be "
  289. "removed in 0.7. Use a strategy class to determine availability "
  290. "instead"), DeprecationWarning)
  291. return get_partner_wrapper(self.partner_id).dispatch_date(self)
  292. @property
  293. def lead_time(self):
  294. warnings.warn((
  295. "StockRecord.lead_time is deprecated and will be "
  296. "removed in 0.7. Use a strategy class to determine availability "
  297. "instead"), DeprecationWarning)
  298. return get_partner_wrapper(self.partner_id).lead_time(self)
  299. # Price methods - deprecated in 0.6
  300. @property
  301. def price_incl_tax(self):
  302. """
  303. Return a product's price including tax.
  304. This defaults to the price_excl_tax as tax calculations are
  305. domain specific. This class needs to be subclassed and tax logic
  306. added to this method.
  307. """
  308. warnings.warn((
  309. "StockRecord.price_incl_tax is deprecated and will be "
  310. "removed in 0.7. Use a strategy class to determine price "
  311. "information instead"), DeprecationWarning, stacklevel=2)
  312. if self.price_excl_tax is None:
  313. return D('0.00')
  314. return self.price_excl_tax + self.price_tax
  315. @property
  316. def price_tax(self):
  317. """
  318. Return a product's tax value
  319. """
  320. warnings.warn((
  321. "StockRecord.price_incl_tax is deprecated and will be "
  322. "removed in 0.7. Use a strategy class to determine price "
  323. "information instead"), DeprecationWarning)
  324. return get_partner_wrapper(self.partner_id).calculate_tax(self)
  325. class AbstractStockAlert(models.Model):
  326. """
  327. A stock alert. E.g. used to notify users when a product is 'back in stock'.
  328. """
  329. stockrecord = models.ForeignKey(
  330. 'partner.StockRecord', related_name='alerts',
  331. verbose_name=_("Stock Record"))
  332. threshold = models.PositiveIntegerField(_("Threshold"))
  333. OPEN, CLOSED = "Open", "Closed"
  334. status_choices = (
  335. (OPEN, _("Open")),
  336. (CLOSED, _("Closed")),
  337. )
  338. status = models.CharField(_("Status"), max_length=128, default=OPEN,
  339. choices=status_choices)
  340. date_created = models.DateTimeField(_("Date Created"), auto_now_add=True)
  341. date_closed = models.DateTimeField(_("Date Closed"), blank=True, null=True)
  342. def close(self):
  343. self.status = self.CLOSED
  344. self.save()
  345. close.alters_data = True
  346. def __unicode__(self):
  347. return _('<stockalert for "%(stock)s" status %(status)s>') % {'stock': self.stockrecord, 'status': self.status}
  348. class Meta:
  349. abstract = True
  350. ordering = ('-date_created',)
  351. verbose_name = _('Stock Alert')
  352. verbose_name_plural = _('Stock Alerts')