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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. from django.contrib.sites.models import Site
  2. from oscar.core.loading import import_module
  3. import_module('order.models', ['ShippingAddress', 'Order', 'Line',
  4. 'LinePrice', 'LineAttribute', 'OrderDiscount'], locals())
  5. import_module('order.signals', ['order_placed'], locals())
  6. class OrderNumberGenerator(object):
  7. u"""
  8. Simple object for generating order numbers.
  9. We need this as the order number is often required for payment
  10. which takes place before the order model has been created.
  11. """
  12. def order_number(self, basket):
  13. u"""
  14. Return an order number for a given basket
  15. """
  16. return 100000 + basket.id
  17. class OrderCreator(object):
  18. u"""
  19. Places the order by writing out the various models
  20. """
  21. def place_order(self, user, basket, shipping_address, shipping_method,
  22. billing_address, total_incl_tax, total_excl_tax, order_number=None):
  23. u"""
  24. Placing an order involves creating all the relevant models based on the
  25. basket and session data.
  26. """
  27. if not order_number:
  28. generator = OrderNumberGenerator()
  29. order_number = generator.order_number(basket)
  30. order = self._create_order_model(user, basket, shipping_address,
  31. shipping_method, billing_address, total_incl_tax,
  32. total_excl_tax, order_number)
  33. for line in basket.all_lines():
  34. self._create_line_models(order, line)
  35. for discount in basket.discounts:
  36. self._create_discount_model(order, discount)
  37. for voucher in basket.vouchers.all():
  38. self._record_voucher_usage(order, voucher, user)
  39. basket.set_as_submitted()
  40. # Send signal for analytics to pick up
  41. order_placed.send(sender=self, order=order, user=user)
  42. return order
  43. def _create_order_model(self, user, basket, shipping_address, shipping_method,
  44. billing_address, total_incl_tax, total_excl_tax, order_number):
  45. u"""Creates an order model."""
  46. order_data = {'basket': basket,
  47. 'number': order_number,
  48. 'site': Site._default_manager.get_current(),
  49. 'total_incl_tax': total_incl_tax,
  50. 'total_excl_tax': total_excl_tax,
  51. 'shipping_address': shipping_address,
  52. 'shipping_incl_tax': shipping_method.basket_charge_incl_tax(),
  53. 'shipping_excl_tax': shipping_method.basket_charge_excl_tax(),
  54. 'shipping_method': shipping_method.name}
  55. if billing_address:
  56. order_data['billing_address'] = billing_address
  57. if user.is_authenticated():
  58. order_data['user_id'] = user.id
  59. order = Order(**order_data)
  60. order.save()
  61. return order
  62. def _get_partner_for_product(self, product):
  63. u"""Returns the partner for a product"""
  64. if product.has_stockrecord:
  65. return product.stockrecord.partner
  66. raise AttributeError("No partner found for product '%s'" % product)
  67. def _create_line_models(self, order, basket_line):
  68. u"""Creates the batch line model."""
  69. partner = self._get_partner_for_product(basket_line.product)
  70. stockrecord = basket_line.product.stockrecord
  71. order_line = Line(order=order,
  72. # Partner details
  73. partner=partner,
  74. partner_name=partner.name,
  75. partner_sku=stockrecord.partner_sku,
  76. # Product details
  77. product=basket_line.product,
  78. title=basket_line.product.get_title(),
  79. quantity=basket_line.quantity,
  80. # Price details
  81. line_price_excl_tax=basket_line.line_price_excl_tax_and_discounts,
  82. line_price_incl_tax=basket_line.line_price_incl_tax_and_discounts,
  83. line_price_before_discounts_excl_tax=basket_line.line_price_excl_tax,
  84. line_price_before_discounts_incl_tax=basket_line.line_price_incl_tax,
  85. # Reporting details
  86. unit_cost_price = stockrecord.cost_price,
  87. unit_site_price = stockrecord.price_incl_tax,
  88. unit_retail_price = stockrecord.price_retail,
  89. # Shipping details
  90. est_dispatch_date = basket_line.product.stockrecord.dispatch_date
  91. )
  92. order_line.save()
  93. self._create_line_price_models(order, order_line, basket_line)
  94. self._create_line_attributes(order, order_line, basket_line)
  95. def _create_line_price_models(self, order, order_line, basket_line):
  96. u"""Creates the batch line price models"""
  97. breakdown = basket_line.get_price_breakdown()
  98. for price_incl_tax, price_excl_tax, quantity in breakdown:
  99. LinePrice._default_manager.create(order=order,
  100. line=order_line,
  101. quantity=quantity,
  102. price_incl_tax=price_incl_tax,
  103. price_excl_tax=price_excl_tax)
  104. def _create_line_attributes(self, order, order_line, basket_line):
  105. u"""Creates the batch line attributes."""
  106. for attr in basket_line.attributes.all():
  107. LineAttribute._default_manager.create(line=order_line,
  108. option=attr.option,
  109. type=attr.option.code,
  110. value=attr.value)
  111. def _create_discount_model(self, order, discount):
  112. u"""
  113. Creates an order discount model for each discount attached to the basket.
  114. """
  115. order_discount = OrderDiscount(order=order, offer=discount['offer'], amount=discount['discount'])
  116. if discount['voucher']:
  117. order_discount.voucher = discount['voucher']
  118. order_discount.voucher_code = discount['voucher'].code
  119. order_discount.save()
  120. def _record_voucher_usage(self, order, voucher, user):
  121. u"""
  122. Updates the models that care about this voucher.
  123. """
  124. voucher.record_usage(order, user)
  125. voucher.num_orders += 1
  126. voucher.save()