Works on migration of relationship tables

This commit is contained in:
Regis Houssin 2009-12-11 16:31:42 +00:00
parent e28c4b03a9
commit b3ae671237
12 changed files with 227 additions and 193 deletions

View File

@ -836,14 +836,33 @@ class CommonObject
/**
* Add objects linked in llx_element_element.
*/
function add_object_linked($sourceid,$sourcetype,$targetid,$targettype)
function add_object_linked()
{
$this->db->begin();
$sql = "INSERT INTO ".MAIN_DB_PREFIX."element_element (";
$sql.= "fk_source";
$sql.= ", sourcetype";
$sql.= ", fk_target";
$sql.= ", targettype";
$sql.= ") VALUES (";
$sql.= $this->origin_id;
$sql.= ", '".$this->origin."'";
$sql.= ", ".$this->id;
$sql.= ", '".$this->element."'";
$sql.= ")";
if ($this->db->query($sql))
{
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->lasterror()." - sql=$sql";
$this->db->rollback();
return 0;
}
}
/**
@ -856,8 +875,8 @@ class CommonObject
// Links beetween objects are stored in this table
$sql = 'SELECT fk_source, sourcetype, fk_target, targettype';
$sql.= ' FROM '.MAIN_DB_PREFIX.'element_element';
$sql.= " WHERE (fk_source = '".$this->id."' AND sourcetype = 'invoice')";
$sql.= " OR (fk_target = '".$this->id."' AND targettype = 'invoice')";
$sql.= " WHERE (fk_source = '".$this->id."' AND sourcetype = '".$this->origin."')";
$sql.= " OR (fk_target = '".$this->id."' AND targettype = '".$this->element."')";
dol_syslog("CommonObject::load_object_linked sql=".$sql);
$resql = $this->db->query($sql);

View File

@ -38,7 +38,7 @@ class Expedition extends CommonObject
{
var $db;
var $error;
var $element="expedition";
var $element="shipping";
var $fk_element="fk_expedition";
var $table_element="expedition";
@ -147,14 +147,14 @@ class Expedition extends CommonObject
if (! $error && $this->id && $this->origin_id)
{
$ret = $this->add_object_linked();
if (!$ret)
{
$error++;
}
if ($conf->commande->enabled)
{
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'co_exp (fk_expedition, fk_commande) VALUES ('.$this->id.','.$this->origin_id.')';
if (!$this->db->query($sql))
{
$error++;
}
$sql = "UPDATE ".MAIN_DB_PREFIX."commande SET fk_statut = 2 WHERE rowid=".$this->origin_id;
if (! $this->db->query($sql))
{
@ -163,12 +163,6 @@ class Expedition extends CommonObject
}
else
{
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'pr_exp (fk_expedition, fk_propal) VALUES ('.$this->id.','.$this->origin_id.')';
if (!$this->db->query($sql))
{
$error++;
}
// TODO definir un statut
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_statut = 9 WHERE rowid=".$this->origin_id;
if (! $this->db->query($sql))
@ -240,27 +234,20 @@ class Expedition extends CommonObject
$sql.= ", e.weight, e.weight_units, e.size, e.size_units, e.width, e.height";
$sql.= ", ".$this->db->pdate("e.date_expedition")." as date_expedition, e.model_pdf, e.fk_adresse_livraison";
$sql.= ", e.fk_expedition_methode, e.tracking_number";
if ($conf->commande->enabled)
{
$sql.= ", ce.fk_commande as origin_id";
$sql.= ", c.ref_client";
}
else
{
$sql.= ", pe.fk_propal as origin_id";
$sql.= ", p.ref_client";
}
$sql.= ", el.fk_source as origin_id";
$sql.= ", ori.ref_client";
if ($conf->livraison_bon->enabled) $sql.=", l.rowid as livraison_id";
$sql.= " FROM ".MAIN_DB_PREFIX."expedition as e";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."element_element as el ON e.rowid = el.fk_target";
if ($conf->commande->enabled)
{
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."co_exp as ce ON e.rowid = ce.fk_expedition";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."commande as c ON ce.fk_commande = c.rowid";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."commande as ori ON el.fk_source = ori.rowid";
$sql.= " AND el.sourcetype = 'commande'";
}
else
{
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."pr_exp as pe ON e.rowid = pe.fk_expedition";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."propal as p ON pe.fk_propal = p.rowid";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."propal as ori ON el.fk_source = ori.rowid";
$sql.= " AND el.sourcetype = 'propal'";
}
if ($conf->livraison_bon->enabled) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."livraison as l ON e.rowid = l.fk_expedition";
$sql.= " WHERE e.rowid = ".$id;
@ -573,18 +560,25 @@ class Expedition extends CommonObject
{
$this->db->begin();
$sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet WHERE fk_expedition = ".$this->id;
$sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet";
$sql.= " WHERE fk_expedition = ".$this->id;
if ( $this->db->query($sql) )
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."co_exp WHERE rowid = ".$this->id;
$sql = "DELETE FROM ".MAIN_DB_PREFIX."element_element";
$sql.= " WHERE fk_target = ".$this->id;
$sql.= " AND targettype = '".$this->element."'";
if ( $this->db->query($sql) )
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."expedition WHERE rowid = ".$this->id;
$sql = "DELETE FROM ".MAIN_DB_PREFIX."expedition";
$sql.= " WHERE rowid = ".$this->id;
if ( $this->db->query($sql) )
{
$this->db->commit();
// On efface le r<EFBFBD>pertoire de pdf provisoire
// On efface le repertoire de pdf provisoire
$expref = dol_sanitizeFileName($this->ref);
if ($conf->expedition->dir_output)
{
@ -612,18 +606,21 @@ class Expedition extends CommonObject
}
else
{
$this->error=$this->db->lasterror()." - sql=$sql";
$this->db->rollback();
return -3;
}
}
else
{
$this->error=$this->db->lasterror()." - sql=$sql";
$this->db->rollback();
return -2;
}
}
else
{
$this->error=$this->db->lasterror()." - sql=$sql";
$this->db->rollback();
return -1;
}

View File

@ -154,9 +154,16 @@ if ($_REQUEST["action"] == 'confirm_delete' && $_REQUEST["confirm"] == 'yes')
{
$expedition = new Expedition($db);
$expedition->fetch($_GET["id"]);
$expedition->delete();
Header("Location: liste.php");
exit;
$result = $expedition->delete();
if ($result > 0)
{
Header("Location: liste.php");
exit;
}
else
{
$mesg = $expedition->error;
}
}
}
@ -535,6 +542,11 @@ else
if ($expedition->id > 0)
{
if ($mesg)
{
print '<div class="error">'.$mesg.'</div>';
}
$typeobject = $expedition->origin;
$origin = $expedition->origin;
$expedition->fetch_object();

View File

@ -60,8 +60,8 @@ $sql = "SELECT e.rowid, e.ref";
$sql.= ", s.nom, s.rowid as socid";
$sql.= ", c.ref as commande_ref, c.rowid as commande_id";
$sql.= " FROM ".MAIN_DB_PREFIX."expedition as e";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."co_exp as ce ON e.rowid = ce.fk_expedition";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."commande as c ON ce.fk_commande = c.rowid";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."element_element as el ON e.rowid = el.fk_target";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."commande as c ON el.fk_source = c.rowid";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = e.fk_soc";
if (!$user->rights->societe->client->voir && !$socid)
{
@ -194,8 +194,8 @@ $sql = "SELECT e.rowid, e.ref";
$sql.= ", s.nom, s.rowid as socid";
$sql.= ", c.ref as commande_ref, c.rowid as commande_id";
$sql.= " FROM ".MAIN_DB_PREFIX."expedition as e";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."co_exp as ce ON e.rowid = ce.fk_expedition";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."commande as c ON ce.fk_commande = c.rowid";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."element_element as el ON e.rowid = el.fk_target";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."commande as c ON el.fk_source = c.rowid";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = e.fk_soc";
if (!$user->rights->societe->client->voir && !$socid)
{

View File

@ -21,7 +21,7 @@
/**
* \file htdocs/expedition/liste.php
* \ingroup expedition
* \brief Page de la liste des exp<EFBFBD>ditions/livraisons
* \brief Page de la liste des expeditions/livraisons
* \version $Id$
*/
@ -53,17 +53,18 @@ llxHeader('',$langs->trans('ListOfSendings'),$helpurl);
$sql = "SELECT e.rowid, e.ref,".$db->pdate("e.date_expedition")." as date_expedition, e.fk_statut";
$sql.= ", s.nom as socname, s.rowid as socid";
$sql.= ", ori.ref as oriref, ori.rowid as oriid";
$sql.= ", ori.ref as origin_ref, ori.rowid as origin_id";
$sql.= " FROM ".MAIN_DB_PREFIX."expedition as e";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."element_element as el ON e.rowid = el.fk_target";
if ($conf->commande->enabled)
{
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."co_exp as ce ON e.rowid = ce.fk_expedition";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."commande as ori ON ce.fk_commande = ori.rowid";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."commande as ori ON el.fk_source = ori.rowid";
$sql.= " AND el.sourcetype = 'commande'";
}
else
{
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."pr_exp as pe ON e.rowid = pe.fk_expedition";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."propal as ori ON pe.fk_commande = ori.rowid";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."propal as ori ON el.fk_source = ori.rowid";
$sql.= " AND el.sourcetype = 'propal'";
}
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = e.fk_soc";
$sql.= " WHERE s.entity = ".$conf->entity;
@ -123,11 +124,11 @@ if ($resql)
print '<td><a href="'.DOL_URL_ROOT.'/comm/fiche.php?socid='.$objp->socid.'">'.$objp->socname.'</a></td>';
if ($conf->commande->enabled)
{
print '<td><a href="'.DOL_URL_ROOT.'/expedition/commande.php?id='.$objp->oriid.'">'.$objp->oriref.'</a></td>';
print '<td><a href="'.DOL_URL_ROOT.'/expedition/commande.php?id='.$objp->origin_id.'">'.$objp->origin_ref.'</a></td>';
}
else
{
print '<td><a href="'.DOL_URL_ROOT.'/expedition/propal.php?propalid='.$objp->oriid.'">'.$objp->oriref.'</a></td>';
print '<td><a href="'.DOL_URL_ROOT.'/expedition/propal.php?propalid='.$objp->origin_id.'">'.$objp->origin_ref.'</a></td>';
}
$now = time();

