-
Notifications
You must be signed in to change notification settings - Fork 71
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
Dynamic shipping tax class calculation #192
Conversation
Thanks a lot @aboritskiy! Will have a closer look ASAP. |
it looks like travis tries to work with composer 2.x, I gave a quick look through the code, I don't get why it happens cause you already have |
here is the explanation of composer failure: https://github.com/composer/composer/blob/master/CHANGELOG.md
i.e. |
Thanks @aboritskiy. Composer is not the issue, our implemented fix works. Restarted the build and we are almost green :-) Could you please fix the code style issues from https://travis-ci.com/github/firegento/firegento-magesetup2/jobs/463710485? |
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.
Thanks a lot for your valuable work, @aboritskiy! I left some comments and hope that they make sense to you :) If anything is unclear, tell me.
after a bit more testing I realized this solution is working incorrectly for b2b cases, i.e. when customer has VAT ID set and is getting VAT removed. I wasn't able to fix it right quick, cause there is not enough information in the scope of this pluin to do calculation properly. I have to rework this again. Looks like I also missed couple of edge cases with type hints, typehints are a very minor thing comparing to the b2b issue. This requires further reworking and testing, I will proceed on this the next days and either update this pull-request or submit new one. I'm not sure if we should keep this one open, for me it will help to see an overview of feedback, so I'd keep it open, but if for you, @sprankhub, it is not cool somehow - feel free to decline this pull-request. |
Thanks for your feedback, @aboritskiy! Happy to keep that open until the new version is implemented. Merry Christmas! |
ok, here is a new version, turns out I had to add another completely new feature here to let Magento substract VAT correctly for b2b customers. There is very little left from original approach to be honest. Let's see what Travis says about code style. |
look through original code-review comments and cleanup code style, providing a bit more strict, yet more meaningful typhints
also this dependency was causing an exception: report.CRITICAL: Missing required argument $country of FireGento\MageSetup\Model\Config.
otherwise the calculation doesn't have access to the quote, and doesn't work in admin section. I can't say it is nicest possible implementaion, but looking at magento core code, I don't see how to achieve the same effect better, cause magento implies that shipping tax class is a static thing.
regenerated the en_US file, using n98-magerun2.phar i18n:collect-phrases -o vendor/firegento/magesetup2/i18n/en_US.csv vendor/firegento/magesetup2/ also made sure tranlsation keys are on the matching lines between en_US and de_DE files.
in short: add a custom address total processor that will act shortly before tax total processor (see sorting order) and figure out the product tax class with highest tax rate among current cart items. Later on the calculated value is used as shipping tax class.
this commit allows proper calculation when cross-border trade is enabled. In short: * price for end-customers should stay the same no matter the tax * for business customers, VAT should be removed. * VAT amount should be calculated based on destination country default tax rate and NOT default store tax rate like in magento default. Current implementation is hacky and brings duplicaiton, this is mostly caused by the location of the code we need to override here. Target code is located in: Magento\Tax\Model\Calculation\AbstractCalculator that class is inherited by whole bunch of classes: Magento\Tax\Model\Calculation\AbstractAggregateCalculator Magento\Tax\Model\Calculation\RowBaseCalculator Magento\Tax\Model\Calculation\TotalBaseCalculator Magento\Tax\Model\Calculation\UnitBaseCalculator Vertex\Tax\Model\Calculation\VertexCalculator most interesting for us here is RowBaseCalculator and TotalBaseCalculator. We also override UnitBaseCalculator, though I'm not sure if that is needed. I do not override VertexCalculator for 2 reasons: * I didn't test this * somebody who uses Vertex platform will not bother implementing proper tax calculation in magento itself, as Vertex will calculate tax. Possible future improvement here is to rewrite implementation so that required change is not duplicated and injected either as Plugin or at least as Trait. Leaving current implementation is also considered a good option though, cause it heavily relies on initialized dependencies of `AbstractCalculator` some of those dependencies are tricky to get initialized in the plugin, e.g. `storeId` or `getAddressRateRequest()`. More generally I think this solution can be implemented best by patching magento core, author is actually surprised why magento does not support such calculation mechanics out of the box.
move total calculation override to a trait, to reduce code duplication, I'm not using plugins here cause methods that are being overriden are proteced and plugin is not possible. Also adding necessary config flags to control new features
regenerated the en_US file, using n98-magerun2.phar i18n:collect-phrases -o vendor/firegento/magesetup2/i18n/en_US.csv vendor/firegento/magesetup2/ also made sure tranlsation keys are on the matching lines between en_US and de_DE files.
remove typed properties, those are only supported by php7.4
Ok, looks like I was able to resolve all code style issues, even those that are also present in |
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.
Thanks a lot for the update, @aboritskiy! To be honest, I did not test this, but just checked the code and left some comments. If you could add some integration tests, this would be awesome. However, this is not a blocker for me to merge this. It would just help to merge this in good conscience :-)
@@ -13,5 +13,10 @@ | |||
<including_shipping_costs>0</including_shipping_costs> | |||
</price> | |||
</catalog> | |||
<tax> | |||
<calculation> |
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.
Should we add classes/dynamic_shipping_tax_class
for consistency here 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.
ehm, not really.
I placed it right next to magento's standard tax/calculation/cross_border_trade_enabled
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 I was not clear enough. My idea was to also add a default value for classes/dynamic_shipping_tax_class
. IMHO, it makes sense to define a default value for each config value.
<group id="calculation"> | ||
<field id="cross_border_trade_advanced_enabled" translate="label" type="select" sortOrder="71" showInDefault="1" showInWebsite="1" showInStore="0"> | ||
<label>Enable Cross Border Trade Advanced</label> | ||
<comment>When catalog price includes tax, enable this setting to fix the price for end-customers, but allow business customers to have VAT substracted.</comment> |
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.
This is quite a new feature. I am fine with adding it to MageSetup
, but I would also like to have the OK from @frostblogNet...
@@ -22,6 +22,8 @@ | |||
<tax__calculation__shipping_includes_tax>1</tax__calculation__shipping_includes_tax> | |||
<tax__calculation__apply_after_discount>1</tax__calculation__apply_after_discount> | |||
<tax__calculation__discount_tax>1</tax__calculation__discount_tax> | |||
<tax__calculation__cross_border_trade_enabled>0</tax__calculation__cross_border_trade_enabled> |
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 has this been added? Doesn't this mean we change this setting during our setup? I think we should not touch this during our setup, because it is not required for any market (especially not for the German market).
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.
well, I don't have strong opinion here - can remove that 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.
Please remove this for the reasons I mentioned above.
*/ | ||
public function isDynamicShippingTaxClassActive() | ||
{ | ||
return (bool)$this->scopeConfig->getValue( |
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.
Can we use isSetFlag
?
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.
any thinking behind this? we should probably change other methods as well to match that
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.
Since isSetFlag
does exactly what we want, I think it just makes sense to use it. I did not check the old code, but IMHO, we should use it where a bool
is retrieved from the config. If you would not like to change this, fine for me.
*/ | ||
public function isAdvancedCrossBorderTradeActive() | ||
{ | ||
return (bool)$this->scopeConfig->getValue( |
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.
Can we use isSetFlag
?
@aboritskiy any update on this? Thanks! |
@aboritskiy I have currently installed your pull request. Works like a charm exactly what we need. Only thing is that the shipping price is not changed with this tax class on the shipping method selection block. See screenshots: In the first screen it should also be 16,35 incl tax. |
I'm very sorry, but something not nice happened and I have to remove this from public domain. |
Please make sure these boxes are checked before submitting your PR - thank you!
Issue
This PR fixes issue # .
Proposed changes
this is rebased and reworked version of #176
major changes comparing to the original pull request:
0.4.1