
    #j"                     d   d dl mZ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  ej                   dd	g
      ed               Z ej                   ddg
      ed               Z ej                   dd	g
      ed               Z ej                   ddg
      ed               Zy)    )jsonifyrequest)api_bp)SessionLocal)MonthlyTaxConfigProductTaxProfileProductPurchaseCost)Ad)require_auth)datetimez/finance/tax-configsGET)methodsc            
      j   t               } 	 | j                  t              j                  t        j                  j                               j                         }|D cg c]Y  }|j                  |j                  t        |j                        t        |j                        |j                  |j                  d[ }}t        d|d      | j                          S c c}w # t        $ r3}t        dt!        |      d      dfcY d }~| j                          S d }~ww xY w# | j                          w xY w)N)idreference_monthfull_das_ratedas_without_icms_ratenotes	is_activeTsuccessdataFr   error  )r   queryr   order_byr   descallr   floatr   r   r   r   r   close	Exceptionstr)dbconfigscresultes        F/var/www/hypershopcomercio.com.br/hyper-ai/app/api/endpoints/fiscal.pyget_tax_configsr*   	   s     
B((+,556F6V6V6[6[6]^bbd 	
  dd#$#4#4!&q!7).q/F/F)G[[	
 	
 489 	
	
  A53q6:;S@@

A 	
sC   AC! AC<C! C! !	D*DDD  DD   D2POSTc            	      4   t         j                  } t               }	 | j                  d      }|s t	        ddd      df|j                          S |j                  t              j                  t        j                  |k(        j                         }|st        |      }|j                  |       t        | j                  dd            |_        t        | j                  d	d            |_        | j                  d
d      |_        | j                  dd      |_        t#        j$                         |_        |j)                          |j+                  |       t	        d|j,                  |j                  t        |j                        t        |j                        dd      |j                          S # t.        $ rC}|j1                          t	        dt3        |      d      dfcY d }~|j                          S d }~ww xY w# |j                          w xY w)Nr   Fzreference_month is requiredr   i  )r   r   r   r   r    r   T)r   r   r   r   r   r   )r   jsonr   getr   r!   r   r   filterr   firstaddr    r   r   r   r   r   utcnow
updated_atcommitrefreshr   r"   rollbackr#   )r   r$   	ref_monthconfigr(   s        r)   create_or_update_tax_configr:      s    <<D	B!HH./	u7TUVX[[< 	
9 *+223C3S3SW`3`aggi%i@FFF6N$TXXoq%AB',TXX6Mq-Q'R$xx,88K6$OO-
		


6ii#)#9#9!&v';';!<).v/K/K)L	
  	
	  A
53q6:;S@@

	A 	
s6   "F6 EF6 6	H?(G='H(H =HH Hz/ads/<ad_id>/fiscal-profilec                    t               }	 |j                  t              j                  t        j                  | k(        j                         }|s t        ddd      df|j                          S |j                  t              j                  t        j                  | k(        j                         }|j                  t              j                  t        j                  | k(  t        j                  dk(        j                         }d }|r|j                  |j                  |j                  |j                  |j                   |j"                  |j$                  |j&                  |j(                  |j*                  t-        |j*                        nd |j.                  t-        |j.                        nd |j0                  t-        |j0                        nd |j2                  t-        |j2                        nd |j4                  |j                  d}d }|r|j                  t-        |j6                        |j8                  t-        |j8                        nd |j:                  t-        |j:                        nd|j<                  t-        |j<                        nd|j>                  t-        |j>                        nd|j@                  |jB                  |jD                  |j                  d
}t        d||d	d
      |j                          S # tF        $ r3}t        dtI        |      d      dfcY d }~|j                          S d }~ww xY w# |j                          w xY w)NFAd not foundr     T)skuproduct_origin	origin_ufdestination_uf_defaultncmcesthas_sthas_ipi	has_difalmva_rateipi_rateorigin_icms_ratedestination_icms_rater   r   g        )
r>   	real_costnf_valuefreight_costpackaging_costother_costssupplier_name	nf_numberdata_sourcer   )tax_profilepurchase_costr   r   )%r   r   r
   r0   r   r1   r   r!   r   mlb_idr	   r   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   r    rH   rI   rJ   r   rK   rL   rM   rN   rO   rP   rQ   rR   r"   r#   )ad_idr$   adprofilecosttax_profile_datapurchase_cost_datar(   s           r)   get_fiscal_profiler\   G   s    
