Skip to content

Conversation

gwharton
Copy link
Contributor

@gwharton gwharton commented Oct 24, 2017

The current UPS Module uses the UPS Rate excluding TAX to use as the rate charged to the customer during checkout.

This modification adds an additional config option so you can choose whether to request and include TAX information from UPS in the rate or not.

This modification is especially useful for businesses who are not TAX registered and cannot claim back the TAX charged by UPS. This allows them to pass on the TAX costs to the customer as part of the overall shipping rate.

Description

Fixed Issues (if relevant)

  1. magento/magento2#<issue_number>: Issue title
  2. ...

Manual testing scenarios

  1. ...
  2. ...

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

The current UPS Module uses the UPS Rate excluding TAX to use as the rate charged to the customer during checkout.

This modification adds an additional config option so you can choose whether to request and include TAX information from UPS in the rate or not.

This modification is especially useful for businesses who are not TAX registered and cannot claim back the TAX charged by UPS. This allows them to pass on the TAX costs to the customer as part of the overall shipping rate.
@magento-cicd2
Copy link
Contributor

magento-cicd2 commented Oct 24, 2017

CLA assistant check
All committers have signed the CLA.

gwharton added 2 commits October 24, 2017 23:17
Remove blank line that was added by mistake
This pull request adds enough lines of code to the function to trigger the excessive method length check in PHPMD.

The majority of the code in the function is not mine and I do not want to change stuff for the sake of passing this check.

My goal was to make the changes with as little impact on the existing code base as possible.
@gwharton
Copy link
Contributor Author

gwharton commented Nov 8, 2017

I branched 2.2-develop for this fix, but if approved can it be merged into 2.3-develop aswell so future releases get the changes.

@okorshenko okorshenko self-assigned this Nov 21, 2017
@okorshenko okorshenko added this to the November 2017 milestone Nov 21, 2017
@okorshenko okorshenko modified the milestones: December 2017, January 2018 Jan 8, 2018
@misha-kotov
Copy link

Hi @gwharton , thanks for your contribution. A couple things:

  • Include TAX in Rate should be changed to Include Tax in Rate in the admin panel configuraitons
  • Can you clarify where in the admin panel config this new setting would appear?
  • What's name of the new response field that will be returned in addition to the existing GrandTotal ?

Changed admin panel label from TAX to Tax
@gwharton
Copy link
Contributor Author

The admin panel setting appears below the "Enable Negotiated Rates" setting, but only when UPS Type is set to "United Parcel Service XML". Getting TAX information (and negotiated rates) are not supported in the non XML service type.

screenshot 16

@gwharton
Copy link
Contributor Author

Here are the responses received for the following combinations with negotiated rates set

For a UK-UK shipment (taxes applicable)

Include Tax in rate = NO

    <NegotiatedRates>
      <NetSummaryCharges>
        <GrandTotal>
          <CurrencyCode>GBP</CurrencyCode>
          <MonetaryValue>9.35</MonetaryValue>
        </GrandTotal>
      </NetSummaryCharges>
    </NegotiatedRates>

Include Tax in rate = YES

    <NegotiatedRates>
      <TaxCharges>
        <Type>VAT</Type>
        <MonetaryValue>1.87</MonetaryValue>
      </TaxCharges>
      <NetSummaryCharges>
        <GrandTotal>
          <CurrencyCode>GBP</CurrencyCode>
          <MonetaryValue>9.35</MonetaryValue>
        </GrandTotal>
        <TotalChargesWithTaxes>
          <CurrencyCode>GBP</CurrencyCode>
          <MonetaryValue>11.22</MonetaryValue>
        </TotalChargesWithTaxes>
      </NetSummaryCharges>
    </NegotiatedRates>

For a UK-USA shipment (taxes NOT applicable)

Include Tax in rate = NO

    <NegotiatedRates>
      <NetSummaryCharges>
        <GrandTotal>
          <CurrencyCode>GBP</CurrencyCode>
          <MonetaryValue>44.37</MonetaryValue>
        </GrandTotal>
      </NetSummaryCharges>
    </NegotiatedRates>

Include Tax in rate = YES

    <NegotiatedRates>
      <NetSummaryCharges>
        <GrandTotal>
          <CurrencyCode>GBP</CurrencyCode>
          <MonetaryValue>44.37</MonetaryValue>
        </GrandTotal>
      </NetSummaryCharges>
    </NegotiatedRates>

@gwharton
Copy link
Contributor Author

And the same responses received without negotiated rates set

For a UK-UK shipment (taxes applicable)

Include Tax in rate = NO

    <TransportationCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>6.45</MonetaryValue>
    </TransportationCharges>
    <ServiceOptionsCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>0.00</MonetaryValue>
    </ServiceOptionsCharges>
    <TotalCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>6.45</MonetaryValue>
    </TotalCharges>

