-
Notifications
You must be signed in to change notification settings - Fork 150
Fix transport cost related issues #1082
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -224,13 +224,16 @@ def sum(type = :gross) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
elsif %i[groups groups_without_markup].include?(type) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for go in group_orders.includes(group_order_articles: { order_article: :article_version }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for goa in go.group_order_articles | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
when :groups | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
total += goa.result * goa.order_article.article_version.fc_group_order_price | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
when :groups_without_markup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
total += goa.result * goa.order_article.article_version.gross_group_order_price | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
when :groups | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
total += go.total | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
when :groups_without_markup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
go_price_without_markup = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for goa in go.group_order_articles | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
go_price_without_markup += goa.result * goa.order_article.article_version.gross_group_order_price | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
total += go_price_without_markup.round(2) || 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
total += go.transport if go.transport | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -273,7 +276,7 @@ def close!(user, transaction_type = nil, financial_link = nil, create_foodcoop_t | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
update_price_of_group_orders! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
transaction do # Start updating account balances | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
charge_group_orders!(user, transaction_type, financial_link) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
total_group_charges = charge_group_orders!(user, transaction_type, financial_link) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if stockit? # Decreases the quantity of stock_articles | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for oa in order_articles.includes(article_version: :article) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -285,7 +288,7 @@ def close!(user, transaction_type = nil, financial_link = nil, create_foodcoop_t | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if create_foodcoop_transaction | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ft = FinancialTransaction.new({ financial_transaction_type: transaction_type, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
user: user, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
amount: sum(:groups), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
amount: total_group_charges * -1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
note: transaction_note, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
financial_link: financial_link }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ft.save! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -405,12 +408,15 @@ def update_price_of_group_orders! | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def charge_group_orders!(user, transaction_type = nil, financial_link = nil) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
note = transaction_note | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
total = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
group_orders.includes(:ordergroup).each do |group_order| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if group_order.ordergroup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
price = group_order.total * -1 # decrease! account balance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
group_order.ordergroup.add_financial_transaction!(price, note, user, transaction_type, financial_link, group_order) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
next unless group_order.ordergroup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
price = group_order.total * -1 # decrease! account balance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
transaction = group_order.ordergroup.add_financial_transaction!(price, note, user, transaction_type, financial_link, group_order) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
total += transaction.amount | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
total | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
409
to
420
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm personally not a big fan of having a method alter stuff and returning other stuff (except for maybe the object itself). Disregarding that, if you'd like to keep it in there, I'd prefer this from a coding style perspective (untested!):
Suggested change
The I'm summing the positive However actually I'd even prefer not returning the sum here, but using this in
You'd have to test it for performance of course, but I think it shouldn't do much harm there, as caching should alleviate things and it would be much more readable IMO. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ah, I had missed that - by that you mean that the sum should actually be less precise...? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for reviewing :) I don't remember whether I had requested it ...
It is nullable, there's no Lines 219 to 221 in 02853a5
Exactly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ah, you're right, sorry - I forgot about that logic (kind of quirky how stock orders currently work 🙄 ). And I wonder if the
Okay, then how about this?: # Query shorthand method to make things more readable:
def non_stock_group_orders
group_orders.where.not(ordergroup_id: nil)
end
# Let the `charge_group_orders!` method return the `FinancialTransaction` instances themselves
# as IMO that's the least unexpected return value:
def charge_group_orders!(user, transaction_type = nil, financial_link = nil)
note = transaction_note
non_stock_group_orders.includes(:ordergroup).map do |group_order|
price = group_order.total * -1 # decrease! account balance
group_order.ordergroup.add_financial_transaction!(price, note, user, transaction_type, financial_link, group_order)
end
end
# ... then in `close!` use this to calculate the sum:
transactions = charge_group_orders!(user, transaction_type, financial_link)
# ...
amount: transactions.sum(&:amount) * -1 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again - I think you should edit your PR description to something like to make this clearer for other people reviewing:
... Maybe you could elaborate on the "elsewhere" though. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def transaction_note | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why's the
|| 0
needed here?