Skip to content

Commit 79e176e

Browse files
authored
Merge pull request #43 from gersonfs/master
Atualizando tabela de prefixos GS1
2 parents 2f60364 + 4752cbb commit 79e176e

7 files changed

Lines changed: 168 additions & 40 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ Este pacote é aderente com os [PSR-1], [PSR-2] e [PSR-4]. Se você observar neg
144144

145145
Não deixe de se cadastrar no [grupo de discussão do NFePHP](http://groups.google.com/group/nfephp) para acompanhar o desenvolvimento e participar das discussões e tirar dúvidas!
146146

147+
## Atualizando a tabela de prefixos GS1
148+
149+
Faça o download da tabela do site https://www.nfe.fazenda.gov.br/ e exporte a planilha com os prefixos em formato UTF-8 CSV pelo Excel.
150+
147151
## Install
148152

149153
**Este pacote está listado no [Packgist](https://packagist.org/) foi desenvolvido para uso do [Composer](https://getcomposer.org/), portanto não será explicitada nenhuma alternativa de instalação.**

src/Gtin.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ final class Gtin
4242
* Length of GTIN
4343
*/
4444
protected int $lenght;
45+
4546
/**
46-
* Prefixes collection
47+
* Tabela de prefixos GS1 baixado do site nfe.fazenda.gov.br e convertido em CSV
4748
*/
48-
protected array $stdPrefixCollection;
49+
protected array $tabelaPrefixoGs1 = [];
4950
/**
5051
* Validation of prefix of GTIN
5152
*/
@@ -84,9 +85,11 @@ public function __construct(?string $gtin = null, ?Certificate $certificate = nu
8485
$this->validPrefix = true;
8586
return;
8687
}
87-
$this->stdPrefixCollection = json_decode(
88-
file_get_contents(__DIR__.'/prefixcollection.json')
89-
);
88+
89+
$linhas = file(__DIR__ . '/Tabela_Prefixo_GS1.csv', FILE_IGNORE_NEW_LINES);
90+
unset($linhas[0]); //Remove o header
91+
$this->tabelaPrefixoGs1 = $linhas;
92+
9093
if (empty($gtin)) {
9194
throw new \InvalidArgumentException("Um numero GTIN deve ser passado.");
9295
}
@@ -279,8 +282,9 @@ protected function getCheckDigit(): int
279282

280283
protected function getPrefixDefinition(string $prefix): ?PrefixDefinition
281284
{
282-
foreach ($this->stdPrefixCollection as $std) {
283-
$definition = new PrefixDefinition($std);
285+
foreach ($this->tabelaPrefixoGs1 as $registro) {
286+
[$nIni, $nFim, $restrito, $regiao] = explode(';', $registro);
287+
$definition = new PrefixDefinition($nIni, $nFim, $restrito, $regiao);
284288
if ($definition->hasPrefix($prefix)) {
285289
return $definition;
286290
}

src/PrefixDefinition.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,35 @@
44

55
class PrefixDefinition
66
{
7-
private \stdClass $std;
87

9-
public function __construct(\stdClass $std)
8+
private string $nIni;
9+
private string $nFim;
10+
private string $restricted;
11+
private string $region;
12+
public function __construct(string $nIni, string $nFim, string $restricted, string $region)
1013
{
11-
$this->std = $std;
14+
$this->nIni = $nIni;
15+
$this->nFim = $nFim;
16+
$this->restricted = $restricted;
17+
$this->region = $region;
1218
}
1319

1420
public function getRegion(): string
1521
{
16-
return (string)$this->std->region;
22+
return $this->region;
1723
}
1824

1925
public function hasPrefix(string $prefix): bool
2026
{
2127
$pf = (int) $prefix;
22-
$nI = (int) $this->std->nIni;
23-
$nF = (int) $this->std->nFim;
28+
$nI = (int) $this->nIni;
29+
$nF = (int) $this->nFim;
2430
return $pf >= $nI && $pf <= $nF;
2531
}
2632

2733
public function isRestricted(): bool
2834
{
29-
return $this->std->restricted == "1";
35+
return $this->restricted == '1';
3036
}
3137

3238
}

src/Tabela_Prefixo_GS1.csv

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
fxPrefixIni;fxPrefixFim;indEsp;fxPais_Nome
2+
000;019;0;GS1 US
3+
020;029;1;Números de circulação restrita dentro da região
4+
030;039;0;GS1 US
5+
040;049;1;GS1 Números de circulação restrita dentro da empresa
6+
050;059;1;GS1 US reserved for future use
7+
060;139;0;GS1 US
8+
200;299;1;GS1 Números de circulação restrita dentro da região
9+
300;379;0;GS1 France
10+
380;380;0;GS1 Bulgaria
11+
383;383;0;GS1 Slovenija
12+
385;385;0;GS1 Croatia
13+
387;387;0;GS1 BIH (Bosnia-Herzegovina)
14+
389;389;0;GS1 Montenegro
15+
400;440;0;GS1 Germany
16+
450;459;0;GS1 Japan
17+
490;499;0;GS1 Japan
18+
460;469;0;GS1 Russia
19+
470;470;0;GS1 Kyrgyzstan
20+
471;471;0;GS1 Taiwan
21+
474;474;0;GS1 Estonia
22+
475;475;0;GS1 Latvia
23+
476;476;0;GS1 Azerbaijan
24+
477;477;0;GS1 Lithuania
25+
478;478;0;GS1 Uzbekistan
26+
479;479;0;GS1 Sri Lanka
27+
480;480;0;GS1 Philippines
28+
481;481;0;GS1 Belarus
29+
482;482;0;GS1 Ukraine
30+
483;483;0;GS1 Turkmenistan
31+
484;484;0;GS1 Moldova
32+
485;485;0;GS1 Armenia
33+
486;486;0;GS1 Georgia
34+
487;487;0;GS1 Kazakstan
35+
488;488;0;GS1 Tajikistan
36+
489;489;0;GS1 Hong Kong
37+
500;509;0;GS1 UK
38+
520;521;0;GS1 Association Greece
39+
528;528;0;GS1 Lebanon
40+
529;529;0;GS1 Cyprus
41+
530;530;0;GS1 Albania
42+
531;531;0;GS1 Macedonia
43+
535;535;0;GS1 Malta
44+
539;539;0;GS1 Ireland
45+
540;549;0;GS1 Belgium & Luxembourg
46+
560;560;0;GS1 Portugal
47+
569;569;0;GS1 Iceland
48+
570;579;0;GS1 Denmark
49+
590;590;0;GS1 Poland
50+
594;594;0;GS1 Romania
51+
599;599;0;GS1 Hungary
52+
600;601;0;GS1 South Africa
53+
603;603;0;GS1 Ghana
54+
604;604;0;GS1 Senegal
55+
608;608;0;GS1 Bahrain
56+
609;609;0;GS1 Mauritius
57+
611;611;0;GS1 Morocco
58+
613;613;0;GS1 Algeria
59+
615;615;0;GS1 Nigeria
60+
616;616;0;GS1 Kenya
61+
618;618;0;GS1 Ivory Coast
62+
619;619;0;GS1 Tunisia
63+
620;620;0;GS1 Tanzania
64+
621;621;0;GS1 Syria
65+
622;622;0;GS1 Egypt
66+
623;623;0;GS1 Brunei
67+
624;624;0;GS1 Libya
68+
625;625;0;GS1 Jordan
69+
626;626;0;GS1 Iran
70+
627;627;0;GS1 Kuwait
71+
628;628;0;GS1 Saudi Arabia
72+
629;629;0;GS1 Emirates
73+
640;649;0;GS1 Finland
74+
690;699;0;GS1 China
75+
700;709;0;GS1 Norway
76+
729;729;0;GS1 Israel
77+
730;739;0;GS1 Sweden
78+
740;740;0;GS1 Guatemala
79+
741;741;0;GS1 El Salvador
80+
742;742;0;GS1 Honduras
81+
743;743;0;GS1 Nicaragua
82+
744;744;0;GS1 Costa Rica
83+
745;745;0;GS1 Panama
84+
746;746;0;GS1 Republica Dominicana
85+
750;750;0;GS1 Mexico
86+
754;755;0;GS1 Canada
87+
759;759;0;GS1 Venezuela
88+
760;769;0;GS1 Schweiz, Suisse, Svizzera
89+
770;771;0;GS1 Colombia
90+
773;773;0;GS1 Uruguay
91+
775;775;0;GS1 Peru
92+
777;777;0;GS1 Bolivia
93+
778;779;0;GS1 Argentina
94+
780;780;0;GS1 Chile
95+
784;784;0;GS1 Paraguay
96+
786;786;0;GS1 Ecuador
97+
789;790;0;GS1 Brasil
98+
800;839;0;GS1 Italy
99+
840;849;0;GS1 Spain
100+
850;850;0;GS1 Cuba
101+
858;858;0;GS1 Slovakia
102+
859;859;0;GS1 Czech
103+
860;860;0; GS1 Serbia
104+
865;865;0;GS1 Mongolia
105+
867;867;0;GS1 North Korea
106+
868;869;0;GS1 Turkey
107+
870;879;0;GS1 Netherlands
108+
880;880;0;GS1 South Korea
109+
884;884;0;GS1 Cambodia
110+
885;885;0;GS1 Thailand
111+
888;888;0;GS1 Singapore
112+
890;890;0;GS1 India
113+
893;893;0;GS1 Vietnam
114+
896;896;0;GS1 Pakistan
115+
899;899;0;GS1 Indonesia
116+
900;919;0;GS1 Austria
117+
930;939;0;GS1 Australia
118+
940;949;0;GS1 New Zealand
119+
950;950;1;GS1 Global Office
120+
951;951;1;Numera‡Æo para EPC Tag Data Standard
121+
955;955;0;GS1 Malaysia
122+
958;958;0;GS1 Macau
123+
960;969;1;Global Office (GTIN-8s)
124+
977;977;1;Serial publications (ISSN)
125+
978;979;1;Bookland (ISBN)
126+
980;980;1;Refund receipts
127+
981;984;1;GS1 Coupon identification for common currency areas
128+
990;999;1;GS1 Coupon identification

src/prefixcollection.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/GtinTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,15 @@ public function test_is_restricted(): void
244244

245245
public function test_is_restricted_invalid_prefix(): void
246246
{
247-
$this->expectErrorMessage("510 is not a valid prefix!");
247+
$this->expectExceptionMessage("510 is not a valid prefix!");
248248
$gtin = new Gtin('5109907267612');
249249
$gtin->isRestricted();
250250
}
251+
252+
public function test_invalid_prefix(): void
253+
{
254+
$this->expectExceptionMessage("O prefixo 606 do GTIN é INVALIDO [Not Found].");
255+
$gtin = new Gtin('6065299511600');
256+
$gtin->isValid();
257+
}
251258
}

tests/PrefixDefinitionTest.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,25 @@ class PrefixDefinitionTest extends TestCase
1010

1111
public function test_is_restricted(): void
1212
{
13-
$definition = new PrefixDefinition((object)[
14-
"nIni" => "000",
15-
"nFim" => "019",
16-
"restricted" => "0",
17-
"region" => "GS1 US"
18-
]);
13+
$definition = new PrefixDefinition("000", "019", "0", "GS1 US");
1914
$this->assertFalse($definition->isRestricted());
2015
}
2116

2217
public function test_is_not_restricted(): void
2318
{
24-
$definition = new PrefixDefinition((object)[
25-
"nIni" => "020",
26-
"nFim" => "029",
27-
"restricted" => "1",
28-
"region" => "Números de circulação restrita dentro da região"
29-
]);
19+
$definition = new PrefixDefinition("020", "029", "1", "Números de circulação restrita dentro da região");
3020
$this->assertTrue($definition->isRestricted());
3121
}
3222

3323
public function test_get_region(): void
3424
{
35-
$definition = new PrefixDefinition((object)[
36-
"nIni" => "020",
37-
"nFim" => "029",
38-
"restricted" => "1",
39-
"region" => "Números de circulação restrita dentro da região"
40-
]);
25+
$definition = new PrefixDefinition("020", "029", "1", "Números de circulação restrita dentro da região");
4126
$this->assertEquals("Números de circulação restrita dentro da região", $definition->getRegion());
4227
}
4328

4429
public function testHasPrefix(): void
4530
{
46-
$definition = new PrefixDefinition((object)[
47-
"nIni" => "020",
48-
"nFim" => "029",
49-
"restricted" => "1",
50-
"region" => "Números de circulação restrita dentro da região"
51-
]);
31+
$definition = new PrefixDefinition("020", "029", "1", "Números de circulação restrita dentro da região");
5232

5333
$this->assertFalse($definition->hasPrefix("999"));
5434
$this->assertFalse($definition->hasPrefix("030"));

0 commit comments

Comments
 (0)