def self.build_prior_month(payee, as_of = Time.current, opts = {})
start_date, end_date, year, month = effective_dates(as_of)
statement = payee.earnings_statements.build(
:year => year,
:month => month,
:balance => 0,
:revenue => 0
)
statement.generate(start_date, end_date, year, month)
statement.update_totals
statement
end
这是我在我的模型中的一个功能。它statement
正确返回,但不保存到数据库中。这是为什么?
该generate
函数如下所示:
def generate_ledger_items(start_date, end_date, year, month)
payee.ledger_entries.for_month_and_year(month, year).each do |entry|
earnings_statement_items.build(
:items => entry.item_group,
:units => entry.units,
:net_revenue => entry.net_revenue,
:net_revenue_per_unit => [entry.net_revenue, entry.units].all? ? (entry.net_revenue / entry.units).round(2) : nil,
:fees => entry.service_fees,
:payments_collected => entry.spendings,
:fee_terms => entry.terms || entry.description, # When would terms be set?
:due => entry.credit || -entry.debit
)
end
end
def update_totals
self.revenue = earnings_statement_items.net_revenue
self.balance = earnings_statement_items.total
end