3
؀h1                 @   s  d dl mZmZmZmZmZmZmZ d dlmZm	Z	 d dl
Z
d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlT d	d
 Zejddgddd Zejddd Zejddd Zejddgddd Zejddgddd ZdS )    )Flaskjsonifyrender_templateurl_forrequestredirectjson)datetime	timedeltaN)app)db)VehicleChargesSeed)Vehicle)*c             C   s   | j j  d S )N)sessionclose)self r   ,/booking_service/app/routes/vehicles_urls.pyr      s    r   z/vehicle/charges/newPOST)methodsc              C   sB  g } y&t jd j  t jd s(| jd W n, tk
rV } z| jd W Y d d }~X nX y&t jd j  t jd s|| jd W n, tk
r } z| jd W Y d d }~X nX y&t jd j  t jd s| jd W n. tk
r  } z| jd	 W Y d d }~X nX y(t jd
 j  t jd
 s(| jd W n. tk
rX } z| jd W Y d d }~X nX y,tt jd j  t jd s| jd W n. tk
r } z| jd W Y d d }~X nX y(t jd j  t jd s| jd W n. tk
r } z| jd W Y d d }~X nX y0tt jd j  tt jd s<| jd W n. tk
rl } z| jd W Y d d }~X nX y(t jd j  t jd s| jd W n. tk
r } z| jd W Y d d }~X nX | rg }|jd td| idfS tjj	t
jt
jd kj }xF|D ]>}|jj t jd
 j krg }|jd td|idfS qW t
ttj t jd t jd t jd t jd
 j t jd t jd t jd t jd t jd d
}tjj| y0tjj  tt g }|jd  td|id!fS  tk
r< } z6tjj  tt g }|jd" t|t|d#dfS d }~X nX d S )$Nincome_account_codezIncome Account Code is empty.zIncome Account Code is missing.department_analysis_codez"Department Analysis Code is empty.z$Department Analysis Code is missing.outlet_analysis_codezOutlet Analysis Code is empty.z Outlet Analysis Code is missing.categoryzCategory is empty.zCategory is missing.costzCost is empty.zCost is missing.currencyzCurrency is empty.zCurrency is missing.
max_numberzMax number is empty.zMax number is missing.
session_idzSession ID is empty.zSession ID is missing.z5You appear to be missing some data. Please try again.messagesi  z+That category already exists in the system.messagedescription)
vehicle_charge_public_idvehicle_charge_income_codevehicle_charge_outlet_codevehicle_charge_dept_codevehicle_charge_categoryvehicle_charge_category_costvehicle_charge_cost_currencyvehicle_charge_descriptionmaximum_numberr   zSuccessfully added a vehicle.   zAThere was an error adding that vehicle category. Try again later.)r    error)r   r   stripappendKeyErrorstrr   r   r   queryr   filterdeletion_markerallr&   loweruuiduuid4titleaddcommitr   	Exceptionrollback)r   er    Zcheck_categoryZeach_categoryvehicler   r   r   add_new_vehicle_charge   s    










r?   z/vehicle/charges/viewc              C   sx  t jjtjtjd kjtjdkj dkr4tj	  t jjtjtjd kj
tjj j } | szg }|jd td|iS g }x| D ]}i }|j|d< |j|d< t|j|d< y8tjtj|j}|j d d d	 |d
< |j|d< W n( tk
r   d|d
< |j|d< Y nX d|d< d |d< d|d< |j|d< |j|d< |j|d< |j|d< |j|d< |j| qW td|iS d S )Nz$99330ddf-4bd6-4c5e-8844-66b6d7ba1134r   zDThere are no vehicle categories present in the system at the moment.r    r"   r&   r'   datacurrency_namer(   vehicle_charge_cost_currency_idzN/Adiscountdiscount_reasonvehiclesr   r#   r$   r%   r)   )r   r   r1   r   r2   r3   r"   countr   seed_default_vehicle_chargesorder_byvehicle_charge_idascr4   r.   r   r&   floatr'   requestsgetget_currencyformatr(   r   r;   r*   r#   r$   r%   r)   )get_all_vehicle_chargesoutputsinglereturn_datar   r   r   r   view_all_vehicles_charges   sF    








rT   z!/vehicle/charges/view/<charge_id>c             C   sx  t jjtjtjd kjtjdkj dkr4tj	  t jjtjtjd kjtj| kj
 }|szg }|jd td|iS g }x|D ]}i }|j|d< |j|d< t|j|d< y8tjtj|j}|j d d d	 |d
