Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(integrations): Sync credit notes add service #2085

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/services/integrations/aggregator/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def fallback_item
@fallback_item ||= collection_mapping(:fallback_item)
end

def amount(amount_cents)
currency = invoice.total_amount.currency
def amount(amount_cents, resource:)
currency = resource.total_amount.currency

amount_cents.round.fdiv(currency.subunit_to_unit)
end
Expand Down
104 changes: 104 additions & 0 deletions app/services/integrations/aggregator/credit_notes/create_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# frozen_string_literal: true

module Integrations
module Aggregator
module CreditNotes
class CreateService < Integrations::Aggregator::Invoices::BaseService
def initialize(credit_note:)
@credit_note = credit_note

super(invoice:)
end

def action_path
"v1/#{provider}/creditnotes"
end

def call
return result unless integration
return result unless integration.sync_credit_notes
return result unless credit_note.finalized?
return result unless fallback_item
ivannovosad marked this conversation as resolved.
Show resolved Hide resolved

response = http_client.post_with_response(payload, headers)
result.external_id = JSON.parse(response.body)

IntegrationResource.create!(
integration:,
external_id: result.external_id,
syncable_id: credit_note.id,
syncable_type: 'CreditNote',
resource_type: :credit_note,
)

result
rescue LagoHttpClient::HttpError => e
error = e.json_message
code = error['type']
message = error.dig('payload', 'message')

deliver_error_webhook(customer:, code:, message:)

raise e
end

def call_async
return result.not_found_failure!(resource: 'credit_note') unless credit_note

::Integrations::Aggregator::CreditNotes::CreateJob.perform_later(credit_note:)

result.credit_note_id = credit_note.id
result
end

private

attr_reader :credit_note

delegate :customer, to: :credit_note, allow_nil: true
delegate :invoice, to: :credit_note

def coupons
output = []

if credit_note.coupons_adjustment_amount_cents > 0
output << {
'item' => coupon_item&.external_id,
'account' => coupon_item&.external_account_code,
'quantity' => 1,
'rate' => -amount(credit_note.coupons_adjustment_amount_cents, resource: credit_note)
}
end

output
end

def payload
{
'type' => 'creditmemo',
'isDynamic' => true,
'columns' => {
'tranid' => credit_note.number,
'entity' => integration_customer.external_customer_id,
'istaxable' => true,
'taxitem' => tax_item.external_id,
'taxamountoverride' => amount(credit_note.taxes_amount_cents, resource: credit_note),
'otherrefnum' => credit_note.number,
'custbody_lago_id' => credit_note.id,
'tranId' => credit_note.id
},
'lines' => [
{
'sublistId' => 'item',
'lineItems' => credit_note.items.map { |item| item(item.fee) } + coupons
}
],
'options' => {
'ignoreMandatoryFields' => false
}
}
end
end
end
end
end
8 changes: 4 additions & 4 deletions app/services/integrations/aggregator/invoices/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def discounts
'item' => coupon_item.external_id,
'account' => coupon_item.external_account_code,
'quantity' => 1,
'rate' => -amount(invoice.coupons_amount_cents)
'rate' => -amount(invoice.coupons_amount_cents, resource: invoice)
}
end

Expand All @@ -86,7 +86,7 @@ def discounts
'item' => credit_item.external_id,
'account' => credit_item.external_account_code,
'quantity' => 1,
'rate' => -amount(invoice.prepaid_credit_amount_cents)
'rate' => -amount(invoice.prepaid_credit_amount_cents, resource: invoice)
}
end

Expand All @@ -95,7 +95,7 @@ def discounts
'item' => credit_note_item.external_id,
'account' => credit_note_item.external_account_code,
'quantity' => 1,
'rate' => -amount(invoice.credit_notes_amount_cents)
'rate' => -amount(invoice.credit_notes_amount_cents, resource: invoice)
}
end

Expand All @@ -111,7 +111,7 @@ def payload(type)
'entity' => integration_customer.external_customer_id,
'istaxable' => true,
'taxitem' => tax_item&.external_id,
'taxamountoverride' => amount(invoice.taxes_amount_cents),
'taxamountoverride' => amount(invoice.taxes_amount_cents, resource: invoice),
'otherrefnum' => invoice.number,
'custbody_lago_id' => invoice.id,
'custbody_ava_disable_tax_calculation' => true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"456"
Loading
Loading