o
    hV                     @   s   d dl mZ d dlmZ d dlmZ d dlmZmZ d dl	m	Z	m
Z
 d dl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)timezone)Decimal)CustomerService)datetime	timedeltaNc                       s   e Zd Zg 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
Zedd Zedd Zejdd	eddZejdd	eddZejdd	d
ZejejdZe Ze Ze ZejdeddZejddZ ej!ddZ"ej!ddZ#dd Z$ fddZ%edd Z&  Z'S )Invoice))unpaidUnpaid)paidPaid)overdueOverdue	cancelled	Cancelled   T)
max_lengthuniqueinvoices	on_deleterelated_name)r   null
      )
max_digitsdecimal_placesc                 C      | j S N)service_amountself r$   ./var/www/html/optinet_system/billing/models.pypackage_amount      zInvoice.package_amountc                 C   r   r    servicer"   r$   r$   r%   package   r'   zInvoice.packagez0.00)r   r   defaultr+   r
   r   choicesr+   )blankauto_now_addauto_nowc                 C   s   d| j  d| jj S )NzInvoice  - )invoice_numbercustomer	full_namer"   r$   r$   r%   __str__/   s   zInvoice.__str__c                    s`   | j sdt d dtt d d   | _ | j| j	 | j
 | _t j|i | d S )NzINV-z%Y%m%d-   )r5   r   nowstrftimestruuiduuid4upperr!   installation_feepenalty_amounttotal_amountsupersave)r#   argskwargs	__class__r$   r%   rE   2   s   0zInvoice.savec                 C   s   | j dko| jt  k S )Nr
   )statusdue_dater   r;   dater"   r$   r$   r%   
is_overdue;   s   zInvoice.is_overdue)(__name__
__module____qualname__STATUS_CHOICESr   	CharFieldr5   
ForeignKeyr   CASCADEr6   r   SET_NULLr)   DecimalFieldr!   propertyr&   r*   r   rA   rB   rC   	DateFieldr   r;   
issue_daterK   billing_period_startbilling_period_endrJ   	TextFieldnotesDateTimeField
created_at
updated_atr8   rE   rM   __classcell__r$   r$   rH   r%   r	      s2    

	r	   c                   @   s  e Zd Zg dZejeejddZeje	ejdZ
ejeejdddZedd Zejd	d
Zejdd
Zejded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ejddZdd Zedd Zedd Zedd Zedd Zdd  ZG d!d" d"Z d#S )$ServicePeriod))activeActive)expiredExpired)	suspended	Suspendedr   service_periodsr   r   T)r   r   r/   c                 C   r   r    r(   r"   r$   r$   r%   r*   L   r'   zServicePeriod.packagez"Exact start time of service period)	help_textz Exact end time of service periodr   rc   r-   Fz2Was this period auto-renewed from account balance?)r+   rk   )r   r/   r0   r2   c                 C   s(   | j j d| jd d| jd S )Nr4   z%Y-%m-%d %H:%Mz to )r6   customer_idservice_startr<   service_endr"   r$   r$   r%   r8   ]   s   (zServicePeriod.__str__c                 C   s.   t  }| jdko| j|  ko| jkS   S )Nrc   )r   r;   rJ   rm   rn   )r#   r;   r$   r$   r%   	is_active`   s   &zServicePeriod.is_activec                 C   s   t  | jkp| jdkS )Nre   )r   r;   rn   rJ   r"   r$   r$   r%   
is_expirede   s   zServicePeriod.is_expiredc                 C   s   | j rtdS | jt  S )Nr   )rp   r   rn   r   r;   r"   r$   r$   r%   time_remainingi   s   zServicePeriod.time_remainingc                 C   s   | j jS r    )rq   daysr"   r$   r$   r%   days_remainingo   s   zServicePeriod.days_remainingc                 C   sl   | j jdkr| j}|tdd }||fS | j jdkr(| j}|tdd }||fS | j}|tdd }||fS )z5Calculate the next service period start and end datesmonthly   )rr   	quarterlyZ   im  )r)   billing_cyclern   r   )r#   
next_startnext_endr$   r$   r%   calculate_next_period_datess   s   z)ServicePeriod.calculate_next_period_datesc                   @   s   e Zd ZdgZdS )zServicePeriod.Metaz-service_startN)rN   rO   rP   orderingr$   r$   r$   r%   Meta   s    
r}   N)!rN   rO   rP   rQ   r   rS   r   rT   r6   r   r)   r	   invoicerW   r*   r^   rm   rn   rR   rJ   BooleanFieldis_auto_renewedrenewal_attempted_atsuspended_atr_   r`   r8   ro   rp   rq   rs   r{   r}   r$   r$   r$   r%   rb   ?   s4    




rb   c                   @   sD   e Zd ZejeejdZe Z	ej
ddZejddZdd ZdS )BillingCyclerj   Tr,   r0   c                 C   s   | j j d| j S )Nz	 - Next: )r6   r7   next_billing_dater"   r$   r$   r%   r8      s   zBillingCycle.__str__N)rN   rO   rP   r   rS   r   rT   r6   rX   r   r   ro   r^   r_   r8   r$   r$   r$   r%   r      s    r   )	django.dbr   django.utilsr   decimalr   customers.modelsr   r   r   r   r>   Modelr	   rb   r   r$   r$   r$   r%   <module>   s    7F