get_settings();
if ($settings['enabled'] !== 'yes') { return; }
if (!(defined('REST_REQUEST') && REST_REQUEST)) {
add_filter('woocommerce_checkout_fields', array($this, 'inject_classic_fields'), 10);
add_action('wp_enqueue_scripts', array($this, 'enqueue_assets'));
}
add_action('woocommerce_checkout_update_order_meta', array($this, 'save_classic_to_order_meta'));
add_action('woocommerce_admin_order_data_after_billing_address', array($this, 'render_admin_order_panel'));
if (did_action('woocommerce_init') && function_exists('woocommerce_register_additional_checkout_field')) {
$this->register_block_fields();
} elseif (!did_action('woocommerce_init')) {
add_action('woocommerce_init', array($this, 'register_block_fields'), 20);
} else {
add_action('woocommerce_blocks_loaded', array($this, 'register_block_fields'));
}
add_action('woocommerce_store_api_checkout_update_order_from_request', array($this, 'sync_block_fields_to_legacy_meta'));
}
public function get_settings($raw = false) {
$stored = get_option(self::OPTION_KEY, array());
if (!is_array($stored)) { $stored = array(); }
$legacy = get_option('om_checkout_settings', array());
if (!is_array($legacy)) { $legacy = array(); }
return $raw ? (array_merge(array(
'enabled' => isset($legacy['invoicing_enabled']) ? (string) $legacy['invoicing_enabled'] : 'no',
'invoice_field_label' => isset($legacy['invoice_field_label']) ? (string) $legacy['invoice_field_label'] : '',
'invoice_yes_label' => isset($legacy['invoice_yes_label']) ? (string) $legacy['invoice_yes_label'] : '',
'invoice_no_label' => isset($legacy['invoice_no_label']) ? (string) $legacy['invoice_no_label'] : '',
'vat_field_label' => isset($legacy['vat_field_label']) ? (string) $legacy['vat_field_label'] : '',
'irs_field_label' => isset($legacy['irs_field_label']) ? (string) $legacy['irs_field_label'] : '',
'profession_field_label' => isset($legacy['profession_field_label']) ? (string) $legacy['profession_field_label'] : '',
'move_company_field' => isset($legacy['move_company_field']) ? (string) $legacy['move_company_field'] : 'no',
'aade_enabled' => isset($legacy['aade_enabled']) ? (string) $legacy['aade_enabled'] : 'no',
'aade_vat' => isset($legacy['aade_vat']) ? (string) $legacy['aade_vat'] : '',
'aade_username' => isset($legacy['aade_username']) ? (string) $legacy['aade_username'] : '',
'aade_password' => isset($legacy['aade_password']) ? (string) $legacy['aade_password'] : '',
), $stored)) : self::fill_i18n_defaults(array_merge(array(
'enabled' => isset($legacy['invoicing_enabled']) ? (string) $legacy['invoicing_enabled'] : 'no',
'invoice_field_label' => isset($legacy['invoice_field_label']) ? (string) $legacy['invoice_field_label'] : '',
'invoice_yes_label' => isset($legacy['invoice_yes_label']) ? (string) $legacy['invoice_yes_label'] : '',
'invoice_no_label' => isset($legacy['invoice_no_label']) ? (string) $legacy['invoice_no_label'] : '',
'vat_field_label' => isset($legacy['vat_field_label']) ? (string) $legacy['vat_field_label'] : '',
'irs_field_label' => isset($legacy['irs_field_label']) ? (string) $legacy['irs_field_label'] : '',
'profession_field_label' => isset($legacy['profession_field_label']) ? (string) $legacy['profession_field_label'] : '',
'move_company_field' => isset($legacy['move_company_field']) ? (string) $legacy['move_company_field'] : 'no',
'aade_enabled' => isset($legacy['aade_enabled']) ? (string) $legacy['aade_enabled'] : 'no',
'aade_vat' => isset($legacy['aade_vat']) ? (string) $legacy['aade_vat'] : '',
'aade_username' => isset($legacy['aade_username']) ? (string) $legacy['aade_username'] : '',
'aade_password' => isset($legacy['aade_password']) ? (string) $legacy['aade_password'] : '',
), $stored));
}
private static function i18n_labels() {
return array(
'invoice_field_label' => __('Do you need an invoice?', 'ai-eshop-optimizer'),
'invoice_yes_label' => __('Yes, I want an invoice', 'ai-eshop-optimizer'),
'invoice_no_label' => __('No, I want a receipt', 'ai-eshop-optimizer'),
'vat_field_label' => __('VAT number', 'ai-eshop-optimizer'),
'irs_field_label' => __('Tax office', 'ai-eshop-optimizer'),
'profession_field_label' => __('Business activity', 'ai-eshop-optimizer'),
);
}
private static function label_msgids() {
return array(
'invoice_field_label' => array('Do you need an invoice?'),
'invoice_yes_label' => array('Yes, I want an invoice'),
'invoice_no_label' => array('No, I want a receipt'),
'vat_field_label' => array('VAT number'),
'irs_field_label' => array('Tax office'),
'profession_field_label' => array('Business activity', 'Activity'),
);
}
public static function shipped_defaults() {
static $memo = null;
if (null !== $memo) { return $memo; }
$catalogues = array();
foreach ((array) glob(AIEO_DIR . 'languages/ai-eshop-optimizer-*.l10n.php') as $file) {
$data = include $file;
if (is_array($data) && isset($data['messages']) && is_array($data['messages'])) {
$catalogues[] = $data['messages'];
}
}
$memo = array();
foreach (self::label_msgids() as $key => $msgids) {
$known = array();
foreach ($msgids as $msgid) {
$known[] = $msgid;
foreach ($catalogues as $messages) {
if (isset($messages[$msgid]) && is_string($messages[$msgid]) && '' !== $messages[$msgid]) {
$known[] = $messages[$msgid];
}
}
}
$memo[$key] = array_values(array_unique(array_map('trim', $known)));
}
return $memo;
}
public static function is_shipped_default($key, $value) {
$value = trim((string) $value);
$defaults = self::shipped_defaults();
return '' !== $value && isset($defaults[$key]) && in_array($value, $defaults[$key], true);
}
public static function default_shipped_labels() {
if (get_option(self::LABELS_DEFAULTED)) { return; }
$stored = get_option(self::OPTION_KEY, array());
$stored = is_array($stored) ? $stored : array();
$legacy = get_option('om_checkout_settings', array());
$legacy = is_array($legacy) ? $legacy : array();
$patch = array();
foreach (array_keys(self::label_msgids()) as $key) {
$value = array_key_exists($key, $stored) ? (string) $stored[$key] : (string) ($legacy[$key] ?? '');
if (self::is_shipped_default($key, $value)) {
$patch[$key] = '';
}
}
if ($patch) {
update_option(self::OPTION_KEY, array_merge($stored, $patch), false);
}
update_option(self::LABELS_DEFAULTED, gmdate('Y-m-d H:i:s'), true);
}
private static function fill_i18n_defaults(array $s) {
if (!did_action('init')) { return $s; }
foreach (self::i18n_labels() as $k => $v) {
if (!isset($s[$k]) || '' === trim((string) $s[$k])) { $s[$k] = $v; }
}
return $s;
}
public function ajax_save() {
AIEO_DMM::verify_ajax_request();
$aade_password = isset($_POST['aade_password']) ? sanitize_text_field(wp_unslash($_POST['aade_password'])) : '';
if ('' === trim(str_replace("\u{2022}", '', $aade_password))) {
$aade_password = (string) ($this->get_settings(true)['aade_password'] ?? '');
}
$patch = array(
'enabled' => !empty($_POST['enabled']) ? 'yes' : 'no',
'invoice_field_label' => isset($_POST['invoice_field_label']) ? sanitize_text_field(wp_unslash($_POST['invoice_field_label'])) : '',
'invoice_yes_label' => isset($_POST['invoice_yes_label']) ? sanitize_text_field(wp_unslash($_POST['invoice_yes_label'])) : '',
'invoice_no_label' => isset($_POST['invoice_no_label']) ? sanitize_text_field(wp_unslash($_POST['invoice_no_label'])) : '',
'vat_field_label' => isset($_POST['vat_field_label']) ? sanitize_text_field(wp_unslash($_POST['vat_field_label'])) : '',
'irs_field_label' => isset($_POST['irs_field_label']) ? sanitize_text_field(wp_unslash($_POST['irs_field_label'])) : '',
'profession_field_label' => isset($_POST['profession_field_label']) ? sanitize_text_field(wp_unslash($_POST['profession_field_label'])) : '',
'move_company_field' => !empty($_POST['move_company_field']) ? 'yes' : 'no',
'aade_enabled' => !empty($_POST['aade_enabled']) ? 'yes' : 'no',
'aade_vat' => isset($_POST['aade_vat']) ? sanitize_text_field(wp_unslash($_POST['aade_vat'])) : '',
'aade_username' => isset($_POST['aade_username']) ? sanitize_text_field(wp_unslash($_POST['aade_username'])) : '',
'aade_password' => $aade_password,
);
foreach (array_keys(self::label_msgids()) as $key) {
if (self::is_shipped_default($key, $patch[$key])) {
$patch[$key] = '';
}
}
update_option(self::OPTION_KEY, $patch, false);
wp_send_json_success(array('message' => __('Invoicing settings saved.', 'ai-eshop-optimizer')));
}
public function ajax_aade_lookup() {
$s = $this->get_settings();
if ($s['aade_enabled'] !== 'yes') {
wp_send_json_error(array('message' => __('AADE lookup not enabled.', 'ai-eshop-optimizer')));
}
check_ajax_referer('om_checkout_invoice', 'nonce');
$vat = isset($_POST['vat']) ? sanitize_text_field(wp_unslash($_POST['vat'])) : '';
$vat = preg_replace('/\D+/', '', (string) $vat);
if ($vat === '' || strlen($vat) !== 9) {
wp_send_json_error(array('message' => __('Invalid VAT (must be 9 digits).', 'ai-eshop-optimizer')));
}
$username = (string) $s['aade_username'];
$password = (string) $s['aade_password'];
$called_by = (string) $s['aade_vat'];
if ($username === '' || $password === '' || $called_by === '') {
wp_send_json_error(array('message' => __('AADE credentials not configured.', 'ai-eshop-optimizer')));
}
$result = $this->call_aade_api($vat, $called_by, $username, $password);
if (isset($result['error'])) {
wp_send_json_error(array('message' => $result['error']));
}
wp_send_json_success($result);
}
private function call_aade_api($vat, $called_by, $username, $password) {
if (!class_exists('SoapClient')) {
return array('error' => __('PHP SOAP extension not available on this server.', 'ai-eshop-optimizer'));
}
$wsdl = 'https://www1.gsis.gr/webtax2/wsgsis/RgWsPublic/RgWsPublicPort?WSDL';
try {
$client = new SoapClient($wsdl, array(
'trace' => true,
'exceptions' => true,
'cache_wsdl' => WSDL_CACHE_NONE,
));
$params = array(
'INPUT_REC' => array(
'afm_called_by' => $called_by,
'afm_called_for' => $vat,
),
'OUTPUT_REC' => array(),
'pCallSeqId_out'=> 0,
'pErrorRec_out' => array(),
);
$headers = new SoapHeader(
'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd',
'Security',
new SoapVar(
'
' . esc_html__('VAT number', 'ai-eshop-optimizer') . ': ' . esc_html($vat) . '
'; } if ($irs !== '') { echo '' . esc_html__('Tax office', 'ai-eshop-optimizer') . ': ' . esc_html($irs) . '
'; } if ($store !== '') { echo '' . esc_html__('Business activity', 'ai-eshop-optimizer') . ': ' . esc_html($store) . '
'; } echo '