o
    h%                     @   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#m$Z$m%Z%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dd Z.eeddG dd deZ/eeddG dd deZ0eeddG dd  d eZ1d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)calculate_farecalculate_taxescreate_ordertotal_daytime_minutes)authdispatch)namec                   @      e Zd Zdd ZdS )Tripc              
   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 )NUser not login.messager   Fsafer   id)r&   manyT)triptaxesr   -Something went wrong. Please try again later.)r   r   r   HTTP_401_UNAUTHORIZEDr   objectsgetGETr   allr   r   dataHTTP_200_OK	ExceptionHTTP_400_BAD_REQUEST)selfrequestuser_idr)   r*   
serializertax_serializere r;   1/var/www/html/DRIVERZZ_DRIVER_APIS/trips/views.pyr.      s   
 $zTrip.getN__name__
__module____qualname__r.   r;   r;   r;   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$   )	driver_idr   0   r   1)
status__lt)
status__gtTr'   )tripsr   )r   r   r   r,   r   r-   filterr/   r.   r   r1   r2   )r5   r6   r7   rH   trip_statusr8   r;   r;   r<   r.   1   s   zTripList.getNr=   r;   r;   r;   r<   rA   /       rA   c              
   C   s  t | j}zWd}|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
 }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y^ } 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   r)   
start_time%I:%M %pend_time
start_date z%Y-%m-%d %H:%M:%Send_date<   x   )minutesstation_typeuniform_type	trip_type)duration__gterU   rV   dayrW   durationr   )duration__ltrU   rV   rY   rW   z	-duration)day_type   Tr'   )amountr*   r   Fr$   )!jsonloadsbodyr   strptimer   strinttotal_secondsr   r   r-   rI   order_bylenrater   r.   rL   hourminuter   dater   r0   r   r   r1   r   r2   r3   printr4   )r6   request_datar^   rL   rN   start_timestampend_timestamp
time_deltatotal_minutesextra_minutesrate_day
rate_nightday_chargesnight_chargesrY   	day_startday_endday_minutesnight_minutesr*   r9   r:   r;   r;   r<   calculateFareC   sf     ((((


 (&r{   c                   @   r   )	StartTripc                 C   s   t |}|d krtdtjddtjdS z5t|j}tjj	||d d}d|_t
  |_t
  d|_|  td	tjddtjdW S  tjy_   td
tjddtjd Y S w )Nr!   r"   Fr$   r&   rB   r&   rD   rM   zTrip start successfully.r+   )r   r   r   r,   r_   r`   ra   r   r-   r.   r   nowrk   trip_start_dater   strftimetrip_start_timesaver2   DoesNotExistr4   r5   r6   r7   rm   r)   r;   r;   r<   post   s   zStartTrip.postNr>   r?   r@   r   r;   r;   r;   r<   r|      rK   r|   c                   @   r   )OnTheWayTripc                 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$   r&   r}   r]   zTrip updated successfully.r+   )r   r   r   r,   r_   r`   ra   r   r-   r.   r   r2   r   r4   r   r;   r;   r<   r      s   zOnTheWayTrip.postNr   r;   r;   r;   r<   r      rK   r   c                   @   r   )CompleteTripc                 C   s   t |}|d krtdtjddtjdS zKt|j}tjj	||d d}t
  |_t
  d|_tt||_tt|j|_|  t|j t||_tdtjddtjdW S  tjyu   td	tjddtjd Y S w )
Nr!   r"   Fr$   r&   r}   rM   zTrip completed successfully.r+   )r   r   r   r,   r_   r`   ra   r   r-   r.   r   r~   rk   trip_end_dater   r   trip_end_timerd   r   r^   r   
net_amountr   rl   r   reference_idr2   r   r4   r   r;   r;   r<   r      s"   

zCompleteTrip.postNr   r;   r;   r;   r<   r      rK   r   )2r_   django.httpr   django.shortcutsr   rest_frameworkr   r   r   r   django.utils.decoratorsr	   django.views.decorators.csrfr
   django.viewsr   DRIVERZZ_DRIVER_APISr   authentication.authr   rates.modelsr   r   r   trips.modelsr   transactions.modelsr   django.db.modelsr   serializersr   r   r   trips.helpersr   r   r   r   razorpayClientRAZORPAY_KEY_IDRAZORPAY_KEY_SECRETclientr    rA   r{   r|   r   r   r;   r;   r;   r<   <module>   s<   



F

