<s> from __future__ import absolute_import , unicode_literals <EOL> from decimal import Decimal , ROUND_HALF_UP <EOL> import plata <EOL> from plata . discount . models import DiscountBase <EOL> class ProcessorBase ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , shared_state ) : <EOL> self . shared_state = shared_state <EOL> def split_cost ( self , cost_incl_tax , tax_rate ) : <EOL> """<STR_LIT>""" <EOL> cost_incl_tax , tax_rate = Decimal ( cost_incl_tax ) , Decimal ( tax_rate ) <EOL> cost_excl_tax = cost_incl_tax / ( <NUM_LIT:1> + tax_rate / <NUM_LIT:100> ) <EOL> return cost_excl_tax , cost_incl_tax - cost_excl_tax <EOL> def add_tax_details ( self , tax_details , tax_rate , price , discount , <EOL> tax_amount ) : <EOL> """<STR_LIT>""" <EOL> zero = Decimal ( '<STR_LIT>' ) <EOL> discount = discount or zero <EOL> row = tax_details . setdefault ( tax_rate , { <EOL> '<STR_LIT>' : zero , <EOL> '<STR_LIT>' : zero , <EOL> '<STR_LIT>' : tax_rate , <EOL> '<STR_LIT>' : zero , <EOL> '<STR_LIT>' : zero , <EOL> } ) <EOL> row [ '<STR_LIT>' ] += price <EOL> row [ '<STR_LIT>' ] += discount <EOL> row [ '<STR_LIT>' ] += tax_amount <EOL> row [ '<STR_LIT>' ] += price - discount + tax_amount <EOL> def set_processor_value ( self , group , key , value ) : <EOL> self . shared_state . setdefault ( group , { } ) [ key ] = value <EOL> def get_processor_value ( self , group , key = None ) : <EOL> dic = self . shared_state . get ( group , { } ) <EOL> if key : <EOL> return dic . get ( key ) <EOL> return dic <EOL> def process ( self , order , items ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class InitializeOrderProcessor ( ProcessorBase ) : <EOL> """<STR_LIT>""" <EOL> def process ( self , order , items ) : <EOL> order . items_subtotal = Decimal ( '<STR_LIT>' ) <EOL> order . items_tax = Decimal ( '<STR_LIT>' ) <EOL> order . items_discount = Decimal ( '<STR_LIT>' ) <EOL> for item in items : <EOL> item . _line_item_price = item . quantity * item . _unit_price <EOL> item . _line_item_discount = Decimal ( '<STR_LIT>' ) <EOL> class DiscountProcessor ( ProcessorBase ) : <EOL> """<STR_LIT>""" <EOL> def process ( self , order , items ) : <EOL> remaining = Decimal ( '<STR_LIT>' ) <EOL> for applied in order . applied_discounts . exclude ( <EOL> type = DiscountBase . MEANS_OF_PAYMENT ) : <EOL> applied . apply ( order , items ) <EOL> remaining += applied . remaining <EOL> discounts = order . data . get ( '<STR_LIT>' , { } ) <EOL> discounts [ '<STR_LIT>' ] = remaining <EOL> order . data [ '<STR_LIT>' ] = discounts <EOL> class MeansOfPaymentDiscountProcessor ( ProcessorBase ) : <EOL> """<STR_LIT>""" <EOL> def process ( self , order , items ) : <EOL> remaining = Decimal ( '<STR_LIT>' ) <EOL> for applied in order . applied_discounts . filter ( <EOL> type = DiscountBase . MEANS_OF_PAYMENT ) : <EOL> applied . apply ( order , items ) <EOL> remaining += applied . remaining <EOL> discounts = order . data . get ( '<STR_LIT>' , { } ) <EOL> discounts [ '<STR_LIT>' ] = remaining <EOL> order . data [ '<STR_LIT>' ] = discounts <EOL> class TaxProcessor ( ProcessorBase ) : <EOL> """<STR_LIT>""" <EOL> def process ( self , order , items ) : <EOL> tax_details = { } <EOL> for item in items : <EOL> taxable = item . _line_item_price - ( item . _line_item_discount or <NUM_LIT:0> ) <EOL> item . _line_item_tax = ( taxable * item . tax_rate / <NUM_LIT:100> ) . quantize ( <EOL> Decimal ( '<STR_LIT>' ) ) <EOL> self . add_tax_details ( <EOL> tax_details , <EOL> item . tax_rate , <EOL> item . _line_item_price , <EOL> item . _line_item_discount , <EOL> item . _line_item_tax , <EOL> ) <EOL> order . data [ '<STR_LIT>' ] = list ( tax_details . items ( ) ) <EOL> class ItemSummationProcessor ( ProcessorBase ) : <EOL> """<STR_LIT>""" <EOL> def process ( self , order , items ) : <EOL> for item in items : <EOL> order . items_subtotal += item . _line_item_price <EOL> order . items_discount += item . _line_item_discount or <NUM_LIT:0> <EOL> order . items_tax += item . _line_item_tax <EOL> self . set_processor_value ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> order . items_subtotal - order . items_discount + order . items_tax ) <EOL> class ZeroShippingProcessor ( ProcessorBase ) : <EOL> """<STR_LIT>""" <EOL> def process ( self , order , items ) : <EOL> order . shipping_cost = Decimal ( '<STR_LIT>' ) <EOL> order . shipping_discount = Decimal ( '<STR_LIT>' ) <EOL> order . shipping_tax = Decimal ( '<STR_LIT>' ) <EOL> self . set_processor_value ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> class FixedAmountShippingProcessor ( ProcessorBase ) : <EOL> """<STR_LIT>""" <EOL> def process ( self , order , items ) : <EOL> cost = plata . settings . PLATA_SHIPPING_FIXEDAMOUNT [ '<STR_LIT>' ] <EOL> tax = plata . settings . PLATA_SHIPPING_FIXEDAMOUNT [ '<STR_LIT>' ] <EOL> order . shipping_cost , __ = self . split_cost ( cost , tax ) <EOL> order . shipping_discount = min ( <EOL> order . discount_remaining , <EOL> order . shipping_cost , <EOL> ) <EOL> order . shipping_tax = tax / <NUM_LIT:100> * ( <EOL> order . shipping_cost - order . shipping_discount ) <EOL> self . set_processor_value ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> order . shipping_cost - order . shipping_discount <EOL> + order . shipping_tax ) <EOL> tax_details = dict ( order . data . get ( '<STR_LIT>' , [ ] ) ) <EOL> self . add_tax_details ( <EOL> tax_details , tax , order . shipping_cost , <EOL> order . shipping_discount , order . shipping_tax ) <EOL> order . data [ '<STR_LIT>' ] = list ( tax_details . items ( ) ) <EOL> class ApplyRemainingDiscountToShippingProcessor ( ProcessorBase ) : <EOL> """<STR_LIT>""" <EOL> def process ( self , order , items ) : <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> class OrderSummationProcessor ( ProcessorBase ) : <EOL> """<STR_LIT>""" <EOL> def process ( self , order , items ) : <EOL> """<STR_LIT>""" <EOL> total = sum ( <EOL> self . get_processor_value ( '<STR_LIT>' ) . values ( ) , <EOL> Decimal ( '<STR_LIT>' ) , <EOL> ) <EOL> order . total = total . quantize ( Decimal ( '<STR_LIT>' ) , rounding = ROUND_HALF_UP ) </s>
