;** Bib_Estq ******************************************************************
;*  Biblioteca de Rotinas de Trabalho/Estoque                                  *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 29/04/2009 por Anivel Augusto Rodrigues
;Ultima Alteracao:   /  /     por
;Motivo Alteracao:

@ 22,28 ?? "Gerando Biblioteca Bib_Estq.Lib"
Style Blink
?? "... "

If IsFile("\\Sistemas\\Estoque\\Bib_Estq.Lib")
   Then Run NoRefresh "Del \\Sistemas\\Estoque\\Bib_Estq.Lib>Nul"
EndIf
CreateLib "\\Sistemas\\Estoque\\Bib_Estq"


;*******************************************************************************
;* Rotina calculo EAN13                                                        *
;*******************************************************************************
Proc Mod11_CalcEan13(Opcao,valor)
Private Opcao, Valor,
        dig1, dig2, dig3, dig4, dig5,
        dig6, dig7, dig8, dig9, dig10,
        dig11, dig12,
        soma_dig_par, soma_dig_impar, soma_dig,
        resultado, digito_ean

Valor=AlinhM(SubStr(StrVal(Valor),1,12),12)

If Valor="000000000000"
   Then Return ""
EndIf
If Not E_Numerico(Valor)
   Then Return ""
EndIf

dig1 =NumVal(SubStr(valor,01,1))
dig2 =NumVal(SubStr(valor,02,1))
dig3 =NumVal(SubStr(valor,03,1))
dig4 =NumVal(SubStr(valor,04,1))
dig5 =NumVal(SubStr(valor,05,1))
dig6 =NumVal(SubStr(valor,06,1))
dig7 =NumVal(SubStr(valor,07,1))
dig8 =NumVal(SubStr(valor,08,1))
dig9 =NumVal(SubStr(valor,09,1))
dig10=NumVal(SubStr(valor,10,1))
dig11=NumVal(SubStr(valor,11,1))
dig12=NumVal(SubStr(valor,12,1))

soma_dig_par   = 3 * (dig2 + dig4 + dig6 + dig8 + dig10 + dig12)
soma_dig_impar = dig1 + dig3 + dig5 + dig7 + dig9 + dig11
soma_dig       = soma_dig_par + soma_dig_impar
resultado = ((1 + Int((soma_dig) / 10)) * 10) - soma_dig
If resultado = 10
   Then digito_ean = 0
   Else digito_ean = resultado
EndIf

If Opcao="CALC"
   Then Return StrVal(Valor)+StrVal(digito_ean)
   Else Return digito_ean
EndIf
EndProc

;*******************************************************************************
;* Rotina calculo EAN8																												 *
;*******************************************************************************
Proc Mod11_CalcEan8(Opcao, Valor)
Private Opcao, Valor,
        dig1, dig2, dig3, dig4, dig5,
				dig6, dig7,
        soma_dig_par, soma_dig_impar, soma_dig,
        resultado, digito_ean

Valor=AlinhM(SubStr(StrVal(Valor),1,7),07)

If Valor="0000000"
   Then Return ""
EndIf
If Not E_Numerico(Valor)
   Then Return ""
EndIf

dig1 =NumVal(SubStr(valor,01,1))
dig2 =NumVal(SubStr(valor,02,1))
dig3 =NumVal(SubStr(valor,03,1))
dig4 =NumVal(SubStr(valor,04,1))
dig5 =NumVal(SubStr(valor,05,1))
dig6 =NumVal(SubStr(valor,06,1))
dig7 =NumVal(SubStr(valor,07,1))

soma_dig_par   = dig2 + dig4 + dig6
soma_dig_impar = 3 * (dig1 + dig3 + dig5 + dig7)
soma_dig       = soma_dig_par + soma_dig_impar
resultado = ((1 + Int((soma_dig) / 10)) * 10) - soma_dig
If resultado = 10
   Then digito_ean = 0
   Else digito_ean = resultado
EndIf

If Opcao="CALC"
   Then Return StrVal(Valor)+StrVal(digito_ean)
   Else Return digito_ean
EndIf
EndProc

;*******************************************************************************
;Calculo Mod10
;*******************************************************************************
Proc CalcMod10(Bloco)
Private peso, pesos, soma, resto, wc, wi

peso  ="121212"
wi    =6

pesos =""
soma  =0
resto =0

For wc From 1 to Len(bloco)
  If wi=0
     then wi=8
  EndIf   
  pesos = Substr(peso,wi,1)+pesos
  wi=wi-1
EndFor  

 
For wc From 1 to Len(bloco)
  soma=soma+NUMVAL(SUBSTR (pesos,wc,1))* NUMVAL(SUBSTR (bloco,wc,1))
EndFor
resto=Mod(soma,11)
if (resto=10) or (resto=0) or (resto=1) 
   then digito = 1                  
   Else digito = 11 - resto    
EndIF                           

Return digito
EndProc

;******************************************************************************
;** Monta Procedures                                                        ***
;******************************************************************************
WriteLib "\\Sistemas\\Estoque\\Bib_Estq"
     Mod11_CalcEan13, Mod11_CalcEan8, CalcMod10

