o
    Hh                     @   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
 d dlZG dd deZdS )	    )BaseCommand)timezone)Decimal)PaymentUnreconciledPayment)CustomerNc                   @   s    e Zd ZdZdd Zdd ZdS )Commandz3Find payments that should be marked as unreconciledc                 C   s&   |j dddd |j dtddd d S )	Nz--create-missing
store_truezHCreate unreconciled payment records for payments without valid customers)actionhelpz--days   z)Number of days to look back (default: 30))typedefaultr   )add_argumentint)selfparser r   W/var/www/html/optinet_system/payments/management/commands/find_unreconciled_payments.pyadd_arguments   s   
zCommand.add_argumentsc                 O   s  |d }|d }t  }|t j|d }| jd|  d|   g }tjjd||d}|D ]}	g }
|	j	s@|	j
s@|
d |	j	rZz|	j	}|jsO|
d	 W n	   |
d
 Y |	j
ry|	j
j	ryz|	j
j	}|jsn|
d W n	   |
d Y |	jrz8|	j}t|trt|}d }d|v r|d }|rz	tjj|d W n tjy   |
d| d Y nw W n ty } z|
d|  W Y d }~nd }~ww |
r||	|
d q1tjj||d }| jdt| d | jd|  |D ]}|d }	|d }
| jd|	j  | jd|	j  | jd|	j  | jd|	j  | jdd|
  |rtjj|	jd }|s|	jrd }|	jpTd!}|	jrz"|	j}t|trht|}d|v rq|d }d"|v rz|d" }W n   Y tjj |	j||	j||	j!p|	jd#d$d|
 |	jd%}| jd&|j"  q|r| jd'|j"  q| jd( q|s| jd) | jd* d S )+Ncreate_missingdays)r   zChecking payments from z to mpesa)payment_methodcreated_at__gtecreated_at__ltezNo customer or invoice linkedzCustomer has no customer_idzCustomer relationship brokenz#Invoice customer has no customer_idz$Invoice customer relationship brokenBillRefNumber)customer_idzAccount reference 'z' not foundzError parsing callback data: )paymentissues)r   r   z
Found z" potentially unreconciled paymentsz'Existing unreconciled payment records: r   r   z
Payment ID: zAmount: KES zDate: z	Receipt: zIssues: z, )transaction_idUNKNOWN MSISDNsystem_errorz,Auto-created from existing payment. Issues: )r    phone_numberamountaccount_referencetransaction_timereasonnotescallback_datau+     → Created unreconciled payment record: u+     → Unreconciled payment already exists: u:     → No receipt number, cannot create unreconciled recordzB
To create unreconciled payment records, run with --create-missingz
Done!)#r   now	timedeltastdoutwritedater   objectsfiltercustomerinvoiceappendr   mpesa_callback_data
isinstancestrjsonloadsr   getDoesNotExist	Exceptionr   countlen
payment_idr&   
created_atmpesa_receipt_numberjoinfirstmpesa_phone_numbercreatecompleted_atid)r   argsoptionsr   	days_backend_date
start_dateproblematic_paymentsmpesa_paymentsr   r   r3   r+   account_refeexisting_unreconcileditemexistingphoneunreconciledr   r   r   handle   s    







zCommand.handleN)__name__
__module____qualname__r   r   rW   r   r   r   r   r   
   s    r   )django.core.management.baser   django.utilsr   decimalr   payments.modelsr   r   customers.modelsr   r9   r   r   r   r   r   <module>   s   