fiscalizer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,47 @@
1
+ class Fiscalizer
2
+ class Tax
3
+
4
+ attr_accessor :base, :rate, :name
5
+
6
+ def initialize base: 0.0, rate: 0.0, name: ""
7
+ @base = base
8
+ @rate = rate
9
+ @name = name
10
+ end # initialize
11
+
12
+ # Math
13
+ def base
14
+ return @base.to_f.round(2)
15
+ end # base
16
+
17
+ def rate
18
+ return @rate.to_f.round(2)
19
+ end # rate
20
+
21
+ def total
22
+ return (base * (rate / 100.0) ).round(2)
23
+ end # total
24
+
25
+ def summed
26
+ return (base + total).round(2)
27
+ end # summed
28
+
29
+ # Convert to string
30
+ def base_str
31
+ return ("%15.2f" % base).strip
32
+ end # base_str
33
+
34
+ def rate_str
35
+ return ("%3.2f" % rate).strip
36
+ end # rate_str
37
+
38
+ def total_str
39
+ return ("%15.2f" % total).strip
40
+ end # total_str
41
+
42
+ def summed_str
43
+ return ("%15.2f" % summed ).strip
44
+ end # summed_str
45
+
46
+ end # Tax
47
+ end # Fiscalizer
@@ -0,0 +1,3 @@
1
+ class Fiscalizer
2
+ VERSION = "0.0.1"
3
+ end
data/test/test_echo.rb ADDED
@@ -0,0 +1,20 @@
1
+ require "test/unit"
2
+ require "fiscalizer"
3
+
4
+ class EchoTest < Test::Unit::TestCase
5
+
6
+ def test_initialization
7
+ # Manual build
8
+ echo = Fiscalizer::Echo.new
9
+ echo.text = "This is a test of text assignment"
10
+ # Test
11
+ assert_equal "This is a test of text assignment", echo.text, "Text was not assigned"
12
+
13
+ # Automatic test
14
+ echo = nil
15
+ echo = Fiscalizer::Echo.new text: "This is a test of text assignment"
16
+ # Test
17
+ assert_equal "This is a test of text assignment", echo.text, "Text was not automatically assigned"
18
+ end # test_initialization
19
+
20
+ end # EchoTest
data/test/test_fee ADDED
@@ -0,0 +1,64 @@
1
+ require "test/unit"
2
+ require "fiscalizer"
3
+
4
+ class FeeTest < Test::Unit::TestCase
5
+
6
+ def test_initialization
7
+ # Manual build
8
+ fee = Fiscalizer::Fee.new
9
+ fee.name = "My cool new fee"
10
+ fee.value = 234.16
11
+ assert_equal "My cool new fee", fee.name, "Fee's name not set"
12
+ assert_equal 234.16, fee.value, "Fee's value not set"
13
+
14
+ # Automatic build
15
+ fee = Fiscalizer::Fee.new name: "My cool new fee", value: 234.16
16
+ assert_equal "My cool new fee", fee.name, "Fee's name not set"
17
+ assert_equal 234.16, fee.value, "Fee's value not set"
18
+ end # test_initialization
19
+
20
+ def test_math
21
+ # Whole number
22
+ fee = Fiscalizer::Fee.new name: "Test", value: 100
23
+ assert_equal 100, fee.value, "Whole number is wrong"
24
+
25
+ # Decimal number
26
+ fee = Fiscalizer::Fee.new name: "Test", value: 13.67
27
+ assert_equal 13.67, fee.value, "Decimal number is wrong"
28
+
29
+ # Decimal whole number
30
+ fee = Fiscalizer::Fee.new name: "Test", value: 13.00
31
+ assert_equal 13.00, fee.value, "Decimal whole number is wrong"
32
+
33
+ # Rounding down
34
+ fee = Fiscalizer::Fee.new name: "Test", value: 13.674961205
35
+ assert_equal 13.67, fee.value, "Rounding down is wrong"
36
+
37
+ # Rounding up
38
+ fee = Fiscalizer::Fee.new name: "Test", value: 13.675961205
39
+ assert_equal 13.68, fee.value, "Rounding down is wrong"
40
+ end # test_math
41
+
42
+ def test_string
43
+ # Whole number
44
+ fee = Fiscalizer::Fee.new name: "Test", value: 100
45
+ assert_equal "100.00", fee.value_str, "Whole number string is wrong"
46
+
47
+ # Decimal number
48
+ fee = Fiscalizer::Fee.new name: "Test", value: 13.67
49
+ assert_equal "13.67", fee.value_str, "Decimal number string is wrong"
50
+
51
+ # Decimal whole number
52
+ fee = Fiscalizer::Fee.new name: "Test", value: 13.00
53
+ assert_equal "13.00", fee.value_str, "Decimal whole number string is wrong"
54
+
55
+ # Rounding down
56
+ fee = Fiscalizer::Fee.new name: "Test", value: 13.674961205
57
+ assert_equal "13.67", fee.value_str, "Rounding down string is wrong"
58
+
59
+ # Rounding up
60
+ fee = Fiscalizer::Fee.new name: "Test", value: 13.675961205
61
+ assert_equal "13.68", fee.value_str, "Rounding down string is wrong"
62
+ end # test_string
63
+
64
+ end
@@ -0,0 +1,166 @@
1
+ require "test/unit"
2
+ require "fiscalizer"
3
+ require 'nokogiri'
4
+ require 'openssl'
5
+
6
+ class FiscalizerTest < Test::Unit::TestCase
7
+ # Configure
8
+ KEY_PUBLIC_PATH = "/path/to/fiskal1.cert"
9
+ KEY_PRIVATE_PATH = "/path/to/privateKey.key"
10
+ CERTIFICATE_PATH = "/path/to/democacert.pem"
11
+ CERTIFICATE_P12_PATH = "/path/to/fiskal1.pfx"
12
+ URL_FISKAL = "https://cistest.apis-it.hr:8449/FiskalizacijaServiceTest"
13
+ CER_ISSUED = "OU=DEMO,O=FINA,C=HR"
14
+ PASSWORD = "12345678"
15
+ # Use P12
16
+ EXPORTED_KEYS = false
17
+ # Test specific info
18
+ UUID = "ca996cc7-fcc3-4c50-961b-40c8b875a5e8"
19
+ ECHO = "This is a simple test..."
20
+ # Personal information
21
+ PIN = "00000000000"
22
+ PIN_OPERATOR = "00000000000"
23
+
24
+ def test_initialization
25
+ # Manual build
26
+ fiscal = Fiscalizer.new
27
+
28
+ # Populate
29
+ fiscal.url = "www.somewhere.com"
30
+ fiscal.key_public_path = "path/to/my/cert.pem"
31
+ fiscal.key_private_path = "path/to/my/cert.cer"
32
+
33
+ # Test
34
+ assert_equal "www.somewhere.com", fiscal.url, "Manual URL assignment"
35
+ assert_equal "path/to/my/cert.pem", fiscal.key_public_path, "Manual public certificate location assignment"
36
+ assert_equal "path/to/my/cert.cer", fiscal.key_private_path, "Manual private certificate location assignment"
37
+
38
+ # Automatic Build
39
+ fiscal = nil
40
+ fiscal = Fiscalizer.new url: "www.somewhere.com",
41
+ key_public_path: "path/to/my/cert.pem",
42
+ key_private_path: "path/to/my/cert.cer"
43
+
44
+ # Test
45
+ assert_equal "www.somewhere.com", fiscal.url, "Automatic URL assignment"
46
+ assert_equal "path/to/my/cert.pem", fiscal.key_public_path, "Automatic public certificate location assignment"
47
+ assert_equal "path/to/my/cert.cer", fiscal.key_private_path, "Automatic private certificate location assignment"
48
+ end # fiscal_ruby_test
49
+
50
+ def test_echo
51
+ fiscal = Fiscalizer.new url: URL_FISKAL,
52
+ key_public_path: KEY_PUBLIC_PATH,
53
+ key_private_path: KEY_PRIVATE_PATH,
54
+ certificate_path: CERTIFICATE_PATH,
55
+ certificate_issued_by: CER_ISSUED
56
+ echo = fiscal.echo text: ECHO
57
+ assert_equal ECHO, echo.response, "Echo response message does not match sent message"
58
+ assert echo.echo?, "Automatic echo check failed"
59
+ end # test_echo
60
+
61
+ def test_office
62
+ # Configuration
63
+ use_exported_keys = true
64
+ # -- Here Be Dragons --
65
+ fiscal = nil
66
+ if use_exported_keys
67
+ fiscal = Fiscalizer.new url: URL_FISKAL,
68
+ key_public_path: KEY_PUBLIC_PATH,
69
+ key_private_path: KEY_PRIVATE_PATH,
70
+ certificate_path: CERTIFICATE_PATH,
71
+ certificate_issued_by: CER_ISSUED
72
+ else
73
+ fiscal = Fiscalizer.new url: URL_FISKAL,
74
+ certificate_path: CERTIFICATE_PATH,
75
+ certificate_p12_path: CERTIFICATE_P12_PATH,
76
+ certificate_issued_by: CER_ISSUED,
77
+ password: PASSWORD
78
+ end
79
+
80
+ assert fiscal!=nil, "Failed to initialize"
81
+ # Generate invoice
82
+ office = fiscal.office uuid: UUID,
83
+ time_sent: Time.now,
84
+ pin: PIN,
85
+ office_label: "Poslovnica1",
86
+ adress_street_name: "Somewhere",
87
+ adress_house_num: "42",
88
+ adress_house_num_addendum: "AD",
89
+ adress_post_num: "10000",
90
+ adress_settlement: "Block 25-C",
91
+ adress_township: "Vogsphere",
92
+ adress_other: nil,
93
+ office_time: "Pon-Pet: 8:00-16:00",
94
+ take_effect_date: Time.now + 3600 * 24 * 7,
95
+ closure_mark: nil,
96
+ specific_purpose: nil
97
+ assert !office.errors?, "Returned an error"
98
+ assert office.uuid != nil, "'UUID' was not returned"
99
+ assert office.processed_at != nil, "'Processed at' was not returned"
100
+ end # test_office
101
+
102
+ def test_invoice
103
+ use_exported_keys = true
104
+ # -- Here Be Dragons --
105
+ fiscal = nil
106
+ if use_exported_keys
107
+ fiscal = Fiscalizer.new url: URL_FISKAL,
108
+ key_public_path: KEY_PUBLIC_PATH,
109
+ key_private_path: KEY_PRIVATE_PATH,
110
+ certificate_path: CERTIFICATE_PATH,
111
+ certificate_issued_by: CER_ISSUED
112
+ else
113
+ fiscal = Fiscalizer.new url: URL_FISKAL,
114
+ certificate_path: CERTIFICATE_PATH,
115
+ certificate_p12_path: CERTIFICATE_P12_PATH,
116
+ certificate_issued_by: CER_ISSUED,
117
+ password: PASSWORD
118
+ end
119
+ # Generate taxes
120
+ taxes_vat = []
121
+ taxes_spending = []
122
+ taxes_other = []
123
+ (0..5).each do |i|
124
+ tax = Fiscalizer::Tax.new
125
+ tax.base = rand(10000 * 100).to_f / 100.0
126
+ tax.rate = rand(100 * 100).to_f / 100.0
127
+ taxes_vat << tax
128
+ end
129
+ (0..5).each do |i|
130
+ tax = Fiscalizer::Tax.new
131
+ tax.base = rand(10000 * 100).to_f / 100.0
132
+ tax.rate = rand(100 * 100).to_f / 100.0
133
+ taxes_spending << tax
134
+ end
135
+ (0..5).each do |i|
136
+ tax = Fiscalizer::Tax.new
137
+ tax.base = rand(10000 * 100).to_f / 100.0
138
+ tax.rate = rand(100 * 100).to_f / 100.0
139
+ tax.name = "My Test Tax #{i}"
140
+ taxes_other << tax
141
+ end
142
+ # Generate invoice
143
+ invoice = fiscal.invoice uuid: UUID,
144
+ time_sent: Time.now,
145
+ pin: PIN,
146
+ in_vat_system: true,
147
+ time_issued: Time.now - 3600,
148
+ consistance_mark: "P",
149
+ issued_number: "1",
150
+ issued_office: "Pm2",
151
+ issued_machine: "3",
152
+ tax_vat: taxes_vat,
153
+ tax_spending: taxes_spending,
154
+ tax_other: taxes_other,
155
+ payment_method: "g",
156
+ operator_pin: PIN_OPERATOR,
157
+ subsequent_delivery: false,
158
+ value_non_taxable: 200.0
159
+
160
+ assert !invoice.errors?, "Returned an error"
161
+ assert invoice.uuid != nil, "'UUID' was not returned"
162
+ assert invoice.processed_at != nil, "'Processed at' was not returned"
163
+ assert invoice.unique_identifier != nil, "Uniqe Identifier (JIR) was not returned"
164
+ end # test_invoice
165
+
166
+ end # FiscalizerRubyTest
@@ -0,0 +1,28 @@
1
+ require "test/unit"
2
+ require "fiscalizer"
3
+
4
+ class CommunicationTest < Test::Unit::TestCase
5
+
6
+ def test_initialization
7
+ # Manual build
8
+ comm = Fiscalizer::Communication.new
9
+ assert comm, "Failed to initialize"
10
+
11
+ # Default value
12
+ assert_equal "http://www.apis-it.hr/fin/2012/types/f73", comm.tns, "Tns was not assigned by default"
13
+ assert_equal "http://www.apis-it.hr/fin/2012/types/f73 FiskalizacijaSchema.xsd", comm.schemaLocation, "Schema Location was not assigned by default"
14
+
15
+ # Manual assignment
16
+ comm.tns = "test.tns.hr"
17
+ comm.schemaLocation = "test.tns.hr/Schema.xml"
18
+ assert_equal "test.tns.hr", comm.tns, "Tns was not assigned"
19
+ assert_equal "test.tns.hr/Schema.xml", comm.schemaLocation, "Schema Location was not assigned"
20
+
21
+ # Automatic assignment
22
+ comm = nil
23
+ comm = Fiscalizer::Communication.new tns: "test.tns.hr", schemaLocation: "test.tns.hr/Schema.xml"
24
+ assert_equal "test.tns.hr", comm.tns, "Tns was not assigned"
25
+ assert_equal "test.tns.hr/Schema.xml", comm.schemaLocation, "Schema Location was not assigned"
26
+ end # test_initialization
27
+
28
+ end
@@ -0,0 +1,11 @@
1
+ require "test/unit"
2
+ require "fiscalizer"
3
+
4
+ class InvoiceTest < Test::Unit::TestCase
5
+
6
+ def test_initialization
7
+ # Manual build
8
+ # Automatic build
9
+ end # test_initialization
10
+
11
+ end
@@ -0,0 +1,11 @@
1
+ require "test/unit"
2
+ require "fiscalizer"
3
+
4
+ class OfficeTest < Test::Unit::TestCase
5
+
6
+ def test_initialization
7
+ # Manual build
8
+ # Automatic build
9
+ end # test_initialization
10
+
11
+ end
data/test/test_tax.rb ADDED
@@ -0,0 +1,89 @@
1
+ require "test/unit"
2
+ require "fiscalizer"
3
+
4
+ class TaxTest < Test::Unit::TestCase
5
+
6
+ def test_initialization
7
+ # Manual build
8
+ tax = Fiscalizer::Tax.new
9
+ assert tax, "Failed to initialize"
10
+ tax.base = 100
11
+ tax.rate = 25
12
+ tax.name = "My test tax"
13
+ assert_equal 100, tax.base, "Wrong base assigned"
14
+ assert_equal 25, tax.rate, "Wrong rate assigned"
15
+ assert_equal "My test tax", tax.name, "Wrong name assigned"
16
+
17
+ tax = nil
18
+ tax = Fiscalizer::Tax.new base: 100, rate: 25, name: "My test tax"
19
+ assert tax, "Failed to initialize"
20
+ assert_equal 100, tax.base, "Automatically assigned wrong base"
21
+ assert_equal 25, tax.rate, "Automatically assigned wrong rate"
22
+ assert_equal "My test tax", tax.name, "Automatically assigned worng name"
23
+ end # test_initialization
24
+
25
+ def test_rounding
26
+ # Whole numbers
27
+ tax = Fiscalizer::Tax.new base: 100, rate: 1
28
+ assert_equal 100, tax.base, "Whole number base is wrong" # 100 -> 100
29
+ assert_equal 1, tax.rate, "Whole number rate is wrong" # 1 -> 1 -> 0.01
30
+ assert_equal 1, tax.total, "Whole number total is wrong" # 100 * 0.01 = 1
31
+ assert_equal 101, tax.summed, "Whole number summed is wrong" # 100 + 1 = 101
32
+
33
+ # Decimal numbers
34
+ tax = Fiscalizer::Tax.new base: 1.5, rate: 10.5
35
+ assert_equal 1.50, tax.base, "Decimal number base is wrong" # 1.5 -> 1.5
36
+ assert_equal 10.5, tax.rate, "Decimal number rate is wrong" # 10.5 -> 10.5 -> 0.105
37
+ assert_equal 0.16, tax.total, "Decimal number total is wrong" # 1.5 * 0.105 = 0.1575
38
+ assert_equal 1.66, tax.summed, "Decimal number summed is wrong" # 1.5 + 0.16 = 1.66
39
+
40
+ # Whole decimal numbers
41
+ tax = Fiscalizer::Tax.new base: 100.0, rate: 1.0
42
+ assert_equal 100.0, tax.base, "Whole decimal number base is wrong" # 100 -> 100
43
+ assert_equal 1.000, tax.rate, "Whole decimal number rate is wrong" # 1 -> 1 -> 0.01
44
+ assert_equal 1.000, tax.total, "Whole decimal number total is wrong" # 100 * 0.01 = 1
45
+ assert_equal 101.0, tax.summed, "Whole decimal number summed is wrong" # 100 + 1 = 101
46
+
47
+ # Round down
48
+ tax = Fiscalizer::Tax.new base: 13.111992, rate: 10.10499999999
49
+ assert_equal 13.11, tax.base, "Round down number base is wrong" # 13.111992 -> 13.11
50
+ assert_equal 10.10, tax.rate, "Round down number rate is wrong" # 10.10499999999 -> 10.10 -> 0.1010
51
+ assert_equal 1.320, tax.total, "Round down number total is wrong" # 13.11 * 0.1010 = 1.32411
52
+ assert_equal 14.43, tax.summed, "Round down number summed is wrong" # 13.11 + 1.32 = 14.43
53
+
54
+ # Round up
55
+ tax = Fiscalizer::Tax.new base: 13.2450000001, rate: 25.9989541
56
+ assert_equal 13.25, tax.base, "Round up number base is wrong" # 13.2450000001 -> 13.25
57
+ assert_equal 26.00, tax.rate, "Round up number rate is wrong" # 25.9989541 -> 26 -> 0.26
58
+ assert_equal 3.450, tax.total, "Round up number total is wrong" # 13.25 * 0.26 = 3.445
59
+ assert_equal 16.70, tax.summed, "Round up number summed is wrong" # 13.25 + 3.45 = 16.7
60
+ end # test_rounding
61
+
62
+ def test_string_conversion
63
+ # Whole numbers
64
+ tax = Fiscalizer::Tax.new base: 100, rate: 1
65
+ assert_equal "100.00", tax.base_str, "Whole number base is wrong"
66
+ assert_equal "1.00", tax.rate_str, "Whole number rate is wrong"
67
+ assert_equal "1.00", tax.total_str, "Whole number total is wrong"
68
+ assert_equal "101.00", tax.summed_str, "Whole number summed is wrong"
69
+
70
+ # Decimal numbers
71
+ tax = Fiscalizer::Tax.new base: 1.5, rate: 10.5
72
+ assert_equal "1.50", tax.base_str, "Decimal number base is wrong"
73
+ assert_equal "10.50", tax.rate_str, "Decimal number rate is wrong"
74
+ assert_equal "0.16", tax.total_str, "Decimal number total is wrong"
75
+ assert_equal "1.66", tax.summed_str, "Decimal number summed is wrong"
76
+
77
+ # Whole decimal numbers
78
+ tax = Fiscalizer::Tax.new base: 100.000, rate: 1.000
79
+ assert_equal "100.00", tax.base_str, "Whole decimal number base is wrong"
80
+ assert_equal "1.00", tax.rate_str, "Whole decimal number rate is wrong"
81
+ assert_equal "1.00", tax.total_str, "Whole decimal number total is wrong"
82
+ assert_equal "101.00", tax.summed_str, "Whole decimal number summed is wrong"
83
+ end # test_string_conversion
84
+
85
+ def test_math
86
+
87
+ end # test_math
88
+
89
+ end