id = $id; $instance->objType = $type; $instance->idHesabix = $idHesabix; $instance->idWp = $idWp; $instance->idWpAttribute = $idWpAttribute; $instance->uidHesabix = $uidHesabix; return $instance; } } class HesabixWpFaService { public function __construct() { } public function getWpFaByWpId($objType, $idWp, $idWpAttribute = 0) { if (!isset($objType) || !isset($idWp)) return false; global $wpdb; $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}hesabix WHERE `id_ps` = %d AND `id_ps_attribute` = %d AND `obj_type` = %s", $idWp, $idWpAttribute, $objType ) ); if (isset($row)) return $this->mapWpFa($row); return null; } public function getWpFaByHesabixCode($objType, $codehx, $idWpAttribute = 0) { if (!isset($objType) || !isset($codehx)) return false; global $wpdb; $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}hesabix WHERE `id_hesabix` = %d AND `id_ps_attribute` = %d AND `obj_type` = %s", $codehx, $idWpAttribute, $objType ) ); if (isset($row)) return $this->mapWpFa($row); // If no exact match, try to find any product with this hesabix code $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}hesabix WHERE `id_hesabix` = %d AND `obj_type` = %s LIMIT 1", $codehx, $objType ) ); if (isset($row)) return $this->mapWpFa($row); return null; } public function getWpFaSearch($woocommerce_search_code = '', $woocommerce_attribute_search_code = '', $hesabix_search_code = '', $obj_type_search = '') { global $wpdb; $conditions = []; $params = []; if ($woocommerce_search_code !== '') { $conditions[] = "id_ps = %s"; $params[] = $woocommerce_search_code; } if ($woocommerce_attribute_search_code !== '' || $woocommerce_attribute_search_code === '0') { $conditions[] = "id_ps_attribute = %s"; $params[] = $woocommerce_attribute_search_code; } if ($hesabix_search_code !== '') { $conditions[] = "id_hesabix = %s"; $params[] = $hesabix_search_code; } if ($obj_type_search !== '' && $obj_type_search != '0') { $conditions[] = "obj_type = %s"; $params[] = $obj_type_search; } $sql = "SELECT * FROM {$wpdb->prefix}hesabix"; if (!empty($conditions)) { $sql .= " WHERE " . implode(" AND ", $conditions); } $prepared_sql = $wpdb->prepare($sql, ...$params); $result = $wpdb->get_results($prepared_sql); $wpFaObjects = array(); if (isset($result) && is_array($result) && count($result) > 0) { foreach ($result as $item) { $wpFaObjects[] = $this->mapWpFa($item); } } return $wpFaObjects; } public function getWpFaByHesabixId($objType, $hesabixId) { if (!isset($objType) || !isset($hesabixId)) return false; global $wpdb; $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}hesabix WHERE `id_hesabix` = %d AND `obj_type` = %s", $hesabixId, $objType ) ); if (isset($row)) return $this->mapWpFa($row); return null; } public function getWpFaId($objType, $idWp, $idWpAttribute = 0) { if (!isset($objType) || !isset($idWp)) return false; global $wpdb; $row = $wpdb->get_row( $wpdb->prepare( "SELECT `id` FROM {$wpdb->prefix}hesabix WHERE `id_ps` = %d AND `id_ps_attribute` = %d AND `obj_type` = %s", $idWp, $idWpAttribute, $objType ) ); if (is_object($row)) return (int) $row->id; else return false; } public function getWpFaIdByHesabixId($objType, $hesabixId) { if (!isset($objType) || !isset($hesabixId)) return false; global $wpdb; $row = $wpdb->get_row( $wpdb->prepare( "SELECT `id` FROM {$wpdb->prefix}hesabix WHERE `id_hesabix` = %d AND `obj_type` = %s", $hesabixId, $objType ) ); if (isset($row)) return (int) $row->id; return null; } public function getProductCodeByWpId($id_product, $id_attribute = 0) { $obj = $this->getWpFaByWpId('product', $id_product, $id_attribute); if ($obj != null) return $obj->idHesabix; return null; } public function getProductUidByWpId($id_product, $id_attribute = 0) { $obj = $this->getWpFaByWpId('product', $id_product, $id_attribute); if ($obj != null) return $obj->uidHesabix; return null; } public function getProductUidByCode($code) { $obj = $this->getWpFaByHesabixCode('product', $code); if ($obj != null) return $obj->uidHesabix; $api = new Hesabix_Api(); $response = $api->apiRequest('api/commodity/info/' . $code); if ($response->code == $code) { return $response->id; } return null; } public function getCustomerCodeByWpId($id_customer) { $obj = $this->getWpFaByWpId('customer', $id_customer); if ($obj != null) return $obj->idHesabix; return null; } public function getCustomerUidByWpId($id_customer) { $obj = $this->getWpFaByWpId('customer', $id_customer); if ($obj != null) return $obj->uidHesabix; return null; } public function getCustomerUidByCode($code_customer) { $obj = $this->getWpFaByHesabixCode('customer', $code_customer); if ($obj != null) return $obj->uidHesabix; return null; } public function getInvoiceCodeByWpId($id_order) { $obj = $this->getWpFaByWpId('order', $id_order); if ($obj != null) return $obj->idHesabix; return null; } public function getInvoiceUidByWpId($id_order) { $obj = $this->getWpFaByWpId('order', $id_order); if ($obj != null) return $obj->uidHesabix; return null; } public function getProductAndCombinations($idWp) { global $wpdb; $sql = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}hesabix WHERE `obj_type` = 'product' AND `id_ps` = %d", $idWp ); $result = $wpdb->get_results($sql); $wpFaObjects = array(); if (isset($result) && is_array($result) && count($result) > 0) { foreach ($result as $item) $wpFaObjects[] = $this->mapWpFa($item); return $wpFaObjects; } return null; } public function mapWpFa($sqlObj): WpFa { $wpFa = new WpFa(); $wpFa->id = $sqlObj->id; $wpFa->idHesabix = $sqlObj->id_hesabix; $wpFa->uidHesabix = $sqlObj->uid_hesabix; $wpFa->idWp = $sqlObj->id_ps; $wpFa->idWpAttribute = $sqlObj->id_ps_attribute; $wpFa->objType = $sqlObj->obj_type; return $wpFa; } public function saveProduct($item): bool { $hesabix_code = 0; $hesabix_uid = 0; $id_product = 0; $id_attribute = 0; if (isset($item->code)) $hesabix_code = (int) $item->code; if (isset($item->id)) $hesabix_uid = (int) $item->id; else $hesabix_uid = 0; // Check both possible tag field names $tagData = null; if (isset($item->tags)) { if (is_string($item->tags)) { $tagData = json_decode($item->tags); } else { $tagData = $item->tags; } } elseif (isset($item->Tag)) { if (is_string($item->Tag)) { $tagData = json_decode($item->Tag); } else { $tagData = $item->Tag; } } if ($tagData) { if (isset($tagData->id_product)) $id_product = (int) $tagData->id_product; if (isset($tagData->id_attribute)) $id_attribute = (int) $tagData->id_attribute; } $wpFaService = new HesabixWpFaService(); $wpFa = $wpFaService->getWpFaByHesabixId('product', $hesabix_code); if (!$wpFa) { $wpFa = WpFa::newWpFa(0, 'product', $hesabix_code, $id_product, $id_attribute, $hesabix_uid); $result = $wpFaService->save($wpFa); if ($result) { HesabixLogService::log(array("Item successfully added. Item code: $hesabix_code. Product ID: $id_product-$id_attribute")); } else { HesabixLogService::log(array("Failed to add item to database. Item code: $hesabix_code. Product ID: $id_product-$id_attribute")); return false; } } else { $wpFa->idHesabix = $hesabix_code; $wpFa->uidHesabix = $hesabix_uid; $result = $wpFaService->update($wpFa); if ($result) { HesabixLogService::log(array("Item successfully updated. Item code: $hesabix_code. Product ID: $id_product-$id_attribute")); } else { HesabixLogService::log(array("Failed to update item in database. Item code: $hesabix_code. Product ID: $id_product-$id_attribute")); return false; } } return true; } public function saveCustomer($customer): bool { // Handle both string and object formats for tags $tagData = null; if (isset($customer->tags)) { if (is_string($customer->tags)) { $tagData = json_decode($customer->tags); } else { $tagData = $customer->tags; } } elseif (isset($customer->Tag)) { if (is_string($customer->Tag)) { $tagData = json_decode($customer->Tag); } else { $tagData = $customer->Tag; } } if (!$tagData || (int) $tagData->id_customer == 0) return true; $id = $this->getWpFaId('customer', (int) $tagData->id_customer); global $wpdb; if (!$id) { $wpdb->insert( $wpdb->prefix . 'hesabix', array( 'id_hesabix' => (int) $customer->code, 'uid_hesabix' => (int) $customer->id, 'obj_type' => 'customer', 'id_ps' => (int) $tagData->id_customer ), array( '%d', '%d', '%s', '%d' ) ); HesabixLogService::writeLogStr("Customer successfully added. Customer code: " . (string) $customer->code . ". Customer ID: " . $tagData->id_customer); } else { $wpdb->update( $wpdb->prefix . 'hesabix', array( 'id_hesabix' => (int) $customer->code, 'uid_hesabix' => (int) $customer->id, 'obj_type' => 'customer', 'id_ps' => (int) $tagData->id_customer, ), array('id' => $id), array( '%d', '%d', '%s', '%d' ), array('%d') ); HesabixLogService::writeLogStr("Customer successfully updated. Customer code: " . (string) $customer->code . ". Customer ID: " . $tagData->id_customer); } return true; } public function saveInvoice($invoice, $orderType) { // Handle both string and object formats for Tag $tagData = null; if (isset($invoice->Tag)) { if (is_string($invoice->Tag)) { $tagData = json_decode($invoice->Tag); } else { $tagData = $invoice->Tag; } } elseif (isset($invoice->tags)) { if (is_string($invoice->tags)) { $tagData = json_decode($invoice->tags); } else { $tagData = $invoice->tags; } } if (!$tagData) { HesabixLogService::log(array("No tag data found for invoice")); return false; } $id = $this->getWpFaId('order', (int) $tagData->id_order); $invoiceNumber = (int) $invoice->Number; $objType = $orderType == 0 ? 'order' : 'returnOrder'; if (!$id) { global $wpdb; $result = $wpdb->insert( $wpdb->prefix . 'hesabix', array( 'id_hesabix' => $invoiceNumber, 'obj_type' => $objType, 'id_ps' => (int) $tagData->id_order, ), array( '%d', '%s', '%d' ) ); if ($result) { if ($objType == 'order') HesabixLogService::log(array("Invoice successfully added. invoice number: " . (string) $invoice->Number . ", order id: " . $tagData->id_order)); else HesabixLogService::log(array("Return Invoice successfully added. Customer code: " . (string) $invoice->Number . ", order id: " . $tagData->id_order)); } else { HesabixLogService::log(array("Failed to add invoice to database: " . $wpdb->last_error)); return false; } } else { global $wpdb; $result = $wpdb->update( $wpdb->prefix . 'hesabix', array( 'id_hesabix' => $invoiceNumber, 'obj_type' => $objType, 'id_ps' => (int) $tagData->id_order, ), array('id' => $id), array( '%d', '%s', '%d' ), array('%d') ); if ($result) { if ($objType == 'order') HesabixLogService::log(array("Invoice successfully updated. invoice number: " . (string) $invoice->Number . ", order id: " . $tagData->id_order)); else HesabixLogService::log(array("Return Invoice successfully updated. Customer code: " . (string) $invoice->Number . ", order id: " . $tagData->id_order)); } else { HesabixLogService::log(array("Failed to update invoice in database: " . $wpdb->last_error)); return false; } } return true; } public function save(WpFa $wpFa) { global $wpdb; $result = $wpdb->insert( $wpdb->prefix . 'hesabix', array( 'id_hesabix' => $wpFa->idHesabix, 'obj_type' => $wpFa->objType, 'id_ps' => (int) $wpFa->idWp, 'id_ps_attribute' => (int) $wpFa->idWpAttribute, 'uid_hesabix' => (int) $wpFa->uidHesabix, ), array( '%d', '%s', '%d', '%d', '%d' ) ); if ($result === false) { HesabixLogService::log(array("Error inserting into database: " . $wpdb->last_error)); return false; } return true; } public function update(WpFa $wpFa) { global $wpdb; $idHesabix = isset($wpFa->idHesabix) ? (int) $wpFa->idHesabix : 0; $uidHesabix = isset($wpFa->uidHesabix) ? (int) $wpFa->uidHesabix : 0; $objType = isset($wpFa->objType) ? $wpFa->objType : ''; $idWp = isset($wpFa->idWp) ? (int) $wpFa->idWp : 0; $idWpAttribute = isset($wpFa->idWpAttribute) ? (int) $wpFa->idWpAttribute : 0; $result = $wpdb->update( $wpdb->prefix . 'hesabix', array( 'id_hesabix' => $idHesabix, 'obj_type' => $objType, 'id_ps' => $idWp, 'id_ps_attribute' => $idWpAttribute, 'uid_hesabix' => $uidHesabix, ), array('id' => $wpFa->id), array( '%d', '%s', '%d', '%d', '%d' ), array('%d') ); if ($result === false) { HesabixLogService::log(array("Error updating database: " . $wpdb->last_error)); return false; } return true; } public function delete(WpFa $wpFa) { global $wpdb; $id = isset($wpFa->id) ? (int) $wpFa->id : 0; $wpdb->delete( $wpdb->prefix . 'hesabix', array('id' => $id), array('%d') ); } public function deleteAll($productId) { global $wpdb; $productId = isset($productId) ? (int) $productId : 0; $wpdb->delete( $wpdb->prefix . 'hesabix', array('id_ps' => $productId), array('%d') ); } }