-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathwpo-ips-functions-ubl.php
116 lines (96 loc) · 3.63 KB
/
wpo-ips-functions-ubl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/*
|--------------------------------------------------------------------------
| UBL Document global functions
|--------------------------------------------------------------------------
*/
/**
* Sanitizes a string for use in UBL documents by stripping all HTML tags and decoding HTML entities to plain text.
*
* @param string $string
*
* @return string
*/
function wpo_ips_ubl_sanitize_string( string $string ): string {
$string = wp_strip_all_tags( $string );
return htmlspecialchars_decode( $string, ENT_QUOTES );
}
/**
* Get UBL tax data from fallback
*
* @param string $key Can be category, scheme, or reason
* @param int|null $rate_id The tax rate ID
* @param \WC_Abstract_Order|null $order The order object
* @return string
*/
function wpo_ips_ubl_get_tax_data_from_fallback( string $key, ?int $rate_id, ?\WC_Abstract_Order $order ): string {
$result = '';
if ( ! in_array( $key, array( 'category', 'scheme', 'reason' ) ) ) {
return $result;
}
$tax_rate_class = '';
$ubl_tax_settings = get_option( 'wpo_wcpdf_settings_ubl_taxes', array() );
if ( ! is_null( $rate_id ) && class_exists( '\WC_TAX' ) && is_callable( array( '\WC_TAX', '_get_tax_rate' ) ) ) {
$tax_rate = \WC_Tax::_get_tax_rate( $rate_id, OBJECT );
if ( ! empty( $tax_rate ) && is_numeric( $tax_rate->tax_rate ) ) {
$result = isset( $ubl_tax_settings['rate'][ $tax_rate->tax_rate_id ][ $key ] ) ? $ubl_tax_settings['rate'][ $tax_rate->tax_rate_id ][ $key ] : '';
$tax_rate_class = $tax_rate->tax_rate_class;
}
}
if ( empty( $tax_rate_class ) ) {
$tax_rate_class = 'standard';
}
if ( empty( $result ) || 'default' === $result ) {
$result = isset( $ubl_tax_settings['class'][ $tax_rate_class ][ $key ] ) ? $ubl_tax_settings['class'][ $tax_rate_class ][ $key ] : '';
}
// check if order is tax exempt
if ( wpo_wcpdf_order_is_vat_exempt( $order ) ) {
switch ( $key ) {
case 'scheme':
$result = 'VAT';
break;
case 'category':
$result = 'AE';
break;
case 'reason':
$result = 'VATEX-EU-AE';
break;
}
$result = apply_filters( 'wpo_ips_ubl_get_tax_data_from_fallback_vat_exempt', $result, $key, $rate_id, $order );
}
return $result;
}
/**
* Save UBL order taxes
*
* @param \WC_Abstract_Order $order
* @return void
*/
function wpo_ips_ubl_save_order_taxes( \WC_Abstract_Order $order ): void {
foreach ( $order->get_taxes() as $item_id => $tax_item ) {
if ( is_a( $tax_item, '\WC_Order_Item_Tax' ) && is_callable( array( $tax_item, 'get_rate_id' ) ) ) {
// get tax rate id from item
$tax_rate_id = $tax_item->get_rate_id();
// read tax rate data from db
if ( class_exists( '\WC_TAX' ) && is_callable( array( '\WC_TAX', '_get_tax_rate' ) ) ) {
$tax_rate = \WC_Tax::_get_tax_rate( $tax_rate_id, OBJECT );
if ( ! empty( $tax_rate ) && is_numeric( $tax_rate->tax_rate ) ) {
// store percentage in tax item meta
wc_update_order_item_meta( $item_id, '_wcpdf_rate_percentage', $tax_rate->tax_rate );
$ubl_tax_settings = get_option( 'wpo_wcpdf_settings_ubl_taxes', array() );
$tax_fields = array( 'category', 'scheme', 'reason' );
foreach ( $tax_fields as $field ) {
$value = isset( $ubl_tax_settings['rate'][ $tax_rate->tax_rate_id ][ $field ] ) ? $ubl_tax_settings['rate'][ $tax_rate->tax_rate_id ][ $field ] : '';
if ( empty( $value ) || 'default' === $value ) {
$value = wpo_ips_ubl_get_tax_data_from_fallback( $field, $tax_rate_id, $order );
}
wc_update_order_item_meta( $item_id, '_wcpdf_ubl_tax_' . $field, $value );
}
}
}
}
}
}