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

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