;** ET_Z439 ********************************************************************
;* Monta Etiqueta Impressora Termica - Zebra                                   *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 28/08/2019 por Bruno Carvalho
;Ultima Alteracao:   /  /     por                 
;Motivo Alteracao: 

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

Print File Arquivo "O\n"
Print File Arquivo "N\n"
Print File Arquivo "q880\n"
Print File Arquivo "Q184,24+\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,03,0,1,1,1,N,\""+DITEA[1]+"\"\n"
        Print File Arquivo "A00,20,0,1,1,1,N,\""+DITEB[1]+"\"\n"
        Switch
          Case Search("EAN13",TPBARRA[1])>0 : Print File Arquivo "B12,43,0,E30,2,2,60,B,\""+CITEM[1]+"\"\n"
          Case Search("EAN8",TPBARRA[1])>0  : Print File Arquivo "B12,43,0,E80,2,2,60,B,\""+CITEM[1]+"\"\n"
          OtherWise                         : Print File Arquivo "B12,43,0,1,2,2,60,B,\""+CITEM[1]+"\"\n"  
        EndSwitch             
        Print File Arquivo "A00,138,0,2,2,1,N,\""+"R$ "+PRUNI[1]+"\"\n"
EndIf
If Not IsBlank(CITEM[2])
   Then Print File Arquivo "A308,03,0,1,1,1,N,\""+DITEA[2]+"\"\n"
        Print File Arquivo "A308,20,0,1,1,1,N,\""+DITEB[2]+"\"\n"
        Switch
          Case Search("EAN13",TPBARRA[2])>0 : Print File Arquivo "B328,43,0,E30,2,2,60,B,\""+CITEM[2]+"\"\n"
          Case Search("EAN8",TPBARRA[2])>0  : Print File Arquivo "B328,43,0,E80,2,2,60,B,\""+CITEM[2]+"\"\n"
          OtherWise                         : Print File Arquivo "B328,43,0,1,2,2,60,B,\""+CITEM[2]+"\"\n" 
        EndSwitch     
        Print File Arquivo "A309,138,0,2,2,1,N,\""+"R$ "+PRUNI[2]+"\"\n"
EndIf
If Not IsBlank(CITEM[3])
   Then Print File Arquivo "A604,03,0,1,1,1,N,\""+DITEA[3]+"\"\n"
        Print File Arquivo "A604,20,0,1,1,1,N,\""+DITEB[3]+"\"\n"
        Switch
          Case Search("EAN13",TPBARRA[3])>0 : Print File Arquivo "B624,43,0,E30,2,2,60,B,\""+CITEM[3]+"\"\n"
          Case Search("EAN8",TPBARRA[3])>0  : Print File Arquivo "B624,43,0,E80,2,2,60,B,\""+CITEM[3]+"\"\n"
          OtherWise                         : Print File Arquivo "B624,43,0,1,2,2,60,B,\""+CITEM[3]+"\"\n"
        EndSwitch
        Print File Arquivo "A605,138,0,2,2,1,N,\""+"R$ "+PRUNI[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 Not IsAssigned(SYS_CODIGO_CLIENTE) Then SYS_CODIGO_CLIENTE="" EndIf

If IsAssigned(DITEA)
   Then Release Vars DITEA, DITEB, CITEM, TPBARRA
EndIf
Array   DITEA[3]
Array   DITEB[3]
Array   CITEM[3]
Array TPBARRA[3]
Array   PRUNI[3]

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

ne=3
Coedit "Answer"
Scan
  CodItem     = [Codigo do Item]
  CodItemRaiz = PegaCodigoRaiz(CodItem)
    
  If Not IsBlank(VSDGTABELA2)
     Then GetRecord "\\Sistemas\\Vendas\\Tbprite" VSDGTABELA2,CodItemRaiz To TABELADEPRECO
          If RetVal
              Then PRUNIT = StrVal(NumVal(TABELADEPRECO["Preco Unitario"])) 
              Else GetRecord "\\Sistemas\\Vendas\\Tbprite" VSDGTABELA2,CodItem To TABELADEPRECO 
                   If RetVal
                      Then PRUNIT = StrVal(NumVal(TABELADEPRECO["Preco Unitario"])) 
                      Else PRUNIT = ""
                   EndIf   
          EndIf
     Else PRUNIT = ""    
  EndIf
  
  PosI = Search(".",PRUNIT)
  If PosI > 0
     Then DECIMAL = Ltrim(Rtrim(Substr(PRUNIT,PosI+1,Len(PRUNIT))))
          If Len(DECIMAL) = 1
             Then DECIMAL = DECIMAL + "0"
          EndIf      
          PRUNIT = Substr(PRUNIT,1,PosI-1) +","+DECIMAL
  EndIf  
  
  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]=""
            TPBARRA[i]=""  
              PRUNI[i]=""
            EndFor
            ne=1
    EndIf
    DITEA[ne]=Ltrim(SubStr([Descricao do Item],01,22))
    DITEB[ne]=Ltrim(SubStr([Descricao do Item],23,22))
  TPBARRA[ne]=Upper([Tipo Codigo_Barras])
    If SYS_CODIGO_CLIENTE="0385"
       Then CITEM[ne]=[Codigo do Item]
       Else 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 
    EndIf
    PRUNI[ne]=PRUNIT

    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
