Quickbooks is a software which is mainly used to organize the expenses of a small scale or medium scaled business.
- Install gems omniauth and omniauth-quickbooks to authenticate with Quickbooks.
- By default, the gem quickbooks-ruby will be in production mode but you can run it in other mode (development or testing).
- Create a rails initializer and set the OAUTHCONSUMERKEY and OAUTHCONSUMERSECRET properly and set the callback in the routes in order to inform rails about the request.
- You can set the OAuth access credentials with four fields accesstoken, accesssecret, companyid, tokenexpires_at to avoid connecting to Quickbooks often.
How to create/update users
After, all the important and basic setup is done now its time to learn how to generate invoices for the user. Without an user generating invoices makes no sense. Hence, the first thing is to know the user. There are two possible cases for this:
- If the user does not exist in Quickbooks, create the user and generate the invoice.
- If the user already exists in Quickbooks, update the user and generate the invoice.
If the user does not exist then instantiate a Service object on the Customer and create it.
... def build_user_information service = Quickbooks::Service::Customer.new service.access_token = access_token service.company_id = company_id quickbooks_customer = Quickbooks::Model::Customer.new quickbooks_customer = set_user_information quickbooks_customer service.create(quickbooks_customer) end def set_user_information quickbooks_customer # Name quickbooks_customer.given_name = quickbooks_customer.display_name = quickbooks_customer.print_on_check_name = quickbooks_customer.fully_qualified_name = "First Last" # Email quickbooks_email = Quickbooks::Model::EmailAddress.new quickbooks_email.address = "email@example.com" quickbooks_customer.primary_email_address = quickbooks_email # Phone Number quickbooks_phone_number = Quickbooks::Model::TelephoneNumber.new quickbooks_phone_number.free_form_number = 9876543210 quickbooks_customer.primary_phone = quickbooks_phone_number # Address quickbooks_address = Quickbooks::Model::PhysicalAddress.new quickbooks_address.line1 = "line1" quickbooks_address.line2 = "line2" quickbooks_address.city = "city" quickbooks_address.country = "country" quickbooks_customer.billing_address = quickbooks_address quickbooks_customer end
The above piece of code has two methods,
- The first method is to create the user in the Quickbooks.
- The second method is to set the data using an object of Quickbooks's customer.
service.create(quickbooks_customer) will create a new Quickbooks customer with an id as its primary key, say 21 in this case.
If the user already exists in Quickbooks then get the customer and update it.
... def update_user_information service = Quickbooks::Service::Customer.new service.access_token = access_token service.company_id = company_id quickbooks_customer = service.fetch_by_id(21) quickbooks_customer = set_user_information quickbooks_customer service.update(quickbooks_customer) end
updateuserinformation is to update the data of the existing customer.
There are couples of things that you can play with the service object
users = service.all
users will be an array with all the objects of the service (customer in our case).
users = service.query()
It will be a SQL like structure to retrieve objects.
In the next post there will be a procedure to generate the invoices for the created customer.
Read part 2 of the blog which talks about managing invoices in Quickbooks.