Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Faker\Provider\en_GB;
4
 
5
class Company extends \Faker\Provider\Company
6
{
7
    public const VAT_PREFIX = 'GB';
8
    public const VAT_TYPE_DEFAULT = 'vat';
9
    public const VAT_TYPE_BRANCH = 'branch';
10
    public const VAT_TYPE_GOVERNMENT = 'gov';
11
    public const VAT_TYPE_HEALTH_AUTHORITY = 'health';
12
 
13
    /**
14
     * UK VAT number
15
     *
16
     * This method produces numbers that are _reasonably_ representative
17
     * of those issued by government
18
     *
19
     * @see https://en.wikipedia.org/wiki/VAT_identification_number#VAT_numbers_by_country
20
     */
21
    public static function vat(string $type = null): string
22
    {
23
        switch ($type) {
24
            case static::VAT_TYPE_BRANCH:
25
                return static::generateBranchTraderVatNumber();
26
 
27
            case static::VAT_TYPE_GOVERNMENT:
28
                return static::generateGovernmentVatNumber();
29
 
30
            case static::VAT_TYPE_HEALTH_AUTHORITY:
31
                return static::generateHealthAuthorityVatNumber();
32
 
33
            default:
34
                return static::generateStandardVatNumber();
35
        }
36
    }
37
 
38
    /**
39
     * Standard
40
     * 9 digits (block of 3, block of 4, block of 2)
41
     *
42
     * This uses the format introduced November 2009 onward where the first
43
     * block starts from 100 and the final two digits are generated via a the
44
     * modulus 9755 algorithm
45
     */
46
    private static function generateStandardVatNumber(): string
47
    {
48
        $firstBlock = static::numberBetween(100, 999);
49
        $secondBlock = static::randomNumber(4, true);
50
 
51
        return sprintf(
52
            '%s%d %d %d',
53
            static::VAT_PREFIX,
54
            $firstBlock,
55
            $secondBlock,
56
            static::calculateModulus97($firstBlock . $secondBlock),
57
        );
58
    }
59
 
60
    /**
61
     * Health authorities
62
     * the letters HA then 3 digits from 500 to 999 (e.g. GBHA599)
63
     */
64
    private static function generateHealthAuthorityVatNumber(): string
65
    {
66
        return sprintf(
67
            '%sHA%d',
68
            static::VAT_PREFIX,
69
            static::numberBetween(500, 999),
70
        );
71
    }
72
 
73
    /**
74
     * Branch traders
75
     * 12 digits (as for 9 digits, followed by a block of 3 digits)
76
     */
77
    private static function generateBranchTraderVatNumber(): string
78
    {
79
        return sprintf(
80
            '%s %d',
81
            static::generateStandardVatNumber(),
82
            static::randomNumber(3, true),
83
        );
84
    }
85
 
86
    /**
87
     * Government departments
88
     * the letters GD then 3 digits from 000 to 499 (e.g. GBGD001)
89
     */
90
    private static function generateGovernmentVatNumber(): string
91
    {
92
        return sprintf(
93
            '%sGD%s',
94
            static::VAT_PREFIX,
95
            str_pad((string) static::numberBetween(0, 499), 3, '0', STR_PAD_LEFT),
96
        );
97
    }
98
 
99
    /**
100
     * Apply a Modulus97 algorithm to an input
101
     *
102
     * @see https://library.croneri.co.uk/cch_uk/bvr/43-600
103
     */
104
    public static function calculateModulus97(string $input, bool $use9755 = true): string
105
    {
106
        $digits = str_split($input);
107
 
108
        if (count($digits) !== 7) {
109
            throw new \InvalidArgumentException();
110
        }
111
        $multiplier = 8;
112
        $sum = 0;
113
 
114
        foreach ($digits as $digit) {
115
            $sum += (int) $digit * $multiplier;
116
            --$multiplier ;
117
        }
118
 
119
        if ($use9755) {
120
            $sum = $sum + 55;
121
        }
122
 
123
        while ($sum > 0) {
124
            $sum -= 97;
125
        }
126
        $sum = $sum * -1;
127
 
128
        return str_pad((string) $sum, 2, '0', STR_PAD_LEFT);
129
    }
130
}