diff --git a/app/interactors/spree_cm_commissioner/subscriptions_order_creator.rb b/app/interactors/spree_cm_commissioner/subscriptions_order_creator.rb index bbb778a4b..bfe086740 100644 --- a/app/interactors/spree_cm_commissioner/subscriptions_order_creator.rb +++ b/app/interactors/spree_cm_commissioner/subscriptions_order_creator.rb @@ -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) diff --git a/spec/controllers/spree/billing/report_controller_spec.rb b/spec/controllers/spree/billing/report_controller_spec.rb index 5cf0ee2d8..c4827d7e3 100644 --- a/spec/controllers/spree/billing/report_controller_spec.rb +++ b/spec/controllers/spree/billing/report_controller_spec.rb @@ -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 @@ -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 diff --git a/spec/interactors/spree_cm_commissioner/subscriptions_order_creator_spec.rb b/spec/interactors/spree_cm_commissioner/subscriptions_order_creator_spec.rb index 26ea6224d..7e3c569c2 100644 --- a/spec/interactors/spree_cm_commissioner/subscriptions_order_creator_spec.rb +++ b/spec/interactors/spree_cm_commissioner/subscriptions_order_creator_spec.rb @@ -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 diff --git a/spec/queries/spree_cm_commissioner/subscription_orders_query_spec.rb b/spec/queries/spree_cm_commissioner/subscription_orders_query_spec.rb index 986112533..d9e07e11f 100644 --- a/spec/queries/spree_cm_commissioner/subscription_orders_query_spec.rb +++ b/spec/queries/spree_cm_commissioner/subscription_orders_query_spec.rb @@ -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, @@ -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,