Problem/Motivation

A discount line item postprocess hook should be provided for clean tax/vat calculation.

@todo Fill out more detail on the problem this patch is solving.

Proposed resolution

What the provided patch does is essentially:

  • add rules-hooks to alter created discount line items

Remaining tasks

@todo

  1. Review
  2. Add hook usage to commerce_discount.api.php

User interface changes

n/a

API changes

Additional hooks.

Data model changes

Comments

geek-merlin’s picture

Status: Active » Needs review
StatusFileSize
new13.12 KB

Patch flying in.

geek-merlin’s picture

geek-merlin’s picture

Issue summary: View changes

Just to note i use this patch happily in production for months now.
Note that it isn't possible to do correct tax calculation without this (except in special cases).

haggins’s picture

I just applied the patch but I don't see any change of the behavior. Do I have to change the price calculation rules order in any way?

This is what I get now:

Product price: 100€ (including 19% tax)
Discount: 10% (= 10€)
Calculated tax on checkout: 15,97€ (should be 14,37€)

If I weight the tax calculation rule higher than the dicount rule, then I get the correct tax (14,37€) but the displayed discount is only 8,40€ instead of 10€.

geek-merlin’s picture

hi patrick, please look at the parent issue and apply all patches and install the module.
;-)
axel

haggins’s picture

Hi Axel,

thank you. Unfortunately I'm using commerce_tax and not commerce_vat. Bad luck...I'll try to test it before DC next week.

sampaka’s picture

hi. thanks a lot for your patches. I had no problem to apply the patches to the vat commerce module. But the patch here (commerce_discount-2276335-1.patch) cannot be applied to current 7.x-1.x-dev. It seems more that the current 7.x-1.0-alpha4 and not 7.x-1.x-dev is the destination file.
Thanks in advance!

geek-merlin’s picture

Issue summary: View changes
geek-merlin’s picture

StatusFileSize
new2.08 KB

OMG, forgot to attach the patch!

joelpittet’s picture

@axel.rutz this seems like the answers to my prayers... just want to confirm the intent. If I wanted to make this pricing component of the discount non-inclusive in the line item's amount would this be where I should do the modifications?

Maybe something like this could be added to the api docs?


/**
 * Implements hook_commerce_discount_line_item().
 */
function hook_commerce_discount_line_item($line_item_wrapper, $discount_name) {
  // Bail out, no components to modify.
  if (is_null($line_item_wrapper->commerce_unit_price->data->value())) {
    return;
  }
  $data = $line_item_wrapper->commerce_unit_price->data->value() + array('components' => array());
  foreach ($data['components'] as $base_key => $base_component) {
    if (strpos($base_component['name'], 'discount|') === 0) {
      $data['components'][$base_key]['included'] = FALSE;
    }
  }
  $line_item_wrapper->commerce_unit_price->data = $data;
}

The the discount $type would be a bit more useful in my case than the $discount name. Though also the discount name is strange that it's not the machine name...

joelpittet’s picture

Status: Needs review » Needs work

Testbot

joelpittet’s picture

Status: Needs work » Needs review

testbot is being nice, retest

Status: Needs review » Needs work
discipolo’s picture

Status: Needs work » Needs review
StatusFileSize
new1.64 KB

just a reroll to see if testbot likes it better.

joelpittet’s picture

I've since found a different workaround for the use-case I mentioned in #10.

This needs a reasonable use-case added to the issue summary and just a general issue summary cleanup, hopefully using the template. @see https://www.drupal.org/issue-summaries or use dreditor has a button for that.

joelpittet’s picture

Status: Needs review » Needs work

Thanks for the re-roll @discipolo

Status: Needs work » Needs review
geek-merlin’s picture

StatusFileSize
new2.64 KB

Seems a simple reroll did not suffice as the code paths have changed and we need to insert the hook in 2 places (create line item, updte line item). Updated patch flying in.

joelpittet’s picture

Issue summary: View changes
Status: Needs review » Postponed (maintainer needs more info)

@axel.rutz could you update the issue summary to explain how this is useful for VAT. Please explain how modifications in a post process hook will or could be used for VAT.

joelpittet’s picture

I've put markers and cleaned up the issue summary to help you out with the template from http://dreditor.org

geek-merlin’s picture

Status: Postponed (maintainer needs more info) » Needs review

@joel: i'm a bit (too) hasty these days and can't do it as thorough as i like it.
Everything is described in the crosslinked meta issue #2276227: [META] Use order discount with VAT

damienmckenna’s picture

Status: Needs review » Needs work

This needs documentation.

Scott Robertson’s picture

Status: Needs work » Closed (duplicate)
Related issues: +#2429595: Order discounts are not taken into account during tax calculation
vincentdemers’s picture

I've updated this patch so that it also lauch the postprocess hook after free_shipping, percent_off_shipping & shipping_upgrade price components have been added.

This was necessary for me to calculate negative tax on free_shipping price components.