<s> from decimal import Decimal as D <EOL> from django . conf import settings <EOL> from django . contrib . sites . models import Site <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from oscar . core . loading import get_class , get_model <EOL> from . import exceptions <EOL> Order = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Line = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> OrderDiscount = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> order_placed = get_class ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class OrderNumberGenerator ( object ) : <EOL> """<STR_LIT>""" <EOL> def order_number ( self , basket ) : <EOL> """<STR_LIT>""" <EOL> return <NUM_LIT> + basket . id <EOL> class OrderCreator ( object ) : <EOL> """<STR_LIT>""" <EOL> def place_order ( self , basket , total , <EOL> shipping_method , shipping_charge , user = None , <EOL> shipping_address = None , billing_address = None , <EOL> order_number = None , status = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if basket . is_empty : <EOL> raise ValueError ( _ ( "<STR_LIT>" ) ) <EOL> if not order_number : <EOL> generator = OrderNumberGenerator ( ) <EOL> order_number = generator . order_number ( basket ) <EOL> if not status and hasattr ( settings , '<STR_LIT>' ) : <EOL> status = getattr ( settings , '<STR_LIT>' ) <EOL> try : <EOL> Order . _default_manager . get ( number = order_number ) <EOL> except Order . DoesNotExist : <EOL> pass <EOL> else : <EOL> raise ValueError ( _ ( "<STR_LIT>" ) <EOL> % order_number ) <EOL> order = self . create_order_model ( <EOL> user , basket , shipping_address , shipping_method , shipping_charge , <EOL> billing_address , total , order_number , status , ** kwargs ) <EOL> for line in basket . all_lines ( ) : <EOL> self . create_line_models ( order , line ) <EOL> self . update_stock_records ( line ) <EOL> for application in basket . offer_applications : <EOL> application [ '<STR_LIT:message>' ] = application [ '<STR_LIT>' ] . apply_deferred_benefit ( basket , order , <EOL> application ) <EOL> if application [ '<STR_LIT:result>' ] . affects_shipping : <EOL> shipping_discount = shipping_method . discount ( basket ) <EOL> if shipping_discount <= D ( '<STR_LIT>' ) : <EOL> continue <EOL> application [ '<STR_LIT>' ] = shipping_discount <EOL> self . create_discount_model ( order , application ) <EOL> self . record_discount ( application ) <EOL> for voucher in basket . vouchers . all ( ) : <EOL> self . record_voucher_usage ( order , voucher , user ) <EOL> order_placed . send ( sender = self , order = order , user = user ) <EOL> return order <EOL> def create_order_model ( self , user , basket , shipping_address , <EOL> shipping_method , shipping_charge , billing_address , <EOL> total , order_number , status , ** extra_order_fields ) : <EOL> """<STR_LIT>""" <EOL> order_data = { '<STR_LIT>' : basket , <EOL> '<STR_LIT>' : order_number , <EOL> '<STR_LIT>' : Site . _default_manager . get_current ( ) , <EOL> '<STR_LIT>' : total . currency , <EOL> '<STR_LIT>' : total . incl_tax , <EOL> '<STR_LIT>' : total . excl_tax , <EOL> '<STR_LIT>' : shipping_charge . incl_tax , <EOL> '<STR_LIT>' : shipping_charge . excl_tax , <EOL> '<STR_LIT>' : shipping_method . name , <EOL> '<STR_LIT>' : shipping_method . code } <EOL> if shipping_address : <EOL> order_data [ '<STR_LIT>' ] = shipping_address <EOL> if billing_address : <EOL> order_data [ '<STR_LIT>' ] = billing_address <EOL> if user and user . is_authenticated ( ) : <EOL> order_data [ '<STR_LIT>' ] = user . id <EOL> if status : <EOL> order_data [ '<STR_LIT:status>' ] = status <EOL> if extra_order_fields : <EOL> order_data . update ( extra_order_fields ) <EOL> order = Order ( ** order_data ) <EOL> order . save ( ) <EOL> return order <EOL> def create_line_models ( self , order , basket_line , extra_line_fields = None ) : <EOL> """<STR_LIT>""" <EOL> product = basket_line . product <EOL> stockrecord = basket_line . stockrecord <EOL> if not stockrecord : <EOL> raise exceptions . UnableToPlaceOrder ( <EOL> "<STR_LIT>" % basket_line . id ) <EOL> partner = stockrecord . partner <EOL> line_data = { <EOL> '<STR_LIT>' : order , <EOL> '<STR_LIT>' : partner , <EOL> '<STR_LIT>' : partner . name , <EOL> '<STR_LIT>' : stockrecord . partner_sku , <EOL> '<STR_LIT>' : stockrecord , <EOL> '<STR_LIT>' : product , <EOL> '<STR_LIT:title>' : product . get_title ( ) , <EOL> '<STR_LIT>' : product . upc , <EOL> '<STR_LIT>' : basket_line . quantity , <EOL> '<STR_LIT>' : <EOL> basket_line . line_price_excl_tax_incl_discounts , <EOL> '<STR_LIT>' : <EOL> basket_line . line_price_incl_tax_incl_discounts , <EOL> '<STR_LIT>' : <EOL> basket_line . line_price_excl_tax , <EOL> '<STR_LIT>' : <EOL> basket_line . line_price_incl_tax , <EOL> '<STR_LIT>' : stockrecord . cost_price , <EOL> '<STR_LIT>' : basket_line . unit_price_incl_tax , <EOL> '<STR_LIT>' : basket_line . unit_price_excl_tax , <EOL> '<STR_LIT>' : stockrecord . price_retail , <EOL> '<STR_LIT>' : <EOL> basket_line . purchase_info . availability . dispatch_date <EOL> } <EOL> extra_line_fields = extra_line_fields or { } <EOL> if hasattr ( settings , '<STR_LIT>' ) : <EOL> if not ( extra_line_fields and '<STR_LIT:status>' in extra_line_fields ) : <EOL> extra_line_fields [ '<STR_LIT:status>' ] = getattr ( <EOL> settings , '<STR_LIT>' ) <EOL> if extra_line_fields : <EOL> line_data . update ( extra_line_fields ) <EOL> order_line = Line . _default_manager . create ( ** line_data ) <EOL> self . create_line_price_models ( order , order_line , basket_line ) <EOL> self . create_line_attributes ( order , order_line , basket_line ) <EOL> self . create_additional_line_models ( order , order_line , basket_line ) <EOL> return order_line <EOL> def update_stock_records ( self , line ) : <EOL> """<STR_LIT>""" <EOL> if line . product . get_product_class ( ) . track_stock : <EOL> line . stockrecord . allocate ( line . quantity ) <EOL> def create_additional_line_models ( self , order , order_line , basket_line ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def create_line_price_models ( self , order , order_line , basket_line ) : <EOL> """<STR_LIT>""" <EOL> breakdown = basket_line . get_price_breakdown ( ) <EOL> for price_incl_tax , price_excl_tax , quantity in breakdown : <EOL> order_line . prices . create ( <EOL> order = order , <EOL> quantity = quantity , <EOL> price_incl_tax = price_incl_tax , <EOL> price_excl_tax = price_excl_tax ) <EOL> def create_line_attributes ( self , order , order_line , basket_line ) : <EOL> """<STR_LIT>""" <EOL> for attr in basket_line . attributes . all ( ) : <EOL> order_line . attributes . create ( <EOL> option = attr . option , <EOL> type = attr . option . code , <EOL> value = attr . value ) <EOL> def create_discount_model ( self , order , discount ) : <EOL> """<STR_LIT>""" <EOL> order_discount = OrderDiscount ( <EOL> order = order , <EOL> message = discount [ '<STR_LIT:message>' ] or '<STR_LIT>' , <EOL> offer_id = discount [ '<STR_LIT>' ] . id , <EOL> frequency = discount [ '<STR_LIT>' ] , <EOL> amount = discount [ '<STR_LIT>' ] ) <EOL> result = discount [ '<STR_LIT:result>' ] <EOL> if result . affects_shipping : <EOL> order_discount . category = OrderDiscount . SHIPPING <EOL> elif result . affects_post_order : <EOL> order_discount . category = OrderDiscount . DEFERRED <EOL> voucher = discount . get ( '<STR_LIT>' , None ) <EOL> if voucher : <EOL> order_discount . voucher_id = voucher . id <EOL> order_discount . voucher_code = voucher . code <EOL> order_discount . save ( ) <EOL> def record_discount ( self , discount ) : <EOL> discount [ '<STR_LIT>' ] . record_usage ( discount ) <EOL> if '<STR_LIT>' in discount and discount [ '<STR_LIT>' ] : <EOL> discount [ '<STR_LIT>' ] . record_discount ( discount ) <EOL> def record_voucher_usage ( self , order , voucher , user ) : <EOL> """<STR_LIT>""" <EOL> voucher . record_usage ( order , user ) </s>
