o
    ayhT	                     @   s^   d dl mZ d dlmZ d dlmZ d dl mZ d dlmZ d dlmZ G dd dejZdS )    )models)Decimal)ValidationErrorc                       s   e Zd ZejddddZejddZejdddZejddd	Zej	dd
Z
ejdd
ZejddZejddeddZejddeddZ fddZdd Zdd ZdddZ  ZS )Customer
   TF)
max_lengthuniqueeditabled   )r   )r   blank   )r   r   )r   )auto_now_add   0.00)
max_digitsdecimal_placesdefaultc                    sX   | j s!tjd }|rt|j dd  d nd}d|d| _ t j|i | d S )Nz-id      CST04d)customer_idr   objectsorder_byfirstintsupersave)selfargskwargslast_customernew_id	__class__ )/var/www/html/kangema/customers/models.pyr      s
   zCustomer.savec                 C   s   | j  d| j S )N )
first_name	last_name)r   r%   r%   r&   __str__   s   zCustomer.__str__c                 C   s   | j | | jkS )zi
        Check if a customer can make a purchase on credit without exceeding their credit limit.
        )credit_balancecredit_limit)r   amountr%   r%   r&   can_purchase_on_credit"   s   zCustomer.can_purchase_on_creditc                 C   sR   |r| j | }|tdk rtdn| j | }|| jkr td|| _ |   dS )z
        Updates the credit balance based on a credit purchase or a payment.

        - `amount`: Decimal value of transaction.
        - `is_payment`: Boolean. If `True`, it means the customer is making a payment, so we decrease the balance.
        r   z0Credit balance cannot be negative after payment.z$Customer cannot exceed credit limit.N)r+   r   r   r,   r   )r   r-   
is_paymentnew_balancer%   r%   r&   update_credit_balance(   s   


zCustomer.update_credit_balance)F)__name__
__module____qualname__r   	CharFieldr   r(   r)   phone
EmailFieldemail	TextFieldaddressDateTimeField
date_addedDecimalFieldr   r,   r+   r   r*   r.   r1   __classcell__r%   r%   r#   r&   r   
   s    r   N)	django.dbr   decimalr   django.core.exceptionsr   Modelr   r%   r%   r%   r&   <module>   s    