QuickBooks Rubygem Documentation

Example Code

First include the gem
  require 'rubygems'
  require 'quickbooks'

  # The following examples will also assume including the days_and_times gem:
  require 'days_and_times'
        
Next, set up your connection method and specify your SDK version (optional, if your connection is a two-way communication with QuickBooks)
# Example A: Connect to the local desktop Quickbooks.
Quickbooks.use_adapter :ole


Quickbooks.qbxml_version = "4.0"
# -- OR skip this step and the gem will automatically ask
#    Quickbooks for the latest supported version.

# Example B: Using the spew adapter, which just returns
#            the generated QBXML command string.
Quickbooks.use_adapter :spew
Quickbooks.qbxml_version = "6.0" # <-- or whatever version you wish

# Example C: Using the HTTP adapter, which will communicate over
#            the network via HTTP to the
#            QuickBooks HTTP Bridge service on the other end
Quickbooks.use_adapter :http, 'your-IP-or-dynamic-ip-name'

# Note: You can also ask for the versions
#       available in the QuickBooks you're connected to:
Quickbooks.connection.qbxml_versions
        
Find a Customer by name
  QB::Customer.first(:FullName => "Joni Gortella")
        
Create a new Customer
customer = QB::Customer.new(:Name => "Mack Froth",
                            :FirstName => "Mack",
                            :LastName => "Froth")
customer.valid? # => true; Customer requires only Name.
customer.save
        
