;** Et_Z002C *******************************************************************
;* Monta Etiqueta p/ Imp. T‚rmica - Zebra - Interno             ** CACHARREL** *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 04/11/2010 por Bruno Carvalho
;Ultima Alteracao: 26/10/2011 por Bruno Carvalho
;Motivo Alteracao: Adicionado Codigo Concentrador
;Ultima Alteracao: 27/08/2013 por Andrey Fillipe
;Motivo Alteracao: Alterado Codigo Concentrador para Preco Unitario
;Ultima Alteracao: 30/10/2013 por Andrey Fillipe
;Motivo Alteracao: Adicionado mensagem referente a troca
;Ultima Alteracao: 23/04/2015 por Bruno Carvalho
;Motivo Alteracao: Novos ajustes na largura e altura da etiqueta com 3 colunas (2x0,25cm + 10,5cm x 5cm sem espa‡amento entre elas)
;Ultima Alteracao: 09/10/2015 por Andrey Fillipe
;Motivo Alteracao: Adicionado Segunda Tabela de Preco e Segundo Valor na etiqueta
;Ultima Alteracao: 02/12/2015 por Andrey Fillipe
;Motivo Alteracao: Retirado Proc TABELA2()
;Ultima Alteracao: 07/11/2018 por Bruno Carvalho
;Motivo Alteracao: Ajustes padrÆo p/ codigo de barras EAN8, EAN13 ou COD128

;*******************************************************************************
Proc Monta_Etiqueta()
Moveto "Answer"
Print File Arquivo "O\n"
Print File Arquivo "N\n"

; Utilizava at‚ 23.04.2015 -----------------------------------------------------
; Mas disseram que desde o final de 2014 saia as 6 primeiras corretas e as demais erradas
;Print File Arquivo "q768\n"
;Print File Arquivo "Q191,14+\n"
;-------------------------------------------------------------------------------

Print File Arquivo "q880\n"
Print File Arquivo "Q400+\n"
Print File Arquivo "S2\n"
Print File Arquivo "D8\n"
Print File Arquivo "ZT\n"

If Not IsBlank(CITEM[1])
   Then Print File Arquivo "A10,10,0,4,1,1,N,\""+NOMEMP[1]+"\"\n"
        Print File Arquivo "A10,55,0,2,1,1,N,\""+DITEA[1]+"\"\n"
        Print File Arquivo "A10,75,0,2,1,1,N,\""+DITEAA[1]+"\"\n"   
        Print File Arquivo "A10,120,0,2,1,1,N,\""+DITEB[1]+"\"\n" 
        Print File Arquivo "A10,165,0,2,1,1,N,\""+PRECO[1]+"\"\n" 
        Print File Arquivo "A10,210,0,2,1,1,N,\""+PRECO2[1]+"\"\n" 
        Switch
          Case Search("EAN13",TPBARRA[1])>0 : Print File Arquivo "B17,250,0,E30,2,2,45,B,\""+CITEM[1]+"\"\n" 
          Case Search("EAN8",TPBARRA[1])>0  : Print File Arquivo "B17,250,0,E80,2,2,45,B,\""+CITEM[1]+"\"\n" 
          OtherWise                         : Print File Arquivo "B17,250,0,1,2,2,45,B,\""+CITEM[1]+"\"\n"  
        EndSwitch      
        Print File Arquivo "A10,340,0,2,1,1,N,\""+OBS1[1]+"\"\n"
        Print File Arquivo "A10,360,0,2,1,1,N,\""+OBS2[1]+"\"\n"
EndIf

