-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.go
executable file
·374 lines (332 loc) · 11.1 KB
/
report.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
* 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 (
"bytes"
"context"
"crypto/rsa"
"encoding/xml"
"fmt"
"io"
"net/http"
"regexp"
"strings"
xhttp "github.com/Golang-Tanzania/tra-vfd/internal/http"
"github.com/Golang-Tanzania/tra-vfd/internal/models"
)
var ErrReportSubmitFailed = fmt.Errorf("report submit failed")
type (
// ReportTotals contains different number of totals
ReportTotals struct {
DailyTotalAmount float64
Gross float64
Corrections float64
Discounts float64
Surcharges float64
TicketsVoid int64
TicketsVoidTotal float64
TicketsFiscal int64
TicketsNonFiscal int64
}
Address struct {
Name string
Street string
Mobile string
City string
Country string
}
ReportParams struct {
Date string
Time string
VRN string
TIN string
UIN string
TaxOffice string
RegistrationID string
ZNumber string
EFDSerial string
RegistrationDate string
}
ReportRequest struct {
Params *ReportParams
Address *Address
Totals *ReportTotals
VATS []VATTOTAL
Payment []Payment
}
)
// submitReport submits a report to the VFD server.
func submitReport(ctx context.Context, client *http.Client, requestURL string, headers *RequestHeaders,
privateKey *rsa.PrivateKey,
report *ReportRequest,
) (*Response, error) {
var (
certSerial = headers.CertSerial
bearerToken = headers.BearerToken
)
newContext, cancel := context.WithCancel(ctx)
defer cancel()
payload, err := ReportBytes(
privateKey, report.Params, *report.Address, report.VATS,
report.Payment, *report.Totals)
if err != nil {
return nil, fmt.Errorf("failed to generate the report payload: %w", err)
}
req, err := http.NewRequestWithContext(newContext, http.MethodPost, requestURL, bytes.NewBuffer(payload))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", ContentTypeXML)
req.Header.Set("Routing-Key", SubmitReportRoutingKey)
req.Header.Set("Cert-Serial", encodeBase64String(certSerial))
req.Header.Set("Authorization", fmt.Sprintf("bearer %s", bearerToken))
resp, err := client.Do(req)
if err != nil {
return nil, checkNetworkError(newContext, "submit report", err)
}
defer resp.Body.Close()
out, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("%v : %w", ErrReportSubmitFailed, err)
}
if resp.StatusCode == http.StatusInternalServerError {
errBody := models.Error{}
err = xml.NewDecoder(bytes.NewBuffer(out)).Decode(&errBody)
if err != nil {
return nil, fmt.Errorf("%v : %w", ErrReportSubmitFailed, err)
}
return nil, fmt.Errorf("registration error: %s", errBody.Message)
}
response := models.ReportAckEFDMS{}
err = xml.NewDecoder(bytes.NewBuffer(out)).Decode(&response)
if err != nil {
return nil, fmt.Errorf("%v : %w", ErrReportSubmitFailed, err)
}
return &Response{
Number: response.ZACK.ZNUMBER,
Date: response.ZACK.DATE,
Time: response.ZACK.TIME,
Code: response.ZACK.ACKCODE,
Message: response.ZACK.ACKMSG,
}, nil
}
func SubmitReport(ctx context.Context, url string, headers *RequestHeaders, privateKey *rsa.PrivateKey,
report *ReportRequest,
) (*Response, error) {
client := xhttp.Instance()
return submitReport(ctx, client, url, headers, privateKey, report)
}
func (lines *Address) AsList() []string {
return []string{
strings.ToUpper(lines.Name),
strings.ToUpper(lines.Street),
fmt.Sprintf("MOBILE: %s", lines.Mobile),
strings.ToUpper(fmt.Sprintf("%s,%s", lines.City, lines.Country)),
}
}
// sumVatTotals
func sumVatTotals(vats []VATTOTAL) models.VATTOTALS {
vatTotalMap := map[string]struct {
NetAmount float64
TaxAmount float64
}{
"A-18.00": {0, 0},
"B-0.00": {0, 0},
"C-0.00": {0, 0},
"D-0.00": {0, 0},
"E-0.00": {0, 0},
}
for _, vat := range vats {
rate := fmt.Sprintf("%s-%.2f", vat.ID, vat.Rate)
vatTotalMap[rate] = struct {
NetAmount float64
TaxAmount float64
}{
vatTotalMap[rate].NetAmount + vat.NetAmount,
vatTotalMap[rate].TaxAmount + vat.TaxAmount,
}
}
return models.VATTOTALS{
VATTOTAL: []*models.VATTOTAL{
{
VATRATE: "A-18.00",
NETTAMOUNT: fmt.Sprintf("%.2f", vatTotalMap["A-18.00"].NetAmount),
TAXAMOUNT: fmt.Sprintf("%.2f", vatTotalMap["A-18.00"].TaxAmount),
},
{
VATRATE: "B-0.00",
NETTAMOUNT: fmt.Sprintf("%.2f", vatTotalMap["B-0.00"].NetAmount),
TAXAMOUNT: fmt.Sprintf("%.2f", vatTotalMap["B-0.00"].TaxAmount),
},
{
VATRATE: "C-0.00",
NETTAMOUNT: fmt.Sprintf("%.2f", vatTotalMap["C-0.00"].NetAmount),
TAXAMOUNT: fmt.Sprintf("%.2f", vatTotalMap["C-0.00"].TaxAmount),
},
{
VATRATE: "D-0.00",
NETTAMOUNT: fmt.Sprintf("%.2f", vatTotalMap["D-0.00"].NetAmount),
TAXAMOUNT: fmt.Sprintf("%.2f", vatTotalMap["D-0.00"].TaxAmount),
},
{
VATRATE: "E-0.00",
NETTAMOUNT: fmt.Sprintf("%.2f", vatTotalMap["E-0.00"].NetAmount),
TAXAMOUNT: fmt.Sprintf("%.2f", vatTotalMap["E-0.00"].TaxAmount),
},
},
}
}
// sumPayments sums all payments
func sumPayments(payments []Payment) models.PAYMENTS {
paymentMap := map[string]float64{
"CASH": 0.0,
"CHEQUE": 0.0,
"CCARD": 0.0,
"EMONEY": 0.0,
"INVOICE": 0.0,
}
for _, p := range payments {
pType := string(p.Type)
paymentMap[pType] += p.Amount
}
// paymentList contains a list of payments and the order
// should be the same as in the paymentMap
paymentsList := make([]*models.PAYMENT, 5)
paymentsList[0] = &models.PAYMENT{
PMTTYPE: "CASH",
PMTAMOUNT: fmt.Sprintf("%.2f", paymentMap["CASH"]),
}
paymentsList[1] = &models.PAYMENT{
PMTTYPE: "CHEQUE",
PMTAMOUNT: fmt.Sprintf("%.2f", paymentMap["CHEQUE"]),
}
paymentsList[2] = &models.PAYMENT{
PMTTYPE: "CCARD",
PMTAMOUNT: fmt.Sprintf("%.2f", paymentMap["CCARD"]),
}
paymentsList[3] = &models.PAYMENT{
PMTTYPE: "EMONEY",
PMTAMOUNT: fmt.Sprintf("%.2f", paymentMap["EMONEY"]),
}
paymentsList[4] = &models.PAYMENT{
PMTTYPE: "INVOICE",
PMTAMOUNT: fmt.Sprintf("%.2f", paymentMap["INVOICE"]),
}
return models.PAYMENTS{
PAYMENT: paymentsList,
}
}
func generateZReport(params *ReportParams, address Address, vats []VATTOTAL, payments []Payment, totals ReportTotals) *models.ZREPORT {
const (
SIMIMSI = "WEBAPI"
FWVERSION = "3.0"
FWCHECKSUM = "WEBAPI"
VATCHANGENUM = "0"
HEADCHANGENUM = "0"
ERRORS = ""
)
PAYMENTS := sumPayments(payments)
VATTOTALS := sumVatTotals(vats)
TT := models.REPORTTOTALS{
DAILYTOTALAMOUNT: totals.DailyTotalAmount,
GROSS: totals.Gross,
CORRECTIONS: totals.Corrections,
DISCOUNTS: totals.Discounts,
SURCHARGES: totals.Surcharges,
TICKETSVOID: totals.TicketsVoid,
TICKETSVOIDTOTAL: totals.TicketsVoidTotal,
TICKETSFISCAL: totals.TicketsFiscal,
TICKETSNONFISCAL: totals.TicketsNonFiscal,
}
report := &models.ZREPORT{
XMLName: xml.Name{},
Text: "",
DATE: params.Date,
TIME: params.Time,
HEADER: struct {
Text string `xml:",chardata"`
LINE []string `xml:"LINE"`
}{
LINE: address.AsList(),
},
VRN: params.VRN,
TIN: params.TIN,
TAXOFFICE: params.TaxOffice,
REGID: params.RegistrationID,
ZNUMBER: params.ZNumber,
EFDSERIAL: params.EFDSerial,
REGISTRATIONDATE: params.RegistrationDate,
USER: params.UIN,
SIMIMSI: SIMIMSI,
TOTALS: TT,
VATTOTALS: VATTOTALS,
PAYMENTS: PAYMENTS,
CHANGES: struct {
Text string `xml:",chardata"`
VATCHANGENUM string `xml:"VATCHANGENUM"`
HEADCHANGENUM string `xml:"HEADCHANGENUM"`
}{
VATCHANGENUM: VATCHANGENUM,
HEADCHANGENUM: HEADCHANGENUM,
},
ERRORS: ERRORS,
FWVERSION: FWVERSION,
FWCHECKSUM: FWCHECKSUM,
}
// report.RoundOff()
return report
}
// ReportBytes returns the bytes of the report payload. It calls xml.Marshal on the report.
// then replace all the occurrences of <PAYMENT>, </PAYMENT>, <VATTOTAL>, </VATTOTAL> with empty string ""
// and then add the xml.Header to the beginning of the payload.
func ReportBytes(privateKey *rsa.PrivateKey, params *ReportParams, address Address,
vats []VATTOTAL, payments []Payment,
totals ReportTotals,
) ([]byte, error) {
zReport := generateZReport(params, address, vats, payments, totals)
payload, err := xml.Marshal(zReport)
if err != nil {
return nil, fmt.Errorf("failed to marshal the report: %w", err)
}
payloadString := formatReportXmlPayload(payload, totals, vats, payments)
signedPayload, err := SignPayload(privateKey, []byte(payloadString))
if err != nil {
return nil, fmt.Errorf("failed to sign the payload: %w", err)
}
base64PayloadSignature := encodeBase64Bytes(signedPayload)
report := fmt.Sprintf("<EFDMS>%s<EFDMSSIGNATURE>%s</EFDMSSIGNATURE></EFDMS>", payloadString, base64PayloadSignature)
report = fmt.Sprintf("%s%s", xml.Header, report)
return []byte(report), nil
}
func formatReportXmlPayload(payload []byte, totals ReportTotals, vats []VATTOTAL, payments []Payment) string {
replaceList := []string{"<PAYMENT>", "", "</PAYMENT>", "", "<VATTOTAL>", "", "</VATTOTAL>", ""}
replacer := strings.NewReplacer(replaceList...)
payloadString := replacer.Replace(string(payload))
var (
regexDailyAmount = regexp.MustCompile(`<DAILYTOTALAMOUNT>.*</DAILYTOTALAMOUNT>`)
regexGrossAmount = regexp.MustCompile(`<GROSS>.*</GROSS>`)
dailyAmountTag = fmt.Sprintf("<DAILYTOTALAMOUNT>%.2f</DAILYTOTALAMOUNT>", totals.DailyTotalAmount)
grossAmountTag = fmt.Sprintf("<GROSS>%.2f</GROSS>", totals.Gross)
)
payloadString = regexDailyAmount.ReplaceAllString(payloadString, dailyAmountTag)
payloadString = regexGrossAmount.ReplaceAllString(payloadString, grossAmountTag)
return payloadString
}