Automattic\WooCommerce\Internal\Admin

Analytics::get_refund_double_counted_parent_ids │ private static │ WC 1.0

Get the parent orders whose refund rows add up to more than the order itself, the sign of a partial refund followed by a double-counted full refund (#66320).

Refunds are found through the parent index of the active order table, so the query only reads the refunds of the parent orders that match the condition.

Method of the class: Analytics{}

No Hooks.

Returns

int[]. Parent order IDs in ascending order.

Usage

$result = Analytics::get_refund_double_counted_parent_ids( $parent_condition, $limit ): array;
$parent_condition(string) (required)
Prepared SQL condition on the parent stats row, aliased o.
$limit(int)
Maximum number of IDs to return; 0 for no limit.

Analytics::get_refund_double_counted_parent_ids() code WC 11.3.0

private static function get_refund_double_counted_parent_ids( string $parent_condition, int $limit = 0 ): array {
	global $wpdb;

	if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
		$orders_table  = OrdersTableDataStore::get_orders_table_name();
		$id_column     = 'id';
		$parent_column = 'parent_order_id';
		$type_column   = 'type';
	} else {
		$orders_table  = $wpdb->posts;
		$id_column     = 'ID';
		$parent_column = 'post_parent';
		$type_column   = 'post_type';
	}

	$stats_table = $wpdb->prefix . 'wc_order_stats';
	$limit_sql   = $limit > 0 ? $wpdb->prepare( 'LIMIT %d', $limit ) : '';
	// Half the smallest currency unit absorbs floating-point noise but still catches a double-counted
	// smallest-unit refund. Capped at 8 decimals, beyond which DOUBLE sums get too noisy to compare.
	$tolerance_sql = sprintf( '%.10F', 0.5 / ( 10 ** min( wc_get_price_decimals(), 8 ) ) );

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table and column names are hardcoded; the condition is prepared by the caller.
	$parent_ids = $wpdb->get_col(
		"SELECT o.order_id
		FROM {$stats_table} AS o
		INNER JOIN {$orders_table} AS refund ON refund.{$parent_column} = o.order_id AND refund.{$type_column} = 'shop_order_refund'
		INNER JOIN {$stats_table} AS r ON r.order_id = refund.{$id_column}
		WHERE {$parent_condition} AND o.parent_id = 0
		GROUP BY o.order_id
		HAVING COUNT(*) > 1
			AND ABS( SUM( r.net_total + r.tax_total + r.shipping_total ) ) > MAX( o.net_total + o.tax_total + o.shipping_total ) + {$tolerance_sql}
		ORDER BY o.order_id ASC
		{$limit_sql}"
	);
	// phpcs:enable

	// Read the error before logging: the database log handler runs its own query, which clears it.
	$db_error = $wpdb->last_error;
	if ( $db_error ) {
		wc_get_logger()->error(
			sprintf( 'Double-counted refunds query failed: %s', $db_error ),
			array( 'source' => 'wc-analytics-order-import' )
		);
		throw new \Exception( $db_error ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
	}

	return array_map( 'intval', $parent_ids );
}