Skip to content

Commit bc3dacb

Browse files
author
Chris Joyce
committed
First commit
0 parents  commit bc3dacb

22 files changed

+1631
-0
lines changed

CreateInvoice.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package whmcsgo
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"strings"
7+
"time"
8+
)
9+
10+
/*
11+
CreateInvoice Create an invoice using the provided parameters.
12+
13+
WHMCS API docs
14+
15+
https://developers.whmcs.com/api-reference/getclients/
16+
17+
Request Parameters
18+
19+
draft
20+
bool Should the invoice be created in draft status (No need to pass $status also) Optional
21+
paymentmethod
22+
string The payment method of the created invoice in system format Optional
23+
taxrate
24+
float The first level tax rate to apply to the invoice to override the system default Optional
25+
taxrate2
26+
float The second level tax rate to apply to the invoice to override the system default Optional
27+
28+
Response Parameters
29+
30+
result
31+
string The result of the operation: success or error
32+
invoiceid
33+
int The ID of the newly created invoice
34+
status
35+
string The status of the newly created invoice
36+
37+
*/
38+
func (s *BillingService) CreateInvoice(userID int, invoice CreateInvoiceRequest) (int, *Response, error) {
39+
//a := new(BillingItem)
40+
41+
userid := fmt.Sprintf("%d", userID)
42+
invoiceParams := map[string]string{"userid": userid}
43+
44+
switch invoice.Status {
45+
case "Draft", "Unpaid", "Paid":
46+
invoiceParams["status"] = invoice.Status
47+
default:
48+
return 0, &Response{}, fmt.Errorf("unsupported status value: %s", invoice.Status)
49+
}
50+
51+
invoiceParams["sendinvoice"] = FormatBool(invoice.SendInvoice)
52+
invoiceParams["autoapplycredit"] = FormatBool(invoice.AutoApplyCredit)
53+
54+
layout := "2006-01-02"
55+
56+
if !invoice.Date.IsZero() {
57+
invoiceParams["date"] = invoice.Date.Format(layout)
58+
}
59+
60+
if !invoice.DueDate.IsZero() {
61+
invoiceParams["duedate"] = invoice.DueDate.Format(layout)
62+
}
63+
64+
if len(invoice.Notes) > 0 {
65+
invoiceParams["notes"] = invoice.Notes
66+
}
67+
68+
if len(invoice.LineItems) < 1 {
69+
return 0, &Response{}, fmt.Errorf("No Line items for invoice found")
70+
}
71+
72+
for _, li := range invoice.LineItems {
73+
// fmt.Println(li)
74+
75+
invoiceParams[fmt.Sprintf("itemdescription%d", li.ItemOrder)] = li.ItemDescription
76+
invoiceParams[fmt.Sprintf("itemamount%d", li.ItemOrder)] = fmt.Sprintf("%.2f", li.ItemAmount)
77+
invoiceParams[fmt.Sprintf("itemtaxed%d", li.ItemOrder)] = FormatBool(li.ItemTaxed)
78+
79+
}
80+
//
81+
82+
//
83+
fmt.Println(invoiceParams)
84+
85+
resp, err := do(s.client, Params{parms: invoiceParams, u: "CreateInvoice"}, nil)
86+
if err != nil {
87+
return 0, resp, err
88+
}
89+
90+
// WHMCS returns a error sometimes that is not in JSON !
91+
r := strings.Replace(resp.Body, `<div class="alert alert-error">Module credit_purchase_improvement: Module error occured. Please contact with support.</div>`, ``, -1)
92+
ir := createInvoiceReply{}
93+
json.Unmarshal([]byte(r), &ir)
94+
95+
return ir.InvoiceID, resp, err
96+
97+
}
98+
99+
// CreateInvoiceRequest the new invoice to be created for a client
100+
type CreateInvoiceRequest struct {
101+
// The ID of the client to charge
102+
Status string // The status of the invoice being created Paid,Unpaid,Draft
103+
SendInvoice bool // Should the Invoice Created Email be sent to the client
104+
Date time.Time // The date that the invoice should show as created
105+
DueDate time.Time // The due date of the newly created invoice
106+
Notes string // The notes to appear on the created invoice
107+
AutoApplyCredit bool // Should credit on the client account be automatically applied to the invoice
108+
LineItems []CreateInvoiceItems // Invoice Line Items
109+
}
110+
111+
// CreateInvoiceItems the new invoice to be created for a client
112+
type CreateInvoiceItems struct {
113+
ItemOrder int // The Order to be show on the invoice
114+
ItemDescription string // The line items description
115+
ItemAmount float32 // The line items amount
116+
ItemTaxed bool // The line items is taxed value
117+
118+
}
119+
120+
type createInvoiceReply struct {
121+
InvoiceID int `json:"invoiceid"`
122+
Result string `json:"result"`
123+
Status string `json:"status"`
124+
}

0 commit comments

Comments
 (0)