Skip to content

Commit

Permalink
feat(netsuite-taxes): Adjust netsuite credit note payload - add taxes
Browse files Browse the repository at this point in the history
  • Loading branch information
ivannovosad committed Sep 24, 2024
1 parent 6247a76 commit 12ff8df
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,47 @@ module CreditNotes
module Payloads
class Netsuite < BasePayload
def body
{
result = {
'type' => 'creditmemo',
'isDynamic' => true,
'columns' => columns,
'lines' => [
{
'sublistId' => 'item',
'lineItems' => credit_note.items.map { |credit_note_item| item(credit_note_item) } + coupons
'lineItems' => credit_note_items + coupons
}
],
'options' => {
'ignoreMandatoryFields' => false
}
}

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

result
end

private

def credit_note_items
items.map { |credit_note_item| item(credit_note_item) }
end

def tax_line_items
items.map { |credit_note_item| tax_line_item(credit_note_item) }
end

def items
@items ||= credit_note.items.order(created_at: :asc)
end

def columns
result = {
'tranid' => credit_note.number,
Expand Down Expand Up @@ -71,6 +94,41 @@ def item(credit_note_item)
}
end

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

def tax_line_items_with_adjusted_taxes
taxes_amount_cents_sum = tax_line_items.sum { |f| f['taxamount'].to_d }

return tax_line_items if taxes_amount_cents_sum == credit_note.taxes_amount_cents

adjusted_first_tax = false

tax_line_items.map do |credit_note_item|
if credit_note_item['taxamount'] > 0 && !adjusted_first_tax
amount = amount(credit_note.taxes_amount_cents, resource: credit_note)
credit_note_item['taxamount'] += amount - taxes_amount_cents_sum
adjusted_first_tax = true
end

credit_note_item
end
end

def taxes_amount(credit_note_item)
subunit_to_unit = credit_note_item.amount.currency.subunit_to_unit.to_d
amount = credit_note_item.amount_cents.fdiv(subunit_to_unit) * credit_note_item.credit_note.taxes_rate
amount.round(2)
end

def coupons
output = []

Expand All @@ -79,7 +137,25 @@ def coupons
'item' => coupon_item&.external_id,
'account' => coupon_item&.external_account_code,
'quantity' => 1,
'rate' => -amount(credit_note.coupons_adjustment_amount_cents, resource: credit_note)
'rate' => -amount(credit_note.coupons_adjustment_amount_cents, resource: credit_note),
'taxdetailsreference' => 'coupon_item'
}
end

output
end

def coupon_taxes
output = []

if credit_note.coupons_adjustment_amount_cents > 0
output << {
'taxbasis' => 1,
'taxamount' => 0,
'taxrate' => credit_note.taxes_rate,
'taxtype' => tax_item.tax_type,
'taxcode' => tax_item.tax_code,
'taxdetailsreference' => 'coupon_item'
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def discount_taxes

output << {
'taxbasis' => 1,
'taxamount' => amount((tax_diff_amount_cents || 0).abs, resource: invoice),
'taxamount' => amount(tax_diff_amount_cents, resource: invoice),
'taxrate' => invoice.taxes_rate,
'taxtype' => tax_item.tax_type,
'taxcode' => tax_item.tax_code,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@
'columns' => {
'tranid' => credit_note.number,
'entity' => integration_customer.external_customer_id,
'istaxable' => true,
'taxitem' => integration_collection_mapping5&.external_id,
'taxamountoverride' => 80.0,
'taxregoverride' => true,
'taxdetailsoverride' => true,
'otherrefnum' => credit_note.number,
'custbody_ava_disable_tax_calculation' => true,
'custbody_lago_id' => credit_note.id,
'tranId' => credit_note.id
},
Expand All @@ -150,13 +150,15 @@
'item' => 'm2',
'account' => 'm22',
'quantity' => 1,
'rate' => 2.12
'rate' => 2.12,
'taxdetailsreference' => anything
},
{
'item' => '2',
'account' => '22',
'quantity' => 1,
'rate' => -20.0
'rate' => -20.0,
'taxdetailsreference' => 'coupon_item'
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,6 @@
'columns' => {
'tranid' => invoice.id,
'entity' => integration_customer.external_customer_id,
'istaxable' => true,
'taxitem' => integration_collection_mapping5&.external_id,
'taxamountoverride' => 80.0,
'otherrefnum' => invoice.number,
'custbody_lago_id' => invoice.id,
'custbody_ava_disable_tax_calculation' => true,
Expand All @@ -159,37 +156,43 @@
'item' => '3',
'account' => '33',
'quantity' => 0.0,
'rate' => 0.0
'rate' => 0.0,
'taxdetailsreference' => anything
},
{
'item' => '4',
'account' => '44',
'quantity' => 0.0,
'rate' => 0.0
'rate' => 0.0,
'taxdetailsreference' => anything
},
{
'item' => 'm2',
'account' => 'm22',
'quantity' => 2,
'rate' => 4.1212121212334
'rate' => 4.1212121212334,
'taxdetailsreference' => anything
},
{
'item' => '2',
'account' => '22',
'quantity' => 1,
'rate' => -20.0
'rate' => -20.0,
'taxdetailsreference' => 'coupon_item'
},
{
'item' => '6',
'account' => '66',
'quantity' => 1,
'rate' => -40.0
'rate' => -40.0,
'taxdetailsreference' => 'credit_item'
},
{
'item' => '1', # Fallback item instead of credit note
'account' => '11',
'quantity' => 1,
'rate' => -60.0
'rate' => -60.0,
'taxdetailsreference' => 'credit_note_item'
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,6 @@
'columns' => {
'tranid' => invoice.id,
'entity' => integration_customer.external_customer_id,
'istaxable' => true,
'taxitem' => integration_collection_mapping5.external_id,
'taxamountoverride' => 80.0,
'otherrefnum' => invoice.number,
'custbody_lago_id' => invoice.id,
'custbody_ava_disable_tax_calculation' => true,
Expand All @@ -155,37 +152,43 @@
'item' => '3',
'account' => '33',
'quantity' => 0.0,
'rate' => 0.0
'rate' => 0.0,
'taxdetailsreference' => anything
},
{
'item' => '4',
'account' => '44',
'quantity' => 0.0,
'rate' => 0.0
'rate' => 0.0,
'taxdetailsreference' => anything
},
{
'item' => 'm2',
'account' => 'm22',
'quantity' => 2,
'rate' => 4.12
'rate' => 4.12,
'taxdetailsreference' => anything
},
{
'item' => '2',
'account' => '22',
'quantity' => 1,
'rate' => -20.0
'rate' => -20.0,
'taxdetailsreference' => 'coupon_item'
},
{
'item' => '6',
'account' => '66',
'quantity' => 1,
'rate' => -40.0
'rate' => -40.0,
'taxdetailsreference' => 'credit_item'
},
{
'item' => '1', # Fallback item instead of credit note
'account' => '11',
'quantity' => 1,
'rate' => -60.0
'rate' => -60.0,
'taxdetailsreference' => 'credit_note_item'
}
]
}
Expand Down

0 comments on commit 12ff8df

Please sign in to comment.