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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. from itertools import chain
  2. from decimal import Decimal as D
  3. from django.db import models
  4. from django.contrib.auth.models import User
  5. from django.template.defaultfilters import slugify
  6. from django.utils.translation import ugettext_lazy as _
  7. from django.db.models import Sum
  8. from django.core.exceptions import ObjectDoesNotExist
  9. class AbstractOrder(models.Model):
  10. """
  11. The main order model
  12. """
  13. number = models.CharField(_("Order number"), max_length=128, db_index=True)
  14. # We track the site that each order is placed within
  15. site = models.ForeignKey('sites.Site')
  16. basket = models.ForeignKey('basket.Basket', null=True, blank=True)
  17. # Orders can be anonymous so we don't always have a customer ID
  18. user = models.ForeignKey(User, related_name='orders', null=True, blank=True)
  19. # Billing address is not always required (eg paying by gift card)
  20. billing_address = models.ForeignKey('order.BillingAddress', null=True, blank=True)
  21. # Total price looks like it could be calculated by adding up the
  22. # prices of the associated lines, but in some circumstances extra
  23. # order-level charges are added and so we need to store it separately
  24. total_incl_tax = models.DecimalField(_("Order total (inc. tax)"), decimal_places=2, max_digits=12)
  25. total_excl_tax = models.DecimalField(_("Order total (excl. tax)"), decimal_places=2, max_digits=12)
  26. # Shipping charges
  27. shipping_incl_tax = models.DecimalField(_("Shipping charge (inc. tax)"), decimal_places=2, max_digits=12, default=0)
  28. shipping_excl_tax = models.DecimalField(_("Shipping charge (excl. tax)"), decimal_places=2, max_digits=12, default=0)
  29. # Not all lines are actually shipped (such as downloads), hence shipping address
  30. # is not mandatory.
  31. shipping_address = models.ForeignKey('order.ShippingAddress', null=True, blank=True)
  32. shipping_method = models.CharField(_("Shipping method"), max_length=128, null=True, blank=True)
  33. # Use this field to indicate that an order is on hold / awaiting payment
  34. status = models.CharField(_("Status"), max_length=100, null=True, blank=True)
  35. # Index added to this field for reporting
  36. date_placed = models.DateTimeField(auto_now_add=True, db_index=True)
  37. @property
  38. def basket_total_incl_tax(self):
  39. """
  40. Return basket total including tax
  41. """
  42. return self.total_incl_tax - self.shipping_incl_tax
  43. @property
  44. def basket_total_excl_tax(self):
  45. """
  46. Return basket total excluding tax
  47. """
  48. return self.total_excl_tax - self.shipping_excl_tax
  49. @property
  50. def total_before_discounts_incl_tax(self):
  51. total = D('0.00')
  52. for line in self.lines.all():
  53. total += line.line_price_before_discounts_incl_tax
  54. total += self.shipping_incl_tax
  55. return total
  56. @property
  57. def total_before_discounts_excl_tax(self):
  58. total = D('0.00')
  59. for line in self.lines.all():
  60. total += line.line_price_before_discounts_excl_tax
  61. total += self.shipping_excl_tax
  62. return total
  63. @property
  64. def total_discount_incl_tax(self):
  65. """
  66. The amount of discount this order received
  67. """
  68. discount = D('0.00')
  69. for line in self.lines.all():
  70. discount += line.discount_incl_tax
  71. return discount
  72. @property
  73. def total_discount_excl_tax(self):
  74. discount = D('0.00')
  75. for line in self.lines.all():
  76. discount += line.discount_excl_tax
  77. return discount
  78. @property
  79. def total_tax(self):
  80. return self.total_incl_tax - self.total_excl_tax
  81. @property
  82. def num_lines(self):
  83. return self.lines.count()
  84. @property
  85. def num_items(self):
  86. u"""
  87. Returns the number of items in this order.
  88. """
  89. num_items = 0
  90. for line in self.lines.all():
  91. num_items += line.quantity
  92. return num_items
  93. @property
  94. def shipping_status(self):
  95. events = self.shipping_events.all()
  96. if not len(events):
  97. return ''
  98. # Collect all events by event-type
  99. map = {}
  100. for event in events:
  101. event_name = event.event_type.name
  102. if event_name not in map:
  103. map[event_name] = []
  104. map[event_name] = list(chain(map[event_name], event.line_quantities.all()))
  105. # Determine last complete event
  106. status = _("In progress")
  107. for event_name, event_line_quantities in map.items():
  108. if self._is_event_complete(event_line_quantities):
  109. status = event_name
  110. return status
  111. def _is_event_complete(self, event_quantites):
  112. # Form map of line to quantity
  113. map = {}
  114. for event_quantity in event_quantites:
  115. line_id = event_quantity.line_id
  116. map.setdefault(line_id, 0)
  117. map[line_id] += event_quantity.quantity
  118. for line in self.lines.all():
  119. if map[line.id] != line.quantity:
  120. return False
  121. return True
  122. class Meta:
  123. abstract = True
  124. ordering = ['-date_placed',]
  125. permissions = (
  126. ("can_view", "Can view orders (eg for reporting)"),
  127. )
  128. def __unicode__(self):
  129. return u"#%s" % (self.number,)
  130. class AbstractOrderNote(models.Model):
  131. """
  132. A note against an order.
  133. This are often used for audit purposes too. IE, whenever an admin
  134. makes a change to an order, we create a note to record what happened.
  135. """
  136. order = models.ForeignKey('order.Order', related_name="notes")
  137. # These are sometimes programatically generated so don't need a
  138. # user everytime
  139. user = models.ForeignKey('auth.User', null=True)
  140. # We allow notes to be classified although this isn't always needed
  141. INFO, WARNING, ERROR = 'Info', 'Warning', 'Error'
  142. note_type = models.CharField(max_length=128, null=True)
  143. message = models.TextField()
  144. date = models.DateTimeField(auto_now_add=True)
  145. class Meta:
  146. abstract = True
  147. def __unicode__(self):
  148. return u"'%s' (%s)" % (self.message[0:50], self.user)
  149. class AbstractCommunicationEvent(models.Model):
  150. """
  151. An order-level event involving a communication to the customer, such
  152. as an confirmation email being sent.
  153. """
  154. order = models.ForeignKey('order.Order', related_name="communication_events")
  155. type = models.ForeignKey('customer.CommunicationEventType')
  156. date = models.DateTimeField(auto_now_add=True)
  157. class Meta:
  158. abstract = True
  159. def __unicode__(self):
  160. return u"'%s' event for order #%s" % (self.type.name, self.order.number)
  161. class AbstractLine(models.Model):
  162. """
  163. A order line (basically a product and a quantity)
  164. Not using a line model as it's difficult to capture and payment
  165. information when it splits across a line.
  166. """
  167. order = models.ForeignKey('order.Order', related_name='lines')
  168. # We store the partner, their SKU and the title for cases where the product has been
  169. # deleted from the catalogue. We also store the partner name in case the partner
  170. # gets deleted at a later date.
  171. partner = models.ForeignKey('partner.Partner', related_name='order_lines', blank=True, null=True, on_delete=models.SET_NULL)
  172. partner_name = models.CharField(_("Partner name"), max_length=128)
  173. partner_sku = models.CharField(_("Partner SKU"), max_length=128)
  174. title = models.CharField(_("Title"), max_length=255)
  175. # We don't want any hard links between orders and the products table so we allow
  176. # this link to be NULLable.
  177. product = models.ForeignKey('catalogue.Product', on_delete=models.SET_NULL, blank=True, null=True)
  178. quantity = models.PositiveIntegerField(default=1)
  179. # Price information (these fields are actually redundant as the information
  180. # can be calculated from the LinePrice models
  181. line_price_incl_tax = models.DecimalField(decimal_places=2, max_digits=12)
  182. line_price_excl_tax = models.DecimalField(decimal_places=2, max_digits=12)
  183. # Price information before discounts are applied
  184. line_price_before_discounts_incl_tax = models.DecimalField(decimal_places=2, max_digits=12)
  185. line_price_before_discounts_excl_tax = models.DecimalField(decimal_places=2, max_digits=12)
  186. # REPORTING FIELDS
  187. # Cost price (the price charged by the fulfilment partner for this product).
  188. unit_cost_price = models.DecimalField(decimal_places=2, max_digits=12, blank=True, null=True)
  189. # Normal site price for item (without discounts)
  190. unit_price_incl_tax = models.DecimalField(decimal_places=2, max_digits=12, blank=True, null=True)
  191. unit_price_excl_tax = models.DecimalField(decimal_places=2, max_digits=12, blank=True, null=True)
  192. # Retail price at time of purchase
  193. unit_retail_price = models.DecimalField(decimal_places=2, max_digits=12, blank=True, null=True)
  194. # Partner information
  195. partner_line_reference = models.CharField(_("Partner reference"), max_length=128, blank=True, null=True,
  196. help_text=_("This is the item number that the partner uses within their system"))
  197. partner_line_notes = models.TextField(blank=True, null=True)
  198. # Partners often want to assign some status to each line to help with their own
  199. # business processes.
  200. status = models.CharField(_("Status"), max_length=255, null=True, blank=True)
  201. # Estimated dispatch date - should be set at order time
  202. est_dispatch_date = models.DateField(blank=True, null=True)
  203. @property
  204. def description(self):
  205. """
  206. Returns a description of this line including details of any
  207. line attributes.
  208. """
  209. desc = self.title
  210. ops = []
  211. for attribute in self.attributes.all():
  212. ops.append("%s = '%s'" % (attribute.type, attribute.value))
  213. if ops:
  214. desc = "%s (%s)" % (desc, ", ".join(ops))
  215. return desc
  216. @property
  217. def discount_incl_tax(self):
  218. return self.line_price_before_discounts_incl_tax - self.line_price_incl_tax
  219. @property
  220. def discount_excl_tax(self):
  221. return self.line_price_before_discounts_excl_tax - self.line_price_excl_tax
  222. @property
  223. def shipping_status(self):
  224. u"""Returns a string summary of the shipping status of this line"""
  225. status_map = self._shipping_event_history()
  226. if not status_map:
  227. return ''
  228. events = []
  229. last_complete_event_name = None
  230. for event_dict in status_map:
  231. if event_dict['quantity'] == self.quantity:
  232. events.append(event_dict['name'])
  233. last_complete_event_name = event_dict['name']
  234. else:
  235. events.append("%s (%d/%d items)" % (event_dict['name'],
  236. event_dict['quantity'], self.quantity))
  237. if last_complete_event_name == status_map[-1]['name']:
  238. return last_complete_event_name
  239. return ', '.join(events)
  240. def has_shipping_event_occurred(self, event_type):
  241. u"""Checks whether this line has passed a given shipping event"""
  242. for event_dict in self._shipping_event_history():
  243. if event_dict['name'] == event_type.name and event_dict['quantity'] == self.quantity:
  244. return True
  245. return False
  246. @property
  247. def is_product_deleted(self):
  248. return self.product == None
  249. def _shipping_event_history(self):
  250. u"""
  251. Returns a list of shipping events"""
  252. status_map = {}
  253. for event in self.shippingevent_set.all():
  254. event_name = event.event_type.name
  255. event_quantity = event.line_quantities.get(line=self).quantity
  256. if event_name in status_map:
  257. status_map[event_name]['quantity'] += event_quantity
  258. else:
  259. status_map[event_name] = {'name': event_name, 'quantity': event_quantity}
  260. return list(status_map.values())
  261. class Meta:
  262. abstract = True
  263. verbose_name_plural = _("Order lines")
  264. def __unicode__(self):
  265. if self.product:
  266. title = self.product.title
  267. else:
  268. title = '<missing product>'
  269. return u"Product '%s', quantity '%s'" % (title, self.quantity)
  270. class AbstractLineAttribute(models.Model):
  271. u"""An attribute of a line."""
  272. line = models.ForeignKey('order.Line', related_name='attributes')
  273. option = models.ForeignKey('catalogue.Option', null=True, on_delete=models.SET_NULL, related_name="line_attributes")
  274. type = models.CharField(_("Type"), max_length=128)
  275. value = models.CharField(_("Value"), max_length=255)
  276. class Meta:
  277. abstract = True
  278. def __unicode__(self):
  279. return "%s = %s" % (self.type, self.value)
  280. class AbstractLinePrice(models.Model):
  281. u"""
  282. For tracking the prices paid for each unit within a line.
  283. This is necessary as offers can lead to units within a line
  284. having different prices. For example, one product may be sold at
  285. 50% off as it's part of an offer while the remainder are full price.
  286. """
  287. order = models.ForeignKey('order.Order', related_name='line_prices')
  288. line = models.ForeignKey('order.Line', related_name='prices')
  289. quantity = models.PositiveIntegerField(default=1)
  290. price_incl_tax = models.DecimalField(decimal_places=2, max_digits=12)
  291. price_excl_tax = models.DecimalField(decimal_places=2, max_digits=12)
  292. shipping_incl_tax = models.DecimalField(decimal_places=2, max_digits=12, default=0)
  293. shipping_excl_tax = models.DecimalField(decimal_places=2, max_digits=12, default=0)
  294. class Meta:
  295. abstract = True
  296. def __unicode__(self):
  297. return u"Line '%s' (quantity %d) price %s" % (self.line, self.quantity, self.price_incl_tax)
  298. # PAYMENT EVENTS
  299. class AbstractPaymentEventType(models.Model):
  300. """
  301. Payment events are things like 'Paid', 'Failed', 'Refunded'
  302. """
  303. name = models.CharField(max_length=128)
  304. code = models.SlugField(max_length=128)
  305. sequence_number = models.PositiveIntegerField(default=0)
  306. def save(self, *args, **kwargs):
  307. if not self.code:
  308. self.code = slugify(self.name)
  309. super(AbstractPaymentEventType, self).save(*args, **kwargs)
  310. class Meta:
  311. abstract = True
  312. verbose_name_plural = _("Payment event types")
  313. ordering = ('sequence_number',)
  314. def __unicode__(self):
  315. return self.name
  316. class AbstractPaymentEvent(models.Model):
  317. """
  318. An event is something which happens to a line such as
  319. payment being taken for 2 items, or 1 item being dispatched.
  320. """
  321. order = models.ForeignKey('order.Order', related_name='payment_events')
  322. lines = models.ManyToManyField('order.Line', through='PaymentEventQuantity')
  323. event_type = models.ForeignKey('order.PaymentEventType')
  324. date = models.DateTimeField(auto_now_add=True)
  325. class Meta:
  326. abstract = True
  327. verbose_name_plural = _("Payment events")
  328. def __unicode__(self):
  329. return u"Payment event for order #%d" % self.order
  330. class PaymentEventQuantity(models.Model):
  331. """
  332. A "through" model linking lines to payment events
  333. """
  334. event = models.ForeignKey('order.PaymentEvent', related_name='line_quantities')
  335. line = models.ForeignKey('order.Line')
  336. quantity = models.PositiveIntegerField()
  337. class AbstractShippingEvent(models.Model):
  338. u"""
  339. An event is something which happens to a group of lines such as
  340. 1 item being dispatched.
  341. """
  342. order = models.ForeignKey('order.Order', related_name='shipping_events')
  343. lines = models.ManyToManyField('order.Line', through='ShippingEventQuantity')
  344. event_type = models.ForeignKey('order.ShippingEventType')
  345. notes = models.TextField(_("Event notes"), blank=True, null=True,
  346. help_text="This could be the dispatch reference, or a tracking number")
  347. date = models.DateTimeField(auto_now_add=True)
  348. class Meta:
  349. abstract = True
  350. verbose_name_plural = _("Shipping events")
  351. ordering = ['-date']
  352. def __unicode__(self):
  353. return u"Order #%s, type %s" % (
  354. self.order.number, self.event_type)
  355. def num_affected_lines(self):
  356. return self.lines.count()
  357. class ShippingEventQuantity(models.Model):
  358. u"""A "through" model linking lines to shipping events"""
  359. event = models.ForeignKey('order.ShippingEvent', related_name='line_quantities')
  360. line = models.ForeignKey('order.Line')
  361. quantity = models.PositiveIntegerField()
  362. def _check_previous_events_are_complete(self):
  363. u"""Checks whether previous shipping events have passed"""
  364. previous_events = ShippingEventQuantity._default_manager.filter(line=self.line,
  365. event__event_type__sequence_number__lt=self.event.event_type.sequence_number)
  366. self.quantity = int(self.quantity)
  367. for event_quantities in previous_events:
  368. if event_quantities.quantity < self.quantity:
  369. raise ValueError("Invalid quantity (%d) for event type (a previous event has not been fully passed)" % self.quantity)
  370. def _check_new_quantity(self):
  371. quantity_row = ShippingEventQuantity._default_manager.filter(line=self.line,
  372. event__event_type=self.event.event_type).aggregate(Sum('quantity'))
  373. previous_quantity = quantity_row['quantity__sum']
  374. if previous_quantity == None:
  375. previous_quantity = 0
  376. if previous_quantity + self.quantity > self.line.quantity:
  377. raise ValueError("Invalid quantity (%d) for event type (total exceeds line total)" % self.quantity)
  378. def save(self, *args, **kwargs):
  379. # Default quantity to full quantity of line
  380. if not self.quantity:
  381. self.quantity = self.line.quantity
  382. self._check_previous_events_are_complete()
  383. self._check_new_quantity()
  384. super(ShippingEventQuantity, self).save(*args, **kwargs)
  385. def __unicode__(self):
  386. return "%s - quantity %d" % (self.line.product, self.quantity)
  387. class AbstractShippingEventType(models.Model):
  388. u"""Shipping events are things like 'OrderPlaced', 'Acknowledged', 'Dispatched', 'Refunded'"""
  389. # Code is used in forms
  390. code = models.CharField(max_length=128)
  391. # Name is the friendly description of an event
  392. name = models.CharField(max_length=255)
  393. # Code is used in forms
  394. code = models.SlugField(max_length=128)
  395. is_required = models.BooleanField(default=True, help_text="This event must be passed before the next shipping event can take place")
  396. # The normal order in which these shipping events take place
  397. sequence_number = models.PositiveIntegerField(default=0)
  398. def save(self, *args, **kwargs):
  399. if not self.code:
  400. self.code = slugify(self.name)
  401. super(AbstractShippingEventType, self).save(*args, **kwargs)
  402. class Meta:
  403. abstract = True
  404. verbose_name_plural = _("Shipping event types")
  405. ordering = ('sequence_number',)
  406. def __unicode__(self):
  407. return self.name
  408. class AbstractOrderDiscount(models.Model):
  409. """
  410. A discount against an order.
  411. Normally only used for display purposes so an order can be listed with discounts displayed
  412. separately even though in reality, the discounts are applied at the line level.
  413. """
  414. order = models.ForeignKey('order.Order', related_name="discounts")
  415. offer = models.ForeignKey('offer.ConditionalOffer', null=True, on_delete=models.SET_NULL)
  416. voucher = models.ForeignKey('voucher.Voucher', related_name="discount_vouchers", null=True, on_delete=models.SET_NULL)
  417. voucher_code = models.CharField(_("Code"), max_length=128, db_index=True, null=True)
  418. amount = models.DecimalField(decimal_places=2, max_digits=12, default=0)
  419. class Meta:
  420. abstract = True
  421. def __unicode__(self):
  422. return u"Discount of %r from order %s" % (self.amount, self.order)
  423. def description(self):
  424. if self.voucher_code:
  425. return self.voucher_code
  426. return self.offer.name