;** ET_Z408 ********************************************************************
;* Monta Etiqueta p/ Impressora Termica - CODE128                              *
;*******************************************************************************
;Autor: Visao/Vise
;Desenvolvido    : 07/03/2018 por Bruno Carvalho
;Ultima Alteracao: 08/03/2018 por Bruno Carvalho 
;Motivo Alteracao: Ajustes p/ imprimir inverso na etiqueta -> De "ZT" para "ZB"  
;Ultima Alteracao: 16/04/2018 por Bruno Carvalho 
;Motivo Alteracao: Ajustes no rendimento p/ sair na linha da largura e 2 linhas p/ Simbologia
;                  Adicionado array de controle da simbologia -> SIMBOL_ORIGINAL 

;ETIQUETA CANCELADA POIS SEGUNDO O CLIENTE A IMPRESSAO DE SIMBOLOGIA NAO PODE SAIR EM DUAS LINHAS

;*******************************************************************************
;* Ajusta Simbologia p/ ser impresso em 2 linhas
;*******************************************************************************
Proc Ajusta_Simbologia_2Linhas(NUMERO_SIMBOL)
Private ii,ARRAY_AUX_SIMBOL,NUMERO_SIMBOL

If DynArraySize(SIMBOL)-NUMERO_SIMBOL <=0
   Then Return False
EndIf   

DynArray ARRAY_AUX_SIMBOL[]
For ii From 1 To DynArraySize(SIMBOL)-NUMERO_SIMBOL
  ARRAY_AUX_SIMBOL[ii] = SIMBOL[NUMERO_SIMBOL+ii] 
EndFor 

Release Vars SIMBOL 
DynArray SIMBOL[]
For ii From 1 To DynArraySize(ARRAY_AUX_SIMBOL)
  SIMBOL[¡i] = ARRAY_AUX_SIMBOL[ii] 
Endfor

Release Vars ARRAY_AUX_SIMBOL,ii

Return True

EndProc ;Ajusta_Simbologia_2Linhas()

;*******************************************************************************
;* Ajusta Simbologia Original                       
;*******************************************************************************
Proc Ajusta_Simbologia_Original() 
Private ii

Release Vars SIMBOL
DynArray SIMBOL[]
For ii From 1 To DynArraySize(SIMBOL_ORIGINAL)
  SIMBOL[ii]=SIMBOL_ORIGINAL[ii]
EndFor 

EndProc ;Ajusta_Simbologia_Original()

;*******************************************************************************
;* Rotina para Montar Etiqueta
;*******************************************************************************
Proc Monta_Etiqueta()
Moveto "Answer"

Print File Arquivo "O\n"
Print File Arquivo "N\n"
Print File Arquivo "q800\n"
Print File Arquivo "Q600\n"
Print File Arquivo "S2\n"
Print File Arquivo "D8\n"
Print File Arquivo "ZB\n"

If Not IsBlank(CITEM[1])
   Then Print File Arquivo "A020,030,0,3,1,2,N,\""+" REF: "+"\"\n"
        Print File Arquivo "A100,035,0,1,2,2,N,\""+DITEA1[1]+"\"\n"
        
        Print File Arquivo "A020,075,0,3,1,2,N,\""+"      "+"\"\n"
        Print File Arquivo "A100,075,0,1,2,2,N,\""+DITEA2[1]+"\"\n"
        
        Print File Arquivo "A020,130,0,3,1,2,N,\""+"LARG: "+"\"\n"
        Print File Arquivo "A100,135,0,1,2,2,N,\""+DITEB[1]+"\"\n"
        Print File Arquivo "A220,130,0,3,1,2,N,\""+"REND: "+"\"\n"
        Print File Arquivo "A300,135,0,1,2,2,N,\""+DITEC[1]+"\"\n"
        
        Print File Arquivo "A020,190,0,1,2,2,N,\""+DCOMP1[1]+"\"\n"
        Print File Arquivo "A020,230,0,1,2,2,N,\""+DCOMP2[1]+"\"\n"
        Print File Arquivo "A020,270,0,1,2,2,N,\""+DCOMP3[1]+"\"\n"
        
        Print File Arquivo "B50,310,0,1,3,2,75,B,\""+CITEM[1]+"\"\n"
        
        Imprime_Simbologia("ZEBRA","020","430","4")
        If Ajusta_Simbologia_2Linhas(4)
           Then Imprime_Simbologia("ZEBRA","020","495","4")
                Ajusta_Simbologia_Original()
        EndIf
EndIf

