Class: SpreeAvatax::SalesInvoice
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- SpreeAvatax::SalesInvoice
- Defined in:
- app/models/spree_avatax/sales_invoice.rb
Overview
A SalesInvoice is persisted by Avatax but it’s not recognized as complete until it’s “committed”.
Defined Under Namespace
Classes: AlreadyCommittedError, CommitInvoiceNotFound
Constant Summary collapse
- DOC_TYPE =
'SalesInvoice'
- CANCEL_CODE =
'DocVoided'
Class Method Summary collapse
- .cancel(order) ⇒ Object
- .commit(order) ⇒ Object
-
.generate(order) ⇒ Object
Calls the Avatax API to generate a sales invoice and calculate taxes on the line items.
Class Method Details
.cancel(order) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'app/models/spree_avatax/sales_invoice.rb', line 96 def cancel(order) if !SpreeAvatax::Config.enabled logger.info("Avatax disabled. Skipping SalesInvoice.cancel for order #{order.number}") return end return if order.avatax_sales_invoice.nil? result = cancel_tax(order.avatax_sales_invoice) order.avatax_sales_invoice.update!({ canceled_at: Time.now, cancel_transaction_id: result[:transaction_id], }) rescue Exception => e if SpreeAvatax::Config.sales_invoice_cancel_error_handler SpreeAvatax::Config.sales_invoice_cancel_error_handler.call(order, e) else raise end end |
.commit(order) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/models/spree_avatax/sales_invoice.rb', line 73 def commit(order) if !SpreeAvatax::Config.enabled logger.info("Avatax disabled. Skipping SalesInvoice.commit for order #{order.number}") return end return if !SpreeAvatax::Shared.taxable_order?(order) raise CommitInvoiceNotFound.new("No invoice for order #{order.number}") if order.avatax_sales_invoice.nil? post_tax(order.avatax_sales_invoice) order.avatax_sales_invoice.update!(committed_at: Time.now) order.avatax_sales_invoice rescue Exception => e if SpreeAvatax::Config.sales_invoice_commit_error_handler SpreeAvatax::Config.sales_invoice_commit_error_handler.call(order, e) else raise end end |
.generate(order) ⇒ Object
Calls the Avatax API to generate a sales invoice and calculate taxes on the line items. On failure it will raise. On success it updates taxes on the order and its line items and create a SalesInvoice record.
At this point the record is saved but uncommitted on Avatax's end.
After the order completes the “.commit” method will get called and we’ll commit the
sales invoice, which marks it as complete on Avatax's end.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/models/spree_avatax/sales_invoice.rb', line 23 def generate(order) bench_start = Time.now if !SpreeAvatax::Config.enabled logger.info("Avatax disabled. Skipping SalesInvoice.generate for order #{order.number}") return end return if order.completed? || !SpreeAvatax::Shared.taxable_order?(order) taxable_records = order.line_items + order.shipments taxable_records.each do |taxable_record| taxable_record.update_column(:pre_tax_amount, taxable_record.discounted_amount.round(2)) end result = SpreeAvatax::SalesShared.get_tax(order, DOC_TYPE) # run this immediately to ensure that everything matches up before modifying the database tax_line_data = SpreeAvatax::SalesShared.build_tax_line_data(order, result) if sales_invoice = order.avatax_sales_invoice if sales_invoice.committed_at.nil? sales_invoice.destroy else raise AlreadyCommittedError.new("Sales invoice #{sales_invoice.id} is already committed.") end end SpreeAvatax::SalesShared.update_taxes(order, tax_line_data) sales_invoice = order.create_avatax_sales_invoice!({ transaction_id: result[:transaction_id], doc_id: result[:doc_id], doc_code: result[:doc_code], doc_date: result[:doc_date], pre_tax_total: result[:total_amount], additional_tax_total: result[:total_tax], }) sales_invoice rescue Exception => e if SpreeAvatax::Config.sales_invoice_generate_error_handler SpreeAvatax::Config.sales_invoice_generate_error_handler.call(order, e) else raise end ensure duration = (Time.now - bench_start).round Rails.logger.info "avatax_sales_invoice_generate_duration=#{(duration*1000).round}" end |