;** Et_Z397B *******************************************************************
;* Monta Etiqueta p/ Impressora Termica - Eltron                  * Modelo 133 *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 16/10/2019 por Bruno Carvalho
;Ultima Alteracao:   /  /     por                  
;Motivo Alteracao:  

;*******************************************************************************
;*Monta Etiqueta                                                               *
;*******************************************************************************
Proc Monta_Etiqueta()

Moveto "Answer"
Print File Arquivo "O\n"
Print File Arquivo "N\n"
Print File Arquivo "q640\n"
Print File Arquivo "Q480+\n"
Print File Arquivo "S8\n"
Print File Arquivo "D12\n"
Print File Arquivo "ZB\n"

If Not IsBlank(CITEM[1])
   Then TM = (Len(PRECO[1])*35)+10
        
        Imprime_Imagem_Termica("10","40","LO397G")
                
        Print File Arquivo "A10,100,0,3,1,1,N,\""+DITEA[1]+"\"\n"    
        Print File Arquivo "A10,125,0,3,1,1,N,\""+DITEB[1]+"\"\n"
        Print File Arquivo "A10,150,0,2,1,1,N,\""+REF[1]+"\"\n"
        Print File Arquivo "A160,125,0,4,1,2,N,\""+" TAM: "+"\"\n"
        Print File Arquivo "A250,125,0,4,2,2,N,\""+TAM[1]+"\"\n"
        Print File Arquivo "A10,175,0,1,1,1,N,\""+"TROCA APENAS COM ESSA ETIQUETA"+"\"\n"
        Switch          
          Case Search("EAN13",TPBARRA[1])>0 : Print File Arquivo "B035,200,0,E30,2,2,30,B,\""+CDBAR[1]+"\"\n" 
          Case Search("EAN8",TPBARRA[1])>0  : Print File Arquivo "B035,200,0,E80,2,2,30,B,\""+CDBAR[1]+"\"\n" 
          OtherWise                         : Print File Arquivo "B020,200,0,1,3,2,35,B,\""+CDBAR[1]+"\"\n"
        EndSwitch                
        Print File Arquivo "A10,268,0,3,1,2,N,\""+"R$"+"\"\n"
        Print File Arquivo "A50,268,0,3,2,2,N,\""+PRECO[1]+"\"\n"
        Print File Arquivo "A"+StrVal(10+TM)+",285,0,2,1,1,N,\""+"a prazo"+"\"\n"
        
        Print File Arquivo "A10,315,0,2,1,1,N,\""+"-5% OFF A VISTA"+"\"\n"                
        
       ;Print File Arquivo "A210,285,0,2,1,1,N,\""+"-5% OFF"+"\"\n"
       ;Print File Arquivo "A210,310,0,2,1,1,N,\""+"A VISTA"+"\"\n"
                
        Switch
          Case Search("EAN13",TPBARRA[1])>0 : Print File Arquivo "B025,360,0,E30,2,2,40,B,\""+CDBAR[1]+"\"\n"  
          Case Search("EAN8",TPBARRA[1])>0  : Print File Arquivo "B025,360,0,E80,2,2,40,B,\""+CDBAR[1]+"\"\n" 
          OtherWise                         : Print File Arquivo "B030,360,0,1,3,2,40,B,\""+CDBAR[1]+"\"\n"
        EndSwitch
        Print File Arquivo "A210,410,0,2,1,1,N,\""+Format("W7,AR",PRECO[1])+"\"\n"  
EndIf

If Not IsBlank(CITEM[2])
   Then TM = (Len(PRECO[2])*35)+10
   
        Imprime_Imagem_Termica("330","40","LO397G")
    
        Print File Arquivo "A330,100,0,3,1,1,N,\""+DITEA[2]+"\"\n"    
        Print File Arquivo "A330,125,0,3,1,1,N,\""+DITEB[2]+"\"\n"
        Print File Arquivo "A330,150,0,2,1,1,N,\""+REF[2]+"\"\n"
        Print File Arquivo "A485,125,0,4,1,2,N,\""+" TAM: "+"\"\n"
        Print File Arquivo "A575,125,0,4,2,2,N,\""+TAM[2]+"\"\n"
        Print File Arquivo "A330,175,0,1,1,1,N,\""+"TROCA APENAS COM ESSA ETIQUETA"+"\"\n"
        Switch
          Case Search("EAN13",TPBARRA[2])>0 : Print File Arquivo "B350,200,0,E30,2,1,50,B,\""+CDBAR[2]+"\"\n" 
          Case Search("EAN8",TPBARRA[2])>0  : Print File Arquivo "B350,200,0,E80,2,1,50,B,\""+CDBAR[2]+"\"\n" 
          OtherWise                         : Print File Arquivo "B345,200,0,1,3,1,35,B,\""+CDBAR[2]+"\"\n"
        EndSwitch
        
        Print File Arquivo "A330,268,0,3,1,2,N,\""+"R$"+"\"\n"
        Print File Arquivo "A370,268,0,3,2,2,N,\""+PRECO[2]+"\"\n"
        Print File Arquivo "A"+StrVal(330+TM)+",285,0,2,1,1,N,\""+"a prazo"+"\"\n"
        
        Print File Arquivo "A330,315,0,2,1,1,N,\""+"-5% OFF A VISTA"+"\"\n"
        
       ;Print File Arquivo "A535,285,0,2,1,1,N,\""+"-5% OFF"+"\"\n"
       ;Print File Arquivo "A535,310,0,2,1,1,N,\""+"A VISTA"+"\"\n"
        
        Switch
          Case Search("EAN13",TPBARRA[2])>0 : Print File Arquivo "B350,360,0,E30,2,1,50,B,\""+CDBAR[2]+"\"\n"  
          Case Search("EAN8",TPBARRA[2])>0  : Print File Arquivo "B350,360,0,E80,2,1,50,B,\""+CDBAR[2]+"\"\n" 
          OtherWise                         : Print File Arquivo "B345,360,0,1,3,1,40,B,\""+CDBAR[2]+"\"\n"
        EndSwitch
        Print File Arquivo "A530,410,0,2,1,1,N,\""+Format("W7,AR",PRECO[2])+"\"\n"
