1

저는 이미 비즈니스 (무선 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 

와 의사 코드를 입력해야하는지에 대한 어떤 제안/I가 모델에서 지금까지 한 일입니까? 나는 어떤 수준의 리팩터링도 할 수 있으므로, 이것을 처리 할 수있는 더 좋은 방법을 생각해 볼 수 있는지 알려 주시기 바랍니다!

답변

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