B8XXb\  %0668u~FGLj 	
g ((,-445F5M5MQV5VW]]_xx+,33&&%/))T1
 %' 	
  {{")"8"8$..*1*H*H{{!.."??$..7>7G7G7SE'"2"23Y]7>7G7G7SE'"2"23Y]GNG_G_GkE'*B*B$CquQXQnQnQzw/L/L)M  AE $.. $ "xx"4>>248MM4ME$--0SW<@<M<M<Yd&7&7 8_b@D@S@S@_%(;(;"<eh:>:J:J:VuT%5%56\_!%!3!3!^^#//!^^" /!3
  	
  A53q6:;S@@

A 	
s7   AL 1JL 	M	M.M	/M M		M MPUTc                 
   t         j                  }t               }	 |j                  t              j                  t        j                  | k(        j                         }|s t        ddd      df|j                          S |j                  d      }|j                  d      }|r:|j                  t              j                  t        j                  | k(        j                         }|st        |       }|j                  |       |j                  d      |_        |j                  d	d
      |_        |j                  d      |_        |j                  d      |_        |j                  d      |_        |j                  d      |_        |j                  dd      |_        |j                  dd      |_        |j                  dd      |_        |j                  d      t/        |j                  d            nd |_        |j                  d      t/        |j                  d            nd |_        |j                  d      t/        |j                  d            nd |_        |j                  d      t/        |j                  d            nd |_        |j                  d      |_        |j                  dd      |_        t=        j>                         |_         |rt/        |j                  dd            }|j                  d      t/        |j                  d            nd }d }	||dkD  r||z  dz  }	|j                  tB              j                  tB        j                  | k(  tB        j:                  dk(        jE                         }
|
D ]"  }d|_        t=        j>                         |_#        $ tC        | |j                  d      |||	t/        |j                  dd            t/        |j                  dd            t/        |j                  dd            |j                  d       |j                  d!      |j                  d"d#      dt=        j>                         $      }|j                  |       |jI                          t        dd%d&      |j                          S # tJ        $ rC}|jM                          t        dtO        |      d      d'fcY d }~|j                          S d }~ww xY w# |j                          w xY w)(NFr<   r   r=   rS   rT   )rU   r>   r?   nacionalr@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   r   r   TrK   r   rL   g      Y@rM   rN   rO   rP   rQ   rR   manual)rU   r>   rK   rL   nf_percentagerM   rN   rO   rP   rQ   rR   r   effective_fromz#Fiscal profile updated successfully)r   messager   )(r   r.   r   r   r
   r0   r   r1   r   r!   r/   r   rU   r2   r>   r?   r@   rA   rB   rC   rD   rE   rF   r    rG   rH   rI   rJ   r   r   r   r3   r4   r	   r   effective_untilr5   r"   r7   r#   )rV   r   r$   rW   tax_data	cost_datarX   rK   rL   ra   active_costsacnew_costr(   s                 r)   update_fiscal_profilerj      s-    <<D	BLXXb\  %0668u~FGLR 	
O 88M*HH_-	 hh01889J9Q9QUZ9Z[aacG+59w",,u-GK%-\\2BJ%OG" ([ 9G-5\\:R-SG*",,u-GK#<</GL%\\(E:GN&ll9e<GO ([% @GBJ,,zBZBfuX\\*%=>lpGBJ,,zBZBfuX\\*%=>lpGRZR^R^_qRrR~uX\\:L-M'N  EIG$\d\h\h  jA  ]B  ]NE(,,?V2W,X  TXG)$LL1GM ([$ ?G!)!2G immK;<I;D==;T;`uY]]:67fjH M#	A!)I!5 > 88$78??#**e3#--5 ce 
 # 7$%-__%6"7
 +MM%(#!+"9==#CD$Y]]3CQ%GH!)--q"AB'mmO<#--4%MM-B'0H FF8
		44YZ[ 	
	  A
53q6:;S@@

	A 	
s7   AR) PR) )	S52(S0S5S8 0S55S8 8T
N)flaskr   r   app.apir   app.core.databaser   app.models.fiscalr   r   r	   app.models.adr
   app.api.endpoints.authr   r   router*   r:   r\   rj        r)   <module>rt      s    "  * V V  / $ug6  7( $vh7$  8$L +eW=:  >:x +eW=O  >Ors   