eventespresso / event-espresso-core

Event Espresso 4 Core for WordPress: Build an Event Ticketing Website Today!
https://eventespresso.com
GNU General Public License v3.0
122 stars 87 forks source link

Tax not displayed for additional line items #225

Closed boethius closed 6 years ago

boethius commented 7 years ago

In the EU, the tax needs to be displayed for all items, either as a correct sub total or for every item.

Example

Please see the issue here: https://sannalive.se/?ee=msg_url_trigger&snd_msgr=html&gen_msgr=html&message_type=invoice&context=purchaser&token=1-d793cec0bef06104e655e4cc97ac1549&GRP_ID=22&id=9545

AND: https://www.dropbox.com/s/ijxo2mkp79mwlso/Screenshot%202017-04-20%2019.03.01.png?dl=0

Tax is supposed to be 108 kr, not 98 kr. The total 539 is correct, but the line item list is missing the tax for 10% surcharge. 431 + 98 = 529!!!

Code

The following is the code used to add the cart_modifier


/**
 * bc_add_cart_modifier
 *
 * @param \EE_SPCO_Reg_Step $payment_options_reg_step
 * @throws \EE_Error
 */
function bc_add_cart_modifier( EE_SPCO_Reg_Step $payment_options_reg_step ) {
    // CHANGE THESE TO YOUR LIKING
    $cart_modifier_name = 'Fakturaavgift';
    $cart_modifier_amount = 10.00;
    $cart_modifier_description = '10% avgift för faktura betalning.';
    $cart_modifier_taxable = true; // or false if surcharge is not taxable
    $payment_methods_with_surcharges = array( 'invoice' );
    // get what the user selected for payment method
    $selected_method_of_payment = $payment_options_reg_step->checkout->selected_method_of_payment;
    if ( ! in_array( $selected_method_of_payment, $payment_methods_with_surcharges ) ) {
        // does not require surcharge
        return;
    }
    $cart = $payment_options_reg_step->checkout->cart;
    if ( ! $cart instanceof EE_Cart ) {
        // ERROR
        return;
    }
    $total_line_item = $cart->get_grand_total();
    if ( ! $total_line_item instanceof EE_Line_Item && ! $total_line_item->is_total() ) {
        // ERROR
        return;
    }
    EE_Registry::instance()->load_helper( 'Line_Item' );
    $success = EEH_Line_Item::add_percentage_based_item(
        $total_line_item,
        $cart_modifier_name,
        $cart_modifier_amount,
        $cart_modifier_description,
        $cart_modifier_taxable
    );
    if ( $success ) {
        $new_total   = $total_line_item->total();
        $transaction = $payment_options_reg_step->checkout->transaction;
        if ( $transaction instanceof EE_Transaction ) {
            $transaction->set_total( $new_total );
            $success = $transaction->save();
            if ( $success ) {
                /** @type EE_Registration_Processor $registration_processor */
                $registration_processor = EE_Registry::instance()->load_class( 'Registration_Processor' );
                $registration_processor->update_registration_final_prices( $transaction );
            }
        }
    }
}
add_action( "AHEE__Single_Page_Checkout__before_payment_options__process_reg_step", 'bc_add_cart_modifier', 10, 1 );

Template Shortcodes

[TAX_LINE_ITEM_LIST]

</td></tr>
<tr class="item sub-item tax-total">
    <td class="item_I">[LINE_ITEM_NAME]</td>
    <td class="item_I">[LINE_ITEM_DESCRIPTION]</td>
    <td class="item_I"></td>
    <td class="item_r">[LINE_ITEM_AMOUNT]</td>
    <td class="item_r">[LINE_ITEM_TOTAL]</td>
</tr>
<tr>
    <td colspan="5">

[ADDITIONAL_ITEM_LIST]

</td>
</tr>
<tr class="item">
    <td class="item_I">[LINE_ITEM_NAME][LINE_ITEM_TAXABLE_*]</td>
    <td class="item_I">[LINE_ITEM_DESCRIPTION]</td>
    <td class="item_I">[LINE_ITEM_QUANTITY]</td>
    <td class="item_c">[LINE_ITEM_AMOUNT]</td>
    <td class="item_r">[LINE_ITEM_TOTAL]</td>
</tr>
    <td colspan="5">
mnelson4 commented 7 years ago

hi @boethius I'm reproducing this issue. Sorry for the inconvenience there, but I discovered a few issues in our relevant code. Please try branch BUG-10677-recalculating-taxes-on-line-items-doesnt-actually-save-them, and use this modification of your code snippet (we need to explicitly recalculate the taxes). Please confirm this resolves your issue and let me know if you see any problems:

/**
 * bc_add_cart_modifier
 *
 * @param \EE_SPCO_Reg_Step $payment_options_reg_step
 * @throws \EE_Error
 */
