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

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