+
    7iH0                         R t ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RI	H
t
 ^ RIHt  ^ RIt^ RItR tR tR tRR ltR	 tR
 tR tRR ltRR ltR#   ] d    RtRt L0i ; i)z:Shared helpers: geo, taxa, printing, delivery persistence.N)session)conectar)obter_dados_lojac                    R p\         P                  ! W ,
          4      p\         P                  ! W1,
          4      p\         P                  ! V^,          4      ^,          \         P                  ! \         P                  ! V 4      4      \         P                  ! \         P                  ! V4      4      ,          \         P                  ! V^,          4      ^,          ,          ,           p^\         P                  ! \         P
                  ! V4      \         P
                  ! ^V,
          4      4      ,          p\        WH,          ^4      # )g     @)mathradianssincosatan2sqrtround)	lat1lon1lat2lon2Rdlatdlonacs	   &&&&     1C:\Users\joaqu\OneDrive\LojaOnline\helpers_app.pyhaversine_kmr      s    A<<$D<<$Da$((4<<+=">$,,W[J\A]"]`d`h`himpqiq`rvw`w"wwA	DJJtyy|TYYq1u%566A?    c                    V '       d   \        V P                  4       4      ^8  d   R# RpRV RRR^/pRR/p \        P                  ! WV^R	7      pVP                  ^8w  d   R# VP                  4       pV'       g   R# \        V^ ,          R
,          4      \        V^ ,          R,          4      3#   \         d   p\        RT4       Ru Rp?# Rp?ii ; i)   Nz*https://nominatim.openstreetmap.org/searchqformatjsonlimit
User-Agentznovaloja-geocoder/1.0)paramsheaderstimeoutlatlonz[GEOCODE ERRO]NN)	lenstriprequestsgetstatus_coder   float	Exceptionprint)enderecourlr    r!   rdataes   &      r   geocodificarr3      s    s8>>+,q0
6C8Xvw:F45G
LLWaH==CvvxT!WU^$eDGEN&;;; "s/   +B0 $B0 <B0 >1B0 0C;CCCc           
          R pV RV RV  RV RV R2
p\         P                  ! V^RR/R7      pVP                  ^8w  d   R# VP                  4       pV'       d   VP                  R	4      R
8w  d   R# VP                  R4      ;'       g    . pV'       g   R# V^ ,          P                  R4      p	V	f   R# \	        V	R,          ^4      #   \
         d   p
