Files
le-matelot/client/plugins/combidecli/CombidecliAdmin.class.php
2020-01-27 08:56:08 +01:00

384 lines
9.8 KiB
PHP

<?php
/*************************************************************************************/
/* */
/* Thelia - Plugin de combinaison des déclinaisons */
/* */
/* Copyright (c) Franck Allimant, 2011 */
/* email : franck.allimant@cqfdev.fr */
/* web : http://www.cqfdev.fr */
/* idée originale: Jean-Baptiste Billot (ottoroots@gmail.com) */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program; if not, write to the Free Software */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/* */
/*************************************************************************************/
include_once(realpath(dirname(__FILE__)) . "/Combidecli.class.php");
class CombidecliAdmin extends Combidecli
{
public $combinaisons = array();
public $declinaisondescs = array();
public $warning_no_decli = false;
public $refproduit;
public function CombidecliAdmin()
{
parent::Combidecli();
$this->refproduit = $_REQUEST['ref'];
$this->lang = intval($_REQUEST['lang']) > 0 ? intval($_REQUEST['lang']) : 1;
}
public function prerequis()
{
$ok = true;
$result = $this->_query("SELECT version();");
$mversion = mysql_result($result, 0, 0);
$tversion = new Variable();
$tversion->charger('version');
if (intval($tversion->valeur) < 143 || intval($mversion[0]) < 5)
{
$ok = "Le plugin combidecli nécessite MySQL 5 ou supérieur et Thelia 1.4.3 ou supérieur. Versions détectées: MySQL: $mversion, Thelia: $tversion->valeur.";
}
return $ok;
}
public function combidecli_actives()
{
return 0 != intval(mysql_result(
$this->_query("
select
count(*)
from
$this->table cd
left join
produit p on p.id = cd.produit
where
p.ref = '$this->refproduit'
"), 0));
}
private function creer_combinaisons($produit, $declinaisons, $index, $ref, &$reflist)
{
$n = count($declinaisons);
if ($n > 0)
{
if ($index >= $n)
{
// Créer une nouvelle combinaison si elle n'existe pas
$ref = ltrim($ref, ',');
$reflist[] = "'$ref'";
$cd = new Combidecli();
if (! $cd->charger_ref($ref, $produit->id))
{
$cd->produit = $produit->id;
$cd->ref = $ref;
$cd->stock = 0;
$cd->surplus = 0;
$cd->actif = 1;
$cd->add();
}
return;
}
foreach($declinaisons[$index] as $declidisp)
{
$this->creer_combinaisons($produit, $declinaisons, 1 + $index, $ref . ',' . $declidisp, $reflist);
}
}
}
public function combiner($ref = false)
{
$produit = new Produit();
if (! $ref) $ref = $this->refproduit;
if ($produit->charger($ref))
{
if (is_array($_REQUEST['composants']))
{
$listedeclis = implode(',', $_REQUEST['composants']);
// Ajouter les declinaisons a prendre en compte.
CombidecliProduit::supprimer_par_produit($produit->id);
$cdp = new CombidecliProduit();
foreach($_REQUEST['composants'] as $iddecli)
{
$cdp->produit = $produit->id;
$cdp->declinaison = $iddecli;
$cdp->add();
}
}
else
{
// Dans le cas de l'Activation / desactivation de declidisp : on n'a pas les infos 'composant'.
// On le recupère dans la table CombidecliProduit
$liste = CombidecliProduit::lister_par_produit($produit->id);
$listedeclis = '';
foreach($liste as $item) $listedeclis .= $item->declinaison . ',';
$listedeclis = rtrim($listedeclis, ',');
}
if ($listedeclis == '') return;
$rd = new Rubdeclinaison();
$d = new Declinaison();
$result = $this->_query("
select
d.id
from
$rd->table rd
left join
$d->table d on rd.declinaison = d.id
where
rd.rubrique = $produit->rubrique
and
d.id in ($listedeclis)
order by
d.id
");
$dd = new Declidisp();
$ddd = new Declidispdesc();
$edp = new Exdecprod();
$declinaisons = array();
$idx = -1;
$curr_decli = false;
while ($result && $decli = mysql_fetch_object($result))
{
$order = "dd.declinaison, ddd.classement";
$query = "
select
dd.id as id_declidisp, dd.declinaison as id_declinaison
from
$dd->table dd
left join
$ddd->table ddd on ddd.declidisp = dd.id and lang = 1
where
dd.declinaison = $decli->id
and
dd.id in (select declidisp from $edp->table where produit=$produit->id)
order by
$order
";
$resdisp = $this->_query($query);
while($resdisp && $disp = mysql_fetch_object($resdisp))
{
if ($curr_decli != $disp->id_declinaison)
{
$idx++;
$curr_decli = $disp->id_declinaison;
}
$declinaisons[$idx][] = $disp->id_declidisp;
}
}
$reflist = array();
$this->creer_combinaisons($produit, $declinaisons, 0, '', $reflist);
// Supprimer les déclinaisons éventuellement obsolètes
if (count($reflist) == 0)
{
$this->warning_no_decli = true;
$where = "";
}
else
{
$where = "and ref not in (".implode(',', $reflist).")";
}
$this->_query("DELETE from $this->table where produit=$produit->id $where");
}
}
// Mettre a jour tout le catalogue après une modif des déclinaisons
// (ajout / suppression de declidip, ajout de déclinaison, etc.)
public function combiner_catalogue($rubrique = false)
{
$produit = new Produit();
$query = "SELECT id, ref from $produit->table";
if ($rubrique) $query .= " where rubrique=$rubrique";
$result = $this->_query($query);
while ($result && $row = mysql_fetch_object($result))
{
// S'il y a des combinaisons pour le produit, les mettre à jour
$check = $this->_query("select id from $this->table where produit=$row->id");
if (mysql_numrows($check) > 0) $this->combiner($row->ref);
}
}
public function supprimer()
{
$produit = new Produit();
if ($produit->charger($this->refproduit))
{
$this->_query("DELETE FROM ".self::TABLE." WHERE produit=$produit->id");
CombidecliProduit::supprimer_par_produit($produit->id);
}
}
public function maj()
{
if (isset($_REQUEST['dc_id']))
{
foreach($_REQUEST['dc_id'] as $idcombidecli)
{
$combidecli = new Combidecli();
if ($combidecli->charger($idcombidecli))
{
$combidecli->stock = floatval($_REQUEST["dc_stock_$idcombidecli"]);
$combidecli->surplus = floatval($_REQUEST["dc_surplus_$idcombidecli"]);
$combidecli->actif = isset($_REQUEST["dc_actif_$idcombidecli"]) && intval($_REQUEST["dc_actif_$idcombidecli"]) != 0 ? 1 : 0;
$combidecli->maj();
}
}
$this->modprod($this->refproduit);
}
}
function label_combinaison($ref_combinaison)
{
$SEP_LABEL = ' / ';
$inclause = $ref_combinaison;
if ($inclause == '') return "$ref_combinaison: ERREUR !";
$d = new Declinaison();
$dd = new Declidisp();
$ddd = new Declidispdesc();
$query = "
select
titre
from
$ddd->table ddd
left join
$dd->table dd on dd.id = ddd.declidisp
left join
$d->table d on d.id = dd.declinaison
where
declidisp in ($inclause)
and
lang = $this->lang
order by
d.classement
";
$reslabel = $this->_query($query);
$label = '';
while ($reslabel && $rowlabel = mysql_fetch_object($reslabel))
{
$label .= $rowlabel->titre . $SEP_LABEL;
}
return rtrim($label, $SEP_LABEL);
}
function prepare_page()
{
$produit = new Produit();
if ($produit->charger($this->refproduit))
{
// Calculer le label de chaque combinaison
$result = $this->_query("SELECT * from ".self::TABLE." WHERE produit=$produit->id order by id");
while ($result && $combidecli = mysql_fetch_object($result, 'Combidecli'))
{
$combidecli->label = $this->label_combinaison($combidecli->ref);
$this->combinaisons[] = $combidecli;
}
// Liste des declinaisons participantes
$this->composants = array();
$liste = CombidecliProduit::lister_par_produit($produit->id);
foreach($liste as $item) $this->composants[] = $item->declinaison;
// La liste des declinaisons a combiner
$rd = new Rubdeclinaison();
$d = new Declinaison();
$dd = new Declinaisondesc();
$result = $this->_query("
select
dd.*
from
$rd->table rd
left join
$d->table d on rd.declinaison = d.id
left join
$dd->table dd on dd.declinaison = d.id and lang=$this->lang
where
rd.rubrique = $produit->rubrique
order by
d.classement
");
while ($result && $row = mysql_fetch_object($result, 'Declinaisondesc'))
{
$this->declinaisondescs[] = $row;
}
$this->stock_produit = $produit->stock;
}
}
}
?>