Quickbooks SDK Part 2
Quickbooks Communication
I know the previous page was quite a few lines but very little code. Trust me, this will make things much easier and faster once we dig back in to this next section. Let’s now focus on communicating with Quickbooks itself. This is the fun part. We’re now going to create a new class and file called Quickbooks.cs. This class will handle all communications to your Quickbooks company file. We’re also going to make all of the methods, well, most of the methods in this class static. This will allow us to call this code anywhere else in our code as long as we keep the namespaces the same. This should be a huge hint towards “integration” between Quickbooks and Magento. Let us not stray! Code now, think later! Inside our Quickbooks.cs file add these lines of code:
class Quickbooks
{
private static SessionManager sessionMgr = SessionManager.getInstance(); // establish connection to Quickbooks
public static IInvoiceRet addInvoice(IOrder order)
{
IMsgSetRequest msgSet = sessionMgr.getMsgSetRequest(); // storage for messages to send to Quickbooks
IInvoiceAdd invoice = msgSet.AppendInvoiceAddRq(); // creates a request to create a new invoice
//Accounts Receivable
invoice.ARAccountRef.ListID.SetValue("00000...."); // must be a valid ListID in the correct format
//Billing Address - Here's where those earlier classes do their magic.
invoice.BillAddress.Addr1.SetValue(order.billing_address);
invoice.BillAddress.City.SetValue(order.billing_city);
invoice.BillAddress.State.SetValue(order.billing_state);
invoice.BillAddress.PostalCode.SetValue(order.billing_zip);
//Shipping Address
invoice.ShipAddress.Addr1.SetValue(order.shipping_address);
invoice.ShipAddress.City.SetValue(order.shipping_city);
invoice.ShipAddress.State.SetValue(order.shipping_state);
invoice.ShipAddress.PostalCode.SetValue(order.shipping_zip);
for (int i = 0; i < order.order_items.Count; i++) // loops through items in order and adds them to the invoice request
{
IORInvoiceLineAdd invoiceLine = invoice.ORInvoiceLineAddList.Append(); // creates line item in Quickbooks invoice
if (isInventoryPart(order.order_items[i])) // These functions will be documented in part 3 of this tutorial.
{
IItemInventoryRet existingItem = getInventoryPart(order.order_items[i]); // Does item already exist in Quickbooks?
if (existingItem != null) // Yes it does. Let's pull information from it.
{
invoiceLine.InvoiceLineAdd.ItemRef.ListID.SetValue(existingItem.ListID.GetValue());
invoiceLine.InvoiceLineAdd.Quantity.SetValue(order.order_items[i].quantity);
var amount = Math.Round(order.order_items[i].price * order.order_items[i].quantity, 2);
invoiceLine.InvoiceLineAdd.Amount.SetValue((double)amount);
}
else // No, it doesn't. Let's create it and add it to Quickbooks.
{
addInventoryPart(order.order_items[i]);
IItemInventoryRet newItem = getInventoryPart(order.order_items[i]);
invoiceLine.InvoiceLineAdd.ItemRef.ListID.SetValue(newItem.ListID.GetValue());
invoiceLine.InvoiceLineAdd.Quantity.SetValue(order.order_items[i].quantity);
var amount = Math.Round(order.order_items[i].price * order.order_items[i].quantity, 2);
invoiceLine.InvoiceLineAdd.Amount.SetValue((double)amount);
}
}
else // Proceeding code same as above except for being non-inventory part.
{
IItemNonInventoryRet existingItem = getNonInventoryPart(order.order_items[i]);
if (existingItem != null)
{
invoiceLine.InvoiceLineAdd.ItemRef.ListID.SetValue(existingItem.ListID.GetValue());
invoiceLine.InvoiceLineAdd.Quantity.SetValue(order.order_items[i].quantity);
var amount = Math.Round(order.order_items[i].price * order.order_items[i].quantity, 2);
invoiceLine.InvoiceLineAdd.Amount.SetValue((double)amount);
}
else
{
addNonInventoryPart(order.order_items[i]);
IItemNonInventoryRet newItem = getNonInventoryPart(order.order_items[i]);
invoiceLine.InvoiceLineAdd.ItemRef.ListID.SetValue(newItem.ListID.GetValue());
invoiceLine.InvoiceLineAdd.Quantity.SetValue(order.order_items[i].quantity);
var amount = Math.Round(order.order_items[i].price * order.order_items[i].quantity, 2);
invoiceLine.InvoiceLineAdd.Amount.SetValue((double)amount);
}
}
}
//Shipping Charge
IORInvoiceLineAdd shippingLine = invoice.ORInvoiceLineAddList.Append();
IItemOtherChargeRet shippingCharge = getShippingCharge();
shippingLine.InvoiceLineAdd.ItemRef.ListID.SetValue(shippingCharge.ListID.GetValue());
shippingLine.InvoiceLineAdd.Quantity.SetValue(1);
shippingLine.InvoiceLineAdd.Amount.SetValue(order.shipping_charge);
//Order Number
invoice.RefNumber.SetValue(order.order_number);
//Order Date
invoice.TxnDate.SetValue(order.order_date);
//Customer - Again, some of these functions will be documented later in part 3 of this tutorial.
if (getCustomer(order) == null) // If customer doesn't exist, create one in Quickbooks.
{
ICustomerRet newCustomer = addCustomer(order);
if (newCustomer != null)
{
invoice.CustomerRef.ListID.SetValue(newCustomer.ListID.GetValue());
}
else
{
// Exception
}
}
else
{
ICustomerRet customer = getCustomer(order);
invoice.CustomerRef.ListID.SetValue(customer.ListID.GetValue());
}
if (sessionMgr.doRequests(ref msgSet)) // Send all the information in message set to Quickbooks for processing.
{
MessageBox.Show("Error creating invoice...");
return null;
}
IResponseList responseList = (IResponseList)sessionMgr.getResponseList(); // Grabs all responses from processing the message set.
for (int i = 0; i < responseList.Count; i++)
{
IResponse response = responseList.GetAt(i);
if (response.StatusCode >= 0)
{
if (response.Detail != null)
{
ENResponseType responseType = (ENResponseType)response.Type.GetValue();
if (responseType == ENResponseType.rtInvoiceAddRs)
{
return (IInvoiceRet)response.Detail;
}
}
}
}
return null; // Remember to return null here because it DOES serve a function in relation to the rest program.
}
If/when you download the Quickbooks SDK, it does provide documentation of some of the code above if you wish to look into it further. I hope that doesn’t make your brain explode. You must think of Quickbooks in terms of a messaging system not so different than email. Quickbooks sits in the background waiting for “messages” from the message set, processes those messages, then sends back a response. If you can remember that simple caveat, understanding of the code will come just from reading it.
Thanks for staying with me so far. More to come in part 3 in a few days!
Trackbacks / Pingbacks