Ruby on rails 在rails计费系统中对发票应用付款
我已经为我的业务(无线ISP)编写了一个定制计费系统的大部分功能部件,但有一点让我为难 以下是基本概述: 这是一个为我的客户提供的定期计费系统,每月自动生成发票并发送给每个客户。然而,我需要允许支票/现金付款,因为我与当地客户打交道,而且还需要允许预付款,这样我就不能只使用Stripe的定期账单。基本上,付款与发票不直接关联,以允许此类付款 型号/发票.rbRuby on rails 在rails计费系统中对发票应用付款,ruby-on-rails,ruby,ruby-on-rails-3,billing,recurring-billing,Ruby On Rails,Ruby,Ruby On Rails 3,Billing,Recurring Billing,我已经为我的业务(无线ISP)编写了一个定制计费系统的大部分功能部件,但有一点让我为难 以下是基本概述: 这是一个为我的客户提供的定期计费系统,每月自动生成发票并发送给每个客户。然而,我需要允许支票/现金付款,因为我与当地客户打交道,而且还需要允许预付款,这样我就不能只使用Stripe的定期账单。基本上,付款与发票不直接关联,以允许此类付款 型号/发票.rb class Invoice < ActiveRecord::Base belongs_to :customer has_ma
class Invoice < ActiveRecord::Base
belongs_to :customer
has_many :line_items
has_many :invoice_payments
has_many :payments, through: :invoice_payments
class Payment < ActiveRecord::Base
belongs_to :customer
class Customer < ActiveRecord::Base
has_many :subscriptions, dependent: :destroy
has_many :services, through: :subscriptions
has_many :invoices, :order => 'id DESC'
has_many :payments
def apply_unapplied_payments
unpaid_invoices = invoices.find_by_status("unpaid")
unapplied_payments = payments.where("invoice_id is null")
unpaid_invoices.each do |invoice|
# find the oldest unapplied payment
# apply payment to this invoice
# if payment > invoice, keep whatever amount is left and move to next invoice
# apply leftover amount to invoice, if invoice is still unpaid, move to next unapplied payment
end
customer.update_balance
end
关于如何填写伪代码有什么建议吗?我对任何级别的重构都持开放态度,所以如果你能想出更好的方法来处理这个问题,请告诉我 下面是我要做的(可能需要付款和发票类中的一些助手方法):
明亮的我没有那样想,而是陷入了思考。每一个循环。
def apply_unapplied_payments
unpaid_invoices = invoices.find_by_status("unpaid")
unapplied_payments = payments.where("invoice_id is null")
invoice = unpaid_invoices.shift
payment = unapplied_payments.shift
while invoice && invoice.remaining_balance > 0 && payment && payment.remaining_credit > 0
apply_payment_to_invoice(invoice, payment)
invoice = unpaid_invoices.shift if invoice.remaining_balance == 0
payment = unapplied_payments.shift if payment.remaining_credit == 0
end
end