Module: SpreeAvataxOfficial::Spree::OrderDecorator
- Defined in:
- app/models/spree_avatax_official/spree/order_decorator.rb
Class Method Summary collapse
Instance Method Summary collapse
- #address_validation_enabled? ⇒ Boolean
- #avalara_integration ⇒ Object
- #avatax_discount_amount ⇒ Object
- #avatax_enabled? ⇒ Boolean
- #avatax_tax_calculation_required? ⇒ Boolean
-
#create_tax_charge! ⇒ Object
We need to override this so the default Spree tax calculation is not triggered.
- #line_items_discounted_in_avatax? ⇒ Boolean
- #recalculate_avatax_taxes ⇒ Object
- #tax_address_symbol ⇒ Object
- #taxable_items ⇒ Object
- #validate_tax_address ⇒ Object
Class Method Details
.prepended(base) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'app/models/spree_avatax_official/spree/order_decorator.rb', line 4 def self.prepended(base) base.register_update_hook :recalculate_avatax_taxes base.has_many :avatax_transactions, class_name: 'SpreeAvataxOfficial::Transaction' base.has_one :avatax_sales_invoice_transaction, -> { where(transaction_type: 'SalesInvoice') }, class_name: 'SpreeAvataxOfficial::Transaction', inverse_of: :order base.state_machine.before_transition to: :delivery, do: :validate_tax_address, if: :address_validation_enabled? base.state_machine.after_transition to: :canceled, do: :void_in_avatax base.state_machine.after_transition to: :complete, do: :commit_in_avatax # Recalculate when the order's address changes (selecting an existing address from the address book). base.after_update :recalculate_avatax_taxes_on_address_change end |
Instance Method Details
#address_validation_enabled? ⇒ Boolean
85 86 87 |
# File 'app/models/spree_avatax_official/spree/order_decorator.rb', line 85 def address_validation_enabled? avalara_integration&.preferred_address_validation_enabled || false end |
#avalara_integration ⇒ Object
21 22 23 |
# File 'app/models/spree_avatax_official/spree/order_decorator.rb', line 21 def avalara_integration store&.integrations&.active&.find_by(type: 'Spree::Integrations::Avalara') end |
#avatax_discount_amount ⇒ Object
41 42 43 |
# File 'app/models/spree_avatax_official/spree/order_decorator.rb', line 41 def avatax_discount_amount adjustments.promotion.eligible.sum(:amount).abs end |
#avatax_enabled? ⇒ Boolean
25 26 27 |
# File 'app/models/spree_avatax_official/spree/order_decorator.rb', line 25 def avatax_enabled? avalara_integration.present? end |
#avatax_tax_calculation_required? ⇒ Boolean
33 34 35 36 37 38 39 |
# File 'app/models/spree_avatax_official/spree/order_decorator.rb', line 33 def avatax_tax_calculation_required? return false unless tax_address&.persisted? return false unless line_items.any? return false if delivery_required? && shipments.empty? true end |
#create_tax_charge! ⇒ Object
We need to override this so the default Spree tax calculation is not triggered. The actual tax calculation by Avalara is done in #recalculate_avatax_taxes
55 56 57 58 59 |
# File 'app/models/spree_avatax_official/spree/order_decorator.rb', line 55 def create_tax_charge! return if avatax_enabled? super end |
#line_items_discounted_in_avatax? ⇒ Boolean
45 46 47 |
# File 'app/models/spree_avatax_official/spree/order_decorator.rb', line 45 def line_items_discounted_in_avatax? adjustments.promotion.eligible.any? end |
#recalculate_avatax_taxes ⇒ Object
61 62 63 64 65 66 67 |
# File 'app/models/spree_avatax_official/spree/order_decorator.rb', line 61 def recalculate_avatax_taxes return unless avatax_enabled? SpreeAvataxOfficial::CreateTaxAdjustmentsService.call(order: self) update_totals persist_totals end |
#tax_address_symbol ⇒ Object
49 50 51 |
# File 'app/models/spree_avatax_official/spree/order_decorator.rb', line 49 def tax_address_symbol ::Spree::Config.tax_using_ship_address ? :ship_address : :bill_address end |
#taxable_items ⇒ Object
29 30 31 |
# File 'app/models/spree_avatax_official/spree/order_decorator.rb', line 29 def taxable_items line_items.reload + shipments.reload end |
#validate_tax_address ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/models/spree_avatax_official/spree/order_decorator.rb', line 69 def validate_tax_address response = SpreeAvataxOfficial::Address::Validate.call( address: tax_address, order: self ) return if response.success? = response.value&.body&.dig('messages') = .present? ? .map { || ['summary'] }.join('. ') : 'Address validation failed' errors.add(:base, ) false end |