我已经建立了一种机制来查找自动生成的发票,并在更改计划或将用户添加到当前结算周期的计划时使用新信息对其进行更新。它将比从现在开始记录一个月,一个季度,六个月甚至一年后更容易。这也减少了财务部门必须找到未来发票并手动更新它的负担。我希望我在代码中的评论是清楚的。
我遇到了first_manual_invoice
的障碍。我更愿意在dup
上加注clone
或manual_invoice
;但是,当代码运行第二次或第三次时,将来的发票继续除以期限频率。有没有想过重复这个记录?我不想依赖它的父母(这就是我现在所做的)。
# Finds the upcoming / unpaid system invoice.
manual_invoice = @subscription.manual_invoices.system.not_paid.latest.first
# TODO: This is very hacky. If I use dup or clone on manual_invoice, and
# when change_plan is executed, the calculation runs, regardless if
# there were any updates. Also, chaining these scopes brings back all
# of the records when using .latest... so, I end up with an invoice
# where paid_at is nil.
first_manual_invoice = @subscription.manual_invoices.system.paid.first
# Guards changing of plan from blowing up if manual_invoice.nil?
if manual_invoice
subscription_term_type = @subscription.term_type
# Iterate through the invoice's items.
first_manual_invoice.items.each do |item|
# Set new_amount by taking the new max_users quantity and multiply it
# by the previous amount. Then divide it by the previous max_users
# quantity. Finally, divide by payment frequency per term.
new_amount = ((max_users.to_i * item.amount) / item.quantity)
if subscription_term_type == 'month'
new_amount = new_amount / 12
elsif subscription_term_type == 'quarter'
new_amount = new_amount / 4
elsif subscription_term_type == 'semiannual'
new_amount = new_amount / 2
elsif subscription_term_type == 'annual'
new_amount = new_amount / 1
end
# Update the manual_invoice items with the calcuated new_amount and
# user quantity.
manual_invoice.items.first.update_attributes(quantity: max_users,
amount: new_amount)
# Update the manual_invoice.
manual_invoice.update_attributes(amount_in_cents: new_amount,
term_type: term_type,
term_start_date: term_start_date,
term_end_date: term_end_date,
due_on: term_end_date)
end
end