\        RT
4        Rp
?
R# Rp
?
ii ; i)z/http://router.project-osrm.org/route/v1/driving/,;z.?overview=false&alternatives=false&steps=falser   znovaloja-osrm/1.0)r"   r!   NcodeOkroutesdistanceg     @@z[OSRM ERRO])r(   r)   r*   r   r   r,   r-   )lat_lojalon_lojalat_clilon_clibaser/   r0   r1   r:   dist_mr2   s   &&&&       r   distancia_osrm_kmrB   1   s    @az8*AgYay@noLLa,@S1TU==Cvvxtxx'4/(#))rz*>Vf_a(( mQs;   ?C	 C	 C	 3C	 
C	 C	 6C	 	C+C&&C+c                   \        V4      pVR,          pVR,          pV P                  RR4      V P                  RR4      V P                  RR4      V P                  RR4      V P                  RR4      R	.pR
P                  V Uu. uF  qf'       g   K  VNK  	  up4      p\        V4      w  rVe*   V	e&   \	        W4W4      p
V
e   WV	3#  \        W4W4      pWV	3# R# u upi   \         d   p\        RT4       ^ Y3u Rp?# Rp?ii ; i)uD   Calcula distância entre a loja e o cliente usando dados cadastradoslatitude	longituder.    nrocasabairrocidadeestadoBrasil, Nz[HAVERSINE ERRO])    NN)r   r)   joinr3   rB   r   r,   r-   )dados
id_clientelojaloja_latloja_lonpartespendereco_strr>   r?   	dist_osrmdist_havr2   s   &&           r   calcular_distancia_clienterY   G   s   J'DJHK H 			*b!		)R 		(B		(B		(BF 995A1aa56L#L1Gw2%h'K	 w..	'#HIHg--  6  	'$a(g&&	's*   	CC
C! !D,D;DDc           	        RpRp \        4       pVP                  RR7      pVP                  R4       VP                  4       pV'       gC   \	        R4         V'       d   VP                  4         T'       d   TP                  4        R# R# VP                  R4      pVP                  R4      pVea   W8  d[   Ve   \        V4      MRp\	        R	V  R
V RV 24       T V'       d   VP                  4         T'       d   TP                  4        # # \        ^^4       F  pVP                  RV R24      pVP                  RV R24      p	Vf   K3  W8:  g   K;  V	e   \        V	4      MRp\	        R	V  RV RV 24       Tu  V'       d   VP                  4         T'       d   TP                  4        # # 	  VP                  R4      p
V
e   \        V
4      MRp\	        R	V  RV 24       T V'       d   VP                  4         T'       d   TP                  4        # #   \         d     ELi ; i  \         d     R# i ; i  \         d     ELZi ; i  \         d     # i ; i  \         d     Li ; i  \         d     # i ; i  \         d     Li ; i  \         d     # i ; i  \         dp   p\	        RT4        Rp? T'       d   TP                  4        M  \         d     Mi ; i T'       d   TP                  4        R# R#   \         d     R# i ; iRp?ii ; i   T'       d   TP                  4        M  \         d     Mi ; i T'       d   TP                  4        i i   \         d     i i ; i; i)uF   Calcula taxa de entrega baseado na distância usando tabela txentrega.NT
dictionaryz'SELECT * FROM txentrega WHERE chave = 1z5[TAXA ENTREGA] Nenhuma faixa configurada em txentregag        faixa1_dfaixa1_vu   [TAXA ENTREGA] Distância u   km é menor que faixa1_d (u!   km), retornando taxa mínima: R$ faixa_d_vzkm cai na faixa z: R$ 	faixa10_vz"km ultrapassa todas as faixas: R$ z[TAXA ENTREGA ERRO])
r   cursorexecutefetchoner-   closer,   r)   r+   range)	distanciaconnrc   r_   	faixa_1_d	faixa_1_vtaxaifaixa_dfaixa_v
faixa_10_v
taxa_finalr2   s   &            r   calcular_taxa_entregarr   d   sc   DF/z-@A!IJ<		

 E IIj)	IIj)	 Y%:'0'<5##D,YK7QR[Q\\}  C  ~D  E (		

 1 q"Aii%s".Gii%s".G"y';)0)<uW~#29+=MaSPUVZU[\]		

 1  YY{+
*4*@U:&c
*9+5WXbWcde		

   		
  		  		
  		  		
  		  		
  		  #Q'	 			

  			 			

  		si  AJ+ H  1H2 9H2 AJ+  I9II:J+ J+ (J+ I'I8$I866J+ -J	JJ H/.H/2I IIII$#I$'I54I58JJ	JJJ('J(+L%6L L( K   K.-K.2L :L LL L%%L( (N *MN MN MN M/M/-N /M=:N <M==N c                    V '       g   V # / pV P                  4        F8  w  r#\        V\        P                  4      '       d   \	        V4      W&   K4  W1V&   K:  	  V# )z6Converte Decimal para float para que jsonify funcione.)items
isinstancedecimalDecimalr+   )rowoutkvs   &   r   convert_typesr|      sK    

C		a))1XCFF	 
 Jr   c                 &   Rp Rp \        4       p V P                  4       pVP                  R4       VP                  R4       VP                  4       pV'       d
   V^ ,          MR  V'       d   VP	                  4         T '       d   T P	                  4        # #   \
         d     L(i ; i  \
         d     # i ; i  \
         dp   p\        RT4        Rp? T'       d   TP	                  4        M  \
         d     Mi ; i T '       d   T P	                  4        R# R#   \
         d     R# i ; iRp?ii ; i   T'       d   TP	                  4        M  \
         d     Mi ; i T '       d   T P	                  4        i i   \
         d     i i ; i; i)uE   Obtém o nome da impressora ativa da tabela impressoras (imprenro=1).Nz
            CREATE TABLE IF NOT EXISTS impressoras (
                id INT AUTO_INCREMENT PRIMARY KEY,
                nomedaimpressora VARCHAR(255) NOT NULL,
                imprenro TINYINT NOT NULL DEFAULT 0
            )
        zCSELECT nomedaimpressora FROM impressoras WHERE imprenro = 1 LIMIT 1z[IMPRESSORA DB ERRO])r   rc   rd   re   rf   r,   r-   )ri   rc   rx   r2   s       r   get_printer_from_dbr~      s_   DFz	
 	\]oos1v$&
		

   		
  		  $a(	 			

  			 			

  		s   AB; &B?B*B*B'&B'*B87B8;D5D0D8 C0 0C>=C>D 
D D-,D-0D55D8 8F:EFE!F E!!F%E?-E?=F?F
FFFc                   \         P                  R8w  g	   \        f   R#  T;'       g    \        P                  ! 4       p\        P                  ! V4      p \        P
                  ! V^R4      p \        P                  ! V4       \        V \        4      '       d   T pM\        V 4      P                  RRR7      pVR,          pV'       dq   VP                  4       P                  4       pRV9   g   R	V9   d   VR
,          pMCRV9   d   VR,          pM2RV9   g   RV9   g   RV9   d   VR,          pMVR
,          pM	VR
,          p\        P                  ! WF4       \        P                  ! V4       \        P                  ! V4        \        P                   ! V4       R#   \        P                  ! T4       i ; i  \        P                   ! T4       i ; i  \"         d   pR\        T4      3u Rp?# Rp?ii ; i)z+Envia texto RAW para impressora no Windows.win32NFcp1252replace)errorss   dbematechdarumas   mepsons   ielgintancadiebolds   V )Fu>   Impressão silenciosa disponível apenas no Windows (pywin32).)PedidoNRAW)TN)sysplatform
win32printGetDefaultPrinterOpenPrinterStartDocPrinterStartPagePrinterru   bytesstrencoder'   lowerWritePrinterEndPagePrinterEndDocPrinterClosePrinterr,   )	conteudoprinter_namemarca_impressoranomehPrinterhJobr1   marcar2   s	   &&&      r   send_to_printerr      s   
||w*"4VV ==z;;=))$/	.--h;RSD3++H5h..#Dx=///KDO+D' 0 6 6 8 > > @%.(e2C K/D$- K/D$-E1AYRWEW O3D K/D+''7))(3((2##H- ((2##H- c!f}sS   G ,G F; 0DF  1F; G  F88F; ;GG G8!G3-G83G8c                $
   RpRp V P                  R4      pRRRRRRRRRRR	RR
^ /pV EF  pVP                  R4      '       d&   VP                  RR4      P                  4       VR&   K@  VP                  R4      '       d&   VP                  RR4      P                  4       VR&   K|  VP                  R4      '       d&   VP                  RR4      P                  4       VR&   K  VP                  R4      '       d&   VP                  RR4      P                  4       VR	&   K  VP                  R4      '       d   VP                  RR4      P                  4       pVP                  R4      p	\	        V	4      ^8  d   V	^ ,          P                  4       VR&   \	        V	4      ^8  d   V	^,          P                  4       VR&   EK  EK  RV9   g   EK  VP                  RR4      P                  4       p
 \        V
4      VR
&   EK  	  VR,          '       gC   \        R4        V'       d    VP                  4        V'       d    VP                  4        R# R# \        4       pVP                  RR7      pV EF  pVP                  RR4      p\        VP                  R^ 4      4      p\        VP                  R^4      4      pVP                  RR4      pVP                  RR4      pRV9   d   RpRp\        P                  ! R4      p. R)OpVR
,          VR,          VR,          VR,          VR,          VR,          VR	,          VVVVVVP                  RVR,          4      V.pVe#   VP                  R4       VP                  V4       RRP                  V4       R RP                  R!.\	        V4      ,          4       R"2pVP!                  VV4       VP#                  4        \        R#V R$V R%V R&VR,           R'V 2
4       EK  	   T'       d    TP                  4        T'       d    TP                  4        R# R#   \         d    ^ TR
&    EK!  i ; i  \         d     EL+i ; i  \         d     R# i ; i  \         d   p\        R(T 24       h Rp?ii ; i  \         d     Li ; i  \         d     R# i ; i  T'       d$    TP                  4        M  \         d     Mi ; iT'       d$    TP                  4        i   \         d     i i ; ii ; i)*z1Grava dados do pedido na tabela deliverypendente.N
telefonerF   cepr   r.   rG   complemento	nropedidozTel:zCEP:zNome:zCompl:zEnd:r6   zPEDIDO NRO:u/   [DELIVERY PENDENTE] Sem telefone, não gravandoTr[   precoqtdchaveclassezTAXA ENTREGA	TXENTREGArP   clienteformapagamentozINSERT INTO deliverypendente (rL   z
) VALUES (z%s)z[DELIVERY PENDENTE] z	 (chave: z
, classe: z) gravado para z
 | Forma: z[DELIVERY PENDENTE ERRO] )r   r   r   r   r.   rG   r   produtor   
quantidadecodigoprodutor   r   rP   )split
startswithr   r'   r&   intr,   r-   rf   r   rc   r)   r+   r   appendrN   rd   commit)r   produtosforma_pagamentori   rc   linhascliente_datalinhaend_inforT   nro_strprod	prod_nome
prod_precoprod_qtd
prod_chaveprod_classerP   colunasvaloressqlr2   s   &&&                   r   gravar_delivery_pendenter      s   DFf%2Br2
 E''+0==+D+J+J+LZ(!!&))&+mmFB&?&E&E&GU#!!'**',}}Wb'A'G'G'IV$!!(++.3mmHb.I.O.O.Q]+!!&)) ==4::<!,v;!#/5ay/@L,v;!#.4Qioo.?L+ $%'--r:@@B203GL-' , J''CDt  

 } z-D,Itxx34J488E1-.H'2.J((8R0K*(
) \2JG" [)Z(U#V$Z(Y']+  L,@AG  */0/2499W3E2FjQUQZQZ\`[adghodp[pQqPrrstCNN3(KKM&yk:,jQ\P]]lmy  {E  nF  mG  GQ  Ra  Qb  ca n  

 G ! 201L-2B  
    )!-.  
    

  s  A9Q2  ;Q2 <;Q2 8;Q2 4BQ2 	 Q2 *P68Q2 Q2 !Q :Q  F1Q2 	R "R% 6QQ2 
QQ2 QQ Q/.Q/2R=RRR7 R"!R"%R43R47	TSTS TS  
T,S=<T=TT
TT)Nr%   )__doc__rv   r   r   mysql.connectormysqlr(   flaskr   databaser   services.dados_lojar   r   win32apir,   r   r3   rB   rY   rr   r|   r~   r   r    r   r   <module>r      sx    @   
     0&,:3l
 F$NjC  JHs   A A#"A#