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

@@ -14,8 +14,8 @@ class FinalizeUserInvoices
def perform
return unless settings_are_valid?
invoices = Spree::Order.where('distributor_id = (?) AND created_at >= (?) AND created_at <= (?) AND completed_at IS NULL',
@accounts_distributor, start_date + 1.day, end_date + 1.day)
invoices = Spree::Order.where('distributor_id = (?) AND created_at >= (?) AND created_at < (?) AND completed_at IS NULL',
@accounts_distributor, start_date, end_date)
invoices.each do |invoice|
finalize(invoice)
@@ -27,7 +27,6 @@ class FinalizeUserInvoices
# we can update these to read from those preferences
invoice.payments.create(payment_method_id: Spree::Config.default_accounts_payment_method_id, amount: invoice.total)
invoice.update_attribute(:shipping_method_id, Spree::Config.default_accounts_shipping_method_id)
while invoice.state != "complete"
invoice.next
end

View File

@@ -26,13 +26,25 @@ class UpdateUserInvoices
def update_invoice_for(user, billable_periods)
current_adjustments = []
invoice = user.current_invoice
invoice = user.invoice_for(start_date, end_date)
billable_periods.reject{ |bp| bp.turnover == 0 }.each do |billable_period|
adjustment = invoice.adjustments.where(source_id: billable_period).first
adjustment ||= invoice.adjustments.new( adjustment_attrs_from(billable_period), :without_protection => true)
adjustment.update_attributes( label: adjustment_label_from(billable_period), amount: billable_period.bill )
current_adjustments << adjustment
if invoice.persisted? && invoice.created_at != start_date
Bugsnag.notify(RuntimeError.new("InvoiceDateConflict"), {
start_date: start_date,
end_date: end_date,
existing_invoice: invoice.as_json
})
elsif invoice.complete?
Bugsnag.notify(RuntimeError.new("InvoiceAlreadyFinalized"), {
invoice: invoice.as_json
})
else
billable_periods.reject{ |bp| bp.turnover == 0 }.each do |billable_period|
adjustment = invoice.adjustments.where(source_id: billable_period).first
adjustment ||= invoice.adjustments.new( adjustment_attrs_from(billable_period), :without_protection => true)
adjustment.update_attributes( label: adjustment_label_from(billable_period), amount: billable_period.bill )
current_adjustments << adjustment
end
end
clean_up_and_save(invoice, current_adjustments)
@@ -74,6 +86,8 @@ class UpdateUserInvoices
end
if current_adjustments.any?
# Invoices should be "created" at the beginning of the period to which they apply
invoice.created_at = start_date unless invoice.persisted?
invoice.save
else
Bugsnag.notify(RuntimeError.new("Empty Persisted Invoice"), {

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

View File

@@ -8,8 +8,6 @@
- @invoices.order('created_at DESC').each do |invoice|
- month = (invoice.created_at.localtime - 1.day)
- range = "#{month.beginning_of_month.strftime("%d/%m/%y")}"
- range += " - #{[month.end_of_month, Time.now].min.strftime("%d/%m/%y")}"
.row.invoice_title
.eight.columns.alpha
%h4= "#{month.strftime("%b %Y")}#{( invoice.completed_at ? '' : '*' )}"
@@ -32,11 +30,11 @@
%td= adjustment.display_amount
- invoice.adjustments.where('source_type <> (?)', "BillablePeriod").each do |adjustment|
%tr
%td= range
%td &nbsp;
%td= adjustment.label
%td= adjustment.display_amount
%tr.total
%td= range
%td &nbsp;
%td TOTAL
%td= invoice.display_total