If Not IsBlank(CITEM[2])
   Then Print File Arquivo "A290,10,0,4,1,1,N,\""+NOMEMP[2]+"\"\n"
        Print File Arquivo "A290,55,0,2,1,1,N,\""+DITEA[2]+"\"\n"
        Print File Arquivo "A290,75,0,2,1,1,N,\""+DITEAA[2]+"\"\n"   
        Print File Arquivo "A290,120,0,2,1,1,N,\""+DITEB[2]+"\"\n"   
        Print File Arquivo "A290,165,0,2,1,1,N,\""+PRECO[2]+"\"\n" 
        Print File Arquivo "A290,210,0,2,1,1,N,\""+PRECO2[2]+"\"\n" 
        Switch
          Case Search("EAN13",TPBARRA[2])>0 : Print File Arquivo "B303,250,0,E30,2,2,45,B,\""+CITEM[2]+"\"\n"
          Case Search("EAN8",TPBARRA[2])>0  : Print File Arquivo "B303,250,0,E80,2,2,45,B,\""+CITEM[2]+"\"\n" 
          OtherWise                         : Print File Arquivo "B303,250,0,1,2,2,45,B,\""+CITEM[2]+"\"\n" 
        EndSwitch      
        Print File Arquivo "A290,340,0,2,1,1,N,\""+OBS1[2]+"\"\n"
        Print File Arquivo "A290,360,0,2,1,1,N,\""+OBS2[2]+"\"\n"
EndIf

If Not IsBlank(CITEM[3])
   Then Print File Arquivo "A568,10,0,4,1,1,N,\""+NOMEMP[3]+"\"\n"
        Print File Arquivo "A568,55,0,2,1,1,N,\""+DITEA[3]+"\"\n"
        Print File Arquivo "A568,75,0,2,1,1,N,\""+DITEAA[3]+"\"\n"   
        Print File Arquivo "A568,120,0,2,1,1,N,\""+DITEB[3]+"\"\n"   
        Print File Arquivo "A568,165,0,2,1,1,N,\""+PRECO[3]+"\"\n"
        Print File Arquivo "A568,210,0,2,1,1,N,\""+PRECO2[3]+"\"\n" 
        Switch
          Case Search("EAN13",TPBARRA[3])>0 : Print File Arquivo "B579,250,0,E30,2,2,45,B,\""+CITEM[3]+"\"\n" 
          Case Search("EAN8",TPBARRA[3])>0  : Print File Arquivo "B579,250,0,E80,2,2,45,B,\""+CITEM[3]+"\"\n" 
          OtherWise                         : Print File Arquivo "B579,250,0,1,2,2,45,B,\""+CITEM[3]+"\"\n" 
        EndSwitch      
        Print File Arquivo "A568,340,0,2,1,1,N,\""+OBS1[3]+"\"\n"
        Print File Arquivo "A568,360,0,2,1,1,N,\""+OBS2[3]+"\"\n"
EndIf

If CITEM[1]=CITEM[2] And CITEM[1]=CITEM[3]
   Then Print File Arquivo "P"+StrVal(CR)+"\n"
        ce=ce+((CR-1)*3)
   Else Print File Arquivo "P1\n"
EndIf
ne=3
EndProc

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc SD_Dialog_Tabela(Disparo,Campo,Evento,Elemento)
Temp=True
MSG_CAMPO=""
TECLA=""
If Disparo="OPEN"  Then AoCriarShowDialog() EndIf
If Disparo="EVENT" And Evento["Type"]="KEY" Then
   Switch
     Case Evento["KeyCode"] = 9: Temp=False
     Case Evento["KeyCode"] = -59: Dialog_F1()
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case Campo="YES" : Campo="TG_TABELA3"
          EndSwitch
     Case (Evento["KeyCode"] <= -104 And Evento["KeyCode"] >= -113) Or 
           Evento["KeyCode"]  = -139 Or  Evento["KeyCode"]  = -140:
           SYS_TECLA_ACIONADA=Evento["KeyCode"]
           Seta_Tecla_Acionada()
           CancelDialog
     Case Evento["KeyCode"] = 13:    
          Temp=False
          If Checa_TAG()
             Then Switch
                    Case Campo="TG_TABELA3" : 
                         If Not IsBlank(VSDGTABELA3)
                            Then Campo="YES"
                         EndIf   
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="":
     MSG_CAMPO=""
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp

EndProc

;*******************************************************************************
;* Monta Comandos p/ Impressora Termica                                        *
;*******************************************************************************
Proc Monta_Comandos()

If IsAssigned(DITEA)
   Then Release Vars DITEA, DITEB, CITEM, PRECO, PRECO2, NOMEMP, OBS1, OBS2, TPBARRA
EndIf
Array  DITEA [3]
Array  DITEAA[3]
Array  DITEB [3]
Array  CITEM [3]
Array TPBARRA[3]
Array  PRECO [3]
Array  PRECO2[3]
Array  NOMEMP[3]
Array    OBS1[3]
Array    OBS2[3]

