-
-
Notifications
You must be signed in to change notification settings - Fork 707
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
Include vouchers in report: Enterprise Fees With Tax Report By Order #11985
Include vouchers in report: Enterprise Fees With Tax Report By Order #11985
Conversation
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.
Good work!
return BigDecimal(0) unless (voucher_adjustment = voucher_adjustments.take) | ||
|
||
voucher = voucher_adjustment.originator |
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.
I wonder if this can be better expressed as AR association. has_many :vouchers, through: :voucher_adjustments
should work. Maybe even has_one
?
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.
Yes it's the best scenario for the situation at hand.
I'd go for the has_many because this would help us incorporate multiple vouchers if in future we have this feature without making any schema level changes.
We can have an issue to incorporate this comment and use this relationship wherever required. What do you say?
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.
I think it's fine to do it in this issue. I don't think we had a need for it so far so that's probably why it hasn't been done, and yes we should use has_many
.
# @return [BigDecimal] The rate of the voucher if applied to the order | ||
def applied_voucher_rate | ||
# As an order can have only one voucher, hence using +take+ because each voucher adjustment will have the same voucher | ||
return BigDecimal(0) unless (voucher_adjustment = voucher_adjustments.take) |
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.
Assignment within a conditional is hard to read. I would split this.
|
||
def apply_voucher_on_amount(order, amount) | ||
rate = order.applied_voucher_rate | ||
amount + (amount*rate) |
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.
Mathematically, this can be simplified to amount * (1 + rate)
.
@@ -1544,4 +1544,50 @@ def advance_to_delivery_state(order) | |||
expect(order.voucher_adjustments).to eq(expected_adjustments) | |||
end | |||
end | |||
|
|||
describe '#applied_voucher_rate' do |
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.
For future reference: it's better to include the spec in the same commit as the code change needing the spec.
def apply_voucher_on_amount(order, amount) | ||
rate = order.applied_voucher_rate | ||
result = amount + (amount * rate) | ||
BigDecimal(result.to_s).round(2, BigDecimal::ROUND_HALF_UP) |
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.
It's a shame that we have to round. Unfortunately, there's no better way with the current data structure of vouchers. But I'm pretty sure that we will have rounding errors one day.
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.
Yeah I was also thinking that we might face the data round off issues. I was wondering how we can avoid this thing. 😅
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.
The only solution I can think of is a complete re-modelling of the voucher adjustments. Instead of adjusting the whole order, we could adjust each line item. The total of all voucher adjustments could still be different to the rate applied to the whole order but that's easier to explain. It could also be easier to use those adjustments in reports like this one. But it's a big chunk of work that I wouldn't start right now. Gaetan and Lynne also expressed their concerns over starting this work while there's not big reason for it.
|
||
it 'returns the BigDecimal 0 value' do | ||
actual = order.applied_voucher_rate | ||
expect(actual.class).to eq(BigDecimal) |
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.
I would use be_kind_of(BigDecimal)
matcher, but this works as well.
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.
TIL: be_kind_of
matcher. Thanks :)
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.
There's also be_a
doing the same.
# As an order can have only one voucher, | ||
# hence using +take+ as each voucher adjustment will have the same voucher |
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.
Note for next time, it's better to fix Rubocop issue in a separate commit.
return BigDecimal(0) unless (voucher_adjustment = voucher_adjustments.take) | ||
|
||
voucher = voucher_adjustment.originator |
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.
I think it's fine to do it in this issue. I don't think we had a need for it so far so that's probably why it hasn't been done, and yes we should use has_many
.
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.
It looks like consensus is to change to an AR association like has_many :vouchers, through: :voucher_adjustments
adjustment_ids = enterprise_fee_adjustment_ids(query_result_row) | ||
query = Spree::Adjustment.tax | ||
query = query.where(included: true) unless included.nil? | ||
query = query.where(originator_id: tax_rate_id(query_result_row)) unless all == true | ||
query.where(order_id:) | ||
tax_amount = query.where(order_id:) |
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.
We could actually remove the order_id
variable and refer directly to order
. AR automatically uses the ID anyway.
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.
That's great! 😍
It's updated here: 1d44a3f
Just to re-iterate, this should be handled in a separate issue as we may need to replace code in many other places which might be out of the issue's scope. Thanks. :) |
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.
Ok, I didn't realise it was a big change.
Hi @chahmedejaz, To calculate the numbers (maybe I'm wrong):
Can you find out where those additional 8 ct. in the report are coming from? I don't think it's correct. But maybe you can convince me that it is. 😉 I will move this back to In Dev so you can check this is more detail. Thanks! |
- For applied_voucher_rate method in order
- Fix lint issues
0c0c9c7
to
ff48825
Compare
Hey @drummer83 - Sorry for getting back on this late. Yeah, you are right. I've fixed this issue. It should be good now. Hey @dacook, @mkllnk - Can you please review this small fix: ff48825 Thanks ❤️ |
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.
Great work on picking that up Konrad, and for fixing it Ahmed!
Thanks, @chahmedejaz! Yes, it's looking good now. Same orders, same report, correct numbers. 👍 Merging! 🎉 |
What? Why?
What should we test?
Release notes
Changelog Category (reviewers may add a label for the release notes):
The title of the pull request will be included in the release notes.