pl-commerce 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,122 @@
1
+ # Copyright: mati@sz.home.pl
2
+ # License: LGPL
3
+ # Date: 12-02-2007
4
+ # Version 0.1
5
+ # http://pl.wikipedia.org/wiki/NIP
6
+ module PLCommerce
7
+ class Nip
8
+
9
+ CONTROL_CODE_POSITION = 9;
10
+ WAGES = [6,5,7,2,3,4,5,6,7];
11
+
12
+ #constructor
13
+ def initialize(nipL, opts = {:lenient => false})
14
+ raise ArgumentError, 'nip argument must be either of type String or Bignum' unless nipL.instance_of? String or nipL.instance_of? Bignum
15
+ @lenient = opts[:lenient];
16
+ #puts nip
17
+ @nip = strip_NIP(nipL);
18
+ if not @lenient
19
+ pre_validate_NIP(@nip);
20
+ end
21
+ end
22
+
23
+ def valid?
24
+ #if lenient and validation produces problems
25
+ #it means that this number is not correct
26
+ begin
27
+ pre_validate_NIP(@nip)
28
+ rescue Exception => e
29
+ if @lenient
30
+ return false;
31
+ end
32
+
33
+ raise e;
34
+ end
35
+
36
+ sum = 0;
37
+ for i in 0..(@nip.length-2)
38
+ sum += @nip[i, 1].to_i * WAGES[i].to_i;
39
+ end
40
+
41
+ iControlCode = sum % 11;
42
+ iControlCode = iControlCode % 10;
43
+
44
+ iControlNIPValue = @nip[CONTROL_CODE_POSITION, 1].to_i;
45
+
46
+ return (iControlCode == iControlNIPValue);
47
+ end
48
+
49
+ #gets the code of Inland Revenue
50
+ def get_ir_code
51
+ begin
52
+ pre_validate_NIP(@nip, true, false, false)
53
+ #3 characters is minimum to get IR code
54
+ if @nip.length < 3
55
+ return '?';
56
+ end
57
+ rescue Exception => e
58
+ if @lenient
59
+ return '?';
60
+ end
61
+
62
+ raise e;
63
+ end
64
+
65
+ return @nip[0, 3].to_i;
66
+ end
67
+
68
+ def get_ir_code_desc
69
+ begin
70
+ pre_validate_NIP(@nip, true, false, false)
71
+ rescue Exception => e
72
+ if @lenient
73
+ return '?';
74
+ end
75
+
76
+ raise e;
77
+ end
78
+
79
+ return PLCommerce::IRCodes.get_ir_code_dsc(get_ir_code);
80
+ end
81
+
82
+ def self.valid?(nip, lenient = false)
83
+ return Nip.new(nip, lenient).valid?;
84
+ end
85
+
86
+ def to_s
87
+ return @nip;
88
+ end
89
+
90
+ def pre_validate_NIP(nip, val_nil_and_empty = true, val_size = true, val_numeric = true)
91
+ raise ArgumentError, 'Must provide NIP' if (nip.nil? || nip.empty?) && val_nil_and_empty
92
+ raise ArgumentError, 'Must be 10 digit' if ((not nip.length == 10) && val_size)
93
+ if (Regexp.compile('(\d{3}-\d{3}-\d{2}-\d{2})|(\d{3}-\d{2}-\d{2}-\d{3})|(\d{10})').match(nip) == nil && val_numeric)
94
+ raise ArgumentError, 'NIP should be numeric and have format of xxx-xxx-xx-xx or xxx-xx-xx-xxx'
95
+ end
96
+ end
97
+
98
+ #removed dashed and whitespaces, tabs from a string number
99
+ def strip_NIP(nip)
100
+ #first sub replaces all dash characters with empty string
101
+ #second sub replaced all whitespaced, tabs with empty string
102
+
103
+ sStrippedNIP = String.new(nip.to_s)
104
+ for i in 1..nip.to_s.count(' -')
105
+ sStrippedNIP = sStrippedNIP.sub(/[-]/, '')
106
+ sStrippedNIP = sStrippedNIP.sub(/[\s]/, '')
107
+ end
108
+
109
+ return sStrippedNIP
110
+ end
111
+
112
+ alias ir_code get_ir_code;
113
+ alias ir_code_desc get_ir_code_desc;
114
+ private(:strip_NIP, :pre_validate_NIP);
115
+
116
+ end
117
+ end
118
+
119
+ #aFile = File.new('src/nip_ir_codes.yml')
120
+ #aFile.each_line do |line|
121
+ # puts line[0,3] + " => '" + line[5,line.length-(5+1)] + "',";
122
+ #end
@@ -0,0 +1,169 @@
1
+ # Copyright: mati@sz.home.pl
2
+ # License: LGPL
3
+ # Date: 12-02-2007
4
+ # Version 0.1.0
5
+ #
6
+ # http://chemeng.p.lodz.pl/zylla/ut/pesel.html
7
+ # http://pl.wikipedia.org/wiki/PESEL
8
+ require 'date'
9
+ module PLCommerce
10
+ class Pesel
11
+
12
+ SEX_POSITION = 9;
13
+ CONTROL_CODE_POSITION = 10;
14
+
15
+ WAGES = [1,3,7,9,1,3,7,9,1,3];
16
+
17
+ # Constructor
18
+ # It takes pesel as first argument
19
+ # first argument can be either number or string
20
+ def initialize(pesel, opts = {:lenient => false})
21
+ raise ArgumentError, 'pesel argument must be either of type String or Bignum' unless pesel.instance_of? String or pesel.instance_of? Bignum
22
+
23
+ @lenient = opts[:lenient];
24
+ @pesel = strip_PESEL(pesel);
25
+ if not @lenient
26
+ pre_validate_PESEL(@pesel);
27
+ end
28
+ end
29
+
30
+ def sex
31
+ begin
32
+ #to get sex we don't need all characters to be numeric but we need the same length
33
+ pre_validate_PESEL(@pesel, true, true, false)
34
+ rescue Exception => e
35
+ return '?'
36
+ end
37
+
38
+ iSexValue = @pesel[SEX_POSITION, 1].to_i
39
+ if (iSexValue % 2) == 0
40
+ return 'F'
41
+ end
42
+
43
+ return 'M'
44
+ end
45
+
46
+ def male?
47
+ return sex == 'M'
48
+ end
49
+
50
+ def female?
51
+ return sex == 'F'
52
+ end
53
+
54
+ def valid?
55
+ #if lenient, it means we will not be throwing exceptions if pesel is incorrect
56
+ # we will simply return false in that case
57
+ if @lenient
58
+ begin
59
+ pre_validate_PESEL(@pesel)
60
+ rescue Exception => e
61
+ return false
62
+ end
63
+ end
64
+
65
+ sum = 0
66
+ for i in 0..(@pesel.length-2)
67
+ sum += @pesel[i, 1].to_i * WAGES[i].to_i
68
+ end
69
+
70
+ iControlCode = sum % 10;
71
+
72
+ iControlCode = 10 - iControlCode;
73
+ if iControlCode == 10
74
+ iControlCode = 0;
75
+ end
76
+
77
+ iControlPESELValue = @pesel[CONTROL_CODE_POSITION, 1].to_i;
78
+
79
+ return (iControlPESELValue == iControlCode);
80
+ end
81
+
82
+ def get_date_of_birth
83
+ begin
84
+ pre_validate_PESEL(@pesel, true, false, false)
85
+ #we need at least first 6 numbers
86
+ if @pesel.length < 6
87
+ return '?';
88
+ end
89
+
90
+ rescue Exception => e
91
+ if @lenient
92
+ return '?'
93
+ end
94
+
95
+ raise e
96
+ end
97
+
98
+ sYearLow = '19';
99
+ sYearHigh = @pesel[0, 2];
100
+ iMonth = @pesel[2, 2].to_i;
101
+ iDay = @pesel[4, 2].to_i;
102
+
103
+ iMileniumCode = @pesel[2, 1].to_i;
104
+ case iMileniumCode
105
+ #(1900 - 1999) 20 century
106
+ when 0..1
107
+ sYearLow = '19';
108
+ #(2000 - 2099) 21st century
109
+ when 2..3
110
+ sYearLow = '20';
111
+ iMonth = iMonth - 20;
112
+ #(2100 - 2199) 22nd century
113
+ when 4..5
114
+ begin
115
+ sYearLow = '21';
116
+ iMonth = iMonth - 40;
117
+ end
118
+ #(2200 - 2299) 23rd century
119
+ when 6..7
120
+ sYearLow = '22';
121
+ iMonth = iMonth - 60;
122
+
123
+ #(1800 - 1899) 19th century
124
+ when 8..9
125
+ sYearLow = '18';
126
+ iMonth = iMonth - 80;
127
+ end
128
+
129
+ sYear = sYearLow + sYearHigh;
130
+ iYear = sYear.to_i;
131
+
132
+ return Date.new(iYear, iMonth, iDay)
133
+ end
134
+
135
+ def self.valid?(pesel, lenient = false)
136
+ return Pesel.new(pesel, lenient).valid?
137
+ end
138
+
139
+ def self.sex?(pesel, lenient = false)
140
+ return Pesel.new(pesel, lenient).sex?
141
+ end
142
+
143
+ def to_s
144
+ return @pesel;
145
+ end
146
+
147
+ def pre_validate_PESEL(pesel, val_nil_and_empty = true, val_size = true, val_numeric = true)
148
+ raise ArgumentError, 'Must provide PESEL' if ((pesel.nil? || pesel.empty?) && val_nil_and_empty);
149
+ raise ArgumentError, 'Pesel size should be exacly eleven characters' if ((not pesel.length == 11) && val_size);
150
+ raise ArgumentError, 'Pesel should be numeric' if (Regexp.compile('(\d{11})').match(pesel) == nil && val_numeric);
151
+ end
152
+
153
+ ##removes whitespaces, tabs from a string pesel number
154
+ def strip_PESEL(pesel)
155
+ sStrippedPESEL = String.new(pesel.to_s)
156
+ for i in 1..sStrippedPESEL.count(' ')
157
+ sStrippedPESEL = sStrippedPESEL.sub(/[\s]/, '')
158
+ end
159
+
160
+ return sStrippedPESEL
161
+ end
162
+
163
+ alias dob get_date_of_birth
164
+ alias date_of_birth get_date_of_birth
165
+
166
+ private(:pre_validate_PESEL, :strip_PESEL)
167
+
168
+ end
169
+ end
@@ -0,0 +1,19 @@
1
+ require 'test/unit'
2
+ require 'lib/pl-commerce'
3
+
4
+ class IRCodesTest < Test::Unit::TestCase
5
+ #def setup
6
+ #end
7
+
8
+ #def teardown
9
+ #end
10
+
11
+ def test_ir_code1
12
+ assert('Urząd Skarbowy Poznań-Nowe Miasto' == PLCommerce::IRCodes.get_ir_code_dsc(362));
13
+ end
14
+
15
+ def test_ir_code2
16
+ assert('Urząd Skarbowy we Wrześni' == PLCommerce::IRCodes.get_ir_code_dsc(369));
17
+ end
18
+
19
+ end
@@ -0,0 +1,66 @@
1
+ require 'test/unit'
2
+ require 'date'
3
+ require 'lib/pl-commerce'
4
+
5
+ class NipTest < Test::Unit::TestCase
6
+ #def setup
7
+ #end
8
+
9
+ #def teardown
10
+ #end
11
+
12
+ def test_nip1
13
+ n = PLCommerce::Nip.new('362-398-12-30');
14
+ assert(n.valid?, 'Nip should be valid');
15
+ assert(n.ir_code == 362, 'IR code should be 362');
16
+ assert(n.ir_code_desc == 'Urząd Skarbowy Poznań-Nowe Miasto', 'Should not be null');
17
+ end
18
+
19
+ def test_nip2
20
+ n = PLCommerce::Nip.new('123-456-32-18');
21
+ assert(n.valid?, 'NIP should be valid');
22
+ assert(n.to_s == '1234563218');
23
+ assert(n.ir_code_desc == 'Urząd Skarbowy w Piasecznie', 'Should not be null');
24
+ end
25
+
26
+ def test_nip3
27
+ n = PLCommerce::Nip.new('120-456-33-17');
28
+ assert(!n.valid?, 'NIP should not be valid');
29
+ end
30
+
31
+ def test_nip4
32
+ n = PLCommerce::Nip.new('1234563218');
33
+ assert(n.valid?, 'NIP should be valid');
34
+ end
35
+
36
+ def test_nip5
37
+ n = PLCommerce::Nip.new(' 1 2 3 4 5 6 3 2 1 8');
38
+ assert(n.valid?, 'NIP should be valid');
39
+ assert(n.to_s == '1234563218');
40
+ end
41
+
42
+ def test_nip6
43
+ n = PLCommerce::Nip.new('123-4 5 6 3 2-18');
44
+ assert(n.valid?, 'NIP should be valid');
45
+ assert(n.to_s == '1234563218');
46
+ end
47
+
48
+ def test_nip7
49
+ n = PLCommerce::Nip.new(1234563218);
50
+ assert(n.valid?, 'NIP should be valid');
51
+ assert(n.to_s == '1234563218');
52
+ end
53
+
54
+ def test_nip8
55
+ n = PLCommerce::Nip.new('123456321x', :lenient => true);
56
+ assert(n.valid? == false, 'NIP should be invalid');
57
+ end
58
+
59
+ def test_nip9
60
+ n = PLCommerce::Nip.new('362', :lenient => true);
61
+ assert(n.valid? == false, 'NIP should be invalid');
62
+ assert(n.ir_code == 362);
63
+ assert(n.ir_code_desc == 'Urząd Skarbowy Poznań-Nowe Miasto');
64
+ end
65
+
66
+ end
@@ -0,0 +1,137 @@
1
+ require 'test/unit'
2
+ require 'date'
3
+ require 'lib/pl-commerce'
4
+
5
+ class PeselTest < Test::Unit::TestCase
6
+ #def setup
7
+ #end
8
+
9
+ #def teardown
10
+ #end
11
+
12
+ def test_pesel1
13
+ p = PLCommerce::Pesel.new('49040501580');
14
+ assert(p.valid?, 'Pesel should be valid');
15
+ assert(p.female?, 'Should be female');
16
+ assert(p.sex == 'F', 'Should be F');
17
+ assert(p.dob == Date.new(1949, 4, 5), 'Date should match');
18
+ assert(p.to_s == '49040501580', 'to_s method should return pesel');
19
+ end
20
+
21
+ def test_pesel2
22
+ p = PLCommerce::Pesel.new('79082203573');
23
+ assert(p.valid?, 'Pesel should be valid');
24
+ assert(p.male?, 'Should be male');
25
+ assert(p.sex == 'M', 'Should be M');
26
+ assert(p.dob == Date.new(1979, 8, 22), 'Date should match');
27
+ end
28
+
29
+ def test_pesel2a
30
+ p = PLCommerce::Pesel.new(79082203573);
31
+ assert(p.valid?, 'Pesel should be valid');
32
+ assert(p.male?, 'Should be male');
33
+ assert(p.sex == 'M', 'Should be M');
34
+ assert(p.dob == Date.new(1979, 8, 22), 'Date should match');
35
+ end
36
+
37
+ def test_pesel3
38
+ p = PLCommerce::Pesel.new('76101105382');
39
+ assert(p.valid?, 'Pesel should be valid');
40
+ assert(p.female?, 'Should be female');
41
+ assert(p.sex == 'F', 'Should be F');
42
+ assert(p.dob == Date.new(1976, 10, 11), 'Date should match');
43
+ end
44
+
45
+ def test_pesel3a
46
+ p = PLCommerce::Pesel.new('7 6 1 0 1 1 0 5 3 8 2');
47
+ assert(p.valid?, 'Pesel should be valid');
48
+ assert(p.female?, 'Should be female');
49
+ assert(p.sex == 'F', 'Should be F');
50
+ assert(p.dob == Date.new(1976, 10, 11), 'Date should match');
51
+ assert(p.to_s == '76101105382', 'to string method should return pesel');
52
+ end
53
+
54
+ def test_pesel4
55
+ p = PLCommerce::Pesel.new('76101105381');
56
+ assert(!p.valid?, 'Pesel should be invalid');
57
+ begin
58
+ assert(p.female?, 'Should be female');
59
+ assert(false, 'should not happen');
60
+ rescue Exception => e
61
+ end
62
+
63
+ begin
64
+ assert(p.sex == 'F', 'Should be F');
65
+ assert(false, 'should not happen');
66
+ rescue Exception => e
67
+ end
68
+
69
+ begin
70
+ assert(p.dob == Date.new(1976, 10, 11));
71
+ assert(false, 'should not happen');
72
+ rescue Exception => e
73
+ end
74
+ end
75
+
76
+ def test_pesel5
77
+ p = PLCommerce::Pesel.new('4904050158x', :lenient => true);
78
+ assert(p.valid? == false, 'Pesel should be invalid');
79
+ assert(p.dob == Date.new(1949, 4, 5), 'Date should match even pesel is invalid');
80
+ assert(p.female?, 'We should be able to find out that it is female');
81
+ end
82
+
83
+ def test_pesel6
84
+ p = PLCommerce::Pesel.new('', :lenient => true);
85
+ assert(p.valid? == false, 'Pesel should be invalid');
86
+ assert(p.sex == '?', 'We have no idea of sex');
87
+ assert(p.female? == false, 'We have no idea of sex');
88
+ assert(p.male? == false, 'We have no idea of sex');
89
+ end
90
+
91
+ def test_pesel7
92
+ p = PLCommerce::Pesel.new('790822', :lenient => true);
93
+ assert(p.valid? == false, 'Pesel should be invalid');
94
+ assert(p.sex == '?', 'We have no idea of sex');
95
+ assert(p.female? == false, 'We have no idea of sex');
96
+ assert(p.male? == false, 'We have no idea of sex');
97
+ assert(p.dob == Date.new(1979, 8, 22), 'Date should match, enough date fields');
98
+ end
99
+
100
+ def test_pesel8
101
+ p = PLCommerce::Pesel.new('79082', :lenient => true);
102
+ assert(p.valid? == false, 'Pesel should be invalid');
103
+ assert(p.sex == '?', 'We have no idea of sex');
104
+ assert(p.female? == false, 'We have no idea of sex');
105
+ assert(p.male? == false, 'We have no idea of sex');
106
+ assert(p.dob == '?', 'Not enough of date fields');
107
+ end
108
+
109
+ def test_pesel9
110
+ p = PLCommerce::Pesel.new('49240501586');
111
+ assert(p.valid?, 'Pesel should be valid');
112
+ assert(p.female?, 'Should be female');
113
+ assert(p.sex == 'F', 'Should be F');
114
+ assert(p.dob == Date.new(2049, 4, 5), 'Date should match');
115
+ assert(p.to_s == '49240501586', 'to_s method should return pesel');
116
+ end
117
+
118
+ #year 2149
119
+ def test_pesel10
120
+ p = PLCommerce::Pesel.new('49440501582');
121
+ assert(p.valid?, 'Pesel should be valid');
122
+ assert(p.female?, 'Should be female');
123
+ assert(p.sex == 'F', 'Should be F');
124
+ assert(p.dob == Date.new(2149, 4, 5), 'Date should match');
125
+ assert(p.to_s == '49440501582', 'to_s method should return pesel');
126
+ end
127
+
128
+ def test_pesel11
129
+ p = PLCommerce::Pesel.new('49520501583');
130
+ assert(p.valid?, 'Pesel should be valid');
131
+ assert(p.female?, 'Should be female');
132
+ assert(p.sex == 'F', 'Should be F');
133
+ assert(p.dob == Date.new(2149, 12, 5), 'Date should match');
134
+ assert(p.to_s == '49520501583', 'to_s method should return pesel');
135
+ end
136
+
137
+ end