Find all Customers created today (note that QuickBooks can't search by datetime, only by date)
  customers = QB::Customer.all(:FromModifiedDate => Time.today)
        
Find all Invoices for a specific Customer
  # Since you can pass in several OwnerIDs in the API,
  # always pass this argument in an array.
  # In Invoices, OwnerID refers to the Customer.

  QB::Invoice.all(:OwnerID => [customer[:ListID]])
        
Create a new SalesOrder
  sales_order = QB::SalesOrder.new(
            :CustomerRef => {:FullName => 'Mack Froth'})

  sales_order[:SalesOrderLine] << {
            :ItemRef => {:ListID => '123-456-7890'},
            :Desc => "Specialized Gew-Gaw",
            :Quantity => 2,
            :Amount => 22.99 }

  sales_order.save
        
Create a TimeTracking entry
  QB::TimeTracking.create(
    :CustomerRef => {:FullName => 'Smack Industries'},
    :EntityRef => {:FullName => 'Mack Froth'},
    :Duration => '1H30M',
    :BillableStatus => 'Billable' )
        
Find out what parameters are available for any given query
  >> QB::Invoice.query_attributes
  => ["TxnID", "RefNumber", "RefNumberCaseSensitive", "MaxReturned", "ModifiedDateRangeFilter",
      "TxnDateRangeFilter", "EntityFilter", "AccountFilter", "RefNumberFilter", "RefNumberRangeFilter",
      "PaidStatus", "IncludeLineItems", "IncludeLinkedTxns", "IncludeRetElement", "OwnerID"]

  >> QB::ItemInventory.query_attributes
  => ["ListID", "FullName", "MaxReturned", "ActiveStatus", "FromModifiedDate", "ToModifiedDate",
      "NameFilter", "NameRangeFilter", "IncludeRetElement", "OwnerID"]

  >> QB::Customer.query_attributes
  => ["ListID", "FullName", "MaxReturned", "ActiveStatus", "FromModifiedDate", "ToModifiedDate",
      "NameFilter", "NameRangeFilter", "TotalBalanceFilter", "IncludeRetElement", "OwnerID"]
        

QuickBooks is a registered trademark of Intuit, Inc.

 


Usage Tutorial

Runtime Setup

Connect to QuickBooks on the same machine:
Quickbooks.use_adapter :ole, "app-name"

Connect to QuickBooks using the HTTP Connector:
Quickbooks.use_adapter :http, "ip-address", "app-name", "password"
For HTTPS, simply use :https instead of :http.

NOTE: If you are using the desktop_connector.exe, it listens on port 56225

Other miscellaneous adapters available:
:spew (default) - just returns the generated QBXML instead of communicating it to QuickBooks.
:test - allows you to create mock responses to emulate communication with QuickBooks, for automated test purposes.

Set the QBXML version manually (saves a couple seconds vs negotiating with QuickBooks to select the latest version both support):
Quickbooks.qbxml_version = "6.0"

Canadian versions (as of this writing) use something like:
Quickbooks.qbxml_version = "CA3.0"

Getting Objects from QuickBooks

Get all active Customers:
customers = QB::Customer.all

Get all inactive Customers:
inactive = QB::Customer.all(:ActiveStatus => "InactiveOnly")

Get a Customer by exact name:
todd = QB::Customer.first(:FullName => ["Todd Skeet"])

Get all Customers whose names start with "Ti":
ti_x = QB::Customer.all(:NameFilter => {:MatchCriterion => "StartsWith", :Name => "Ti"})

Get the next 7 Customers after (and including) "Sally Moore":
after_sally = QB::Customer.all(:MaxReturned => 7,
                      :NameRangeFilter => {:FromName => "Sally Moore"})

Get all unpaid Invoices for sally:
sally_invoices = QB::Invoice.all(:EntityFilter => after_sally[0].to_ref,
                      :PaidStatus => "NotPaidOnly")

Get all unpaid Invoices for sally, referenced by exact name:
sally_invoices = QB::Invoice.all(:EntityFilter => {:FullName => "Sally Moore"},
                      :PaidStatus => "NotPaidOnly")

Get an Inventory Item named "Small Baseball":
play_ball = QB::ItemInventory.first(:FullName => ["Small Baseball"])

Create New Objects in QuickBooks

Create a new Customer:
jerry = QB::Customer.new(:Name => "Jerry Kreid",
                      :FirstName => "Jerry", :LastName => "Kreid")
jerry.save
  

Create a new Inventory Item:
(prerequisites: know what your accounts are named)
play_ball = QB::ItemInventory.new(
                   :Name => "Small Baseball",
                   :SalesPrice => "6.49",
                   :IncomeAccountRef => {:FullName => "Sales"},
                   :AssetAccountRef => {:FullName => "Inventory Asset"},
                   :COGSAccountRef => {:FullName => "Cost of Goods Sold"})
play_ball.save
  

Create a new Sales Order, in which Jerry Kreid orders a Small Baseball:
(prerequisites: customer, item)
sale = QB::SalesOrder.new(
             :CustomerRef => jerry.to_ref,
             :SalesOrderLine => [{
                     :ItemRef => play_ball.to_ref,
                     :Quantity => 2,
                     :Rate => play_ball[:SalesPrice],
                     :Desc => play_ball[:FullName]
             }])
sale.save

Notice above that the SalesOrderLine property is singular in name, but since you can have several values, you MUST assign an array to it. You can also append more SalesOrderLine items to it later:
sale[:SalesOrderLine] << QB::SalesOrderLine.new(
                 :ItemRef => {:FullName => "Small Baseball"},
                 :Quantity => 2,
                 :Rate => play_ball[:SalesPrice],
                 :Desc => play_ball[:FullName])

Create an Invoice:
(prerequisites: customer, item)
invoice = QB::Invoice.new(
                :CustomerRef => jerry.to_ref,
                :InvoiceLine => [{
                         :ItemRef => play_ball.to_ref,
                         :Desc => play_ball[:Name],
                         :Quantity => 2,
                         :Rate => play_ball[:SalesPrice]
                }])
invoice.save

Data Extensions

There are a couple of methods added to assist you in working with Extended Data on any object:

Causes extended data under the default owner '0' to be retrieved with the object. Change true to a value if you need a different owner:
QB::Customer.first(:IncludeExtData => true)

If you already have an object loaded, and you decide you need the extended data, you can load it in-place using load_extended_data. The default owner is '0' as usual, but you can add an optional argument of a different owner.
customer = QB::Customer.first
customer.load_extended_data

Use this method to change extended data. You'll need to know the key of the data, and include the value to set. Add an extra argument for the owner if you need to specify a non-default owner.
customer.update_extended_data(key, value)


List of QuickBooks Objects

You can access any of the following object types as QB::ObjectType -- for example, QB::Account or QB::AgingReport.

ARRefundCreditCard
Account
AccountTaxLineInfo
AppliedToTxn

BillPaymentCheck
BillPaymentCreditCard
Bill
BillToPay
BillingRatePerItem
BillingRate
BuildAssembly

CashBackInfo
Charge
Check
Class
CompanyActivity
Company
ComponentItemLine
CreditCardCharge
CreditCardCredit
CreditMemoLineGroup
CreditMemoLine
CreditMemo
CustomerMsg
Customer
CustomerType

DataEventRecoveryInfo
DataEventSubscription
DataExtDefDel
DataExtDef
DataExtDel
DataExt
DateDrivenTerms
DepositLine
Deposit
DiscountLine

Employee
EstimateLineGroup
EstimateLine
Estimate
ExpenseLine

Host

InventoryAdjustmentLine
InventoryAdjustment
InvoiceLineGroup
InvoiceLine
Invoice
ItemAssembliesCanBuild
ItemDiscount
ItemFixedAsset
ItemGroupLine
ItemGroup
ItemInventoryAssembly
ItemInventory
ItemLine
ItemNonInventory

ItemOtherCharge

ItemPayment

ItemReceipt

ItemSalesTaxGroup

ItemSalesTax

ItemService

ItemSubtotal


JobType

JournalEntry


ListDeleted


OtherName


PaymentMethod

PayrollItemNonWage

PayrollItemWage

Preferences

PriceLevelPerItem

PriceLevel

PurchaseOrderLineGroup

PurchaseOrderLine

PurchaseOrder


ReceivePayment

ReceivePaymentToDeposit

RefundAppliedToTxn


SalesOrderLineGroup

SalesOrderLine

SalesOrder

SalesReceiptLineGroup

SalesReceiptLine

SalesReceipt

SalesRep

SalesTaxCode

SalesTaxLine

SalesTaxPaymentCheckLine

SalesTaxPaymentCheck

ShipMethod

ShippingLine

StandardTerms


TaxLineInfo

Template

TimeTracking

ToDo

Transaction

TxnDeleted


UIEventSubscription

UIExtensionSubscription

UnitOfMeasureSet


VehicleMileage

Vehicle

VendorCredit

Vendor

VendorType


WorkersCompCode


Querying QuickBooks Data
Many of the above Object types can be queried for existing records. You can find out what attributes are available for querying by running the #query_attributes method on the class constant -- for example:
QB::Account.query_attributes

To get more specific (but more cryptic) information about the entire structure of a possible query, study the output of #query_xsd, for example:
QB::Account.query_xsd

To explore the structure that defines creating a new object, inspect the output of #write_xsd, for example:
QB::Invoice.write_xsd

And for the modifiable properties, inspect #read_write_xsd, for example:
QB::SalesReceipt.read_write_xsd

This xsd information is retrieved from the SDK specs when the gem is first initialized. There are too many of these specifications and too detailed for me to list them all here.

You don't have to remember the previous few method names. Just remember this one big helpful method:
puts QB::Customer.help
This will tell you the methods you can run to get further help about the Customer model. Of course, replace the word Customer with any other model you want to get help on.


QuickBooks SDK Documentation

At this link you can find the official documentation for the QuickBooks SDK. Hopefully this gem is easy enough to use that you don't have to revert to this monstrous and cryptic documentation, but if you do want to get more familiar with how the SDK works underneath everything, go ahead and try to tackle this thing:
http://developer.intuit.com/qbSDK-current/doc/html/wwhelp/wwhimpl/js/html/wwhelp.htm