o
    pwh7                     @   s|  d dl Z d dlmZ d dlmZ d dlmZ d dlmZmZ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 d dlmZmZmZ d dlmZ d dlmZ d dlmZ ddlmZm Z m!Z! d dl"m#Z# d dl$Z$e$j%ej&ej'fdZ(eeddG dd deZ)eeddG dd deZ*eeddG dd deZ+edd Z,edd Z-eeddG dd  d eZ.dS )!    N)JsonResponse)renderstatus)datetime	timedeltatime)method_decorator)csrf_exempt)View)settings)verify_token)Daytimes	RateListsTaxes)Trips)Transactions)Q   )TripSerializerTaxSerializerTripsSerializer)total_daytime_minutes)authdispatch)namec                   @   s   e Zd Zdd Zdd ZdS )Tripc              
   C   sz  t |}|d krtdtjddtjdS zt|j}t }||_|d |_	|d |_
|d |_|d |_|d	 |_|d
 |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|  tdtjddtjdW S  t y } zt!| tdtj"ddtj"dW  Y d }~S d }~ww )NUser not login.messager   Fsafer   	trip_typepickup_addresspickup_latitudepickup_longitudedestination_addressdestination_latitudedestination_longitude
start_date
start_timeend_dateend_timestation_typeinstructionsvehicle_modelvehicle_typetransmission_typevehicle_model_namevehicle_type_nametransmission_type_nameamount
net_amountzTrip added successfully-Something went wrong. Please try again later.)#r   r   r   HTTP_401_UNAUTHORIZEDjsonloadsbodyr   user_idr"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   vehicle_model_idvehicle_type_idtransmission_type_idr2   r3   r4   r5   r6   saveHTTP_200_OK	ExceptionprintHTTP_400_BAD_REQUEST)selfrequestr<   request_datatripe rJ   3/var/www/html/DRIVERZZ_CUSTOMER_APIS/trips/views.pypost   sF   




















$z	Trip.postc              
   C   s   t |}|d krtdtjddtjdS z,tjj|jdd}tj	 }t
|dd}t|dd}t|j|jtjd	dtjdW S  ty^ } ztd
tjddtjdW  Y d }~S d }~ww )Nr   r   Fr    idrM   manyT)rH   taxesr   r7   )r   r   r   r8   r   objectsgetGETr   allr   r   datarA   rB   rD   )rE   rF   r<   rH   rQ   
serializertax_serializerrI   rJ   rJ   rK   rS   B   s   
 $zTrip.getN)__name__
__module____qualname__rL   rS   rJ   rJ   rJ   rK   r      s    %r   c                   @      e Zd Zdd ZdS )EditTripc              
   C   s  t |}|d krtdtjddtjdS zt|j}tjj	|d}||_
|d |_|d |_|d |_|d	 |_|d
 |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|   tdtj!ddtj!dW S  t"y } zt#| tdtj$ddtj$dW  Y d }~S d }~ww )Nr   r   Fr    rN   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   zTrip updated successfullyr7   )%r   r   r   r8   r9   r:   r;   r   rR   rS   r<   r"   r#   r$   r%   r&   r'   r(   edit_start_dateedit_start_timeedit_end_dateedit_end_timer-   r.   r=   r>   r?   r2   r3   r4   r5   r6   r@   rA   rB   rC   rD   )rE   rF   rM   r<   rG   rH   rI   rJ   rJ   rK   rL   U   sF   




















$zEditTrip.postNrY   rZ   r[   rL   rJ   rJ   rJ   rK   r]   R   s    r]   c                   @   r\   )TripListc                 C   s   t |}|d krtdtjddtjdS tjj|d}|jd}|dkr,|jdd	}n|d
kr7|jdd}n|jdd}t	|dd}t|j
tjddtjdS )Nr   r   Fr    )r<   r   0   r   1)
status__lt)
status__gtTrO   )tripsr   )r   r   r   r8   r   rR   filterrT   rS   r   rV   rA   )rE   rF   r<   ri   trip_statusrW   rJ   rJ   rK   rS      s   zTripList.getN)rY   rZ   r[   rS   rJ   rJ   rJ   rK   rc   ~       rc   c              
   C   s  t | j}z_d}|d }t|d d }t|d d }tt|d d t| d}tt|d	 d t| d}|| }t| d
 }|dkrSnd| }	|t	|	d }|| }t| d
 }t
