Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Update eCommerceSite.class.php #99

Open
wants to merge 1 commit into
base: 2022.5.3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion class/data/eCommerceSite.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,32 @@ function __construct($db)
function cleanOrphelins()
{
$stopwatch_id = eCommerceUtils::startAndLogStopwatch(__METHOD__);

require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
$cat = new Categorie( $this->db);
$all_cat = $cat->get_full_arbo('product');

$parent_id = $this->fk_cat_product;

$cat_arbo = array();
foreach ($all_cat as $category) {
if (preg_match('/_'.$parent_id.'(_|$)/', $category['fullpath'])) {
$cat_arbo[$category['id']] = $category;
}

$cache_categories['product'] = $cat_arbo;
}

$cats_id = array_keys($cache_categories['product']);

// Clean orphelins entries to have a clean database (having such records should not happen)
/*$sql = "DELETE FROM ".MAIN_DB_PREFIX."ecommerce_category WHERE type = ".Categorie::TYPE_PRODUCT." AND fk_category NOT IN (select rowid from ".MAIN_DB_PREFIX."categorie)";
$this->db->query($sql);
$sql = "DELETE FROM ".MAIN_DB_PREFIX."ecommerce_category WHERE type = ".Categorie::TYPE_CUSTOMER." AND fk_category NOT IN (select rowid from ".MAIN_DB_PREFIX."categorie)";
$this->db->query($sql);*/
$sql = "DELETE FROM " . MAIN_DB_PREFIX . "ecommerce_category WHERE fk_category NOT IN (select rowid from " . MAIN_DB_PREFIX . "categorie)";
$this->db->query($sql);
$sql = "DELETE " . MAIN_DB_PREFIX . "ecommerce_product FROM " . MAIN_DB_PREFIX . "ecommerce_product LEFT JOIN (SELECT DISTINCT p.rowid AS product_id, es.rowid AS site_id FROM " . MAIN_DB_PREFIX . "product AS p LEFT JOIN " . MAIN_DB_PREFIX . "categorie_product AS cp ON cp.fk_product = p.rowid LEFT JOIN " . MAIN_DB_PREFIX . "ecommerce_site AS es ON es.fk_cat_product = cp.fk_categorie WHERE es.rowid IS NOT NULL) AS pl ON pl.product_id = " . MAIN_DB_PREFIX . "ecommerce_product.fk_product AND pl.site_id = " . MAIN_DB_PREFIX . "ecommerce_product.fk_site WHERE pl.product_id IS NULL";
$sql = "DELETE " . MAIN_DB_PREFIX . "ecommerce_product FROM " . MAIN_DB_PREFIX . "ecommerce_product LEFT JOIN (SELECT DISTINCT p.rowid AS product_id, es.rowid AS site_id FROM " . MAIN_DB_PREFIX . "product AS p LEFT JOIN " . MAIN_DB_PREFIX . "categorie_product AS cp ON cp.fk_product = p.rowid AND cp.fk_categorie IN (" . implode(',', $cats_id) . "), " . MAIN_DB_PREFIX . "categorie AS c LEFT JOIN " . MAIN_DB_PREFIX . "ecommerce_site AS es ON es.fk_cat_product = c.rowid WHERE es.rowid IS NOT NULL) AS pl ON pl.product_id = " . MAIN_DB_PREFIX . "ecommerce_product.fk_product AND pl.site_id = " . MAIN_DB_PREFIX . "ecommerce_product.fk_site WHERE pl.product_id IS NULL";
$this->db->query($sql);
$sql = "DELETE " . MAIN_DB_PREFIX . "ecommerce_societe FROM " . MAIN_DB_PREFIX . "ecommerce_societe LEFT JOIN (SELECT DISTINCT s.rowid AS company_id, es.rowid AS site_id FROM " . MAIN_DB_PREFIX . "societe AS s LEFT JOIN " . MAIN_DB_PREFIX . "categorie_societe AS cs ON cs.fk_soc = s.rowid LEFT JOIN " . MAIN_DB_PREFIX . "ecommerce_site AS es ON es.fk_cat_societe = cs.fk_categorie WHERE es.rowid IS NOT NULL) AS sl ON sl.company_id = " . MAIN_DB_PREFIX . "ecommerce_societe.fk_societe AND sl.site_id = " . MAIN_DB_PREFIX . "ecommerce_societe.fk_site WHERE sl.company_id IS NULL";
$this->db->query($sql);
Expand Down