Skip to content

Commit

Permalink
close #2122 void last invoice if penalty
Browse files Browse the repository at this point in the history
  • Loading branch information
tykealy committed Dec 6, 2024
1 parent b011684 commit 11d8c3e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ def add_penalty_to_order
amount: context.last_order.outstanding_balance + penalty_rate_amount,
label: customer.vendor.penalty_label || 'Penalty'
)

void_previous_invoice
end

def void_previous_invoice
return if context.last_order.blank?

context.last_order.payments.last.void! if context.last_order.payments.last.present?
context.last_order.update(payment_state: 'failed')
end

def generate_invoice(last_invoice_date, month, active_subscriptions)
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/spree/billing/report_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
describe 'overdue_report' do
before do
SpreeCmCommissioner::Subscription.all.each do |subscription|
subscription.orders.each {|o| o.payments.last.pend! }
subscription.orders.each {|o| o.payments.last.update(state: 'pending') }
end
end

Expand Down Expand Up @@ -49,7 +49,7 @@
describe 'failed_report' do
before do
SpreeCmCommissioner::Subscription.all.each do |subscription|
subscription.orders.each {|o| o.payments.last.void! }
subscription.orders.each {|o| o.payments.last.update(state: 'void') }
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@
expect(customer.orders.count).to eq 2
expect(last_order.total).to eq last_order.item_total + order.outstanding_balance + penalty_rate_amount
end
it "void the last order payment" do
payment_method = create(:check_payment_method)
create :cm_subscription, customer: customer, quantity: 1, start_date: today - 2.month
order = create :order , user_id: customer.user.id, total: 30, item_total: 30, state: 'complete', completed_at: today - 1.month, payment_total: 20
order.payments.create!(amount: 20, payment_method: payment_method)
customer.update!(last_invoice_date: today - 1.month)
described_class.call(customer: customer, today: today)
expect(customer.orders.count).to eq 2
expect(customer.orders.first.payment_state).to eq 'failed'
end
end
context "when customer have store credit" do
before do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
end

it 'return 0 overdues when due date == current date' do
subscription_jan2.orders.each {|o| o.payments.each{|p| p.pend! }} # pending = balance_due

query = described_class.new(
current_date: '2023-01-07'.to_date,
vendor_id: customer.vendor.id,
Expand All @@ -46,7 +44,7 @@
it 'return 1 overdues when due date < current date' do
subscription_jan2
SpreeCmCommissioner::SubscriptionsOrderCreator.call(customer: customer, today: today)
subscription_jan2.orders.each {|o| o.payments.each{|p| p.pend! }} # pending = balance_due
subscription_jan2.orders.each {|o| o.payments.each{|p| p.update(state: "pending") }}
query = described_class.new(
current_date: '2024-08-01'.to_date,
vendor_id: customer.vendor.id,
Expand Down

0 comments on commit 11d8c3e

Please sign in to comment.