您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

processing.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from django.db.models.loading import get_model
  2. from oscar.apps.order.exceptions import InvalidShippingEvent
  3. ShippingEventQuantity = get_model('order', 'ShippingEventQuantity')
  4. PaymentEventQuantity = get_model('order', 'PaymentEventQuantity')
  5. class EventHandler(object):
  6. def handle_order_status_change(self, order, new_status):
  7. """
  8. Handle a requested order status change
  9. """
  10. order.set_status(new_status)
  11. def handle_shipping_event(self, order, event_type, lines, line_quantities, **kwargs):
  12. """
  13. Handle a shipping event for a given order.
  14. This might involve taking payment, sending messages and
  15. creating the event models themeselves. You will generally want to
  16. override this method to implement the specifics of you order processing
  17. pipeline.
  18. """
  19. self.create_shipping_event(order, event_type, lines, line_quantities, **kwargs)
  20. def has_any_line_passed_shipping_event(self, order, lines, line_quantities, event_name):
  21. """
  22. Test whether any one of the lines passed has been through the event
  23. specified.
  24. """
  25. events = order.shipping_events.filter(event_type__name=event_name)
  26. remaining_qtys = [line.quantity - qty for line, qty in zip(lines, line_quantities)]
  27. spare_line_qtys = dict(zip([line.id for line in lines], remaining_qtys))
  28. for event in events:
  29. for line_qty in event.line_quantities.all():
  30. line_id = line_qty.line.id
  31. if line_id in spare_line_qtys:
  32. spare_line_qtys[line_id] -= line_qty.quantity
  33. return any(map(lambda x: x<0, spare_line_qtys.values()))
  34. def have_lines_passed_shipping_event(self, order, lines, line_quantities, event_name):
  35. """
  36. Test whether the passed lines and quantities have been through the
  37. specified shipping event.
  38. This is useful for validating if certain shipping events are allowed (ie
  39. you can't return something before it has shipped).
  40. """
  41. events = order.shipping_events.filter(event_type__name=event_name)
  42. required_line_qtys = dict(zip([line.id for line in lines], line_quantities))
  43. for event in events:
  44. for line_qty in event.line_quantities.all():
  45. line_id = line_qty.line.id
  46. if line_id in required_line_qtys:
  47. required_line_qtys[line_id] -= line_qty.quantity
  48. return not any(map(lambda x: x>0, required_line_qtys.values()))
  49. def handle_payment_event(self, order, event_type, amount, lines=None,
  50. line_quantities=None, **kwargs):
  51. """
  52. Handle a payment event for a given order.
  53. """
  54. self.create_payment_event(order, event_type, amount, lines, line_quantities, **kwargs)
  55. def are_stock_allocations_available(self, lines, line_quantities):
  56. """
  57. Check whether stock records still have enough stock to honour the
  58. requested allocations.
  59. """
  60. for line, qty in zip(lines, line_quantities):
  61. record = line.product.stockrecord
  62. if not record.is_allocation_consumption_possible(qty):
  63. return False
  64. return True
  65. def consume_stock_allocations(self, order, lines, line_quantities):
  66. """
  67. Consume the stock allocations for the passed lines
  68. """
  69. for line, qty in zip(lines, line_quantities):
  70. if line.product:
  71. line.product.stockrecord.consume_allocation(qty)
  72. def cancel_stock_allocations(self, order, lines, line_quantities):
  73. """
  74. Cancel the stock allocations for the passed lines
  75. """
  76. for line, qty in zip(lines, line_quantities):
  77. line.product.stockrecord.cancel_allocation(qty)
  78. def create_shipping_event(self, order, event_type, lines, line_quantities,
  79. **kwargs):
  80. reference = kwargs.get('reference', None)
  81. event = order.shipping_events.create(event_type=event_type,
  82. notes=reference)
  83. try:
  84. for line, quantity in zip(lines, line_quantities):
  85. ShippingEventQuantity.objects.create(event=event,
  86. line=line,
  87. quantity=quantity)
  88. except InvalidShippingEvent:
  89. event.delete()
  90. raise
  91. def create_payment_event(self, order, event_type, amount, lines=None,
  92. line_quantities=None, **kwargs):
  93. event = order.payment_events.create(event_type=event_type, amount=amount)
  94. if lines and line_quantities:
  95. for line, quantity in zip(lines, line_quantities):
  96. PaymentEventQuantity.objects.create(event=event,
  97. line=line,
  98. quantity=quantity)
  99. def create_communication_event(self, order, event_type):
  100. order.communication_events.create(event_type=event_type)
  101. def create_note(self, order, message, note_type='System'):
  102. order.notes.create(message=message, note_type=note_type)