Skip to content

Commit

Permalink
Update branch from changes on main
Browse files Browse the repository at this point in the history
  • Loading branch information
wolveix committed Apr 5, 2024
1 parent 055f8a4 commit 47bae90
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 410 deletions.
21 changes: 0 additions & 21 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
// Copyright (c) 2018, Randy Westlund. All rights reserved.
// This code is under the BSD-2-Clause license.

/*
Package quickbooks provides access to Intuit's QuickBooks Online API.
NOTE: This library is very incomplete. I just implemented the minimum for my
use case. Pull requests welcome :)
// Do this after you go through the normal OAuth process.
var client = oauth2.NewClient(ctx, tokenSource)
// Initialize the client handle.
var qb = quickbooks.Client{
Client: client,
Endpoint: quickbooks.SandboxEndpoint,
RealmId: "some company account Id"'
}
// Make a request!
var companyInfo, err = qb.FindCompanyInfo()
*/
package quickbooks

import (
Expand Down Expand Up @@ -173,7 +153,6 @@ func (c *Client) req(method string, endpoint string, payloadData interface{}, re
time.Sleep(1 * time.Minute)
c.throttled = false
}(c)
break
default:
return parseFailure(resp)
}
Expand Down
162 changes: 162 additions & 0 deletions credit_memo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package quickbooks

import (
"encoding/json"
"errors"
"strconv"
)

type CreditMemo struct {
SyncToken string `json:",omitempty"`
DocNumber string
CustomMemo string `json:",omitempty"`
TxnDate *Date `json:",omitempty"`
TotalAmt json.Number `json:",omitempty"`
CustomRef *ReferenceType `json:",omitempty"`
Line []Line
CurrencyRef *ReferenceType `json:",omitempty"`
APAccountRef *ReferenceType `json:",omitempty"`
SalesTermRef *ReferenceType `json:",omitempty"`
LinkedTxn []LinkedTxn `json:",omitempty"`
TransactionLocationType string `json:",omitempty"`
DueDate Date `json:",omitempty"`
TxnTaxDetail *TxnTaxDetail `json:",omitempty"`
ExchangeRate json.Number `json:",omitempty"`
DepartmentRef *ReferenceType `json:",omitempty"`
IncludeInAnnualTPAR bool `json:",omitempty"`
HomeBalance json.Number `json:",omitempty"`
RecurDataRef *ReferenceType `json:",omitempty"`
Balance json.Number `json:",omitempty"`
Id string `json:",omitempty"`
MetaData MetaData `json:",omitempty"`
}

// CreateCreditMemo creates the given CreditMemo on the QuickBooks server, returning
// the resulting CreditMemo object.
func (c *Client) CreateCreditMemo(creditMemo *CreditMemo) (*CreditMemo, error) {
var resp struct {
CreditMemo CreditMemo
Time Date
}

if err := c.post("creditMemo", creditMemo, &resp, nil); err != nil {
return nil, err
}

return &resp.CreditMemo, nil
}

// DeleteCreditMemo deletes the given credit memo.
func (c *Client) DeleteCreditMemo(creditMemo *CreditMemo) error {
if creditMemo.Id == "" || creditMemo.SyncToken == "" {
return errors.New("missing id/sync token")
}

return c.post("creditMemo", creditMemo, nil, map[string]string{"operation": "delete"})
}

// FindCreditMemos retrieves the full list of credit memos from QuickBooks.
func (c *Client) FindCreditMemos() ([]CreditMemo, error) {
var resp struct {
QueryResponse struct {
CreditMemos []CreditMemo `json:"CreditMemo"`
MaxResults int
StartPosition int
TotalCount int
}
}

if err := c.query("SELECT COUNT(*) FROM CreditMemo", &resp); err != nil {
return nil, err
}

if resp.QueryResponse.TotalCount == 0 {
return nil, errors.New("no creditMemos could be found")
}

creditMemos := make([]CreditMemo, 0, resp.QueryResponse.TotalCount)

for i := 0; i < resp.QueryResponse.TotalCount; i += queryPageSize {
query := "SELECT * FROM CreditMemo ORDERBY Id STARTPOSITION " + strconv.Itoa(i+1) + " MAXRESULTS " + strconv.Itoa(queryPageSize)

if err := c.query(query, &resp); err != nil {
return nil, err
}

if resp.QueryResponse.CreditMemos == nil {
return nil, errors.New("no creditMemos could be found")
}

creditMemos = append(creditMemos, resp.QueryResponse.CreditMemos...)
}

return creditMemos, nil
}

// FindCreditMemoById retrieves the given credit memo from QuickBooks.
func (c *Client) FindCreditMemoById(id string) (*CreditMemo, error) {
var resp struct {
CreditMemo CreditMemo
Time Date
}

if err := c.get("creditMemo/"+id, &resp, nil); err != nil {
return nil, err
}

return &resp.CreditMemo, nil
}

// QueryCreditMemos accepts n SQL query and returns all credit memos found using it.
func (c *Client) QueryCreditMemos(query string) ([]CreditMemo, error) {
var resp struct {
QueryResponse struct {
CreditMemos []CreditMemo `json:"CreditMemo"`
StartPosition int
MaxResults int
}
}

if err := c.query(query, &resp); err != nil {
return nil, err
}

if resp.QueryResponse.CreditMemos == nil {
return nil, errors.New("could not find any creditMemos")
}

return resp.QueryResponse.CreditMemos, nil
}

// UpdateCreditMemo updates the given credit memo.
func (c *Client) UpdateCreditMemo(creditMemo *CreditMemo) (*CreditMemo, error) {
if creditMemo.Id == "" {
return nil, errors.New("missing creditMemo id")
}

existingCreditMemo, err := c.FindCreditMemoById(creditMemo.Id)
if err != nil {
return nil, err
}

creditMemo.SyncToken = existingCreditMemo.SyncToken

payload := struct {
*CreditMemo
Sparse bool `json:"sparse"`
}{
CreditMemo: creditMemo,
Sparse: true,
}

var creditMemoData struct {
CreditMemo CreditMemo
Time Date
}

if err = c.post("creditMemo", payload, &creditMemoData, nil); err != nil {
return nil, err
}

return &creditMemoData.CreditMemo, err
}
14 changes: 5 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
module github.com/rwestlund/quickbooks-go

go 1.18
go 1.20

require (
github.com/stretchr/testify v1.6.1
golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5
github.com/stretchr/testify v1.9.0
golang.org/x/oauth2 v0.19.0
gopkg.in/guregu/null.v4 v4.0.0
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/golang/protobuf v1.4.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 47bae90

Please sign in to comment.