Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reports #259

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/quickbooks-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
require 'quickbooks/model/customer_change'
require 'quickbooks/model/vendor_change'
require 'quickbooks/model/item_change'
require 'quickbooks/model/reports'
require 'quickbooks/model/report'
require 'quickbooks/model/credit_memo_change'
require 'quickbooks/model/payment_change'

Expand Down
52 changes: 52 additions & 0 deletions lib/quickbooks/model/report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module Quickbooks
module Model
class Report < BaseModel

attr_accessor :xml

def all_rows
@all_rows ||= xml.css("ColData:first-child").map {|node| parse_row(node.parent) }
end

def columns
@columns ||= begin
nodes = xml.css('Column')
nodes.map do |node|
# There is also a ColType field, but it does not seem valuable to capture
node.at('ColTitle').content
end
end
end

def find_row(label)
all_rows.find {|r| r[0] == label }
end

private

# Parses the given row:
# <Row type="Data">
# <ColData value="Checking" id="35"/>
# <ColData value="1201.00"/>
# <ColData value="200.50"/>
# </Row>
#
# To:
# ['Checking', BigDecimal(1201.00), BigDecimal(200.50)]
def parse_row(row_node)
row_node.elements.map.with_index do |el, i|
value = el.attr('value')

if i.zero? # Return the first column as a string, its the label.
value
elsif value.blank?
nil
else
BigDecimal(value)
end
end
end

end
end
end
17 changes: 0 additions & 17 deletions lib/quickbooks/model/reports.rb

This file was deleted.

8 changes: 2 additions & 6 deletions lib/quickbooks/service/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,9 @@ def parse_collection(response, model)
collection.count = xml.xpath(path_to_nodes).count
if collection.count > 0
xml.xpath(path_to_nodes).each do |xa|
entry = model.from_xml(xa)
addition = xml.xpath(path_to_nodes)[0].xpath("//xmlns:Currency").children.to_s if "#{model::XML_NODE}" == "Reports"
entry.currency = addition if "#{model::XML_NODE}" == "Reports"
collection.body = response.body if "#{model::XML_NODE}" == "Reports"
results << entry
results << model.from_xml(xa)
end
end
end

collection.entries = results
rescue => ex
Expand Down
13 changes: 4 additions & 9 deletions lib/quickbooks/service/reports.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Docs: https://developer.intuit.com/docs/0100_accounting/0400_references/reports
module Quickbooks
module Service
class Reports < BaseService
Expand All @@ -16,21 +17,15 @@ def url_for_query(which_report = 'BalanceSheet', date_macro = 'This Fiscal Year-
end
end

def fetch_collection(model, date_macro, object_query, options = {})
response = do_http_get(url_for_query(object_query, date_macro, options))

parse_collection(response, model)

end

def query(object_query = 'BalanceSheet', date_macro = 'This Fiscal Year-to-date', options = {})
fetch_collection(model, date_macro , object_query, options)
do_http_get(url_for_query(object_query, date_macro, options))
model.new(:xml => @last_response_xml)
end

private

def model
Quickbooks::Model::Reports
Quickbooks::Model::Report
end

end
Expand Down
Loading