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

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