o
    rPh                     @   sZ   d dl mZ d dlmZ d dlmZmZ d dlmZ d dl	Z	e	
eZG dd deZdS )    )BaseCommand)timezone)PaymentUnreconciledPayment)CustomerNc                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )Commandz?Find and reconcile paybill payments that might have been missedc                 C   sf   |j dtddd |j dtdd |j dtd	d |j d
tdd |j dtdd |j dddd d S )Nz--days-back   z6Number of days to look back for payments (default: 30))typedefaulthelpz--transaction-idz+Specific M-Pesa transaction ID to reconcile)r	   r   z--customer-idz%Customer ID for manual reconciliationz--amountz,Transaction amount for manual reconciliationz--phonez/Customer phone number for manual reconciliationz--create-unreconciled
store_truez3Create unreconciled payment record for manual entry)actionr   )add_argumentintstrfloat)selfparser r   W/var/www/html/optinet_system/payments/management/commands/reconcile_paybill_payments.pyadd_arguments   s>   
zCommand.add_argumentsc                 O   s:   |d r|  | d S |d r| | d S | | d S )Ncreate_unreconciledtransaction_id)"create_manual_unreconciled_paymentreconcile_specific_transactionscan_for_missing_payments)r   argsoptionsr   r   r   handle.   s
   zCommand.handlec                 C   s  t |d |d |d gs| j| jd dS ztjj|d d }|r8| j| j	d|j
  W dS tjj|d d }|rV| j| j	d	|j  W dS tjj|d |d |d |d
dt ddddd	}| j| jd|j  | jd|d   | jd|d   | jd|d   | jd W dS  ty } z| j| jdt|  W Y d}~dS d}~ww )z.Create an unreconciled payment record manuallyr   amountphonezKFor manual entry, you must provide: --transaction-id, --amount, and --phoneNmpesa_receipt_numberPayment already exists: )r   z%Unreconciled payment already exists: customer_idMANUAL_ENTRYManualEntryaccount_not_foundz%Manually created unreconciled payment)	r   phone_numberr   account_referencetransaction_time
first_name	last_namereasonnoteszCreated unreconciled payment: zTransaction ID: Amount: KES zPhone: z5
You can now resolve this payment in the admin panel.z%Error creating unreconciled payment: )allstdoutwritestyleERRORr   objectsfilterfirstWARNING
payment_idr   idcreategetr   nowSUCCESS	Exceptionr   )r   r   existing_paymentexisting_unreconciledunreconcileder   r   r   r   6   sd   

z*Command.create_manual_unreconciled_paymentc           
      C   s  |d }| d}|s| j| jd dS ztjj|d }|s3| j| jd|  W dS t	jj|d }|rO| j| j
d|j  W dS | d	}|sc| j| jd
 W dS t	jj||dd|| ddd| dt d	}ddlm} |j||d| |jdd | j| jd | jd|j  | jd|j d|j  | jd|  W dS  ty }	 z| j| jdt|	  W Y d}	~	dS d}	~	ww )z Reconcile a specific transactionr   r$   zFYou must provide --customer-id for specific transaction reconciliationN)r$   zCustomer not found: r!   r#   r   z,You must provide --amount for reconciliationmpesa	completedr     zMANUAL-z#Manually reconciled paybill payment)	customerr   payment_methodstatusr"   mpesa_phone_numberreference_numberr/   completed_atr   )CustomerBalanceServicez'Manual M-Pesa payment reconciliation - )rH   r   descriptionreference_idprocessed_byz Payment reconciled successfully!zPayment ID: z
Customer: z - r0   zError reconciling payment: )r=   r2   r3   r4   r5   r   r6   r7   r8   r   r9   r:   r<   r   r>   customers.servicesrN   add_balancer?   r$   display_namer@   r   )
r   r   r   r$   rH   existingr   paymentrN   rD   r   r   r   r   n   sz   





z&Command.reconcile_specific_transactionc                 C   s   |d }| j d| d tjjdt tj|d d }| j d| d |d	kr@| j d
 | j d | j d | j d | j d | j d | j d dS )z-Scan for potentially missing paybill payments	days_backz2Scanning for missing paybill payments in the last z days...pending)days)rJ   created_at__gtezFound z unreconciled paymentsr   z&
To view unreconciled payments, visit:z.Admin Panel > Payments > Unreconciled Paymentsz!Or go to: /payments/unreconciled/z#
To manually add a missing payment:zpython manage.py reconcile_paybill_payments --create-unreconciled --transaction-id=XXXXXXXX --amount=1000 --phone=254712345678 --customer-id=OPT000001z%
To reconcile a specific transaction:zkpython manage.py reconcile_paybill_payments --transaction-id=XXXXXXXX --customer-id=OPT000001 --amount=1000N)	r2   r3   r   r6   r7   r   r>   	timedeltacount)r   r   rW   unreconciled_countr   r   r   r      s"   z!Command.scan_for_missing_paymentsN)	__name__
__module____qualname__r   r   r   r   r   r   r   r   r   r   r   
   s    !8Jr   )django.core.management.baser   django.utilsr   payments.modelsr   r   customers.modelsr   logging	getLoggerr^   loggerr   r   r   r   r   <module>   s   