d t
| ztjj||d |d d|d dd}
tjj||d |d d|d dd}t|
dkrtjj||d |d d|d dd}
tjj||d |d d|d dd}|
d j}|d j}tjjdd}t|jd }tjjdd}t|jd }d}d}|j|jf}|j|jf}t||||}| | kr|d }|| }||| 7 }||| 7 }tj }t|dd}t||jtjddtjdW W S  tyf } zt
| tdg tj ddtj dW  Y d }~W S d }~ww  ty } zt
| tdg tj ddtj dW  Y d }~S d }~ww )Nr   rH   r*   z%I:%M %pr,   r)    z%Y-%m-%d %H:%M:%Sr+   <   x   )minutesz------------------r-   uniform_typer"   )duration__gter-   rq   dayr"   durationr   )duration__ltr-   rq   rs   r"   z	-duration)day_typere   TrO   )r5   rQ   r   Fr    )!r9   r:   r;   r   strptimer   strinttotal_secondsr   rC   r   rR   rj   order_bylenrater   rS   r*   hourminuter   dater   rU   r   r   rV   r   rA   rB   rD   )rF   rG   r5   r*   r,   start_timestampend_timestamp
time_deltatotal_minutesextra_minutesrate_day
rate_nightday_chargesnight_chargesrs   	day_startday_endday_minutesnight_minutesrQ   rX   rI   rJ   rJ   rK   calculateFare   sj     ((((


 (&r   c                 C   s  | j d}| j d}| j d}| j d}| j d}ztj|||||d}|r|dkrtj|}ztjj|d}t	| d	W W S  tj
y   t }z;tjt|d
}	||_||_||_|	j|_|d |_d|	_|	  |  |jrt	| d	W  Y W S t	| dW  Y W S  ty }
 zt	| dW  Y d }
~
 Y W S d }
~
ww w W d S W d S  ty }
 zt	| dW  Y d }
~
S d }
~
ww )Nrazorpay_payment_idrazorpay_payment_link_id"razorpay_payment_link_reference_idrazorpay_payment_link_statusrazorpay_signature)payment_link_idpayment_link_reference_idpayment_link_statusr   r   paid)razorpay_order_idztrips/success.html)reference_idamount_paidr   ztrips/failed.html)rT   rS   clientutilityverify_payment_link_signaturepayment_linkfetchr   rR   r   DoesNotExistr   r   r   r   r   r<   r5   payment_statusr@   rM   rB   )rF   r   order_id
booking_idr   r   resultpayment_detailstransactionrH   rI   rJ   rJ   rK   paymentHandler   sV   
r   c                   @   r\   )
CancelTripc                 C   s   t |}|d krtdtjddtjdS z$t|j}tjj	||d d}d|_|
  tdtjddtjdW S  tjyN   td	tjddtjd Y S w )
Nr   r   Fr    rM   )r<   rM      zTrip canceled successfully.r7   )r   r   r   r8   r9   r:   r;   r   rR   rS   r@   rA   r   rD   )rE   rF   r<   rG   rH   rJ   rJ   rK   rL   	  s   zCancelTrip.postNrb   rJ   rJ   rJ   rK   r     rl   r   )/r9   django.httpr   django.shortcutsr   rest_frameworkr   r   r   r   django.utils.decoratorsr	   django.views.decorators.csrfr
   django.viewsr   DRIVERZZ_CUSTOMER_APISr   authentication.authr   rates.modelsr   r   r   trips.modelsr   transactions.modelsr   django.db.modelsr   serializersr   r   r   trips.helpersr   razorpayClientRAZORPAY_KEY_IDRAZORPAY_KEY_SECRETr   r   r]   rc   r   r   r   rJ   rJ   rJ   rK   <module>   s<   

7
+
H

+