Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1204 from pwsdotru/issue-inn-and-kpp
Browse files Browse the repository at this point in the history
Add INN and KPP support for ru_RU locale
  • Loading branch information
fzaninotto authored May 4, 2017
2 parents 309b4e9 + 9095fdf commit e6513a6
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,12 @@ echo $faker->premiumRatePhoneNumber; // "0900123456"

// Generates a Russian bank name (based on list of real russian banks)
echo $faker->bank; // "ОТП Банк"

//Generate a Russian Tax Payment Number for Company
echo $faker->inn; // 7813540735

//Generate a Russian Tax Code for Company
echo $faker->kpp; // 781301001
```

### `Faker\Provider\sv_SE\Payment`
Expand Down
34 changes: 34 additions & 0 deletions src/Faker/Calculator/Inn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace Faker\Calculator;

class Inn
{
/**
* Generates INN Checksum
*
* https://ru.wikipedia.org/wiki/%D0%98%D0%B4%D0%B5%D0%BD%D1%82%D0%B8%D1%84%D0%B8%D0%BA%D0%B0%D1%86%D0%B8%D0%BE%D0%BD%D0%BD%D1%8B%D0%B9_%D0%BD%D0%BE%D0%BC%D0%B5%D1%80_%D0%BD%D0%B0%D0%BB%D0%BE%D0%B3%D0%BE%D0%BF%D0%BB%D0%B0%D1%82%D0%B5%D0%BB%D1%8C%D1%89%D0%B8%D0%BA%D0%B0
*
* @param string $inn
* @return string Checksum (one digit)
*/
public static function checksum($inn)
{
$multipliers = array(1 => 2, 2 => 4, 3 => 10, 4 => 3, 5 => 5, 6 => 9, 7 => 4, 8 => 6, 9 => 8);
$sum = 0;
for ($i = 1; $i <= 9; $i++) {
$sum += intval(substr($inn, $i-1, 1)) * $multipliers[$i];
}
return strval(($sum % 11) % 10);
}

/**
* Checks whether an INN has a valid checksum
*
* @param string $inn
* @return boolean
*/
public static function isValid($inn)
{
return self::checksum(substr($inn, 0, -1)) === substr($inn, -1, 1);
}
}
21 changes: 21 additions & 0 deletions src/Faker/Provider/ru_RU/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,25 @@ public static function companyNameSuffix()
{
return static::randomElement(static::$companyNameSuffixes);
}

public static function inn($area_code = "")
{
if ($area_code === "" || intval($area_code) == 0) {
//Simple generation code for areas in Russian without check for valid
$area_code = static::numberBetween(1, 91);
} else {
$area_code = intval($area_code);
}
$area_code = str_pad($area_code, 2, '0', STR_PAD_LEFT);
$inn_base = $area_code . static::numerify('#######');
return $inn_base . \Faker\Calculator\Inn::checksum($inn_base);
}

public static function kpp($inn = "")
{
if ($inn == "" || strlen($inn) < 4) {
$inn = static::inn();
}
return substr($inn, 0, 4) . "01001";
}
}
50 changes: 50 additions & 0 deletions test/Faker/Calculator/InnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Faker\Test\Calculator;

use Faker\Calculator\Inn;

class InnTest extends \PHPUnit_Framework_TestCase
{

public function checksumProvider()
{
return array(
array('143525744', '4'),
array('500109285', '3'),
array('500109285', '3'),
array('500109285', '3'),
array('027615723', '1')
);
}

/**
* @dataProvider checksumProvider
*/
public function testChecksum($inn, $checksum)
{
$this->assertEquals($checksum, Inn::checksum($inn), $inn);
}

public function validatorProvider()
{
return array(
array('5902179757', true),
array('5408294405', true),
array('2724164617', true),
array('0726000515', true),
array('6312123552', true),

array('1111111111', false),
array('0123456789', false),
);
}

/**
* @dataProvider validatorProvider
*/
public function testIsValid($inn, $isValid)
{
$this->assertEquals($isValid, Inn::isValid($inn), $inn);
}
}
36 changes: 36 additions & 0 deletions test/Faker/Provider/ru_RU/CompanyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Faker\Test\Provider\ru_RU;

use Faker\Generator;
use Faker\Provider\ru_RU\Company;

class CompanyTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Generator
*/
private $faker;

public function setUp()
{
$faker = new Generator();
$faker->addProvider(new Company($faker));
$this->faker = $faker;
}

public function testINN()
{
$this->assertRegExp('/^[0-9]{10}$/', $this->faker->inn);
$this->assertEquals("77", substr($this->faker->inn("77"), 0, 2));
$this->assertEquals("02", substr($this->faker->inn(2), 0, 2));
}

public function testKPP()
{
$this->assertRegExp('/^[0-9]{9}$/', $this->faker->kpp);
$this->assertEquals("01001", substr($this->faker->kpp, -5, 5));
$inn = $this->faker->inn;
$this->assertEquals(substr($inn, 0, 4), substr($this->faker->kpp($inn), 0, 4));
}
}

0 comments on commit e6513a6

Please sign in to comment.