<?php

if (!class_exists( 'WP_List_Table')) {
    require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}

require_once PRIMER_PATH . 'views/get_order_list.php';
require_once PRIMER_PATH . 'admin/includes/my_data_json.php';
require_once PRIMER_PATH . 'includes/vendor/dompdf/autoload.inc.php';

use PrimerDompdf\Dompdf;
use PrimerDompdf\Options;


class PrimerReceipts extends WP_List_Table {

	function __construct() {
		parent::__construct(
			array(
				'singular' => __( 'Order', 'primer' ),
				'plural' => __( 'Orders', 'primer' ),
				'ajax' => true,
			)
		);
		$this->prepare_items();

		add_action( 'wp_print_scripts', [ __CLASS__, '_list_table_css' ] );
	}

    public function display():void {
			$this->search_box( esc_html__( 'Search  order id', 'primer' ), 'filter_action' );
			parent::display();
	}

	function get_columns():array {
		return array(
			'cb'		 	        => '<input type="checkbox" />',
			'order_id'		        => __('No.', 'primer'),
			'order_date' 	        => __('Order Date', 'primer'),
			'order_hour'	        => __('Hour', 'primer'),
			'order_client'	        => __('Client', 'primer'),
			'order_price'	        => __('Total Price', 'primer'),
			'order_status'	        => __('Order Status', 'primer'),
			'payment_status'        => __('Payment Method', 'primer'),
            'transmission_failure'	=> __('Transmission Status', 'primer'),
			'receipt_date'	        => __('Voucher', 'primer'),
            'receipt_status'	    => __('Receipt Log', 'primer'),
            'credit_receipt'        => __('Credit Receipt', 'primer'),
            'credit_log'            => __('Credit Log', 'primer'),
			'receipt_id'	        => __('Receipt ID', 'primer'),
			'accept_48'             => __('Accept 48','primer')
		);
	}
	function get_sortable_columns():array {
		return array();
	}

	function column_default($item, $column_name) {
		if ($column_name !== 'receipt_date' && $column_name !== 'receipt_status' && $column_name !== 'receipt_id' && $column_name !== 'credit_receipt' && $column_name !== 'credit_log') {
			echo '<a href="' . esc_url(admin_url('post.php?post=' . absint($item['order_id'])) . '&action=edit') . '" target="_blank" class="order-view"><strong>' . esc_attr($item[$column_name]) . '</strong></a>';
		}
        else {
			if ($column_name == 'receipt_status') {
				if (!empty($item[$column_name])) {
                    $allowed_html = array(
                            'a' => array(
                                    'href' => array(),
                                    'title' => array(),
                                    'target' => array()
                            ),
                            'strong' => array(
                                    'class' => array(),
                                    )
                    );
                    $escape = '<a href="' . $item[$column_name] . '" target="_blank" class="order-view"><strong>' . __('Log', 'primer') . '</strong></a>';
                    echo wp_kses($escape,$allowed_html);
				} else {
					echo '';
				}
			}
            elseif ($column_name == 'receipt_date') {
				$receipt_id = $item['receipt_id'];
				if (!empty($receipt_id)) {
					 $new_url = add_query_arg(array('receipt' => 'view'), rtrim(get_permalink($receipt_id), '/'));
                     $order   = wc_get_order($item['order_id']);
                     $transmission_failure_order      = $order->get_meta('order_date_failed');
                     $transmission_failure_check      = $order->get_meta('transmission_failure_check');
                     $failed_credit_receipt_to_issue  = $order->get_meta('credit_receipt_failed_to_issue');
                     $receiptId                       = $order->get_meta('order_id_from_receipt');
                     $json_send_to_api                = json_decode(get_post_meta(get_post_meta($receiptId, get_post_meta($receiptId, 'credit_receipt', true) ? 'credit_log_id_for_order' : 'log_id_for_order', true), 'json_send_to_api', true), true);
                     $receipt_series                  = $json_send_to_api['invoice'][0]['invoiceHeader']['series'] == 0 ? '' : $json_send_to_api['invoice'][0]['invoiceHeader']['series'];
                     $receipt_aa                      = $json_send_to_api['invoice'][0]['invoiceHeader']['aa'];
                     $verify_success_receipt          = get_post_meta($receiptId,'response_invoice_mark',true);
                     if ((!empty($transmission_failure_order) && $transmission_failure_check == 2) || empty($verify_success_receipt)) {
                         $new_url = add_query_arg(array('receipt' => 'view'), rtrim(get_permalink($receipt_id), '/').'-failed');
                         if (empty($item['credit_receipt_id'])) {
                             $new_url = add_query_arg(array('receipt' => 'view'), rtrim(get_permalink($receipt_id), '/'));
                         }
                     }
                     if ($failed_credit_receipt_to_issue == 'yes') {
                         if (!empty($transmission_failure_order)) {
                             $new_url = add_query_arg(array('receipt' => 'view'), rtrim(get_permalink($receipt_id), '/').'-failed');
                         }
                         if ($transmission_failure_check == 2 || !empty($verify_success_receipt)) {
                             $new_url = add_query_arg(array('receipt' => 'view'), rtrim(get_permalink($receipt_id), '/'));
                             $new_url = preg_replace('/-failed/', '', $new_url);
                         }
                     }
                     $new_url = preg_replace('/credit-/', '', $new_url);
					echo '<a href="' . esc_url($new_url) . '" target="_blank" class="order-view"><strong>'. esc_attr($receipt_series) . ' ' . esc_attr($receipt_aa) . ', ' . esc_attr($item[$column_name]) . '</strong></a>';
				} else {
					return $item[$column_name];
				}
			}
            elseif ($column_name == 'credit_receipt') {
                $receipt_id = $item['credit_receipt_id'];
				if (!empty($receipt_id)) {
					$new_url = add_query_arg(array('receipt' => 'view'), rtrim(get_permalink($receipt_id), '/'));
					echo '<a href="' . esc_url($new_url) . '" target="_blank" class="order-view"><strong>' . esc_attr($item['credit_receipt_date']) . '</strong></a>';
				} else {
					return '';
				}
			}
            elseif($column_name == 'credit_log') {
                $receipt_id = $item['credit_receipt_id'];
				if (!empty($receipt_id)) {
                    $credit_log = get_post_meta($receipt_id,'credit_log_id_for_order',true);
                    $receipt_status_from_meta_url_prefix = admin_url('admin.php?page=primer_receipts_logs');
                    $receipt_status_from_meta_url    = $receipt_status_from_meta_url_prefix . '&order_log=' . $credit_log;
                    echo '<a href="' . esc_url($receipt_status_from_meta_url) . '" target="_blank" class="order-view"><strong> Log </strong></a>';
				} else {
					 return '';
				}
			}
            else {
				return $item[$column_name];
			}
		}
	}

	private array $hidden_columns = array(
		'receipt_id', 'accept_48'
	);

	function column_cb($item):string {
		return sprintf(
			'<input type="checkbox" name="orders[]" id="order_'.$item['order_id'].'" value="%s" />',
			$item['order_id']
		);
	}

	protected function get_bulk_actions():array {
		return array();
	}