If Not IsBlank(CITEM[2])
   Then Print File Arquivo "A420,030,0,3,1,2,N,\""+" REF: "+"\"\n"
        Print File Arquivo "A500,035,0,1,2,2,N,\""+DITEA1[2]+"\"\n"
        
        Print File Arquivo "A420,075,0,3,1,2,N,\""+"      "+"\"\n"
        Print File Arquivo "A500,075,0,1,2,2,N,\""+DITEA2[2]+"\"\n"
        
        Print File Arquivo "A420,130,0,3,1,2,N,\""+"LARG: "+"\"\n"
        Print File Arquivo "A500,135,0,1,2,2,N,\""+DITEB[2]+"\"\n"
        Print File Arquivo "A620,130,0,3,1,2,N,\""+"REND: "+"\"\n"
        Print File Arquivo "A700,135,0,1,2,2,N,\""+DITEC[2]+"\"\n"
        
        Print File Arquivo "A420,190,0,1,2,2,N,\""+DCOMP1[2]+"\"\n"
        Print File Arquivo "A420,230,0,1,2,2,N,\""+DCOMP2[2]+"\"\n"
        Print File Arquivo "A420,270,0,1,2,2,N,\""+DCOMP3[2]+"\"\n"
        
        Print File Arquivo "B450,310,0,1,3,2,75,B,\""+CITEM[2]+"\"\n"        
        
        Imprime_Simbologia("ZEBRA","420","430","4")
        If Ajusta_Simbologia_2Linhas(4)
           Then Imprime_Simbologia("ZEBRA","420","495","4")
                Ajusta_Simbologia_Original()
        EndIf
EndIf

If CITEM[1]=CITEM[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(DITEA)
   Then Release Vars DITEA1, DITEA2, DITEB, DITEC, DCOMP1, DCOMP2, DCOMP3, CITEM
EndIf

NEMPR="Vlnus  Modas"

Array DITEA1[2]
Array DITEA2[2]
Array DITEB[2] 
Array DITEC[2]
Array DCOMP1[2]
Array DCOMP2[2]
Array DCOMP3[2]
Array CITEM[2]

Arquivo=PrivDir()+"Et_Z408.ext"
Deleta_Arquivo(Arquivo)

ne=2
Coedit "Answer"
Scan
  LARGURA    = ""
  RENDIMENTO = ""
  COMPOSICAO = ""
  SIMBOLOGIA = ""
  GetRecord "\\Sistemas\\Estoque\\Iten_Tec" [Codigo do Item] To REG_TECIDO
  If RetVal
     Then LARGURA    = REG_TECIDO["Largura"]                
          RENDIMENTO = REG_TECIDO["Rendimento"]
          COMPOSICAO = REG_TECIDO["Composicao"]
          SIMBOLOGIA = REG_TECIDO["Simbologia_Impressao"]
  EndIf
  
  DESCRICAO_COR = ""                          
  REFERENCIA    = ""
  GetRecord "\\Sistemas\\Estoque\\Itens" [Codigo do Item] To REG_ITEM
  If RetVal
     Then DESCRICAO_COR = REG_ITEM["Descricao da Cor_Tecido"]
          REFERENCIA    = REG_ITEM["Referencia"]                                                
  EndIf
  
  Release Vars CONT_SIMBOL,SIMBOL 
  CONT_SIMBOL=0
  DynArray SIMBOL[]
  If SIMBOLOGIA<>""
     Then PosI = Search("-",SIMBOLOGIA)
          If PosI>0
             Then While PosI>0
                    CONT_SIMBOL         = CONT_SIMBOL +1
                    SIMBOL[CONT_SIMBOL] = SubStr(SIMBOLOGIA,1,PosI-1)
                    SIMBOLOGIA = SubStr(SIMBOLOGIA,PosI+1,Len(SIMBOLOGIA))
                    PosI = Search("-",SIMBOLOGIA)
                    If PosI=0
                       Then CONT_SIMBOL         = CONT_SIMBOL +1
                            SIMBOL[CONT_SIMBOL] = SIMBOLOGIA
                            QuitLoop
                    EndIf   
                  EndWhile
             Else SIMBOL[1]=SIMBOLOGIA
          EndIf
     Else SIMBOL[1]=""
  EndIf  
  ;** Controle para o array original da Simbologia, pois ele eh alterado a cada
  ;   processo da impressÆo em 2 linhas da simbologia      
  DynArray SIMBOL_ORIGINAL[]
  For ii From 1 To DynArraySize(SIMBOL)
    SIMBOL_ORIGINAL[ii] = SIMBOL[ii]
  EndFor

  
  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
              DITEA1[i]=""
              DITEA2[i]=""
              DITEB[i]=""
              DITEC[i]=""
              DCOMP1[i]=""
              DCOMP2[i]=""
              DCOMP3[i]=""
              CITEM[i]=""
            EndFor
            ne=1
    EndIf
    
    DITEA1[ne]  = REFERENCIA
    If DESCRICAO_COR<>""
       Then DITEA2[ne] = DESCRICAO_COR
    EndIf
        
    DITEB[ne]  = Format("W6.2,AL",LARGURA)
    DITEC[ne]  = Format("W6.2,AL",RENDIMENTO)
    DCOMP1[ne] = SubStr(COMPOSICAO,01,18)
    DCOMP2[ne] = SubStr(COMPOSICAO,19,18)
    DCOMP3[ne] = SubStr(COMPOSICAO,37,18)
    CITEM[ne]  = [Codigo do Item]
    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
