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($username) . '' . esc_html($password) . '', XSD_ANYXML ) ); $client->__setSoapHeaders($headers); $result = $client->rgWsPublicAfmMethod($params); if (!isset($result->OUTPUT_REC)) { return array('error' => __('No data returned from AADE.', 'ai-eshop-optimizer')); } return array( 'commer_title' => isset($result->OUTPUT_REC->commer_title) ? (string) $result->OUTPUT_REC->commer_title : '', 'onomasia' => isset($result->OUTPUT_REC->onomasia) ? (string) $result->OUTPUT_REC->onomasia : '', 'doy_descr' => isset($result->OUTPUT_REC->doy_descr) ? (string) $result->OUTPUT_REC->doy_descr : '', 'postal_address' => isset($result->OUTPUT_REC->postal_address) ? (string) $result->OUTPUT_REC->postal_address : '', 'postal_address_no' => isset($result->OUTPUT_REC->postal_address_no) ? (string) $result->OUTPUT_REC->postal_address_no : '', 'postal_area_description' => isset($result->OUTPUT_REC->postal_area_description) ? (string) $result->OUTPUT_REC->postal_area_description : '', 'postal_zip_code' => isset($result->OUTPUT_REC->postal_zip_code) ? (string) $result->OUTPUT_REC->postal_zip_code : '', 'activities' => isset($result->OUTPUT_REC->firm_act_tab->item) ? array_map(function($a){ return isset($a->firm_act_descr) ? (string) $a->firm_act_descr : ''; }, (array) $result->OUTPUT_REC->firm_act_tab->item) : array(), ); } catch (\Throwable $e) { return array('error' => $e->getMessage()); } } public function inject_classic_fields($fields) { $s = $this->get_settings(); $options = array( 'NO' => (string) $s['invoice_no_label'], 'YES' => (string) $s['invoice_yes_label'], ); $fields['billing']['billing_timologio'] = array( 'type' => 'select', 'label' => (string) $s['invoice_field_label'], 'options' => $options, 'default' => 'NO', 'class' => array('form-row-wide'), 'priority' => 115, 'required' => true, ); $fields['billing']['billing_vat'] = array( 'type' => 'text', 'label' => (string) $s['vat_field_label'], 'class' => array('form-row-wide', 'timologio-hide'), 'priority' => 116, 'required' => false, ); $fields['billing']['billing_irs'] = array( 'type' => 'text', 'label' => (string) $s['irs_field_label'], 'class' => array('form-row-wide', 'timologio-hide'), 'priority' => 117, 'required' => false, ); $fields['billing']['billing_store'] = array( 'type' => 'text', 'label' => (string) $s['profession_field_label'], 'class' => array('form-row-wide', 'timologio-hide'), 'priority' => 118, 'required' => false, ); if ($s['move_company_field'] === 'yes' && isset($fields['billing']['billing_company'])) { $fields['billing']['billing_company']['class'][] = 'timologio-hide'; $fields['billing']['billing_company']['priority'] = 116; } return $fields; } public function enqueue_assets() { if (!function_exists('is_checkout') || !is_checkout()) { return; } $version = defined('AIEO_VERSION') ? AIEO_VERSION : '1.0'; wp_enqueue_style( 'aieo-checkout-invoicing', plugins_url('assets/css/aieo-checkout-invoicing.css', AIEO_FILE), array(), $version ); wp_enqueue_script( 'aieo-checkout-invoicing', plugins_url('assets/js/aieo-checkout-invoicing.js', AIEO_FILE), array('jquery'), $version, true ); $s = $this->get_settings(); wp_localize_script('aieo-checkout-invoicing', 'omCheckoutInvoice', array( 'ajaxUrl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('om_checkout_invoice'), 'aadeEnabled' => $s['aade_enabled'] === 'yes' && (string) $s['aade_username'] !== '' && (string) $s['aade_password'] !== '' && (string) $s['aade_vat'] !== '', )); } public function save_classic_to_order_meta($order_id) { $order = wc_get_order($order_id); if (!$order) { return; } $fields = array('billing_timologio', 'billing_vat', 'billing_irs', 'billing_store'); foreach ($fields as $f) { if (isset($_POST[$f])) { $order->update_meta_data('_' . $f, sanitize_text_field(wp_unslash($_POST[$f]))); } } $order->save(); } public function render_admin_order_panel($order) { if (!is_a($order, 'WC_Order')) { return; } $tim = (string) $order->get_meta('_billing_timologio'); if ($tim !== 'YES') { return; } $vat = (string) $order->get_meta('_billing_vat'); $irs = (string) $order->get_meta('_billing_irs'); $store = (string) $order->get_meta('_billing_store'); echo '
'; echo '

' . esc_html__('Invoice details', 'ai-eshop-optimizer') . '

'; if ($vat !== '') { echo '

' . 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 '
'; } public function register_block_fields() { if (!function_exists('woocommerce_register_additional_checkout_field')) { return; } $s = $this->get_settings(); $fields = array( array('id' => 'aieo/wants_invoice', 'label' => (string) $s['invoice_field_label'], 'type' => 'checkbox'), array('id' => 'aieo/billing_vat', 'label' => (string) $s['vat_field_label'], 'type' => 'text'), array('id' => 'aieo/billing_irs', 'label' => (string) $s['irs_field_label'], 'type' => 'text'), array('id' => 'aieo/billing_store', 'label' => (string) $s['profession_field_label'], 'type' => 'text'), ); $registered = self::registered_checkout_field_ids(); foreach ($fields as $f) { if (in_array($f['id'], $registered, true)) { continue; } try { woocommerce_register_additional_checkout_field(array( 'id' => $f['id'], 'label' => $f['label'], 'location' => 'address', 'type' => $f['type'], 'required' => false, )); } catch (\Throwable $e) { } } } private static function registered_checkout_field_ids() { $pkg = 'Automattic\\WooCommerce\\Blocks\\Package'; $cls = 'Automattic\\WooCommerce\\Blocks\\Domain\\Services\\CheckoutFields'; if (!class_exists($pkg) || !class_exists($cls) || !method_exists($pkg, 'container')) { return array(); } try { $svc = $pkg::container()->get($cls); if ($svc && method_exists($svc, 'get_additional_fields')) { return array_map('strval', array_keys((array) $svc->get_additional_fields())); } } catch (\Throwable $e) { } return array(); } public function sync_block_fields_to_legacy_meta($order) { if (!is_a($order, 'WC_Order')) { return; } $wants = self::block_field_value($order, 'wants_invoice'); if ($wants === '1' || $wants === 'true' || $wants === 'yes') { $order->update_meta_data('_billing_timologio', 'YES'); $vat = self::block_field_value($order, 'billing_vat'); $irs = self::block_field_value($order, 'billing_irs'); $store = self::block_field_value($order, 'billing_store'); if ($vat !== '') { $order->update_meta_data('_billing_vat', $vat); } if ($irs !== '') { $order->update_meta_data('_billing_irs', $irs); } if ($store !== '') { $order->update_meta_data('_billing_store', $store); } } else { $order->update_meta_data('_billing_timologio', 'NO'); } $order->save(); } private static function block_field_value($order, $key) { foreach (array('_wc_billing/aieo/', '_wc_shipping/aieo/', '_wc_other/aieo/') as $prefix) { $v = (string) $order->get_meta($prefix . $key); if ($v !== '') { return $v; } } return ''; } public function render_subtab() { $s = $this->get_settings(); $form_id = 'aieo-card-checkout-invoicing-invoice-fields-at-checkout-form'; rooster_admin_band_open(array('cols' => '2:1')); list($open, $close) = AIEO_DMM_UI::card(array( 'anchor' => 'checkout-invoicing-invoice-fields-at-checkout', 'title' => __('Invoice fields at checkout', 'ai-eshop-optimizer'), 'col' => 1, 'description' => __('Customers who need a business invoice tick a box at checkout and add their VAT number, tax office and business activity. They are saved where invoice plugins and order exports already look for them.', 'ai-eshop-optimizer'), 'footer' => class_exists('\Rooster\Admin\States') ? \Rooster\Admin\States::save_button($form_id) : '', )); echo $open; $switch = static function ($name, $on, $label, $sentence) { $id = 'aieo-inv-' . str_replace('_', '-', $name); return \Rooster\Admin\Ui::field_row(array( 'id' => $id, 'label' => $label, 'control' => \Rooster\Admin\Ui::toggle($name, (bool) $on, array('id' => $id, 'hidden' => false, 'label' => $sentence)), )); }; $rows = $switch('enabled', $s['enabled'] === 'yes', __('Enable invoicing fields', 'ai-eshop-optimizer'), __('Show the invoice option at checkout', 'ai-eshop-optimizer')); $defaults = self::i18n_labels(); $raw = $this->get_settings(true); foreach (array( 'invoice_field_label' => __('Toggle question label', 'ai-eshop-optimizer'), 'invoice_yes_label' => __('"Yes, invoice" option', 'ai-eshop-optimizer'), 'invoice_no_label' => __('"No, receipt" option', 'ai-eshop-optimizer'), 'vat_field_label' => __('VAT number field label', 'ai-eshop-optimizer'), 'irs_field_label' => __('Tax office field label', 'ai-eshop-optimizer'), 'profession_field_label' => __('Business activity field label', 'ai-eshop-optimizer'), ) as $k => $label) { $fid = 'aieo-inv-' . str_replace('_', '-', $k); $value = self::is_shipped_default($k, (string) $raw[$k]) ? '' : (string) $raw[$k]; $rows .= \Rooster\Admin\Ui::field_row(array( 'id' => $fid, 'label' => $label, 'control' => \Rooster\Admin\Ui::input($k, $value, array('id' => $fid, 'medium' => true, 'placeholder' => (string) $defaults[$k])), )); } $rows .= $switch('move_company_field', $s['move_company_field'] === 'yes', __('Group company field with invoice fields', 'ai-eshop-optimizer'), __('Keep the company name with the invoice fields', 'ai-eshop-optimizer')); echo AIEO_DMM_UI::card_form('aieo_dmm_checkout_invoicing_save', $this->get_id(), $form_id, '
' . $rows . '
'); echo $close; $aade_form = 'aieo-card-checkout-invoicing-aade-vat-lookup-form'; list($open, $close) = AIEO_DMM_UI::card(array( 'anchor' => 'checkout-invoicing-aade-vat-lookup', 'icon' => 'search', 'title' => __('AADE VAT Lookup', 'ai-eshop-optimizer'), 'description' => __('For Greek VAT numbers only: when a customer enters one, the company name, tax office and business activity are filled in from AADE, the Greek tax authority. VAT numbers from other countries are not looked up. You need AADE credentials (apply at gsis.gr), and your server needs PHP\'s SOAP extension.', 'ai-eshop-optimizer'), 'col' => 1, 'footer' => class_exists('\Rooster\Admin\States') ? \Rooster\Admin\States::save_button($aade_form) : '', )); echo $open; $rows = $switch('aade_enabled', $s['aade_enabled'] === 'yes', __('Enable AADE lookup', 'ai-eshop-optimizer'), __('Fill in the invoice fields from AADE once a Greek VAT number is entered', 'ai-eshop-optimizer')); $rows .= \Rooster\Admin\Ui::field_row(array( 'id' => 'aieo-inv-aade-vat', 'label' => __('Caller VAT (your shop\'s VAT number)', 'ai-eshop-optimizer'), 'control' => \Rooster\Admin\Ui::input('aade_vat', (string) $s['aade_vat'], array('id' => 'aieo-inv-aade-vat', 'short' => true, 'code' => true, 'maxlength' => 9, 'pattern' => '\d{9}')), 'help' => __('Your shop\'s own 9-digit Greek VAT number, which AADE asks for with every lookup.', 'ai-eshop-optimizer'), )); $rows .= \Rooster\Admin\Ui::field_row(array( 'id' => 'aieo-inv-aade-username', 'label' => __('AADE username', 'ai-eshop-optimizer'), 'control' => \Rooster\Admin\Ui::input('aade_username', (string) $s['aade_username'], array('id' => 'aieo-inv-aade-username', 'medium' => true, 'autocomplete' => 'off')), )); $rows .= \Rooster\Admin\Ui::field_row(array( 'id' => 'aieo-inv-aade-password', 'label' => __('AADE password', 'ai-eshop-optimizer'), 'control' => \Rooster\Admin\Fields::secret('aade_password', '' !== (string) $s['aade_password'], array('id' => 'aieo-inv-aade-password', 'label' => __('AADE password', 'ai-eshop-optimizer'))), 'help' => __('Saved in your site\'s database. A separate AADE account for this, not your personal login, is safer.', 'ai-eshop-optimizer'), )); echo AIEO_DMM_UI::card_form('aieo_dmm_checkout_invoicing_save', $this->get_id(), $aade_form, '
' . $rows . '
'); echo $close; list($open, $close) = AIEO_DMM_UI::card(array( 'anchor' => 'checkout-invoicing-block-checkout-note', 'icon' => 'text-cursor-input', 'title' => __('Invoice fields in block checkout', 'ai-eshop-optimizer'), 'col' => 2, 'description' => __('The invoice fields also appear on the block checkout, and what customers enter there is saved in the same place as on the classic checkout, so your invoice templates and exports read it the same way.', 'ai-eshop-optimizer'), )); echo $open . $close; rooster_admin_band_close(); } }