<s> from decimal import Decimal as D <EOL> class OfferApplications ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . applications = { } <EOL> def __iter__ ( self ) : <EOL> return self . applications . values ( ) . __iter__ ( ) <EOL> def __len__ ( self ) : <EOL> return len ( self . applications ) <EOL> def add ( self , offer , result ) : <EOL> if offer . id not in self . applications : <EOL> self . applications [ offer . id ] = { <EOL> '<STR_LIT>' : offer , <EOL> '<STR_LIT:result>' : result , <EOL> '<STR_LIT:name>' : offer . name , <EOL> '<STR_LIT:description>' : result . description , <EOL> '<STR_LIT>' : offer . get_voucher ( ) , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : D ( '<STR_LIT>' ) } <EOL> self . applications [ offer . id ] [ '<STR_LIT>' ] += result . discount <EOL> self . applications [ offer . id ] [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> @ property <EOL> def offer_discounts ( self ) : <EOL> """<STR_LIT>""" <EOL> discounts = [ ] <EOL> for application in self . applications . values ( ) : <EOL> if not application [ '<STR_LIT>' ] and application [ '<STR_LIT>' ] > <NUM_LIT:0> : <EOL> discounts . append ( application ) <EOL> return discounts <EOL> @ property <EOL> def voucher_discounts ( self ) : <EOL> """<STR_LIT>""" <EOL> discounts = [ ] <EOL> for application in self . applications . values ( ) : <EOL> if application [ '<STR_LIT>' ] and application [ '<STR_LIT>' ] > <NUM_LIT:0> : <EOL> discounts . append ( application ) <EOL> return discounts <EOL> @ property <EOL> def shipping_discounts ( self ) : <EOL> """<STR_LIT>""" <EOL> discounts = [ ] <EOL> for application in self . applications . values ( ) : <EOL> if application [ '<STR_LIT:result>' ] . affects_shipping : <EOL> discounts . append ( application ) <EOL> return discounts <EOL> @ property <EOL> def grouped_voucher_discounts ( self ) : <EOL> """<STR_LIT>""" <EOL> voucher_discounts = { } <EOL> for application in self . voucher_discounts : <EOL> voucher = application [ '<STR_LIT>' ] <EOL> if voucher . code not in voucher_discounts : <EOL> voucher_discounts [ voucher . code ] = { <EOL> '<STR_LIT>' : voucher , <EOL> '<STR_LIT>' : application [ '<STR_LIT>' ] , <EOL> } <EOL> else : <EOL> voucher_discounts [ voucher . code ] += application . discount <EOL> return voucher_discounts . values ( ) <EOL> @ property <EOL> def post_order_actions ( self ) : <EOL> """<STR_LIT>""" <EOL> applications = [ ] <EOL> for application in self . applications . values ( ) : <EOL> if application [ '<STR_LIT:result>' ] . affects_post_order : <EOL> applications . append ( application ) <EOL> return applications <EOL> @ property <EOL> def offers ( self ) : <EOL> """<STR_LIT>""" <EOL> return dict ( [ ( a [ '<STR_LIT>' ] . id , a [ '<STR_LIT>' ] ) for a in <EOL> self . applications . values ( ) ] ) <EOL> class ApplicationResult ( object ) : <EOL> is_final = is_successful = False <EOL> discount = D ( '<STR_LIT>' ) <EOL> description = None <EOL> BASKET , SHIPPING , POST_ORDER = <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> <EOL> affects = None <EOL> @ property <EOL> def affects_basket ( self ) : <EOL> return self . affects == self . BASKET <EOL> @ property <EOL> def affects_shipping ( self ) : <EOL> return self . affects == self . SHIPPING <EOL> @ property <EOL> def affects_post_order ( self ) : <EOL> return self . affects == self . POST_ORDER <EOL> class BasketDiscount ( ApplicationResult ) : <EOL> """<STR_LIT>""" <EOL> affects = ApplicationResult . BASKET <EOL> def __init__ ( self , amount ) : <EOL> self . discount = amount <EOL> @ property <EOL> def is_successful ( self ) : <EOL> return self . discount > <NUM_LIT:0> <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % self . discount <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . discount ) <EOL> ZERO_DISCOUNT = BasketDiscount ( D ( '<STR_LIT>' ) ) <EOL> class ShippingDiscount ( ApplicationResult ) : <EOL> """<STR_LIT>""" <EOL> is_successful = is_final = True <EOL> affects = ApplicationResult . SHIPPING <EOL> SHIPPING_DISCOUNT = ShippingDiscount ( ) <EOL> class PostOrderAction ( ApplicationResult ) : <EOL> """<STR_LIT>""" <EOL> is_final = is_successful = True <EOL> affects = ApplicationResult . POST_ORDER <EOL> def __init__ ( self , description ) : <EOL> self . description = description </s>
