14 Mai 2019 23:42
Func ValidaGTIN( cCodGtin )
Local nPosi := 0
Local nDigito := 0
Local nSomatoria := 0
Local aAlgoritmo := {}
Local lRetorno := .F.
aAlgoritmo := { 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3 }
cCodGtin := AllTrim( str(cCodGtin,14) )
If ( Val( cCodGtin ) <= 0 ) .Or. !( StrZero( Len( cCodGtin ), 2 ) $ [08,12,13,14] )
Return lRetorno
Else
nPosi := ( ( 14 - Len( cCodGtin ) ) + 1 )
EndIf
For x := 1 To ( Len( cCodGtin ) - 1 )
nSomatoria += ( Val( SubStr( cCodGtin, x, 1 ) ) * aAlgoritmo[ nPosi ] )
nPosi++
Next
// nDigito := Val( Right( Hb_NToS( nSomatoria ), 1 ) )
nDigito := Val( Right( LTrim( Str(nSomatoria,1) ), 1 ) )
If nDigito != 0
nDigito := ( 10 - nDigito )
EndIf
lRetorno := ( LTrim( Str(nDigito,1) ) = Right( cCodGtin, 1 ) )
Return lRetorno
15 Mai 2019 09:12
Func ValidaGTIN( cCodGtin )
Local nPosi := 0
Local nDigito := 0
Local nSomatoria := 0
Local aAlgoritmo := {}
Local lRetorno := .F.
aAlgoritmo := { 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3 }
If ValType( cCodGtin ) = "N"
cCodGtin := AllTrim( Str( cCodGtin ) )
Else
cCodGtin := AllTrim( cCodGtin )
EndIf
If ( Val( cCodGtin ) <= 0 ) .Or. !( StrZero( Len( cCodGtin ), 2 ) $ [08,12,13,14] )
Return lRetorno
Else
nPosi := ( ( 14 - Len( cCodGtin ) ) + 1 )
EndIf
For x := 1 To ( Len( cCodGtin ) - 1 )
nSomatoria += ( Val( SubStr( cCodGtin, x, 1 ) ) * aAlgoritmo[ nPosi ] )
nPosi++
Next
nDigito := Val( Right( LTrim( Str( nSomatoria ) ), 1 ) )
If nDigito != 0
nDigito := ( 10 - nDigito )
EndIf
lRetorno := ( LTrim( Str(nDigito,1) ) = Right( cCodGtin, 1 ) )
Return lRetorno
15 Mai 2019 18:56
16 Mai 2019 08:41
16 Mai 2019 10:06
CLS
?
? ValidaGTIN( Val( "7898584070440" ) )
? ValidaGTIN( Val( "7898584070457" ) )
? ValidaGTIN( Val( "7898584070464" ) )
?
Func ValidaGTIN( cCodGtin )
Local nPosi := 0
Local nDigito := 0
Local nSomatoria := 0
Local aAlgoritmo := {}
Local lRetorno := .F.
aAlgoritmo := { 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3 }
If ValType( cCodGtin ) = "N"
cCodGtin := AllTrim( Str( cCodGtin ) )
Else
cCodGtin := AllTrim( cCodGtin )
EndIf
If ( Val( cCodGtin ) <= 0 ) .Or. !( StrZero( Len( cCodGtin ), 2 ) $ [08,12,13,14] )
Return lRetorno
Else
nPosi := ( ( 14 - Len( cCodGtin ) ) + 1 )
EndIf
For x := 1 To ( Len( cCodGtin ) - 1 )
nSomatoria += ( Val( SubStr( cCodGtin, x, 1 ) ) * aAlgoritmo[ nPosi ] )
nPosi++
Next
nDigito := Val( Right( LTrim( Str( nSomatoria ) ), 1 ) )
If nDigito != 0
nDigito := ( 10 - nDigito )
EndIf
lRetorno := ( LTrim( Str(nDigito,1) ) = Right( cCodGtin, 1 ) )
Return lRetorno
17 Mai 2019 22:40