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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from django.db import models
  2. from django.contrib.auth.models import User
  3. from django.utils.translation import ugettext_lazy as _
  4. class AbstractOrder(models.Model):
  5. """
  6. An order
  7. """
  8. number = models.IntegerField(_("Order number"))
  9. basket = models.ForeignKey('basket.Basket')
  10. customer = models.ForeignKey(User, related_name='orders')
  11. billing_address = models.ForeignKey('order.BillingAddress')
  12. # Totals
  13. total_incl_tax = models.DecimalField(decimal_places=2, max_digits=12)
  14. delivery_incl_tax = models.DecimalField(decimal_places=2, max_digits=12)
  15. date_placed = models.DateTimeField(auto_now_add=True)
  16. class Meta:
  17. abstract = True
  18. def __unicode__(self):
  19. return "#%d (customer: %s, amount: %.2f)" % (self.number, self.customer.username, self.total_incl_tax)
  20. class AbstractBatch(models.Model):
  21. """
  22. A batch of items from a single fulfillment partner
  23. This is a set of order lines which are fulfilled by a single partner
  24. """
  25. order = models.ForeignKey('order.Order')
  26. partner = models.ForeignKey('stock.Partner')
  27. delivery_method = models.CharField(_("Delivery method"), max_length=128)
  28. # Not all batches are actually delivered (such as downloads)
  29. delivery_address = models.ForeignKey('order.DeliveryAddress', null=True, blank=True)
  30. # Whether the batch should be dispatched in one go, or as they become available
  31. dispatch_option = models.CharField(max_length=128, null=True, blank=True)
  32. def num_items(self):
  33. return len(self.lines.all())
  34. class Meta:
  35. abstract = True
  36. verbose_name_plural = _("Batches")
  37. def __unicode__(self):
  38. return "%s batch for order #%d" % (self.partner.name, self.order.number)
  39. class AbstractBatchLine(models.Model):
  40. """
  41. A line within a batch.
  42. Not using a line model as it's difficult to capture and payment
  43. information when it splits across a line.
  44. """
  45. batch = models.ForeignKey('order.Batch', related_name='lines')
  46. # As all order items are stored in their own row, this ID is used to
  47. # determine which are part of the same line.
  48. line_id = models.CharField(max_length=128)
  49. product = models.ForeignKey('product.Item')
  50. quantity = models.PositiveIntegerField(default=1)
  51. # Price information
  52. line_price_incl_tax = models.DecimalField(decimal_places=2, max_digits=12)
  53. line_price_excl_tax = models.DecimalField(decimal_places=2, max_digits=12)
  54. unit_price_incl_tax = models.DecimalField(decimal_places=2, max_digits=12)
  55. unit_price_excl_tax = models.DecimalField(decimal_places=2, max_digits=12)
  56. delivery_incl_tax = models.DecimalField(decimal_places=2, max_digits=12)
  57. delivery_excl_tax = models.DecimalField(decimal_places=2, max_digits=12)
  58. # Partner information
  59. partner_reference = models.CharField(max_length=128, blank=True, null=True,
  60. help_text=_("This is the item number that the partner uses within their system"))
  61. partner_notes = models.TextField(blank=True, null=True)
  62. class Meta:
  63. abstract = True
  64. verbose_name_plural = _("Batch lines")
  65. def __unicode__(self):
  66. return "Product '%s', quantity '%s'" % (self.product, self.quantity)
  67. class AbstractBatchLineEvent(models.Model):
  68. """
  69. An event is something which happens to a line such as
  70. payment being taken for 2 items, or 1 item being dispatched.
  71. """
  72. line = models.ForeignKey('order.BatchLine')
  73. quantity = models.PositiveIntegerField(default=1)
  74. event_type = models.ForeignKey('order.BatchLineEventType')
  75. notes = models.TextField(blank=True, null=True)
  76. date = models.DateTimeField(auto_now_add=True)
  77. class Meta:
  78. abstract = True
  79. verbose_name_plural = _("Batch line events")
  80. def __unicode__(self):
  81. return "Order #%d, batch #%d, line %s: %d items %s" % (
  82. self.line.batch.order.number, self.line.batch.id, self.line.line_id, self.quantity, self.event_type)
  83. class AbstractBatchLineEventType(models.Model):
  84. """
  85. Event types: eg Paid, Cancelled, Dispatched, Returned
  86. """
  87. name = models.CharField(max_length=128)
  88. class Meta:
  89. abstract = True
  90. verbose_name_plural = _("Batch line event types")
  91. def __unicode__(self):
  92. return self.name
  93. class AbstractBatchLineAttribute(models.Model):
  94. """
  95. An attribute of a batch line.
  96. """
  97. line = models.ForeignKey('order.BatchLine', related_name='attributes')
  98. type = models.CharField(max_length=128)
  99. value = models.CharField(max_length=255)
  100. class Meta:
  101. abstract = True
  102. def __unicode__(self):
  103. return "%s = %s" % (self.type, self.value)