Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

abstract_models.py 23KB

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