	function extra_tablenav($which):void {
		if ($which !== 'bottom') {
            $primer_orders = new PrimerOrderList();
            $current_page = $this->get_pagenum();
            $primer_orders_customers = $primer_orders->get_users_from_orders($current_page);
            $unique_customers = [];
            foreach ($primer_orders_customers as $primer_orders_customer) {
                $hash = $primer_orders_customer['order_client_id'];
                $unique_customers[$hash] = $primer_orders_customer;
            }
            $order_customers = array_values($unique_customers);
            ?>

            <div class="alignleft actions">
                <h2><?php esc_html_e('Filters', 'primer'); ?></h2>
                <h3><?php esc_html_e('Date Range:', 'primer'); ?></h3>
                <div class="filter_blocks_wrapper">
                    <div class="left_wrap">
                        <div class="filter_block">
                                <label for="primer_order_year" style="float: left;"><?php esc_html_e('Year: ', 'primer'); ?></label>
                                <select name="primer_order_year" id="primer_order_year">
                                    <?php
                                    $year_from = !empty($orders_dates = $primer_orders->get_dates_from_orders()) ? date('Y', min($orders_dates)) : '';
                                    $year_to = date('Y');
                                    $primer_order_year = isset($_GET['primer_order_year']) ? sanitize_text_field($_GET['primer_order_year']) : $year_to;
                                    $range_years = range($year_from, $year_to);
                                    foreach ($range_years as $range_year) { ?>
                                        <option value="<?php echo esc_attr($range_year); ?>" <?php selected($range_year, $primer_order_year); ?>><?php echo esc_attr($range_year); ?></option>
                                    <?php }
                                    ?>
                                </select>
                        </div>

                        <div class="filter_block">
                            <label for="order_date_from">
                                <?php esc_html_e('From: ', 'primer'); ?></label>
                                <input type="text" id="order_date_from" name="order_date_from" placeholder="Date From" value="" />
                            <label for="order_date_to">
                                <?php esc_html_e('To: ', 'primer'); ?></label>
                                <input type="text" id="order_date_to" name="order_date_to" placeholder="Date To" value="" />
                        </div>

                        <div class="filter_block">
                            <label for="primer_order_client" style="float: left;"><?php esc_html_e('Client: ', 'primer'); ?></label>
                                <select name="primer_order_client" id="primer_order_client" data-placeholder="<?php esc_html_e('Select clients', 'primer'); ?>">
                                    <option value=""></option>
                                    <?php
                                    $get_customer = isset($_GET['primer_order_client']) ? sanitize_text_field($_GET['primer_order_client']) : '';
                                    foreach ( $order_customers as $primer_orders_customer => $order_customer ) {
                                        if ($order_customer['order_client_id']) { ?>
                                            <option value="<?php echo esc_attr($order_customer['order_client_id']); ?>" <?php selected($get_customer, $order_customer['order_client_id']); ?>><?php echo esc_attr($order_customer['order_client']); ?></option>
                                        <?php } else { ?>
                                            <option value="<?php echo esc_attr($order_customer['order_client_id']); ?>" <?php selected($get_customer, $order_customer['order_client_id']); ?>><?php esc_html_e('Guest client', 'primer'); ?></option>
                                        <?php }
                                    } ?>
                                </select>
                        </div>
                    </div>

                    <div class="right_wrap">
                        <div class="filter_block">
                                    <label for="primer_order_status" style="float: left;"><?php esc_html_e('Order Status: ', 'primer'); ?></label>
                                    <select name="primer_order_status" title="<?php esc_html_e('Select order status', 'primer'); ?>" id="primer_order_status">
                                        <option selected disabled><?php esc_html_e('Select order status', 'primer'); ?></option>
                                    <?php
                                    $orders_all_statuses = wc_get_order_statuses();
                                    $primer_order_status = isset($_GET['primer_order_status']) ? sanitize_text_field($_GET['primer_order_status']) : '';
                                    foreach ($orders_all_statuses as $order_status => $order_status_value ) { ?>
                                        <option value="<?php echo esc_attr($order_status); ?>" <?php
                                        selected($order_status, $primer_order_status);?>><?php echo esc_attr($order_status_value); ?></option>
                                    <?php }
                                    ?>
                                    </select>
                        </div>
                        <div class="filter_block">
                                <label for="primer_receipt_status" style="float: left;"><?php esc_html_e('Receipt Status: ', 'primer'); ?></label>
                                <select name="primer_receipt_status" id="primer_receipt_status">
                                <?php
                                $get_status = isset($_GET['primer_receipt_status']) ? sanitize_text_field($_GET['primer_receipt_status']) : '';
                                $status_of_receipts = array(
                                        '' => __('All'),
                                        'issued' => __('Issued', 'primer'),
                                        'not_issued' => __('Not Issued', 'primer'),
                                        'transmission_failure_check' => __('Transmission Failure', 'primer')
                                    );
                                foreach ( $status_of_receipts as $status_k => $status_value ) { ?>
                                        <option value="<?php echo esc_attr($status_k); ?>" <?php selected($status_k, $get_status); ?>><?php echo esc_attr($status_value); ?></option>
                                    <?php }
                                ?>
                                </select>
                        </div>
                        <div class="apply_btn btn_order"><input type="submit" class="button" id="filter_action" name="filter_action" value="<?php esc_html_e('Apply filter', 'primer'); ?>" /></div>
                    </div>
                </div>

            <?php

            $primer_licenses = get_option('primer_licenses');
            /// ??
            if (!(in_array(3, $primer_licenses['wpModules']))) {
                echo '<h4><i>'. esc_html(__('Orders for invoices are not shown because license key does not support them', 'primer')) .'</i></h4>';
            }
            if (!(in_array(4, $primer_licenses['wpModules']))) {
                echo '<h4><i>'. esc_html(__('Orders for invoices-receipts within E.U or out E.U. are not shown because license key does not support them', 'primer')).'</i></h4>';
            }
             ?>
            </div>

            <div class="loadingio-spinner-spinner-chyosfc7wi6" id="mySpinner"><div class="ldio-drsjmtezgls"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div></div>

            <?php
             $formatted_min_order_date = '';
             $formatted_max_order_date = '';
             if (!empty($orders_dates)) {
                 $min_order_date = min($orders_dates);
                 $max_order_date = max($orders_dates);
                 $formatted_min_order_date = date('m/d/Y', $min_order_date);
                 $formatted_max_order_date = date('m/d/Y', $max_order_date);
             }
             ?>

            <script>
                jQuery(document).ready(function ($) {

                    $('#primer_order_client').selectWoo({
                        allowClear:  true,
                        placeholder: $( this ).data( 'placeholder' )
                    });

                    let select_year = $('select[name="primer_order_year"]').val();
                    let min_order_date = "<?php echo esc_attr($formatted_min_order_date); ?>";
                    let max_order_date = "<?php echo esc_attr($formatted_max_order_date); ?>";
                    let min_year_range = new Date(min_order_date).getFullYear();
                    let date_from = $('input[name="order_date_from"]'),
                        date_to   = $('input[name="order_date_to"]');

                    $('input[name="order_date_from"], input[name="order_date_to"]').datepicker({
                        changeMonth: true,
                        changeYear: true,
                        dateFormat: "yy-mm-dd",
                        minDate: new Date(min_order_date),
                        maxDate: "now",
                        yearRange: min_year_range + ":now"
                    });

                    $('input[name="order_date_from"]').datepicker("option", "minDate", new Date(min_order_date));
                    $('input[name="order_date_to"]').datepicker("option", "maxDate", new Date(max_order_date));


                    <?php if (isset($_GET['order_date_from']) && !empty($_GET['order_date_from'])) { ?>
                        $('input[name="order_date_from"]').datepicker("setDate", new Date("<?php esc_html_e($_GET['order_date_from'], 'primer'); ?>"));
                        <?php } else { ?>
                        // $('input[name="order_date_from"]').datepicker("option", "minDate", new Date(min_order_date));
                        <?php } ?>

                        <?php if (isset($_GET['order_date_to']) && !empty($_GET['order_date_to'])) { ?>
                        $('input[name="order_date_to"]').datepicker("setDate", new Date("<?php esc_html_e($_GET['order_date_to'], 'primer'); ?>"));
                        <?php } else { ?>
                            // $('input[name="order_date_to"]').datepicker("option", "minDate", new Date(max_order_date));
                        <?php } ?>


                    $('select[name="primer_order_year"]').on('change', function () {
                        select_year = $(this).val();

                        $('input[name="order_date_from"], input[name="order_date_to"]').datepicker("destroy");
                        $('input[name="order_date_from"], input[name="order_date_to"]').datepicker({
                            changeMonth: true,
                            changeYear: true,
                            dateFormat: "yy-mm-dd",
                            minDate: new Date(min_order_date),
                            maxDate: "now",
                            yearRange: min_year_range + ":now"
                        });

                        var currentDate = new Date();
                        var currentDay = currentDate.getDate();
                        var currentMonth = currentDate.getMonth()+1;
                        var set_current_date = currentMonth + '/' + currentDay + '/' + select_year;

                        $('input[name="order_date_from"]').datepicker("setDate", new Date(set_current_date));
                        $('input[name="order_date_to"]').datepicker( 'option', 'minDate', date_from.val() );
                    });


                    date_from.on( 'change', function() {
                        date_to.datepicker( 'option', 'minDate', date_from.val() );
                    });
                    date_to.on('change', function () {
                        date_to.datepicker('option', 'maxDate', date_to.val());
                    });

                    var atLeastOneIsChecked = $('input[name="orders[]"]:checked').length > 0;
                    if (atLeastOneIsChecked) {
                        $('.convert_orders input[type="submit"]').removeAttr('disabled');
                    }

                    function checker() {
                        var length_inputs = $('input[name="orders[]"]').length;
                        var trues = new Array();
                        $('input[name="orders[]"]').each(function (i, el) {

                            if ($(el).prop('checked') == true || $(el).is(':checked') == true) {
                                $('.convert_orders input[type="submit"]').removeAttr('disabled');
                                trues.push($(el));
                            }
                        })
                        if (trues.length <= 0) {
                            $('.convert_orders input[type="submit"]').attr('disabled', true);
                        }
                    }

                    $('.wp-list-table #cb input:checkbox').on('click', function () {
                        checker();
                        if ($(this).is(':checked')) {
                            $('.convert_orders input[type="submit"]').removeAttr('disabled');
                        } else {
                            $('.convert_orders input[type="submit"]').attr('disabled', true);
                        }
                    });

                    $('.wp-list-table input[name="orders[]"]').on('click', function () {
                        checker();
                    });

                    $('#filter_action').on('click', function() {
                      $('#current-page-selector').val('1');
                    });

                });
            </script>
        <?php
        }
    }

