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 #1381 from ppelgrims/french-vat-formatter
Browse files Browse the repository at this point in the history
French vat formatter
  • Loading branch information
fzaninotto authored Dec 14, 2017
2 parents 07201f7 + bdb5994 commit e8fce01
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/Faker/Provider/fr_FR/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function catchPhrase()
*/
public function siret($formatted = true)
{
$siret = $this->siren(false);
$siret = self::siren(false);
$nicFormat = static::randomElement(static::$siretNicFormats);
$siret .= $this->numerify($nicFormat);
$siret .= Luhn::computeCheckDigit($siret);
Expand All @@ -129,9 +129,9 @@ public function siret($formatted = true)
* @see http://fr.wikipedia.org/wiki/Syst%C3%A8me_d%27identification_du_r%C3%A9pertoire_des_entreprises
* @return string
*/
public function siren($formatted = true)
public static function siren($formatted = true)
{
$siren = $this->numerify('%#######');
$siren = self::numerify('%#######');
$siren .= Luhn::computeCheckDigit($siren);
if ($formatted) {
$siren = substr($siren, 0, 3) . ' ' . substr($siren, 3, 3) . ' ' . substr($siren, 6, 3);
Expand Down
11 changes: 8 additions & 3 deletions src/Faker/Provider/fr_FR/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ class Payment extends \Faker\Provider\Payment
*/
public function vat($spacedNationalPrefix = true)
{
$prefix = ($spacedNationalPrefix) ? "FR " : "FR";

return sprintf("%s%s%s%s", $prefix, self::randomNumber(2, true), $this->siren($spacedNationalPrefix));
$siren = Company::siren(false);
$key = (12 + 3 * ($siren % 97)) % 97;
$pattern = "%s%'.02d%s";
if ($spacedNationalPrefix) {
$siren = trim(chunk_split($siren, 3, ' '));
$pattern = "%s %'.02d %s";
}
return sprintf($pattern, 'FR', $key, $siren);
}

/**
Expand Down
49 changes: 49 additions & 0 deletions test/Faker/Provider/fr_FR/PaymentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Faker\Test\Provider\fr_FR;

use Faker\Calculator\Luhn;
use Faker\Generator;
use Faker\Provider\fr_FR\Payment;
use PHPUnit\Framework\TestCase;

class PaymentTest extends TestCase
{
private $faker;

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

public function testFormattedVat()
{
$vat = $this->faker->vat(true);
$this->assertRegExp("/^FR\s\w{2}\s\d{3}\s\d{3}\s\d{3}$/", $vat);

$vat = str_replace(' ', '', $vat);
$siren = substr($vat, 4, 12);
$this->assertTrue(Luhn::isValid($siren));

$key = (int) substr($siren, 2, 2);
if ($key === 0) {
$this->assertEqual($key, (12 + 3 * ($siren % 97)) % 97);
}
}

public function testUnformattedVat()
{
$vat = $this->faker->vat(false);
$this->assertRegExp("/^FR\w{2}\d{9}$/", $vat);

$siren = substr($vat, 4, 12);
$this->assertTrue(Luhn::isValid($siren));

$key = (int) substr($siren, 2, 2);
if ($key === 0) {
$this->assertEqual($key, (12 + 3 * ($siren % 97)) % 97);
}
}
}

0 comments on commit e8fce01

Please sign in to comment.