3

\                 @   sr   d dl mZ G dd deZG dd deZG dd deZeedZG d	d
 d
eZe Z	G dd deZ
dS )    )Decimalc               @   sx   e Zd ZdZdd Zdd Zdd Zdd	 Zed
d Z	edd Z
edd Zedd Zedd Zedd ZdS )OfferApplicationsa#  
    A collection of offer applications and the discounts that they give.

    Each offer application is stored as a dict which has fields for:

    * The offer that led to the successful application
    * The result instance
    * The number of times the offer was successfully applied
    c             C   s
   i | _ d S )N)applications)self r   3/var/www/html/oscar2019/oscar/apps/offer/results.py__init__   s    zOfferApplications.__init__c             C   s   | j j j S )N)r   values__iter__)r   r   r   r   r
      s    zOfferApplications.__iter__c             C   s
   t | jS )N)lenr   )r   r   r   r   __len__   s    zOfferApplications.__len__c             C   sj   |j | jkr4|||j|j|j dtdd| j|j < | j|j  d  |j7  < | j|j  d  d7  < d S )Nr   z0.00)offerresultnamedescriptionvoucherfreqdiscountr   r      )idr   r   r   Zget_voucherDr   )r   r   r   r   r   r   add   s    zOfferApplications.addc             C   s<   g }x2| j j D ]$}|d  r|d dkr|j| qW |S )zN
        Return basket discounts from offers (but not voucher offers)
        r   r   r   )r   r	   append)r   	discountsapplicationr   r   r   offer_discounts$   s
    z!OfferApplications.offer_discountsc             C   s:   g }x0| j j D ]"}|d r|d dkr|j| qW |S )z8
        Return basket discounts from vouchers.
        r   r   r   )r   r	   r   )r   r   r   r   r   r   voucher_discounts/   s
    z#OfferApplications.voucher_discountsc             C   s0   g }x&| j j D ]}|d jr|j| qW |S )z+
        Return shipping discounts
        r   )r   r	   affects_shippingr   )r   r   r   r   r   r   shipping_discounts:   s
    
z$OfferApplications.shipping_discountsc             C   s^   i }xP| j D ]F}|d }|d }|j|kr<||d||j< q||j d  |7  < qW |j S )z
        Return voucher discounts aggregated up to the voucher level.

        This is different to the voucher_discounts property as a voucher can
        have multiple offers associated with it.
        r   r   )r   r   )r   coder	   )r   r   r   r   r   r   r   r   grouped_voucher_discountsE   s    
z+OfferApplications.grouped_voucher_discountsc             C   s0   g }x&| j j D ]}|d jr|j| qW |S )zV
        Return successful offer applications which didn't lead to a discount
        r   )r   r	   affects_post_orderr   )r   r   r   r   r   r   post_order_actionsZ   s
    
z$OfferApplications.post_order_actionsc             C   s   t dd | jj D S )zH
        Return a dict of offers that were successfully applied
        c             S   s   g | ]}|d  j |d  fqS )r   )r   ).0ar   r   r   
<listcomp>j   s    z,OfferApplications.offers.<locals>.<listcomp>)dictr   r	   )r   r   r   r   offerse   s    zOfferApplications.offersN)__name__
__module____qualname____doc__r   r
   r   r   propertyr   r   r   r    r"   r'   r   r   r   r   r      s   	r   c               @   sR   e Zd Zd ZZedZdZd\ZZ	Z
dZedd Zed	d
 Zedd ZdS )ApplicationResultFz0.00Nr   r      c             C   s   | j | jkS )N)affectsBASKET)r   r   r   r   affects_basket{   s    z ApplicationResult.affects_basketc             C   s   | j | jkS )N)r/   SHIPPING)r   r   r   r   r      s    z"ApplicationResult.affects_shippingc             C   s   | j | jkS )N)r/   
POST_ORDER)r   r   r   r   r!      s    z$ApplicationResult.affects_post_order)r   r   r.   )r(   r)   r*   is_finalis_successfulr   r   r   r0   r2   r3   r/   r,   r1   r   r!   r   r   r   r   r-   n   s   
r-   c               @   s:   e Zd ZdZejZdd Zedd Z	dd Z
dd	 Zd
S )BasketDiscountz]
    For when an offer application leads to a simple discount off the basket's
    total
    c             C   s
   || _ d S )N)r   )r   amountr   r   r   r      s    zBasketDiscount.__init__c             C   s
   | j dkS )Nr   )r   )r   r   r   r   r5      s    zBasketDiscount.is_successfulc             C   s
   d| j  S )Nz<Basket discount of %s>)r   )r   r   r   r   __str__   s    zBasketDiscount.__str__c             C   s   d| j j| jf S )Nz%s(%r))	__class__r(   r   )r   r   r   r   __repr__   s    zBasketDiscount.__repr__N)r(   r)   r*   r+   r-   r0   r/   r   r,   r5   r8   r:   r   r   r   r   r6      s   r6   z0.00c               @   s   e Zd ZdZd ZZejZdS )ShippingDiscountzR
    For when an offer application leads to a discount from the shipping cost
    TN)	r(   r)   r*   r+   r5   r4   r-   r2   r/   r   r   r   r   r;      s   r;   c               @   s&   e Zd ZdZd ZZejZdd Z	dS )PostOrderActionz
    For when an offer condition is met but the benefit is deferred until after
    the order has been placed.  Eg buy 2 books and get 100 loyalty points.
    Tc             C   s
   || _ d S )N)r   )r   r   r   r   r   r      s    zPostOrderAction.__init__N)
r(   r)   r*   r+   r4   r5   r-   r3   r/   r   r   r   r   r   r<      s   r<   N)decimalr   r   objectr   r-   r6   ZZERO_DISCOUNTr;   ZSHIPPING_DISCOUNTr<   r   r   r   r   <module>   s   j