Skip to content

Instantly share code, notes, and snippets.

@rayning0
Created September 23, 2017 02:01
Show Gist options
  • Select an option

  • Save rayning0/eee216074fb00a2a06ed84f6d4a2de8c to your computer and use it in GitHub Desktop.

Select an option

Save rayning0/eee216074fb00a2a06ed84f6d4a2de8c to your computer and use it in GitHub Desktop.
def total_tax_amount_from_db
total_amount = invoice_custom_vendor_items.where(is_taxable: true)
.map(&:total_amount)
.sum
tax = Tax.new(zipcode: shipping_zip, revenue: total_amount).get_tax
if tax['ItemMessages'].empty?
taxes = tax['GroupList'][0]
self.shipping_state = taxes['StateCode']
self.shipping_county = taxes['TaxList'][0]['CountyName']
self.shipping_city = taxes['TaxList'][0]['CityName']
self.total_tax_rate = taxes['TaxList'].map{ |t| t['TaxRate'] }.sum
BigDecimal.new(tax['TotalTax'])
else
# Error message. Probably invalid ZIP code
wrong_shipping_zip(tax['ItemMessages'][0]['Message'])
end
end
def wrong_shipping_zip(error_message)
errors.add(:shipping_zip, error_message)
raise ActiveRecord::RecordInvalid.new(self)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment