o
    h                     @   sP   d dl mZ d dlmZ d dlmZ d dlmZ G dd dZG dd dZ	d	S )
    )timezone)Q)	timedelta)Decimalc                   @   s6   e Zd ZdZedd Zed	ddZedd ZdS )
ServicePeriodManagerzJ
    Service for managing customer service periods and billing cycles
    c                 C   sl   ddl m} t }|jj| d|dd }|s!dddd	d
S |jr,dd|j	d	d
S dd|j	|j
|dS )z{
        Get current service status for a customer
        Returns dict with status, expires_at, and other details
           ServicePeriodactive)customerstatusservice_start__lte-service_startinactivezNo active service periodNr   )r   message
expires_atdays_remainingexpiredzService has expiredzService is active)r   r   r   r   service_period)modelsr	   r   nowobjectsfilterorder_byfirst
is_expiredservice_endr   )r   r	   r   current_period r   0/var/www/html/optinet_system/billing/services.py#get_customer_current_service_status   s6   
z8ServicePeriodManager.get_customer_current_service_statusNc                 C   sv   ddl m} |st }|jdkr|tdd }n|jdkr&|tdd }n|tdd }|jj| ||||d	d
}|S )z<
        Create a new service period for a customer
        r   r   monthly   )days	quarterlyZ   im  r
   )r   serviceinvoiceservice_startr   r   )r   r	   r   r   billing_cycler   r   create)r   r&   
start_timer'   r	   end_timer   r   r   r   create_service_period6   s"   

	z*ServicePeriodManager.create_service_periodc              	   C   s   ddl m} ddlm} |jj| ddd }|r| js#dd	d
S | j	| jj
k r/ddd
S | \}}|jj| | j||ddt d}| j	}|  j	| jj
8  _	|   |jj| d| jj
d|j d|j || j	d d|dS )zR
        Attempt to renew customer's service period using account balance
        r   r   r   )CustomerBalanceTransactionr
   )r   r   r   Fz!No active service or service plan)successr   zInsufficient account balanceT)r   r&   r(   r   r   is_auto_renewedrenewal_attempted_atdebitz Auto-renewal for service period zSP-)r   transaction_typeamountdescriptionreference_idbalance_beforebalance_after)r/   r   )r   r	   customers.modelsr.   r   r   r   r   r&   account_balancepricecalculate_next_period_datesr*   r   r   saveid)r   r	   r.   r   
next_startnext_end
new_periodold_balancer   r   r   renew_service_periodS   sF   







z)ServicePeriodManager.renew_service_period)NN)__name__
__module____qualname____doc__staticmethodr    r-   rC   r   r   r   r   r      s    
(r   c                   @   s   e Zd ZdZedd ZdS )AutoBillingServicez1
    Service for automated billing processes
    c                  C   s   ddl m}  ddlm} dddddd}|jjdt d}|D ]I}|j}|j	rTt
|}|d	 r=|d
 d  d7  < q!d|_t |_|  |d
 d  d7  < q!d|_t |_|  |d
 d  d7  < q!|S )zN
        Run daily billing tasks including renewals and notifications
        r   )Customerr   r   )renewed	suspendedfailed)expired_servicesupcoming_invoicesr
   )r   service_end__lter/   rN   rK   rL   )r9   rJ   r   r	   r   r   r   r   r   can_auto_renewr   rC   r   suspended_atr=   )rJ   r	   resultsexpired_periodsperiodr   renewal_resultr   r   r   run_daily_billing_tasks   s0   



z*AutoBillingService.run_daily_billing_tasksN)rD   rE   rF   rG   rH   rW   r   r   r   r   rI      s    rI   N)
django.utilsr   django.db.modelsr   datetimer   decimalr   r   rI   r   r   r   r   <module>   s    