Skip to content

Commit

Permalink
misc(fee): Delete zero amount finalized fees
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet committed Jan 3, 2025
1 parent 8d43eac commit 783317c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions db/migrate/20250103124802_drop_zero_amount_fees.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

class DropZeroAmountFees < ActiveRecord::Migration[7.1]
disable_ddl_transaction!

def change
sql = <<~SQL
DELETE FROM fees
USING invoices
WHERE
fees.invoice_id = invoices.id
AND invoices.status IN (1, 2, 6) -- finalized, voided and closed
AND fees.fee_type = 0 -- charge
AND fees.amount_cents = 0
AND fees.units = 0
AND fees.pay_in_advance = false
AND fees.true_up_parent_fee_id IS NULL
AND fees.id NOT IN (
SELECT f.true_up_parent_fee_id
FROM fees f
WHERE f.true_up_parent_fee_id IS NOT NULL
)
AND fees.id NOT IN (
SELECT fee_id
FROM adjusted_fees
WHERE adjusted_fees.fee_id IS NOT NULL
)
SQL

ApplicationRecord.connection.execute(sql)
end
end
2 changes: 1 addition & 1 deletion db/schema.rb

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

0 comments on commit 783317c

Please sign in to comment.