Last active
April 25, 2022 12:49
-
-
Save sushobhan-pal/d3ed960d0bf7f9b3b9d35aae27110e50 to your computer and use it in GitHub Desktop.
Tax Collected at Source support for Indian businesses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 ?