	function prepare_items():void {
		$per_page         = 20;
        $current_page     = $this->get_pagenum();
		$get_total_orders = new PrimerOrderList();
        $isGuestClient    = false;

        if (isset($_GET['primer_order_client']) && $_GET['primer_order_client'] == 0) {
            $isGuestClient = true;
        }

		if ((isset($_GET['primer_order_status'])) || (isset($_GET['primer_order_client']) && (!empty($_GET['primer_order_client'])) || $isGuestClient ) || (isset($_GET['order_date_from']) && !empty($_GET['order_date_from'])) || (isset($_GET['order_date_to']) && !empty($_GET['order_date_to'])) || (isset($_GET['primer_receipt_status']) && !empty($_GET['primer_receipt_status']) ) || (isset($_GET['s']) && !empty($_GET['s']) )) {
            $order_status     = isset($_REQUEST['primer_order_status']) ? sanitize_text_field($_REQUEST['primer_order_status']) : '';
            $get_orders_list  = $get_total_orders->get_with_params($current_page,$_REQUEST['order_date_from'], $_REQUEST['order_date_to'], $_GET['primer_order_client'], $order_status, $_GET['primer_receipt_status']);
            $total_orders     = $get_orders_list['total_orders'];
            $get_orders_list  = $get_orders_list['orders'];
        } else {
            $get_orders_list  = $get_total_orders->get($current_page);
        }

		$columns                = $this->get_columns();
		$hidden                 = $this->hidden_columns;
		$sortable               = $this->get_sortable_columns();
		$this->_column_headers  = array($columns, $hidden, $sortable);
		$this->items            = $get_orders_list;
		$data                   = $this->items;
        $total_items            = $total_orders ?? array_sum((array)wp_count_posts('shop_order'));
        $this->items            = $data;

		$this->set_pagination_args(
			array(
				'total_items'	=> $total_items,
				'per_page'	    => $per_page,
				'total_pages'	=> ceil($total_items / $per_page),
			)
		);
	}

	function no_items():void {
		esc_html_e('No orders found.', 'primer');
	}

	function process_bulk_action():void {
		$orders         = isset( $_REQUEST['wp_ajax_list_order'] ) ?? array();
		$orders         = array_map( 'sanitize_text_field', $orders );
		$current_action = $this->current_action();
		if (!empty($current_action)) {
			if (empty($orders)) {
				echo '<div id="message" class="error"><p>Error! You need to select multiple records to perform a bulk action!</p></div>';
				return;
			}
		} else {
            return;
		}
	}

	function show_all_orders():string {
		ob_start();
		$status = filter_input( INPUT_GET, 'status' );
		include_once PRIMER_PATH . 'views/admin_order_list.php';
		return ob_get_clean();
	}

	function handle_main_primer_admin_menu():void {

		do_action('primer_orders_menu_start');
		$action = filter_input(INPUT_GET, 'primer_action');
		$action = empty($action) ? filter_input(INPUT_POST, 'action') : $action;

		if (empty($action)) {
			$action = sanitize_text_field($_GET['page']);
		}

		?>
		<div class="wrap primer-admin-menu-wrap">
			<div class="plugin_caption_version"><?php echo esc_attr(PRIMER_NAME . ' v'. PRIMER_VERSION); ?></div>
		<?php

		 if ($_GET['page'] === 'wp_ajax_list_order') { ?>
		 	<h2><?php esc_html_e('Orders', 'primer'); ?>
			<?php
			do_action('primer_menu_nav_tabs', $action); ?>
			</h2>
			<?php
			do_action('primer_menu_body_' . $action);
			$output = apply_filters('primer_menu_body_override', '', $action);

			if (!empty($output)) {
				$allowed_html = wp_kses_allowed_html();
				echo wp_kses($output, $allowed_html);
				echo '</div>';
				return;
			}
            ?>
		 <?php
         } ?>

			<?php

			switch ($action) {
				case 'orders_list':
					echo ($this->show_all_orders());
					break;
				case 'primer_receipts':
					echo ($this->show_all_receipts());
					break;
				default:
					// Show the orders listing by default
					echo ($this->show_all_orders());
					break;
			}
			echo '</div>';
	}

}

