Skip to content

Instantly share code, notes, and snippets.

View pythonandchips's full-sized avatar

Colin Gemmell pythonandchips

View GitHub Profile
@pythonandchips
pythonandchips / delete_recurring_invoices.rb
Last active February 17, 2021 16:24
Remove recurring invoices
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 February 10, 2021 09:23
Backfill recurring_invoice_profile_id
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 January 12, 2021 11:08
View component data table
<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>
require "benchmark"
require "perform_on_replica"
company = Company.find(126455)
class BenchmarkExporter
include PerformOnReplica
def initialize(company)
@pythonandchips
pythonandchips / overdue_count.rb
Last active July 22, 2020 13:15
Get count of overdue invoice per company.
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 March 9, 2020 15:29
Get list of negative recurring invoices
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 December 4, 2019 16:24
Compare recurring invoice html output
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
@pythonandchips
pythonandchips / fix_fcb_stock_issue.rb
Created October 14, 2019 10:34
Fix stock transaction with incorrect values due to FCB error.
def fix_fcb_stock(company)
bills = company.bills.in_foreign_currency.where.not(stock_item_id: nil)
bills.each do |bill|
company.stock_mapper.update_stock_entry(bill, force: true)
end
stock_transactions = StockTransaction
.where(stock_item_id: bills.map(&:stock_item_id))
.where("created_at >= ?", bills.map(&:created_at).min)
@pythonandchips
pythonandchips / di_fix.rb
Last active September 10, 2019 08:37
FCB DI ISSUE FIX
# This updates any bill that had incorrectly calculated vat effects
# when the bill was in foreign currency and was ether ec service or ec goods
# To be ran ater Fix PR is deployed https://github.com/fac/freeagent/pull/19751
def fix_fcb_di
bills = Bill.in_foreign_currency.where(
ec_status: [EcStatus::EC_SERVICES, EcStatus::EC_GOODS]
)
progress_bar = ProgressBar.new(bills.length)
@pythonandchips
pythonandchips / backfill_bill_currency.rb
Last active July 15, 2019 10:01
Back fill currency on bills
progress_bar = ProgressBar.new(Company.count)
Company.all.find_in_batches.each do |companies|
companies.pluck(:id, :native_currency).each do |(company_id, native_currency)|
progress_bar.increment!
Bill.where(company_id: company_id).update_all(currency: native_currency)
end
end