o
    h                     @   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G dd dejZdS )    )models)User)Productc                   @   sT   e Zd ZdZejeejddZeje	ejdZ
ejddZG dd dZd	d
 ZdS )	SavedItemzA
    Represents a product saved by a user for later viewing.
    saved_items)	on_deleterelated_namer   Tauto_now_addc                   @   s   e Zd ZdZdS )zSavedItem.Meta)userproductN)__name__
__module____qualname__unique_together r   r   $/var/www/html/kangema/shop/models.pyMeta   s    r   c                 C   s   | j j d| jj S )Nz saved )r   usernamer   nameselfr   r   r   __str__   s   zSavedItem.__str__N)r   r   r   __doc__r   
ForeignKeyr   CASCADEr   r   r   DateTimeField
created_atr   r   r   r   r   r   r      s    r   c                   @   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)r   costr   r   r   r   r      s   ,zShippingMethod.__str__N)r   r   r   r   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r   r$   r#   (   )r!   r$   r#   r"   r
   c                 C   "   | j r
| j j dS d| j dS )z4
        String representation of the cart.
        z's CartzGuest Cart (Session: ))r   r   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>7   s    z'Cart.get_total_price.<locals>.<genexpr>r   sumitemsselect_relatedallr   r   r   r   r>   3      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 r=   quantityr?   r   r   r   rB   =       z'Cart.get_total_items.<locals>.<genexpr>)rD   rE   rG   r   r   r   r   get_total_items9   s   zCart.get_total_itemsN)r   r   r   r   r   r   r   r   r   r/   r:   r   r   r   r>   rM   r   r   r   r   r5   %   s    r5   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.
    rE   r   r   r	      r*   c                 C   s   | j  d| jj d| j S )z9
        String representation of the cart item.
        x z in )rK   r   r   cartr   r   r   r   r   H   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).
        )r   discount_priceselling_pricer   r   r   r   get_unit_priceN   s   zCartItem.get_unit_pricec                 C   s   |   | j S )z?
        Calculate the total price for this cart item.
        )rU   rK   r   r   r   r   r>   T   s   zCartItem.get_total_priceN)r   r   r   r   r   r   r5   r   rR   r   r   PositiveIntegerFieldrK   r   rU   r>   r   r   r   r   rN   @   s    rN   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.
    Tr6   r$   r#      )r!   r!   r#   r$   r       c                 C   r8   )z@
        String representation of the shipping address.
        z
's AddresszGuest Address (r9   )r   r   guest_emailr   r   r   r   r   g   r;   zShippingAddress.__str__N)r   r   r   r   r   r   r   r   r   
EmailFieldr\   r/   address_line_1address_line_2citypostal_codecountryr   r   r   r   r   rW   [   s    rW   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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.
    Tr6   rX   )r   r$   rY   rZ   r[   ))mpesazM-Pesa)cardzCard Payment)payment_on_deliveryPayment on Deliveryrd   )r!   choicesr+      )Pendingrk   )Paidrl   )Failedrm   )rg   rg   rk   r
   r%   r&   r'   )rj   )
Processingrn   )Shippedro   )	Deliveredrp   )	Cancelledrq   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   r<   )zC
        Calculate the total number of items in the order.
        c                 s   rI   r=   rJ   r?   r   r   r   rB      rL   z(Order.get_total_items.<locals>.<genexpr>r   rC   r   r   r   r   rM      rH   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   r   r   r   r   r]   r\   rW   SET_NULLshipping_addressr   ru   r/   mpesa_checkout_request_idpayment_methodmpesa_payment_statusr   r   r2   rt   rw   r   rM   rv   rz   r   r   r   r   rc   n   s8    	
rc   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.
    rE   rO   r	   r%   r&   r'   c                 C   s   | j  d| jj d| jj S )z:
        String representation of the order item.
        rQ   z in Order #)rK   r   r   orderrs   r   r   r   r   r      rH   zOrderItem.__str__c                 C   s   | j | j S )z@
        Calculate the total price for this order item.
        )pricerK   r   r   r   r   r>      s   zOrderItem.get_total_priceN)r   r   r   r   r   r   rc   r   r   r   r   rV   rK   r2   r   r   r>   r   r   r   r   r      s    r   N)	django.dbr   django.contrib.auth.modelsr   inventory.modelsr   Modelr   r   r5   rN   rW   rc   r   r   r   r   r   <module>   s    D