Last active
April 11, 2019 19:36
-
-
Save mabsboza/5895dd33b621dc5b208584da75f918c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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