Skip to content

Instantly share code, notes, and snippets.

@ziyadm
Last active August 29, 2015 14:26
Show Gist options
  • Save ziyadm/cf4958b8318f2716c07f to your computer and use it in GitHub Desktop.
Save ziyadm/cf4958b8318f2716c07f to your computer and use it in GitHub Desktop.
updated_clean_accounting_data
def create_projected_cost_from_cost!(cost)
cost.flip.costs.create!(
amount_cents: cost.amount_cents,
description: cost.description,
source: cost.source,
creator: cost.creator,
account: cost.account,
kind: :projected
)
end
handled_flip_ids = []
errored_flip_ids = []
# for each properly generataed uea, take each budget limit and make them projected costs
uea_flip_ids = Flip.joins(:unit_economics).where(unit_economics: { state: :approved }).joins(:budget_limits).pluck(:id).uniq; nil
flips = Flip.where(id: uea_flip_ids).includes(:budget_limits, :unit_economics).each do |flip|
begin
Flip.transaction do
flip.budget_limits.each do |budget_limit|
flip.costs.create!(
amount_cents: budget_limit.amount_cents,
description: "[UEA] #{budget_limit.account.to_s.gsub('.', ' ').humanize}",
source: flip.unit_economics.first,
creator: flip.unit_economics.first.approver,
account: budget_limit.account,
kind: :projected,
)
end
end
handled_flip_ids.push(flip.id)
rescue => e
errored_flip_ids.push(flip.id)
end
end; nil
# for cost items that are renovations OR repairs
# and that are budget items or budget adjustments
reno_flip_ids = Cost.where('account like ? or account like ?', 'renovations%', 'repairs%').where.not(kind: [:actual]).where.not(flip_id: uea_flip_ids).pluck(:flip_id).uniq; nil
flips = Flip.where(id: reno_flip_ids).includes(:costs).each do |flip|
print '.'
begin
Flip.transaction do
costs_by_bucket = flip.costs.to_a.select { |cost| [:budgeted, :budget_adjustment].include?(cost.kind) }.group_by { |c| c.account.split('.').first }
costs_by_bucket = costs_by_bucket.slice('renovations', 'repairs', 'repair')
costs_to_consider = costs_by_bucket.values.flatten
costs_by_kind = costs_to_consider.group_by(&:kind)
if costs_by_kind[:budget_adjustment].blank? && costs_to_consider.select { |c| c.source_type == 'WorkOrder' }.blank?
# move
costs_by_kind[:budgeted].each { |cost| cost.update_column(:kind, :projected) }
elsif (costs_by_kind[:budget_adjustment].present? || costs_to_consider.select { |c| c.source_type == 'WorkOrder' }.present?) && costs_by_kind[:budgeted].present?
# dup
costs_by_kind[:budgeted].each { |cost| create_projected_cost_from_cost!(cost) }
else
# manual fix is required
errored_flip_ids.push(flip.id)
end
end
handled_flip_ids.push(flip.id)
rescue
errored_flip_ids.push(flip.id)
end
end; nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment