Obsolete BillablePeriods only deleted if their associated order is not already complete

This commit is contained in:
Rob Harrington
2015-11-19 11:27:20 +11:00
parent 199a3c38f9
commit d46712de84
2 changed files with 8 additions and 3 deletions

View File

@@ -91,10 +91,10 @@ class UpdateBillablePeriods
def clean_up_untouched_billable_periods_for(enterprise, job_start_time)
# Snag and then delete any BillablePeriods which overlap
obsolete_billable_periods = enterprise.billable_periods.where('ends_at > (?) AND begins_at < (?) AND updated_at < (?)', start_date, end_date, job_start_time)
obsolete_billable_periods = enterprise.billable_periods.where('ends_at > (?) AND begins_at < (?) AND billable_periods.updated_at < (?)', start_date, end_date, job_start_time)
if obsolete_billable_periods.any?
current_billable_periods = enterprise.billable_periods.where('ends_at >= (?) AND begins_at <= (?) AND updated_at > (?)', start_date, end_date, job_start_time)
current_billable_periods = enterprise.billable_periods.where('ends_at >= (?) AND begins_at <= (?) AND billable_periods.updated_at > (?)', start_date, end_date, job_start_time)
Delayed::Worker.logger.info "#{enterprise.name} #{start_date.strftime("%F %T")} #{job_start_time.strftime("%F %T")}"
Delayed::Worker.logger.info "#{obsolete_billable_periods.first.updated_at.strftime("%F %T")}"
@@ -105,7 +105,9 @@ class UpdateBillablePeriods
})
end
obsolete_billable_periods.each(&:delete)
obsolete_billable_periods.includes({ account_invoice: :order}).
where('spree_orders.state <> \'complete\' OR account_invoices.order_id IS NULL').
each(&:delete)
end
private