-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (tax-integrations): add flow for syncing void/dispute lost invoi…
…ces (#2414) ## Context Currently Lago is implementing integration with tax provider Anrok ## Description This PR adds logic for syncing voided invoice with tax provider
- Loading branch information
1 parent
d2a7250
commit 8a5b5c7
Showing
17 changed files
with
607 additions
and
1 deletion.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
app/graphql/mutations/invoices/retry_tax_provider_voiding.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mutations | ||
module Invoices | ||
class RetryTaxProviderVoiding < BaseMutation | ||
include AuthenticableApiUser | ||
include RequiredOrganization | ||
|
||
REQUIRED_PERMISSION = 'invoices:update' | ||
|
||
description 'Retry voided invoice sync' | ||
|
||
argument :id, ID, required: true | ||
|
||
type Types::Invoices::Object | ||
|
||
def resolve(**args) | ||
invoice = current_organization.invoices.visible.find_by(id: args[:id]) | ||
result = ::Invoices::ProviderTaxes::VoidService.call(invoice:) | ||
|
||
result.success? ? result.invoice : result_error(result) | ||
end | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module Invoices | ||
module ProviderTaxes | ||
class VoidJob < ApplicationJob | ||
queue_as 'integrations' | ||
|
||
def perform(invoice:) | ||
return unless invoice.customer.anrok_customer | ||
|
||
# NOTE: We don't want to raise error here. | ||
# If sync fails, invoice would be marked and retry option would be available in the UI | ||
Invoices::ProviderTaxes::VoidService.call(invoice:) | ||
end | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# frozen_string_literal: true | ||
|
||
module Invoices | ||
module ProviderTaxes | ||
class VoidService < BaseService | ||
def initialize(invoice:) | ||
@invoice = invoice | ||
|
||
super | ||
end | ||
|
||
def call | ||
return result.not_found_failure!(resource: 'invoice') if invoice.blank? | ||
|
||
invoice.error_details.tax_voiding_error.discard_all | ||
|
||
tax_result = Integrations::Aggregator::Taxes::Invoices::VoidService.new(invoice:).call | ||
|
||
if frozen_transaction?(tax_result) | ||
negate_result = perform_invoice_negate | ||
|
||
unless negate_result.success? | ||
return result.validation_failure!(errors: {tax_error: [negate_result.error.code]}) | ||
end | ||
elsif !tax_result.success? | ||
create_error_detail(tax_result.error.code) | ||
|
||
return result.validation_failure!(errors: {tax_error: [tax_result.error.code]}) | ||
end | ||
|
||
result.invoice = invoice | ||
|
||
result | ||
end | ||
|
||
private | ||
|
||
attr_reader :invoice | ||
|
||
delegate :customer, to: :invoice | ||
|
||
def perform_invoice_negate | ||
negate_result = Integrations::Aggregator::Taxes::Invoices::NegateService.new(invoice:).call | ||
|
||
create_error_detail(negate_result.error.code) unless negate_result.success? | ||
|
||
negate_result | ||
end | ||
|
||
def create_error_detail(code) | ||
error_result = ErrorDetails::CreateService.call( | ||
owner: invoice, | ||
organization: invoice.organization, | ||
params: { | ||
error_code: :tax_voiding_error, | ||
details: { | ||
tax_voiding_error: code | ||
} | ||
} | ||
) | ||
error_result.raise_if_error! | ||
end | ||
|
||
# transactionFrozenForFiling error means that tax is already reported to the tax authority | ||
# We should call negate action instead | ||
def frozen_transaction?(tax_result) | ||
!tax_result.success? && tax_result.error.code == 'transactionFrozenForFiling' | ||
end | ||
end | ||
end | ||
end |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
11 changes: 11 additions & 0 deletions
11
spec/fixtures/integration_aggregator/taxes/invoices/failure_response_void.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"succeededInvoices": [], | ||
"failedInvoices": [ | ||
{ | ||
"id": "invoice_id", | ||
"validation_errors": { | ||
"type": "transactionFrozenForFiling" | ||
} | ||
} | ||
] | ||
} |
Oops, something went wrong.