;** Et_cr *********************************************************************
;* Monta Etiqueta p/ Impressora Termica                                        *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    :   /  /     por Anivel Augusto Rodrigues
;Ultima Alteracao: 04/11/2003 por Karen Lauar Nery
;Motivo Alteracao: Ajustes
;Ultima Alteracao: 29/03/2007 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes contador ce
;Ultima Alteracao: 15/08/2007 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Run
;Ultima Alteracao: 29/02/2008 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes DispositivoI
;Ultima Alteracao: 06/11/2018 por Bruno Carvalho
;Motivo Alteracao: Ajustes padrĈo p/ codigo de barras EAN8, EAN13 ou COD128

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

Moveto "Itens"
Moveto [Codigo do Item]
Locate [Answer->Codigo do Item]
[Codigo de Barras]=[Codigo do Item]
UnLockRecord

Moveto "Answer"
Print File Arquivo "O\n"
Print File Arquivo "N\n"
Print File Arquivo "q768\n"
Print File Arquivo "Q207,20+0\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 "A26,169,0,2,1,1,N,\""+NEMPR[1]+"\"\n"
        Switch
          Case Search("EAN13",TPBARRA[1])>0 : Print File Arquivo "B28,67,0,E30,2,2,67,B,\""+CITEM[1]+"\"\n"
          Case Search("EAN8",TPBARRA[1])>0  : Print File Arquivo "B28,67,0,E80,2,2,67,B,\""+CITEM[1]+"\"\n"
          OtherWise                         : Print File Arquivo "B28,67,0,1,2,2,67,B,\""+CITEM[1]+"\"\n" 
        EndSwitch
        Print File Arquivo "A01,7,0,2,1,1,N,\""+DITEA[1]+"\"\n"
        Print File Arquivo "A01,34,0,2,1,1,N,\""+DITEB[1]+"\"\n"
EndIf
If Not IsBlank(CITEM[2])
   Then Print File Arquivo "A290,169,0,2,1,1,N,\""+NEMPR[2]+"\"\n"
        Switch
          Case Search("EAN13",TPBARRA[2])>0 : Print File Arquivo "B292,67,0,E30,2,2,67,B,\""+CITEM[2]+"\"\n"
          Case Search("EAN8",TPBARRA[2])>0  : Print File Arquivo "B292,67,0,E80,2,2,67,B,\""+CITEM[2]+"\"\n"
          OtherWise                         : Print File Arquivo "B292,67,0,1,2,2,67,B,\""+CITEM[2]+"\"\n" 
        EndSwitch
        Print File Arquivo "A264,7,0,2,1,1,N,\""+DITEA[2]+"\"\n"
        Print File Arquivo "A264,34,0,2,1,1,N,\""+DITEB[2]+"\"\n"
EndIf
If Not IsBlank(CITEM[3])
   Then Print File Arquivo "A554,169,0,2,1,1,N,\""+NEMPR[3]+"\"\n"
        Switch
          Case Search("EAN13",TPBARRA[3])>0 : Print File Arquivo "B556,67,0,E30,2,2,67,B,\""+CITEM[3]+"\"\n"
          Case Search("EAN8",TPBARRA[3])>0  : Print File Arquivo "B556,67,0,E80,2,2,67,B,\""+CITEM[3]+"\"\n"
          OtherWise                         : Print File Arquivo "B556,67,0,1,2,2,67,B,\""+CITEM[3]+"\"\n" 
        EndSwitch
        Print File Arquivo "A528,7,0,2,1,1,N,\""+DITEA[3]+"\"\n"
        Print File Arquivo "A528,34,0,2,1,1,N,\""+DITEB[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, TPBARRA
EndIf
Array   DITEA[3]
Array   DITEB[3]
Array   CITEM[3]
Array TPBARRA[3] 
Array   NEMPR[3]

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

ne=3
Coedit "Itens","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]=""
            TPBARRA[i]=""
              NEMPR[i]=""
            EndFor
            ne=1
    EndIf
    DITEA[ne]=SubStr([Descricao do Item],01,19)
    DITEB[ne]=" "+SubStr([Descricao do Item],20,30)
  TPBARRA[ne]=Upper([Tipo Codigo_Barras])
    Switch
      Case Search("EAN13",TPBARRA[2])>0 Or
           Search("EAN8",TPBARRA[2])>0   : CITEM[ne]=[Codigo de Barras]
      OtherWise                          : CITEM[ne]=[Codigo do Item] 
    EndSwitch  
    If SYS_CODIGO_CLIENTE="0006"
       Then NEMPR[ne]="CR Ind. Com. Conf. Ltda"
       Else NEMPR[ne]=""
    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
