Class: Effective::QbApi
- Inherits:
-
Object
- Object
- Effective::QbApi
- Defined in:
- app/models/effective/qb_api.rb
Instance Attribute Summary collapse
-
#realm ⇒ Object
Returns the value of attribute realm.
Instance Method Summary collapse
- #accounts ⇒ Object
-
#accounts_collection ⇒ Object
Only accounts we can use for the Deposit to Account setting.
- #api_error ⇒ Object
- #app_url ⇒ Object
- #build_address(address) ⇒ Object
-
#company_info ⇒ Object
Singular.
- #create_customer(order:) ⇒ Object
- #create_sales_receipt(sales_receipt:) ⇒ Object
- #customer_url(obj) ⇒ Object
- #delete_customer(customer:) ⇒ Object
- #find_customer(order:) ⇒ Object
- #find_or_create_customer(order:) ⇒ Object
- #find_sales_receipt(id:) ⇒ Object
-
#initialize(realm:) ⇒ QbApi
constructor
A new instance of QbApi.
- #item_html(item) ⇒ Object
-
#item_names ⇒ Object
Array of strings.
- #items ⇒ Object
- #items_collection ⇒ Object
- #payment_methods ⇒ Object
- #payment_methods_collection ⇒ Object
- #price_to_amount(price) ⇒ Object
- #sales_receipt_url(obj) ⇒ Object
- #tax_codes ⇒ Object
- #tax_rates ⇒ Object
-
#taxes_collection ⇒ Object
Returns a Hash of BigDecimal.to_s String Tax Rate => TaxCode Object { ‘0.0’ => ‘Quickbooks::Model::TaxCode(Exempt)’, ‘5.0’ => ‘Quickbooks::Model::TaxCode(GST)’ }.
- #with_service(name, &block) ⇒ Object
Constructor Details
Instance Attribute Details
#realm ⇒ Object
Returns the value of attribute realm.
6 7 8 |
# File 'app/models/effective/qb_api.rb', line 6 def realm @realm end |
Instance Method Details
#accounts ⇒ Object
57 58 59 |
# File 'app/models/effective/qb_api.rb', line 57 def accounts @accounts ||= with_service('Account') { |service| service.all } end |
#accounts_collection ⇒ Object
Only accounts we can use for the Deposit to Account setting
62 63 64 65 66 67 68 |
# File 'app/models/effective/qb_api.rb', line 62 def accounts_collection accounts .select { |account| ['Bank', 'Other Current Asset'].include?(account.account_type) } .sort_by { |account| [account.account_type, account.name] } .map { |account| [account.name, account.id, account.account_type] } .group_by(&:last) end |
#api_error ⇒ Object
44 45 46 47 48 49 50 |
# File 'app/models/effective/qb_api.rb', line 44 def api_error begin company_info.present?; nil rescue => e return e. end end |
#app_url ⇒ Object
13 14 15 |
# File 'app/models/effective/qb_api.rb', line 13 def app_url Quickbooks.sandbox_mode ? 'https://app.sandbox.qbo.intuit.com/app' : 'https://app.qbo.intuit.com/app' end |
#build_address(address) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/models/effective/qb_api.rb', line 30 def build_address(address) raise('Expected a Effective::Address') unless address.kind_of?(Effective::Address) Quickbooks::Model::PhysicalAddress.new( line1: address.address1, line2: address.address2, line3: address.try(:address3), city: address.city, country: address.country, country_sub_division_code: address.state_code, postal_code: address.postal_code ) end |
#company_info ⇒ Object
Singular
53 54 55 |
# File 'app/models/effective/qb_api.rb', line 53 def company_info @company_info ||= with_service('CompanyInfo') { |service| service.fetch_by_id(realm.realm_id) } end |
#create_customer(order:) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'app/models/effective/qb_api.rb', line 136 def create_customer(order:) raise('expected an Effective::Order') unless order.kind_of?(Effective::Order) with_service('Customer') do |service| customer = Quickbooks::Model::Customer.new( primary_email_address: Quickbooks::Model::EmailAddress.new(order.email), display_name: scrub(order.qb_online_customer_display_name) ) service.create(customer) end end |
#create_sales_receipt(sales_receipt:) ⇒ Object
153 154 155 |
# File 'app/models/effective/qb_api.rb', line 153 def create_sales_receipt(sales_receipt:) with_service('SalesReceipt') { |service| service.create(sales_receipt) } end |
#customer_url(obj) ⇒ Object
21 22 23 |
# File 'app/models/effective/qb_api.rb', line 21 def customer_url(obj) "#{app_url}/customerdetail?nameId=#{obj.try(:customer_id) || obj.try(:id) || obj}" end |
#delete_customer(customer:) ⇒ Object
149 150 151 |
# File 'app/models/effective/qb_api.rb', line 149 def delete_customer(customer:) with_service('Customer') { |service| service.delete(customer) } end |
#find_customer(order:) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/models/effective/qb_api.rb', line 109 def find_customer(order:) raise('expected an Effective::Order') unless order.kind_of?(Effective::Order) with_service('Customer') do |service| util = Quickbooks::Util::QueryBuilder.new # Find by display name customer = service.find_by(:display_name, scrub(order.qb_online_customer_display_name))&.first # Find by email customer ||= if order.email.present? email = util.clause("PrimaryEmailAddr", "LIKE", order.email) service.query("SELECT * FROM Customer WHERE #{email}")&.first end # Find by first name and last name customer ||= if order.billing_first_name.present? && order.billing_last_name.present? first_name = util.clause("GivenName", "LIKE", order.billing_first_name) last_name = util.clause("FamilyName", "LIKE", order.billing_last_name) service.query("SELECT * FROM Customer WHERE #{first_name} AND #{last_name}")&.first end customer end end |
#find_or_create_customer(order:) ⇒ Object
105 106 107 |
# File 'app/models/effective/qb_api.rb', line 105 def find_or_create_customer(order:) find_customer(order: order) || create_customer(order: order) end |
#find_sales_receipt(id:) ⇒ Object
157 158 159 |
# File 'app/models/effective/qb_api.rb', line 157 def find_sales_receipt(id:) with_service('SalesReceipt') { |service| service.find_by(:id, id) } end |
#item_html(item) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'app/models/effective/qb_api.rb', line 87 def item_html(item) details = [ ("##{item.id}"), (item.sku if item.sku.present?), (item.description if item.description.present?) ].compact.join(', ') "<span>#{item.name}</span> <small>(#{details})</small>" end |
#item_names ⇒ Object
Array of strings
83 84 85 |
# File 'app/models/effective/qb_api.rb', line 83 def item_names items_collection.values.flatten(1).map(&:first).map(&:to_s).reject(&:blank?).uniq.sort end |
#items ⇒ Object
70 71 72 |
# File 'app/models/effective/qb_api.rb', line 70 def items @items ||= with_service('Item') { |service| service.all } end |
#items_collection ⇒ Object
74 75 76 77 78 79 80 |
# File 'app/models/effective/qb_api.rb', line 74 def items_collection items .reject { |item| item.type == 'Category' } .sort_by { |item| [item.type, item.name] } .map { |item| [item.name, item.id, {'data-html': item_html(item)}, item.type] } .group_by(&:last) end |
#payment_methods ⇒ Object
97 98 99 |
# File 'app/models/effective/qb_api.rb', line 97 def payment_methods @payment_methods ||= with_service('PaymentMethod') { |service| service.all } end |
#payment_methods_collection ⇒ Object
101 102 103 |
# File 'app/models/effective/qb_api.rb', line 101 def payment_methods_collection payment_methods.sort_by(&:name).map { |payment_method| [payment_method.name, payment_method.id] } end |
#price_to_amount(price) ⇒ Object
25 26 27 28 |
# File 'app/models/effective/qb_api.rb', line 25 def price_to_amount(price) raise('Expected an Integer price') unless price.kind_of?(Integer) (price / 100.0).round(2) end |
#sales_receipt_url(obj) ⇒ Object
17 18 19 |
# File 'app/models/effective/qb_api.rb', line 17 def sales_receipt_url(obj) "#{app_url}/salesreceipt?txnId=#{obj.try(:sales_receipt_id) || obj.try(:id) || obj}" end |
#tax_codes ⇒ Object
161 162 163 |
# File 'app/models/effective/qb_api.rb', line 161 def tax_codes @tax_codes ||= with_service('TaxCode') { |service| service.all } end |
#tax_rates ⇒ Object
165 166 167 |
# File 'app/models/effective/qb_api.rb', line 165 def tax_rates @tax_rates ||= with_service('TaxRate') { |service| service.all } end |
#taxes_collection ⇒ Object
Returns a Hash of BigDecimal.to_s String Tax Rate => TaxCode Object { ‘0.0’ => ‘Quickbooks::Model::TaxCode(Exempt)’, ‘5.0’ => ‘Quickbooks::Model::TaxCode(GST)’ }
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'app/models/effective/qb_api.rb', line 171 def taxes_collection @taxes_collection ||= begin rates = tax_rates() codes = tax_codes() # Find Exempt 0.0 exempt = codes.find do |code| rate_id = code.sales_tax_rate_list.tax_rate_detail.first&.tax_rate_ref&.value rate = rates.find { |rate| rate.id == rate_id } if rate_id code.name.downcase.include?('exempt') && rate && rate.rate_value == 0.0 end exempt = [['0.0', exempt]] if exempt.present? # Find The rest tax_codes = codes.select(&:active?).map do |code| rate_id = code.sales_tax_rate_list.tax_rate_detail.first&.tax_rate_ref&.value rate = rates.find { |rate| rate.id == rate_id } if rate_id [rate.rate_value.to_s, code] if rate && (exempt.blank? || rate.rate_value.to_f > 0.0) end (Array(exempt) + tax_codes.compact.uniq { |key, _| key }).to_h end end |
#with_service(name, &block) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'app/models/effective/qb_api.rb', line 198 def with_service(name, &block) klass = "Quickbooks::Service::#{name}".constantize with_authenticated_request do |access_token| service = klass.new(company_id: realm.realm_id, access_token: access_token) # quickbooks-ruby's rebuild_connection! creates a new Faraday connection # on every service call with no timeouts. Reuse a single connection with # timeouts so all calls share one TCP connection via HTTP keep-alive. service.oauth.client.connection = faraday_connection yield(service) end end |