1
Dear colleagues, currently I am working on a project that carries out the sending of tax coupons, in it I arrived at a function that generates the key of the nfe, in it I receive the following data:
- City code
- Year and Month of issue
- Cnpj of the Company
- Model of the note (65 in the case)
- Series
- Note number (9 positions)
- Type of issue
- Note number with 8 positions
Function code:
public function GeraChaveNFe($CodCidade , $AnoMesEmissao, $CnpjEmpresa , $Modelo , $Serie, $NumeroNF, $TipoEmissao){
$NF8 = "";
$NF9 = "";
$Chave = "";
$Digito = "";
$tam = strlen($NumeroNF);
if($tam > 0){
$NF9 = str_pad($NumeroNF, 9 - $tam, "0", STR_PAD_LEFT);
$NF8 = str_pad($NumeroNF, 8 - $tam, "0", STR_PAD_LEFT);
}else{
$NF9 = $this->right($NumeroNF, 9);
$NF8 = $this->right($NumeroNF, 8);
}
$Chave = $CodCidade . $AnoMesEmissao . $CnpjEmpresa . $Modelo . $Serie . $NF9 . $TipoEmissao . $NF8;
$Digito = $this->DigitoMod11($Chave);
return $Chave . $Digito;
}
My doubt is in the following function:
function right($str, $length) {
return substr($str, -$length);
}
Function Right does the same as String.Right
VB and C#, when searching I didn’t find the same function in php... so I used substr.
The question is, is the substr really the equivalent of the right or is there a function that can replace this method?
Thanks Jorge, this code fragment is the result of a migration of Vb technology to php, so as the deadline is short we ended up doing some "gambiarras" for the system to work, congratulations for the explanation
– Alvaro Alves
@Alvaroalves how good it was to you. You can use the function
substr
because it will satisfy what you did in theVB
. I only recommend reading the documentation for more details.– Jorge.M