Class: SpreeAvatax::ReturnInvoice
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- SpreeAvatax::ReturnInvoice
- Defined in:
- app/models/spree_avatax/return_invoice.rb
Defined Under Namespace
Classes: AlreadyCommittedError, AvataxApiError, ReturnItemResponseMissing
Constant Summary collapse
- DOC_TYPE =
'ReturnInvoice'
- DESTINATION_CODE =
"1"
- TAX_OVERRIDE_TYPE =
'TaxDate'
- TAX_OVERRIDE_REASON =
'Adjustment for return'
Class Method Summary collapse
-
.finalize(reimbursement) ⇒ Object
Commit the return invoice on Avatax’s side after the reimbursement completes.
-
.generate(reimbursement) ⇒ Object
Calls the Avatax API to generate a return invoice and calculate taxes on the return items.
Class Method Details
.finalize(reimbursement) ⇒ Object
Commit the return invoice on Avatax’s side after the reimbursement completes. On failure it will raise. On success it markes the invoice as committed.
81 82 83 84 85 86 87 88 89 90 |
# File 'app/models/spree_avatax/return_invoice.rb', line 81 def finalize(reimbursement) if !SpreeAvatax::Config.enabled logger.info("Avatax disabled. Skipping ReturnInvoice.finalize for reimbursement #{reimbursement.number}") return end post_tax(reimbursement.return_invoice) reimbursement.return_invoice.update!(committed: true) end |
.generate(reimbursement) ⇒ Object
Calls the Avatax API to generate a return invoice and calculate taxes on the return items. On failure it will raise. On success it will update the tax amounts on each return item and create a ReturnInvoice record.
At this point an uncommitted return invoice has been created on Avatax's side.
After the reimbursement completes the “.finalize” method will get called and we’ll commit the
return invoice.
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 72 73 74 75 76 |
# File 'app/models/spree_avatax/return_invoice.rb', line 33 def generate(reimbursement) if !SpreeAvatax::Config.enabled logger.info("Avatax disabled. Skipping ReturnInvoice.generate for reimbursement #{reimbursement.number}") return end success_result = get_tax(reimbursement) if reimbursement.return_invoice if reimbursement.return_invoice.committed? raise AlreadyCommittedError.new("Return invoice #{reimbursement.return_invoice.id} is already committed.") else reimbursement.return_invoice.destroy end end # Array.wrap required because the XML engine the Avatax gem uses turns child nodes into # {...} instead of [{...}] when there is only one child. tax_lines = Array.wrap(success_result[:tax_lines][:tax_line]) reimbursement.return_items.each do |return_item| tax_line = tax_lines.detect { |l| l[:no] == return_item.id.to_s } if tax_line.nil? Rails.logger.error("missing return item #{return_item.id} in avatax response: #{success_result.inspect}") raise ReturnItemResponseMissing.new("couldn't find return item #{return_item.id} in avatax response") end tax = BigDecimal.new(tax_line[:tax]).abs return_item.update_attributes!({ additional_tax_total: tax }) end reimbursement.create_return_invoice!({ committed: false, doc_id: success_result[:doc_id], doc_code: success_result[:doc_code], doc_date: success_result[:doc_date], pre_tax_total: success_result[:total_amount], additional_tax_total: success_result[:total_tax], }) end |