hesabixWCPlugin/admin/class-hesabix-admin.php

2078 lines
85 KiB
PHP
Raw Normal View History

2025-08-05 17:24:52 +03:30
<?php
/**
* Hesabix Admin Class
*
* @package Hesabix
* @author Mohammad Rezai
* @author URI https://pirouz.xyz
* @since 1.0.0
*/
include_once(dirname(__FILE__) . '/services/hesabixLogService.php');
include_once(dirname(__FILE__) . '/services/hesabixWpFaService.php');
class Hesabix_Admin
{
private $plugin_name;
private $version;
public function __construct($plugin_name, $version)
{
$this->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")) {
?>
<style>
@font-face {
font-family: 'IRANYekanWeb';
src: url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>eot/IRANYekanWebThin.eot');
src: url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>eot/IRANYekanWebThin.eot?#iefix') format('embedded-opentype'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>woff2/IRANYekanWebThin.woff2') format('woff2'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>woff/IRANYekanWebThin.woff') format('woff'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>ttf/IRANYekanWebThin.ttf') format('truetype'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>svg/iranyekanwebthin.svg#IRANYekanWebThin') format('svg');
font-weight: 100;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: 'IRANYekanWeb';
src: url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>eot/IRANYekanWebLight.eot');
src: url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>eot/IRANYekanWebLight.eot?#iefix') format('embedded-opentype'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>woff2/IRANYekanWebLight.woff2') format('woff2'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>woff/IRANYekanWebLight.woff') format('woff'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>ttf/IRANYekanWebLight.ttf') format('truetype'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>svg/iranyekanweblight.svg#IRANYekanWebLight') format('svg');
font-weight: 300;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: 'IRANYekanWeb';
src: url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>eot/IRANYekanWebRegular.eot');
src: url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>eot/IRANYekanWebRegular.eot?#iefix') format('embedded-opentype'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>woff2/IRANYekanWebRegular.woff2') format('woff2'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>woff/IRANYekanWebRegular.woff') format('woff'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>ttf/IRANYekanWebRegular.ttf') format('truetype'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>svg/IRANYekanWebRegular.svg#IRANYekanWebRegular') format('svg');
font-weight: 400;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: 'IRANYekanWeb';
src: url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>eot/IRANYekanWebMedium.eot');
src: url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>eot/IRANYekanWebMedium.eot?#iefix') format('embedded-opentype'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>woff2/IRANYekanWebMedium.woff2') format('woff2'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>woff/IRANYekanWebMedium.woff') format('woff'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>ttf/IRANYekanWebMedium.ttf') format('truetype'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>svg/iranyekanwebmedium.svg#IRANYekanWebMedium') format('svg');
font-weight: 500;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: 'IRANYekanWeb';
src: url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>eot/IRANYekanWebBold.eot');
src: url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>eot/IRANYekanWebBold.eot?#iefix') format('embedded-opentype'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>woff2/IRANYekanWebBold.woff2') format('woff2'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>woff/IRANYekanWebBold.woff') format('woff'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>ttf/IRANYekanWebBold.ttf') format('truetype'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>svg/iranyekanwebbold.svg#IRANYekanWebBold') format('svg');
font-weight: 700;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: 'IRANYekanWeb';
src: url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>eot/IRANYekanWebExtraBold.eot');
src: url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>eot/IRANYekanWebExtraBold.eot?#iefix') format('embedded-opentype'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>woff2/IRANYekanWebExtraBold.woff2') format('woff2'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>woff/IRANYekanWebExtraBold.woff') format('woff'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>ttf/IRANYekanWebExtraBold.ttf') format('truetype'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>svg/iranyekanwebextrabold.svg#IRANYekanWebExtraBold') format('svg');
font-weight: 800;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: 'IRANYekanWeb';
src: url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>eot/IRANYekanWebBlack.eot');
src: url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>eot/IRANYekanWebBlack.eot?#iefix') format('embedded-opentype'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>woff2/IRANYekanWebBlack.woff2') format('woff2'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>woff/IRANYekanWebBlack.woff') format('woff'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>ttf/IRANYekanWebBlack.ttf') format('truetype'),
url('<?php echo plugin_dir_url(__FILE__) . '../assets/fonts/' ?>svg/iranyekanwebblack.svg#IRANYekanWebBlack') format('svg');
font-weight: 900;
font-style: normal;
font-display: swap;
}
</style>
<?php
wp_enqueue_style($this->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 '<div class="error"><p>' . sprintf(__('Hesabix Plugin requires the %s to work!', 'hesabix'), '<a href="https://wordpress.org/plugins/woocommerce/" target="_blank">' . __('WooCommerce', 'hesabix') . '</a>') . '</p></div>';
}
public function hesabix_live_mode_notice()
{
echo '<div class="error"><p>' . __('Hesabix Plugin need to connect to Hesabix Accounting, Please check the API credential!', 'hesabix') . '</p></div>';
}
public function hesabix_business_expired_notice()
{
echo '<div class="error"><p>' . __('Cannot connect to Hesabix. Business expired.', 'hesabix') . '</p></div>';
}
public function hesabix_currency_notice()
{
echo '<div class="error"><p>' . __('Hesabix Plugin cannot works! because WooCommerce currency in not match with Hesabix.', 'hesabix') . '</p></div>';
}
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(
'<div class="notice notice-error is-dismissible"><p>%s</p></div>',
__('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('<div class="notice notice-success is-dismissible"><p>%s %d</p></div>', __('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 '<mark class="order-status"><span>' . $row->id_hesabix . '</span></mark>';
} else {
echo '<small></small>';
}
break;
case 'hesabix-column-submit-invoice':
$product_id = $post_id->ID;
echo '<a role="button" class="button btn-submit-invoice" ';
echo 'data-order-id="' . $product_id . '">';
echo __('Submit Invoice', 'hesabix');
echo '</a>';
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 '<mark class="order-status"><span>' . $row->id_hesabix . '</span></mark>';
else
echo '<small></small>';
break;
case 'hesabix-column-submit-invoice':
echo '<a role="button" class="button btn-submit-invoice" ';
echo "data-order-id='$post_id'>";
echo __('Submit Invoice', 'hesabix');
echo '</a>';
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) : '';
?>
<hr>
<table class="form-table">
<tr>
<th><label for="user_hesabix_code"
class="text-info"><?php echo __('Contact Code in Hesabix', 'hesabix'); ?></label>
</th>
<td>
<input type="text" value="<?php echo $code; ?>" name="user_hesabix_code" id="user_hesabix_code"
class="regular-text"><br />
<div class="description mt-2">
<?php echo __("The contact code of this user in Hesabix, if you want to map this user "
. "to a contact in Hesabix, enter the Contact code.", 'hesabix'); ?>
</div>
</td>
</tr>
</table>
<hr>
<?php
}
public function hesabix_hook_user_register($id_customer)
{
$user_hesabix_code = $_REQUEST['user_hesabix_code'];
if (isset($user_hesabix_code) && $user_hesabix_code !== "") {
$wpFaService = new HesabixWpFaService();
$wpFaOld = $wpFaService->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 '<div class="error"><p>' . __('The new Item code already used for another Item', 'hesabix') . '</p></div>';
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 '<div class="error"><p>' . __('The new Item code already used for another Item', 'hesabix') . '</p></div>';
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") {
?>
<div id="panel_product_data_hesabix" class="panel woocommerce_options_panel"
data-product-id="<?php echo $id_product ?>">
هنوز محصول ذخیره نشده است.
<br>
پس از ذخیره محصول، در این قسمت می توانید ارتباط محصول و متغیرهای آن با حسابیکس
را مدیریت کنید.
</div>
<?php
return;
}
global $items;
$items[] = HesabixItemService::mapProduct($product, $id_product, false);
$items[0]["Quantity"] = $product->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++;
}
}
?>
<div id="panel_product_data_hesabix" class="panel woocommerce_options_panel"
data-product-id="<?php echo $id_product ?>">
<table class="table table-striped">
<tr class="small fw-bold">
<td>نام کالا</td>
<td>کد در حسابیکس</td>
<td>ذخیره کد</td>
<td>حذف ارتباط</td>
<td>بروزرسانی قیمت و موجودی</td>
<td>قیمت</td>
<td>موجودی</td>
</tr>
<?php
foreach ($items as $item) {
?>
<tr>
<td><?php echo $item["Name"]; ?></td>
<td><input type="text" value="<?php echo $item["Code"]; ?>" id="hesabix-item-<?php echo $item["Id"]; ?>"
style="width: 75px;" class="hesabix-item-code" data-id="<?php echo $item["Id"]; ?>"></td>
<td><input type="button" value="ذخیره" data-id="<?php echo $item["Id"]; ?>"
class="button hesabix-item-save"></td>
<td><input type="button" value="حذف ارتباط" data-id="<?php echo $item["Id"]; ?>"
class="button hesabix-item-delete-link"></td>
<td><input type="button" value="بروزرسانی" data-id="<?php echo $item["Id"]; ?>"
class="button button-primary hesabix-item-update"></td>
<td id="hesabix-item-price-<?php echo $item["Id"] ?>">
<?php echo Hesabix_Admin_Functions::getPriceInWooCommerceDefaultCurrency($item["SellPrice"]); ?>
</td>
<td id="hesabix-item-quantity-<?php echo $item["Id"] ?>"><?php echo $item["Quantity"]; ?></td>
</tr>
<?php
}
?>
</table>
<input type="button" value="ذخیره همه" id="hesabix-item-save-all" class="button">
<input type="button" value="حذف ارتباط همه" id="hesabix-item-delete-link-all" class="button">
<input type="button" value="بروزرسانی همه" id="hesabix-item-update-all" class="button button-primary">
</div>
<?php
}
function admin_products_hesabixId_column($columns)
{
echo '<style>
#hesabixID {
width: 5vw;
color: #2271b1;
}
#hesabixID a {
width: 100%;
}
</style>';
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 '<div>';
foreach ($items as $item) {
if ($column == 'hesabixID') {
$hesabixId = $item["code"];
echo "<span class='button button-secondary'>" . $hesabixId . " " . "</span>";
}
}
echo '</div>';
}
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 '<p><strong>' . __('National code', 'hesabix') . ': </strong> ' . '<br>' . '<strong>' . get_post_meta($orderId, $NationalCode, true) . '</strong></p>';
}
if ($EconomicCode_isActive == 'yes')
echo '<p><strong>' . __('Economic code', 'hesabix') . ': </strong> ' . '<br>' . '<strong>' . get_post_meta($orderId, $EconomicCode, true) . '</strong></p>';
if ($RegistrationNumber_isActive == 'yes')
echo '<p><strong>' . __('Registration number', 'hesabix') . ': </strong> ' . '<br>' . '<strong>' . get_post_meta($orderId, $RegistrationNumber, true) . '</strong></p>';
if ($Website_isActive == 'yes')
echo '<p><strong>' . __('Website', 'hesabix') . ': </strong> ' . '<br>' . '<a target="_blank" href="https://' . get_post_meta($orderId, $Website, true) . '">' . get_post_meta($orderId, $Website, true) . '</a></p>';
}
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 {
2025-08-06 01:12:27 +03:30
$plugin_stats = wp_remote_get('https://app.hesabix.ir/api/wordpress/plugin/stats');
error_log(print_r($plugin_stats['body'], true));
2025-08-05 17:24:52 +03:30
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 adminSaveApiSettingsCallback()
{
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 adminTestApiConnectionCallback()
{
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 = '<div class="hesabix-api-error-container">
<div class="hesabix-error-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 473.931 473.931" xml:space="preserve"><circle style="fill:#e84849" cx="236.966" cy="236.966" r="236.966"/><path style="fill:#f4f5f5" d="M429.595 245.83c0 16.797-13.624 30.417-30.417 30.417H74.73c-16.797 0-30.421-13.62-30.421-30.417v-17.743c0-16.797 13.624-30.417 30.421-30.417h324.448c16.793 0 30.417 13.62 30.417 30.417z"/></svg>
</div>
<h3>' . __('API Connection Required', 'hesabix') . '</h3>
<p>' . __('This tab requires a valid API connection. Please configure your API settings from the API tab first.', 'hesabix') . '</p>
<div class="hesabix-error-actions">
<button type="button" class="hesabix-btn" onclick="hesabix_load_tab_content(\'api\')">
<svg fill="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M18.113 13.03a6 6 0 0 0 .057-.806c0-.28-.025-.542-.057-.806l1.735-1.357a.415.415 0 0 0 .099-.526l-1.645-2.846a.41.41 0 0 0-.502-.18l-2.048.822a6 6 0 0 0-1.39-.806l-.312-2.18A.4.4 0 0 0 13.647 4h-3.29a.4.4 0 0 0-.403.345l-.313 2.18a6.3 6.3 0 0 0-1.39.806l-2.047-.823a.4.4 0 0 0-.502.181L4.057 9.535a.405.405 0 0 0 .099.526l1.735 1.357a7 7 0 0 0-.058.806c0 .272.025.543.058.806l-1.735 1.357a.415.415 0 0 0-.099.527l1.645 2.845c.099.181.32.247.502.181l2.047-.822q.64.495 1.39.806l.313 2.18a.4.4 0 0 0 .403.345h3.29a.4.4 0 0 0 .403-.346l.312-2.179a6.3 6.3 0 0 0 1.39-.806l2.048.822c.19.074.403 0 .502-.18l1.645-2.846a.415.415 0 0 0-.1-.527zm-6.111 2.073a2.88 2.88 0 0 1-2.879-2.879 2.88 2.88 0 0 1 2.879-2.878 2.88 2.88 0 0 1 2.878 2.878 2.88 2.88 0 0 1-2.878 2.879" fill-rule="evenodd"/></svg>
' . __('Go to API Settings', 'hesabix') . '
</button>
</div>
</div>';
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()
));
}
}
}