;** Et_Z0369 *******************************************************************
;* Monta Etiqueta p/ Imp. T‚rmica - Zebra - Interno                * Modelo 91 *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 03/06/2015 por Bruno Carvalho
;Ultima Alteracao: 18/06/2015 por Bruno Carvalho
;Motivo Alteracao: Os comandos iniciais p/ a impressÆo estavÆo sendo executados apenas 1 vez (INI=0)                          
;Ultima Alteracao: 07/11/2018 por Bruno Carvalho
;Motivo Alteracao: Ajustes padrÆo p/ codigo de barras EAN8, EAN13 ou COD128 

;*******************************************************************************
Proc Monta_Etiqueta()
Moveto "Answer"

Print File Arquivo "O\n"
Print File Arquivo "N\n"
Print File Arquivo "q880\n"
Print File Arquivo "Q400+\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,10,0,3,1,2,N,\""+REF[1]+"\"\n"
        Print File Arquivo "A10,60,0,2,1,1,N,\""+DITEA[1]+"\"\n"
        Print File Arquivo "A10,80,0,2,1,1,N,\""+DITEB[1]+"\"\n"   
        Print File Arquivo "A10,105,0,2,1,2,N,\""+TAMANHO[1]+"\"\n"
        Print File Arquivo "A10,150,0,2,1,2,N,\""+COR[1]+"\"\n"
        Print File Arquivo "A10,195,0,2,1,2,N,\""+MARCA[1]+"\"\n"
        Print File Arquivo "A10,240,0,2,1,2,N,\""+PRECO[1]+"\"\n" 
        Switch
          Case Search("EAN13",TPBARRA[1])>0 : Print File Arquivo "B48,280,0,E30,2,2,45,B,\""+CITEM[1]+"\"\n" 
          Case Search("EAN8",TPBARRA[1])>0  : Print File Arquivo "B48,280,0,E80,2,2,45,B,\""+CITEM[1]+"\"\n"
          OtherWise                         : Print File Arquivo "B48,280,0,1,2,2,45,B,\""+CITEM[1]+"\"\n"
        EndSwitch  
EndIf

If Not IsBlank(CITEM[2])
   Then Print File Arquivo "A295,10,0,3,1,2,N,\""+REF[2]+"\"\n"
        Print File Arquivo "A295,60,0,2,1,1,N,\""+DITEA[2]+"\"\n"
        Print File Arquivo "A295,80,0,2,1,1,N,\""+DITEB[2]+"\"\n"   
        Print File Arquivo "A295,105,0,2,1,2,N,\""+TAMANHO[2]+"\"\n"
        Print File Arquivo "A295,150,0,2,1,2,N,\""+COR[2]+"\"\n"
        Print File Arquivo "A295,195,0,2,1,2,N,\""+MARCA[2]+"\"\n"
        Print File Arquivo "A295,240,0,2,1,2,N,\""+PRECO[2]+"\"\n"  
        Switch
          Case Search("EAN13",TPBARRA[2])>0 : Print File Arquivo "B332,280,0,E30,2,2,45,B,\""+CITEM[2]+"\"\n"
          Case Search("EAN8",TPBARRA[2])>0  : Print File Arquivo "B332,280,0,E80,2,2,45,B,\""+CITEM[2]+"\"\n" 
          OtherWise                         : Print File Arquivo "B332,280,0,1,2,2,45,B,\""+CITEM[2]+"\"\n"
        EndSwitch  
EndIf

If Not IsBlank(CITEM[3])
   Then Print File Arquivo "A575,10,0,3,1,2,N,\""+REF[3]+"\"\n"
        Print File Arquivo "A575,60,0,2,1,1,N,\""+DITEA[3]+"\"\n"
        Print File Arquivo "A575,80,0,2,1,1,N,\""+DITEB[3]+"\"\n"   
        Print File Arquivo "A575,105,0,2,1,2,N,\""+TAMANHO[3]+"\"\n"
        Print File Arquivo "A575,150,0,2,1,2,N,\""+COR[3]+"\"\n"
        Print File Arquivo "A575,195,0,2,1,2,N,\""+MARCA[3]+"\"\n"
        Print File Arquivo "A575,240,0,2,1,2,N,\""+PRECO[3]+"\"\n" 
        Switch
          Case Search("EAN13",TPBARRA[3])>0 : Print File Arquivo "B610,280,0,E30,2,2,45,B,\""+CITEM[3]+"\"\n" 
          Case Search("EAN8",TPBARRA[3])>0  : Print File Arquivo "B610,280,0,E80,2,2,45,B,\""+CITEM[3]+"\"\n" 
          OtherWise                         : Print File Arquivo "B610,280,0,1,2,2,45,B,\""+CITEM[3]+"\"\n"
        EndSwitch  
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()
SYS_VALOR()
Release Vars INI,COD_ITEM,SYS_NOMEMP_DEFAULT,SYS_EMPRESA_DEFAULT,TAMANHO,
             COR,MARCA,AUX_MARCA,AUX_TAMANHO,AUX_COR,DITEA, REF, CITEM, PRECO, NOMEMP,TPBARRA  