View File

@ -1,24 +0,0 @@
-- ===================================================================
-- Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
-- Copyright (C) 2008 Regis Houssin <regis@dolibarr.fr>
-- Copyright (C) 2009 Laurent Destailleur <eldy@users.sourceforge.net>
--
-- 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.
--
-- $Id$
-- ===================================================================
ALTER TABLE llx_co_exp ADD INDEX idx_co_exp_fk_commande (fk_commande);
ALTER TABLE llx_co_exp ADD INDEX idx_co_exp_fk_expedition (fk_expedition);

View File

@ -1,27 +0,0 @@
-- ===================================================================
-- Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
-- Copyright (C) 2008 Regis Houssin <regis@dolibarr.fr>
--
-- 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.
--
-- $Id$
-- ===================================================================
create table llx_co_exp
(
rowid integer AUTO_INCREMENT PRIMARY KEY,
fk_commande integer NOT NULL,
fk_expedition integer NOT NULL
)type=innodb;

View File

@ -1,24 +0,0 @@
-- ===================================================================
-- Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
-- Copyright (C) 2008 Regis Houssin <regis@dolibarr.fr>
-- Copyright (C) 2009 Laurent Destailleur <eldy@users.sourceforge.net>
--
-- 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.
--
-- $Id$
-- ===================================================================
ALTER TABLE llx_pr_exp ADD INDEX idx_pr_exp_fk_propal (fk_propal);
ALTER TABLE llx_pr_exp ADD INDEX idx_pr_exp_fk_expedition (fk_expedition);

