Preventing double creation of invoices by recognising completed invoices within the specifed period

This commit is contained in:
Rob Harrington
2015-07-10 11:17:51 +08:00
parent 11c3cf5f71
commit 6cc403cd92
7 changed files with 127 additions and 55 deletions

View File

@@ -18,7 +18,6 @@ Spree.user_class.class_eval do
validate :limit_owned_enterprises
def known_users
if admin?
Spree::User.scoped
@@ -49,10 +48,9 @@ Spree.user_class.class_eval do
owned_enterprises(:reload).size < enterprise_limit
end
def current_invoice
start_of_current_billing_period = (Time.now - 1.day).beginning_of_month
existing = orders.where('distributor_id = (?) AND created_at >= (?) AND completed_at IS NULL',
Spree::Config[:accounts_distributor_id], start_of_current_billing_period).first
def invoice_for(start_date, end_date)
existing = orders.where('distributor_id = (?) AND created_at >= (?) AND created_at < (?)',
Spree::Config[:accounts_distributor_id], start_date, end_date).first
existing || orders.new(distributor_id: Spree::Config[:accounts_distributor_id])
end