Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mabsboza/5895dd33b621dc5b208584da75f918c7 to your computer and use it in GitHub Desktop.
Save mabsboza/5895dd33b621dc5b208584da75f918c7 to your computer and use it in GitHub Desktop.
cat > UpdateExpenseOrderTotal.rb
a = load "#{Rails.root}/UpdateExpenseOrderTotal.rb"
a = UpdateExpenseOrderTotal
a.new('tenant').run
class UpdateExpenseOrderTotal
def initialize(tenant)
@tenant = tenant
end
def run
Apartment::Tenant.switch! @tenant
update_finance_order_total
end
def get_profit_centers_by_order(order_id)
Finance::ExpenseOrderProfitCenter.where(finance_expense_order_id: order_id)
end
def get_all_expense_orders_with_zero
Finance::ExpenseOrder.where(total: 0)
end
def update_finance_order_total
get_all_expense_orders_with_zero.each do |order|
profits = get_profit_centers_by_order(order.id)
total = profits.map(&:total).inject(0) { |sum, total| sum + (total.nil? ? 0 : total) }
order.update(total: total)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment