
    uwh!                        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'jP                  ejR                  ejT                  f      Z+ eed       G d de             Z, eed       G d de             Z-ed        Z. eed       G d de             Z/ eed       G d de             Z0y)    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 Zy)Tripc                 j   t        |      }|d k(  r-t        dt        j                  ddt        j                        S 	 t        j
                  j                  |j                  j                  d            }t        j
                  j                         }t        |d      }t        |d      }t        |j                  |j                  t        j                  d	dt        j                        S # t        $ r7}t        d
t        j                  ddt        j                        cY d }~S d }~ww xY 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es           K/mnt/ACDE16A4DE16673C/PROJECTS/DRIVERZZ/DRIVERZZ_DRIVER_APIS/trips/views.pyr0   zTrip.get   s4   w'd?+<vGcGc dkpy  zV  zV  W  Wy}}((gkkood.C(D))++Du=
!.u4!@#Z__H[H[gmgygy$z  BG  PV  Pb  Pb  c  c y#0_kq  lG  lG  %H  OT  ]c  ]x  ]x  y  yys   B2C2 2	D2;,D-'D2-D2N__name__
__module____qualname__r0        r=   r!   r!      s    yrC   r!   c                       e Zd Zd Zy)TripListc                    t        |      }|d k(  r-t        dt        j                  ddt        j                        S t        j
                  j                  |      }|j                  j                  d      }|dk(  r|j                  d	      }n*|d
k(  r|j                  d      }n|j                  d      }t        |d      }t        |j                  t        j                  ddt        j                        S )Nr#   r$   Fr&   )	driver_idr   0   r   1)
status__lt)
status__gtTr)   )tripsr   )r   r   r   r.   r   r/   filterr1   r0   r   r3   r4   )r7   r8   r9   rM   trip_statusr:   s         r=   r0   zTripList.get1   s    w'd?+<vGcGc dkpy  zV  zV  W  WMM((W(=E!++//(3Kc!a0#!4!4(T:J*//VEWEW X_dmsmm  A  ArC   Nr>   rB   rC   r=   rE   rE   /   s    ArC   rE   c                    t        j                  | j                        }	 d}|d   }t        j                  |d   d      j                         }t        j                  |d   d      j                         }t        j                  t        |d         dz   t        |      z   d      }t        j                  t        |d	         dz   t        |      z   d      }||z
  }t        |j                         d
z        }|dk\  rn5d|z
  }	|t        |	      z   }||z
  }t        |j                         d
z        }	 t        j                  j                  ||d   |d   d|d         j                  d      }
t        j                  j                  ||d   |d   d|d         j                  d      }t        |
      dk(  rxt        j                  j                  ||d   |d   d|d         j                  d      }
t        j                  j                  ||d   |d   d|d         j                  d      }|
d   j                  }|d   j                  }t         j                  j#                  d      }t        j                  |j$                  d      j                         }t         j                  j#                  d      }t        j                  |j$                  d      j                         }d}d}|j&                  |j(                  f}|j&                  |j(                  f}t+        ||||      }|j-                         |j-                         k(  r|dz  }||z
  }|||z  z  }|||z  z  }t.        j                  j1                         }t3        |d      }t5        ||j6                  t8        j:                  ddt8        j:                        S # t<        $ rC}t?        |       t5        dg t8        j@                  ddt8        j@                        cY d }~S d }~ww xY w# t<        $ rC}t?        |       t5        dg t8        j@                  ddt8        j@                        cY d }~S d }~ww xY 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__gterZ   r[   dayr\   durationr   )duration__ltrZ   r[   r^   r\   z	-duration)day_typerI   Tr)   )amountr,   r   Fr&   )!jsonloadsbodyr   strptimer	   strinttotal_secondsr   r   r/   rN   order_bylenrater   r0   rQ   hourminuter   dater   r2   r   r   r3   r   r4   r5   printr6   )r8   request_datarb   rQ   rS   start_timestampend_timestamp