Include Tax in rate = YES

    <TransportationCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>6.45</MonetaryValue>
    </TransportationCharges>
    <ServiceOptionsCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>0.00</MonetaryValue>
    </ServiceOptionsCharges>
    <TaxCharges>
      <Type>VAT</Type>
      <MonetaryValue>1.29</MonetaryValue>
    </TaxCharges>
    <TotalCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>6.45</MonetaryValue>
    </TotalCharges>
    <TotalChargesWithTaxes>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>7.74</MonetaryValue>
    </TotalChargesWithTaxes>

For a UK-USA shipment (taxes NOT applicable)

Include Tax in rate = NO

    <TransportationCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>35.16</MonetaryValue>
    </TransportationCharges>
    <ServiceOptionsCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>0.00</MonetaryValue>
    </ServiceOptionsCharges>
    <TotalCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>35.16</MonetaryValue>
    </TotalCharges>

Include Tax in rate = YES

    <TransportationCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>35.16</MonetaryValue>
    </TransportationCharges>
    <ServiceOptionsCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>0.00</MonetaryValue>
    </ServiceOptionsCharges>
    <TotalCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>35.16</MonetaryValue>
    </TotalCharges>

@gwharton
Copy link
Contributor Author

The code change checks for the presence of the TaxCharges field. If the field is present and the option to include tax in rate is set to Yes, then the MonetaryValue of the TaxCharges field is added to the rate.

There is an alternative method to get the total including tax as if tax is applicable to the rate then the additional field "TotalChargesWithTaxes" could be used instead of summing the two fields to obtain the total including tax, but I chose not to use this option to minimise the amount of code changes. Let me know if this method is preferred and I will update.

@misha-kotov
Copy link

misha-kotov commented Jan 19, 2018

@gwharton sorry for the delay. I think this will all work, but I'm wondering if it's better for naming consistency to name "total" and "total with taxes" fields like this:

Negotiated rates = ON, Include Tax in Rate = ON
GrandTotal
GrantTotalWithTaxes -> update this name

Negotiated rates = OFF, , Include Tax in Rate = ON
TotalCharges
TotalChargesWithTaxes

One last thing: I'd prefer to change the admin panel setting name slightly. Currently the name Include Tax in Total is confusing bc it can be interpreted as "the tax value will be included in the exiting 'total' field".
I propose to call it something like Request Tax-Inclusive Rate with hint text of "When available, this value is returned in addition to totals without tax".

gwharton added 2 commits January 23, 2018 11:33
Updated admin panel label to "Request Tax-Inclusive Rate"

Added comment "When applicable, taxes (sales tax, VAT etc.) are included in the rate".

The above comment seems more appropriate than the one suggested as "When available, this value is returned in addition to totals without tax" would mean nothing to the end user. All he knows about/cares about is that the end total includes any taxes or not.
…aths.

Previously the routine obtained the base cost, and then if taxes existed, it added the tax on to get the total.

Now the function checks for the presence of the "TotalChargesWithTaxes" field. If present and the "Request Tax-Inclusive Rate" option is set, then the total is obtained from the "TotalChargesWithTaxes" field. If not present, or the "Request Tax-Inclusive Rate" option is not set, then the total is obtained from the original field which does not include tax.

Also moved the setting of the "responseCurrencyCode" variable into the same logic. This ensures that the correct response currency code is always set correctly based on the value returned from UPS. Previously "responseCurrencyCode" would only have ever been set correctly if negotiated rates were never used.
@gwharton
Copy link
Contributor Author

@misha-kotov I have updated the labelling on the admin panel to be inline with your comments, although I have proposed a slightly different comment to the suggested one.

I have also changed the logic to get rid of the maths. There is no point in obtaining the base charge, then the taxes and adding the two together, when the total including tax is also returned from UPS and can be used directly.

I have also fixed a problem with the setting of the responseCurrencyCode which was not working correctly when negotiated rates were used, and was made worse by my original commit.

With regard to the naming of the fields in the response, these are the field names that are returned by UPS in their API and are beyond our control I am afraid. They're not consistent, but we are stuck with them.

Updated admin panel label and comment in i18n
@okorshenko
Copy link
Contributor

Hi @gwharton thank you for update. Could you please cover your improvement with integration tests so that we can proceed with PR acceptance
Thank you

@misha-kotov
Copy link

@gwharton thanks for your edits. Now that you changed the math calculations, can you provide examples of how it looks now? The 4 combinations/examples you gave above were very useful.

@gwharton
Copy link
Contributor Author

@misha-kotov There are 8 possible routes through the code. I will summarise all 8 here.

Option 1

Negotiated Rates Option set to "NO"
Request Tax Inclusive Rate Option set to "NO"
Shipping source and destination means tax is applicable to shipment (ex UK -> UK)

