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

feat(anrok-integration): add graphql support for fetching taxes #2254

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

module Mutations
module Integrations
module Anrok
class FetchDraftInvoiceTaxes < BaseMutation
include AuthenticableApiUser
include RequiredOrganization

REQUIRED_PERMISSION = 'invoices:create'

description 'Fetches taxes for one-off invoice'

input_object_class Types::Invoices::CreateInvoiceInput

type Types::Integrations::AnrokObjects::FeeObject.collection_type

def resolve(**args)
customer = Customer.find_by(
id: args[:customer_id],
organization_id: current_organization.id
)

result = ::Integrations::Aggregator::Taxes::Invoices::CreateDraftService.new(
invoice: invoice(customer, args),
fees: fees(args)
).call

result.success? ? result.fees : result_error(result)
end

private

# Note: We need to pass invoice object to the service that return taxes. This service should
# work with real invoice objects. In this case, it should also work with invoice that is not stored yet,
# because we need to fetch taxes for one-off invoice UI form.
def invoice(customer, args)
OpenStruct.new(
issuing_date: Time.current.in_time_zone(customer.applicable_timezone).to_date,
currency: args[:currency],
customer:
)
end

def fees(args)
args[:fees].map do |fee|
unit_amount_cents = fee[:unit_amount_cents]
units = fee[:units]&.to_f || 1

OpenStruct.new(
add_on_id: fee[:add_on_id],
item_id: fee[:add_on_id],
amount_cents: (unit_amount_cents * units).round
)
end
end
end
end
end
end
20 changes: 20 additions & 0 deletions app/graphql/types/integrations/anrok_objects/breakdown_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Types
module Integrations
module AnrokObjects
class BreakdownObject < Types::BaseObject
graphql_name 'AnrokBreakdownObject'

field :name, String, null: true
field :rate, GraphQL::Types::Float, null: true
field :tax_amount, GraphQL::Types::BigInt, null: true
field :type, String, null: true

def rate
BigDecimal(object.rate)
end
end
end
end
end
18 changes: 18 additions & 0 deletions app/graphql/types/integrations/anrok_objects/fee_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module Types
module Integrations
module AnrokObjects
class FeeObject < Types::BaseObject
graphql_name 'AnrokFeeObject'

field :amount_cents, GraphQL::Types::BigInt, null: true
field :item_code, String, null: true
field :item_id, String, null: true
field :tax_amount_cents, GraphQL::Types::BigInt, null: true

field :tax_breakdown, [Types::Integrations::AnrokObjects::BreakdownObject]
end
end
end
end
2 changes: 2 additions & 0 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ class MutationType < Types::BaseObject
field :create_anrok_integration, mutation: Mutations::Integrations::Anrok::Create
field :update_anrok_integration, mutation: Mutations::Integrations::Anrok::Update

field :fetch_draft_invoice_taxes, mutation: Mutations::Integrations::Anrok::FetchDraftInvoiceTaxes

field :create_xero_integration, mutation: Mutations::Integrations::Xero::Create
field :update_xero_integration, mutation: Mutations::Integrations::Xero::Update

Expand Down
2 changes: 1 addition & 1 deletion app/services/integrations/aggregator/base_payload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def billable_metric_item(fee)
def add_on_item(fee)
integration
.integration_mappings
.find_by(mappable_type: 'AddOn', mappable_id: fee.add_on.id) || fallback_item
.find_by(mappable_type: 'AddOn', mappable_id: fee.add_on_id) || fallback_item
end

def tax_item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,33 @@ def process_response(body)
fees = body['succeededInvoices']&.first.try(:[], 'fees')

if fees
result.fees = fees
result.fees = fees.map do |fee|
OpenStruct.new(
item_id: fee['item_id'],
item_code: fee['item_code'],
amount_cents: fee['amount_cents'],
tax_amount_cents: fee['tax_amount_cents'],
tax_breakdown: tax_breakdown(fee['tax_breakdown'])
)
end
else
code = body['failedInvoices'].first['validation_errors']['type']
message = 'Service failure'

result.service_failure!(code:, message:)
end
end

def tax_breakdown(breakdown)
breakdown.map do |b|
OpenStruct.new(
name: b['name'],
rate: b['rate'],
tax_amount: b['tax_amount'],
type: b['type']
)
end
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(integration:, customer:, invoice:, fees: [])

@customer = customer
@invoice = invoice
@fees = fees
@fees = fees.is_a?(Array) ? fees : fees.order(created_at: :asc)
end

def body
Expand All @@ -28,7 +28,7 @@ def body
'taxable' => customer.tax_identification_number.present?,
'tax_number' => customer.tax_identification_number
},
'fees' => fees.order(created_at: :asc).map { |fee| fee_item(fee) }
'fees' => fees.map { |fee| fee_item(fee) }
}
]
end
Expand Down
43 changes: 43 additions & 0 deletions schema.graphql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading