Skip to content

Instantly share code, notes, and snippets.

@sushobhan-pal
Last active April 25, 2022 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sushobhan-pal/d3ed960d0bf7f9b3b9d35aae27110e50 to your computer and use it in GitHub Desktop.
Save sushobhan-pal/d3ed960d0bf7f9b3b9d35aae27110e50 to your computer and use it in GitHub Desktop.
Tax Collected at Source support for Indian businesses
<?php
add_action( 'wcfmmp_order_item_processed', function($commission_id, $order_id, $order, $vendor_id, $product_id, $order_item_id, $grosse_total, $total_commission, $is_auto_withdrawal, $commission_rule) {
global $WCFMmp, $wpdb;
$order = wc_get_order( $order_id );
if( !is_a( $order, 'WC_Order' ) ) return;
$order_item_tcs_processed = wc_get_order_item_meta( $order_item_id, '_wcfm_tcs_processed', true );
if( $order_item_tcs_processed ) return;
$line_item = new WC_Order_Item_Product( $order_item_id );
$product = $line_item->get_product();
$product_id = $line_item->get_product_id();
$variation_id = $line_item->get_variation_id();
$tcs_tax = $line_item->get_subtotal() * .01;
$total_commission = $total_commission - $tcs_tax;
$wpdb->update("{$wpdb->prefix}wcfm_marketplace_orders", array( 'total_commission' => round( $total_commission, 2 ) ), array('ID' => $commission_id), array('%s'), array('%d'));
wc_update_order_item_meta( $order_item_id, '_wcfm_tcs_processed', 1 );
$WCFMmp->wcfmmp_commission->wcfmmp_update_commission_meta( $commission_id, 'tcs_tax', $tcs_tax );
}, 10, 10);
add_action( 'wcfm_manual_order_reset', function( $order_id, $commission_order = true ) {
global $wpdb;
if( $commission_order ) {
$marketplace_orders = $wpdb->get_results( $wpdb->prepare( "SELECT ID, item_id from {$wpdb->prefix}wcfm_marketplace_orders WHERE order_id = %d", $order_id ) );
foreach( $marketplace_orders as $marketplace_order ) {
wc_delete_order_item_meta( $marketplace_order->item_id, '_wcfm_tcs_processed' );
}
}
}, 29, 2 );
add_action( 'wcfm_manual_order_processed', function( $order_id, $order_posted, $order = '' ) {
global $wpdb;
if ( ! $order_id ) return;
if ( get_post_meta( $order_id, '_wcfmmp_order_processed', true ) ) return;
if ( ! $order ) $order = wc_get_order( $order_id );
if ( ! is_a( $order, 'WC_Order' ) ) return;
$marketplace_orders = $wpdb->get_results( $wpdb->prepare( "SELECT ID, item_id from {$wpdb->prefix}wcfm_marketplace_orders WHERE order_id = %d", $order_id ) );
foreach( $marketplace_orders as $marketplace_order ) {
wc_delete_order_item_meta( $marketplace_order->item_id, '_wcfm_tcs_processed' );
}
}, 29, 3 );
add_action('wcfm_vendor_order_details_before_total_earning', function( $order_id, $vendor_id='') {
global $wpdb, $WCFMmp;
if ( ! $order_id || ! $vendor_id || ! wcfm_is_vendor($vendor_id) ) return;
$order = wc_get_order( $order_id );
if ( ! is_a( $order, 'WC_Order' ) ) return;
$order_currency = $order->get_currency();
$sql = "
SELECT GROUP_CONCAT(ID) as commission_ids,
GROUP_CONCAT(item_id) as order_item_ids,
SUM(commission_amount) as line_total,
SUM(total_commission) as total_commission,
SUM(item_total) as item_total,
SUM(item_sub_total) as item_sub_total,
SUM(shipping) as shipping,
SUM(tax) as tax,
SUM( shipping_tax_amount) as shipping_tax_amount,
SUM( refunded_amount) as refunded_amount,
SUM( discount_amount) as discount_amount
FROM {$wpdb->prefix}wcfm_marketplace_orders
WHERE order_id = " . $order_id . "
AND `vendor_id` = " . $vendor_id . "
AND `is_refunded` != 1";
$order_due = $wpdb->get_results( $sql );
if( !$order_due || !isset( $order_due[0] ) ) return;
$tcs_tax = 0;
$commission_ids = explode( ",", $order_due[0]->commission_ids );
foreach( $commission_ids as $commission_id ) {
if( method_exists( $WCFMmp->wcfmmp_commission, 'wcfmmp_get_commission_meta' ) ) {
$tcs_tax += (float) $WCFMmp->wcfmmp_commission->wcfmmp_get_commission_meta( $commission_id, 'tcs_tax' );
}
}
if(apply_filters('wcfm_always_show_tcs_amount', $tcs_tax, $order_id, $vendor_id)) {
?>
<tr>
<th class="label" colspan="2" style="text-align:right;">Tax Collected at Source (1%):</th>
<td class="total" style="text-align:center;">
<div class="view">
<?php
echo '-' .wc_price( $tcs_tax, array( 'currency' => $order_currency ) );
?>
</div>
</td>
</tr>
<?php
}
}, 10, 2);
@GNAG1993
Copy link

