Skip to content

Instantly share code, notes, and snippets.

View pythonandchips's full-sized avatar

Colin Gemmell pythonandchips

View GitHub Profile
@pythonandchips
pythonandchips / dupliate_invoice_references.rb
Last active February 17, 2021 15:42
Find duplicate invoice references
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).
@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)
def set_rates(end_date)
us_rates = [
{base_currency: "GBP", counter_currency: "USD", rate: BigDecimal("1.5480658613")},
{base_currency: "GBP", counter_currency: "USD", rate: BigDecimal("1.5522279056")},
{base_currency: "GBP", counter_currency: "USD", rate: BigDecimal("1.5473")},
{base_currency: "GBP", counter_currency: "USD", rate: BigDecimal("1.5474")},
{base_currency: "GBP", counter_currency: "USD", rate: BigDecimal("1.5535749501")},
{base_currency: "GBP", counter_currency: "USD", rate: BigDecimal("1.5484416182")},
{base_currency: "GBP", counter_currency: "USD", rate: BigDecimal("1.5566405175")},
{base_currency: "GBP", counter_currency: "USD", rate: BigDecimal("1.5520790022")},