View File

@ -1,27 +0,0 @@
-- ===================================================================
-- Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
-- Copyright (C) 2008 Regis Houssin <regis@dolibarr.fr>
--
-- 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.
--
-- $Id$
-- ===================================================================
create table llx_pr_exp
(
rowid integer AUTO_INCREMENT PRIMARY KEY,
fk_propal integer NOT NULL,
fk_expedition integer NOT NULL
)type=innodb;

View File

@ -226,10 +226,15 @@ if (isset($_POST['action']) && preg_match('/upgrade/i',$_POST["action"]))
migrate_restore_missing_links($db,$langs,$conf);
migrate_directories($db,$langs,$conf,'/compta','/banque');
migrate_directories($db,$langs,$conf,'/societe','/mycompany');
// Script pour V2.7 -> V2.8
migrate_relationship_tables($db,$langs,$conf,'co_exp','fk_commande','commande','fk_expedition','shipping');
migrate_relationship_tables($db,$langs,$conf,'pr_exp','fk_propal','propal','fk_expedition','shipping');
// On commit dans tous les cas.
@ -2210,60 +2215,66 @@ function migrate_commande_deliveryaddress($db,$langs,$conf)
print '<b>'.$langs->trans('MigrationDeliveryAddress')."</b><br>\n";
$error = 0;
$db->begin();
$sql = "SELECT c.fk_adresse_livraison, ce.fk_expedition";
$sql.= " FROM ".MAIN_DB_PREFIX."commande as c";
$sql.= ", ".MAIN_DB_PREFIX."co_exp as ce";
$sql.= " WHERE c.rowid = ce.fk_commande";
$sql.= " AND c.fk_adresse_livraison IS NOT NULL AND c.fk_adresse_livraison != 0";
$resql = $db->query($sql);
if ($resql)
if ($db->DDLInfoTable(MAIN_DB_PREFIX."co_exp"))
{
$i = 0;
$num = $db->num_rows($resql);
if ($num)
$db->begin();
$sql = "SELECT c.fk_adresse_livraison, ce.fk_expedition";
$sql.= " FROM ".MAIN_DB_PREFIX."commande as c";
$sql.= ", ".MAIN_DB_PREFIX."co_exp as ce";
$sql.= " WHERE c.rowid = ce.fk_commande";
$sql.= " AND c.fk_adresse_livraison IS NOT NULL AND c.fk_adresse_livraison != 0";
$resql = $db->query($sql);
if ($resql)
{
while ($i < $num)
$i = 0;
$num = $db->num_rows($resql);
if ($num)
{
$obj = $db->fetch_object($resql);
$sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET";
$sql.= " fk_adresse_livraison = '".$obj->fk_adresse_livraison."'";
$sql.= " WHERE rowid=".$obj->fk_expedition;
$resql2=$db->query($sql);
if ($resql2)
while ($i < $num)
{
$obj = $db->fetch_object($resql);
$sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET";
$sql.= " fk_adresse_livraison = '".$obj->fk_adresse_livraison."'";
$sql.= " WHERE rowid=".$obj->fk_expedition;
$resql2=$db->query($sql);
if (!$resql2)
{
$error++;
dol_print_error($db);
}
print ". ";
$i++;
}
else
{
$error++;
dol_print_error($db);
}
print ". ";
$i++;
}
}
if ($error == 0)
{
$db->commit();
else
{
print $langs->trans('AlreadyDone')."<br>\n";
}
if ($error == 0)
{
$db->commit();
}
else
{
$db->rollback();
}
}
else
{
dol_print_error($db);
$db->rollback();
}
}
else
{
dol_print_error($db);
$db->rollback();
print $langs->trans('AlreadyDone')."<br>\n";
}
print '</td></tr>';
@ -2401,7 +2412,10 @@ function migrate_restore_missing_links($db,$langs,$conf)
}
}
else print $langs->trans('AlreadyDone')."<br>\n";
else
{
print $langs->trans('AlreadyDone')."<br>\n";
}
if ($error == 0)
{
@ -2421,6 +2435,97 @@ function migrate_restore_missing_links($db,$langs,$conf)
print '</td></tr>';
}
/*
* Migration des tables de relation
*/
function migrate_relationship_tables($db,$langs,$conf,$table,$fk_source,$sourcetype,$fk_target,$targettype)
{
print '<tr><td colspan="4">';
print '<br>';
print '<b>'.$langs->trans('MigrationRelationshipTables',MAIN_DB_PREFIX.$table)."</b><br>\n";
$error = 0;
if ($db->DDLInfoTable(MAIN_DB_PREFIX.$table))
{
dolibarr_install_syslog("upgrade2::migrate_relationship_tables table = ".MAIN_DB_PREFIX.$table);
$db->begin();
$sqlSelect = "SELECT ".$fk_source.", ".$fk_target;
$sqlSelect.= " FROM ".MAIN_DB_PREFIX.$table;
$resql = $db->query($sqlSelect);
if ($resql)
{
$i = 0;
$num = $db->num_rows($resql);
if ($num)
{
while ($i < $num)
{
$obj = $db->fetch_object($resql);
$sqlInsert = "INSERT INTO ".MAIN_DB_PREFIX."element_element (";
$sqlInsert.= "fk_source";
$sqlInsert.= ", sourcetype";
$sqlInsert.= ", fk_target";
$sqlInsert.= ", targettype";
$sqlInsert.= ") VALUES (";
$sqlInsert.= $obj->$fk_source;
$sqlInsert.= ", '".$sourcetype."'";
$sqlInsert.= ", ".$obj->$fk_target;
$sqlInsert.= ", '".$targettype."'";
$sqlInsert.= ")";
$result=$db->query($sqlInsert);
if (! $result)
{
$error++;
dol_print_error($db);
}
print ". ";
$i++;
}
}
else
{
print $langs->trans('AlreadyDone')."<br>\n";
}
if ($error == 0)
{
$sqlDrop = "DROP TABLE ".MAIN_DB_PREFIX.$table;
if ($db->query($sqlDrop))
{
$db->commit();
}
else
{
$db->rollback();
}
}
else
{
$db->rollback();
}
}
else
{
dol_print_error($db);
$db->rollback();
}
}
else
{
print $langs->trans('AlreadyDone')."<br>\n";
}
print '</td></tr>';
}
/*
* Migration directory

View File

@ -147,6 +147,7 @@ MigrationInvoice=Data migration for customers' invoices
MigrationContract=Data migration for contracts
MigrationSuccessfullUpdate=Upgrade successful
MigrationUpdateFailed=Failed upgrade process
MigrationRelationshipTables=Data migration for relationship tables (%s)
# Payments Update
MigrationPaymentsUpdate=Payment data correction

View File

@ -149,6 +149,7 @@ MigrationInvoice=Migration de données sur les factures clients
MigrationContract=Migration de données sur les contrats
MigrationSuccessfullUpdate=Mise à jour réussie
MigrationUpdateFailed=La mise à jour à échouée
MigrationRelationshipTables=Migration des tables de liaison (%s)
# Payments Update
MigrationPaymentsUpdate=Mise a jour des paiments (lien n-n paiements-factures)