Hi, its working but its rounding off automatically. This tax should not round off.

@sushobhan-pal
Copy link
Author

Actually, I read in some blog that the TCS amount is always in the whole number and is rounded off to the next whole number in case of a floating value. If you could redirect me to an authenticated resource for the rounding off rule I would be happy to update the code.

Meanwhile, for your purpose, you can replace line number 16 $tcs_tax = ceil($line_item->get_subtotal() * .01); with $tcs_tax = WC_Tax::round( $line_item->get_subtotal() * .01, wc_get_price_decimals() );
Also, modify line 83 simply by removing the (int) part from the expression.

Thanks!

@GNAG1993
Copy link

Hey,
Sorry for that little confusion. You are absolutely right. Thanks for the support.
I would like to know about one more thing, do you guys provide customization service. I extremely need to customize commission & withdrawal invoices.

@sushobhan-pal
Copy link
Author

Sorry, as of now due to our existing project load, we aren't taking any new custom work.

@K-Krishna11
Copy link

Actually, I read in some blog that the TCS amount is always in the whole number and is rounded off to the next whole number in case of a floating value. If you could redirect me to an authenticated resource for the rounding off rule I would be happy to update the code.

Meanwhile, for your purpose, you can replace line number 16 $tcs_tax = ceil($line_item->get_subtotal() * .01); with $tcs_tax = WC_Tax::round( $line_item->get_subtotal() * .01, wc_get_price_decimals() );
Also, modify line 83 simply by removing the (int) part from the expression.

Thanks!

How to collect the TCS??. Is it showing Every vendor wise in the frontend?

@opsinha80
Copy link

Thank you so much. I have discussed with my legal counsel and they told me that since we're majorly dealing in microtransactions, we need to apply the rounding off on weekly/monthly basis.

For example, A vendor gets 2 orders in a week of values ₹203 and ₹220 each, then the system to deduct real 1% off, i.e. ₹2+₹2 and round them up collectively to ₹10 on weekly/monthly accumulated transactions and not per transaction basis.

Can you please help me out?

@K-Krishna11
Copy link

Thank you so much. I have discussed with my legal counsel and they told me that since we're majorly dealing in microtransactions, we need to apply the rounding off on weekly/monthly basis.

For example, A vendor gets 2 orders in a week of values ₹203 and ₹220 each, then the system to deduct real 1% off, i.e. ₹2+₹2 and round them up collectively to ₹10 on weekly/monthly accumulated transactions and not per transaction basis.

Can you please help me out?

Yes you are correct. I also need help regarding this !!

@cmoorthys
Copy link

When a variant item is purchased, first I added a black varient, then added a yellow varient of same item. Then the TCS being calculated is wrong when checked out. Is it working correct for all of you ?

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