;** ET_Z0428 *******************************************************************
;* Monta Etiqueta p/ Impressora Termica - Zebra                   * Modelo 120 *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 09/01/2018 por Bruno Carvalho
;Ultima Alteracao: 11/01/2018 por Bruno Carvalho                
;Motivo Alteracao: Ajsutes na impressÆo
;Ultima Alteracao: 10/07/2019 por Bruno Carvalho                
;Motivo Alteracao: Ajustes na impressÆo ao montar o "P"+StrVal(CR)

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

Print File Arquivo "I8,A,001\n"
Print File Arquivo "Q184,016\n"
Print File Arquivo "q720\n"
Print File Arquivo "rN\n"
Print File Arquivo "S3\n"
Print File Arquivo "D15\n"
Print File Arquivo "ZT\n"
Print File Arquivo "JF\n"
Print File Arquivo "O\n"
Print File Arquivo "R72,0\n"
Print File Arquivo "f100\n"
Print File Arquivo "N\n"

If Not IsBlank(COD_ITEM[1])
   Then Print File Arquivo "A30,02,0,2,1,1,N,\""+DITEA1[1]+"\"\n"
        Print File Arquivo "A30,22,0,2,1,1,N,\""+DITEA2[1]+"\"\n"
        Print File Arquivo "A30,46,0,2,1,1,N,\""+DITEB[1]+"\"\n"
        Print File Arquivo "A30,69,0,2,1,1,N,\""+PRECO_REF[1]+"\"\n"
        Switch
          Case Search("EAN13",TPBARRA[1])>0 : Print File Arquivo "B65,103,0,E30,2,30,50,B,\""+CDBAR[1]+"\"\n" 
          Case Search("EAN8",TPBARRA[1])>0  : Print File Arquivo "B80,93,0,E80,2,30,50,B,\""+CDBAR[1]+"\"\n"
          OtherWise                         : Print File Arquivo "B80,93,0,1,2,30,50,B,\""+CDBAR[1]+"\"\n"
        EndSwitch   
EndIf                                           

If Not IsBlank(COD_ITEM[2])
   Then Print File Arquivo "A368,02,0,2,1,1,N,\""+DITEA1[2]+"\"\n"
        Print File Arquivo "A368,22,0,2,1,1,N,\""+DITEA2[2]+"\"\n"
        Print File Arquivo "A368,46,0,2,1,1,N,\""+DITEB[2]+"\"\n"
        Print File Arquivo "A368,69,0,2,1,1,N,\""+PRECO_REF[2]+"\"\n"
        Switch
          Case Search("EAN13",TPBARRA[2])>0 : Print File Arquivo "B405,103,0,E30,2,30,50,B,\""+CDBAR[2]+"\"\n" 
          Case Search("EAN8",TPBARRA[2])>0  : Print File Arquivo "B430,93,0,E80,2,30,50,B,\""+CDBAR[2]+"\"\n"
          OtherWise                         : Print File Arquivo "B430,93,0,1,2,30,50,B,\""+CDBAR[2]+"\"\n"
        EndSwitch   
EndIf       

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

EndProc

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

If IsAssigned(COD_ITEM)
   Then Release Vars COD_ITEM, DITEA1, DITEA2, DITEB, CDBAR, TPBARRA, PRECO_REF
EndIf

Array  COD_ITEM[2]
Array    DITEA1[2]
Array    DITEA2[2]
Array     DITEB[2]
Array     CDBAR[2]
Array   TPBARRA[2]
Array PRECO_REF[2]

Arquivo=PrivDir()+"ET_Z428.zeb"
Deleta_Arquivo(Arquivo)
            
Cont = 0            
Item = ""
ne   = 2

Coedit "Answer"
Scan
  If Item<>[Codigo do Item]
     Then Item=[Codigo do Item] 
          GetRecord "\\Sistemas\\Estoque\\Itens" Item To REG_ITEM
          If RetVal
             Then DESC_COR   = REG_ITEM["Descricao da Cor_Tecido"]
                  REFERENCIA = REG_ITEM["Referencia"]
                  TAMANHO    = SubStr(REG_ITEM["Descricao do Item"],29,2)       ;REG_ITEM["DTamanho"]
             Else DESC_COR   = ""
                  REFERENCIA = ""
                  TAMANHO    = ""
          EndIf
  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
          COD_ITEM[i]=""
            DITEA1[i]=""
            DITEA2[i]="" 
             DITEB[i]=""
             CDBAR[i]=""
           TPBARRA[i]=""
         PRECO_REF[i]=""
           EndFor
           ne=1
   EndIf
                      
   ACHOU_PRECO = False
   CodItem=PegaCodigoRaiz([Codigo do Item]) 
   GetRecord "\\Sistemas\\Vendas\\Tbprite" VSDGTABELA,CodItem To REG_PRECO
   If RetVal
      Then ACHOU_PRECO = True 
      Else CodItem=[Codigo do Item] 
           GetRecord "\\Sistemas\\Vendas\\Tbprite" VSDGTABELA,CodItem To REG_PRECO 
           If RetVal Then ACHOU_PRECO = True EndIf   
   EndIf   
   
   If ACHOU_PRECO
      Then PRECO_REF[ne] = Spaces(3)+AlinhM(REFERENCIA,6)+Tira_CharNum(StrVal(REG_PRECO["Preco Unitario"]))+
                           AlinhM(StrVal(Month(Today())),2)+StrVal(Year(Today()))+"0" 
      Else PRECO_REF[ne] = Spaces(3)+AlinhM(REFERENCIA,6)+"0000"+AlinhM(StrVal(Month(Today())),2)+StrVal(Year(Today()))+"0"
   EndIf   
      
 COD_ITEM[ne]  = [Codigo do Item]
   DITEA1[ne]  = Rtrim(Ltrim(Format("W30",Rtrim(SubStr([Descricao do Item],01,22)))))
   DITEA2[ne]  = Rtrim(Ltrim(Format("W30",Rtrim(SubStr([Descricao do Item],23,22)))))
    DITEB[ne]  = "TAM: "+Format("W5",TAMANHO)+"REF.: "+Format("W6",REFERENCIA)
  TPBARRA[ne]  = Upper([Tipo Codigo_Barras])    
    
    CDBAR[ne]  = [Codigo de 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

ImpriArq(Arquivo)

Sleep 700
Deleta_Arquivo(Arquivo)
ClearAll
EndProc

Monta_Comandos()
Release Procs Monta_Etiqueta,Monta_Comandos
