;** Et_Et_0391 *****************************************************************
;* Monta Etiqueta p/ Imp. T‚rmica - Zebra - Interno                * Modelo 98 *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 31/01/2017 por Rafael Matos
;Ultima Alteracao: 09/02/2017 por Bruno Carvalho  
;Motivo Alteracao: Ajustes  
;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"
Print File Arquivo "q768\n"     ;816
Print File Arquivo "Q168,16+\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 "A00,9,0,2,1,1,N,\""+DITEA[1]+"\"\n"
        Print File Arquivo "A00,27,0,2,1,1,N,\""+DITEB[1]+"\"\n"
        Print File Arquivo "A00,55,0,3,1,1,N,\""+CODCT[1]+"\"\n"
        Switch
          Case Search("EAN13",TPBARRA[1])>0 : Print File Arquivo "B12,85,0,E30,2,2,45,B,\""+CITEM[1]+"\"\n"
          Case Search("EAN8",TPBARRA[1])>0  : Print File Arquivo "B12,85,0,E80,2,2,45,B,\""+CITEM[1]+"\"\n" 
          OtherWise                         : Print File Arquivo "B12,85,0,1,2,2,45,B,\""+CITEM[1]+"\"\n"
        EndSwitch  
EndIf
If Not IsBlank(CITEM[2])
   Then Print File Arquivo "A270,9,0,2,1,1,N,\""+DITEA[2]+"\"\n"      ; A270
        Print File Arquivo "A270,27,0,2,1,1,N,\""+DITEB[2]+"\"\n"
        Print File Arquivo "A270,55,0,3,1,1,N,\""+CODCT[2]+"\"\n"     
        Switch
          Case Search("EAN13",TPBARRA[2])>0 : Print File Arquivo "B288,85,0,E30,2,2,45,B,\""+CITEM[2]+"\"\n"  ; B288 
          Case Search("EAN8",TPBARRA[2])>0  : Print File Arquivo "B288,85,0,E80,2,2,45,B,\""+CITEM[2]+"\"\n" 
          OtherWise                         : Print File Arquivo "B288,85,0,1,2,2,45,B,\""+CITEM[2]+"\"\n"   
        EndSwitch  
EndIf                      
If Not IsBlank(CITEM[3])
   Then Print File Arquivo "A546,9,0,2,1,1,N,\""+DITEA[3]+"\"\n"      ; A548
        Print File Arquivo "A546,27,0,2,1,1,N,\""+DITEB[3]+"\"\n"
        Print File Arquivo "A546,55,0,3,1,1,N,\""+CODCT[3]+"\"\n"
        Switch
          Case Search("EAN13",TPBARRA[3])>0 : Print File Arquivo "B564,85,0,E30,2,2,45,B,\""+CITEM[3]+"\"\n"  ; A564 
          Case Search("EAN8",TPBARRA[3])>0  : Print File Arquivo "B564,85,0,E80,2,2,45,B,\""+CITEM[3]+"\"\n"  
          OtherWise                         : Print File Arquivo "B564,85,0,1,2,2,45,B,\""+CITEM[3]+"\"\n"  
        EndSwitch  
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

;*******************************************************************************
;* Monta Comandos p/ Impressora Termica                                        *
;*******************************************************************************
Proc Monta_Comandos()
If IsAssigned(DITEA)
   Then Release Vars DITEA, DITEB, CITEM, CODCT, PRUNIT1, PRUNIT2, PRUNITARIO2, PRUNITARIO3,TPBARRA
EndIf
Array   DITEA[3]
Array   DITEB[3]
Array   CITEM[3]
Array TPBARRA[3]
Array   CODCT[3]       
                
PRUNITARIO2=""
PRUNITARIO3=""

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

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
  While True
    ce=ce+1
    ne=ne+1
    If ne=4
       Then For i From 1 To 3
              DITEA[i]=""
              DITEB[i]=""
              CITEM[i]=""
              CODCT[i]=""   
            TPBARRA[i]=""      
            EndFor
            ne=1
    EndIf
    
    CodItem     = [Codigo do Item]
    CodItemRaiz = PegaCodigoRaiz(CodItem)
    
    PRUNITARIO2=""
    PRUNITARIO3=""
  
    If Not IsBlank(VSDGTABELA2)
       Then GetRecord "\\Sistemas\\Vendas\\Tbprite" VSDGTABELA2,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 PRUNIT2 = Substr(PRUNIT2,1,PosI-1) 
    EndIf  
    
    If Not IsBlank(VSDGTABELA3)
       Then GetRecord "\\Sistemas\\Vendas\\Tbprite" VSDGTABELA3,CodItemRaiz To TABELADEPRECO3
            If RetVal
               Then PRUNIT3 = StrVal(NumVal(TABELADEPRECO3["Preco Unitario"]))
               Else PRUNIT3 = "00"
           EndIf
       Else PRUNIT3 = "00"    
    EndIf
      
    PosI = Search(".",PRUNIT3)
    If PosI > 0
       Then PRUNIT3 = Substr(PRUNIT3,1,PosI-1) 
    EndIf  
    
    For i from 1 to LEN(PRUNIT2)
        PRUNITARIO2 = Substr(PRUNIT2,i,1)+PRUNITARIO2
    EndFor
    
    For i from 1 to LEN(PRUNIT3)
        PRUNITARIO3 = Substr(PRUNIT3,i,1)+PRUNITARIO3
    EndFor        
    
    DITEA[ne] = SubStr([Descricao do Item],01,17)
    DITEB[ne] = SubStr([Descricao do Item],18,30)
  TPBARRA[ne] = Upper([Tipo Codigo_Barras])
    If Search("EAN13",TPBARRA[ne])>0 Or  Search("EAN8",TPBARRA[ne])>0
       Then CITEM[ne] = [Codigo de Barras]
       Else CITEM[ne] = [Codigo do Item  ] 
    EndIf
    CODCT[ne]=Rtrim(Substr([Referencia],1,8))+"0"+PRUNITARIO3+"0"+PRUNITARIO2
    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