function bc_add_cart_modifier( EE_SPCO_Reg_Step $payment_options_reg_step ) {
    // CHANGE THESE TO YOUR LIKING
    $cart_modifier_name = 'Fakturaavgift';
    $cart_modifier_amount = 10.00;
    $cart_modifier_description = '10% avgift för faktura betalning.';
    $cart_modifier_taxable = true; // or false if surcharge is not taxable
    $payment_methods_with_surcharges = array( 'invoice' );
    // get what the user selected for payment method
    $selected_method_of_payment = $payment_options_reg_step->checkout->selected_method_of_payment;
    if ( ! in_array( $selected_method_of_payment, $payment_methods_with_surcharges ) ) {
        // does not require surcharge
        return;
    }
    $cart = $payment_options_reg_step->checkout->cart;
    if ( ! $cart instanceof EE_Cart ) {
        // ERROR
        return;
    }
    $total_line_item = $cart->get_grand_total();
    if ( ! $total_line_item instanceof EE_Line_Item && ! $total_line_item->is_total() ) {
        // ERROR
        return;
    }
    $pretax_subtotal = EEH_Line_Item::get_pre_tax_subtotal($total_line_item);
    EE_Registry::instance()->load_helper( 'Line_Item' );
    $success = EEH_Line_Item::add_percentage_based_item(
        $pretax_subtotal,
        $cart_modifier_name,
        $cart_modifier_amount,
        $cart_modifier_description,
        $cart_modifier_taxable
    );
    if ( $success ) {
        $total_line_item->recalculate_total_including_taxes();
        $new_total   = $total_line_item->total();
        $transaction = $payment_options_reg_step->checkout->transaction;
        if ( $transaction instanceof EE_Transaction ) {
            $transaction->set_total( $new_total );
            $success = $transaction->save();
            if ( $success ) {
                /** @type EE_Registration_Processor $registration_processor */
                $registration_processor = EE_Registry::instance()->load_class( 'Registration_Processor' );
                $registration_processor->update_registration_final_prices( $transaction );
            }
        }
    }
}
add_action( "AHEE__Single_Page_Checkout__before_payment_options__process_reg_step", 'bc_add_cart_modifier', 10, 1 );
boethius commented 7 years ago

@mnelson4, thank you. I tried the above code, but it now omitting the line item entirely: https://www.dropbox.com/s/vapr7kcymwajh31/Screenshot%202017-04-23%2022.31.25.png?dl=0

This is however only on the receipt/invoice (where it matters), the admin shows it correctly: https://www.dropbox.com/s/g9f5hrq1i3sbrvd/Screenshot%202017-04-23%2022.40.51.png?dl=0

Not sure, do I need to update the above short codes too?

mnelson4 commented 7 years ago

@boethius is this the case for all NEW registrations? (Ie, might you be looking at an invoice for an registration made before changing code?)

I'm not reproducing what you're seeing. The final total is correct in both the invoice (see https://drive.google.com/a/eventespresso.com/file/d/0B5P8GXTvZgfMV0hQUGVKbzZEdXc/view?usp=drivesdk), receipt, and admin (see https://drive.google.com/a/eventespresso.com/file/d/0B5P8GXTvZgfMWlRrVFIyVjRzWHM/view?usp=drivesdk),

I don't think there's a problem with your shortcodes, but just to double-check you could try resetting the invoice message template (make a DB backup before you do, or a copy of all your template's settings) by going to the WP admin, then under "Event Espresso" go to "Messages", then the "Default Message Templates" tab, edit the "Invoice" template, and click "Reset Templates".

mnelson4 commented 7 years ago

ping! It would be nice to have clarification on this, as we'd like to merge these changes into EE master (as it resolve the problem from my testing) and we'd like to avoid future problems for others

boethius commented 7 years ago

@mnelson4, I'm having some difficulty with this, as I have the registered version, but the version in the branch seems to be the "decaf" one. Can you confirm that I can replace my registered version with the one from the branch?

Also, from your writing, this only solves future invoiced items, it doesn't update the tax for the line items for past orders, correct?

mnelson4 commented 7 years ago

@mnelson4, I'm having some difficulty with this, as I have the registered version, but the version in the branch seems to be the "decaf" one. Can you confirm that I can replace my registered version with the one from the branch?

Let me clarify, so you're saying the version of EE you were previously using is a "decaf" version? (ie, you got it from wordpress.org, and it doesn't contain the caffeinated folder with its added features). The branch I referred you to is a "caffeinated" version (contains the caffeinated folder) and has more features. You can switch between caffeinated and decaf versions, but if you're familiar with git there's little reason to use decaf, especially when caf is free now.

Also, from your writing, this only solves future invoiced items, it doesn't update the tax for the line items for past orders, correct?

Yes, the fix will only work for FUTURE orders, not past ones. If you want to fix past orders, you'll need to either fix them manually in the database (wp_esp_line_item table) or write a script to do that.

mnelson4 commented 6 years ago

closing this due to inactivity. From my testing the issue was resolved and I was unable to reproduce boethius' issue.