Created
September 23, 2017 02:01
-
-
Save rayning0/eee216074fb00a2a06ed84f6d4a2de8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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