97 lines
3.0 KiB
PHP
97 lines
3.0 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) : '';
|
|
}
|
|
|
|
} |