plugin_name = $plugin_name; $this->version = $version; $this->load_dependencies(); add_action('wp_ajax_hesabix_get_sync_stats_ajax', array($this, 'hesabix_get_sync_stats_ajax')); add_action('wp_ajax_hesabix_load_tab_content', array($this, 'hesabix_load_tab_content')); add_action('wp_ajax_hesabix_save_customers_fields_ajax', array($this, 'hesabix_save_customers_fields_ajax')); } public function hesabix_update_db_check() { $current_db_ver = get_site_option('hesabix_db_version'); if ($current_db_ver === false || $current_db_ver < 1.1) { global $wpdb; $table_name = $wpdb->prefix . "hesabix"; $sql = "ALTER TABLE $table_name ADD `id_ps_attribute` INT(11) UNSIGNED NOT NULL DEFAULT 0 AFTER `id_ps`;"; if (!$wpdb->query($sql)) { HesabixLogService::log(array("Cannot alter table $table_name. Current DB Version: $current_db_ver")); } else { update_option('hesabix_db_version', 1.1); HesabixLogService::log(array("Alter table $table_name. Current DB Version: $current_db_ver")); } } } public function enqueue_styles() { if (isset($_GET['page']) && str_contains($_GET['page'], "hesabix")) { ?> plugin_name, plugin_dir_url(__FILE__) . '../assets/css/hesabix-admin.css', array(), $this->version, 'all'); wp_enqueue_style('bootstrap_css', plugin_dir_url(__FILE__) . '../assets/css/bootstrap.css', array(), $this->version, 'all'); } } public function enqueue_scripts() { wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . '../assets/js/hesabix.js', array('jquery'), $this->version, false); wp_localize_script($this->plugin_name, 'hesabix_ajax', array( 'ajaxurl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('hesabix_ajax_nonce') )); if (isset($_GET['page']) && str_contains($_GET['page'], "hesabix")) { wp_enqueue_script('bootstrap_js', plugin_dir_url(__FILE__) . '../assets/js/bootstrap.bundle.min.js', array('jquery'), $this->version, false); } } private function load_dependencies() { require_once plugin_dir_path(dirname(__FILE__)) . '/admin/partials/hesabix-admin-display.php'; require_once plugin_dir_path(dirname(__FILE__)) . '/admin/partials/hesabix-html-output.php'; require_once plugin_dir_path(dirname(__FILE__)) . '/admin/partials/hesabix-admin-setting.php'; require_once plugin_dir_path(dirname(__FILE__)) . '/admin/partials/hesabix-admin-functions.php'; } public function hesabix_missing_notice() { echo '

' . sprintf(__('Hesabix Plugin requires the %s to work!', 'hesabix'), '' . __('WooCommerce', 'hesabix') . '') . '

'; } public function hesabix_live_mode_notice() { echo '

' . __('Hesabix Plugin need to connect to Hesabix Accounting, Please check the API credential!', 'hesabix') . '

'; } public function hesabix_business_expired_notice() { echo '

' . __('Cannot connect to Hesabix. Business expired.', 'hesabix') . '

'; } public function hesabix_currency_notice() { echo '

' . __('Hesabix Plugin cannot works! because WooCommerce currency in not match with Hesabix.', 'hesabix') . '

'; } public function hesabix_general_notices() { if (!empty($_REQUEST['submit_selected_orders_invoice_in_hesabix'])) { if (!empty($_REQUEST['error_msg']) && $_REQUEST['error_msg'] == "select_max_10_items") { printf( '

%s

', __('Error: Select maximum 10 orders. Due to some limitations in Hesabix API, sending too many requests in one minute is not possible.', 'hesabix') ); } else { $success_count = intval($_REQUEST['success_count']); printf('

%s %d

', __('Selected orders invoices have been saved. Number of saved invoices: ', 'hesabix'), $success_count); } } } public function adminExportProductsCallback() { if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { $batch = wc_clean($_POST['batch']); $totalBatch = wc_clean($_POST['totalBatch']); $total = wc_clean($_POST['total']); $updateCount = wc_clean($_POST['updateCount']); $func = new Hesabix_Admin_Functions(); $result = $func->exportProducts($batch, $totalBatch, $total, $updateCount); if ($result['error']) { if ($updateCount === -1) { $result["redirectUrl"] = admin_url('admin.php?page=hesabix-option&tab=export&productExportResult=false&error=-1'); } else { $result["redirectUrl"] = admin_url('admin.php?page=hesabix-option&tab=export&productExportResult=false'); } } else { $result["redirectUrl"] = admin_url('admin.php?page=hesabix-option&tab=export&productExportResult=true&processed=' . $result['updateCount']); } echo json_encode($result); die(); } } public function adminImportProductsCallback() { if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { $batch = wc_clean($_POST['batch']); $totalBatch = wc_clean($_POST['totalBatch']); $total = wc_clean($_POST['total']); $updateCount = wc_clean($_POST['updateCount']); $func = new Hesabix_Admin_Functions(); $result = $func->importProducts($batch, $totalBatch, $total, $updateCount); $import_count = $result['updateCount']; if ($result['error']) { if ($import_count === -1) { $result["redirectUrl"] = admin_url('admin.php?page=hesabix-option&tab=export&productImportResult=false&error=-1'); } else { $result["redirectUrl"] = admin_url('admin.php?page=hesabix-option&tab=export&productImportResult=false'); } } else { $result["redirectUrl"] = admin_url('admin.php?page=hesabix-option&tab=export&productImportResult=true&processed=' . $import_count); } echo json_encode($result); die(); } } public function adminExportProductsOpeningQuantityCallback() { if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { $batch = wc_clean($_POST['batch']); $totalBatch = wc_clean($_POST['totalBatch']); $total = wc_clean($_POST['total']); $func = new Hesabix_Admin_Functions(); $result = $func->exportOpeningQuantity($batch, $totalBatch, $total); if ($result['error']) { if ($result['errorType'] == 'shareholderError') { $result["redirectUrl"] = admin_url('admin.php?page=hesabix-option&tab=export&productOpeningQuantityExportResult=false&shareholderError=true'); } else if ($result['errorType'] == 'noProduct') { $result["redirectUrl"] = admin_url('admin.php?page=hesabix-option&tab=export&productOpeningQuantityExportResult=false&noProduct=true'); } else { $result["redirectUrl"] = admin_url('admin.php?page=hesabix-option&tab=export&productOpeningQuantityExportResult=false'); } } else { if ($result["done"] == true) update_option('hesabix_use_export_product_opening_quantity', true); $result["redirectUrl"] = admin_url('admin.php?page=hesabix-option&tab=export&productOpeningQuantityExportResult=true'); } echo json_encode($result); die(); } } public function adminExportCustomersCallback() { if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { $batch = wc_clean($_POST['batch']); $totalBatch = wc_clean($_POST['totalBatch']); $total = wc_clean($_POST['total']); $updateCount = wc_clean($_POST['updateCount']); $func = new Hesabix_Admin_Functions(); $result = $func->exportCustomers($batch, $totalBatch, $total, $updateCount); if ($result["error"]) { $result["redirectUrl"] = admin_url('admin.php?page=hesabix-option&tab=export&customerExportResult=false'); } else { $result["redirectUrl"] = admin_url('admin.php?page=hesabix-option&tab=export&customerExportResult=true&processed=' . $result["updateCount"]); } echo json_encode($result); die(); } } public function adminSyncChangesCallback() { if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { include(dirname(__FILE__) . '/../includes/class-hesabix-webhook.php'); new Hesabix_Webhook(); wp_send_json(array( 'success' => true, 'message' => __('changes synced successfully', 'hesabix') )); } } public function admin_sync_products_callback() { if (!wp_verify_nonce($_POST['nonce'], 'hesabix_ajax_nonce')) { wp_send_json_error('Security check failed'); } if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { $batch = wc_clean($_POST['batch']); $totalBatch = wc_clean($_POST['totalBatch']); $total = wc_clean($_POST['total']); $func = new Hesabix_Admin_Functions(); $result = $func->syncProducts($batch, $totalBatch, $total); if ($result['error']) { wp_send_json(array( 'success' => false, 'message' => __('Error syncing prices and inventory. Please check the logs.', 'hesabix') )); } else { wp_send_json(array( 'success' => true, 'message' => __('Prices and inventory synced successfully', 'hesabix') )); } } } public function admin_sync_orders_callback() { if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { $batch = wc_clean($_POST['batch']); $totalBatch = wc_clean($_POST['totalBatch']); $total = wc_clean($_POST['total']); $updateCount = wc_clean($_POST['updateCount']); $from_date = wc_clean($_POST['date']); $end_date = wc_clean($_POST['endDate']); $func = new Hesabix_Admin_Functions(); $result = $func->syncOrders($from_date, $end_date, $batch, $totalBatch, $total, $updateCount); if (!$result['error']) { wp_send_json(array( 'success' => true, 'message' => __('Orders synchronized successfully.', 'hesabix') )); } else { wp_send_json(array( 'success' => false, 'message' => $result['error'] )); } } } public function admin_update_products_callback() { if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { $batch = wc_clean($_POST['batch']); $totalBatch = wc_clean($_POST['totalBatch']); $total = wc_clean($_POST['total']); $func = new Hesabix_Admin_Functions(); $result = $func->updateProductsInHesabixBasedOnStore($batch, $totalBatch, $total); if ($result['error']) { wp_send_json(array( 'success' => false, 'message' => __('Error updating products. Please check the logs.', 'hesabix') )); } else { wp_send_json(array( 'success' => true, 'message' => __('Products updated successfully', 'hesabix') )); } } } public function admin_update_wc_products_callback() { if (!wp_verify_nonce($_POST['nonce'], 'hesabix_ajax_nonce')) { wp_send_json_error('Security check failed'); } if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { $batch = wc_clean($_POST['batch']); $totalBatch = wc_clean($_POST['totalBatch']); $total = wc_clean($_POST['total']); $func = new Hesabix_Admin_Functions(); $result = $func->updateWooCommerceProductsBasedOnHesabix($batch, $totalBatch, $total); if ($result['error']) { wp_send_json(array( 'success' => false, 'message' => __('Error updating products. Please check the logs.', 'hesabix') )); } else { wp_send_json(array( 'success' => true, 'message' => __('Products updated successfully', 'hesabix') )); } } } public function adminUpdateProductsWithFilterCallback() { if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { $offset = wc_clean($_POST['offset']); $rpp = wc_clean($_POST['rpp']); if (abs($rpp - $offset) <= 200) { $func = new Hesabix_Admin_Functions(); $result = $func->updateProductsInHesabixBasedOnStoreWithFilter($offset, $rpp); if ($result['error']) { $result["redirectUrl"] = admin_url('admin.php?page=hesabix-option&tab=sync&$productUpdateWithFilterResult=false'); } else { $result["redirectUrl"] = admin_url('admin.php?page=hesabix-option&tab=sync&$productUpdateWithFilterResult=true'); } echo json_encode($result); die(); } else { $result["redirectUrl"] = admin_url('admin.php?page=hesabix-option&tab=sync&$productUpdateWithFilterResult=false'); echo json_encode($result); die(); } } } public function adminSubmitInvoiceCallback() { HesabixLogService::writeLogStr('Submit Invoice Manually'); if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { $orderId = wc_clean($_POST['orderId']); $func = new Hesabix_Admin_Functions(); $result = $func->setOrder($orderId); if ($result) $func->setOrderPayment($orderId); echo json_encode($result); die(); } } public function adminClearPluginDataCallback() { HesabixLogService::writeLogStr('Clear Plugin Data'); if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { $hesabixApi = new Hesabix_Api(); $result = $hesabixApi->fixClearTags(); if (!$result->Success) { HesabixLogService::log(array("hesabix-Cannot clear tags. Error Message: " . (string) $result->ErrorMessage . ". Error Code: " . (string) $result->ErrorCode)); } global $wpdb; $options = $wpdb->get_results("SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE '%hesabix%'"); foreach ($options as $option) { delete_option($option->option_name); } $wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}hesabix"); die(); } } public function adminInstallPluginDataCallback() { HesabixLogService::writeLogStr('Install Plugin Data'); if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { require_once dirname(__FILE__) . '/includes/class-hesabix-activator.php'; Hesabix_Activator::activate(); die(); } } public function admin_product_add_column($columns) { $hesabixArray = array("hesabix_code" => "کد در حسابیکس"); $columns = $hesabixArray + $columns; return $columns; } public function admin_product_export_rows($rows, $products) { $rowsArray = explode("\n", $rows); $exportRows = []; $reflection = new ReflectionClass($products); $property = $reflection->getProperty('row_data'); $property->setAccessible(true); $productsArray = $property->getValue($products); $matchingArray = []; if (!empty($productsArray)) { foreach ($productsArray as $product) { if (is_array($product) && isset($product['id'])) { $wpFaService = new HesabixWpFaService(); if ($product["type"] == "variation") { if (array_key_exists('parent_id', $product)) { $parentId = $product['parent_id']; $productParentId = explode(':', $parentId)[1]; $wpFa = $wpFaService->getWpFaSearch($productParentId, $product['id'], '', "product"); } } elseif ($product["type"] == "simple" || $product["type"] == "variable") { $wpFa = $wpFaService->getWpFaSearch($product['id'], 0, '', "product"); } if (is_array($wpFa)) { foreach ($wpFa as $item) { if ($item->idWpAttribute != 0) { $matchingArray[$item->idWpAttribute] = $item->idHesabix; } else { $matchingArray[$item->idWp] = $item->idHesabix; } } } } } } foreach ($rowsArray as $row) { if (empty(trim($row))) { continue; } $columns = str_getcsv($row); $inserted = false; if (isset($columns[1])) { foreach ($matchingArray as $wpId => $hesabixId) { if ($columns[1] == $wpId && !$inserted) { $columns[0] = $hesabixId; $inserted = true; break; } } } if (!$inserted) { $columns[0] = "کد ندارد"; } $exportRows[] = implode(",", $columns); } return implode("\n", $exportRows); } public function hesabix_init_internal() { add_rewrite_rule('hesabix-webhook.php$', 'index.php?hesabix_webhook=1', 'top'); } private function checkForSyncChanges() { $syncChangesLastDate = get_option('hesabix_sync_changes_last_date'); if (!isset($syncChangesLastDate) || $syncChangesLastDate == false) { add_option('hesabix_sync_changes_last_date', new DateTime()); $syncChangesLastDate = new DateTime(); } $nowDateTime = new DateTime(); $diff = $nowDateTime->diff($syncChangesLastDate); if ($diff->i >= 4) { HesabixLogService::writeLogStr('Sync Changes Automatically'); update_option('hesabix_sync_changes_last_date', new DateTime()); require_once plugin_dir_path(dirname(__FILE__)) . '/includes/class-hesabix-webhook.php'; new Hesabix_Webhook(); } } public function hesabix_query_vars($query_vars) { $query_vars[] = 'hesabix_webhook'; return $query_vars; } public function custom_hesabix_column_order_list($columns) { $reordered_columns = array(); foreach ($columns as $key => $column) { $reordered_columns[$key] = $column; if ($key == 'order_status') { $reordered_columns['hesabix-column-invoice-number'] = __('Invoice in Hesabix', 'hesabix'); $reordered_columns['hesabix-column-submit-invoice'] = __('Submit Invoice', 'hesabix'); } } return $reordered_columns; } public function custom_orders_list_column_content($column, $post_id) { global $wpdb; if (get_option('woocommerce_custom_orders_table_enabled') == 'yes') { switch ($column) { case 'hesabix-column-invoice-number': $product_id = $post_id->ID; $table_name = $wpdb->prefix . 'hesabix'; $row = $wpdb->get_row( $wpdb->prepare( "SELECT id_hesabix FROM $table_name WHERE id_ps = %d AND obj_type = 'order'", $product_id ) ); if (!empty($row)) { echo '' . $row->id_hesabix . ''; } else { echo ''; } break; case 'hesabix-column-submit-invoice': $product_id = $post_id->ID; echo ''; echo __('Submit Invoice', 'hesabix'); echo ''; break; } } else { switch ($column) { case 'hesabix-column-invoice-number': $row = $wpdb->get_row("SELECT `id_hesabix` FROM `" . $wpdb->prefix . "hesabix` WHERE `id_ps` = $post_id AND `obj_type` = 'order'"); if (!empty($row)) echo '' . $row->id_hesabix . ''; else echo ''; break; case 'hesabix-column-submit-invoice': echo '"; echo __('Submit Invoice', 'hesabix'); echo ''; break; } } } public function hesabix_parse_request(&$wp) { if (array_key_exists('hesabix_webhook', $wp->query_vars)) { include(dirname(__FILE__) . '/includes/hesabix-webhook.php'); exit(); } } public function custom_orders_list_bulk_action($actions) { $actions['submit_invoice_in_hesabix'] = __('Submit Invoice in Hesabix', 'hesabix'); return $actions; } public function custom_orders_list_bulk_action_run($redirect_to, $action, $post_ids) { if ($action !== 'submit_invoice_in_hesabix') return $redirect_to; HesabixLogService::writeLogStr("Submit selected orders invoice"); if (count($post_ids) > 10) return $redirect_to = add_query_arg(array( 'submit_selected_orders_invoice_in_hesabix' => '1', 'error_msg' => 'select_max_10_items' ), $redirect_to); $success_count = 0; $func = new Hesabix_Admin_Functions(); foreach ($post_ids as $orderId) { $result = $func->setOrder($orderId); if ($result) { $success_count++; $func->setOrderPayment($orderId); } } return $redirect_to = add_query_arg(array( 'submit_selected_orders_invoice_in_hesabix' => '1', 'success_count' => $success_count, 'error_msg' => '0' ), $redirect_to); } public function hesabix_hook_edit_user(WP_User $user) { $wpFaService = new HesabixWpFaService(); $code = isset($user) ? $wpFaService->getCustomerCodeByWpId($user->ID) : ''; ?>


getWpFaByHesabixId('customer', $user_hesabix_code); $wpFa = $wpFaService->getWpFa('customer', $id_customer); if (!$wpFaOld || !$wpFa || $wpFaOld->id !== $wpFa->id) { if ($wpFaOld) $wpFaService->delete($wpFaOld); if ($wpFa) { $wpFa->idHesabix = $user_hesabix_code; $wpFaService->update($wpFa); } else { $wpFa = new WpFa(); $wpFa->objType = 'customer'; $wpFa->idWp = $id_customer; $wpFa->idHesabix = intval($user_hesabix_code); $wpFaService->save($wpFa); } } } $function = new Hesabix_Admin_Functions(); if (get_option('hesabix_contact_automatically_save_in_hesabix') == 'yes') $function->setContact($id_customer); } public function hesabix_hook_delete_user($id_customer) { $wpFaService = new HesabixWpFaService(); $id_obj = $wpFaService->getWpFaId('customer', $id_customer); if ($id_obj != false) { global $wpdb; $row = $wpdb->get_row("SELECT `id_hesabix` FROM `" . $wpdb->prefix . "hesabix` WHERE `id` = $id_obj AND `obj_type` = 'customer'"); if (is_object($row)) { $hesabixApi = new Hesabix_Api(); $hesabixApi->contactDelete($row->id_hesabix); } global $wpdb; $wpdb->delete($wpdb->prefix . 'hesabix', array('id_ps' => $id_customer)); HesabixLogService::log(array("Customer deleted. Customer ID: $id_customer")); } } public function hesabix_hook_order_status_change($id_order, $from, $to) { HesabixLogService::writeLogStr("Order Status Hook"); $function = new Hesabix_Admin_Functions(); $invoice_statuses = get_option('hesabix_invoice_status', array()); if (!is_array($invoice_statuses)) { if (is_string($invoice_statuses) && !empty($invoice_statuses)) { $invoice_statuses = array_map('trim', explode(',', $invoice_statuses)); } else { $invoice_statuses = array(); } } foreach ($invoice_statuses as $status) { HesabixLogService::writeLogStr("status: $status"); if ($status == $to) { $orderResult = $function->setOrder($id_order); if ($orderResult) { $payment_statuses = get_option('hesabix_payment_status', array()); if (!is_array($payment_statuses)) { if (is_string($payment_statuses) && !empty($payment_statuses)) { $payment_statuses = array_map('trim', explode(',', $payment_statuses)); } else { $payment_statuses = array(); } } foreach ($payment_statuses as $statusPayment) { if ($statusPayment == $to) $function->setOrderPayment($id_order); } } } } $values = get_option('hesabix_invoice_return_status'); if (is_array($values) || is_object($values)) { foreach ($values as $status) { if ($status == $to) $function->setOrder($id_order, 2, $function->getInvoiceCodeByOrderId($id_order)); } } } public function hesabix_hook_new_order($id_order, $order) { HesabixLogService::writeLogStr("New Order Hook"); $function = new Hesabix_Admin_Functions(); $orderStatus = wc_get_order($id_order)->get_status(); $orderItems = $order->get_items(); $invoice_statuses = get_option('hesabix_invoice_status', array()); if (!is_array($invoice_statuses)) { if (is_string($invoice_statuses) && !empty($invoice_statuses)) { $invoice_statuses = array_map('trim', explode(',', $invoice_statuses)); } else { $invoice_statuses = array(); } } foreach ($invoice_statuses as $status) { HesabixLogService::writeLogStr("status: $status"); if ($status == $orderStatus) { $orderResult = $function->setOrder($id_order, 0, null, $orderItems); if ($orderResult) { $payment_statuses = get_option('hesabix_payment_status', array()); if (!is_array($payment_statuses)) { if (is_string($payment_statuses) && !empty($payment_statuses)) { $payment_statuses = array_map('trim', explode(',', $payment_statuses)); } else { $payment_statuses = array(); } } foreach ($payment_statuses as $statusPayment) { if ($statusPayment == $orderStatus) $function->setOrderPayment($id_order); } } } } HesabixLogService::log(array($orderStatus)); $values = get_option('hesabix_invoice_return_status'); if (is_array($values) || is_object($values)) { foreach ($values as $status) { if ($status == $orderStatus) $function->setOrder($id_order, 2, $function->getInvoiceCodeByOrderId($id_order), $orderItems); } } } public function hesabix_hook_payment_confirmation($id_order, $from, $to) { $payment_statuses = get_option('hesabix_payment_status', array()); if (!is_array($payment_statuses)) { if (is_string($payment_statuses) && !empty($payment_statuses)) { $payment_statuses = array_map('trim', explode(',', $payment_statuses)); } else { $payment_statuses = array(); } } foreach ($payment_statuses as $status) { if ($status == $to) { $function = new Hesabix_Admin_Functions(); $function->setOrderPayment($id_order); } } } private $call_time = 1; public function hesabix_hook_new_product($id_product) { if ($this->call_time === 1) { $this->call_time++; return; } else { $this->call_time = 1; } if (get_option("hesabix_do_not_submit_product_automatically", "no") === "yes") return; $function = new Hesabix_Admin_Functions(); $function->setItems(array($id_product)); } public function hesabix_hook_save_product_variation($id_attribute) { HesabixLogService::writeLogStr("hesabix_hook_save_product_variation"); if (get_option("hesabix_do_not_submit_product_automatically", "no") === "yes" || get_option("hesabix_do_not_submit_product_automatically", "no") == "1") { $variable_field_id = "hesabix_hesabix_item_code_" . $id_attribute; $code = $_POST[$variable_field_id]; $id_product = $_POST['product_id']; if ($code === "") return; if (isset($code)) { global $wpdb; $row = $wpdb->get_row("SELECT * FROM `" . $wpdb->prefix . "hesabix` WHERE `id_hesabix` = " . $code . " AND `obj_type` = 'product'"); if (is_object($row)) { if ($row->id_ps == $id_product && $row->id_ps_attribute == $id_attribute) { return false; } echo '

' . __('The new Item code already used for another Item', 'hesabix') . '

'; HesabixLogService::log(array("The new Item code already used for another Item. Product ID: $id_product")); } else { $row2 = $wpdb->get_row("SELECT * FROM `" . $wpdb->prefix . "hesabix` WHERE `id_ps` = $id_product AND `obj_type` = 'product' AND `id_ps_attribute` = $id_attribute"); if (is_object($row2)) { $wpdb->update($wpdb->prefix . 'hesabix', array( 'id_hesabix' => (int) $code, ), array( 'id_ps' => $id_product, 'id_ps_attribute' => $id_attribute, 'obj_type' => 'product', )); } else if ((int) $code !== 0) { $wpdb->insert($wpdb->prefix . 'hesabix', array( 'id_hesabix' => (int) $code, 'id_ps' => (int) $id_product, 'id_ps_attribute' => $id_attribute, 'obj_type' => 'product', )); } } } $func = new Hesabix_Admin_Functions(); $wpFaService = new HesabixWpFaService(); $code = $wpFaService->getProductCodeByWpId($id_product, $id_attribute); if ($code == null) { $func->setItems(array($id_product)); } } } public function hesabix_hook_delete_product($id_product) { HesabixLogService::writeLogStr("Product Delete Hook"); $func = new Hesabix_Admin_Functions(); $wpFaService = new HesabixWpFaService(); $hesabixApi = new Hesabix_Api(); global $wpdb; $variations = $func->getProductVariations($id_product); if ($variations != false) { foreach ($variations as $variation) { $id_attribute = $variation->get_id(); $code = $wpFaService->getProductCodeByWpId($id_product, $id_attribute); if ($code != false) { $hesabixApi->itemDelete($code); $wpdb->delete($wpdb->prefix . 'hesabix', array('id_hesabix' => $code, 'obj_type' => 'product')); HesabixLogService::log(array("Product variation deleted. Product ID: $id_product-$id_attribute")); } } } $code = $wpFaService->getProductCodeByWpId($id_product); if ($code != false) { $hesabixApi->itemDelete($code); $wpdb->delete($wpdb->prefix . 'hesabix', array('id_hesabix' => $code, 'obj_type' => 'product')); HesabixLogService::log(array("Product deleted. Product ID: $id_product")); } } public function hesabix_hook_delete_product_variation($id_attribute) { $hesabixApi = new Hesabix_Api(); global $wpdb; $row = $wpdb->get_row("SELECT * FROM `" . $wpdb->prefix . "hesabix` WHERE `id_ps_attribute` = $id_attribute AND `obj_type` = 'product'"); if (is_object($row)) { $hesabixApi->itemDelete($row->id_hesabix); $wpdb->delete($wpdb->prefix . 'hesabix', array('id' => $row->id)); HesabixLogService::log(array("Product variation deleted. Product ID: $row->id_ps-$id_attribute")); } } public function hesabix_hook_product_options_general_product_data() { $wpFaService = new HesabixWpFaService(); $value = isset($_GET['post']) ? $wpFaService->getProductCodeByWpId($_GET['post']) : ''; $args = array( 'id' => 'hesabix_hesabix_item_code_0', 'label' => __('Hesabix base item code', 'hesabix'), 'desc_tip' => true, 'description' => __('The base Item code of this product in Hesabix, if you want to map this product to another item in Hesabix, enter the new Item code.', 'hesabix'), 'value' => $value, 'type' => 'number', ); woocommerce_wp_text_input($args); } public function hesabix_hook_process_product_meta($post_id) { $itemCode = isset($_POST['hesabix_hesabix_item_code_0']) ? $_POST['hesabix_hesabix_item_code_0'] : ''; if ($itemCode === "") return; if (isset($itemCode)) { global $wpdb; $row = $wpdb->get_row("SELECT * FROM `" . $wpdb->prefix . "hesabix` WHERE `id_hesabix` = " . $itemCode . " AND `obj_type` = 'product'"); if (is_object($row)) { echo '

' . __('The new Item code already used for another Item', 'hesabix') . '

'; HesabixLogService::log(array("The new Item code already used for another Item. Product ID: $post_id")); } else { $row2 = $wpdb->get_row("SELECT * FROM `" . $wpdb->prefix . "hesabix` WHERE `id_ps` = $post_id AND `obj_type` = 'product' AND `id_ps_attribute` = 0"); if (is_object($row2)) { $wpdb->update($wpdb->prefix . 'hesabix', array( 'id_hesabix' => (int) $itemCode, ), array( 'id_ps' => $post_id, 'id_ps_attribute' => 0, 'obj_type' => 'product', )); } else if ((int) $itemCode !== 0) { $wpdb->insert($wpdb->prefix . 'hesabix', array( 'id_hesabix' => (int) $itemCode, 'id_ps' => (int) $post_id, 'id_ps_attribute' => 0, 'obj_type' => 'product', )); } } } } public function hesabix_hook_product_after_variable_attributes($loop, $variation_data, $variation) { $wpFaService = new HesabixWpFaService(); $value = isset($_POST['product_id']) ? $wpFaService->getProductCodeByWpId($_POST['product_id'], $variation->ID) : ''; $args = array( 'id' => 'hesabix_hesabix_item_code_' . $variation->ID, 'label' => __('Hesabix variable item code', 'hesabix'), 'desc_tip' => true, 'description' => __('The variable Item code of this product variable in Hesabix, if you want to map this product to another item in Hesabix, enter the new Item code.', 'hesabix'), 'value' => $value, ); woocommerce_wp_text_input($args); } public function adminCleanLogFileCallback() { if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { $func = new Hesabix_Admin_Functions(); $result = $func->cleanLogFile(); if ($result) { $redirect_url = admin_url('admin.php?page=hesabix-option&tab=log&cleanLogResult=true'); } else { $redirect_url = admin_url('admin.php?page=hesabix-option&tab=log&cleanLogResult=false'); } echo $redirect_url; die(); } } function add_hesabix_product_data_tab($product_data_tabs) { $product_data_tabs['hesabix'] = array( 'label' => __('Hesabix', 'hesabix'), 'target' => 'panel_product_data_hesabix', ); return $product_data_tabs; } function add_hesabix_product_data_fields() { global $woocommerce, $post, $product; $funcs = new Hesabix_Admin_Functions(); $items = array(); $id_product = $post->ID; $product = wc_get_product($id_product); if ($product->get_status() === "auto-draft") { ?>
هنوز محصول ذخیره نشده است.
پس از ذخیره محصول، در این قسمت می توانید ارتباط محصول و متغیرهای آن با حسابیکس را مدیریت کنید.
get_stock_quantity(); $items[0]["Id"] = $id_product; $i = 1; $variations = $funcs->getProductVariations($id_product); if ($variations) { foreach ($variations as $variation) { $items[] = HesabixItemService::mapProductVariation($product, $variation, $id_product, false); $items[$i]["Quantity"] = $variation->get_stock_quantity(); $items[$i]["Id"] = $variation->get_id(); $i++; } } ?>
نام کالا کد در حسابیکس ذخیره کد حذف ارتباط بروزرسانی قیمت و موجودی قیمت موجودی
" id="hesabix-item-" style="width: 75px;" class="hesabix-item-code" data-id=""> " class="button hesabix-item-save"> " class="button hesabix-item-delete-link"> " class="button button-primary hesabix-item-update"> "> ">
#hesabixID { width: 5vw; color: #2271b1; } #hesabixID a { width: 100%; } '; return array_slice($columns, 0, 3, true) + array('hesabixID' => 'کد حسابیکس') + array_slice($columns, 3, count($columns) - 3, true); } function admin_products_hesabixId_column_content($column) { $funcs = new Hesabix_Admin_Functions(); $items = array(); $id_product = get_the_ID(); $product = wc_get_product($id_product); $items[] = HesabixItemService::mapProduct($product, $id_product, false); $i = 1; $variations = $funcs->getProductVariations($id_product); if ($variations) { foreach ($variations as $variation) { $items[] = HesabixItemService::mapProductVariation($product, $variation, $id_product, false); $i++; } } echo '
'; foreach ($items as $item) { if ($column == 'hesabixID') { $hesabixId = $item["code"]; echo "" . $hesabixId . " " . ""; } } echo '
'; } function adminChangeProductCodeCallback() { if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { $productId = (int) wc_clean($_POST['productId']); $attributeId = (int) wc_clean($_POST['attributeId']); if ($productId == $attributeId) $attributeId = 0; $code = (int) wc_clean($_POST['code']); $result = array(); if (!$code) { $result["error"] = true; $result["message"] = "کد کالا وارد نشده است."; echo json_encode($result); die(); return; } $wpFaService = new HesabixWpFaService(); $wpFa = $wpFaService->getWpFaByHesabixId('product', $code); if ($wpFa) { $result["error"] = true; $result["message"] = "این کد به کالای دیگری متصل است. \n" . $wpFa->idWp . "-" . $wpFa->idWpAttribute; echo json_encode($result); die(); return; } $api = new Hesabix_Api(); $response = $api->itemGet($code); if (!$response->Success) { $result["error"] = true; $result["message"] = "کالایی با کد وارد شده در حسابیکس پیدا نشد."; echo json_encode($result); die(); return; } $wpFa = $wpFaService->getWpFa('product', $productId, $attributeId); if ($wpFa) { $wpFa->idHesabix = $code; $wpFaService->update($wpFa); } else { $wpFa = new WpFa(); $wpFa->idHesabix = $code; $wpFa->idWp = $productId; $wpFa->idWpAttribute = $attributeId; $wpFa->objType = 'product'; $wpFaService->save($wpFa); } $result["error"] = false; echo json_encode($result); die(); } } function adminDeleteProductLinkCallback() { if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { $productId = wc_clean($_POST['productId']); $attributeId = wc_clean($_POST['attributeId']); if ($productId == $attributeId) $attributeId = 0; $result = array(); $wpFaService = new HesabixWpFaService(); $wpFa = $wpFaService->getWpFa('product', $productId, $attributeId); if ($wpFa) { $wpFaService->delete($wpFa); HesabixLogService::writeLogStr("حذف ارتباط کالا. کد کالا: " . $productId . "-" . "کد متغیر:" . $attributeId); } $result["error"] = false; echo json_encode($result); die(); } } function adminUpdateProductCallback() { if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { if ( get_option('hesabix_item_update_price', 'no') == 'no' && get_option('hesabix_item_update_quantity', 'no') == 'no' ) { $result["error"] = true; $result["message"] = "خطا: در تنظیمات افزونه، گزینه های بروزرسانی قیمت و موجودی محصول بر اساس حسابیکس فعال نیستند."; echo json_encode($result); die(); } $productId = wc_clean($_POST['productId']); $attributeId = wc_clean($_POST['attributeId']); if (get_option('hesabix_item_update_quantity', 'no') == 'yes') update_post_meta($attributeId, '_manage_stock', 'yes'); if ($productId == $attributeId) $attributeId = 0; $result = array(); $wpFaService = new HesabixWpFaService(); $wpFa = $wpFaService->getWpFa('product', $productId, $attributeId); if ($wpFa) { $api = new Hesabix_Api(); // $warehouse = get_option('hesabix_item_update_quantity_based_on', "-1"); // if ($warehouse == "-1") // $response = $api->itemGet($wpFa->idHesabix); // else { // $response = $api->itemGetQuantity($warehouse, array($wpFa->idHesabix)); // } $response = $api->itemGet($wpFa->idHesabix); if ($response->Success) { // $item = $warehouse == "-1" ? $response->Result : $response->Result[0]; $item = $response->Result; $newProps = Hesabix_Admin_Functions::setItemChanges($item); $result["error"] = false; $result["newPrice"] = $newProps["newPrice"]; $result["newQuantity"] = $newProps["newQuantity"]; } else { $result["error"] = true; $result["message"] = "کالا در حسابیکس پیدا نشد."; } } echo json_encode($result); die(); } } function adminChangeProductsCodeCallback() { if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { $wpFaService = new HesabixWpFaService(); $productId = (int) wc_clean($_POST['productId']); $itemsData = wc_clean($_POST['itemsData'], true); $result = array(); $codes = []; foreach ($itemsData as $itemData) { $attributeId = (int) $itemData["attributeId"]; $code = (int) $itemData["code"]; if ($productId == $attributeId) $attributeId = 0; $codes[] = str_pad($code, 6, "0", STR_PAD_LEFT); if (!$code) { $result["error"] = true; $result["message"] = "کد کالا وارد نشده است."; echo json_encode($result); die(); return; } $wpFa = $wpFaService->getWpFaByHesabixId('product', $code); $wpFa2 = $wpFaService->getWpFa('product', $productId, $attributeId); if ($wpFa && $wpFa2 && $wpFa->id != $wpFa2->id) { $result["error"] = true; $result["message"] = "این کد ($code) به کالای دیگری متصل است. \n" . $wpFa->idWp . "-" . $wpFa->idWpAttribute; echo json_encode($result); die(); return; } } $api = new Hesabix_Api(); $response = $api->itemGetItemsByCodes(array('values' => $codes)); if ($response->Success) { $items = $response->result; foreach ($codes as $code) { $found = false; foreach ($items as $item) { if ($item->code == $code) $found = true; } if (!$found) { $result["error"] = true; $result["message"] = "کالایی با کد $code در حسابیکس پیدا نشد."; echo json_encode($result); die(); return; } } } else { $result["error"] = true; $result["message"] = "کالایی با کد وارد شده در حسابیکس پیدا نشد."; echo json_encode($result); die(); return; } foreach ($itemsData as $itemData) { $attributeId = (int) $itemData["attributeId"]; $code = (int) $itemData["code"]; if ($productId == $attributeId) $attributeId = 0; $wpFa = $wpFaService->getWpFa('product', $productId, $attributeId); if ($wpFa) { $wpFa->idHesabix = $code; $wpFaService->update($wpFa); } else { $wpFa = new WpFa(); $wpFa->idHesabix = $code; $wpFa->idWp = $productId; $wpFa->idWpAttribute = $attributeId; $wpFa->objType = 'product'; $wpFaService->save($wpFa); } } $result["error"] = false; echo json_encode($result); die(); } } function adminDeleteProductsLinkCallback() { if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { $productId = wc_clean($_POST['productId']); $result = array(); $wpFaService = new HesabixWpFaService(); $wpFaService->deleteAll($productId); HesabixLogService::writeLogStr("حذف ارتباط کالاها. کد کالا: " . $productId); $result["error"] = false; echo json_encode($result); die(); } } function adminUpdateProductAndVariationsCallback() { if (is_admin() && (defined('DOING_AJAX') || DOING_AJAX)) { if ( get_option('hesabix_item_update_price', 'no') == 'no' && get_option('hesabix_item_update_quantity', 'no') == 'no' ) { $result["error"] = true; $result["message"] = "خطا: در تنظیمات افزونه، گزینه های بروزرسانی قیمت و موجودی محصول بر اساس حسابیکس فعال نیستند."; echo json_encode($result); die(); } $api = new Hesabix_Api(); $wpFaService = new HesabixWpFaService(); $productId = wc_clean($_POST['productId']); $productAndCombinations = $wpFaService->getProductAndCombinations($productId); $result = array(); if (count($productAndCombinations) == 0) { $result["error"] = true; $result["message"] = "هیچ ارتباطی پیدا نشد."; echo json_encode($result); die(); } $codes = []; $hesabix_item_update_quantity = get_option('hesabix_item_update_quantity', 'no'); foreach ($productAndCombinations as $p) { $codes[] = str_pad($p->idHesabix, 6, "0", STR_PAD_LEFT); if ($hesabix_item_update_quantity == 'yes') update_post_meta($p->idWpAttribute == 0 ? $p->idWp : $p->idWpAttribute, '_manage_stock', 'yes'); } // $warehouse = get_option('hesabix_item_update_quantity_based_on', "-1"); // if ($warehouse == "-1") // $response = $api->itemGetItemsByCodes($codes); // else { // $response = $api->itemGetQuantity($warehouse, $codes); // } $response = $api->itemGetItemsByCodes($codes); if ($response->Success) { // $items = $warehouse == "-1" ? $response->Result->List : $response->Result; $items = $response->Result->List; $newData = []; $result["error"] = false; foreach ($items as $item) { $newProps = Hesabix_Admin_Functions::setItemChanges($item); $wpFa = $wpFaService->getWpFaByHesabixId('product', $item->Code); $newData[] = array( "newPrice" => $newProps["newPrice"], "newQuantity" => $newProps["newQuantity"], "attributeId" => $wpFa->idWpAttribute > 0 ? $wpFa->idWpAttribute : $wpFa->idWp ); } $result["newData"] = $newData; } else { $result["error"] = true; $result["message"] = "کالایی با کد وارد شده در حسابیکس پیدا نشد."; echo json_encode($result); die(); return; } echo json_encode($result); die(); } } function add_additional_fields_to_checkout($fields) { $add_fields_option = get_option('hesabix_contact_add_fields'); if ($add_fields_option !== '1') { return $fields; } $NationalCode_isActive = get_option('hesabix_contact_NationalCode_checkbox_hesabix'); $EconomicCode_isActive = get_option('hesabix_contact_EconomicCode_checkbox_hesabix'); $RegistrationNumber_isActive = get_option('hesabix_contact_RegistrationNumber_checkbox_hesabix'); $Website_isActive = get_option('hesabix_contact_Website_checkbox_hesabix'); $NationalCode_isRequired = get_option('hesabix_contact_NationalCode_isRequired_hesabix') === 'yes'; $EconomicCode_isRequired = get_option('hesabix_contact_EconomicCode_isRequired_hesabix') === 'yes'; $RegistrationNumber_isRequired = get_option('hesabix_contact_RegistrationNumber_isRequired_hesabix') === 'yes'; $Website_isRequired = get_option('hesabix_contact_Website_isRequired_hesabix') === 'yes'; if ($NationalCode_isActive == 'yes') { $fields['billing']['billing_hesabix_nationalcode'] = array( 'label' => __('National code', 'hesabix'), 'placeholder' => __('please enter your National code', 'hesabix'), 'priority' => 30, 'required' => $NationalCode_isRequired, 'clear' => true, 'maxlength' => 10, ); } if ($EconomicCode_isActive == 'yes') { $fields['billing']['billing_hesabix_economiccode'] = array( 'label' => __('Economic code', 'hesabix'), 'placeholder' => __('please enter your Economic code', 'hesabix'), 'priority' => 31, 'required' => $EconomicCode_isRequired, 'clear' => true ); } if ($RegistrationNumber_isActive == 'yes') { $fields['billing']['billing_hesabix_registerationnumber'] = array( 'label' => __('Registration number', 'hesabix'), 'placeholder' => __('please enter your Registration number', 'hesabix'), 'priority' => 32, 'required' => $RegistrationNumber_isRequired, 'clear' => true ); } if ($Website_isActive == 'yes') { $fields['billing']['billing_hesabix_website'] = array( 'type' => 'url', 'label' => __('Website', 'hesabix'), 'placeholder' => __('please enter your Website address', 'hesabix'), 'priority' => 33, 'required' => $Website_isRequired, 'clear' => true, ); } return $fields; } function show_additional_fields_in_order_detail($order) { $add_fields_option = get_option('hesabix_contact_add_fields'); if ($add_fields_option !== '1') { return; } $orderId = $order->get_id(); $NationalCode = '_billing_hesabix_nationalcode'; $EconomicCode = '_billing_hesabix_economiccode'; $RegistrationNumber = '_billing_hesabix_registerationnumber'; $Website = '_billing_hesabix_website'; $NationalCode_isActive = get_option('hesabix_contact_NationalCode_checkbox_hesabix'); $EconomicCode_isActive = get_option('hesabix_contact_EconomicCode_checkbox_hesabix'); $RegistrationNumber_isActive = get_option('hesabix_contact_RegistrationNumber_checkbox_hesabix'); $Website_isActive = get_option('hesabix_contact_Website_checkbox_hesabix'); if ($NationalCode_isActive == 'yes') { echo '

' . __('National code', 'hesabix') . ': ' . '
' . '' . get_post_meta($orderId, $NationalCode, true) . '

'; } if ($EconomicCode_isActive == 'yes') echo '

' . __('Economic code', 'hesabix') . ': ' . '
' . '' . get_post_meta($orderId, $EconomicCode, true) . '

'; if ($RegistrationNumber_isActive == 'yes') echo '

' . __('Registration number', 'hesabix') . ': ' . '
' . '' . get_post_meta($orderId, $RegistrationNumber, true) . '

'; if ($Website_isActive == 'yes') echo '

' . __('Website', 'hesabix') . ': ' . '
' . '' . get_post_meta($orderId, $Website, true) . '

'; } public function admin_get_home_stats_callback() { if (!wp_verify_nonce($_POST['nonce'], 'hesabix_ajax_nonce')) { wp_die('Security check failed'); } $products_data = Hesabix_Setting::getProductsCount(); $products_count = $products_data['storeProductsCount']; $customers_count = Hesabix_Setting::getCustomersCount(); $orders_count = Hesabix_Setting::getOrdersCount(); $is_connected = $this->quickCheckHesabixConnection(); $response = array( 'success' => true, 'data' => array( 'products_count' => number_format($products_count), 'customers_count' => number_format($customers_count), 'orders_count' => number_format($orders_count), 'is_connected' => $is_connected, 'connected_text' => $is_connected ? __('Connected', 'hesabix') : __('Not Connected', 'hesabix'), 'products_label' => __('Total Products', 'hesabix'), 'customers_label' => __('Total Customers', 'hesabix'), 'orders_label' => __('Total Orders', 'hesabix'), 'status_label' => __('Hesabix Status', 'hesabix') ) ); wp_send_json($response); } private function checkHesabixConnection() { $api_key = get_option('hesabix_account_api'); if (!$api_key) { return false; } try { $hesabix_api = new Hesabix_Api(); $currency_response = $hesabix_api->settingGetCurrency(); if ($currency_response && is_object($currency_response) && $currency_response->Success) { return true; } return false; } catch (Exception $e) { return false; } } private function silentCheckHesabixConnection() { $api_key = get_option('hesabix_account_api'); if (!$api_key) { return false; } try { $hesabix_api = new Hesabix_Api(); $currency_response = $hesabix_api->settingGetCurrency(); if ($currency_response && is_object($currency_response) && $currency_response->Success) { return true; } return false; } catch (Exception $e) { return false; } } private function quickCheckHesabixConnection() { $api_key = get_option('hesabix_account_api'); $live_mode = get_option('hesabix_live_mode', 0); return !empty($api_key) && $live_mode == 1; } public function admin_check_for_updates_callback() { if (!wp_verify_nonce($_POST['nonce'], 'hesabix_ajax_nonce')) { wp_die('Security check failed'); } try { $plugin_stats = wp_remote_get('https://app.hesabix.ir/api/wordpress/plugin/stats'); error_log(print_r($plugin_stats['body'], true)); if ($plugin_stats && !is_wp_error($plugin_stats) && $plugin_stats['response']['code'] == 200) { $json_data = json_decode($plugin_stats['body']); if ($json_data && isset($json_data->Success) && $json_data->Success) { $current_version = HESABIX_VERSION; $latest_version = $json_data->Result->version; $is_update_available = version_compare($current_version, $latest_version, '<'); $response = array( 'success' => true, 'data' => array( 'current_version' => $current_version, 'latest_version' => $latest_version, 'is_update_available' => $is_update_available, 'download_url' => $json_data->Result->download_url, 'changelog' => $json_data->Result->changelog, 'statistics' => $json_data->Result->statistics ) ); } else { $response = array( 'success' => false, 'message' => __('Failed to check for updates', 'hesabix') ); } } else { $response = array( 'success' => false, 'message' => __('Failed to check for updates', 'hesabix') ); } } catch (Exception $e) { $response = array( 'success' => false, 'message' => __('Error checking for updates', 'hesabix') ); } wp_send_json($response); } public function admin_save_api_settingsCallback() { check_ajax_referer('hesabix_ajax_nonce', 'nonce'); try { $api_key = sanitize_text_field($_POST['api_key'] ?? ''); $api_address = sanitize_text_field($_POST['api_address'] ?? '0'); $custom_api_address = sanitize_text_field($_POST['custom_api_address'] ?? ''); update_option('hesabix_account_api', $api_key); update_option('hesabix_api_address', $api_address); update_option('hesabix_custom_api_address', $custom_api_address); Hesabix_Setting::hesabix_set_webhook(false); wp_send_json_success(array( 'message' => __('Settings saved successfully', 'hesabix') )); } catch (Exception $e) { wp_send_json_error(array('message' => $e->getMessage())); } } public function admin_test_api_connectionCallback() { check_ajax_referer('hesabix_ajax_nonce', 'nonce'); try { $api_key = get_option('hesabix_account_api'); $api_address = get_option('hesabix_api_address', 0); if (!$api_key) { wp_send_json_error(array('message' => __('API key not configured', 'hesabix'))); return; } $is_connected = $this->quickCheckHesabixConnection(); if (!$is_connected) { wp_send_json_error(array('message' => __('Connection failed. Please check your API key and try again.', 'hesabix'))); return; } wp_send_json_success(array( 'message' => __('Connection successful!', 'hesabix'), 'business_info' => $data['data'] ?? array() )); } catch (Exception $e) { wp_send_json_error(array('message' => $e->getMessage())); } } public function hesabix_get_sync_stats_ajax() { if (!wp_verify_nonce($_POST['nonce'], 'hesabix_ajax_nonce')) { wp_send_json_error('Security check failed'); } $storeProductsCount = Hesabix_Setting::getProductCountsInStore(); $hesabixProductsCount = Hesabix_Setting::getProductCountsInHesabix(); $linkedProductsCount = Hesabix_Setting::getLinkedProductsCount(); wp_send_json_success(array( 'storeProductsCount' => $storeProductsCount, 'hesabixProductsCount' => $hesabixProductsCount, 'linkedProductsCount' => $linkedProductsCount )); } public function hesabix_load_tab_content() { if (!wp_verify_nonce($_POST['nonce'], 'hesabix_ajax_nonce')) { wp_send_json_error('Security check failed'); } $tab = sanitize_text_field($_POST['tab']); $api_required_tabs = array('catalog', 'customers', 'invoice', 'sync'); if (in_array($tab, $api_required_tabs)) { $is_connected = $this->quickCheckHesabixConnection(); if (!$is_connected) { $error_message = '

' . __('API Connection Required', 'hesabix') . '

' . __('This tab requires a valid API connection. Please configure your API settings from the API tab first.', 'hesabix') . '

'; wp_send_json_success(array('content' => $error_message)); return; } } ob_start(); switch ($tab) { case 'home': do_action('hesabix_home_setting_save_field'); do_action('hesabix_home_setting'); break; case 'extra': do_action('hesabix_extra_setting'); break; case 'catalog': do_action('hesabix_catalog_setting'); break; case 'customers': do_action('hesabix_customers_setting'); break; case 'invoice': do_action('hesabix_invoice_setting'); break; case 'api': do_action('hesabix_api_setting_save_field'); do_action('hesabix_api_setting'); break; case 'export': do_action('hesabix_export_setting'); break; case 'sync': do_action('hesabix_sync_setting'); break; case 'log': do_action('hesabix_log_setting'); break; default: wp_send_json_error('Invalid tab'); break; } $content = ob_get_clean(); wp_send_json_success(array('content' => $content)); } public function hesabix_update_customers_ajax() { if (!wp_verify_nonce($_POST['nonce'], 'hesabix_ajax_nonce')) { wp_send_json_error('Security check failed'); } try { $function = new Hesabix_Admin_Functions(); $customers = get_users(array( 'role' => 'customer', 'number' => -1, 'fields' => 'ID' )); $success_count = 0; $error_count = 0; $errors = array(); foreach ($customers as $customer_id) { $result = $function->setContact($customer_id); if ($result) { $success_count++; } else { $error_count++; $errors[] = "Customer ID: $customer_id"; } } $response = array( 'success' => true, 'message' => __('Customers successfully updated in Hesabix.', 'hesabix'), 'data' => array( 'success_count' => $success_count, 'error_count' => $error_count, 'errors' => $errors ) ); wp_send_json_success($response); } catch (Exception $e) { wp_send_json_error(array( 'message' => __('Error updating customers: ', 'hesabix') . $e->getMessage() )); } } public function hesabix_save_customers_fields_ajax() { if (!wp_verify_nonce($_POST['nonce'], 'hesabix_ajax_nonce')) { wp_send_json_error('Security check failed'); } try { $fields_to_save = array( 'hesabix_contact_add_fields' => isset($_POST['addFieldsRadio']) ? sanitize_text_field($_POST['addFieldsRadio']) : '', 'hesabix_contact_NationalCode_checkbox_hesabix' => isset($_POST['nationalCodeCheck']) ? sanitize_text_field($_POST['nationalCodeCheck']) : 'no', 'hesabix_contact_NationalCode_isRequired_hesabix' => isset($_POST['nationalCodeRequired']) ? sanitize_text_field($_POST['nationalCodeRequired']) : 'no', 'hesabix_contact_NationalCode_text_hesabix' => isset($_POST['nationalCode']) ? sanitize_text_field($_POST['nationalCode']) : '', 'hesabix_contact_EconomicCode_checkbox_hesabix' => isset($_POST['economicCodeCheck']) ? sanitize_text_field($_POST['economicCodeCheck']) : 'no', 'hesabix_contact_EconomicCode_isRequired_hesabix' => isset($_POST['economicCodeRequired']) ? sanitize_text_field($_POST['economicCodeRequired']) : 'no', 'hesabix_contact_EconomicCode_text_hesabix' => isset($_POST['economicCode']) ? sanitize_text_field($_POST['economicCode']) : '', 'hesabix_contact_RegistrationNumber_checkbox_hesabix' => isset($_POST['registrationNumberCheck']) ? sanitize_text_field($_POST['registrationNumberCheck']) : 'no', 'hesabix_contact_RegistrationNumber_isRequired_hesabix' => isset($_POST['registrationNumberRequired']) ? sanitize_text_field($_POST['registrationNumberRequired']) : 'no', 'hesabix_contact_RegistrationNumber_text_hesabix' => isset($_POST['registrationNumber']) ? sanitize_text_field($_POST['registrationNumber']) : '', 'hesabix_contact_Website_checkbox_hesabix' => isset($_POST['websiteCheck']) ? sanitize_text_field($_POST['websiteCheck']) : 'no', 'hesabix_contact_Website_isRequired_hesabix' => isset($_POST['websiteRequired']) ? sanitize_text_field($_POST['websiteRequired']) : 'no', 'hesabix_contact_Website_text_hesabix' => isset($_POST['website']) ? sanitize_text_field($_POST['website']) : '', ); $success = true; foreach ($fields_to_save as $option_name => $option_value) { $result = update_option($option_name, $option_value); if (!$result && get_option($option_name) !== $option_value) { $success = false; } } if ($success) { wp_send_json_success(array('message' => __('Customer fields saved successfully', 'hesabix'))); } else { wp_send_json_error(array('message' => __('Failed to save customer fields', 'hesabix'))); } } catch (Exception $e) { wp_send_json_error(array( 'message' => __('Error saving customer fields: ', 'hesabix') . $e->getMessage() )); } } }