;** Et_Z435 ********************************************************************
;* Monta Etiqueta p/ Impressora Termica - Zebra                                *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 22/05/2019 por Bruno Carvalho
;Ultima Alteracao: 21/06/2019 por Bruno Carvalho                   
;Motivo Alteracao: Ajustes na impressÆo

;*******************************************************************************
Proc Monta_Etiqueta()

Moveto "Answer"
Print File Arquivo "O\n"
Print File Arquivo "N\n"
Print File Arquivo "q808\n"
Print File Arquivo "Q167,17+\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,000,0,2,1,1,N,\""+DITEA[1]+"\"\n"
        Print File Arquivo "A10,022,0,2,1,1,N,\""+DITEB[1]+"\"\n"
        Print File Arquivo "A10,044,0,3,1,1,N,\""+REFER[1]+"\"\n"
        ;Print File Arquivo "A10,066,0,3,1,1,N,\""+COR_TM[1]+"\"\n"
        Switch
          Case Search("EAN13",TPBARRA[1])>0 : Print File Arquivo "B20,090,0,E30,2,2,50,N,\""+CDBAR[1]+"\"\n" 
          Case Search("EAN8",TPBARRA[1])>0  : Print File Arquivo "B20,090,0,E80,2,2,50,N,\""+CDBAR[1]+"\"\n"
          OtherWise                         : Print File Arquivo "B40,080,0,1,2,2,40,N,\""+CDBAR[1]+"\"\n" 
        EndSwitch  
        Print File Arquivo "A10,135,0,3,1,1,N,\""+PRECO[1]+"\"\n"
EndIf
If Not IsBlank(CITEM[2])
   Then
        Print File Arquivo "A290,000,0,2,1,1,N,\""+DITEA[2]+"\"\n"
        Print File Arquivo "A290,022,0,2,1,1,N,\""+DITEB[2]+"\"\n"
        Print File Arquivo "A290,044,0,3,1,1,N,\""+REFER[2]+"\"\n"
        ;Print File Arquivo "A290,066,0,3,1,1,N,\""+COR_TM[2]+"\"\n"
        Switch
          Case Search("EAN13",TPBARRA[2])>0 : Print File Arquivo "B300,090,0,E30,2,2,50,N,\""+CDBAR[2]+"\"\n" 
          Case Search("EAN8",TPBARRA[2])>0  : Print File Arquivo "B300,090,0,E80,2,2,50,N,\""+CDBAR[2]+"\"\n"
          OtherWise                         : Print File Arquivo "B320,080,0,1,2,2,40,N,\""+CDBAR[2]+"\"\n" 
        EndSwitch  
        Print File Arquivo "A290,135,0,3,1,1,N,\""+PRECO[2]+"\"\n"
EndIf
If Not IsBlank(CITEM[3])
   Then
        Print File Arquivo "A570,000,0,2,1,1,N,\""+DITEA[3]+"\"\n"
        Print File Arquivo "A570,022,0,2,1,1,N,\""+DITEB[3]+"\"\n"
        Print File Arquivo "A570,044,0,3,1,1,N,\""+REFER[3]+"\"\n"
        ;Print File Arquivo "A570,066,0,3,1,1,N,\""+COR_TM[3]+"\"\n"
        Switch
          Case Search("EAN13",TPBARRA[3])>0 : Print File Arquivo "B580,090,0,E30,2,2,50,N,\""+CDBAR[3]+"\"\n" 
          Case Search("EAN8",TPBARRA[3])>0  : Print File Arquivo "B580,090,0,E80,2,2,50,N,\""+CDBAR[3]+"\"\n"
          OtherWise                         : Print File Arquivo "B600,080,0,1,2,2,40,N,\""+CDBAR[3]+"\"\n" 
        EndSwitch  
        Print File Arquivo "A570,135,0,3,1,1,N,\""+PRECO[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

;*******************************************************************************
;* Monta Comandos p/ Impressora Termica                                        *
;*******************************************************************************
Proc Monta_Comandos()
If IsAssigned(DITEA)
   Then Release Vars DITEA, DITEB, CITEM, NEMPR, PRECO, CDBAR, TPBARRA, COR_TM
EndIf
Array   DITEA[3]
Array   DITEB[3]
Array   CDBAR[3]
Array   CITEM[3]
Array TPBARRA[3]
Array   REFER[3]
Array   PRECO[3]
Array  COR_TM[3]

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

ne=3                              
    
View "\\Sistemas\\Vendas\\Tbprite"
Coedit "Answer"
Scan
  te      = [Quantidade] 
  CodItem = [Codigo do Item]
    
  If (te-(3-ne))>3
     Then CR=Int((te-(3-ne))/3)
     Else CR=1
  EndIf  
  
  ;---- Cor Tecido/Tamanho -----------------------------------------------------
  CORTM=""
  GetRecord "\\Sistemas\\Estoque\\Itens" CodItem To REG_ITEM
  If RetVal
     Then CORTM = Rtrim(Format("W12",REG_ITEM["Descricao da Cor_Tecido"]))+" - "+Format("W2",REG_ITEM["DTamanho"])
  EndIf   
  
  ;---- Pre‡o A VISTA ----------------------------------------------------------
  If Not IsBlank(VSDGTABELA2)
     Then GetRecord "\\Sistemas\\Vendas\\Tbprite" VSDGTABELA2,PegaCodigoRaiz(CodItem) To TABELADEPRECO1
          If RetVal
              Then PRUNIT_VISTA = Tira_CharNum(StrVal(TABELADEPRECO1["Preco Unitario"])) 
              Else PRUNIT_VISTA = ""
          EndIf
     Else PRUNIT_VISTA = ""    
  EndIf
  
  ;---- Pre‡o A PRAZO ----------------------------------------------------------
  If Not IsBlank(VSDGTABELA3)
     Then GetRecord "\\Sistemas\\Vendas\\Tbprite" VSDGTABELA3,PegaCodigoRaiz(CodItem) To TABELADEPRECO2
          If RetVal
              Then PRUNIT_PRAZO = Tira_CharNum(StrVal(TABELADEPRECO2["Preco Unitario"])) 
              Else PRUNIT_PRAZO = ""
          EndIf
     Else PRUNIT_PRAZO = ""    
  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]=""
              CDBAR[i]=""
            TPBARRA[i]=""  
              CITEM[i]=""
              REFER[i]=""
              PRECO[i]=""
             COR_TM[i]=""
            EndFor
            ne=1
    EndIf
    DITEA[ne]=Rtrim(Ltrim(SubStr([Descricao do Item],01,20)))
    DITEB[ne]=Rtrim(Ltrim(SubStr([Descricao do Item],21,10)))
    CITEM[ne]=[Codigo do Item ]
  TPBARRA[ne]=Upper([Tipo Codigo_Barras])  
    CDBAR[ne]=[Codigo de Barras]
    REFER[ne]=[Referencia]
    If PRUNIT_VISTA<>""  
       Then PRECO[ne]="0"+PRUNIT_VISTA
       Else PRECO[ne] =""
    EndIf    
    If PRUNIT_PRAZO<>""
       Then PRECO[ne]=PRECO[ne] + "0"+PRUNIT_PRAZO
    EndIf    
    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
