EsiPeppol-Woocommerce/app/helpers/Woo_Helper.php
2026-01-14 14:05:23 +01:00

190 lines
6.5 KiB
PHP

<?php
namespace ESI_PEPPOL\helpers;
class PEPPOL_Woo_Helper {
public static function my_get_item_tax_details( \WC_Order_Item $item ): array {
$taxes = $item->get_taxes(); // ['total' => [rate_id => amount], 'subtotal' => ...]
$out = [];
if ( ! empty( $taxes['total'] ) ) {
foreach ( $taxes['total'] as $rate_id => $tax_amount ) {
$tax_amount = floatval( $tax_amount );
if ( $tax_amount == 0 ) continue;
$rate = \WC_Tax::_get_tax_rate( $rate_id );
$out[] = [
'rate_id' => (int) $rate_id,
'percent' => floatval( $rate['tax_rate'] ),
'name' => $rate['tax_rate_name'],
'class' => $rate['tax_rate_class'], // 'standard', 'reduced-rate', etc.
'amount' => $tax_amount,
'compound'=> (bool) $rate['tax_rate_compound'],
];
}
}
return $out;
}
public static function esi_get_order_vat_number( $order ) {
if ( is_numeric( $order ) ) {
$order = wc_get_order( $order );
}
if ( ! $order instanceof \WC_Order ) {
return '';
}
// 1) Clés les plus courantes (ajuste/complète si besoin)
$known_keys = [
'_billing_vat_number',
'billing_vat_number',
'_vat_number',
'vat_number',
'_billing_vat',
'billing_vat',
'_billing_tva',
'billing_tva',
'_tva',
'tva',
'eu_vat_number',
'_eu_vat_number',
'vat_id',
'_vat_id',
];
foreach ( $known_keys as $key ) {
$val = $order->get_meta( $key, true );
if ( ! empty( $val ) ) {
return is_string($val) ? trim($val) : $val;
}
}
// 2) Fallback: scan des meta keys contenant vat/tva
$vat_candidates = [];
foreach ( $order->get_meta_data() as $meta ) {
$k = (string) $meta->key;
if ( preg_match('/\b(vat|tva)\b/i', $k) || preg_match('/(vat|tva)/i', $k) ) {
$v = $meta->value;
if ( is_string($v) ) {
$v = trim($v);
}
if ( ! empty($v) ) {
$vat_candidates[$k] = $v;
}
}
}
// Si plusieurs candidats, tu peux choisir une règle
// Ici: si un seul => return, sinon priorise ceux qui contiennent "billing"
if ( count($vat_candidates) === 1 ) {
return reset($vat_candidates);
}
foreach ( $vat_candidates as $k => $v ) {
if ( stripos($k, 'billing') !== false ) {
return $v;
}
}
// sinon le 1er trouvé
return ! empty($vat_candidates) ? reset($vat_candidates) : '';
}
public static function esi_get_order_vat( $order ) {
if ( is_numeric($order) ) {
$order = wc_get_order($order);
}
if ( ! $order instanceof WC_Order ) {
return [
'found' => false,
'key' => null,
'value' => null,
'source' => null,
];
}
$patterns = [ 'vat','tva','btw','ust','mwst','piva','moms','mva' ];
/* ==================================================
* 1) CHECKOUT FIELDS (si présents)
* ================================================== */
if ( function_exists('WC') && WC()->checkout() ) {
$fields = WC()->checkout()->get_checkout_fields();
foreach ( $fields as $group_fields ) {
foreach ( $group_fields as $key => $def ) {
$k = strtolower($key);
foreach ( $patterns as $p ) {
if ( strpos($k, $p) !== false ) {
// tentatives de lecture meta
foreach ( [ $key, '_' . $key ] as $meta_key ) {
$val = $order->get_meta($meta_key, true);
if ( ! empty($val) ) {
return [
'found' => true,
'key' => $meta_key,
'value' => trim((string)$val),
'source' => 'checkout_field',
];
}
}
}
}
// fallback label
$label = strtolower((string)($def['label'] ?? ''));
if ( $label && preg_match('/\b(vat|tva|btw|mwst|ust|piva|moms|mva)\b/i', $label) ) {
foreach ( [ $key, '_' . $key ] as $meta_key ) {
$val = $order->get_meta($meta_key, true);
if ( ! empty($val) ) {
return [
'found' => true,
'key' => $meta_key,
'value' => trim((string)$val),
'source' => 'checkout_label',
];
}
}
}
}
}
}
/* ==================================================
* 2) SCAN META (fallback ultime)
* ================================================== */
foreach ( $order->get_meta_data() as $meta ) {
$k = strtolower((string)$meta->key);
foreach ( $patterns as $p ) {
if ( strpos($k, $p) !== false ) {
$v = $meta->value;
if ( is_string($v) ) $v = trim($v);
if ( ! empty($v) ) {
return [
'found' => true,
'key' => $meta->key,
'value' => $v,
'source' => 'meta_scan',
];
}
}
}
}
return [
'found' => false,
'key' => null,
'value' => null,
'source' => null,
];
}
}