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); 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; $id_product = 0; $id_attribute = 0; if (isset($item->code)) $hesabix_code = (int) $item->code; if (isset($item->id)) $hesabix_uid = (int) $item->id; if (isset($item->tags)) { $json = $item->tags; if (isset($json->id_product)) $id_product = (int) $json->id_product; if (isset($json->id_attribute)) $id_attribute = (int) $json->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); $wpFaService->save($wpFa); HesabixLogService::log(array("Item successfully added. Item code: $hesabix_code. Product ID: $id_product-$id_attribute")); } else { $wpFa->idHesabix = $hesabix_code; $wpFa->uidHesabix = $hesabix_uid; $wpFaService->update($wpFa); HesabixLogService::log(array("Item successfully updated. Item code: $hesabix_code. Product ID: $id_product-$id_attribute")); } return true; } public function saveCustomer($customer): bool { $json = json_decode($customer->tags); if ((int) $json->id_customer == 0) return true; $id = $this->getWpFaId('customer', (int) $json->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) $json->id_customer ), array( '%d', '%d', '%s', '%d' ) ); HesabixLogService::writeLogStr("Customer successfully added. Customer code: " . (string) $customer->code . ". Customer ID: $json->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) $json->id_customer, ), array('id' => $id), array( '%d', '%d', '%s', '%d' ), array('%d') ); HesabixLogService::writeLogStr("Customer successfully updated. Customer code: " . (string) $customer->code . ". Customer ID: $json->id_customer"); } return true; } public function saveInvoice($invoice, $orderType) { $json = json_decode($invoice->Tag); $id = $this->getPsFaId('order', (int) $json->id_order); $invoiceNumber = (int) $invoice->Number; $objType = $orderType == 0 ? 'order' : 'returnOrder'; if (!$id) { Db::getInstance()->insert('ps_hesabix', array( 'id_hesabix' => $invoiceNumber, 'obj_type' => $objType, 'id_ps' => (int) $json->id_order, )); if ($objType == 'order') LogService::writeLogStr("Invoice successfully added. invoice number: " . (string) $invoice->Number . ", order id: " . $json->id_order); else LogService::writeLogStr("Return Invoice successfully added. Customer code: " . (string) $invoice->Number . ", order id: " . $json->id_order); } else { Db::getInstance()->update('ps_hesabix', array( 'id_hesabix' => $invoiceNumber, 'obj_type' => $objType, 'id_ps' => (int) $json->id_order, ), array('id' => $id), 0, true, true); if ($objType == 'order') LogService::writeLogStr("Invoice successfully updated. invoice number: " . (string) $invoice->Number . ", order id: " . $json->id_order); else LogService::writeLogStr("Return Invoice successfully updated. Customer code: " . (string) $invoice->Number . ", order id: " . $json->id_order); } return true; } public function save(WpFa $wpFa) { global $wpdb; $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( '%s', '%s', '%d', '%d', '%s' ) ); } public function update(WpFa $wpFa) { global $wpdb; $idHesabix = isset($wpFa->idHesabix) ? sanitize_text_field($wpFa->idHesabix) : ''; $uidHesabix = isset($wpFa->uidHesabix) ? sanitize_text_field($wpFa->uidHesabix) : ''; $objType = isset($wpFa->objType) ? sanitize_text_field($wpFa->objType) : ''; $idWp = isset($wpFa->idWp) ? (int) $wpFa->idWp : 0; $idWpAttribute = isset($wpFa->idWpAttribute) ? (int) $wpFa->idWpAttribute : 0; $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( '%s', '%s', '%d', '%d', '%s' ), array('%d') ); } 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') ); } }