< |j|d< W n( tk
r   d|d
< |j|d< Y nX d|d< d |d< d|d< |j|d< |j|d< |j|d< |j|d< |j|d< |j| qW td|iS d S )Nz$99330ddf-4bd6-4c5e-8844-66b6d7ba1134r   z6That no vehicle category is not present in the system.r    r"   r&   r'   r@   rA   r(   rB   zN/ArC   rD   rE   r   r#   r$   r%   r)   )r   r   r1   r   r2   r3   r"   rF   r   rG   r4   r.   r   r&   rK   r'   rL   rM   rN   rO   r(   r   r;   r*   r#   r$   r%   r)   )	charge_idrP   rQ   rR   rS   r   r   r   r   view_single_vehicle_charge   sF    








rV   z/vehicle/charges/editPATCHc              C   s  g } y&t jd j  t jd s(| jd W n, tk
rV } z| jd W Y d d }~X nX y&t jd j  t jd s|| jd W n, tk
r } z| jd W Y d d }~X nX y&t jd j  t jd s| jd W n. tk
r  } z| jd	 W Y d d }~X nX y(t jd
 j  t jd
 s(| jd W n. tk
rX } z| jd W Y d d }~X nX y(t jd j  t jd s| jd W n. tk
r } z| jd W Y d d }~X nX y,tt jd j  t jd s| jd W n. tk
r } z| jd W Y d d }~X nX y(t jd j  t jd s4| jd W n. tk
rd } z| jd W Y d d }~X nX y0tt jd j  tt jd s| jd W n. tk
r } z| jd W Y d d }~X nX y(t jd j  t jd s| jd W n. tk
r } z| jd W Y d d }~X nX | rBg }|jd td| idfS tjj	t
jt
jd kjt
jt jd kj }|sg }|jd td |id!fS t jd |_t jd
 |_t jd |_t jd j |_t jd |_t jd |_t jd" |_t jd |_t jd |_tj |_y0tjj  tt g }|jd# td |id$fS  tk
r } z6tjj  tt g }|jd% t|t|d&dfS d }~X nX d S )'NrU   zCharge ID is empty.zCharge ID is missing.r   zIncome Account Code is empty.zIncome Account Code is missing.r   z"Department Analysis Code is empty.z$Department Analysis Code is missing.r   zOutlet Analysis Code is empty.z Outlet Analysis Code is missing.r   zCategory is empty.zCategory is missing.r   zCost is empty.zCost is missing.r   zCurrency is empty.zCurrency is missing.r   zMax number is empty.zMax number is missing.r   zSession ID is empty.zSession ID is missing.z5You appear to be missing some data. Please try again.r   i  z%That vehicle category does not exist.r    i  r!   z2Successfully changed a vehicle category's details.r+   zAThere was an error adding that vehicle category. Try again later.)r    r,   ) r   r   r-   r.   r/   r0   r   r   r   r1   r   r2   r3   r"   firstr#   r$   r%   r8   r&   r'   r(   r)   r*   r   r	   now
updated_atr:   r   r;   r<   )r   r=   r    Zget_vehicler   r   r   edit_vehicle_charge   s    









r[   z/vehicle/charges/deletec           ,   C   s  g } y&t jd j  t jd s(| jd W n, tk
rV } z| jd W Y d d }~X nX y&t jd j  t jd s|| jd W n, tk
r } z| jd W Y d d }~X nX | rg }|jd td| id	fS tjjt	j
t	jd kj
t	jt jd kj }|s g }|jd
 td|idfS d|_t jd |_y0tjj  tt g }|jd td|idfS  tk
r } z6tjj  tt g }|jd t|t|dd	fS d }~X nX d S )NrU   zCharge ID is empty.zCharge ID is missing.r   zSession ID is empty.zSession ID is missing.z5You appear to be missing some data. Please try again.r   i  z3That vehicle category does not exist in the system.r    i     z$Deletion successful. Please proceed.r+   zCThere was an error deleting that vehicle category. Try again later.)r    r,   )r   r   r-   r.   r/   r   r   r   r1   r   r2   r3   r"   rX   r   r:   r   r;   r<   r0   )r   r=   r    Z
get_charger   r   r   delete_single_vehicle_chargeT  sL    







r]   )flaskr   r   r   r   r   r   r   r	   r
   pymysqlosmathrL   r6   routesr   r   routes.seed_functionsr   database.vehicler   	variablesr   router?   rT   rV   r[   r]   r   r   r   r   <module>   s   $(k22o