Skip to content

Instantly share code, notes, and snippets.

@joshfeck
Created April 16, 2020 19:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joshfeck/bc4ffb1d4417258fce3ce32e00d25ea8 to your computer and use it in GitHub Desktop.
Save joshfeck/bc4ffb1d4417258fce3ce32e00d25ea8 to your computer and use it in GitHub Desktop.
GST, HST, PST, and QST for Canada eh? Event Espresso 4.
<?php
//* Please do NOT include the opening php tag, except of course if you're starting with a blank file
/**
* PLEASE READ AND FOLLOW ALL INSTRUCTIONS IN CAPS
*
* IN ORDER FOR THIS TO WORK YOU NEED TO ACTIVATE THE "ADDRESS QUESTION GROUP"
* THEN SET THE STATE/PROVINCE QUESTION TO BE REQUIRED
*
* BECAUSE THIS QUESTION SHOULD ONLY BE ASKED ONCE PER TRANSACTION
* THE ADDRESS QUESTION GROUP SHOULD NOT BE SET FOR ADDITIONAL REGISTRANTS
* IN OTHER WORDS,
* CHECK THE BOX NEXT TO ADDRESS IN THE "Questions for Primary Registrant"
* SIDEBAR METABOX ONLY
*
*
* @return void
*/
function bc_ee_determine_whether_to_apply_surcharge() {
if ( isset( $_REQUEST[ 'ee_reg_qstn' ] ) ) {
foreach ( $_REQUEST[ 'ee_reg_qstn' ] as $registrations ) {
if ( ! empty( $registrations ) ) {
foreach ( $registrations as $QST_ID => $response ) {
if ( $QST_ID === 'state' ) {
switch ( $response ) {
case '60' :
case '70' :
case '71' :
case '72' :
// apply 5% surcharge Alberta, NWT, Nunavet, Yukon
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', '__return_true' );
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__surcharge_details', 'ee_al_north_surcharge_details' );
break;
case '61' :
case '62' :
// apply 12% surcharge BC, Manitoba
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', '__return_true' );
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__surcharge_details', 'ee_bc_mb_surcharge_details' );
break;
case '63' :
case '64' :
case '65' :
case '67' :
// apply 15% surcharge NB, NL, NS, PE
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', '__return_true' );
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__surcharge_details', 'ee_nb_nl_ns_pe_surcharge_details' );
break;
case '66' :
// apply 13% surcharge Ontario
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', '__return_true' );
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__surcharge_details', 'ee_on_surcharge_details' );
break;
case '68' :
// apply 14.975% surcharge Quebec
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', '__return_true' );
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__surcharge_details', 'ee_qc_surcharge_details' );
break;
case '69' :
// apply 11% surcharge SK
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', '__return_true' );
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__surcharge_details', 'ee_sk_surcharge_details' );
break;
default :
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', '__return_false' );
}
}
}
}
}
}
}
add_action( 'AHEE__EE_System__core_loaded_and_ready', 'bc_ee_determine_whether_to_apply_surcharge', 1 );
function ee_al_north_surcharge_details() {
return array(
'name' => 'gst',
'code' => 'gst',
'description' => 'gst 5%',
'percent' => 5,
'taxable' => false,
);
}
function ee_bc_mb_surcharge_details() {
return array(
'name' => 'gst + pst',
'code' => 'gst-pst',
'description' => 'gst 5% + pst 7%',
'percent' => 12,
'taxable' => false,
);
}
function ee_nb_nl_ns_pe_surcharge_details() {
return array(
'name' => 'hst',
'code' => 'hst',
'description' => 'hst 15%',
'percent' => 15,
'taxable' => false,
);
}
function ee_on_surcharge_details() {
return array(
'name' => 'ontario hst',
'code' => 'ontario-hst',
'description' => 'ontario hst 13%',
'percent' => 13,
'taxable' => false,
);
}
function ee_qc_surcharge_details() {
return array(
'name' => 'quebec gst + qst',
'code' => 'quebec-gst-qst',
'description' => 'quebec gst + qst 14.975%',
'percent' => 14.975,
'taxable' => false,
);
}
function ee_sk_surcharge_details() {
return array(
'name' => 'sk gst + pst',
'code' => 'sk-gst-pst',
'description' => 'gst 5% + pst 6%',
'percent' => 11,
'taxable' => false,
);
}
/**
* DO NOT EDIT ANYTHING EXCEPT DEFAULT SURCHARGE DETAILS
*
* bc_ee_apply_transaction_surcharge
*
* @param \EE_Checkout $checkout
* @return \EE_Checkout
*/
function bc_ee_apply_transaction_surcharge( EE_Checkout $checkout ) {
// DEFAULT SURCHARGE DETAILS - EDIT THIS
$surcharge_details = apply_filters(
'FHEE__bc_ee_apply_transaction_surcharge__surcharge_details',
array(
// name for surcharge that will be displayed
'name' => 'gst',
// unique code used to identify surcharge in the db
'code' => 'province-gst',
// description for line item
'description' => 'province gst',
// percentage amount
'percent' => 5,
// whether or not tax is applied on top of the surcharge
'taxable' => false,
)
);
// STOP EDITING
// apply the surcharge ?
if ( ! apply_filters( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', false ) ) {
return $checkout;
}
// verify checkout
if ( ! $checkout instanceof EE_Checkout ) {
return $checkout;
}
// verify cart
$cart = $checkout->cart;
if ( ! $cart instanceof EE_Cart ) {
return $checkout;
}
// verify grand total line item
$grand_total = $cart->get_grand_total();
if ( ! $grand_total instanceof EE_Line_Item ) {
return $checkout;
}
// has surcharge already been applied ?
$existing_surcharge = $grand_total->get_child_line_item( $surcharge_details[ 'code' ] );
if ( $existing_surcharge instanceof EE_Line_Item ) {
return $checkout;
}
EE_Registry::instance()->load_helper( 'Line_Item' );
$pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal( $grand_total );
$pre_tax_subtotal->add_child_line_item(
EE_Line_Item::new_instance( array(
'LIN_name' => $surcharge_details[ 'name' ],
'LIN_desc' => $surcharge_details[ 'description' ],
'LIN_unit_price' => 0,
'LIN_percent' => $surcharge_details[ 'percent' ],
'LIN_quantity' => NULL,
'LIN_is_taxable' => $surcharge_details[ 'taxable' ],
'LIN_order' => 0,
'LIN_total' => (float) ( $surcharge_details[ 'percent' ] * ( $pre_tax_subtotal->total() / 100 ) ),
'LIN_type' => EEM_Line_Item::type_line_item,
'LIN_code' => $surcharge_details[ 'code' ],
) )
);
$grand_total->recalculate_total_including_taxes();
return $checkout;
}
add_filter( 'FHEE__EED_Single_Page_Checkout___initialize_checkout__checkout', 'bc_ee_apply_transaction_surcharge' );
@sbourk
Copy link

sbourk commented Oct 29, 2020

exactly what I needed!

@joshfeck Since I've been using this code to charge the right taxes to the registrants, the total displayed on the invoice is not correct. It is charging the taxes twice (once with the surcharge, and a second time with actual taxes). Checkout page works, but the invoice is not adding up properly.

More information:
The surcharge is added to the subtotal, and then it adds the taxes (which were disabled on the ticket selector. Strange)
I tried to modify the code right into the invoice "message", from the admin UI, but it doesn't work well.
I looked for a snippet of code, but didn't find any.
I tried to find my way to the code of the invoice thinking I could build a function to override it, but didn't figure it out.

image

Any help would be appreciated.

@joshfeck
Copy link
Author

joshfeck commented Feb 9, 2021

Hi,

It sounds like you have the tickets set up as taxable. In this case, you'll need to use one (taxes) or the other (the code snippet that adds the surcharge). If you use both, both will be charged. Also, in Event Espresso > Prices, no tax should be set up there because the above snippet will be taking the place of any taxes set in the UI.

@sbourk
Copy link

sbourk commented Feb 9, 2021

thanks @joshfeck. you are right, that example added the taxes on the tickets, plus the script. Thanks for getting back to me.

Once I removed the tic for ticket taxes, I end up with an empty amount for HST (0%)on the invoice. Regardless if the attendee is in Quebec (GST+QST) and the HST is not calculated.
plus, the discounts are applied after the taxes calculated using the script - so it doesn't make much sense. See this example
2021-02-09 09_55_50-Window

@SiestaLOL
Copy link

We have our main address question turned off for our second payment method, in order to limit the countries we sell to for certain events.
For another set of events, we need to allow all countries, and charge taxes to Canadians. Is it possible to apply this plugin using another custom set of address questions?

@sbourk
Copy link

sbourk commented Mar 17, 2023

not calculated.
plus, the discounts are applied after the taxes calculated using the script - so it doesn't make much sense. See this example

I removed the HTML section from the UI in the INVOICE and RECEIPT.
I just figure it out.

@sbourk
Copy link

sbourk commented Mar 17, 2023

@joshfeck I found a bug possibly related to this code. When a student registers to an event and then updates their province, the code adds the new tax code to the invoice and does not remove the other one. It results in 2 tax codes on the invoice. Any idea how to fix this?
Many thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment