Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use order shipments #1651

Closed
Prev Previous commit
Next Next commit
Autofix rubocop offenses in file
  • Loading branch information
sauloperez committed Jul 3, 2017
commit 34298c0ec6e3cc459a4d917aed56e9b938b291a8
69 changes: 35 additions & 34 deletions app/jobs/finalize_account_invoices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ def initialize(year = nil, month = nil)
@end_date = Time.zone.local(@year, @month) + 1.month
end

def before(job)
def before(_job)
UpdateBillablePeriods.new(year, month).perform
UpdateAccountInvoices.new(year, month).perform
end

def perform
return unless settings_are_valid?


invoice_orders = AccountInvoice.where(year: year, month: month).map(&:order)
invoice_orders.select{ |order| order.present? && order.completed_at.nil? }.each{ |order| finalize(order) }
invoice_orders
.select { |order| order.present? && order.completed_at.nil? }
.each { |order| finalize(order) }
end

# SHIPPING_METHOD
Expand All @@ -31,12 +32,12 @@ def finalize(invoice_order)
while invoice_order.state != "complete"
if invoice_order.errors.any?
Bugsnag.notify(RuntimeError.new("FinalizeInvoiceError"), {
job: "FinalizeAccountInvoices",
error: "Cannot finalize invoice due to errors",
data: {
errors: invoice_order.errors.full_messages
}
})
job: "FinalizeAccountInvoices",
error: "Cannot finalize invoice due to errors",
data: {
errors: invoice_order.errors.full_messages
}
})
break
else
invoice_order.next
Expand All @@ -49,46 +50,46 @@ def finalize(invoice_order)
def settings_are_valid?
unless end_date <= Time.zone.now
Bugsnag.notify(RuntimeError.new("InvalidJobSettings"), {
job: "FinalizeAccountInvoices",
error: "end_date is in the future",
data: {
end_date: end_date.in_time_zone.strftime("%F %T"),
now: Time.zone.now.strftime("%F %T")
}
})
job: "FinalizeAccountInvoices",
error: "end_date is in the future",
data: {
end_date: end_date.in_time_zone.strftime("%F %T"),
now: Time.zone.now.strftime("%F %T")
}
})
return false
end

unless @accounts_distributor = Enterprise.find_by_id(Spree::Config.accounts_distributor_id)
Bugsnag.notify(RuntimeError.new("InvalidJobSettings"), {
job: "FinalizeAccountInvoices",
error: "accounts_distributor_id is invalid",
data: {
accounts_distributor_id: Spree::Config.accounts_distributor_id
}
})
job: "FinalizeAccountInvoices",
error: "accounts_distributor_id is invalid",
data: {
accounts_distributor_id: Spree::Config.accounts_distributor_id
}
})
return false
end

unless @accounts_distributor.payment_methods.find_by_id(Spree::Config.default_accounts_payment_method_id)
Bugsnag.notify(RuntimeError.new("InvalidJobSettings"), {
job: "FinalizeAccountInvoices",
error: "default_accounts_payment_method_id is invalid",
data: {
default_accounts_payment_method_id: Spree::Config.default_accounts_payment_method_id
}
})
job: "FinalizeAccountInvoices",
error: "default_accounts_payment_method_id is invalid",
data: {
default_accounts_payment_method_id: Spree::Config.default_accounts_payment_method_id
}
})
return false
end

unless @accounts_distributor.shipping_methods.find_by_id(Spree::Config.default_accounts_shipping_method_id)
Bugsnag.notify(RuntimeError.new("InvalidJobSettings"), {
job: "FinalizeAccountInvoices",
error: "default_accounts_shipping_method_id is invalid",
data: {
default_accounts_shipping_method_id: Spree::Config.default_accounts_shipping_method_id
}
})
job: "FinalizeAccountInvoices",
error: "default_accounts_shipping_method_id is invalid",
data: {
default_accounts_shipping_method_id: Spree::Config.default_accounts_shipping_method_id
}
})
return false
end

Expand Down