add_action('wp_ajax_convert_select_orders', 'convert_select_orders');
function convert_select_orders() {
	$order_nonce = isset($_POST['order_nonce']) ? $_POST['order_nonce'] : '';
	check_ajax_referer( 'order_nonce', 'order_nonce' );
	if( wp_verify_nonce( $order_nonce, 'order_nonce') ) die('Stop!');
	$mydata_options = get_option('primer_mydata');
    if (!defined('MINUTE_IN_SECONDS')) {
        define('MINUTE_IN_SECONDS', 60);
    }
    if (get_transient('convert_order_to_invoice_lock')) {
        return;
    }
    set_transient('convert_order_to_invoice_lock', true, MINUTE_IN_SECONDS);
	$classificationType        = '';
	$classificationCategory    = '';
	$classificationCategory_en = 'category1_95';
	$api_url                   = $mydata_options['mydata_api'];
	switch ($api_url) {
		case 'test_api':
			$url = 'https://test-mydataapi.primer.gr/v2/invoices/sendData';
			$api_type = 'test';
			break;
		case 'production_api':
            $url = 'https://erp-mydataapi.primer.gr/v2/invoices/sendData';
			$api_type = 'production';
			break;
		default:
			$url = 'https://test-mydataapi.primer.gr/v2/invoices/sendData';
			$api_type = 'test';
	}
	$primer_license_data = get_option('primer_licenses');
	$username            = $primer_license_data['username'] ? $primer_license_data['username'] : '';
	$password            = $primer_license_data['password'] ? $primer_license_data['password'] : '';
	$user_vat            = $primer_license_data['companyVatNumber'];
    $callingFunction     = 'convert_select_orders';
	$response_data       = '';
	$auth                = base64_encode( "$username" . ':' . "$password" );
	$curl_args = array(
		'timeout' 		=> 30,
		'redirection' 	=> 10,
		'method'		=> 'POST',
		'httpversion' 	=> '1.1',
		'headers'		=> array(
			'Authorization' => 'Basic ' . $auth,
			'Content-Type'	=> 'application/json',
			'charset=UTF-8'
		),
		'sslverify'		=> false
	);
	$total_vat_number = "$user_vat";
	$log_ids          = array();
	$orders           = isset($_GET['orders']) ? $_GET['orders'] : '';
    array_map( 'sanitize_text_field', $orders );
	if (!isset($_GET['orders']) && !empty($mydata_options['last_request']) && isset($_GET['resend_html'])) {
		$orders = $mydata_options['last_request'];
	}
	if (!empty($orders)) {
		foreach ($orders as $order_id) {
            $order             = new WC_Order($order_id);
            $id_of_order       = $order->get_id();
            $issue_date        = new DateTime("now", new DateTimeZone("Europe/Athens"));
            $order_datetime_co = $order->get_meta('order_datetime_failed');
            if ($order_datetime_co != '') {
                $issue_date = new DateTime($order_datetime_co, new DateTimeZone("Europe/Athens"));
            }
            $gr_time = $issue_date->format('Y-m-d');
            $receipt_log_id = wp_insert_post(array(
                'post_type' => 'primer_receipt_log',
                'post_title' => 'Receipt report for #' . $id_of_order,
                'comment_status' => 'closed',
                'ping_status' => 'closed',
                'post_status' => 'publish',
            ));
            if (!ini_get('allow_url_fopen')) {
                $response_data .= '<div class="primer_popup popup_error"><div><h3>'.__('Php option allow_url_fopen is disabled! Please communicate with your hosting provider in order to activate it.', 'primer') .'</h3><br><br><br><br><br></div>';
                $response_data .= '<button class="popup_ok button button-primary">OK</button></div>';
                $receipt_log_value_array[] = __('Php option allow_url_fopen is disabled! Please communicate with your hosting provider in order to activate it.', 'primer');
                $receipt_log_value .= __('Php option allow_url_fopen is disabled! Please communicate with your hosting provider in order to activate it.', 'primer');
                break ;
            }
            if (!extension_loaded('bcmath')) {
                $response_data .= '<div class="primer_popup popup_error"><div><h3>'.__('Php module "bcmath" is disabled! Please communicate with your hosting provider in order to activate it.', 'primer') .'</h3><br><br><br><br><br></div>';
                $response_data .= '<button class="popup_ok button button-primary">OK</button></div>';
                $receipt_log_value_array[] = __('Php module "bcmath" is disabled! Please communicate with your hosting provider in order to activate it.', 'primer');
                $receipt_log_value .= __('Php module "bcmath" is disabled! Please communicate with your hosting provider in order to activate it.', 'primer');
                break ;
            }
            if ($order->get_meta('receipt_status') == 'issued' && $order->get_meta('transmission_failure_check') == null || $order->get_meta('transmission_failure_check') == 2) {
                $response_data .= '<div class="primer_popup popup_error"><div><h3>'.__('This order has already been issued. Please refresh the page.', 'primer') .'</h3><br><br><br><br><br></div>';
                $response_data .= '<button class="popup_ok button button-primary">OK</button></div>';
                $receipt_log_value_array[] = __('This order has already been issued by the automation. Press refresh.', 'primer');
                $receipt_log_value .= __('This order has already been issued by the automation. Press refresh.', 'primer');
                break ;
            }
            $invoice_data            = "";
            $order_country           = '';
            $order_invoice_type      = '';
            $order_vatNumber         = '';
            $user_order_email        = '';
            $currency                = '';
            $currency_symbol         = '';
            $user_id                 = '';
            $order_total_price       = '';
            $user_full_name          = '';
            $user_data               = '';
            $receipt_log_value_array = array();
            $insert_taxonomy         = 'receipt_status';
            $invoice_term            = '';
            $invoice_time            = '';
            $response_data           = '';
            $invoiceType             = '';
            $receipt_log_value       = '';
            $total                   = '';
            $series                  = '';
            $serie                   = '';
            $number                  = '';
            $response         = null;
            $response_message = null;
            $response_code    = null;
            $is_timeout       = false;
            $create_json_instance        = new Create_json();
            $Vat_exemption_categories    = $create_json_instance->getVatExemptionCategories();
            $Vat_exemption_categories_en = $create_json_instance->getVatExemptionCategoriesEn();
            $i = 1;

            while ($i <= 1) {
                $mydata_options = get_option('primer_mydata');
                $series = '';

                $transmission_failure = $order->get_meta('transmission_failure_check');
                $post_id_failed       = $order->get_meta('order_id_from_receipt');
                $already_issued       = get_post_meta($post_id_failed, 'response_invoice_mark', true);

                if (($transmission_failure == 1 || $transmission_failure == 3) && empty($already_issued)) {
                    $serie  = get_post_meta($post_id_failed, '_primer_receipt_series', true);
                    $number = get_post_meta($post_id_failed, '_primer_receipt_number', true);

                    $issue_date               = new DateTime("now", new DateTimeZone("Europe/Athens"));
                    $order_datetime_co        = $order->get_meta('order_datetime_failed');

                    if ($order_datetime_co != '') {
                        $issue_date = new DateTime($order_datetime_co, new DateTimeZone("Europe/Athens"));
                    }

                    $gr_time      = $issue_date->format('Y-m-d');
                    $invoice_time = $issue_date->format('H:i');
                } else {
                    $serie  = get_post_meta($order_id, '_primer_receipt_series', true) != null ? get_post_meta($order_id, '_primer_receipt_series', true) != null : '';
                    $number = get_post_meta($order_id, '_primer_receipt_number', true) != null ? get_post_meta($order_id, '_primer_receipt_number', true) : '';
                }

                $invoice_data = $create_json_instance->create_invoice($user_id, $order_id, $total_vat_number, $mydata_options, $primer_license_data,
                            $total, $series, $serie, $number, $currency, $currency_symbol, $user_data, $insert_taxonomy,
                            $classificationCategory, $classificationCategory_en, $response_data, $receipt_log_value, $receipt_log_value_array,
                            $receipt_log_id, $invoice_term, $gr_time, $invoice_time, $order_total_price, $order_invoice_type,
                            $order_vatNumber, $user_order_email, $order_country, $user_full_name, $primer_smtp, $log_ids, $callingFunction, $invoiceType,
                            $classificationType);
                $curl_args['body']  = json_encode($invoice_data);
                $response           = wp_remote_post($url, $curl_args);
                $response_message   = wp_remote_retrieve_body($response);
                $response_code      = wp_remote_retrieve_response_code($response);
                if ($response_code == 400 && json_decode($response_message, true)['response'][0]['errors']['error'][0]['code'] == 228 ) {
                    $create_json_instance->numbering($order_invoice_type, $order_country,$mydata_options, $series);
                    update_option('primer_mydata', $mydata_options);
                    $i = 1;
                } else {
                    $i++;
                }
            }
            if ($invoice_data == "break") {
                break;
            }
            if (is_wp_error($response)) {
                $response_data .= '<div class="primer_popup popup_error"><div><h3>'.$response->get_error_message().'</h3><br><br><br><br><br></div>';
                $response_data .= '<button class="popup_ok button button-primary">OK</button></div>';
                $receipt_log_value_array[] = $response->get_error_message();
                $string_timeout = 'Operation timed out after';
                $message_timeout = $response->get_error_message();
                $check_string_timeout = strpos($message_timeout,$string_timeout);
                if ($check_string_timeout) {
                    $is_timeout = true;
                    $first_time = $order->get_meta('order_date_failed_timeout');
                    if($first_time == null){
                        $order->update_meta_data('order_date_failed_timeout', $gr_time);
                        $order->save();
                    }
                }
            }
            $general_settings = get_option('primer_generals');
            if($general_settings['primer_cron_transmission_failure'] != 'on' && ($response_code == 502 || $response_code == 512)){
                $receipt_log_value .= __('Could not connect to AADE. Please check your general settings if you want to enable transmission failure option or contact Primer Software if the problem persists.', 'primer');
                $response_data .= '<div class="primer_popup popup_error"><div><h3>'.__('Could not connect to AADE. Please check your general settings if you want to enable transmission failure option or contact Primer Software if the problem persists.', 'primer').'</h3><br><br><br><br><br></div>';
                $response_data .= '<button class="popup_ok button button-primary">OK</button></div>';
                $receipt_log_value_array[] = __('Could not connect to AADE. Please check your general settings if you want to enable transmission failure option or contact Primer Software if the problem persists.','primer');
                break;
            }
            $string_0_remaining = 'You have no other Monthly Invoices remaining';
            $check_string_remaining = strpos($response_message,$string_0_remaining);
            if($check_string_remaining !== false && $api_type == 'production'){
                $mydata_options['check_0_remaining'] = 1;
                update_option( 'primer_mydata', $mydata_options );
            }
            if($mydata_options['check_0_remaining'] == 1 && $api_type == 'production'){
                $receipt_log_value .= __('You have no other monthly invoices left.Please go to MyData settings and press "Get Remaining" button if you are sure that the month is passed and you have remaining invoices.', 'primer');
                $response_data .= '<div class="primer_popup popup_error"><div><h3>'.__('You have no other monthly invoices left.Please go to MyData settings and press "Get Remaining" button if you are sure that the month is passed and you have remaining invoices.', 'primer').'</h3><br><br><br><br><br></div>';
                $response_data .= '<button class="popup_ok button button-primary">OK</button></div>';
                $receipt_log_value_array[] = __('You have no other monthly invoices left.Please go to MyData settings and press "Get Remaining" button if you are sure that the month is passed and you have remaining invoices.','primer');
                break;
            }
            $response_to_array = json_decode($response_message);
            if ($response_code == 400 || $response_code == 422) {
                $response_data .= '<div class="primer_popup popup_error"><div><h3>'.$response_message.'</h3><br><br><br><br><br></div>';
                $response_data .= '<button class="popup_ok button button-primary">OK</button></div>';
                $receipt_log_value_array[] = $response_to_array;
                update_post_meta($receipt_log_id, 'receipt_log_error', $receipt_log_value_array);
                break;
            } else if ($response_code == 403) {
                $response_data .= '<div class="primer_popup popup_error"><div><h3>'.$response_message.'</h3><br><br><br><br><br></div>';
                $response_data .= '<button class="popup_ok button button-primary">OK</button></div>';
                $receipt_log_value_array[] = $response_message;
                update_post_meta($receipt_log_id, 'receipt_log_error', $receipt_log_value_array);
                break;
            }
            if (!empty($response_to_array) || ($response_code > 500 || $is_timeout)) {
                $response_from_array = $response_to_array->response;
                if (!empty($response_from_array) || ($response_code > 500 || $is_timeout)) {
                    if (($response_from_array[0]->statusCode == 'Success') || ($response_code > 500 || $is_timeout)) {
                        $invoice_uid = $response_from_array[0]->invoiceUid;
                        $invoice_mark = $response_from_array[0]->invoiceMark;
                        $invoice_authcode = $response_from_array[0]->authenticationCode;
                        $post_id = wp_insert_post(array(
                            'post_type' => 'primer_receipt',
                            'post_title' => 'Receipt for order #' . $id_of_order,
                            'comment_status' => 'closed',
                            'ping_status' => 'closed',
                            'post_status' => 'publish',
                        ));
                        wp_set_object_terms($post_id, $invoice_term, $insert_taxonomy, false);
                        if ($post_id) {
                            $issue_date = new DateTime("now", new DateTimeZone("Europe/Athens"));
                            $invoice_date = $issue_date->format('d/m/Y');
                            $invoice_time = $issue_date->format('H:i');
                            update_post_meta($post_id, 'success_mydata_date', $invoice_date);
                            update_post_meta($post_id, 'success_mydata_time', $invoice_time);
                            update_post_meta($post_id, 'receipt_type', $invoice_term);
                            update_post_meta($post_id, 'send_to_api_type', $api_type);
                            update_post_meta($post_id, 'order_id_to_receipt', $id_of_order);
                            $order->update_meta_data('order_id_from_receipt', $post_id);
                            add_post_meta($post_id, 'receipt_client', $user_data);
                            add_post_meta($post_id, 'receipt_client_id', $user_id);
                            add_post_meta($post_id, 'receipt_price', $order_total_price . ' ' .$currency_symbol);
                            update_post_meta( $post_id, '_primer_receipt_number', $number );
                            update_post_meta( $post_id, '_primer_receipt_series', $serie );
                            $create_json_instance->numbering($order_invoice_type, $order_country,$mydata_options, $series);
                            update_option('primer_mydata', $mydata_options);
                            if (!empty($invoice_uid)) {
                                update_post_meta($post_id, 'response_invoice_uid', $invoice_uid);
                            }
                            if (!empty($invoice_mark)) {
                                update_post_meta($post_id, 'response_invoice_mark', $invoice_mark);
                            }
                            if (!empty($invoice_authcode)) {
                                update_post_meta($post_id, 'response_invoice_authcode', $invoice_authcode);
                            }
                            if (!isset($primer_license_data['currentBranchID'])) {
                                $currentBranchID = 0;
                            } else {
                                $currentBranchID = $primer_license_data['currentBranchID'];
                            }
                            update_post_meta($post_id, 'branchID', $currentBranchID);
                            if ($response_code == 512) {
                                update_post_meta($post_id, 'connection_fail_message', 'ΑΔΥΝΑΜΙΑ ΔΙΑΣΥΝΔΕΣΗΣ ΠΑΡΟΧΟΥ - ΑΑΔΕ');
                            } elseif ($response_code > 500 || $is_timeout) {
                                update_post_meta($post_id, 'connection_fail_message', 'ΑΔΥΝΑΜΙΑ ΔΙΑΣΥΝΔΕΣΗΣ ΟΝΤΟΤΗΤΑΣ - ΠΑΡΟΧΟΥ');
                            } else {
                                update_post_meta($post_id, 'connection_fail_message', '');
                            }
                            $order->save();
                        }
                        $order_log_id = wc_get_order($id_of_order)->get_meta('log_id_for_order');
                        if ($order_log_id) {
                            update_post_meta($post_id, 'log_id_for_order', $order_log_id);
                            $invoice_date = get_the_date('F j, Y', $post_id);
                            update_post_meta($order_log_id, 'receipt_log_invoice_id', $post_id);
                            update_post_meta($order_log_id, 'receipt_log_invoice_date', $invoice_date);
                            update_post_meta($order_log_id, 'receipt_log_client', $user_data);
                            $get_issue_status = get_post_meta($post_id, 'receipt_status', true);
                            if(empty($get_issue_status)) {
                                $get_issue_status = 'issued';
                            }
                            update_post_meta($order_log_id, 'receipt_log_status', $get_issue_status);
                            update_post_meta($order_log_id, 'receipt_log_error', $receipt_log_value);
                            if($receipt_log_value != null){
                                update_post_meta($order_log_id, 'receipt_log_total_status', 'only_errors');
                            }
                        }
                        $generated_uid = strtoupper(sha1(iconv("UTF-8", "ISO-8859-7",strval($invoice_data['invoice'][0]['issuer']['vatNumber']).'-'.strval($invoice_data['invoice'][0]['invoiceHeader']['issueDate']).'-'.strval($invoice_data['invoice'][0]['issuer']['branch']).'-'.strval($invoice_data['invoice'][0]['invoiceHeader']['invoiceType']).'-'.strval($invoice_data['invoice'][0]['invoiceHeader']['series']).'-'.strval($invoice_data['invoice'][0]['invoiceHeader']['aa']))));
                        primer_generate_qr($post_id, $generated_uid);
                        $primer_options = new Primer_Options();
                        $post_ids_str = '';
                        if (!empty($post_id)) {
                            $post_ids_str = $total_vat_number . $invoice_mark;
                        }
                        $post_arr_id = explode(" ", $post_id);
                        $use_url_params = '?type_logo=id';
                        $generate_html_response = $primer_options->export_receipt_as_static_html_by_page_id($post_arr_id, $use_url_params);
                        $upload_dir = wp_upload_dir()['basedir'];
                        if ($generate_html_response) {
                            $all_files = $upload_dir . '/exported_html_files/tmp_files';
                            $files = $primer_options->get_all_files_as_array($all_files);
                            $zip_file_name = $upload_dir . '/exported_html_files/'.$post_ids_str.'_html.zip';
                            ob_start();
                            echo esc_url($primer_options->create_zip($files, $zip_file_name, $all_files . '/'));
                            $create_zip = ob_get_clean();
                            if ($create_zip == 'created') {
                                $primer_options->rmdir_recursive($upload_dir . '/exported_html_files/tmp_files');
                            }
                        }
                        $log_id = $order->get_meta('log_id_for_order');
                        $json = get_post_meta($log_id,'json_send_to_api',true);
                        $data = json_decode($json, true);
                        $post_url = get_the_permalink($post_id);
                        $post_url = $post_url . '?receipt=view&username='.$primer_license_data['username'];
                        $arrContextOptions = array(
                                    "ssl" => array(
                                        "verify_peer" => false,
                                        "verify_peer_name" => false,
                                    ),
                                    "http" => array(
                                        "header" => [
                                            "User-Agent: Mozilla/5.0",
                                            "Content-type: text/html; charset=utf-8\r\n"
                                        ],
                                    ),
                                );
                        $context = stream_context_create($arrContextOptions);
                        $homepage = file_get_contents($post_url, false, $context);
                        $modified_homepage =  $homepage;
                        $varExemptionCategory = array();
                        foreach ($data['invoice'][0]['invoiceDetails'] as $invoiceDetails) {
                            if ( $invoiceDetails['vatExemptionCategory'] != null){
                                $varExemptionCategory[] = $invoiceDetails['vatExemptionCategory'];
                            }
                        }
                        $varExemptionCategory = array_unique($varExemptionCategory);
                        $varExemptionCategory = array_values($varExemptionCategory);
                        $count = count($varExemptionCategory);
                        $is_greece = $order->get_meta('_billing_country') == 'GR' ? true : false;
                        if ($is_greece) {
                            if ( $count>0 ) {
                                $exception_vat = '<div><span class="skin bold">ΑΠΑΛΛΑΓΗ ΑΠΟ ΤΟ Φ.Π.Α :</span></div>';
                                for ($i = 0; $i < $count; $i++){
                                    $exception_vat .= '<div>'.$Vat_exemption_categories[$varExemptionCategory[$i]].'</div>';
                                }
                            } else {
                                $exception_vat = '';
                            }
                            $modified_homepage = str_replace('<div class="cont_notation"><span class="skin bold">ΠΑΡΑΤΗΡΗΣΕΙΣ:</span>', '<div class="cont_notation">' . $exception_vat . '<span class="skin bold">ΠΑΡΑΤΗΡΗΣΕΙΣ:</span>', $modified_homepage);
                        }
                        else {
                            if ( $count>0 ) {
                                $exception_vat = '<div><span class="skin bold">EXEMPTION FROM VAT :</span></div>';
                                for ($i = 0; $i < $count; $i++) {
                                    $exception_vat .= '<div>' . $Vat_exemption_categories_en[$varExemptionCategory[$i]] . '</div>';
                                }
                            } else {
                                $exception_vat = '';
                            }
                            $modified_homepage = str_replace('<div class="cont_notation"><span class="skin bold">COMMENTS:</span>', '<div class="cont_notation">' . $exception_vat . '<span class="skin bold">COMMENTS:</span>', $modified_homepage);
                        }
                        $isDeliveryNote       = $invoice_data['invoice'][0]['invoiceHeader']['isDeliveryNote'];
                        $deliveryStyle        = $isDeliveryNote ? 'height: 140px' : 'height: 120px';
                        $deliveryStyleSecond  = $isDeliveryNote ? 'height: 180px' : 'height: 160px';
                        $is_receipt = $order->get_meta('_billing_invoice_type') == 'receipt' ? true : false;
                        if ($is_receipt && $is_greece)  {
                            $modified_homepage = str_replace('<div class="information left">', '<div class="information left" style="' . esc_attr($deliveryStyle) . '">', $modified_homepage);
                            $modified_homepage = str_replace('<div class="information right">', '<div class="information right" style="' . esc_attr($deliveryStyle) . '">', $modified_homepage);
                        }
                        elseif ($is_receipt && !$is_greece) {
                            $modified_homepage = str_replace('<div class="information left">', '<div class="information left" style="' . esc_attr($deliveryStyle) . '">', $modified_homepage);
                            $modified_homepage = str_replace('<div class="information right">', '<div class="information right" style="' . esc_attr($deliveryStyle) . '">', $modified_homepage);
                        }
                        elseif (!$is_receipt && $is_greece) {
                            $modified_homepage = str_replace('<div class="information left">', '<div class="information left" style="' . esc_attr($deliveryStyleSecond) . '">', $modified_homepage);
                            $modified_homepage = str_replace('<div class="information right">', '<div class="information right" style="' . esc_attr($deliveryStyleSecond) . '">', $modified_homepage);
                        }
                        elseif (!$is_receipt && !$is_greece) {
                            $modified_homepage = str_replace('<div class="information left">', '<div class="information left" style="' . esc_attr($deliveryStyleSecond) . '">', $modified_homepage);
                            $modified_homepage = str_replace('<div class="information right">', '<div class="information right" style="' . esc_attr($deliveryStyleSecond) . '">', $modified_homepage);
                        }
                        if (!$is_greece) {
                            $modified_homepage = str_replace('<tr><p class="table_titles">CUSTOMER INFORMATION</p>', '<tr><td colspan="2" style="text-align: center;"><p class="table_titles" style="white-space: nowrap; text-align: center;">CUSTOMER INFORMATION</p></td></tr><tr>',$modified_homepage);
                            $modified_homepage = str_replace('<tr><p class="table_titles">OTHER INFORMATION</p>', '<tr><td colspan="2" style="text-align: center;"><p class="table_titles">OTHER INFORMATION</p></td></tr><tr>',$modified_homepage);
                        } else {
                            $modified_homepage = str_replace('<tr><p class="table_titles">ΣΤΟΙΧΕΙΑ ΠΕΛΑΤΗ</p>', '<tr><td colspan="2" style="text-align: center;"><p class="table_titles" style="white-space: nowrap; text-align: center;">ΣΤΟΙΧΕΙΑ ΠΕΛΑΤΗ</p></td></tr><tr>',$modified_homepage);
                            $modified_homepage = str_replace('<tr><p class="table_titles">ΛΟΙΠΑ ΣΤΟΙΧΕΙΑ</p>', '<tr><td colspan="2" style="text-align: center;"><p class="table_titles">ΛΟΙΠΑ ΣΤΟΙΧΕΙΑ</p></td></tr><tr>',$modified_homepage);
                        }
                        $options = new Options();
                        $options->set('isHtml5ParserEnabled', true);
                        $options->set('isPhpEnabled', true);
                        $options->set('isRemoteEnabled', true);
                        $options->set('defaultFont', 'DejaVu Sans');
                        $dompdf = new Dompdf($options);
                        $dompdf->setPaper('A4', 'portrait');
                        $dompdf->loadHtml($modified_homepage);
                        $dompdf->render();
                        $upload_dir = wp_upload_dir()['basedir'];
                        if (!file_exists($upload_dir . '/email-invoices')) {
                            mkdir($upload_dir . '/email-invoices');
                        }
                        $post_name = get_the_title($post_id);
                        $post_name = str_replace(' ', '_', $post_name);
                        $post_name = str_replace('#', '', $post_name);
                        $post_name = strtolower($post_name);
                        $post_name = sanitize_text_field($post_name);
                        $output = $dompdf->output();
                        $upload_dir_file = $upload_dir . '/email-invoices/'.$post_name.'.pdf';
                        if (!empty(realpath($upload_dir . '/email-invoices/'))) {
                            file_put_contents($upload_dir_file, $output);
                        }
                        $attachments = $upload_dir . '/email-invoices/'.$post_name.'.pdf';
                        $post_issued = 'issued';
                        update_post_meta($post_id, 'receipt_status', $post_issued);
                        $order->update_meta_data('receipt_status', $post_issued);
                        $order->save();
                        add_post_meta($post_id, 'receipt_client', $user_data);
                        add_post_meta($post_id, 'receipt_client_id', $user_id);
                        add_post_meta($post_id, 'receipt_price', $order_total_price . ' ' .$currency_symbol);
                        $primer_smtp_options = get_option('primer_emails');
                        if (!empty($primer_smtp_options['email_subject'])) {
                            $primer_smtp_subject = $primer_smtp_options['email_subject'];
                        } else {
                            $primer_smtp_subject = __('Test email subject', 'primer');
                        }
                        if (!empty($primer_smtp_options['quote_available_content'])) {
                            $primer_smtp_message = $primer_smtp_options['quote_available_content'];
                            $client_first_name = $order->get_meta('_billing_first_name');
                            $client_last_name  = $order->get_meta('_billing_last_name');
                            $client_email      = $order->get_meta('_billing_email');
                            $streetAddress     = $order->get_meta('_billing_address_1');
                            $townCity          = $order->get_meta('_billing_city');
                            $phone             = $order->get_meta('_billing_phone');
                            $primer_smtp_message = str_replace('{ClientFirstName}', $client_first_name, $primer_smtp_message);
                            $primer_smtp_message = str_replace('{ClientLastName}', $client_last_name, $primer_smtp_message);
                            $primer_smtp_message = str_replace('{ClientEmail}', $client_email, $primer_smtp_message);
                            $primer_smtp_message = str_replace('{StreetAddress}', $streetAddress, $primer_smtp_message);
                            $primer_smtp_message = str_replace('{TownCity}', $townCity, $primer_smtp_message);
                            $primer_smtp_message = str_replace('{Phone}', $phone, $primer_smtp_message);
                        } else {
                            $primer_smtp_message = __('Test email message', 'primer');
                        }
                        $primer_automatically_send_file = $primer_smtp_options['automatically_send_on_conversation'];
                        $primer_smtp_type = $primer_smtp_options['smtp_type'];
                        if (empty($primer_automatically_send_file)) {
                            $primer_automatically_send_file = 'yes';
                        }
                        $email_logs = '';
                        if (!empty($primer_automatically_send_file) && $primer_automatically_send_file === 'yes' && $order->get_billing_email() != '' && $order->get_billing_email() != null) {
                            $mailResult = false;
                            $primer_smtp = PrimerSMTP::get_instance();
                            if (!empty($primer_smtp_options['email_from_name'])) {
                                $from_name_email = $primer_smtp_options['email_from_name'];
                            } else {
                                $from_name_email = '';
                            }
                            if ($primer_smtp_type == 'wordpress_default') {
                                $headers = array('Content-Type: text/html; charset=UTF-8');
                                $mailResultSMTP = wp_mail($order->get_billing_email(),$primer_smtp_subject,$primer_smtp_message,$headers,$attachments);
                            } else {
                                $mailResultSMTP = $primer_smtp->primer_mail_sender($order->get_billing_email(),$from_name_email, $primer_smtp_subject, $primer_smtp_message, $attachments);
                            }
                            if (! $primer_smtp->credentials_configured()) {
                                $email_logs .= __('Configure your SMTP credentials', 'primer') ."\n";
                            }
                            if (! $primer_smtp->credentials_configured()) {
                                $email_logs .= __('Configure your SMTP credentials', 'primer') ."\n";
                            }
                            if (!empty($mailResultSMTP['error']) && ! $primer_smtp->credentials_configured()) {
                                $response_data .= '<div class="primer_popup popup_error"><div><h3>'.$GLOBALS['phpmailer']->ErrorInfo.'</h3><br><br><br><br><br></div>';
                                $response_data .= '<button class="popup_ok button button-primary">OK</button></div>';
                                update_post_meta($order_log_id, 'receipt_log_email', 'not_sent');
                                $email_logs .= $GLOBALS['phpmailer']->ErrorInfo ."\n";
                                update_post_meta($order_log_id, 'receipt_log_email_error', $email_logs);
                                update_post_meta($order_log_id, 'receipt_log_total_status', 'only_errors');
                            } else {
                                update_post_meta($order_log_id, 'receipt_log_email', 'sent');
                                update_post_meta($order_log_id, 'receipt_log_total_status', 'only_issued');
                            }
                            update_post_meta($post_id, 'exist_error_log', 'exist_log');
                        } else {
                            if (! $primer_smtp->credentials_configured()) {
                                $email_logs .= __('Configure your SMTP credentials', 'primer') ."\n";
                            }
                            $email_logs .= __('Send email automatically on order conversion disabled', 'primer') ."\n";
                            update_post_meta($order_log_id, 'receipt_log_email', 'not_sent');
                            update_post_meta($order_log_id, 'receipt_log_email_error', $email_logs);
                            update_post_meta($order_log_id, 'receipt_log_total_status', 'only_issued');
                        }
                        $order->update_meta_data('transmission_failure_check', 2);
                        $order->update_meta_data('failed_48', 'no');
                        $order->save();
                        $mydata_options['timeout_check_48'] = 0;

                        if (($response_code > 500 || $is_timeout) && $general_settings['primer_cron_transmission_failure'] == 'on') {
                            if ($response_code == 512 ) {
                                $receipt_log_value .= __('Unable to connect Provider and AADE.', 'primer');
                                $receipt_log_value_array[] = __('Unable to connect Provider and AADE.', 'primer');
                                $order->update_meta_data('transmission_failure_check', 3);
                                $order->save();
                                update_post_meta($receipt_log_id, 'receipt_log_error', $receipt_log_value_array);
                            } else {
                                $receipt_log_value .= __('Unable to connect Entity and provider..', 'primer');
                                $receipt_log_value_array[] = __('Unable to connect Entity and provider.', 'primer');
                                $order->update_meta_data('transmission_failure_check', 1);
                                $order->save();
                                update_post_meta($receipt_log_id, 'receipt_log_error', $receipt_log_value_array);
                            }
                            $gr_timezone = new DateTimeZone("Europe/Athens");
                            $gr_time = new DateTime("now", $gr_timezone);
                            $order->update_meta_data('order_date_failed', $gr_time->format('Y-m-d'));
                            $order->update_meta_data('order_datetime_failed', $gr_time->format('Y-m-d H:i:s'));
                            $order->save();
                        }
                        $response_data = '<div class="primer_popup popup_success"><div>';
                        $response_data .= '<h3>'.__("Orders converted", "primer").'</h3><br><br><br><br><br>';
                        $response_data .= '<button class="popup_ok button button-primary">OK</button>';
                        $response_data .= '</div></div>';
                    } else {
                        update_option('primer_mydata', $mydata_options);
                        $response_data .= is_object($response_from_array[0]->errors) ? json_encode($response_from_array[0]->errors) : $response_from_array[0]->errors;
                        $receipt_log_value_array[] = $response_data;
                        continue;
                    }
                }
            }
            if (!empty($receipt_log_value_array)) {
                update_post_meta($receipt_log_id, 'receipt_log_error', $receipt_log_value_array);
                update_post_meta($receipt_log_id, 'receipt_log_total_status', 'only_errors');
            }
        }
		if (!empty($log_ids)) {
			foreach ($log_ids as $log_id) {
				update_post_meta($log_id, 'receipt_log_error', $receipt_log_value_array);
                if(!empty($receipt_log_value_array)){
                    update_post_meta($log_id, 'receipt_log_total_status', 'only_errors');
                }
			}
		}
        $response = array(
                'status' => 'success',
                'data' => $response_data
                );

        echo json_encode($response);
    }

    delete_transient('convert_order_to_invoice_lock');
    wp_die();
}

