Skip to content

Commit

Permalink
charge transport cost as well when accounting order (fix foodcoops#861
Browse files Browse the repository at this point in the history
…after f7c7b56)
  • Loading branch information
twothreenine authored and paroga committed Feb 18, 2022
1 parent 023ab07 commit a141cc7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def charge_group_orders!(user, transaction_type = nil)
note = transaction_note
group_orders.includes(:ordergroup).each do |group_order|
if group_order.ordergroup
price = group_order.price * -1 # decrease! account balance
price = group_order.total * -1 # decrease! account balance
group_order.ordergroup.add_financial_transaction!(price, note, user, transaction_type, nil, group_order)
end
end
Expand Down
26 changes: 26 additions & 0 deletions spec/models/order_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require_relative '../spec_helper'

describe Order do
let!(:ftt) { create :financial_transaction_type }
let(:user) { create :user, groups: [create(:ordergroup)] }

it 'automaticly finishes ended' do
Expand Down Expand Up @@ -151,4 +152,29 @@
expect(orders[1][:group_order]).to be_nil
end
end

describe 'balancing charges correct amounts' do
let!(:transport) { rand(0.1..26.0).round(2) }
let!(:order) { create :order, article_count: 1 }
let!(:oa) { order.order_articles.first }
let!(:go) { create :group_order, order: order, transport: transport }
let!(:goa) { create :group_order_article, group_order: go, order_article: oa, quantity: 1 }

before do
goa.update_quantities(1, 0)
go.reload
go.update_price!
user.ordergroup.update_stats!
oa.update_results!

order.finish!(user)
order.reload
order.close!(user, ftt)
end

it 'creates financial transaction with correct amount' do
expect(goa.result).to be > 0
expect(go.ordergroup.financial_transactions.last.amount).to eq(-go.total)
end
end
end

0 comments on commit a141cc7

Please sign in to comment.