Class: Invoicing::SubmitToTaxjar
- Inherits:
-
BaseService
- Object
- BaseService
- Invoicing::SubmitToTaxjar
- Defined in:
- app/services/invoicing/submit_to_taxjar.rb
Overview
Service object: submit to taxjar.
Defined Under Namespace
Classes: Result
Constant Summary collapse
- MARKETPLACE_PROVIDERS =
Marketplace providers.
{ 5_322_434 => 'amazon', 23_041_827 => 'walmart' }.freeze
Instance Attribute Summary
Attributes inherited from BaseService
Instance Method Summary collapse
- #delete ⇒ Object
- #details ⇒ Object
- #existing_record? ⇒ Boolean
-
#initialize(invoice, options = {}) ⇒ SubmitToTaxjar
constructor
A new instance of SubmitToTaxjar.
- #submit ⇒ Object
- #taxjar_id ⇒ Object
- #update ⇒ Object
Methods inherited from BaseService
#log_debug, #log_error, #log_info, #log_warning, #logger, #process, #tagged_logger
Constructor Details
#initialize(invoice, options = {}) ⇒ SubmitToTaxjar
Returns a new instance of SubmitToTaxjar.
20 21 22 23 24 25 |
# File 'app/services/invoicing/submit_to_taxjar.rb', line 20 def initialize(invoice, = {}) @invoice = invoice @options = @logger = [:logger] || Rails.logger @client = Api::Taxjar.client end |
Instance Method Details
#delete ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'app/services/invoicing/submit_to_taxjar.rb', line 139 def delete @client.show_order(taxjar_id) @client.delete_order(taxjar_id) Result.new( success: true, message: "Record on Taxjar for invoice #{taxjar_id} successfully deleted" ) rescue Api::Taxjar::Error::NotFound @logger.info "Record not found on Taxjar for invoice #{taxjar_id}" Result.new( success: false, message: "Record not found on Taxjar for invoice #{taxjar_id}" ) end |
#details ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'app/services/invoicing/submit_to_taxjar.rb', line 154 def details line_items = [] if @invoice.line_items.present? @invoice.line_items.parents_only.non_shipping.each do |li| next if li.taxable_total.zero? product_identifier = li.item.present? ? li.item.sku : li.cm_category description = if li.item.present? "#{li.item.sku} - #{li.item.name}" else li.description.presence || li.cm_category end price = [li.price, li.discounted_price].max discount = [-li.line_discounts.sum(:amount), 0].max line_items << { quantity: li.quantity, product_identifier: product_identifier, product_tax_code: li.item&.product_tax_code&.product_tax_code, description: description, unit_price: price, discount: discount, sales_tax: li.tax_total } end end amount = @invoice.line_items.to_a.sum(&:taxable_total).to_f shipping = @invoice.shipping_cost.to_f == @invoice.line_items.shipping_only.to_a.sum(&:taxable_total).to_f ? @invoice.shipping_cost.to_f : @invoice.line_items.shipping_only.to_a.sum(&:taxable_total).to_f # Map customer IDs to their marketplace providers marketplace_provider = MARKETPLACE_PROVIDERS[@invoice.customer_id] # Build base transaction details base_details = { customer_id: @invoice.taxjar_customer_id, transaction_id: taxjar_id, provider: marketplace_provider.presence || 'api', transaction_date: @invoice.gl_date.to_s, to_country: @invoice.shipping_address.country.iso, to_zip: @invoice.shipping_address.zip, to_state: @invoice.shipping_address.state_code, to_city: @invoice.shipping_address.city, to_street: @invoice.shipping_address.street1, amount: amount, shipping: shipping, sales_tax: @invoice.tax_total.to_f, line_items: line_items } # Add exemption_type for marketplace providers only base_details[:exemption_type] = 'marketplace' if marketplace_provider.present? base_details end |
#existing_record? ⇒ Boolean
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'app/services/invoicing/submit_to_taxjar.rb', line 74 def existing_record? marketplace_provider = MARKETPLACE_PROVIDERS[@invoice.customer_id] # Check if record already exists begin existing_records = @client.list_orders({ from_transaction_date: @invoice.gl_date.to_s, to_transaction_date: @invoice.gl_date.to_s }) return true if existing_records.include?(taxjar_id) rescue Api::Taxjar::Error # list_orders failed, try show_order instead end # Try to fetch the record directly (with provider parameter for marketplace orders) begin existing_record = if marketplace_provider.present? @client.show_order(taxjar_id, { provider: marketplace_provider }) else @client.show_order(taxjar_id) end existing_record.present? rescue Api::Taxjar::Error::NotFound false end end |
#submit ⇒ Object
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 |
# File 'app/services/invoicing/submit_to_taxjar.rb', line 33 def submit if (blocked = ensure_consistent_ship_to) return blocked end if existing_record? @logger.info "Record already exists on Taxjar for invoice #{taxjar_id}" Result.new( success: false, message: "Record already exists on Taxjar for invoice #{taxjar_id}" ) else new_record = @client.create_order(details) Result.new( success: true, original_tax_amount: @invoice.tax_total, calculated_tax: new_record.sales_tax ) end rescue Api::Taxjar::Error::BadRequest => e flag_ship_to_rejected(e) rescue Api::Taxjar::Error::UnprocessableEntity => e # Handle "Provider tranx already imported" - transaction exists via marketplace integration if e..include?('already imported') Result.new( success: true, message: "Record already exists on Taxjar via marketplace provider for invoice #{taxjar_id}" ) else Result.new( success: false, message: e. ) end rescue Api::Taxjar::Error::ServerError => e Result.new( success: false, message: e. ) end |
#taxjar_id ⇒ Object
27 28 29 30 31 |
# File 'app/services/invoicing/submit_to_taxjar.rb', line 27 def taxjar_id str = @invoice.reference_number.clone str.prepend("#{Rails.env}-") if !Rails.env.production? && !str.start_with?("#{Rails.env}-") str end |
#update ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'app/services/invoicing/submit_to_taxjar.rb', line 101 def update if (blocked = ensure_consistent_ship_to) return blocked end marketplace_provider = MARKETPLACE_PROVIDERS[@invoice.customer_id] # Fetch existing record with provider parameter if applicable if marketplace_provider.present? @client.show_order(taxjar_id, { provider: marketplace_provider }) else @client.show_order(taxjar_id) end updated_record = @client.update_order(details) Result.new( success: true, original_tax_amount: @invoice.tax_total, calculated_tax: updated_record.sales_tax ) rescue Api::Taxjar::Error::BadRequest => e flag_ship_to_rejected(e) rescue Api::Taxjar::Error::NotFound @logger.info "Record not found on Taxjar for invoice #{taxjar_id}" Result.new( success: false, message: "Record not found on Taxjar for invoice #{taxjar_id}" ) rescue Api::Taxjar::Error::Forbidden => e Rails.logger.warn "TaxJar API returned 403 Forbidden for invoice #{taxjar_id}. " \ "Check TaxJar credentials or account permissions. #{e.}" Result.new( success: false, message: "TaxJar request forbidden for invoice #{taxjar_id}: #{e.}" ) end |