Skip to content

Instantly share code, notes, and snippets.

Avatar

Colin Gemmell pythonandchips

View GitHub Profile
View backfill_bill_items.rb
def backfill_bill_items
bills = Bill.all
progress_bar = ProgressBar.new(bills.count)
# Keeping the batch size low to reduce the time we are locking the 100 records
# for update
bills.select(
:id,
:contact_id,
View find_sent_invoice.rb
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"
@pythonandchips
pythonandchips / dupliate_invoice_references.rb
Last active Feb 17, 2021
Find duplicate invoice references
View dupliate_invoice_references.rb
require "perform_on_replica"
def find_duplicate_references
PerformOnReplica.perform_on_replica do
companies = Company.all
progress_bar = ProgressBar.new(companies.count)
companies.includes(:subscription).find_each do |company|
references = company.invoices.
group(:reference).
View delete_recurring_invoices.rb
def delete_all_recurring_invoices
progress_bar = ProgressRateLimiter.new(count: Invoice.where(type: "RecurringInvoice").count)
Invoice.where(type: "RecurringInvoice").find_each do |recurring_invoice|
recurring_invoice.destroy!
progress_bar.increment!
end
end
@pythonandchips
pythonandchips / backfill.rb
Last active Feb 10, 2021
Backfill recurring_invoice_profile_id
View backfill.rb
def backfill_recurring_invoice_profile_id
ids = RecurringInvoiceProfile.select(:id)
progress_bar = ProgressBar.new(ids.count)
RecurringInvoiceProfile.select(:id).find_each do |recurring_invoice_profile|
Invoice.where(recurring_invoice_id: recurring_invoice_profile.id).
update_all(recurring_invoice_profile_id: recurring_invoice_profile.id)
progress_bar.increment!
@pythonandchips
pythonandchips / data_table.html.erb
Created Jan 12, 2021
View component data table
View data_table.html.erb
<div class="data-table">
<%= form_with url: search_path, method: :get do |f| %>
<div class="data-table_controls">
<div class="data-table_length">
<%= f.label "page_size" do %>
Show
<%= f.select "page_size", options_for_select([10, 25, 50, 100], @collection.per_page) %>
entries
<% end %>
</div>
View gist:a676ff28a1349a4ae7f2e30993c0aa52
require "benchmark"
require "perform_on_replica"
company = Company.find(126455)
class BenchmarkExporter
include PerformOnReplica
def initialize(company)
@pythonandchips
pythonandchips / overdue_count.rb
Last active Jul 22, 2020
Get count of overdue invoice per company.
View overdue_count.rb
switch_to_slave_db
companies = Company.joins(:subscription).merge(Subscription.live)
companies_count = companies.count
progress_bar = ProgressBar.new(companies_count)
ProgressBar.new(companies_count)
companies.find_each do |company|
Rails.logger.info({
event: "COMPANY_OVERDUE_INVOICE_COUNT",
@pythonandchips
pythonandchips / gist:d40f7b2f314446a2dcfd58a7cd21e84c
Created Mar 9, 2020
Get list of negative recurring invoices
View gist:d40f7b2f314446a2dcfd58a7cd21e84c
negative_recurring_invoice_profiles = []
count = RecurringInvoiceProfile.active.count
progress = ProgressBar.new(count)
RecurringInvoiceProfile.includes(
:company,
:contact,
{recurring_invoice_profile_items: [:general_ledger_account, :company]},
).active.find_each do |recurring_invoice_profile|
@pythonandchips
pythonandchips / compare_recurring_invoices.rb
Created Dec 4, 2019
Compare recurring invoice html output
View compare_recurring_invoices.rb
class CompareRecurringInvoiceProfiles
def self.perform
recurring_invoice_count = RecurringInvoiceProfile.count
progress = ProgressBar.new(recurring_invoice_count)
RecurringInvoiceProfile.all.find_each do |recurring_invoice_profile|
render_and_compare(recurring_invoice_profile.source_recurring_invoice, recurring_invoice_profile)
progress.increment!
end
end