time_deltatotal_minutesextra_minutesrate_day
rate_nightday_chargesnight_chargesr^   	day_startday_endday_minutesnight_minutesr,   r;   r<   s                         r=   calculateFarer   C   s   ::gll+L>P#F+&&|L'A:NSSU
$$\*%=zJOOQ"++C\0J,KC,OPST^P_,_atu ))#l:.F*G*KCPXM*Y[no"_4
Z557;<C-M)Im,LLM&8J!9!9!;R ?@M(	T ((//m\hiw\x  JV  We  Jf  no  }I  JU  }V/  W  `  `  ak  lH"**11^jky^z  LX  Yg  Lh  pq  K  LW  X1  Y  b  b  cm  nJ8}!$,,33_klz_{  MY  Zh  Mi  qr  @L  MX  @Y3  Z  c  c  do  p&..55=amn|a}  O[  \j  Ok  st  BN  OZ  B[5  \  e  e  fq  r
"1+**K&qM..M""&&!&4C ))#..*EJJLI ""&&!&4C''
CHHJGKM")9)9:I||W^^4G/	7OUbcK##%););)==)Ao)K7MkK//Fmm33FMM%%'E*5t<N6^=P=P\b\n\n ov{  EK  EW  EW  X  X 	T!H1fFaFa binw}  xS  xS  T  T	T  PaqB&B]B]^ejsy  tO  tO  P  	PPsI   DP 5JO 	P8PPP PP 	Q(%8Q#Q(#Q(c                       e Zd Zd Zy)	StartTripc                    t        |      }|d k(  r-t        dt        j                  ddt        j                        S 	 t	        j
                  |j                        }t        j                  j                  ||d         }d|_        t        j                         j                         |_        t        j                         j                         j                  d      |_        |j#                          t        d	t        j$                  ddt        j$                        S # t        j&                  $ r0 t        d
t        j(                  ddt        j(                        cY S w xY w)Nr#   r$   Fr&   r(   rG   r(   rI   rR   zTrip canceled successfully.r-   )r   r   r   r.   rc   rd   re   r   r/   r0   r   nowro   trip_start_dater	   strftimetrip_start_timesaver4   DoesNotExistr6   r7   r8   r9   rq   r+   s        r=   postzStartTrip.post   sV   w'd?+<vGcGc dkpy  zV  zV  W  W	y#zz',,7}}((W<PTCU(V'/||~':':'<$'/||~':':'<'E'Ej'Q$		#0MY_YkYk$lsx  BH  BT  BT  U  U%% y#0_kq  lG  lG  %H  OT  ]c  ]x  ]x  y  yys   C#D# #A E&%E&Nr?   r@   rA   r   rB   rC   r=   r   r      s    yrC   r   c                       e Zd Zd Zy)CompleteTripc                 <   t        |      }|d k(  r-t        dt        j                  ddt        j                        S 	 t	        j
                  |j                        }t        j                  j                  ||d         }t        j                         j                         |_        t        j                         j                         j                  d      |_        t#        t%        |            |_        t#        t)        |j&                              |_        t-        |      |_        t        dt        j0                  ddt        j0                        S # t        j2                  $ r0 t        d	t        j4                  ddt        j4                        cY S w xY w)
Nr#   r$   Fr&   r(   r   rR   zTrip completed successfully.r-   )r   r   r   r.   rc   rd   re   r   r/   r0   r   r   ro   trip_end_dater	   r   trip_end_timerh   r   rb   r   
net_amountr   reference_idr4   r   r6   r   s        r=   r   zCompleteTrip.post   sy   w'd?+<vGcGc dkpy  zV  zV  W  W
y#zz',,7}}((W<PTCU(V%-\\^%8%8%:"%-\\^%8%8%:%C%CJ%O"!."67"%odkk&B"C$0$6!#0NZ`ZlZl$mty  CI  CU  CU  V  V%% y#0_kq  lG  lG  %H  OT  ]c  ]x  ]x  y  yys   DE A FFNr   rB   rC   r=   r   r      s    yrC   r   )1rc   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!   rE   r   r   r   rB   rC   r=   <module>r      s    $ # ! . . 4 4  ) , 3 3  ,  G G _ ^ 	x779U9UV	W +J/y4 y 0y( +J/At A 0A& AP APL +J/y y 0y$ +J/y4 y 0yrC   