CMXX - Catalog API
This commit is contained in:
141
api/v2/get/catalog.php
Normal file
141
api/v2/get/catalog.php
Normal file
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
defined($security_key) or exit;
|
||||
|
||||
//------------------------------------------
|
||||
// Catalog
|
||||
//------------------------------------------
|
||||
|
||||
//------------------------------------------
|
||||
//Create Catalog
|
||||
//------------------------------------------
|
||||
$catalog = []; // Main catalog array
|
||||
|
||||
//------------------------------------------
|
||||
//GET ACTIVE AND SALES RELATED PRODUCTS
|
||||
//------------------------------------------
|
||||
|
||||
$filter = (isset($get_content) && $get_content !='') ? '&'.$get_content : '';
|
||||
|
||||
//GET PRODUCTS
|
||||
$api_url = '/v2/products/salesflag=1&status=1'.$filter;
|
||||
$products = ioApi($api_url,'',$clientsecret);
|
||||
$products = json_decode($products,true);
|
||||
|
||||
foreach ($products as $product) {
|
||||
|
||||
//------------------------------------------
|
||||
// Create product entry in catalog if it doesn't exist
|
||||
//------------------------------------------
|
||||
if (!isset($catalog[$product['rowID']])) {
|
||||
$catalog[$product['rowID']] = $product;
|
||||
$catalog[$product['rowID']]['versions'] = []; // Changed to versions array
|
||||
}
|
||||
|
||||
//------------------------------------------
|
||||
//Check for configurations and add to product
|
||||
//------------------------------------------
|
||||
if (isset($product['configurable']) && $product['configurable'] == 1){
|
||||
|
||||
//GET ACTIVE CONFIGURATIONS ITEMS BASED ON ACTIVE VERSIONS
|
||||
$api_url = '/v2/products_configurations/status=1&version_status=1&productrowid='.$product['rowID'] ;
|
||||
$product_config = ioApi($api_url,'',$clientsecret);
|
||||
$product_config = json_decode($product_config,true);
|
||||
|
||||
//------------------------------------------
|
||||
// Group configurations by version
|
||||
//------------------------------------------
|
||||
$version_configurations = [];
|
||||
|
||||
foreach ($product_config as $item) {
|
||||
if ($item['productrowid'] == $product['rowID']) {
|
||||
// Initialize version array if it doesn't exist
|
||||
if (!isset($version_configurations[$item['version']])) {
|
||||
$version_configurations[$item['version']] = [
|
||||
'version_id' => $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;
|
||||
|
||||
?>
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user