-
Notifications
You must be signed in to change notification settings - Fork 2
/
invoice.go
168 lines (142 loc) · 7.64 KB
/
invoice.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
package nfeio
import (
"encoding/json"
"fmt"
"time"
)
func (c *Client) getInvoicePath() string {
if c.apiUrl == ServiceUrl {
return "serviceinvoices"
}
return "productinvoices"
}
func (c *Client) GetInvoice(idCompany, idInvoice string) (response Invoice, err error) {
err = c.Get(fmt.Sprintf("companies/%s/%s/%s", idCompany, c.getInvoicePath(), idInvoice), nil, nil, &response)
return
}
func (c *Client) GetInvoices(idCompany string, pageCount, pageIndex int64) (response InvoicesResponse, err error) {
err = c.Get(fmt.Sprintf("companies/%s/%s", idCompany, c.getInvoicePath()), Params{"pageCount": pageCount, "pageIndex": pageIndex}, nil, &response)
return
}
func (c *Client) IssueInvoice(idCompany string, invoice *Invoice) (response Invoice, err error) {
err = c.Post(fmt.Sprintf("companies/%s/%s", idCompany, c.getInvoicePath()), invoice, nil, &response)
return
}
func (c *Client) CancelInvoice(idCompany, idInvoice string) (response Invoice, err error) {
err = c.Delete(fmt.Sprintf("companies/%s/%s/%s", idCompany, c.getInvoicePath(), idInvoice), nil, nil, &response)
return
}
func (c *Client) DownloadPDF(idCompany, idInvoice string) (pdf []byte, err error) {
pdf = make([]byte, 0)
err = c.Get(fmt.Sprintf("companies/%s/%s/%s/pdf", idCompany, c.getInvoicePath(), idInvoice), nil, nil, &pdf)
return
}
func (c *Client) DownloadXML(idCompany, idInvoice string) (xml string, err error) {
err = c.Get(fmt.Sprintf("companies/%s/%s/%s/xml", idCompany, c.getInvoicePath(), idInvoice), nil, nil, &xml)
return
}
func (c *Invoice) TotalAmount() (total float64) {
for _, item := range c.Items {
quantity, _ := item.Quantity.Float64()
unitamount, _ := item.UnitAmount.Float64()
total += quantity * unitamount
}
return
}
type InvoiceResponse struct {
Data Invoice `json:"serviceInvoices"`
}
type InvoicesResponse struct {
Data []Invoice `json:"serviceInvoices"`
pagination
}
type Invoice struct {
Id string `json:"id,omitempty"`
Environment string `json:"environment,omitempty"`
FlowStatus string `json:"flowStatus,omitempty"`
FlowMessage string `json:"flowMessage,omitempty"`
BatchNumber json.Number `json:"batchNumber,omitempty"`
BatchCheckNumber string `json:"batchCheckNumber,omitempty"`
Number json.Number `json:"number,omitempty"`
CheckCode string `json:"checkCode,omitempty"`
Status string `json:"status,omitempty"`
RpsType string `json:"rpsType,omitempty"`
RpsStatus string `json:"rpsStatus,omitempty"`
TaxationType string `json:"taxationType,omitempty"`
IssuedOn *time.Time `json:"issuedOn,omitempty"`
CancelledOn *time.Time `json:"cancelledOn,omitempty"`
RpsNumber json.Number `json:"rpsNumber,omitempty"`
RpsSerialNumber string `json:"rpsSerialNumber,omitempty"`
CityServiceCode string `json:"cityServiceCode,omitempty"`
FederalServiceCode string `json:"federalServiceCode,omitempty"`
Description string `json:"description,omitempty"`
ServicesAmount float64 `json:"servicesAmount,omitempty"`
DeductionsAmount float64 `json:"deductionsAmount,omitempty"`
DiscountUnconditionedAmount float64 `json:"discountUnconditionedAmount,omitempty"`
DiscountConditionedAmount float64 `json:"discountConditionedAmount,omitempty"`
BaseTaxAmount float64 `json:"baseTaxAmount,omitempty"`
IssRate json.Number `json:"issRate,omitempty"`
IssTaxAmount float64 `json:"issTaxAmount,omitempty"`
IrAmountWithheld float64 `json:"irAmountWithheld,omitempty"`
PisAmountWithheld float64 `json:"pisAmountWithheld,omitempty"`
CofinsAmountWithheld float64 `json:"cofinsAmountWithheld,omitempty"`
CsllAmountWithheld float64 `json:"csllAmountWithheld,omitempty"`
InssAmountWithheld float64 `json:"inssAmountWithheld,omitempty"`
IssAmountWithheld float64 `json:"issAmountWithheld,omitempty"`
OthersAmountWithheld float64 `json:"othersAmountWithheld,omitempty"`
AmountWithheld float64 `json:"amountWithheld,omitempty"`
AmountNet float64 `json:"amountNet,omitempty"`
Provider *Provider `json:"provider,omitempty"`
Borrower *Borrower `json:"borrower,omitempty"`
Buyer *Buyer `json:"buyer,omitempty"`
Items []Item `json:"items,omitempty"`
ApproximateTax *ApproximateTax `json:"approximateTax,omitempty"`
AdditionalInformation *AdditionalInformation `json:"additionalInformation,omitempty"`
CreatedOn *time.Time `json:"createdOn,omitempty"`
ModifiedOn *time.Time `json:"modifiedOn,omitempty"`
}
type ApproximateTax struct {
Source string `json:"source,omitempty"`
Version string `json:"version,omitempty"`
TotalRate json.Number `json:"totalRate,omitempty"`
}
type AdditionalInformation struct {
Fisco string `json:"fisco,omitempty"`
Taxpayer string `json:"taxpayer,omitempty"`
XmlAuthorized []json.Number `json:"xmlAuthorized,omitempty"`
Effort string `json:"effort,omitempty"`
Order string `json:"order,omitempty"`
Contract string `json:"contract,omitempty"`
TaxDocumentsReference []TaxDocumentsReference `json:"taxDocumentsReference,omitempty"`
TaxpayerComments []TaxpayerComments `json:"taxpayerComments,omitempty"`
ReferencedProcess []ReferencedProcess `json:"referencedProcess,omitempty"`
}
type TaxDocumentsReference struct {
TaxCouponInformation *TaxCouponInformation `json:"taxCouponInformation,omitempty"`
DocumentInvoiceReference *DocumentInvoiceReference `json:"documentInvoiceReference,omitempty"`
DocumentElectronicInvoice *DocumentElectronicInvoice `json:"documentElectronicInvoice,omitempty"`
}
type TaxCouponInformation struct {
ModelDocumentFiscal string `json:"modelDocumentFiscal,omitempty"`
OrderECF string `json:"orderECF,omitempty"`
OrderCountOperation string `json:"orderCountOperation,omitempty"`
}
type DocumentInvoiceReference struct {
State json.Number `json:"state,omitempty"`
YearMonth string `json:"yearMonth,omitempty"`
FederalTaxNumber string `json:"federalTaxNumber,omitempty"`
Model string `json:"model,omitempty"`
Series string `json:"series,omitempty"`
Number string `json:"number,omitempty"`
}
type DocumentElectronicInvoice struct {
AccessKey string `json:"accessKey,omitempty"`
}
type TaxpayerComments struct {
Field string `json:"field,omitempty"`
Text string `json:"text,omitempty"`
}
type ReferencedProcess struct {
IdentifierConcessory string `json:"identifierConcessory,omitempty"`
IdentifierOrigin json.Number `json:"identifierOrigin,omitempty"`
}