1

私は既にビジネスのためのカスタム請求システム(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 

で、これまでに擬似コードを記入する何のために任意の提案を行ってきた何ですか?私はどのレベルのリファクタリングにもオープンしていますので、これを処理するためのより良い方法があると思ったら教えてください!

答えて

0

は、ここで私はやるだろう(支払と請求書クラスで、いくつかのヘルパーメソッドが必要な場合があります)何:

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 
+0

ブリリアント!私はそれについてそのように考えなかったし、それぞれのループで思考が固まっていた。 – gmcintire

関連する問題