Created
February 17, 2021 15:50
-
-
Save pythonandchips/eba1df62ad34633b6b18ebf1e8a288ef to your computer and use it in GitHub Desktop.
Fix sent invoices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fix_sent_invoices(company_id, reference) | |
company = Company.find(company_id) | |
invoices = company.invoices.where(company_id: company_id, reference: reference) | |
return "Only #{invoices.length} found" if invoices.length < 2 | |
puts "update #{invoices.length} invoices (#{invoices.map(&:id).join(",")}) for #{company.subdomain}?" | |
confirm = gets.chomp | |
return unless confirm == "y" | |
invoices[1..].each_with_index do |invoice, index| | |
new_reference = "#{invoice.reference.strip}-FA#{index+1}" | |
invoice.update_column(:reference, new_reference) | |
puts "updated invoice #{invoice.id} reference to #{new_reference}" | |
end | |
invoices.map{|i| i.reload; [i.id, i.reference]} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment