Skip to content

Commit

Permalink
feat(netsuite-taxes): Adjust netsuite invoice payload - add taxes
Browse files Browse the repository at this point in the history
  • Loading branch information
ivannovosad committed Sep 24, 2024
1 parent f1f0898 commit 6247a76
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 65 deletions.
4 changes: 4 additions & 0 deletions app/services/integrations/aggregator/base_payload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ def collection_mapping(type)
private

attr_reader :integration

def tax_item_complete?
tax_item&.tax_nexus.present? && tax_item&.tax_type.present? && tax_item&.tax_code.present?
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@ def body
{
'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
},
'columns' => columns,
'lines' => [
{
'sublistId' => 'item',
Expand All @@ -33,6 +24,25 @@ def body

private

def columns
result = {
'tranid' => credit_note.number,
'entity' => integration_customer.external_customer_id,
'taxregoverride' => true,
'taxdetailsoverride' => true,
'otherrefnum' => credit_note.number,
'custbody_ava_disable_tax_calculation' => true,
'custbody_lago_id' => credit_note.id,
'tranId' => credit_note.id
}

if tax_item&.tax_nexus.present?
result['nexus'] = tax_item.tax_nexus
end

result
end

def item(credit_note_item)
fee = credit_note_item.fee

Expand All @@ -56,7 +66,8 @@ def item(credit_note_item)
'item' => mapped_item.external_id,
'account' => mapped_item.external_account_code,
'quantity' => 1,
'rate' => amount(credit_note_item.amount_cents, resource: credit_note_item.credit_note)
'rate' => amount(credit_note_item.amount_cents, resource: credit_note_item.credit_note),
'taxdetailsreference' => credit_note_item.id
}
end

Expand Down
99 changes: 87 additions & 12 deletions app/services/integrations/aggregator/invoices/payloads/netsuite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,60 @@ class Netsuite < BasePayload
MAX_DECIMALS = 15

def body
{
result = {
'type' => type,
'isDynamic' => true,
'columns' => columns,
'lines' => [
{
'sublistId' => 'item',
'lineItems' => invoice.fees.where('amount_cents > ?', 0).order(created_at: :asc).map do |fee|
item(fee)
end + discounts
'lineItems' => fee_items + discounts
}
],
'options' => {
'ignoreMandatoryFields' => false
}
}

if tax_item_complete?
result['taxdetails'] = [
{
'sublistId' => 'taxdetails',
'lineItems' => tax_line_items + discount_taxes
}
]
end

result
end

private

def tax_line_items
fees.map { |fee| tax_line_item(fee) }
end

def fee_items
fees.map { |fee| item(fee) }
end

def fees
@fees ||= invoice.fees.where('amount_cents > ?', 0).order(created_at: :asc)
end

def columns
result = {
'tranid' => invoice.id,
'entity' => integration_customer.external_customer_id,
'istaxable' => true,
'otherrefnum' => invoice.number,
'custbody_lago_id' => invoice.id,
'custbody_ava_disable_tax_calculation' => true,
'custbody_lago_invoice_link' => invoice_url,
'duedate' => due_date
}

if tax_item
result['taxitem'] = tax_item.external_id
result['taxamountoverride'] = amount(invoice.taxes_amount_cents, resource: invoice)
if tax_item&.tax_nexus.present?
result['nexus'] = tax_item.tax_nexus
end

result
Expand Down Expand Up @@ -79,7 +98,19 @@ def item(fee)
'item' => mapped_item.external_id,
'account' => mapped_item.external_account_code,
'quantity' => fee.units,
'rate' => limited_rate(fee.precise_unit_amount)
'rate' => limited_rate(fee.precise_unit_amount),
'taxdetailsreference' => fee.id
}
end

def tax_line_item(fee)
{
'taxdetailsreference' => fee.id,
'taxamount' => amount(fee.taxes_amount_cents, resource: invoice),
'taxbasis' => 1,
'taxrate' => fee.taxes_rate,
'taxtype' => tax_item.tax_type,
'taxcode' => tax_item.tax_code
}
end

Expand All @@ -91,7 +122,8 @@ def discounts
'item' => coupon_item.external_id,
'account' => coupon_item.external_account_code,
'quantity' => 1,
'rate' => -amount(invoice.coupons_amount_cents, resource: invoice)
'rate' => -amount(invoice.coupons_amount_cents, resource: invoice),
'taxdetailsreference' => 'coupon_item'
}
end

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

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

output
end

def discount_taxes
output = []

if invoice.coupons_amount_cents > 0
tax_diff_amount_cents = invoice.taxes_amount_cents - fees.sum { |f| f['taxes_amount_cents'] }

output << {
'taxbasis' => 1,
'taxamount' => amount((tax_diff_amount_cents || 0).abs, resource: invoice),
'taxrate' => invoice.taxes_rate,
'taxtype' => tax_item.tax_type,
'taxcode' => tax_item.tax_code,
'taxdetailsreference' => 'coupon_item'
}
end

if credit_item && invoice.prepaid_credit_amount_cents > 0
output << {
'taxbasis' => 1,
'taxamount' => 0,
'taxrate' => invoice.taxes_rate,
'taxtype' => tax_item.tax_type,
'taxcode' => tax_item.tax_code,
'taxdetailsreference' => 'credit_item'
}
end

if credit_note_item && invoice.credit_notes_amount_cents > 0
output << {
'taxbasis' => 1,
'taxamount' => 0,
'taxrate' => invoice.taxes_rate,
'taxtype' => tax_item.tax_type,
'taxcode' => tax_item.tax_code,
'taxdetailsreference' => 'credit_note_item'
}
end

Expand Down
Loading

0 comments on commit 6247a76

Please sign in to comment.