-
Notifications
You must be signed in to change notification settings - Fork 3
/
CreateInvoice.go
128 lines (100 loc) · 4.02 KB
/
CreateInvoice.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
package whmcsgo
import (
"encoding/json"
"fmt"
"strings"
"time"
)
/*
CreateInvoice Create an invoice using the provided parameters.
WHMCS API docs
https://developers.whmcs.com/api-reference/getclients/
Request Parameters
draft
bool Should the invoice be created in draft status (No need to pass $status also) Optional
paymentmethod
string The payment method of the created invoice in system format Optional
taxrate
float The first level tax rate to apply to the invoice to override the system default Optional
taxrate2
float The second level tax rate to apply to the invoice to override the system default Optional
Response Parameters
result
string The result of the operation: success or error
invoiceid
int The ID of the newly created invoice
status
string The status of the newly created invoice
*/
func (s *BillingService) CreateInvoice(userID int, invoice CreateInvoiceRequest) (int, *Response, error) {
//a := new(BillingItem)
userid := fmt.Sprintf("%d", userID)
invoiceParams := map[string]string{"userid": userid}
switch invoice.Status {
case "Draft", "Unpaid", "Paid":
invoiceParams["status"] = invoice.Status
default:
return 0, &Response{}, fmt.Errorf("unsupported status value: %s", invoice.Status)
}
invoiceParams["sendinvoice"] = FormatBool(invoice.SendInvoice)
invoiceParams["autoapplycredit"] = FormatBool(invoice.AutoApplyCredit)
layout := "2006-01-02"
if !invoice.Date.IsZero() {
invoiceParams["date"] = invoice.Date.Format(layout)
}
if !invoice.DueDate.IsZero() {
invoiceParams["duedate"] = invoice.DueDate.Format(layout)
}
if len(invoice.Notes) > 0 {
invoiceParams["notes"] = invoice.Notes
}
if len(invoice.LineItems) < 1 {
return 0, &Response{}, fmt.Errorf("No Line items for invoice found")
}
li := lineItemstoParams(invoice.LineItems)
for k, v := range li {
invoiceParams[k] = v
}
resp, err := do(s.client, Params{parms: invoiceParams, u: "CreateInvoice"}, nil)
if err != nil {
return 0, resp, err
}
// WHMCS returns a error sometimes that is not in JSON !
r := strings.Replace(resp.Body, `<div class="alert alert-error">Module credit_purchase_improvement: Module error occured. Please contact with support.</div>`, ``, -1)
ir := InvoiceReply{}
err = json.Unmarshal([]byte(r), &ir)
return ir.InvoiceID, resp, err
}
func lineItemstoParams(items []InvoiceLineItems) map[string]string {
lineItems := map[string]string{}
for _, li := range items {
lineItems[fmt.Sprintf("itemdescription%d", li.ItemOrder)] = li.ItemDescription
lineItems[fmt.Sprintf("itemamount%d", li.ItemOrder)] = fmt.Sprintf("%.2f", li.ItemAmount)
lineItems[fmt.Sprintf("itemtaxed%d", li.ItemOrder)] = FormatBool(li.ItemTaxed)
}
return lineItems
}
// CreateInvoiceRequest the new invoice to be created for a client
type CreateInvoiceRequest struct {
// The ID of the client to charge
Status string // The status of the invoice being created Paid,Unpaid,Draft
SendInvoice bool // Should the Invoice Created Email be sent to the client
Date time.Time // The date that the invoice should show as created
DueDate time.Time // The due date of the newly created invoice
Notes string // The notes to appear on the created invoice
AutoApplyCredit bool // Should credit on the client account be automatically applied to the invoice
LineItems []InvoiceLineItems // Invoice Line Items
}
// InvoiceLineItems the new invoice to be created for a client
type InvoiceLineItems struct {
ItemOrder int // The Order to be show on the invoice
ItemDescription string // The line items description
ItemAmount float32 // The line items amount
ItemTaxed bool // The line items is taxed value
}
// InvoiceReply the status after creating or updating an invoice
type InvoiceReply struct {
InvoiceID int `json:"invoiceid"` // The ID of the invoice
Result string `json:"result"` // The result of the operation: success or error
Status string `json:"status"` // The status of the invoice
}