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.

processing.py 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.
  16. """
  17. self.create_shipping_event(order, event_type, lines, line_quantities, **kwargs)
  18. def handle_payment_event(self, order, event_type, amount, lines=None,
  19. line_quantities=None, **kwargs):
  20. """
  21. Handle a payment event for a given order.
  22. """
  23. self.create_payment_event(order, event_type, amount, lines, line_quantities, **kwargs)
  24. def consume_stock_allocations(order, lines, line_quantities):
  25. """
  26. Consume the stock allocations for the passed lines
  27. """
  28. for line, qty in zip(lines, line_quantities):
  29. line.product.stockrecord.consume_allocation(qty)
  30. def cancel_stock_allocations(order, lines, line_quantities):
  31. """
  32. Cancel the stock allocations for the passed lines
  33. """
  34. for line, qty in zip(lines, line_quantities):
  35. line.product.stockrecord.cancel_allocation(qty)
  36. def create_shipping_event(self, order, event_type, lines, line_quantities,
  37. **kwargs):
  38. reference = kwargs.get('reference', None)
  39. event = order.shipping_events.create(event_type=event_type,
  40. notes=reference)
  41. try:
  42. for line, quantity in zip(lines, line_quantities):
  43. ShippingEventQuantity.objects.create(event=event,
  44. line=line,
  45. quantity=quantity)
  46. except InvalidShippingEvent:
  47. event.delete()
  48. raise
  49. def create_payment_event(self, order, event_type, amount, lines=None,
  50. line_quantities=None, **kwargs):
  51. event = order.payment_events.create(event_type=event_type, amount=amount)
  52. if lines and line_quantities:
  53. for line, quantity in zip(lines, line_quantities):
  54. PaymentEventQuantity.objects.create(event=event,
  55. line=line,
  56. quantity=quantity)
  57. def create_communication_event(self, order, event_type):
  58. order.communication_events.create(event_type=event_type)
  59. def create_note(self, order, message, note_type='System'):
  60. order.notes.create(message=message, note_type=note_type)