Arquivo=PrivDir()+"CR.ejf"
Deleta_Arquivo(Arquivo)

GetRecord "\\Sistemas\\Cadastro\\Empresas" "0001" To REG_EMP
If RetVal
   Then EMPRE = Format("W18,AC",REG_EMP["Nome Fantasia Empresa"])
   Else EMPRE = ""
EndIf  

ne=3
Coedit "Answer"
Scan
  te=[Quantidade]
  If (te-(3-ne))>3
     Then CR=Int((te-(3-ne))/3)
     Else CR=1
  EndIf
  ce=0
  
  CodItem=[Codigo do Item]
  CodItemRaiz=PegaCodigoRaiz(CodItem)
  
  While True
    ce=ce+1
    ne=ne+1
    If ne=4
       Then For i From 1 To 3
              DITEA [i]=""
              DITEAA[i]=""
              DITEB [i]=""
              CITEM [i]=""
             TPBARRA[i]=""  
              PRECO [i]=""
              PRECO2[i]=""
              NOMEMP[i]=""
              OBS1  [i]=""
              OBS2  [i]=""
            EndFor
            ne=1
    EndIf
    
    If Not IsBlank(VSDGTABELA3)
       Then GetRecord "\\Sistemas\\Vendas\\Tbprite" VSDGTABELA3,CodItemRaiz To TABELADEPRECO2
           If RetVal
               Then PRUNIT2 = StrVal(NumVal(TABELADEPRECO2["Preco Unitario"])) 
               Else PRUNIT2 = "00"
           EndIf
       Else PRUNIT2 = "00"    
    EndIf
    
    PosI = Search(".",PRUNIT2)
    If PosI > 0
       Then If Len(SubStr(PRUNIT2,PosI+1,2))=1
               Then PRUNIT2=PRUNIT2+"0"
            EndIf   
            PRUNIT2="00"+SubStr(PRUNIT2,1,PosI-1)+"00"+SubStr(PRUNIT2,PosI+1,Len(PRUNIT2))
       Else PRUNIT2="00"+PRUNIT2+"0000"
    EndIf
    
    If [Preco Unitario]="" Or [Preco Unitario]=BlankNum()
       Then PRUNIT = "00"
       Else PRUNIT = StrVal(NumVal([Preco Unitario]))
    EndIf                              
    
    PosI = Search(".",PRUNIT)
    If PosI > 0
       Then If Len(SubStr(PRUNIT,PosI+1,2))=1
               Then PRUNIT=PRUNIT+"0"
            EndIf   
            PRUNIT="00"+SubStr(PRUNIT,1,PosI-1)+"00"+SubStr(PRUNIT,PosI+1,Len(PRUNIT))
       Else PRUNIT="00"+PRUNIT+"0000"     
    EndIf   
    
    DITEA [ne]=SubStr([Descricao do Item],01,17)
    DITEAA[ne]=SubStr([Descricao do Item],18,30) 
    DITEB [ne]="Ref "+RTrim([Referencia])
   TPBARRA[ne]=Upper([Tipo Codigo_Barras])
    Switch
      Case Search("EAN13",TPBARRA[ne])>0 Or 
           Search("EAN8",TPBARRA[ne])>0   : CITEM[ne]=[Codigo de Barras]
      OtherWise                           : CITEM[ne]=[Codigo do Item]
    EndSwitch   
    PRECO [ne]=PRUNIT
    PRECO2[ne]=PRUNIT2
    If SYS_CODIGO_CLIENTE="0266"
       Then NOMEMP[ne]="CACHARREL"
       Else NOMEMP[ne]=EMPRE
    EndIf   
    OBS1  [ne]="NAO EFETUAMOS TROCA"
    OBS2  [ne]="DE PECAS BRANCAS"

    If ne=3   Then Monta_Etiqueta() EndIf
    If ce>=te Then QuitLoop         EndIf
  EndWhile
EndScan
If ne<3 Then Monta_Etiqueta() EndIf
Do_It!
ClearAll

ImpriArq(Arquivo)

Sleep 700
Deleta_Arquivo(Arquivo)
ClearAll
EndProc

Monta_Comandos()
Release Procs Monta_Etiqueta,Monta_Comandos