XML Snippet received back from UPS Servers

    <TransportationCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>6.45</MonetaryValue>
    </TransportationCharges>
    <ServiceOptionsCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>0.00</MonetaryValue>
    </ServiceOptionsCharges>
    <TotalCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>6.45</MonetaryValue>
    </TotalCharges>

In this combination

$cost is set from TotalCharges->MonetaryValue
$responseCurrencyCode is set from TotalCharges->CurrencyCode

Option 2

Negotiated Rates Option set to "NO"
Request Tax Inclusive Rate Option set to "NO"
Shipping source and destination means NO tax is applicable to shipment (ex UK -> USA)

XML Snippet received back from UPS Servers

    <TransportationCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>35.16</MonetaryValue>
    </TransportationCharges>
    <ServiceOptionsCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>0.00</MonetaryValue>
    </ServiceOptionsCharges>
    <TotalCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>35.16</MonetaryValue>
    </TotalCharges>

In this combination

$cost is set from TotalCharges->MonetaryValue
$responseCurrencyCode is set from TotalCharges->CurrencyCode

Option 3

Negotiated Rates Option set to "NO"
Request Tax Inclusive Rate Option set to "YES"
Shipping source and destination means tax is applicable to shipment (ex UK -> UK)

XML Snippet received back from UPS Servers

    <TransportationCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>6.45</MonetaryValue>
    </TransportationCharges>
    <ServiceOptionsCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>0.00</MonetaryValue>
    </ServiceOptionsCharges>
    <TaxCharges>
      <Type>VAT</Type>
      <MonetaryValue>1.29</MonetaryValue>
    </TaxCharges>
    <TotalCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>6.45</MonetaryValue>
    </TotalCharges>
    <TotalChargesWithTaxes>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>7.74</MonetaryValue>
    </TotalChargesWithTaxes>

In this combination

$cost is set from TotalChargesWithTaxes->MonetaryValue
$responseCurrencyCode is set from TotalChargesWithTaxes->CurrencyCode

Option 4

Negotiated Rates Option set to "NO"
Request Tax Inclusive Rate Option set to "YES"
Shipping source and destination means NO tax is applicable to shipment (ex UK -> USA)

XML Snippet received back from UPS Servers

    <TransportationCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>35.16</MonetaryValue>
    </TransportationCharges>
    <ServiceOptionsCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>0.00</MonetaryValue>
    </ServiceOptionsCharges>
    <TotalCharges>
      <CurrencyCode>GBP</CurrencyCode>
      <MonetaryValue>35.16</MonetaryValue>
    </TotalCharges>

In this combination

$cost is set from TotalCharges->MonetaryValue
$responseCurrencyCode is set from TotalCharges->CurrencyCode

Option 5

Negotiated Rates Option set to "YES"
Request Tax Inclusive Rate Option set to "NO"
Shipping source and destination means tax is applicable to shipment (ex UK -> UK)

XML Snippet received back from UPS Servers

    <NegotiatedRates>
      <NetSummaryCharges>
        <GrandTotal>
          <CurrencyCode>GBP</CurrencyCode>
          <MonetaryValue>9.35</MonetaryValue>
        </GrandTotal>
      </NetSummaryCharges>
    </NegotiatedRates>

In this combination

$cost is set from NegotiatedRates->NetSummaryCharges->GrandTotal->MonetaryValue
$responseCurrencyCode is set from NegotiatedRates->NetSummaryCharges->GrandTotal->CurrencyCode

Option 6

Negotiated Rates Option set to "YES"
Request Tax Inclusive Rate Option set to "NO"
Shipping source and destination means NO tax is applicable to shipment (ex UK -> USA)

XML Snippet received back from UPS Servers

    <NegotiatedRates>
      <NetSummaryCharges>
        <GrandTotal>
          <CurrencyCode>GBP</CurrencyCode>
          <MonetaryValue>44.37</MonetaryValue>
        </GrandTotal>
      </NetSummaryCharges>
    </NegotiatedRates>

In this combination

$cost is set from NegotiatedRates->NetSummaryCharges->GrandTotal->MonetaryValue
$responseCurrencyCode is set from NegotiatedRates->NetSummaryCharges->GrandTotal->CurrencyCode

Option 7

Negotiated Rates Option set to "YES"
Request Tax Inclusive Rate Option set to "YES"
Shipping source and destination means tax is applicable to shipment (ex UK -> UK)

XML Snippet received back from UPS Servers

    <NegotiatedRates>
      <TaxCharges>
        <Type>VAT</Type>
        <MonetaryValue>1.87</MonetaryValue>
      </TaxCharges>
      <NetSummaryCharges>
        <GrandTotal>
          <CurrencyCode>GBP</CurrencyCode>
          <MonetaryValue>9.35</MonetaryValue>
        </GrandTotal>
        <TotalChargesWithTaxes>
          <CurrencyCode>GBP</CurrencyCode>
          <MonetaryValue>11.22</MonetaryValue>
        </TotalChargesWithTaxes>
      </NetSummaryCharges>
    </NegotiatedRates>

