o
    gw                     @   s   d dl mZ d dlmZ d dlmZ G dd dejZG dd dejZG dd	 d	ejZ	G d
d dejZ
G dd dejZG dd dejZdS )    )models)User)Productc                   @   sN   e Zd ZdZejdddZejdddZej	dddZ
ejdd	Zd
d ZdS )ShippingMethodz<
    Represents a shipping method available for orders.
    d   T)
max_lengthunique)blanknull
      
max_digitsdecimal_placesdefaultc                 C   s,   | j  d| jdkrd S d| jd S )zE
        Display name and cost (or 'Free' if the cost is 0).
         - r   FreezKsh z.2f)namecostself r   '/var/www/html/hyperkenya/shop/models.py__str__   s   ,zShippingMethod.__str__N)__name__
__module____qualname____doc__r   	CharFieldr   	TextFielddescriptionDecimalFieldr   BooleanField	is_activer   r   r   r   r   r      s    r   c                   @   sZ   e Zd ZdZejeejdddZej	dddddZ
ejddZdd Zd	d
 Zdd ZdS )Cartz9
    Represents a shopping cart for a user or guest.
    T	on_deleter
   r	   (   )r   r
   r	   r   auto_now_addc                 C   "   | j r
| j j dS d| j dS )z4
        String representation of the cart.
        z's CartzGuest Cart (Session: ))userusernamesession_keyr   r   r   r   r         "zCart.__str__c                 C      t dd | jd D S )zE
        Calculate the total price of all items in the cart.
        c                 s   s    | ]}|  V  qd S N)get_total_price.0itemr   r   r   	<genexpr>(   s    z'Cart.get_total_price.<locals>.<genexpr>productsumitemsselect_relatedallr   r   r   r   r3   $      zCart.get_total_pricec                 C   s   t dd | j D S )zB
        Calculate the total number of items in the cart.
        c                 s       | ]}|j V  qd S r2   quantityr4   r   r   r   r7   .       z'Cart.get_total_items.<locals>.<genexpr>)r:   r;   r=   r   r   r   r   get_total_items*   s   zCart.get_total_itemsN)r   r   r   r   r   
ForeignKeyr   CASCADEr-   r   r/   DateTimeField
created_atr   r3   rC   r   r   r   r   r%      s    r%   c                   @   sV   e Zd ZdZejedejdZeje	ejdZ
ejddZdd Zd	d
 Zdd ZdS )CartItemz/
    Represents a product added to a cart.
    r;   related_namer'   r'      r   c                 C   s   | j  d| jj d| j S )z9
        String representation of the cart item.
        x z in )rA   r8   r   cartr   r   r   r   r   9   s   zCartItem.__str__c                 C   s   | j jp| j jS )zf
        Get the price of the product (discount price if available, otherwise selling price).
        )r8   discount_priceselling_pricer   r   r   r   get_unit_price?   s   zCartItem.get_unit_pricec                 C   s   |   | j S )z?
        Calculate the total price for this cart item.
        )rQ   rA   r   r   r   r   r3   E   s   zCartItem.get_total_priceN)r   r   r   r   r   rD   r%   rE   rN   r   r8   PositiveIntegerFieldrA   r   rQ   r3   r   r   r   r   rH   1   s    rH   c                   @   sz   e Zd ZdZejeejdddZej	dddZ
ejddZejddddZejddZejd	dZejddZd
d ZdS )ShippingAddressz3
    Stores the shipping address for an order.
    Tr&   r
   r	      )r   r   r	   r
   r      c                 C   r+   )z@
        String representation of the shipping address.
        z
's AddresszGuest Address (r,   )r-   r.   guest_emailr   r   r   r   r   X   r0   zShippingAddress.__str__N)r   r   r   r   r   rD   r   rE   r-   
EmailFieldrX   r   address_line_1address_line_2citypostal_codecountryr   r   r   r   r   rS   L   s    rS   c                   @   s   e Zd ZdZejeejdddZej	dddZ
ejeejddZejeejddZejddddZejdg d	d
dZejddZejdddZejdg dd
dZdd Zdd Zdd Zdd ZdS )Orderz8
    Represents an order placed by a user or guest.
    Tr&   rT   )r'   r
   rU   rV   rW   )Pendingra   )Paidrb   )Failedrc   ra   )r   choicesr   r)   r   r   r   )r`   )
Processingre   )Shippedrf   )	Deliveredrg   )	Cancelledrh   c                 C   s$   d| j  d| jr| jj S d S )z5
        String representation of the order.
        zOrder #r   Guest)idr-   r.   r   r   r   r   r      s   $zOrder.__str__c                 C   r1   )zC
        Calculate the total number of items in the order.
        c                 s   r?   r2   r@   r4   r   r   r   r7      rB   z(Order.get_total_items.<locals>.<genexpr>r8   r9   r   r   r   r   rC      r>   zOrder.get_total_itemsc                 C   s   | j | jr
| jj S d S )z@
        Calculate the total price, including shipping.
        r   )total_priceshipping_methodr   r   r   r   r   calculate_total_with_shipping   s   z#Order.calculate_total_with_shippingc                 C   s   || _ |   dS )z1
        Update the status of the order.
        N)statussave)r   
new_statusr   r   r   update_status   s   zOrder.update_statusN)r   r   r   r   r   rD   r   rE   r-   rY   rX   rS   SET_NULLshipping_addressr   rl   r   mpesa_checkout_request_idmpesa_payment_statusrF   rG   r"   rk   rn   r   rC   rm   rq   r   r   r   r   r_   _   s.    	r_   c                   @   sX   e Zd ZdZejedejdZeje	ejdZ
e ZejdddZdd	 Zd
d ZdS )	OrderItemz2
    Represents a single product in an order.
    r;   rI   rK   r   r   r   c                 C   s   | j  d| jj d| jj S )z:
        String representation of the order item.
        rM   z in Order #)rA   r8   r   orderrj   r   r   r   r   r      r>   zOrderItem.__str__c                 C   s   | j | j S )z@
        Calculate the total price for this order item.
        )pricerA   r   r   r   r   r3      s   zOrderItem.get_total_priceN)r   r   r   r   r   rD   r_   rE   rw   r   r8   rR   rA   r"   rx   r   r3   r   r   r   r   rv      s    rv   N)	django.dbr   django.contrib.auth.modelsr   inventory.modelsr   Modelr   r%   rH   rS   r_   rv   r   r   r   r   <module>   s    :