-
Notifications
You must be signed in to change notification settings - Fork 0
/
vat.go
executable file
·165 lines (152 loc) · 5.41 KB
/
vat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright (c) 2023 Golang Tanzania
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package vfd
import (
"fmt"
"math"
)
const (
StandardVATID = "A"
StandardVATRATE = 18.00
StandardVATCODE = 1
SpecialVATID = "B"
SpecialVATRATE = 0.00
SpecialVATCODE = 2
ZeroVATID = "C"
ZeroVATRATE = 0.00
ZeroVATCODE = 3
SpecialReliefVATID = "D"
SpecialReliefVATRATE = 0.00
SpecialReliefVATCODE = 4
ExemptedVATID = "E"
ExemptedVATRATE = 0.00
ExemptedVATCODE = 5
TaxableItemCode = 1
TaxableItemId = "A"
NonTaxableItemCode = 3
NonTaxableItemId = "C"
)
type (
ValueAddedTax struct {
ID string // ID is a character that identifies the ValueAddedTax it can be A,B,C,D or E
Code int64 // Code is a number that identifies the ValueAddedTax it can be 0,1,2,3 or 4
Name string
Percentage float64
}
)
var (
standardVAT = ValueAddedTax{
ID: StandardVATID,
Code: StandardVATCODE,
Name: "Standard ValueAddedTax",
Percentage: StandardVATRATE,
}
specialVAT = ValueAddedTax{
ID: SpecialVATID,
Code: SpecialVATCODE,
Name: "Special ValueAddedTax",
Percentage: SpecialVATRATE,
}
zeroVAT = ValueAddedTax{
ID: ZeroVATID,
Code: ZeroVATCODE,
Name: "Zero ValueAddedTax",
Percentage: ZeroVATRATE,
}
specialReliefVAT = ValueAddedTax{
ID: SpecialReliefVATID,
Code: SpecialReliefVATCODE,
Name: "Special Relief ValueAddedTax",
Percentage: SpecialReliefVATRATE,
}
exemptedVAT = ValueAddedTax{
ID: ExemptedVATID,
Code: ExemptedVATCODE,
Name: "Exempted ValueAddedTax",
Percentage: ExemptedVATRATE,
}
)
func (v *ValueAddedTax) NetAmount(totalAmount float64) float64 {
rate := 1.00 + (v.Percentage / 100)
netAmount := totalAmount / rate
return math.Round(netAmount*100) / 100
}
// Amount calculates the amount of ValueAddedTax that is charged to the buyer.
// The answer is rounded to 2 decimal places.
func (v *ValueAddedTax) Amount(totalAmount float64) float64 {
netAmount := v.NetAmount(totalAmount)
amount := totalAmount - netAmount
return math.Round(amount*100) / 100
}
func ParseTaxCode(code int64) ValueAddedTax {
switch code {
case 1:
return standardVAT
case 2:
return specialVAT
case 3:
return zeroVAT
case 4:
return specialReliefVAT
case 5:
return exemptedVAT
default:
return standardVAT
}
}
// ValueAddedTaxRate returns the ValueAddedTax rate of a certain ValueAddedTax category
func ValueAddedTaxRate(taxCode int64) float64 {
vat := ParseTaxCode(taxCode)
return vat.Percentage
}
// ValueAddedTaxID returns the ValueAddedTax id of a certain ValueAddedTax category
// It returns "A" for standard ValueAddedTax, "B" for special ValueAddedTax,
// "C" for zero ValueAddedTax,"D" for special relief and "E" for
// exempted ValueAddedTax.
func ValueAddedTaxID(taxCode int64) string {
vat := ParseTaxCode(taxCode)
return vat.ID
}
// NetAmount calculates the net price of a product of a certain ValueAddedTax category.
// This is the NetAmount which is collected by the seller without the ValueAddedTax.
// The buyer is charged this NetAmount plus the ValueAddedTax NetAmount. After calculating the
// answer is rounded to 2 decimal places.
// price = netPrice + netPrice * (vatRate / 100)
func NetAmount(taxCode int64, price float64) float64 {
vat := ParseTaxCode(taxCode)
return vat.NetAmount(price)
}
// ValueAddedTaxAmount calculates the amount of ValueAddedTax that is charged to the buyer.
// The answer is rounded to 2 decimal places.
func ValueAddedTaxAmount(taxCode int64, price float64) float64 {
vat := ParseTaxCode(taxCode)
netAmount := vat.NetAmount(price)
amount := price - netAmount
return math.Round(amount*100) / 100
}
// ReportTaxRateID creates a string that contains the ValueAddedTax rate and the ValueAddedTax id
// of a certain ValueAddedTax category. It returns "A-18.00" for standard ValueAddedTax,
// "B-10.00" for special ValueAddedTax, "C-0.00" for zero ValueAddedTax and so on. The ID is then
// used in Z Report to indicate the ValueAddedTax rate and the ValueAddedTax id.
func ReportTaxRateID(taxCode int64) string {
vat := ParseTaxCode(taxCode)
return fmt.Sprintf("%s-%.2f", vat.ID, vat.Percentage)
}