In this combination

$cost is set from NegotiatedRates->NetSummaryCharges->TotalChargesWithTaxes->MonetaryValue
$responseCurrencyCode is set from NegotiatedRates->NetSummaryCharges->TotalChargesWithTaxes->CurrencyCode

Option 8

Negotiated Rates Option set to "YES"
Request Tax Inclusive Rate Option set to "YES"
Shipping source and destination means NO tax is applicable to shipment (ex UK -> USA)

XML Snippet received back from UPS Servers

    <NegotiatedRates>
      <NetSummaryCharges>
        <GrandTotal>
          <CurrencyCode>GBP</CurrencyCode>
          <MonetaryValue>44.37</MonetaryValue>
        </GrandTotal>
      </NetSummaryCharges>
    </NegotiatedRates>

In this combination

$cost is set from NegotiatedRates->NetSummaryCharges->GrandTotal->MonetaryValue
$responseCurrencyCode is set from NegotiatedRates->NetSummaryCharges->GrandTotal->CurrencyCode

I'll take a look at the integration tests now, and make sure all 8 routes are covered by a test.

@magento-engcom-team
Copy link
Contributor

@gwharton thank you for contributing. Please accept Community Contributors team invitation here to gain extended permissions for this repository.

Unit tests added to cover all 8 possible options.
@gwharton
Copy link
Contributor Author

@okorshenko I have added unit tests to cover the code changes. The unit tests use actual live XML response files obtained from the UPS servers, and are injected/fed through the xml parsing routines via the normal collectRates method with the various options set to cover all of the above 8 scenareos.

gwharton added 2 commits January 31, 2018 11:07
This reverts commit 837b5fa.
Unit tests added to cover all 8 possible options. (That work this time)
@gwharton gwharton requested a review from viktym January 31, 2018 12:43
gwharton and others added 3 commits January 31, 2018 15:05
@okorshenko okorshenko modified the milestones: January 2018, February 2018 Feb 7, 2018
@gwharton gwharton removed the request for review from viktym February 8, 2018 15:07
@okorshenko okorshenko modified the milestones: February 2018, March 2018 Mar 1, 2018
magento-engcom-team pushed a commit that referenced this pull request Mar 22, 2018
magento-engcom-team added a commit that referenced this pull request Mar 22, 2018
…nclude TAX in rate #14217

 - Merge Pull Request #14217 from gwharton/magento2:2.3-develop-ups
 - Merged commits:
   1. 933f478
magento-engcom-team pushed a commit that referenced this pull request Mar 22, 2018
magento-engcom-team pushed a commit that referenced this pull request Mar 22, 2018
magento-engcom-team added a commit that referenced this pull request Mar 22, 2018
Accepted Public Pull Requests:
 - #14231: [Forwardport] Add missing implementation for applySortOrder() (by @rostyslav-hymon)
 - #14229: [Forwardport] catalog:images:resize CLI command fix (by @rostyslav-hymon)
 - #14228: [Forwardport] fix incorrect phpdoc return type (by @rostyslav-hymon)
 - #14227: [Forwardport] Use `^1.4` for `composer/composer` (by @rostyslav-hymon)
 - #14225: [Forwardport] Send order email for Braintree Paypal orders (by @dimonovp)
 - #14223: [Forwardport] SQL query is printed into browser in case of exception (by @rostyslav-hymon)
 - #14217: [Forwardport] of PR-#11707 to 2.3-develop UPS Option to include TAX in rate (by @gwharton)


Fixed GitHub Issues:
 - #13631: Totals sort order is not respected in customer account order view (reported by @schmengler) has been fixed in #14231 by @rostyslav-hymon in 2.3-develop branch
   Related commits:
     1. a7ff305

 - #12792: [2.1.10] No order confirmation email after paying with PayPal Express (reported by @aeu) has been fixed in #14225 by @dimonovp in 2.3-develop branch
   Related commits:
     1. 413b898
     2. 2b64618

 - #13778: Braintree Paypal Method No Order Confirmation Email Sent (reported by @snoroozi) has been fixed in #14225 by @dimonovp in 2.3-develop branch
   Related commits:
     1. 413b898
     2. 2b64618

 - #13385: SQL query is printed into browser in case of exception (reported by @alexkuk) has been fixed in #14223 by @rostyslav-hymon in 2.3-develop branch
   Related commits:
     1. f618e14
@magento-engcom-team magento-engcom-team merged commit e58eaac into magento:2.2-develop Mar 23, 2018
@gwharton gwharton deleted the 2.2-develop-ups branch March 23, 2018 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants