Skip to content

Instantly share code, notes, and snippets.

@rposborne
Created August 20, 2012 21:14
Show Gist options
  • Save rposborne/3407957 to your computer and use it in GitHub Desktop.
Save rposborne/3407957 to your computer and use it in GitHub Desktop.
Invoices to QBXML
def to_quickbooks_hash
hash = {"invoice_add"=>{
"customer_ref"=>{
"list_id"=> self.order.account.list_id
#"full_name"=> self.order.account.company_name #"Abercrombie, Kristy" #
},
"txn_date"=>Date.today.xmlschema,
"bill_address"=>{
"addr1"=>self.order.address(:billing, :name),
"addr2"=>self.order.address(:billing, :street_1),
"addr3"=>self.order.address(:billing, :street_2),
"city"=>self.order.address(:billing, :city),
"state"=>self.order.address(:billing, :state),
"postal_code"=>self.order.address(:billing, :zip),
"country"=>self.order.address(:billing, :country),
},
"ship_address"=>{
"addr1"=>self.order.address(:shipping, :name),
"addr2"=>self.order.address(:shipping, :street_1),
"addr3"=>self.order.address(:shipping, :street_2),
"city"=>self.order.address(:shipping, :city),
"state"=>self.order.address(:shipping, :state),
"postal_code"=>self.order.address(:shipping, :zip),
"country"=>self.order.address(:shipping, :country),
},
"is_pending"=>true,
"po_number"=>self.order.customer_po,
"due_date"=>self.order.date_due.nil? ? Date.today.xmlschema : self.order.date_due.xmlschema,
#"sales_rep_ref"=>{
# "list_id"=>"String",
# "full_name"=>"#{self.order.user.profile.salesperson_intials}"
#},
#}"fob"=>"String",
"ship_date"=> self.order.ship_date.nil? ? Date.today.xmlschema : self.order.ship_date.xmlschema,
#{}"ship_method_ref"=>{
#{}"full_name"=>"#{!self.order.shipping_method.nil? ? self.order.shipping_method.name : nil }"
#},
#"item_sales_tax_ref"=>{
# "list_id"=>"String",
# "full_name"=>"String"
# },
"memo"=>"Tracking ##{self.order.id}"
}}
if !self.line_items.empty?
hash["invoice_add"]["invoice_line_add"] = {
"desc"=> self.line_items.first.description,
"quantity"=> self.line_items.first.quantity_shipped,
#"unit_of_measure"=>self.line_items.first.unit_measures unless self.line_items.empty? ,
"rate"=>self.line_items.first.unit_prices
}
end
#line_items_array = []
#self.line_items.each do |item|
#line_items_array << item.to_quickbooks_hash
#end
#hash["invoice_add"]["invoice_line_add_list"] = line_items_array
#puts hash
return hash
end
def self.add_invoices
requests = []
## Sync Proc
sync_invoice = Proc.new do |qbxml|
request_id = qbxml["xml_attributes"]["requestID"]
quickbooks_invoice= qbxml["invoice_ret"]
inv = self.find(request_id)
if !inv
return
end
inv.update_attributes({
:transaction_at=> quickbooks_invoice["time_created"] ,
:ref_number => quickbooks_invoice["ref_number"].to_i ,
:list_id => quickbooks_invoice["txn_id"] ,
:terms => quickbooks_invoice["terms_ref"]["full_name"] ,
:synced_at => Time.now ,
:sync_status => "success",
:pending_sync => false
})
end
api = Quickbooks::API[:qb]
self.pending_sync.includes([:line_items, {:order => [:shipping_method, {:billing_contact => :address},{:user => :profile},{:shipping_contact => :address}, {:account => {:primary_contact => :address}}]}]).limit(2).each do |invoice|
if invoice.order.account.list_id
o = api.hash_to_obj(invoice.to_quickbooks_hash)
o.qbxml_msgs_rq.xml_attributes = {"onError"=>"continueOnError" }
o.qbxml_msgs_rq.invoice_add_rq.xml_attributes = {"requestID" => "#{invoice.id}"}
puts qbxml = o.to_qbxml.to_s
requests <<[ qbxml, sync_invoice]
end
end
return requests
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment