Skip to content

Commit

Permalink
Send canceled invoice to abacus even if state was set before
Browse files Browse the repository at this point in the history
  • Loading branch information
codez committed Nov 26, 2024
1 parent 048da8d commit 159da19
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 16 deletions.
3 changes: 1 addition & 2 deletions app/jobs/invoices/abacus/cancel_invoice_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ def initialize(external_invoice)

def perform
return if !external_invoice ||
external_invoice.abacus_sales_order_key.blank? || # may not have been sent to abacus yet
external_invoice.state == "cancelled" # may have been cancelled already
external_invoice.abacus_sales_order_key.blank? # may not have been sent to abacus yet

sales_order = Invoices::Abacus::SalesOrder.new(external_invoice)
Invoices::Abacus::SalesOrderInterface.new.cancel(sales_order)
Expand Down
6 changes: 4 additions & 2 deletions app/models/concerns/events/courses/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def assigned_participants

def annul_participations
all_participants.update_all("previous_state = state, active = FALSE, state = 'annulled'")
cancel_invoices(all_course_invoices)
cancel_invoices(open_course_invoices)
end

def all_participants # also including not active
Expand All @@ -118,14 +118,16 @@ def all_participants # also including not active

def cancel_invoices(invoices)
invoices.each do |invoice|
invoice.update!(state: :cancelled)
Invoices::Abacus::CancelInvoiceJob.new(invoice).enqueue!
end
end

def all_course_invoices
def open_course_invoices
ExternalInvoice.where(
link_id: participations.select(:id),
link_type: Event::Participation.sti_name,
state: [:draft, :open, :payed],
type: [ExternalInvoice::CourseParticipation, ExternalInvoice::CourseAnnulation].map(&:sti_name)
)
end
Expand Down
11 changes: 2 additions & 9 deletions spec/jobs/invoices/abacus/cancel_invoice_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
require "spec_helper"

describe Invoices::Abacus::CancelInvoiceJob do
let(:invoice) { Fabricate(:external_invoice, person_id: people(:mitglied)) }
let(:invoice) { Fabricate(:external_invoice, person_id: people(:mitglied), state: :cancelled) }
let(:job) { described_class.new(invoice) }

context "when the job works" do
Expand All @@ -19,17 +19,10 @@
allow(Invoices::Abacus::SalesOrder).to receive(:new).with(any_args)
.and_return(sales_order)
allow(Invoices::Abacus::SalesOrderInterface).to receive(:new).and_return(sales_order_interface)
allow(sales_order_interface).to receive(:cancel).with(any_args)
.and_return(true)
end

it "does not update the invoice status" do
job.perform
expect(invoice.reload.state).to eq("open")
end

it "cancels the invoice in the abacus system" do
expect(sales_order_interface).to receive(:cancel)
expect(sales_order_interface).to receive(:cancel).and_return(true)
job.perform
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/models/event/course_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -562,14 +562,14 @@

it "does not set participation state for assigned participations" do
expect { course.update!(state: :closed) }
.not_to change { course.participations.pluck(:state) }
.not_to change { course.participations.order(:state).pluck(:state) }
end

it "sets participation state to attended for summoned participations" do
course.participations.update_all(state: :summoned)
course.update!(state: :closed)

expect(course.participations.pluck(:state)).to eq(["attended", "attended", "attended", "attended"])
expect(course.participations.order(:state).pluck(:state)).to eq(["attended", "attended", "attended", "attended"])
end

it "queues job for absent invoices for absent participants" do
Expand All @@ -592,7 +592,7 @@

it "does nothing" do
expect { course.update!(state: :ready) }
.not_to change { course.participations.pluck(:state) }
.not_to change { course.participations.order(:state).pluck(:state) }
end
end

Expand Down

0 comments on commit 159da19

Please sign in to comment.