add_action('admin_footer', 'fetch_primer_script');
function fetch_primer_script():void {
	$screen = get_current_screen();

	?>
	<?php
	 if ( $screen->id != "toplevel_page_wp_ajax_list_order" && $screen->id != "admin_page_primer_receipts_logs" && $screen->id != "admin_page_primer_receipts_logs_automation") {
		return;
	}
	 ?>

	<script>
		/**
		 * Create and download a temporary file.
		 *
		 * @param {string} filename - File name.
		 * @param {string} text - File content.
		 */
		function download(filename, text) {
			// Create temporary element.
			var element = document.createElement('a');
			element.setAttribute('href', 'data:application/json;charset=<?php echo esc_html( get_option( 'blog_charset' ) ); ?>,' + encodeURIComponent(text));
			element.setAttribute('download', filename+'.json');
			// Set the element to not display.
			element.style.display = 'none';
			document.body.appendChild(element);
			// Simulate click on the element.
			element.click();
			// Remove temporary element.
			document.body.removeChild(element);
		}

        (function ($) {
            function check_exist_receipts(orders) {
                var order_arr = new Array();
                $(orders).each(function (i, el) {
                    var tr_parent = $(el).parents('tr');
                    var sibling_td = tr_parent.find('td.receipt_id');
                    var transmission_failure_column =tr_parent.find('td.transmission_failure');
                    var tran_failure = transmission_failure_column.text();
                    if (sibling_td) {
                        var td_status = sibling_td.text();
                    }
                    if (td_status !== '' && tran_failure !=='Αδυναμία σύνδεσης') {
                        $(el).prop('checked', false);
                    }
                    var order_id = $(el).val();
                    if (order_id) {
                        order_arr.push(order_id);
                    }
                })
            }

            function check_request_receipts(orders) {
                var data_status = '';
                var data_status_json = '';
                var stop_conversion = '';
                var failed_48_system = '';
                <?php
                	$mydata_options = get_option('primer_mydata');
                    if(is_array($mydata_options) && array_key_exists('last_request',$mydata_options)){
                	$last_request = $mydata_options['last_request'];
                    }

                      if(is_array($mydata_options) && !array_key_exists('timeout_check_48',$mydata_options)){
                         $mydata_options['timeout_check_48'] = '0';
                     }
                     $transmission_failure_last_request = '';
                	if (!empty($last_request)) {
                        $order = wc_get_order($last_request[0]);
                        $order_status = $order->get_meta('receipt_status');
                        $send_receipt_json = $order->get_meta('order_id_from_receipt');
                        $transmission_failure_last_request = $order->get_meta('transmission_failure_check');
                		?>
                		data_status = '<?php echo $order_status; ?>';
                		data_status_json = '<?php echo $send_receipt_json; ?>';
                	<?php }
                ?>

                        failed_48_system = '<?php echo $mydata_options['timeout_check_48']; ?>';
                        $(orders).each(function (i, el) {
                    var tr_parent = $(el).parents('tr');
                    var failure48_column = tr_parent.find('td.accept_48');
                    var failed_48 = failure48_column.text();
                    if (failed_48_system === '1' && failed_48 !== 'yes') {
                     stop_conversion = 'stop';
                    }
                });
                return true;
            }

            function __(text, domain) {
                return text;
            }

            $('.submit_convert_orders').on('click', function (e) {
                $('.submit_convert_orders').attr('disabled', true);
                e.preventDefault();
                check_exist_receipts($('input[name="orders[]"]:checked'));
                var count_orders = $('input[name="orders[]"]:checked').length;
                var receipt_word = count_orders == 1 ? 'receipt' : 'receipts';
                var confirmation = confirm('You are about to issue ' + count_orders + ' ' + receipt_word + '. Are you sure?');
                var validation = check_request_receipts($('input[name="orders[]"]:checked'));
                if (confirmation == true && count_orders > 0 && validation == true) {
                    var data = $('#tables-filter').serialize();

                    $.ajax({
                        url: ajaxurl,
                        data: data,
                        beforeSend: function () {
                            var table = $('table.table-view-list');
                            table.css({ 'opacity': '0.5' });
                            // Get the z-index of the table-view-list and add 1 to make sure the spinner is on top
                            var tableZIndex = parseInt(table.css('z-index'), 10) || 0;
                            $('.loadingio-spinner-spinner-chyosfc7wi6').css({ 'z-index': tableZIndex + 1 });
                            $('.loadingio-spinner-spinner-chyosfc7wi6').show();
                        },
                        success: function (response) {
                            console.log(response);
                            var parsedResponse = JSON.parse(response);
                            var response_data = parsedResponse.data;
                            $('#wpbody-content').prepend(response_data);
                            popupOpenClose('.primer_popup');
                            // Add event listener to the "OK" button
                            $('.popup_ok').on('click', function () {
                                location.reload();
                            });
                            $('.loadingio-spinner-spinner-chyosfc7wi6').hide();
                            $('table.table-view-list.orders').css({ 'opacity': '1' });
                            popupOpenClose('.primer_popup');
                            $(document).mouseup(function (e) {
                                var container = $('.primer_popup > div');
                                if (!container.is(e.target) && container.has(e.target).length === 0) {
                                    document.location.reload();
                                }
                            });
                            setTimeout( function () {document.location.reload()}, 5000);
                        }
                    });
                } else {
                    return false;
                }
            });

             function popupOpenClose(popup) {
              if ($('.popup_wrapper').length == 0) {
                $(popup).wrapInner("<div class='popup_wrapper'></div>");
              }
              $(popup).show();

              $('.popup_wrapper').on('click', function(e) {
                if (e.target == this) {
                  if ($(popup).is(':visible')) {
                    $(popup).hide();
                    location.reload();
                  }
                }
              });

              $(popup).find('.popup_ok').on('click', function(e) {
                $(popup).hide();
                location.reload();
              });
            }

            $('#primer-download-order-json').on('click', function (e) {
                e.preventDefault();
                var find_field = $(this).prev();
                var input_field;
                if (find_field.is('input')) {
                    input_field = find_field;
                    var data_id = $(input_field).attr('data-parent_order');
                    download( '<?php echo esc_html(__('Send JSON for order ')) ?>' + data_id, $(input_field).val() );
                }

            });

            $('#primer-download-html-json').on('click', function (e) {
                e.preventDefault();
                var find_field = $(this).prev();
                var input_field;
                if (find_field.is('input')) {
                    input_field = find_field;
                    var data_id = $(input_field).attr('data-parent_order');
                    download( '<?php echo esc_html(__('Send HTML JSON for order ')) ?>' + data_id, $(input_field).val() );
                }
            });

		})(jQuery)
	</script>
<?php }

function check_zone_country($country):bool {
	$country_response = false;
	$eu_countries = array(
		"AT" => "Austria",
		"BE" => "Belgium",
        "BG" => "Bulgaria",
		"CY" => "Cyprus",
        "HR" => "Croatia",
        "CZ" => "Czechia",
        "DK" => "Denmark",
		"EE" => "Estonia",
		"FI" => "Finland",
		"FR" => "France",
		"DE" => "Germany",
		"GR" => "Greece",
        "HU" => "Hungary",
		"IE" => "Ireland",
		"IT" => "Italy",
		"LV" => "Latvia",
		"LT" => "Lithuania",
		"LU" => "Luxembourg",
		"MT" => "Malta",
		"NL" => "Netherlands",
        "PL" => "Poland",
		"PT" => "Portugal",
        "RO" => "Romania",
		"SK" => "Slovak Republic",
		"SI" => "Slovenia",
		"ES" => "Spain",
		"SE" => "Sweden"
	);
		if (array_key_exists($country, $eu_countries)) {
		$country_response = true;
	}
	return $country_response;
}