私は既にビジネスのためのカスタム請求システム(Wireless ISP)に機能部品のほとんどを書いていますが、それをちょっと迷ってしまいました。レール請求システムの請求書への支払いの適用
基本的な概要は次のとおりです。 毎月自動的に請求書を作成して各顧客に送付する、定期的な請求システムです。しかし、私は現地の顧客と対処するため、チェック/現金支払いを許可する必要があります。また、前払いを許可する必要があるため、Stripeの定期請求のようなものを使用することはできません。本質的に、支払いは、これらのタイプの支払いを可能にする請求書と直接関連していません。
モデル/ invoice.rb
class Invoice < ActiveRecord::Base
belongs_to :customer
has_many :line_items
has_many :invoice_payments
has_many :payments, through: :invoice_payments
モデル/ payment.rb
class Payment < ActiveRecord::Base
belongs_to :customer
モデル/ customer.rb
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
としていますが、あとでlib /にある自分のモジュールにそれを抽象化する可能性があります。
これは私がモデル/ customer.rb
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
で、これまでに擬似コードを記入する何のために任意の提案を行ってきた何ですか?私はどのレベルのリファクタリングにもオープンしていますので、これを処理するためのより良い方法があると思ったら教えてください!
ブリリアント!私はそれについてそのように考えなかったし、それぞれのループで思考が固まっていた。 – gmcintire