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 35KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. from itertools import chain
  2. from decimal import Decimal as D
  3. import hashlib
  4. from django.conf import settings
  5. from django.db import models
  6. from django.db.models import Sum
  7. from django.utils import timezone
  8. from django.utils.encoding import python_2_unicode_compatible
  9. from django.utils.translation import ugettext_lazy as _, pgettext_lazy
  10. from django.utils.datastructures import SortedDict
  11. from oscar.core.utils import get_default_currency
  12. from oscar.core.compat import AUTH_USER_MODEL
  13. from oscar.models.fields import AutoSlugField
  14. from . import exceptions
  15. @python_2_unicode_compatible
  16. class AbstractOrder(models.Model):
  17. """
  18. The main order model
  19. """
  20. number = models.CharField(
  21. _("Order number"), max_length=128, db_index=True, unique=True)
  22. # We track the site that each order is placed within
  23. site = models.ForeignKey(
  24. 'sites.Site', verbose_name=_("Site"), null=True,
  25. on_delete=models.SET_NULL)
  26. basket = models.ForeignKey(
  27. 'basket.Basket', verbose_name=_("Basket"),
  28. null=True, blank=True, on_delete=models.SET_NULL)
  29. # Orders can be placed without the user authenticating so we don't always
  30. # have a customer ID.
  31. user = models.ForeignKey(
  32. AUTH_USER_MODEL, related_name='orders', null=True, blank=True,
  33. verbose_name=_("User"), on_delete=models.SET_NULL)
  34. # Billing address is not always required (eg paying by gift card)
  35. billing_address = models.ForeignKey(
  36. 'order.BillingAddress', null=True, blank=True,
  37. verbose_name=_("Billing Address"),
  38. on_delete=models.SET_NULL)
  39. # Total price looks like it could be calculated by adding up the
  40. # prices of the associated lines, but in some circumstances extra
  41. # order-level charges are added and so we need to store it separately
  42. currency = models.CharField(
  43. _("Currency"), max_length=12, default=get_default_currency)
  44. total_incl_tax = models.DecimalField(
  45. _("Order total (inc. tax)"), decimal_places=2, max_digits=12)
  46. total_excl_tax = models.DecimalField(
  47. _("Order total (excl. tax)"), decimal_places=2, max_digits=12)
  48. # Shipping charges
  49. shipping_incl_tax = models.DecimalField(
  50. _("Shipping charge (inc. tax)"), decimal_places=2, max_digits=12,
  51. default=0)
  52. shipping_excl_tax = models.DecimalField(
  53. _("Shipping charge (excl. tax)"), decimal_places=2, max_digits=12,
  54. default=0)
  55. # Not all lines are actually shipped (such as downloads), hence shipping
  56. # address is not mandatory.
  57. shipping_address = models.ForeignKey(
  58. 'order.ShippingAddress', null=True, blank=True,
  59. verbose_name=_("Shipping Address"),
  60. on_delete=models.SET_NULL)
  61. shipping_method = models.CharField(
  62. _("Shipping method"), max_length=128, blank=True)
  63. # Identifies shipping code
  64. shipping_code = models.CharField(blank=True, max_length=128, default="")
  65. # Use this field to indicate that an order is on hold / awaiting payment
  66. status = models.CharField(_("Status"), max_length=100, blank=True)
  67. guest_email = models.EmailField(_("Guest email address"), blank=True)
  68. # Index added to this field for reporting
  69. date_placed = models.DateTimeField(auto_now_add=True, db_index=True)
  70. #: Order status pipeline. This should be a dict where each (key, value) #:
  71. #: corresponds to a status and a list of possible statuses that can follow
  72. #: that one.
  73. pipeline = getattr(settings, 'OSCAR_ORDER_STATUS_PIPELINE', {})
  74. #: Order status cascade pipeline. This should be a dict where each (key,
  75. #: value) pair corresponds to an *order* status and the corresponding
  76. #: *line* status that needs to be set when the order is set to the new
  77. #: status
  78. cascade = getattr(settings, 'OSCAR_ORDER_STATUS_CASCADE', {})
  79. @classmethod
  80. def all_statuses(cls):
  81. """
  82. Return all possible statuses for an order
  83. """
  84. return list(cls.pipeline.keys())
  85. def available_statuses(self):
  86. """
  87. Return all possible statuses that this order can move to
  88. """
  89. return self.pipeline.get(self.status, ())
  90. def set_status(self, new_status):
  91. """
  92. Set a new status for this order.
  93. If the requested status is not valid, then ``InvalidOrderStatus`` is
  94. raised.
  95. """
  96. if new_status == self.status:
  97. return
  98. if new_status not in self.available_statuses():
  99. raise exceptions.InvalidOrderStatus(
  100. _("'%(new_status)s' is not a valid status for order %(number)s"
  101. " (current status: '%(status)s')")
  102. % {'new_status': new_status,
  103. 'number': self.number,
  104. 'status': self.status})
  105. self.status = new_status
  106. if new_status in self.cascade:
  107. for line in self.lines.all():
  108. line.status = self.cascade[self.status]
  109. line.save()
  110. self.save()
  111. set_status.alters_data = True
  112. @property
  113. def is_anonymous(self):
  114. # It's possible for an order to be placed by a customer who then
  115. # deletes their profile. Hence, we need to check that a guest email is
  116. # set.
  117. return self.user is None and bool(self.guest_email)
  118. @property
  119. def basket_total_before_discounts_incl_tax(self):
  120. """
  121. Return basket total including tax but before discounts are applied
  122. """
  123. total = D('0.00')
  124. for line in self.lines.all():
  125. total += line.line_price_before_discounts_incl_tax
  126. return total
  127. @property
  128. def basket_total_before_discounts_excl_tax(self):
  129. """
  130. Return basket total excluding tax but before discounts are applied
  131. """
  132. total = D('0.00')
  133. for line in self.lines.all():
  134. total += line.line_price_before_discounts_excl_tax
  135. return total
  136. @property
  137. def basket_total_incl_tax(self):
  138. """
  139. Return basket total including tax
  140. """
  141. return self.total_incl_tax - self.shipping_incl_tax
  142. @property
  143. def basket_total_excl_tax(self):
  144. """
  145. Return basket total excluding tax
  146. """
  147. return self.total_excl_tax - self.shipping_excl_tax
  148. @property
  149. def total_before_discounts_incl_tax(self):
  150. return (self.basket_total_before_discounts_incl_tax +
  151. self.shipping_incl_tax)
  152. @property
  153. def total_before_discounts_excl_tax(self):
  154. return (self.basket_total_before_discounts_excl_tax +
  155. self.shipping_excl_tax)
  156. @property
  157. def total_discount_incl_tax(self):
  158. """
  159. The amount of discount this order received
  160. """
  161. discount = D('0.00')
  162. for line in self.lines.all():
  163. discount += line.discount_incl_tax
  164. return discount
  165. @property
  166. def total_discount_excl_tax(self):
  167. discount = D('0.00')
  168. for line in self.lines.all():
  169. discount += line.discount_excl_tax
  170. return discount
  171. @property
  172. def total_tax(self):
  173. return self.total_incl_tax - self.total_excl_tax
  174. @property
  175. def num_lines(self):
  176. return self.lines.count()
  177. @property
  178. def num_items(self):
  179. """
  180. Returns the number of items in this order.
  181. """
  182. num_items = 0
  183. for line in self.lines.all():
  184. num_items += line.quantity
  185. return num_items
  186. @property
  187. def shipping_tax(self):
  188. return self.shipping_incl_tax - self.shipping_excl_tax
  189. @property
  190. def shipping_status(self):
  191. events = self.shipping_events.all()
  192. if not len(events):
  193. return ''
  194. # Collect all events by event-type
  195. map = {}
  196. for event in events:
  197. event_name = event.event_type.name
  198. if event_name not in map:
  199. map[event_name] = []
  200. map[event_name] = list(chain(map[event_name],
  201. event.line_quantities.all()))
  202. # Determine last complete event
  203. status = _("In progress")
  204. for event_name, event_line_quantities in map.items():
  205. if self._is_event_complete(event_line_quantities):
  206. status = event_name
  207. return status
  208. @property
  209. def has_shipping_discounts(self):
  210. return len(self.shipping_discounts) > 0
  211. @property
  212. def shipping_before_discounts_incl_tax(self):
  213. # We can construct what shipping would have been before discounts by
  214. # adding the discounts back onto the final shipping charge.
  215. total = D('0.00')
  216. for discount in self.shipping_discounts:
  217. total += discount.amount
  218. return self.shipping_incl_tax + total
  219. def _is_event_complete(self, event_quantities):
  220. # Form map of line to quantity
  221. map = {}
  222. for event_quantity in event_quantities:
  223. line_id = event_quantity.line_id
  224. map.setdefault(line_id, 0)
  225. map[line_id] += event_quantity.quantity
  226. for line in self.lines.all():
  227. if map[line.id] != line.quantity:
  228. return False
  229. return True
  230. class Meta:
  231. abstract = True
  232. app_label = 'order'
  233. ordering = ['-date_placed']
  234. verbose_name = _("Order")
  235. verbose_name_plural = _("Orders")
  236. def __str__(self):
  237. return u"#%s" % (self.number,)
  238. def verification_hash(self):
  239. key = '%s%s' % (self.number, settings.SECRET_KEY)
  240. hash = hashlib.md5(key.encode('utf8'))
  241. return hash.hexdigest()
  242. @property
  243. def email(self):
  244. if not self.user:
  245. return self.guest_email
  246. return self.user.email
  247. @property
  248. def basket_discounts(self):
  249. # This includes both offer- and voucher- discounts. For orders we
  250. # don't need to treat them differently like we do for baskets.
  251. return self.discounts.filter(
  252. category=AbstractOrderDiscount.BASKET)
  253. @property
  254. def shipping_discounts(self):
  255. return self.discounts.filter(
  256. category=AbstractOrderDiscount.SHIPPING)
  257. @property
  258. def post_order_actions(self):
  259. return self.discounts.filter(
  260. category=AbstractOrderDiscount.DEFERRED)
  261. @python_2_unicode_compatible
  262. class AbstractOrderNote(models.Model):
  263. """
  264. A note against an order.
  265. This are often used for audit purposes too. IE, whenever an admin
  266. makes a change to an order, we create a note to record what happened.
  267. """
  268. order = models.ForeignKey('order.Order', related_name="notes",
  269. verbose_name=_("Order"))
  270. # These are sometimes programatically generated so don't need a
  271. # user everytime
  272. user = models.ForeignKey(AUTH_USER_MODEL, null=True,
  273. verbose_name=_("User"))
  274. # We allow notes to be classified although this isn't always needed
  275. INFO, WARNING, ERROR, SYSTEM = 'Info', 'Warning', 'Error', 'System'
  276. note_type = models.CharField(_("Note Type"), max_length=128, blank=True)
  277. message = models.TextField(_("Message"))
  278. date_created = models.DateTimeField(_("Date Created"), auto_now_add=True)
  279. date_updated = models.DateTimeField(_("Date Updated"), auto_now=True)
  280. # Notes can only be edited for 5 minutes after being created
  281. editable_lifetime = 300
  282. class Meta:
  283. abstract = True
  284. app_label = 'order'
  285. verbose_name = _("Order Note")
  286. verbose_name_plural = _("Order Notes")
  287. def __str__(self):
  288. return u"'%s' (%s)" % (self.message[0:50], self.user)
  289. def is_editable(self):
  290. if self.note_type == self.SYSTEM:
  291. return False
  292. delta = timezone.now() - self.date_updated
  293. return delta.seconds < self.editable_lifetime
  294. @python_2_unicode_compatible
  295. class AbstractCommunicationEvent(models.Model):
  296. """
  297. An order-level event involving a communication to the customer, such
  298. as an confirmation email being sent.
  299. """
  300. order = models.ForeignKey(
  301. 'order.Order', related_name="communication_events",
  302. verbose_name=_("Order"))
  303. event_type = models.ForeignKey(
  304. 'customer.CommunicationEventType', verbose_name=_("Event Type"))
  305. date_created = models.DateTimeField(_("Date"), auto_now_add=True)
  306. class Meta:
  307. abstract = True
  308. app_label = 'order'
  309. verbose_name = _("Communication Event")
  310. verbose_name_plural = _("Communication Events")
  311. ordering = ['-date_created']
  312. def __str__(self):
  313. return _("'%(type)s' event for order #%(number)s") \
  314. % {'type': self.event_type.name, 'number': self.order.number}
  315. # LINES
  316. @python_2_unicode_compatible
  317. class AbstractLine(models.Model):
  318. """
  319. An order line
  320. """
  321. order = models.ForeignKey(
  322. 'order.Order', related_name='lines', verbose_name=_("Order"))
  323. # PARTNER INFORMATION
  324. # -------------------
  325. # We store the partner and various detail their SKU and the title for cases
  326. # where the product has been deleted from the catalogue (but we still need
  327. # the data for reporting). We also store the partner name in case the
  328. # partner gets deleted at a later date.
  329. partner = models.ForeignKey(
  330. 'partner.Partner', related_name='order_lines', blank=True, null=True,
  331. on_delete=models.SET_NULL, verbose_name=_("Partner"))
  332. partner_name = models.CharField(
  333. _("Partner name"), max_length=128, blank=True)
  334. partner_sku = models.CharField(_("Partner SKU"), max_length=128)
  335. # A line reference is the ID that a partner uses to represent this
  336. # particular line (it's not the same as a SKU).
  337. partner_line_reference = models.CharField(
  338. _("Partner reference"), max_length=128, blank=True,
  339. help_text=_("This is the item number that the partner uses "
  340. "within their system"))
  341. partner_line_notes = models.TextField(
  342. _("Partner Notes"), blank=True)
  343. # We keep a link to the stockrecord used for this line which allows us to
  344. # update stocklevels when it ships
  345. stockrecord = models.ForeignKey(
  346. 'partner.StockRecord', on_delete=models.SET_NULL, blank=True,
  347. null=True, verbose_name=_("Stock record"))
  348. # PRODUCT INFORMATION
  349. # -------------------
  350. # We don't want any hard links between orders and the products table so we
  351. # allow this link to be NULLable.
  352. product = models.ForeignKey(
  353. 'catalogue.Product', on_delete=models.SET_NULL, blank=True, null=True,
  354. verbose_name=_("Product"))
  355. title = models.CharField(
  356. pgettext_lazy(u"Product title", u"Title"), max_length=255)
  357. # UPC can be null because it's usually set as the product's UPC, and that
  358. # can be null as well
  359. upc = models.CharField(_("UPC"), max_length=128, blank=True, null=True)
  360. quantity = models.PositiveIntegerField(_("Quantity"), default=1)
  361. # REPORTING INFORMATION
  362. # ---------------------
  363. # Price information (these fields are actually redundant as the information
  364. # can be calculated from the LinePrice models
  365. line_price_incl_tax = models.DecimalField(
  366. _("Price (inc. tax)"), decimal_places=2, max_digits=12)
  367. line_price_excl_tax = models.DecimalField(
  368. _("Price (excl. tax)"), decimal_places=2, max_digits=12)
  369. # Price information before discounts are applied
  370. line_price_before_discounts_incl_tax = models.DecimalField(
  371. _("Price before discounts (inc. tax)"),
  372. decimal_places=2, max_digits=12)
  373. line_price_before_discounts_excl_tax = models.DecimalField(
  374. _("Price before discounts (excl. tax)"),
  375. decimal_places=2, max_digits=12)
  376. # Cost price (the price charged by the fulfilment partner for this
  377. # product).
  378. unit_cost_price = models.DecimalField(
  379. _("Unit Cost Price"), decimal_places=2, max_digits=12, blank=True,
  380. null=True)
  381. # Normal site price for item (without discounts)
  382. unit_price_incl_tax = models.DecimalField(
  383. _("Unit Price (inc. tax)"), decimal_places=2, max_digits=12,
  384. blank=True, null=True)
  385. unit_price_excl_tax = models.DecimalField(
  386. _("Unit Price (excl. tax)"), decimal_places=2, max_digits=12,
  387. blank=True, null=True)
  388. # Retail price at time of purchase
  389. unit_retail_price = models.DecimalField(
  390. _("Unit Retail Price"), decimal_places=2, max_digits=12,
  391. blank=True, null=True)
  392. # Partners often want to assign some status to each line to help with their
  393. # own business processes.
  394. status = models.CharField(_("Status"), max_length=255, blank=True)
  395. # Estimated dispatch date - should be set at order time
  396. est_dispatch_date = models.DateField(
  397. _("Estimated Dispatch Date"), blank=True, null=True)
  398. #: Order status pipeline. This should be a dict where each (key, value)
  399. #: corresponds to a status and the possible statuses that can follow that
  400. #: one.
  401. pipeline = getattr(settings, 'OSCAR_LINE_STATUS_PIPELINE', {})
  402. class Meta:
  403. abstract = True
  404. app_label = 'order'
  405. verbose_name = _("Order Line")
  406. verbose_name_plural = _("Order Lines")
  407. def __str__(self):
  408. if self.product:
  409. title = self.product.title
  410. else:
  411. title = _('<missing product>')
  412. return _("Product '%(name)s', quantity '%(qty)s'") % {
  413. 'name': title, 'qty': self.quantity}
  414. @classmethod
  415. def all_statuses(cls):
  416. """
  417. Return all possible statuses for an order line
  418. """
  419. return list(cls.pipeline.keys())
  420. def available_statuses(self):
  421. """
  422. Return all possible statuses that this order line can move to
  423. """
  424. return self.pipeline.get(self.status, ())
  425. def set_status(self, new_status):
  426. """
  427. Set a new status for this line
  428. If the requested status is not valid, then ``InvalidLineStatus`` is
  429. raised.
  430. """
  431. if new_status == self.status:
  432. return
  433. if new_status not in self.available_statuses():
  434. raise exceptions.InvalidLineStatus(
  435. _("'%(new_status)s' is not a valid status (current status:"
  436. " '%(status)s')")
  437. % {'new_status': new_status, 'status': self.status})
  438. self.status = new_status
  439. self.save()
  440. set_status.alters_data = True
  441. @property
  442. def category(self):
  443. """
  444. Used by Google analytics tracking
  445. """
  446. return None
  447. @property
  448. def description(self):
  449. """
  450. Returns a description of this line including details of any
  451. line attributes.
  452. """
  453. desc = self.title
  454. ops = []
  455. for attribute in self.attributes.all():
  456. ops.append("%s = '%s'" % (attribute.type, attribute.value))
  457. if ops:
  458. desc = "%s (%s)" % (desc, ", ".join(ops))
  459. return desc
  460. @property
  461. def discount_incl_tax(self):
  462. return self.line_price_before_discounts_incl_tax \
  463. - self.line_price_incl_tax
  464. @property
  465. def discount_excl_tax(self):
  466. return self.line_price_before_discounts_excl_tax \
  467. - self.line_price_excl_tax
  468. @property
  469. def line_price_tax(self):
  470. return self.line_price_incl_tax - self.line_price_excl_tax
  471. @property
  472. def unit_price_tax(self):
  473. return self.unit_price_incl_tax - self.unit_price_excl_tax
  474. # Shipping status helpers
  475. @property
  476. def shipping_status(self):
  477. """
  478. Returns a string summary of the shipping status of this line
  479. """
  480. status_map = self.shipping_event_breakdown
  481. if not status_map:
  482. return ''
  483. events = []
  484. last_complete_event_name = None
  485. for event_dict in reversed(list(status_map.values())):
  486. if event_dict['quantity'] == self.quantity:
  487. events.append(event_dict['name'])
  488. last_complete_event_name = event_dict['name']
  489. else:
  490. events.append("%s (%d/%d items)" % (
  491. event_dict['name'], event_dict['quantity'],
  492. self.quantity))
  493. if last_complete_event_name == list(status_map.values())[0]['name']:
  494. return last_complete_event_name
  495. return ', '.join(events)
  496. def is_shipping_event_permitted(self, event_type, quantity):
  497. """
  498. Test whether a shipping event with the given quantity is permitted
  499. This method should normally be overriden to ensure that the
  500. prerequisite shipping events have been passed for this line.
  501. """
  502. # Note, this calculation is simplistic - normally, you will also need
  503. # to check if previous shipping events have occurred. Eg, you can't
  504. # return lines until they have been shipped.
  505. current_qty = self.shipping_event_quantity(event_type)
  506. return (current_qty + quantity) <= self.quantity
  507. def shipping_event_quantity(self, event_type):
  508. """
  509. Return the quantity of this line that has been involved in a shipping
  510. event of the passed type.
  511. """
  512. result = self.shipping_event_quantities.filter(
  513. event__event_type=event_type).aggregate(Sum('quantity'))
  514. if result['quantity__sum'] is None:
  515. return 0
  516. else:
  517. return result['quantity__sum']
  518. def has_shipping_event_occurred(self, event_type, quantity=None):
  519. """
  520. Test whether this line has passed a given shipping event
  521. """
  522. if not quantity:
  523. quantity = self.quantity
  524. return self.shipping_event_quantity(event_type) == quantity
  525. def get_event_quantity(self, event):
  526. """
  527. Fetches the ShippingEventQuantity instance for this line
  528. Exists as a separate method so it can be overridden to avoid
  529. the DB query that's caused by get().
  530. """
  531. return event.line_quantities.get(line=self)
  532. @property
  533. def shipping_event_breakdown(self):
  534. """
  535. Returns a dict of shipping events that this line has been through
  536. """
  537. status_map = SortedDict()
  538. for event in self.shipping_events.all():
  539. event_type = event.event_type
  540. event_name = event_type.name
  541. event_quantity = self.get_event_quantity(event).quantity
  542. if event_name in status_map:
  543. status_map[event_name]['quantity'] += event_quantity
  544. else:
  545. status_map[event_name] = {
  546. 'event_type': event_type,
  547. 'name': event_name,
  548. 'quantity': event_quantity
  549. }
  550. return status_map
  551. # Payment event helpers
  552. def is_payment_event_permitted(self, event_type, quantity):
  553. """
  554. Test whether a payment event with the given quantity is permitted.
  555. Allow each payment event type to occur only once per quantity.
  556. """
  557. current_qty = self.payment_event_quantity(event_type)
  558. return (current_qty + quantity) <= self.quantity
  559. def payment_event_quantity(self, event_type):
  560. """
  561. Return the quantity of this line that has been involved in a payment
  562. event of the passed type.
  563. """
  564. result = self.payment_event_quantities.filter(
  565. event__event_type=event_type).aggregate(Sum('quantity'))
  566. if result['quantity__sum'] is None:
  567. return 0
  568. else:
  569. return result['quantity__sum']
  570. @property
  571. def is_product_deleted(self):
  572. return self.product is None
  573. def is_available_to_reorder(self, basket, strategy):
  574. """
  575. Test if this line can be re-ordered using the passed strategy and
  576. basket
  577. """
  578. if not self.product:
  579. return False, (_("'%(title)s' is no longer available") %
  580. {'title': self.title})
  581. try:
  582. basket_line = basket.lines.get(product=self.product)
  583. except basket.lines.model.DoesNotExist:
  584. desired_qty = self.quantity
  585. else:
  586. desired_qty = basket_line.quantity + self.quantity
  587. result = strategy.fetch_for_product(self.product)
  588. is_available, reason = result.availability.is_purchase_permitted(
  589. quantity=desired_qty)
  590. if not is_available:
  591. return False, reason
  592. return True, None
  593. @python_2_unicode_compatible
  594. class AbstractLineAttribute(models.Model):
  595. """
  596. An attribute of a line
  597. """
  598. line = models.ForeignKey(
  599. 'order.Line', related_name='attributes',
  600. verbose_name=_("Line"))
  601. option = models.ForeignKey(
  602. 'catalogue.Option', null=True, on_delete=models.SET_NULL,
  603. related_name="line_attributes", verbose_name=_("Option"))
  604. type = models.CharField(_("Type"), max_length=128)
  605. value = models.CharField(_("Value"), max_length=255)
  606. class Meta:
  607. abstract = True
  608. app_label = 'order'
  609. verbose_name = _("Line Attribute")
  610. verbose_name_plural = _("Line Attributes")
  611. def __str__(self):
  612. return "%s = %s" % (self.type, self.value)
  613. @python_2_unicode_compatible
  614. class AbstractLinePrice(models.Model):
  615. """
  616. For tracking the prices paid for each unit within a line.
  617. This is necessary as offers can lead to units within a line
  618. having different prices. For example, one product may be sold at
  619. 50% off as it's part of an offer while the remainder are full price.
  620. """
  621. order = models.ForeignKey(
  622. 'order.Order', related_name='line_prices', verbose_name=_("Option"))
  623. line = models.ForeignKey(
  624. 'order.Line', related_name='prices', verbose_name=_("Line"))
  625. quantity = models.PositiveIntegerField(_("Quantity"), default=1)
  626. price_incl_tax = models.DecimalField(
  627. _("Price (inc. tax)"), decimal_places=2, max_digits=12)
  628. price_excl_tax = models.DecimalField(
  629. _("Price (excl. tax)"), decimal_places=2, max_digits=12)
  630. shipping_incl_tax = models.DecimalField(
  631. _("Shiping (inc. tax)"), decimal_places=2, max_digits=12, default=0)
  632. shipping_excl_tax = models.DecimalField(
  633. _("Shipping (excl. tax)"), decimal_places=2, max_digits=12, default=0)
  634. class Meta:
  635. abstract = True
  636. app_label = 'order'
  637. ordering = ('id',)
  638. verbose_name = _("Line Price")
  639. verbose_name_plural = _("Line Prices")
  640. def __str__(self):
  641. return _("Line '%(number)s' (quantity %(qty)d) price %(price)s") % {
  642. 'number': self.line,
  643. 'qty': self.quantity,
  644. 'price': self.price_incl_tax}
  645. # PAYMENT EVENTS
  646. @python_2_unicode_compatible
  647. class AbstractPaymentEventType(models.Model):
  648. """
  649. Payment event types are things like 'Paid', 'Failed', 'Refunded'.
  650. These are effectively the transaction types.
  651. """
  652. name = models.CharField(_("Name"), max_length=128, unique=True)
  653. code = AutoSlugField(_("Code"), max_length=128, unique=True,
  654. populate_from='name')
  655. class Meta:
  656. abstract = True
  657. app_label = 'order'
  658. verbose_name = _("Payment Event Type")
  659. verbose_name_plural = _("Payment Event Types")
  660. ordering = ('name', )
  661. def __str__(self):
  662. return self.name
  663. @python_2_unicode_compatible
  664. class AbstractPaymentEvent(models.Model):
  665. """
  666. A payment event for an order
  667. For example:
  668. * All lines have been paid for
  669. * 2 lines have been refunded
  670. """
  671. order = models.ForeignKey(
  672. 'order.Order', related_name='payment_events',
  673. verbose_name=_("Order"))
  674. amount = models.DecimalField(
  675. _("Amount"), decimal_places=2, max_digits=12)
  676. # The reference should refer to the transaction ID of the payment gateway
  677. # that was used for this event.
  678. reference = models.CharField(
  679. _("Reference"), max_length=128, blank=True)
  680. lines = models.ManyToManyField(
  681. 'order.Line', through='PaymentEventQuantity',
  682. verbose_name=_("Lines"))
  683. event_type = models.ForeignKey(
  684. 'order.PaymentEventType', verbose_name=_("Event Type"))
  685. # Allow payment events to be linked to shipping events. Often a shipping
  686. # event will trigger a payment event and so we can use this FK to capture
  687. # the relationship.
  688. shipping_event = models.ForeignKey(
  689. 'order.ShippingEvent', related_name='payment_events',
  690. null=True)
  691. date_created = models.DateTimeField(_("Date created"), auto_now_add=True)
  692. class Meta:
  693. abstract = True
  694. app_label = 'order'
  695. verbose_name = _("Payment Event")
  696. verbose_name_plural = _("Payment Events")
  697. ordering = ['-date_created']
  698. def __str__(self):
  699. return _("Payment event for order %s") % self.order
  700. def num_affected_lines(self):
  701. return self.lines.all().count()
  702. class PaymentEventQuantity(models.Model):
  703. """
  704. A "through" model linking lines to payment events
  705. """
  706. event = models.ForeignKey(
  707. 'order.PaymentEvent', related_name='line_quantities',
  708. verbose_name=_("Event"))
  709. line = models.ForeignKey(
  710. 'order.Line', related_name="payment_event_quantities",
  711. verbose_name=_("Line"))
  712. quantity = models.PositiveIntegerField(_("Quantity"))
  713. class Meta:
  714. app_label = 'order'
  715. verbose_name = _("Payment Event Quantity")
  716. verbose_name_plural = _("Payment Event Quantities")
  717. unique_together = ('event', 'line')
  718. # SHIPPING EVENTS
  719. @python_2_unicode_compatible
  720. class AbstractShippingEvent(models.Model):
  721. """
  722. An event is something which happens to a group of lines such as
  723. 1 item being dispatched.
  724. """
  725. order = models.ForeignKey(
  726. 'order.Order', related_name='shipping_events', verbose_name=_("Order"))
  727. lines = models.ManyToManyField(
  728. 'order.Line', related_name='shipping_events',
  729. through='ShippingEventQuantity', verbose_name=_("Lines"))
  730. event_type = models.ForeignKey(
  731. 'order.ShippingEventType', verbose_name=_("Event Type"))
  732. notes = models.TextField(
  733. _("Event notes"), blank=True,
  734. help_text=_("This could be the dispatch reference, or a "
  735. "tracking number"))
  736. date_created = models.DateTimeField(_("Date Created"), auto_now_add=True)
  737. class Meta:
  738. abstract = True
  739. app_label = 'order'
  740. verbose_name = _("Shipping Event")
  741. verbose_name_plural = _("Shipping Events")
  742. ordering = ['-date_created']
  743. def __str__(self):
  744. return _("Order #%(number)s, type %(type)s") % {
  745. 'number': self.order.number,
  746. 'type': self.event_type}
  747. def num_affected_lines(self):
  748. return self.lines.count()
  749. @python_2_unicode_compatible
  750. class ShippingEventQuantity(models.Model):
  751. """
  752. A "through" model linking lines to shipping events.
  753. This exists to track the quantity of a line that is involved in a
  754. particular shipping event.
  755. """
  756. event = models.ForeignKey(
  757. 'order.ShippingEvent', related_name='line_quantities',
  758. verbose_name=_("Event"))
  759. line = models.ForeignKey(
  760. 'order.Line', related_name="shipping_event_quantities",
  761. verbose_name=_("Line"))
  762. quantity = models.PositiveIntegerField(_("Quantity"))
  763. class Meta:
  764. app_label = 'order'
  765. verbose_name = _("Shipping Event Quantity")
  766. verbose_name_plural = _("Shipping Event Quantities")
  767. unique_together = ('event', 'line')
  768. def save(self, *args, **kwargs):
  769. # Default quantity to full quantity of line
  770. if not self.quantity:
  771. self.quantity = self.line.quantity
  772. # Ensure we don't violate quantities constraint
  773. if not self.line.is_shipping_event_permitted(
  774. self.event.event_type, self.quantity):
  775. raise exceptions.InvalidShippingEvent
  776. super(ShippingEventQuantity, self).save(*args, **kwargs)
  777. def __str__(self):
  778. return _("%(product)s - quantity %(qty)d") % {
  779. 'product': self.line.product,
  780. 'qty': self.quantity}
  781. @python_2_unicode_compatible
  782. class AbstractShippingEventType(models.Model):
  783. """
  784. A type of shipping/fulfillment event
  785. Eg: 'Shipped', 'Cancelled', 'Returned'
  786. """
  787. # Name is the friendly description of an event
  788. name = models.CharField(_("Name"), max_length=255, unique=True)
  789. # Code is used in forms
  790. code = AutoSlugField(_("Code"), max_length=128, unique=True,
  791. populate_from='name')
  792. class Meta:
  793. abstract = True
  794. app_label = 'order'
  795. verbose_name = _("Shipping Event Type")
  796. verbose_name_plural = _("Shipping Event Types")
  797. ordering = ('name', )
  798. def __str__(self):
  799. return self.name
  800. # DISCOUNTS
  801. @python_2_unicode_compatible
  802. class AbstractOrderDiscount(models.Model):
  803. """
  804. A discount against an order.
  805. Normally only used for display purposes so an order can be listed with
  806. discounts displayed separately even though in reality, the discounts are
  807. applied at the line level.
  808. This has evolved to be a slightly misleading class name as this really
  809. track benefit applications which aren't necessarily discounts.
  810. """
  811. order = models.ForeignKey(
  812. 'order.Order', related_name="discounts", verbose_name=_("Order"))
  813. # We need to distinguish between basket discounts, shipping discounts and
  814. # 'deferred' discounts.
  815. BASKET, SHIPPING, DEFERRED = "Basket", "Shipping", "Deferred"
  816. CATEGORY_CHOICES = (
  817. (BASKET, _(BASKET)),
  818. (SHIPPING, _(SHIPPING)),
  819. (DEFERRED, _(DEFERRED)),
  820. )
  821. category = models.CharField(
  822. _("Discount category"), default=BASKET, max_length=64,
  823. choices=CATEGORY_CHOICES)
  824. offer_id = models.PositiveIntegerField(
  825. _("Offer ID"), blank=True, null=True)
  826. offer_name = models.CharField(
  827. _("Offer name"), max_length=128, db_index=True, blank=True)
  828. voucher_id = models.PositiveIntegerField(
  829. _("Voucher ID"), blank=True, null=True)
  830. voucher_code = models.CharField(
  831. _("Code"), max_length=128, db_index=True, blank=True)
  832. frequency = models.PositiveIntegerField(_("Frequency"), null=True)
  833. amount = models.DecimalField(
  834. _("Amount"), decimal_places=2, max_digits=12, default=0)
  835. # Post-order offer applications can return a message to indicate what
  836. # action was taken after the order was placed.
  837. message = models.TextField(blank=True)
  838. @property
  839. def is_basket_discount(self):
  840. return self.category == self.BASKET
  841. @property
  842. def is_shipping_discount(self):
  843. return self.category == self.SHIPPING
  844. @property
  845. def is_post_order_action(self):
  846. return self.category == self.DEFERRED
  847. class Meta:
  848. abstract = True
  849. app_label = 'order'
  850. verbose_name = _("Order Discount")
  851. verbose_name_plural = _("Order Discounts")
  852. def save(self, **kwargs):
  853. if self.offer_id and not self.offer_name:
  854. offer = self.offer
  855. if offer:
  856. self.offer_name = offer.name
  857. if self.voucher_id and not self.voucher_code:
  858. voucher = self.voucher
  859. if voucher:
  860. self.voucher_code = voucher.code
  861. super(AbstractOrderDiscount, self).save(**kwargs)
  862. def __str__(self):
  863. return _("Discount of %(amount)r from order %(order)s") % {
  864. 'amount': self.amount, 'order': self.order}
  865. @property
  866. def offer(self):
  867. Offer = models.get_model('offer', 'ConditionalOffer')
  868. try:
  869. return Offer.objects.get(id=self.offer_id)
  870. except Offer.DoesNotExist:
  871. return None
  872. @property
  873. def voucher(self):
  874. Voucher = models.get_model('voucher', 'Voucher')
  875. try:
  876. return Voucher.objects.get(id=self.voucher_id)
  877. except Voucher.DoesNotExist:
  878. return None
  879. def description(self):
  880. if self.voucher_code:
  881. return self.voucher_code
  882. return self.offer_name or u""