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