EndIf
 
If CITEM[1]=CITEM[2]
   Then Print File Arquivo "P"+StrVal(CR)+"\n"
        ce=ce+((CR-1)*3)
   Else Print File Arquivo "P1\n"
EndIf
ne=2
EndProc

;*******************************************************************************
;* Monta Comandos p/ Impressora Termica                                        *
;*******************************************************************************
Proc Monta_Comandos()
 
If IsAssigned(CITEM)
   Then Release Vars CITEM,DITEA,DITEB,CDBAR,TPBARRA,REF,PRECO,TAM
EndIf

Download_Imagem_Termica("ZEBRA","LO_397G")

Array   CITEM[2]   
Array   DITEA[2]
Array   DITEB[2]
Array   CDBAR[2]
Array TPBARRA[2]
Array     REF[2]
Array     TAM[2]
Array   PRECO[2]

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

ne=2
Coedit "Answer"
Scan
  REFERENCIA   = ""
  TAMANHO      = ""
  GetRecord "\\Sistemas\\Estoque\\Itens" [Codigo do Item] To REG_ITEM 
  If RetVal
     Then TAMANHO      = StrVal(REG_ITEM["DTamanho"])
          REFERENCIA   = REG_ITEM["Referencia"]
  EndIf
  
  If TAMANHO<>""
     Then For ii From 1 To Len(TAMANHO)
            If SubStr(TAMANHO,ii,1)="0"
               Then If ii>1
                       Then TAMANHO = " "+SubStr(TAMANHO,ii+1,Len(TAMANHO))
                       Else TAMANHO = SubStr(TAMANHO,ii-1,Len(TAMANHO))+" "+SubStr(TAMANHO,ii+1,Len(TAMANHO))
                    EndIf   
               Else QuitLoop
            EndIF    
          EndFor
          TAMANHO = Rtrim(Ltrim(TAMANHO)) 
  EndIf   

  te = [Quantidade]  
  If (te-(2-ne))>2
     Then CR=Int((te-(2-ne))/2)
     Else CR=1
  EndIf
  ce=0
  
  While True
    ce=ce+1
    ne=ne+1
    If ne=3
       Then For i From 1 To 2
                 CITEM[i]=""
                 DITEA[i]=""
                 DITEB[i]=""
                 CDBAR[i]=""
               TPBARRA[i]=""
                   REF[i]=""
                   TAM[i]=""
                 PRECO[i]=""
            EndFor
            ne=1
    EndIf 

   PRECO[ne]=""
   GetRecord "\\Sistemas\\Vendas\\Tbprite" VSDGTABELA,PegaCodigoRaiz([Codigo do Item]) To REG_PRECO
   If RetVal
      Then PRECO[ne]=StrVal(REG_PRECO["Preco Unitario"])
      Else GetRecord "\\Sistemas\\Vendas\\Tbprite" VSDGTABELA,[Codigo do Item]  To REG_PRECO 
           If RetVal 
              Then PRECO[ne]=StrVal(REG_PRECO["Preco Unitario"])
           EndIf      
   EndIf     
    
   CITEM[ne] = [Codigo do Item]
   DITEA[ne] = Format("W20",Ltrim(SubStr([Descricao do Item],01,20)))
   DITEB[ne] = Format("W20",Ltrim(SubStr([Descricao do Item],21,20)))
   CDBAR[ne] = [Codigo de Barras]
     REF[ne] = Format("W13","REF: "+REFERENCIA)
     TAM[ne] = TAMANHO 
 TPBARRA[ne] = Upper([Tipo Codigo_Barras])
    
   If ne=2   Then Monta_Etiqueta() EndIf
   If ce>=te Then QuitLoop         EndIf
  EndWhile
EndScan
If ne<2 Then Monta_Etiqueta() EndIf
Do_It!
ClearAll

debug
ImpriArq(Arquivo)

Sleep 700
Deleta_Arquivo(Arquivo)
ClearAll
EndProc

Monta_Comandos()
Release Procs Monta_Etiqueta,Monta_Comandos