COD_ITEM            = ""
SYS_EMPRESA_DEFAULT = ""
SYS_NOMEMP_DEFAULT  = ""
If Not IsEmpty("\\Sistemas\\Cadastro\\Empresas")
   Then View NoWindow "\\Sistemas\\Cadastro\\Empresas"
        Scan For Not IsBlank([Padrao do Sistema])
             SYS_EMPRESA_DEFAULT=[Codigo da Empresa    ]
             SYS_NOMEMP_DEFAULT =[Nome Fantasia Empresa]
        EndScan
        ClearImage
EndIf

INI = 0

Array DITEA  [3]
Array DITEB  [3]
Array REF    [3]
Array CITEM  [3]
Array TPBARRA[3]
Array PRECO  [3]
Array NOMEMP [3]
Array TAMANHO[3]
Array COR    [3]
Array MARCA  [3]

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

ne=3
Coedit "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]  = ""
              REF   [i]  = ""
              CITEM [i]  = ""
            TPBARRA [i]  = ""  
              PRECO [i]  = ""
              NOMEMP[i]  = "" 
              TAMANHO[i] = ""
              COR[i]     = ""
              MARCA[i]   = ""
            EndFor
            ne=1
    EndIf
    
    PRUNIT="00.00"
    GetRecord "\\Sistemas\\Vendas\\Tbprite" VSDGTABELA,PegaCodigoRaiz([Codigo do Item]) To REG_PRECO 
    If RetVal
       Then PRUNIT = REG_PRECO["Preco Unitario"]
    EndIf
    
    If PRUNIT=BlankNum()
       Then PRUNIT="00.00"
       Else PRUNIT=StrVal(NumVal(PRUNIT))
            PosI = Search(".",PRUNIT)
            If PosI > 0
               Then If Len(SubStr(PRUNIT,PosI+1,2))=1
                       Then PRUNIT=PRUNIT+"0"
                    EndIf   
                    PRUNIT="0M"+SubStr(PRUNIT,1,PosI-1)+"G"+SubStr(PRUNIT,PosI+1,Len(PRUNIT))+"C"
            EndIf
    EndIf                              
    
    If COD_ITEM<>[Codigo do Item]
       Then AUX_TAMANHO = ""
            AUX_COR     = ""
            AUX_MARCA   = ""
            COD_ITEM = [Codigo do Item]
            GetRecord "\\Sistemas\\Estoque\\Itens" COD_ITEM To REG_ITEM
            If RetVal
               Then AUX_TAMANHO = "Tam.: "+REG_ITEM["DTamanho"]
                    AUX_MARCA   = SubStr(REG_ITEM["Descricao da Marca"],1,18)
                    AUX_COR     = "Cor.: "+SubStr(REG_ITEM["Codigo da Cor_Tecido"],Len(REG_ITEM["Codigo da Cor_Tecido"])-1,2)
            EndIf
    EndIf           
    
    DITEA  [ne]=Ltrim(SubStr([Descricao do Item],01,20))
    DITEB  [ne]=Ltrim(SubStr([Descricao do Item],21,20)) 
    REF    [ne]="Ref.: "+RTrim([Referencia])
   TPBARRA [ne]=Upper([Tipo Codigo_Barras])
   If Search("EAN13",TPBARRA[ne])>0 Or Search("EAN8",TPBARRA[2])>0
      Then CITEM  [ne]=[Codigo de Barras]
      Else CITEM  [ne]=[Codigo do Item] 
   EndIf
    PRECO  [ne]=PRUNIT
    NOMEMP [ne]=SYS_NOMEMP_DEFAULT
    TAMANHO[ne]=AUX_TAMANHO
    COR    [ne]=AUX_COR 
    MARCA  [ne]=AUX_MARCA 
              
    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
