From a669b2fadfa3ed8ecf2b04b7df9faa22774f8db6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CVeLiTi=E2=80=9D?= <“info@veliti.nl”> Date: Thu, 30 Jan 2025 09:28:50 +0100 Subject: [PATCH] CMXX - Catalog API --- api/v1/get/equipments.php | 2 +- api/v2/get/catalog.php | 141 ++++++++++++++++++++++ api/v2/get/equipments.php | 2 +- api/v2/get/pricelists.php | 24 +++- api/v2/get/products.php | 31 ++++- api/v2/get/products_attributes_items.php | 31 +++-- api/v2/get/products_configurations.php | 28 +++-- assets/functions.php | 22 ++++ catalog.php | 89 ++++++++++++++ equipment.php | 15 ++- equipments.php | 27 +++-- product.php | 27 ++++- product_manage.php | 86 ++++++++++++-- products.php | 21 +++- products_attributes.php | 4 +- products_attributes_manage.php | 2 +- products_configurations.php | 142 ++++++++++++++--------- settings/settingsprofiles.php | 2 +- settings/settingsviews.php | 1 + 19 files changed, 579 insertions(+), 118 deletions(-) create mode 100644 api/v2/get/catalog.php create mode 100644 catalog.php diff --git a/api/v1/get/equipments.php b/api/v1/get/equipments.php index 27915d1..68d1246 100644 --- a/api/v1/get/equipments.php +++ b/api/v1/get/equipments.php @@ -272,7 +272,7 @@ else { } //SQL for Paging - $sql = 'SELECT e.rowID as equipmentID, e.*, p.productcode, p.productname from equipment e LEFT JOIN products p ON e.productrowid = p.rowID '.$whereclause.' ORDER BY '.$sort.' LIMIT :page,:num_products'; + $sql = 'SELECT e.rowID as equipmentID, e.*, p.productcode, p.productname, p.product_media from equipment e LEFT JOIN products p ON e.productrowid = p.rowID '.$whereclause.' ORDER BY '.$sort.' LIMIT :page,:num_products'; } $stmt = $pdo->prepare($sql); diff --git a/api/v2/get/catalog.php b/api/v2/get/catalog.php new file mode 100644 index 0000000..98ccdc2 --- /dev/null +++ b/api/v2/get/catalog.php @@ -0,0 +1,141 @@ + $item['version'], + 'config_setting' => $item['config'], + 'configurations' => [] + ]; + } + + if ($item['type'] == 'product') { + $version_configurations[$item['version']]['configurations'][] = $item; + } + + if ($item['type'] == 'group') { + $api_url = '/v2/products_attributes_items/item_status=1&list=catalog&group_id='.$item['assignment']; + $attributes = ioApi($api_url,'',$clientsecret); + $attributes = json_decode($attributes,true); + + // Add attributes to the group item + $item['attributes'] = $attributes; + $version_configurations[$item['version']]['configurations'][] = $item; + } + } + } + + // Add all version configurations to the catalog + $catalog[$product['rowID']]['versions'] = array_values($version_configurations); + } +} + +//------------------------------------------ +// Lookup pricing (active pricelist and pricelistitems only) +//------------------------------------------ +$api_url = '/v2/pricelists/status=1&item_status=1&list=price'; +$pricelist = ioApi($api_url,'',$clientsecret); +$pricelist = json_decode($pricelist,true); + +foreach ($pricelist as $price) { + + // Add price to product level + if (isset($catalog[$price['product_id']])) { + $catalog[$price['product_id']]['price'] = $price['price']; + $catalog[$price['product_id']]['rrp'] = $price['rrp']; + $catalog[$price['product_id']]['price_modifier'] = $price['price_modifier']; + } + + //Check for configuration (can also include products as above) + foreach ($catalog as &$items) { + if (!empty($items['versions'])) { + foreach ($items['versions'] as &$version) { + foreach ($version['configurations'] as &$config) { + //UPDATE PRODUCT PRICES IN CONFIGURATION + if ($config['type'] == 'product' && $config['assignment'] == $price['product_id']) { + $config['price'] = $price['price']; + $config['rrp'] = $price['rrp']; + $config['price_modifier'] = $price['price_modifier']; + } + + //UPDATE PRICES OF ATTRIBUTES IN GROUPS + if ($config['type'] == 'group') { + //check all attributes + foreach($config['attributes'] as &$attribute) { + if ($attribute['attribute_id'] == $price['product_id']) { + $attribute['price'] = $price['price']; + $attribute['rrp'] = $price['rrp']; + $attribute['price_modifier'] = $price['price_modifier']; + } + } + } + } + } + } + } +} +//------------------------------------------ +//REMOVE KEYS FROM OUTPUT +//------------------------------------------ +$keys_to_remove = ['status','item_status','group_status','version','config','sn','build','softwareversion','healthindex','salesflag','configurable','updatedby','createdby','updated','created']; +removeKeysRecursive($catalog,$keys_to_remove); + +//------------------------------------------ +//JSON_ENCODE +//------------------------------------------ +$messages = json_encode($catalog, JSON_UNESCAPED_UNICODE); + +//------------------------------------------ +//Send results +//------------------------------------------ +echo $messages; + +?> \ No newline at end of file diff --git a/api/v2/get/equipments.php b/api/v2/get/equipments.php index a31b9de..008513c 100644 --- a/api/v2/get/equipments.php +++ b/api/v2/get/equipments.php @@ -267,7 +267,7 @@ else { } //SQL for Paging - $sql = 'SELECT e.rowID as equipmentID, e.*, p.productcode, p.productname from equipment e LEFT JOIN products p ON e.productrowid = p.rowID '.$whereclause.' ORDER BY '.$sort.' LIMIT :page,:num_products'; + $sql = 'SELECT e.rowID as equipmentID, e.*, p.productcode, p.productname, p.product_media from equipment e LEFT JOIN products p ON e.productrowid = p.rowID '.$whereclause.' ORDER BY '.$sort.' LIMIT :page,:num_products'; } $stmt = $pdo->prepare($sql); diff --git a/api/v2/get/pricelists.php b/api/v2/get/pricelists.php index 6379912..4ed1645 100644 --- a/api/v2/get/pricelists.php +++ b/api/v2/get/pricelists.php @@ -23,7 +23,7 @@ switch ($permission) { break; default: $condition = '__salesid___'.$partner->salesid.'___soldto___'.substr($partner->soldto, 0, strpos($partner->soldto, "-")).$soldto_search; - $whereclause = 'WHERE accounthierarchy like "'.$condition.'"'; + $whereclause = 'WHERE pls.accounthierarchy like "'.$condition.'"'; break; } @@ -46,10 +46,14 @@ if(isset($get_content) && $get_content!=''){ } elseif ($v[0] == 'name') { //build up search - $clause .= ' AND name like :'.$v[0]; + $clause .= ' AND pls.name like :'.$v[0]; + } + elseif ($v[0] == 'item_status') { + //build up search + $clause .= ' AND pli.status = :'.$v[0]; } else {//create clause - $clause .= ' AND '.$v[0].' = :'.$v[0]; + $clause .= ' AND pls.'.$v[0].' = :'.$v[0]; } } if ($whereclause == '' && $clause !=''){ @@ -61,11 +65,15 @@ if(isset($get_content) && $get_content!=''){ //Define Query if(isset($criterias['totals']) && $criterias['totals'] ==''){ //Request for total rows - $sql = 'SELECT count(*) as count FROM pricelists '.$whereclause.''; + $sql = 'SELECT count(*) as count FROM pricelists pls '.$whereclause.''; } +elseif (isset($criterias['list']) && $criterias['list'] =='price'){ + $sql = 'SELECT pls.*,pli.* FROM pricelists pls JOIN pricelists_items pli ON pls.rowID = pli.pricelist_ID '.$whereclause; + +} else { //SQL for Paging - $sql = 'SELECT * FROM pricelists '.$whereclause.' LIMIT :page,:num_products'; + $sql = 'SELECT * FROM pricelists pls '.$whereclause.' LIMIT :page,:num_products'; } $stmt = $pdo->prepare($sql); @@ -96,6 +104,12 @@ if(isset($criterias['totals']) && $criterias['totals']==''){ $messages = $stmt->fetch(); $messages = $messages[0]; } +elseif(isset($criterias['list'])){ + //Excute Query + $stmt->execute(); + //Get results + $messages = $stmt->fetchAll(PDO::FETCH_ASSOC); +} else { $current_page = isset($criterias['p']) && is_numeric($criterias['p']) ? (int)$criterias['p'] : 1; $stmt->bindValue('page', ($current_page - 1) * $page_rows_pricelists, PDO::PARAM_INT); diff --git a/api/v2/get/products.php b/api/v2/get/products.php index c195307..b66e52e 100644 --- a/api/v2/get/products.php +++ b/api/v2/get/products.php @@ -48,6 +48,11 @@ if(isset($get_content) && $get_content!=''){ //build up search $clause .= ' AND productcode like :'.$v[0]; } + elseif ($v[0] == 'list' && $v[1] == 'price') { + //Add default selection criteria for LIST + $clause .= ' AND salesflag = 1'; + $clause .= ' AND item_status = 1'; + } else {//create clause $clause .= ' AND '.$v[0].' = :'.$v[0]; } @@ -68,8 +73,32 @@ elseif (isset($criterias['list']) && $criterias['list'] =='') { $sql = 'SELECT * FROM products '.$whereclause.''; } elseif (isset($criterias['list']) && $criterias['list'] =='price'){ + + //SET SPECIFIC WHERECLAUSE + if ($whereclause == ''){ + $whereclause_1 = 'WHERE salesflag = 1'; + $whereclause_2 = 'WHERE item_status = 1'; + } else { + $whereclause_1 = $whereclause .' AND salesflag = 1 AND status = 1 '; + $whereclause_2 = $whereclause .' AND item_status = 1'; + } + //GET ALL PRODUCTS AND PRODUCT ATTRIBUTES FOR PRICING - $sql = '(SELECT rowID as product_id, productname as product_name FROM products where salesflag = 1 '.$whereclause.' ) UNION (SELECT attribute_id as product_id, item_name as product_name FROM `products_attributes_items` WHERE item_status = 1 '.$whereclause.' )'; + $sql = '(SELECT rowID as product_id, productname as product_name FROM products '.$whereclause_1.' ) UNION (SELECT attribute_id as product_id, item_name as product_name FROM `products_attributes_items` '.$whereclause_2.' )'; +} +elseif (isset($criterias['list']) && $criterias['list'] =='config'){ + + //SET SPECIFIC WHERECLAUSE + if ($whereclause == ''){ + $whereclause_1 = 'WHERE salesflag = 1 AND status = 1 AND configurable = 0 '; + $whereclause_2 = 'WHERE group_status = 1'; + } else { + $whereclause_1 = $whereclause .' AND salesflag = 1 AND status = 1 AND configurable = 0 '; + $whereclause_2 = $whereclause .' AND group_status = 1'; + } + + //GET ALL PRODUCTS AND PRODUCT ATTRIBUTES FOR PRICING + $sql = '(SELECT rowID as product_id, productname as product_name FROM products '.$whereclause_1.' ) UNION (SELECT group_id as product_id, group_name as product_name FROM `products_attributes_groups` '.$whereclause_2.' )'; } else { //SQL for Paging diff --git a/api/v2/get/products_attributes_items.php b/api/v2/get/products_attributes_items.php index 4122bdf..94ca0e4 100644 --- a/api/v2/get/products_attributes_items.php +++ b/api/v2/get/products_attributes_items.php @@ -43,15 +43,16 @@ if(isset($get_content) && $get_content!=''){ if ($v[0] == 'page' || $v[0] =='p' || $v[0] =='totals' || $v[0] =='list' || $v[0] =='media'|| $v[0] =='success_msg'){ //do nothing - } - elseif ($v[0] == 'search') { - //build up search - $clause .= ' AND translation like :'.$v[0]; } else {//create clause - $clause .= ' AND '.$v[0].' = :'.$v[0]; + $clause .= ' AND pat.'.$v[0].' = :'.$v[0]; } } + //WHEN LIST = CATALOG change select based on GROUPS instead of ITEMS + if ($criterias['list'] && $criterias['list'] == 'catalog'){ + $clause = str_replace('pat.group_id','pag.group_id',$clause); + } + //CREATE WHERE CLAUSE if ($whereclause == '' && $clause !=''){ $whereclause = 'WHERE '.substr($clause, 4); } else { @@ -60,14 +61,20 @@ if(isset($get_content) && $get_content!=''){ } //Define Query if(isset($criterias['totals']) && $criterias['totals'] ==''){ -//Request for total rows + //Request for total rows $sql = 'SELECT count(*) as count FROM products_attributes_items '.$whereclause.''; + } elseif ($criterias['media'] && $criterias['media'] =='all'){ + //GET ALL MEDIA ITEMS RELATED TO ATTRIBUTES $sql = 'SELECT pat.*, m.full_path FROM products_attributes_items pat LEFT JOIN media m ON pat.item_media = m.rowID '.$whereclause; -} -else { + +} elseif ($criterias['list'] && $criterias['list'] =='catalog'){ + //GET ALL ATTRIBUTE DATA FOR CATALOG GROUPS,ITEMS,MEDIA + $sql = 'SELECT pag.*, pat.*, m.title, m.full_path FROM products_attributes_groups pag JOIN products_attributes_items pat ON pag.rowID = pat.group_id LEFT JOIN media m ON pat.item_media = m.rowID '.$whereclause; + +} else { //SQL for Paging - $sql = 'SELECT * FROM products_attributes_items '.$whereclause.' LIMIT :page,:num_products'; + $sql = 'SELECT * FROM products_attributes_items pat '.$whereclause.' LIMIT :page,:num_products'; } $stmt = $pdo->prepare($sql); @@ -104,6 +111,12 @@ elseif($criterias['media'] && $criterias['media'] =='all'){ //Get results $messages = $stmt->fetchAll(PDO::FETCH_ASSOC); } +elseif($criterias['list'] && $criterias['list'] =='catalog'){ + //Excute Query + $stmt->execute(); + //Get results + $messages = $stmt->fetchAll(PDO::FETCH_ASSOC); +} else { $current_page = isset($criterias['p']) && is_numeric($criterias['p']) ? (int)$criterias['p'] : 1; $stmt->bindValue('page', ($current_page - 1) * $page_rows_products_attributes, PDO::PARAM_INT); diff --git a/api/v2/get/products_configurations.php b/api/v2/get/products_configurations.php index 51bf815..77aef66 100644 --- a/api/v2/get/products_configurations.php +++ b/api/v2/get/products_configurations.php @@ -23,7 +23,7 @@ switch ($permission) { break; default: $condition = '__salesid___'.$partner->salesid.'___soldto___'.substr($partner->soldto, 0, strpos($partner->soldto, "-")).$soldto_search; - $whereclause = 'WHERE accounthierarchy like "'.$condition.'"'; + $whereclause = 'WHERE pc.accounthierarchy like "'.$condition.'"'; break; } @@ -43,13 +43,11 @@ if(isset($get_content) && $get_content!=''){ if ($v[0] == 'page' || $v[0] =='p' || $v[0] =='totals' || $v[0] =='list' || $v[0] =='history'|| $v[0] =='success_msg'){ //do nothing - } - elseif ($v[0] == 'search') { - //build up search - $clause .= ' AND productcode like :'.$v[0]; + } elseif($v[0] == 'version_status'){ + $clause .= ' AND pv.status = :'.$v[0]; } else {//create clause - $clause .= ' AND '.$v[0].' = :'.$v[0]; + $clause .= ' AND pc.'.$v[0].' = :'.$v[0]; } } if ($whereclause == '' && $clause !=''){ @@ -75,15 +73,25 @@ if (isset($criterias['productrowid']) && $criterias['productrowid'] != ''){ //Define Query if(isset($criterias['totals']) && $criterias['totals'] ==''){ //Request for total rows - $sql = 'SELECT count(*) as count FROM products_configurations '.$whereclause.''; + $sql = 'SELECT count(*) as count FROM products_configurations pc '.$whereclause.''; } elseif (isset($criterias['list']) && $criterias['list'] =='') { //SQL for Paging - $sql = 'SELECT * FROM products_configurations '.$whereclause.''; + $sql = 'SELECT * FROM products_configurations pc '.$whereclause.''; } else { - //SQL for Paging - $sql = 'SELECT * FROM products_configurations '.$whereclause.''; + //SQL for Paging include name from different tables + $sql = 'SELECT pc.*, pv.config, + CASE WHEN p.rowID IS NOT NULL THEN p.productname + WHEN pag.group_id IS NOT NULL THEN pag.group_name + END AS assignment_name, + CASE WHEN p.rowID IS NOT NULL THEN "product" + WHEN pag.group_id IS NOT NULL THEN "group" + END AS type + FROM products_configurations pc + LEFT JOIN products p ON p.rowID = pc.assignment + LEFT JOIN products_attributes_groups pag ON pag.group_id = pc.assignment + LEFT JOIN products_versions pv ON pv.rowID = pc.version '.$whereclause.''; } $stmt = $pdo->prepare($sql); diff --git a/assets/functions.php b/assets/functions.php index 0b99d13..62ec1e8 100644 --- a/assets/functions.php +++ b/assets/functions.php @@ -2961,4 +2961,26 @@ function generateLanguageFile($language_key,$token){ } } +} +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++ +// Removekeys from array ++++++++++++++ +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++ +function removeKeysRecursive(array &$array, array $keysToRemove): void { + foreach ($array as $key => &$value) { + // Remove the key if it exists in our removal list + if (in_array($key, $keysToRemove, true)) { + unset($array[$key]); + continue; + } + + // If value is an array, recursively process it + if (is_array($value)) { + removeKeysRecursive($value, $keysToRemove); + + // If array is empty after processing, remove it + if (empty($value)) { + unset($array[$key]); + } + } + } } \ No newline at end of file diff --git a/catalog.php b/catalog.php new file mode 100644 index 0000000..175b259 --- /dev/null +++ b/catalog.php @@ -0,0 +1,89 @@ + +
'.($catalog_p ?? '').'
+'.$success_msg.'
+ +' . $description . '+
'.(($view_product == 1)? ''.(${$responses->productname} ?? $responses->productname).'':(${$responses->productname} ?? $responses->productname)).'
'.(${$responses->productname} ?? $responses->productname).'
+'.(${$responses->url_slug} ?? $responses->url_slug).'