dolibarr  x.y.z
upgrade2.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
3  * Copyright (C) 2005-2018 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2011 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
6  * Copyright (C) 2015-2016 RaphaĆ«l Doursenaud <rdoursenaud@gpcsolutions.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * Upgrade2 scripts can be ran from command line with syntax:
22  *
23  * cd htdocs/install
24  * php upgrade.php 3.4.0 3.5.0 [dirmodule|ignoredbversion]
25  * php upgrade2.php 3.4.0 3.5.0 [MAIN_MODULE_NAME1_TO_ENABLE,MAIN_MODULE_NAME2_TO_ENABLE]
26  *
27  * And for final step:
28  * php step5.php 3.4.0 3.5.0
29  *
30  * Return code is 0 if OK, >0 if error
31  *
32  * Note: To just enable a module from command line, use this syntax:
33  * php upgrade2.php 0.0.0 0.0.0 [MAIN_MODULE_NAME1_TO_ENABLE,MAIN_MODULE_NAME2_TO_ENABLE]
34  */
35 
41 include_once 'inc.php';
42 if (!file_exists($conffile)) {
43  print 'Error: Dolibarr config file was not found. This may means that Dolibarr is not installed yet. Please call the page "/install/index.php" instead of "/install/upgrade.php").';
44 }
45 require_once $conffile;
46 require_once $dolibarr_main_document_root.'/compta/facture/class/facture.class.php';
47 require_once $dolibarr_main_document_root.'/comm/propal/class/propal.class.php';
48 require_once $dolibarr_main_document_root.'/contrat/class/contrat.class.php';
49 require_once $dolibarr_main_document_root.'/commande/class/commande.class.php';
50 require_once $dolibarr_main_document_root.'/fourn/class/fournisseur.commande.class.php';
51 require_once $dolibarr_main_document_root.'/core/lib/price.lib.php';
52 require_once $dolibarr_main_document_root.'/core/class/menubase.class.php';
53 require_once $dolibarr_main_document_root.'/core/lib/files.lib.php';
54 
55 global $langs;
56 
57 $grant_query = '';
58 $step = 2;
59 $error = 0;
60 
61 
62 // Cette page peut etre longue. On augmente le delai autorise.
63 // Ne fonctionne que si on est pas en safe_mode.
64 $err = error_reporting();
65 error_reporting(0);
66 if (!empty($conf->global->MAIN_OVERRIDE_TIME_LIMIT)) {
67  @set_time_limit((int) $conf->global->MAIN_OVERRIDE_TIME_LIMIT);
68 } else {
69  @set_time_limit(600);
70 }
71 error_reporting($err);
72 
73 $setuplang = GETPOST("selectlang", 'aZ09', 3) ?GETPOST("selectlang", 'aZ09', 3) : 'auto';
74 $langs->setDefaultLang($setuplang);
75 $versionfrom = GETPOST("versionfrom", 'alpha', 3) ?GETPOST("versionfrom", 'alpha', 3) : (empty($argv[1]) ? '' : $argv[1]);
76 $versionto = GETPOST("versionto", 'alpha', 3) ?GETPOST("versionto", 'alpha', 3) : (empty($argv[2]) ? '' : $argv[2]);
77 $enablemodules = GETPOST("enablemodules", 'alpha', 3) ?GETPOST("enablemodules", 'alpha', 3) : (empty($argv[3]) ? '' : $argv[3]);
78 
79 $langs->loadLangs(array("admin", "install", "bills", "suppliers"));
80 
81 if ($dolibarr_main_db_type == 'mysqli') {
82  $choix = 1;
83 }
84 if ($dolibarr_main_db_type == 'pgsql') {
85  $choix = 2;
86 }
87 if ($dolibarr_main_db_type == 'mssql') {
88  $choix = 3;
89 }
90 
91 
92 dolibarr_install_syslog("--- upgrade2: entering upgrade2.php page ".$versionfrom." ".$versionto." ".$enablemodules);
93 if (!is_object($conf)) {
94  dolibarr_install_syslog("upgrade2: conf file not initialized", LOG_ERR);
95 }
96 
97 
98 
99 /*
100  * View
101  */
102 
103 if ((!$versionfrom || preg_match('/version/', $versionfrom)) && (!$versionto || preg_match('/version/', $versionto))) {
104  print 'Error: Parameter versionfrom or versionto missing or having a bad format.'."\n";
105  print 'Upgrade must be ran from command line with parameters or called from page install/index.php (like a first install)'."\n";
106  // Test if batch mode
107  $sapi_type = php_sapi_name();
108  $script_file = basename(__FILE__);
109  $path = __DIR__.'/';
110  if (substr($sapi_type, 0, 3) == 'cli') {
111  print 'Syntax from command line: '.$script_file." x.y.z a.b.c [MAIN_MODULE_NAME1_TO_ENABLE,MAIN_MODULE_NAME2_TO_ENABLE...]\n";
112  }
113  exit;
114 }
115 
116 pHeader('', 'step5', GETPOST('action', 'aZ09') ?GETPOST('action', 'aZ09') : 'upgrade', 'versionfrom='.$versionfrom.'&versionto='.$versionto, '', 'main-inside main-inside-borderbottom');
117 
118 
119 if (!GETPOST('action', 'aZ09') || preg_match('/upgrade/i', GETPOST('action', 'aZ09'))) {
120  print '<h3><img class="valignmiddle inline-block paddingright" src="../theme/common/octicons/build/svg/database.svg" width="20" alt="Database"> ';
121  print '<span class="inline-block">'.$langs->trans('DataMigration').'</span></h3>';
122 
123  print '<table border="0" width="100%">';
124 
125  // If password is encoded, we decode it
126  if ((!empty($dolibarr_main_db_pass) && preg_match('/crypted:/i', $dolibarr_main_db_pass)) || !empty($dolibarr_main_db_encrypted_pass)) {
127  require_once $dolibarr_main_document_root.'/core/lib/security.lib.php';
128  if (!empty($dolibarr_main_db_pass) && preg_match('/crypted:/i', $dolibarr_main_db_pass)) {
129  $dolibarr_main_db_pass = preg_replace('/crypted:/i', '', $dolibarr_main_db_pass);
130  $dolibarr_main_db_pass = dol_decode($dolibarr_main_db_pass);
131  $dolibarr_main_db_encrypted_pass = $dolibarr_main_db_pass; // We need to set this as it is used to know the password was initially crypted
132  } else {
133  $dolibarr_main_db_pass = dol_decode($dolibarr_main_db_encrypted_pass);
134  }
135  }
136 
137  // $conf is already instancied inside inc.php
138  $conf->db->type = $dolibarr_main_db_type;
139  $conf->db->host = $dolibarr_main_db_host;
140  $conf->db->port = $dolibarr_main_db_port;
141  $conf->db->name = $dolibarr_main_db_name;
142  $conf->db->user = $dolibarr_main_db_user;
143  $conf->db->pass = $dolibarr_main_db_pass;
144 
145  $db = getDoliDBInstance($conf->db->type, $conf->db->host, $conf->db->user, $conf->db->pass, $conf->db->name, $conf->db->port);
146 
147  if (!$db->connected) {
148  print '<tr><td colspan="4">'.$langs->trans("ErrorFailedToConnectToDatabase", $conf->db->name).'</td><td class="right">'.$langs->trans('Error').'</td></tr>';
149  dolibarr_install_syslog('upgrade2: failed to connect to database :'.$conf->db->name.' on '.$conf->db->host.' for user '.$conf->db->user, LOG_ERR);
150  $error++;
151  }
152 
153  if (!$error) {
154  if ($db->database_selected) {
155  dolibarr_install_syslog('upgrade2: database connection successful :'.$dolibarr_main_db_name);
156  } else {
157  $error++;
158  }
159  }
160 
161  if (empty($dolibarr_main_db_encryption)) {
162  $dolibarr_main_db_encryption = 0;
163  }
164  $conf->db->dolibarr_main_db_encryption = $dolibarr_main_db_encryption;
165  if (empty($dolibarr_main_db_cryptkey)) {
166  $dolibarr_main_db_cryptkey = '';
167  }
168  $conf->db->dolibarr_main_db_cryptkey = $dolibarr_main_db_cryptkey;
169 
170  // Load global conf
171  $conf->setValues($db);
172 
173 
174  $listofentities = array(1);
175 
176  // Create the global $hookmanager object
177  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
178  $hookmanager = new HookManager($db);
179  $hookmanager->initHooks(array('upgrade2'));
180 
181  $parameters = array('versionfrom' => $versionfrom, 'versionto' => $versionto);
182  $object = new stdClass();
183  $action = "upgrade";
184  $reshook = $hookmanager->executeHooks('doUpgradeBefore', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
185  if ($reshook >= 0 && is_array($hookmanager->resArray)) {
186  // Example: $hookmanager->resArray = array(2, 3, 10);
187  $listofentities = array_unique(array_merge($listofentities, $hookmanager->resArray));
188  }
189 
190 
191  /***************************************************************************************
192  *
193  * Migration of data
194  *
195  ***************************************************************************************/
196 
197  // Force to execute this at begin to avoid the new core code into Dolibarr to be broken.
198  $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN birth date';
199  $db->query($sql, 1);
200  $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN dateemployment date';
201  $db->query($sql, 1);
202  $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN dateemploymentend date';
203  $db->query($sql, 1);
204  $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN default_range integer';
205  $db->query($sql, 1);
206  $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN default_c_exp_tax_cat integer';
207  $db->query($sql, 1);
208  $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'extrafields ADD COLUMN langs varchar(24)';
209  $db->query($sql, 1);
210  $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'extrafields ADD COLUMN fieldcomputed text';
211  $db->query($sql, 1);
212  $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'extrafields ADD COLUMN fielddefault varchar(255)';
213  $db->query($sql, 1);
214  $sql = 'ALTER TABLE '.MAIN_DB_PREFIX."extrafields ADD COLUMN enabled varchar(255) DEFAULT '1'";
215  $db->query($sql, 1);
216  $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'extrafields ADD COLUMN help text';
217  $db->query($sql, 1);
218  $sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user_rights ADD COLUMN entity integer DEFAULT 1 NOT NULL';
219  $db->query($sql, 1);
220 
221 
222  $db->begin();
223 
224  foreach ($listofentities as $entity) {
225  // Set $conf context for entity
226  $conf->setEntityValues($db, $entity);
227  // Reset forced setup after the setValues
228  if (defined('SYSLOG_FILE')) {
229  $conf->global->SYSLOG_FILE = constant('SYSLOG_FILE');
230  }
231  $conf->global->MAIN_ENABLE_LOG_TO_HTML = 1;
232 
233  if (!$error) {
234  if (count($listofentities) > 1) {
235  print '<tr><td colspan="4">*** '.$langs->trans("Entity").' '.$entity.'</td></tr>'."\n";
236  }
237 
238  // Current version is $conf->global->MAIN_VERSION_LAST_UPGRADE
239  // Version to install is DOL_VERSION
240  $dolibarrlastupgradeversionarray = preg_split('/[\.-]/', isset($conf->global->MAIN_VERSION_LAST_UPGRADE) ? $conf->global->MAIN_VERSION_LAST_UPGRADE : (isset($conf->global->MAIN_VERSION_LAST_INSTALL) ? $conf->global->MAIN_VERSION_LAST_INSTALL : ''));
241 
242  // Chaque action de migration doit renvoyer une ligne sur 4 colonnes avec
243  // dans la 1ere colonne, la description de l'action a faire
244  // dans la 4eme colonne, le texte 'OK' si fait ou 'AlreadyDone' si rien n'est fait ou 'Error'
245 
246  $versiontoarray = explode('.', $versionto);
247  $versionranarray = explode('.', DOL_VERSION);
248 
249 
250  $afterversionarray = explode('.', '2.0.0');
251  $beforeversionarray = explode('.', '2.7.9');
252  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
253  // Script pour V2 -> V2.1
254  migrate_paiements($db, $langs, $conf);
255 
256  migrate_contracts_det($db, $langs, $conf);
257 
258  migrate_contracts_date1($db, $langs, $conf);
259 
260  migrate_contracts_date2($db, $langs, $conf);
261 
262  migrate_contracts_date3($db, $langs, $conf);
263 
264  migrate_contracts_open($db, $langs, $conf);
265 
266  migrate_modeles($db, $langs, $conf);
267 
268  migrate_price_propal($db, $langs, $conf);
269 
270  migrate_price_commande($db, $langs, $conf);
271 
272  migrate_price_commande_fournisseur($db, $langs, $conf);
273 
274  migrate_price_contrat($db, $langs, $conf);
275 
276  migrate_paiementfourn_facturefourn($db, $langs, $conf);
277 
278 
279  // Script pour V2.1 -> V2.2
280  migrate_paiements_orphelins_1($db, $langs, $conf);
281 
282  migrate_paiements_orphelins_2($db, $langs, $conf);
283 
284  migrate_links_transfert($db, $langs, $conf);
285 
286 
287  // Script pour V2.2 -> V2.4
288  migrate_commande_expedition($db, $langs, $conf);
289 
290  migrate_commande_livraison($db, $langs, $conf);
291 
292  migrate_detail_livraison($db, $langs, $conf);
293 
294 
295  // Script pour V2.5 -> V2.6
296  migrate_stocks($db, $langs, $conf);
297 
298 
299  // Script pour V2.6 -> V2.7
300  migrate_menus($db, $langs, $conf);
301 
302  migrate_commande_deliveryaddress($db, $langs, $conf);
303 
304  migrate_restore_missing_links($db, $langs, $conf);
305 
306  migrate_rename_directories($db, $langs, $conf, '/compta', '/banque');
307 
308  migrate_rename_directories($db, $langs, $conf, '/societe', '/mycompany');
309  }
310 
311  // Script for 2.8
312  $afterversionarray = explode('.', '2.7.9');
313  $beforeversionarray = explode('.', '2.8.9');
314  //print $versionto.' '.versioncompare($versiontoarray,$afterversionarray).' '.versioncompare($versiontoarray,$beforeversionarray);
315  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
316  migrate_price_facture($db, $langs, $conf); // Code of this function works for 2.8+ because need a field tva_tx
317 
318  migrate_relationship_tables($db, $langs, $conf, 'co_exp', 'fk_commande', 'commande', 'fk_expedition', 'shipping');
319 
320  migrate_relationship_tables($db, $langs, $conf, 'pr_exp', 'fk_propal', 'propal', 'fk_expedition', 'shipping');
321 
322  migrate_relationship_tables($db, $langs, $conf, 'pr_liv', 'fk_propal', 'propal', 'fk_livraison', 'delivery');
323 
324  migrate_relationship_tables($db, $langs, $conf, 'co_liv', 'fk_commande', 'commande', 'fk_livraison', 'delivery');
325 
326  migrate_relationship_tables($db, $langs, $conf, 'co_pr', 'fk_propale', 'propal', 'fk_commande', 'commande');
327 
328  migrate_relationship_tables($db, $langs, $conf, 'fa_pr', 'fk_propal', 'propal', 'fk_facture', 'facture');
329 
330  migrate_relationship_tables($db, $langs, $conf, 'co_fa', 'fk_commande', 'commande', 'fk_facture', 'facture');
331 
332  migrate_project_user_resp($db, $langs, $conf);
333 
334  migrate_project_task_actors($db, $langs, $conf);
335  }
336 
337  // Script for 2.9
338  $afterversionarray = explode('.', '2.8.9');
339  $beforeversionarray = explode('.', '2.9.9');
340  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
341  migrate_project_task_time($db, $langs, $conf);
342 
343  migrate_customerorder_shipping($db, $langs, $conf);
344 
345  migrate_shipping_delivery($db, $langs, $conf);
346 
347  migrate_shipping_delivery2($db, $langs, $conf);
348  }
349 
350  // Script for 3.0
351  $afterversionarray = explode('.', '2.9.9');
352  $beforeversionarray = explode('.', '3.0.9');
353  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
354  // No particular code
355  }
356 
357  // Script for 3.1
358  $afterversionarray = explode('.', '3.0.9');
359  $beforeversionarray = explode('.', '3.1.9');
360  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
361  migrate_rename_directories($db, $langs, $conf, '/rss', '/externalrss');
362 
363  migrate_actioncomm_element($db, $langs, $conf);
364  }
365 
366  // Script for 3.2
367  $afterversionarray = explode('.', '3.1.9');
368  $beforeversionarray = explode('.', '3.2.9');
369  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
370  migrate_price_contrat($db, $langs, $conf);
371 
372  migrate_mode_reglement($db, $langs, $conf);
373 
374  migrate_clean_association($db, $langs, $conf);
375  }
376 
377  // Script for 3.3
378  $afterversionarray = explode('.', '3.2.9');
379  $beforeversionarray = explode('.', '3.3.9');
380  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
381  migrate_categorie_association($db, $langs, $conf);
382  }
383 
384  // Script for 3.4
385  // No specific scripts
386 
387  // Tasks to do always and only into last targeted version
388  $afterversionarray = explode('.', '3.6.9'); // target is after this
389  $beforeversionarray = explode('.', '3.7.9'); // target is before this
390  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
391  migrate_event_assignement($db, $langs, $conf);
392  }
393 
394  // Scripts for 3.9
395  $afterversionarray = explode('.', '3.7.9');
396  $beforeversionarray = explode('.', '3.8.9');
397  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
398  // No particular code
399  }
400 
401  // Scripts for 4.0
402  $afterversionarray = explode('.', '3.9.9');
403  $beforeversionarray = explode('.', '4.0.9');
404  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
405  migrate_rename_directories($db, $langs, $conf, '/fckeditor', '/medias');
406  }
407 
408  // Scripts for 5.0
409  $afterversionarray = explode('.', '4.0.9');
410  $beforeversionarray = explode('.', '5.0.9');
411  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
412  // Migrate to add entity value into llx_societe_remise
413  migrate_remise_entity($db, $langs, $conf);
414 
415  // Migrate to add entity value into llx_societe_remise_except
416  migrate_remise_except_entity($db, $langs, $conf);
417  }
418 
419  // Scripts for 6.0
420  $afterversionarray = explode('.', '5.0.9');
421  $beforeversionarray = explode('.', '6.0.9');
422  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
423  if (isModEnabled('multicompany')) {
424  global $multicompany_transverse_mode;
425 
426  // Only if the transverse mode is not used
427  if (empty($multicompany_transverse_mode)) {
428  // Migrate to add entity value into llx_user_rights
429  migrate_user_rights_entity($db, $langs, $conf);
430 
431  // Migrate to add entity value into llx_usergroup_rights
432  migrate_usergroup_rights_entity($db, $langs, $conf);
433  }
434  }
435  }
436 
437  // Scripts for 7.0
438  $afterversionarray = explode('.', '6.0.9');
439  $beforeversionarray = explode('.', '7.0.9');
440  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
441  // Migrate contact association
442  migrate_event_assignement_contact($db, $langs, $conf);
443 
444  migrate_reset_blocked_log($db, $langs, $conf);
445  }
446 
447  // Scripts for 8.0
448  $afterversionarray = explode('.', '7.0.9');
449  $beforeversionarray = explode('.', '8.0.9');
450  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
451  migrate_rename_directories($db, $langs, $conf, '/contracts', '/contract');
452  }
453 
454  // Scripts for 9.0
455  $afterversionarray = explode('.', '8.0.9');
456  $beforeversionarray = explode('.', '9.0.9');
457  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
458  //migrate_user_photospath();
459  }
460 
461  // Scripts for 11.0
462  $afterversionarray = explode('.', '10.0.9');
463  $beforeversionarray = explode('.', '11.0.9');
464  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
469  }
470 
471  // Scripts for 14.0
472  $afterversionarray = explode('.', '13.0.9');
473  $beforeversionarray = explode('.', '14.0.9');
474  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
477  }
478 
479  // Scripts for 16.0
480  $afterversionarray = explode('.', '15.0.9');
481  $beforeversionarray = explode('.', '16.0.9');
482  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
485  }
486 
487  // Scripts for 17.0
488  $afterversionarray = explode('.', '16.0.9');
489  $beforeversionarray = explode('.', '17.0.9');
490  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
492  }
493 
494  // Scripts for 18.0
495  $afterversionarray = explode('.', '170.9');
496  $beforeversionarray = explode('.', '18.0.9');
497  if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
499  }
500  }
501 
502 
503  // Code executed only if migration is LAST ONE. Must always be done.
504  if (versioncompare($versiontoarray, $versionranarray) >= 0 || versioncompare($versiontoarray, $versionranarray) <= -3) {
505  // Reload modules (this must be always done and only into last targeted version, because code to reload module may need table structure of last version)
506  $listofmodule = array(
507  'MAIN_MODULE_ACCOUNTING'=>'newboxdefonly',
508  'MAIN_MODULE_AGENDA'=>'newboxdefonly',
509  'MAIN_MODULE_BOM'=>'menuonly',
510  'MAIN_MODULE_BANQUE'=>'menuonly',
511  'MAIN_MODULE_BARCODE'=>'newboxdefonly',
512  'MAIN_MODULE_CRON'=>'newboxdefonly',
513  'MAIN_MODULE_COMMANDE'=>'newboxdefonly',
514  'MAIN_MODULE_BLOCKEDLOG'=>'noboxes',
515  'MAIN_MODULE_DEPLACEMENT'=>'newboxdefonly',
516  'MAIN_MODULE_DON'=>'newboxdefonly',
517  'MAIN_MODULE_ECM'=>'newboxdefonly',
518  'MAIN_MODULE_EXTERNALSITE'=>'newboxdefonly',
519  'MAIN_MODULE_EXPENSEREPORT'=>'newboxdefonly',
520  'MAIN_MODULE_FACTURE'=>'newboxdefonly',
521  'MAIN_MODULE_FOURNISSEUR'=>'newboxdefonly',
522  'MAIN_MODULE_HOLIDAY'=>'newboxdefonly',
523  'MAIN_MODULE_MARGIN'=>'menuonly',
524  'MAIN_MODULE_MRP'=>'menuonly',
525  'MAIN_MODULE_OPENSURVEY'=>'newboxdefonly',
526  'MAIN_MODULE_PAYBOX'=>'newboxdefonly',
527  'MAIN_MODULE_PRINTING'=>'newboxdefonly',
528  'MAIN_MODULE_PRODUIT'=>'newboxdefonly',
529  'MAIN_MODULE_RECRUITMENT'=>'menuonly',
530  'MAIN_MODULE_RESOURCE'=>'noboxes',
531  'MAIN_MODULE_SALARIES'=>'newboxdefonly',
532  'MAIN_MODULE_SERVICE'=>'newboxdefonly',
533  'MAIN_MODULE_SYSLOG'=>'newboxdefonly',
534  'MAIN_MODULE_SOCIETE'=>'newboxdefonly',
535  'MAIN_MODULE_STRIPE'=>'menuonly',
536  'MAIN_MODULE_TICKET'=>'newboxdefonly',
537  'MAIN_MODULE_TAKEPOS'=>'newboxdefonly',
538  'MAIN_MODULE_USER'=>'newboxdefonly', //This one must be always done and only into last targeted version)
539  'MAIN_MODULE_VARIANTS'=>'newboxdefonly',
540  'MAIN_MODULE_WEBSITE'=>'newboxdefonly',
541  );
542 
543  $result = migrate_reload_modules($db, $langs, $conf, $listofmodule);
544  if ($result < 0) {
545  $error++;
546  }
547  // Reload menus (this must be always and only into last targeted version)
548  $result = migrate_reload_menu($db, $langs, $conf);
549  if ($result < 0) {
550  $error++;
551  }
552  }
553 
554  // Can force activation of some module during migration with parameter 'enablemodules=MAIN_MODULE_XXX,MAIN_MODULE_YYY,...'
555  // In most cases (online install or upgrade) $enablemodules is empty. Can be forced when ran from command line.
556  if (!$error && $enablemodules) {
557  // Reload modules (this must be always done and only into last targeted version)
558  $listofmodules = array();
559  $enablemodules = preg_replace('/enablemodules=/', '', $enablemodules);
560  $tmplistofmodules = explode(',', $enablemodules);
561  foreach ($tmplistofmodules as $value) {
562  $listofmodules[$value] = 'forceactivate';
563  }
564 
565  $resultreloadmodules = migrate_reload_modules($db, $langs, $conf, $listofmodules, 1);
566  if ($resultreloadmodules < 0) {
567  $error++;
568  }
569  }
570 
571 
572  // Can call a dedicated external upgrade process with hook doUpgradeAfterDB()
573  if (!$error) {
574  $parameters = array('versionfrom' => $versionfrom, 'versionto' => $versionto, 'conf'=>$conf);
575  $object = new stdClass();
576  $action = "upgrade";
577  $reshook = $hookmanager->executeHooks('doUpgradeAfterDB', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
578  if ($hookmanager->resNbOfHooks > 0) {
579  if ($reshook < 0) {
580  print '<tr><td colspan="4">';
581  print '<b>'.$langs->trans('UpgradeExternalModule').'</b>: ';
582  print $hookmanager->error;
583  print "<!-- (".$reshook.") -->";
584  print '</td></tr>';
585  } else {
586  print '<tr class="trforrunsql"><td colspan="4">';
587  print '<b>'.$langs->trans('UpgradeExternalModule').' (DB)</b>: <span class="ok">OK</span>';
588  print "<!-- (".$reshook.") -->";
589  print '</td></tr>';
590  }
591  } else {
592  //if (!empty($conf->modules))
593  if (!empty($conf->modules_parts['hooks'])) { // If there is at least one module with one hook, we show message to say nothing was done
594  print '<tr class="trforrunsql"><td colspan="4">';
595  print '<b>'.$langs->trans('UpgradeExternalModule').'</b>: '.$langs->trans("NodoUpgradeAfterDB");
596  print '</td></tr>';
597  }
598  }
599  }
600  }
601 
602  print '</table>';
603 
604  if (!$error) {
605  // Set constant to ask to remake a new ping to inform about upgrade (if first ping was done and OK)
606  $sql = 'UPDATE '.MAIN_DB_PREFIX."const SET VALUE = 'torefresh' WHERE name = 'MAIN_FIRST_PING_OK_ID'";
607  $db->query($sql, 1);
608  }
609 
610  // We always commit.
611  // Process is designed so we can run it several times whatever is situation.
612  $db->commit();
613 
614 
615  /***************************************************************************************
616  *
617  * Migration of files
618  *
619  ***************************************************************************************/
620 
621  foreach ($listofentities as $entity) {
622  // Set $conf context for entity
623  $conf->setEntityValues($db, $entity);
624  // Reset forced setup after the setValues
625  if (defined('SYSLOG_FILE')) {
626  $conf->global->SYSLOG_FILE = constant('SYSLOG_FILE');
627  }
628  $conf->global->MAIN_ENABLE_LOG_TO_HTML = 1;
629 
630 
631  // Copy directory medias
632  $srcroot = DOL_DOCUMENT_ROOT.'/install/medias';
633  $destroot = DOL_DATA_ROOT.'/medias';
634  dolCopyDir($srcroot, $destroot, 0, 0);
635 
636 
637  // Actions for all versions (no database change but delete some files and directories)
638  migrate_delete_old_files($db, $langs, $conf);
639  migrate_delete_old_dir($db, $langs, $conf);
640  // Actions for all versions (no database change but create some directories)
641  dol_mkdir(DOL_DATA_ROOT.'/bank');
642  // Actions for all versions (no database change but rename some directories)
643  migrate_rename_directories($db, $langs, $conf, '/banque/bordereau', '/bank/checkdeposits');
644 
645 
646  $parameters = array('versionfrom' => $versionfrom, 'versionto' => $versionto, 'conf'=>$conf);
647  $object = new stdClass();
648  $action = "upgrade";
649  $reshook = $hookmanager->executeHooks('doUpgradeAfterFiles', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
650  if ($hookmanager->resNbOfHooks > 0) {
651  if ($reshook < 0) {
652  print '<tr><td colspan="4">';
653  print '<b>'.$langs->trans('UpgradeExternalModule').'</b>: ';
654  print $hookmanager->error;
655  print "<!-- (".$reshook.") -->";
656  print '</td></tr>';
657  } else {
658  print '<tr class="trforrunsql"><td colspan="4">';
659  print '<b>'.$langs->trans('UpgradeExternalModule').' (Files)</b>: <span class="ok">OK</span>';
660  print "<!-- (".$reshook.") -->";
661  print '</td></tr>';
662  }
663  } else {
664  //if (!empty($conf->modules))
665  if (!empty($conf->modules_parts['hooks'])) { // If there is at least one module with one hook, we show message to say nothing was done
666  print '<tr class="trforrunsql"><td colspan="4">';
667  print '<b>'.$langs->trans('UpgradeExternalModule').'</b>: '.$langs->trans("NodoUpgradeAfterFiles");
668  print '</td></tr>';
669  }
670  }
671  }
672 
673  $db->close();
674 
675  $silent = 0;
676  if (!$silent) {
677  print '<table width="100%">';
678  print '<tr><td style="width: 30%">'.$langs->trans("MigrationFinished").'</td>';
679  print '<td class="right">';
680  if ($error == 0) {
681  //print '<span class="ok">'.$langs->trans("OK").'</span> - '; // $error = 0 does not mean there is no error (error are not always trapped)
682  } else {
683  print '<span class="error">'.$langs->trans("Error").'</span> - ';
684  }
685 
686  //if (!empty($conf->use_javascript_ajax)) { // use_javascript_ajax is not defined
687  print '<script type="text/javascript">
688  jQuery(document).ready(function() {
689  function init_trrunsql()
690  {
691  console.log("toggle .trforrunsql");
692  jQuery(".trforrunsql").toggle();
693  }
694  init_trrunsql();
695  jQuery(".trforrunsqlshowhide").click(function() {
696  init_trrunsql();
697  });
698  });
699  </script>';
700  print '<a class="trforrunsqlshowhide" href="#">'.$langs->trans("ShowHideDetails").'</a>';
701  //}
702 
703  print '</td></tr>'."\n";
704  print '</table>';
705  }
706 
707  //print '<div><br>'.$langs->trans("MigrationFinished").'</div>';
708 } else {
709  print '<div class="error">'.$langs->trans('ErrorWrongParameters').'</div>';
710  $error++;
711 }
712 
713 $ret = 0;
714 if ($error && isset($argv[1])) {
715  $ret = 1;
716 }
717 dolibarr_install_syslog("Exit ".$ret);
718 
719 dolibarr_install_syslog("--- upgrade2: end");
720 pFooter($error ? 2 : 0, $setuplang);
721 
722 if ($db->connected) {
723  $db->close();
724 }
725 
726 // Return code if ran from command line
727 if ($ret) {
728  exit($ret);
729 }
730 
731 
732 
741 function migrate_paiements($db, $langs, $conf)
742 {
743  print '<tr><td colspan="4">';
744 
745  print '<br>';
746  print '<b>'.$langs->trans('MigrationPaymentsUpdate')."</b><br>\n";
747 
748  $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiement", "fk_facture");
749  $obj = $db->fetch_object($result);
750  if ($obj) {
751  $sql = "SELECT p.rowid, p.fk_facture, p.amount";
752  $sql .= " FROM ".MAIN_DB_PREFIX."paiement as p";
753  $sql .= " WHERE p.fk_facture > 0";
754 
755  $resql = $db->query($sql);
756 
757  dolibarr_install_syslog("upgrade2::migrate_paiements");
758  if ($resql) {
759  $i = 0;
760  $row = array();
761  $num = $db->num_rows($resql);
762 
763  while ($i < $num) {
764  $obj = $db->fetch_object($resql);
765  $row[$i][0] = $obj->rowid;
766  $row[$i][1] = $obj->fk_facture;
767  $row[$i][2] = $obj->amount;
768  $i++;
769  }
770  } else {
771  dol_print_error($db);
772  }
773 
774  if ($num) {
775  print $langs->trans('MigrationPaymentsNumberToUpdate', $num)."<br>\n";
776  if ($db->begin()) {
777  $res = 0;
778  $num = count($row);
779  for ($i = 0; $i < $num; $i++) {
780  $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount)";
781  $sql .= " VALUES (".((int) $row[$i][1]).",".((int) $row[$i][0]).",".((float) $row[$i][2]).")";
782 
783  $res += $db->query($sql);
784 
785  $sql = "UPDATE ".MAIN_DB_PREFIX."paiement SET fk_facture = 0 WHERE rowid = ".((int) $row[$i][0]);
786 
787  $res += $db->query($sql);
788 
789  print $langs->trans('MigrationProcessPaymentUpdate', $row[$i][0])."<br>\n";
790  }
791  }
792 
793  if ($res == (2 * count($row))) {
794  $db->commit();
795  print $langs->trans('MigrationSuccessfullUpdate')."<br>";
796  } else {
797  $db->rollback();
798  print $langs->trans('MigrationUpdateFailed').'<br>';
799  }
800  } else {
801  print $langs->trans('MigrationPaymentsNothingToUpdate')."<br>\n";
802  }
803  } else {
804  print $langs->trans('MigrationPaymentsNothingToUpdate')."<br>\n";
805  }
806 
807  print '</td></tr>';
808 }
809 
820 function migrate_paiements_orphelins_1($db, $langs, $conf)
821 {
822  print '<tr><td colspan="4">';
823 
824  print '<br>';
825  print '<b>'.$langs->trans('MigrationPaymentsUpdate')."</b><br>\n";
826 
827  $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiement", "fk_facture");
828  $obj = $db->fetch_object($result);
829  if ($obj) {
830  // Tous les enregistrements qui sortent de cette requete devrait avoir un pere dans llx_paiement_facture
831  $sql = "SELECT distinct p.rowid, p.datec, p.amount as pamount, bu.fk_bank, b.amount as bamount,";
832  $sql .= " bu2.url_id as socid";
833  $sql .= " FROM (".MAIN_DB_PREFIX."paiement as p, ".MAIN_DB_PREFIX."bank_url as bu, ".MAIN_DB_PREFIX."bank as b)";
834  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON pf.fk_paiement = p.rowid";
835  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu2 ON (bu.fk_bank=bu2.fk_bank AND bu2.type = 'company')";
836  $sql .= " WHERE pf.rowid IS NULL AND (p.rowid=bu.url_id AND bu.type='payment') AND bu.fk_bank = b.rowid";
837  $sql .= " AND b.rappro = 1";
838  $sql .= " AND (p.fk_facture = 0 OR p.fk_facture IS NULL)";
839 
840  $resql = $db->query($sql);
841 
842  dolibarr_install_syslog("upgrade2::migrate_paiements_orphelins_1");
843  $row = array();
844  if ($resql) {
845  $i = $j = 0;
846  $num = $db->num_rows($resql);
847 
848  while ($i < $num) {
849  $obj = $db->fetch_object($resql);
850  if ($obj->pamount == $obj->bamount && $obj->socid) { // Pour etre sur d'avoir bon cas
851  $row[$j]['paymentid'] = $obj->rowid; // paymentid
852  $row[$j]['pamount'] = $obj->pamount;
853  $row[$j]['fk_bank'] = $obj->fk_bank;
854  $row[$j]['bamount'] = $obj->bamount;
855  $row[$j]['socid'] = $obj->socid;
856  $row[$j]['datec'] = $obj->datec;
857  $j++;
858  }
859  $i++;
860  }
861  } else {
862  dol_print_error($db);
863  }
864 
865  if (count($row)) {
866  print $langs->trans('OrphelinsPaymentsDetectedByMethod', 1).': '.count($row)."<br>\n";
867  $db->begin();
868 
869  $res = 0;
870  $num = count($row);
871  for ($i = 0; $i < $num; $i++) {
872  if ($conf->global->MAIN_FEATURES_LEVEL == 2) {
873  print '* '.$row[$i]['datec'].' paymentid='.$row[$i]['paymentid'].' pamount='.$row[$i]['pamount'].' fk_bank='.$row[$i]['fk_bank'].' bamount='.$row[$i]['bamount'].' socid='.$row[$i]['socid'].'<br>';
874  }
875 
876  // On cherche facture sans lien paiement et du meme montant et pour meme societe.
877  $sql = " SELECT distinct f.rowid from ".MAIN_DB_PREFIX."facture as f";
878  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture";
879  $sql .= " WHERE f.fk_statut in (2,3) AND fk_soc = ".((int) $row[$i]['socid'])." AND total_ttc = ".((float) $row[$i]['pamount']);
880  $sql .= " AND pf.fk_facture IS NULL";
881  $sql .= " ORDER BY f.fk_statut";
882  //print $sql.'<br>';
883  $resql = $db->query($sql);
884  if ($resql) {
885  $num = $db->num_rows($resql);
886  //print 'Nb of invoice found for this amount and company :'.$num.'<br>';
887  if ($num >= 1) {
888  $obj = $db->fetch_object($resql);
889  $facid = $obj->rowid;
890 
891  $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount)";
892  $sql .= " VALUES (".((int) $facid).",".((int) $row[$i]['paymentid']).", ".((float) $row[$i]['pamount']).")";
893 
894  $res += $db->query($sql);
895 
896  print $langs->trans('MigrationProcessPaymentUpdate', 'facid='.$facid.'-paymentid='.$row[$i]['paymentid'].'-amount='.$row[$i]['pamount'])."<br>\n";
897  }
898  } else {
899  print 'ERROR';
900  }
901  }
902 
903  if ($res > 0) {
904  print $langs->trans('MigrationSuccessfullUpdate')."<br>";
905  } else {
906  print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
907  }
908 
909  $db->commit();
910  } else {
911  print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
912  }
913  } else {
914  print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
915  }
916 
917  print '</td></tr>';
918 }
919 
930 function migrate_paiements_orphelins_2($db, $langs, $conf)
931 {
932  print '<tr><td colspan="4">';
933 
934  print '<br>';
935  print '<b>'.$langs->trans('MigrationPaymentsUpdate')."</b><br>\n";
936 
937  $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiement", "fk_facture");
938  $obj = $db->fetch_object($result);
939  if ($obj) {
940  // Tous les enregistrements qui sortent de cette requete devrait avoir un pere dans llx_paiement_facture
941  $sql = "SELECT distinct p.rowid, p.datec, p.amount as pamount, bu.fk_bank, b.amount as bamount,";
942  $sql .= " bu2.url_id as socid";
943  $sql .= " FROM (".MAIN_DB_PREFIX."paiement as p, ".MAIN_DB_PREFIX."bank_url as bu, ".MAIN_DB_PREFIX."bank as b)";
944  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON pf.fk_paiement = p.rowid";
945  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu2 ON (bu.fk_bank = bu2.fk_bank AND bu2.type = 'company')";
946  $sql .= " WHERE pf.rowid IS NULL AND (p.fk_bank = bu.fk_bank AND bu.type = 'payment') AND bu.fk_bank = b.rowid";
947  $sql .= " AND (p.fk_facture = 0 OR p.fk_facture IS NULL)";
948 
949  $resql = $db->query($sql);
950 
951  dolibarr_install_syslog("upgrade2::migrate_paiements_orphelins_2");
952  $row = array();
953  if ($resql) {
954  $i = $j = 0;
955  $num = $db->num_rows($resql);
956 
957  while ($i < $num) {
958  $obj = $db->fetch_object($resql);
959  if ($obj->pamount == $obj->bamount && $obj->socid) { // Pour etre sur d'avoir bon cas
960  $row[$j]['paymentid'] = $obj->rowid; // paymentid
961  $row[$j]['pamount'] = $obj->pamount;
962  $row[$j]['fk_bank'] = $obj->fk_bank;
963  $row[$j]['bamount'] = $obj->bamount;
964  $row[$j]['socid'] = $obj->socid;
965  $row[$j]['datec'] = $obj->datec;
966  $j++;
967  }
968  $i++;
969  }
970  } else {
971  dol_print_error($db);
972  }
973 
974  $nberr = 0;
975 
976  $num = count($row);
977  if ($num) {
978  print $langs->trans('OrphelinsPaymentsDetectedByMethod', 2).': '.count($row)."<br>\n";
979  $db->begin();
980 
981  $res = 0;
982  for ($i = 0; $i < $num; $i++) {
983  if ($conf->global->MAIN_FEATURES_LEVEL == 2) {
984  print '* '.$row[$i]['datec'].' paymentid='.$row[$i]['paymentid'].' pamount='.$row[$i]['pamount'].' fk_bank='.$row[$i]['fk_bank'].' '.$row[$i]['bamount'].' socid='.$row[$i]['socid'].'<br>';
985  }
986 
987  // On cherche facture sans lien paiement et du meme montant et pour meme societe.
988  $sql = " SELECT distinct f.rowid from ".MAIN_DB_PREFIX."facture as f";
989  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture";
990  $sql .= " WHERE f.fk_statut in (2,3) AND fk_soc = ".((int) $row[$i]['socid'])." AND total_ttc = ".((float) $row[$i]['pamount']);
991  $sql .= " AND pf.fk_facture IS NULL";
992  $sql .= " ORDER BY f.fk_statut";
993  //print $sql.'<br>';
994  $resql = $db->query($sql);
995  if ($resql) {
996  $num = $db->num_rows($resql);
997  //print 'Nb of invoice found for this amount and company :'.$num.'<br>';
998  if ($num >= 1) {
999  $obj = $db->fetch_object($resql);
1000  $facid = $obj->rowid;
1001 
1002  $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount)";
1003  $sql .= " VALUES (".((int) $facid).",".((int) $row[$i]['paymentid']).", ".((float) $row[$i]['pamount']).")";
1004 
1005  $res += $db->query($sql);
1006 
1007  print $langs->trans('MigrationProcessPaymentUpdate', 'facid='.$facid.'-paymentid='.$row[$i]['paymentid'].'-amount='.$row[$i]['pamount'])."<br>\n";
1008  }
1009  } else {
1010  print 'ERROR';
1011  $nberr++;
1012  }
1013  }
1014 
1015  if ($res > 0) {
1016  print $langs->trans('MigrationSuccessfullUpdate')."<br>";
1017  } else {
1018  print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
1019  }
1020 
1021  $db->commit();
1022  } else {
1023  print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
1024  }
1025 
1026  // Delete obsolete fields fk_facture
1027  $db->begin();
1028 
1029  $sql = "ALTER TABLE ".MAIN_DB_PREFIX."paiement DROP COLUMN fk_facture";
1030  $db->query($sql);
1031 
1032  if (!$nberr) {
1033  $db->commit();
1034  } else {
1035  print 'ERROR';
1036  $db->rollback();
1037  }
1038  } else {
1039  print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
1040  }
1041 
1042  print '</td></tr>';
1043 }
1044 
1045 
1054 function migrate_contracts_det($db, $langs, $conf)
1055 {
1056  print '<tr><td colspan="4">';
1057 
1058  $nberr = 0;
1059 
1060  print '<br>';
1061  print '<b>'.$langs->trans('MigrationContractsUpdate')."</b><br>\n";
1062 
1063  $sql = "SELECT c.rowid as cref, c.date_contrat, c.statut, c.fk_product, c.fk_facture, c.fk_user_author,";
1064  $sql .= " p.ref, p.label, p.description, p.price, p.tva_tx, p.duration, cd.rowid";
1065  $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c";
1066  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p";
1067  $sql .= " ON c.fk_product = p.rowid";
1068  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."contratdet as cd";
1069  $sql .= " ON c.rowid=cd.fk_contrat";
1070  $sql .= " WHERE cd.rowid IS NULL AND p.rowid IS NOT NULL";
1071  $resql = $db->query($sql);
1072 
1073  dolibarr_install_syslog("upgrade2::migrate_contracts_det");
1074  if ($resql) {
1075  $i = 0;
1076  $row = array();
1077  $num = $db->num_rows($resql);
1078 
1079  if ($num) {
1080  print $langs->trans('MigrationContractsNumberToUpdate', $num)."<br>\n";
1081  $db->begin();
1082 
1083  while ($i < $num) {
1084  $obj = $db->fetch_object($resql);
1085 
1086  $sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet (";
1087  $sql .= "fk_contrat, fk_product, statut, label, description,";
1088  $sql .= "date_ouverture_prevue, date_ouverture, date_fin_validite, tva_tx, qty,";
1089  $sql .= "subprice, price_ht, fk_user_author, fk_user_ouverture)";
1090  $sql .= " VALUES (";
1091  $sql .= ((int) $obj->cref).", ".($obj->fk_product ? ((int) $obj->fk_product) : 0).", ";
1092  $sql .= "0, ";
1093  $sql .= "'".$db->escape($obj->label)."', null, ";
1094  $sql .= ($obj->date_contrat ? "'".$db->idate($db->jdate($obj->date_contrat))."'" : "null").", ";
1095  $sql .= "null, ";
1096  $sql .= "null, ";
1097  $sql .= ((float) $obj->tva_tx).", 1, ";
1098  $sql .= ((float) $obj->price).", ".((float) $obj->price).", ".((int) $obj->fk_user_author).",";
1099  $sql .= "null";
1100  $sql .= ")";
1101 
1102  if ($db->query($sql)) {
1103  print $langs->trans('MigrationContractsLineCreation', $obj->cref)."<br>\n";
1104  } else {
1105  dol_print_error($db);
1106  $nberr++;
1107  }
1108 
1109  $i++;
1110  }
1111 
1112  if (!$nberr) {
1113  // $db->rollback();
1114  $db->commit();
1115  print $langs->trans('MigrationSuccessfullUpdate')."<br>";
1116  } else {
1117  $db->rollback();
1118  print $langs->trans('MigrationUpdateFailed').'<br>';
1119  }
1120  } else {
1121  print $langs->trans('MigrationContractsNothingToUpdate')."<br>\n";
1122  }
1123  } else {
1124  print $langs->trans('MigrationContractsFieldDontExist')."<br>\n";
1125  // dol_print_error($db);
1126  }
1127 
1128  print '</td></tr>';
1129 }
1130 
1139 function migrate_links_transfert($db, $langs, $conf)
1140 {
1141  print '<tr><td colspan="4">';
1142 
1143  $nberr = 0;
1144 
1145  print '<br>';
1146  print '<b>'.$langs->trans('MigrationBankTransfertsUpdate')."</b><br>\n";
1147 
1148  $sql = "SELECT ba.rowid as barowid, bb.rowid as bbrowid";
1149  $sql .= " FROM ".MAIN_DB_PREFIX."bank as bb, ".MAIN_DB_PREFIX."bank as ba";
1150  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu ON bu.fk_bank = ba.rowid";
1151  $sql .= " WHERE ba.amount = -bb.amount AND ba.fk_account <> bb.fk_account";
1152  $sql .= " AND ba.datev = bb.datev AND ba.datec = bb.datec";
1153  $sql .= " AND bu.fk_bank IS NULL";
1154  $resql = $db->query($sql);
1155 
1156  dolibarr_install_syslog("upgrade2::migrate_links_transfert");
1157  if ($resql) {
1158  $i = 0;
1159  $row = array();
1160  $num = $db->num_rows($resql);
1161 
1162  if ($num) {
1163  print $langs->trans('MigrationBankTransfertsToUpdate', $num)."<br>\n";
1164  $db->begin();
1165 
1166  while ($i < $num) {
1167  $obj = $db->fetch_object($resql);
1168 
1169  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (";
1170  $sql .= "fk_bank, url_id, url, label, type";
1171  $sql .= ")";
1172  $sql .= " VALUES (";
1173  $sql .= $obj->barowid.",".$obj->bbrowid.", '/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert'";
1174  $sql .= ")";
1175 
1176  //print $sql.'<br>';
1177  dolibarr_install_syslog("migrate_links_transfert");
1178 
1179  if (!$db->query($sql)) {
1180  dol_print_error($db);
1181  $nberr++;
1182  }
1183 
1184  $i++;
1185  }
1186 
1187  if (!$nberr) {
1188  // $db->rollback();
1189  $db->commit();
1190  print $langs->trans('MigrationSuccessfullUpdate')."<br>";
1191  } else {
1192  $db->rollback();
1193  print $langs->trans('MigrationUpdateFailed').'<br>';
1194  }
1195  } else {
1196  print $langs->trans('MigrationBankTransfertsNothingToUpdate')."<br>\n";
1197  }
1198  } else {
1199  dol_print_error($db);
1200  }
1201 
1202  print '</td></tr>';
1203 }
1204 
1213 function migrate_contracts_date1($db, $langs, $conf)
1214 {
1215  print '<tr><td colspan="4">';
1216 
1217  print '<br>';
1218  print '<b>'.$langs->trans('MigrationContractsEmptyDatesUpdate')."</b><br>\n";
1219 
1220  $sql = "update ".MAIN_DB_PREFIX."contrat set date_contrat=tms where date_contrat is null";
1221  dolibarr_install_syslog("upgrade2::migrate_contracts_date1");
1222  $resql = $db->query($sql);
1223  if (!$resql) {
1224  dol_print_error($db);
1225  }
1226  if ($db->affected_rows($resql) > 0) {
1227  print $langs->trans('MigrationContractsEmptyDatesUpdateSuccess')."<br>\n";
1228  } else {
1229  print $langs->trans('MigrationContractsEmptyDatesNothingToUpdate')."<br>\n";
1230  }
1231 
1232  $sql = "update ".MAIN_DB_PREFIX."contrat set datec=tms where datec is null";
1233  dolibarr_install_syslog("upgrade2::migrate_contracts_date1");
1234  $resql = $db->query($sql);
1235  if (!$resql) {
1236  dol_print_error($db);
1237  }
1238  if ($db->affected_rows($resql) > 0) {
1239  print $langs->trans('MigrationContractsEmptyCreationDatesUpdateSuccess')."<br>\n";
1240  } else {
1241  print $langs->trans('MigrationContractsEmptyCreationDatesNothingToUpdate')."<br>\n";
1242  }
1243 
1244  print '</td></tr>';
1245 }
1246 
1255 function migrate_contracts_date2($db, $langs, $conf)
1256 {
1257  print '<tr><td colspan="4">';
1258 
1259  $nberr = 0;
1260 
1261  print '<br>';
1262  print '<b>'.$langs->trans('MigrationContractsInvalidDatesUpdate')."</b><br>\n";
1263 
1264  $sql = "SELECT c.rowid as cref, c.datec, c.date_contrat, MIN(cd.date_ouverture) as datemin";
1265  $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c,";
1266  $sql .= " ".MAIN_DB_PREFIX."contratdet as cd";
1267  $sql .= " WHERE c.rowid=cd.fk_contrat AND cd.date_ouverture IS NOT NULL";
1268  $sql .= " GROUP BY c.rowid, c.date_contrat";
1269  $resql = $db->query($sql);
1270 
1271  dolibarr_install_syslog("upgrade2::migrate_contracts_date2");
1272  if ($resql) {
1273  $i = 0;
1274  $row = array();
1275  $num = $db->num_rows($resql);
1276 
1277  if ($num) {
1278  $nbcontratsmodifie = 0;
1279  $db->begin();
1280 
1281  while ($i < $num) {
1282  $obj = $db->fetch_object($resql);
1283  if ($obj->date_contrat > $obj->datemin) {
1284  $datemin = $db->jdate($obj->datemin);
1285 
1286  print $langs->trans('MigrationContractsInvalidDateFix', $obj->cref, $obj->date_contrat, $obj->datemin)."<br>\n";
1287  $sql = "UPDATE ".MAIN_DB_PREFIX."contrat";
1288  $sql .= " SET date_contrat='".$db->idate($datemin)."'";
1289  $sql .= " WHERE rowid = ".((int) $obj->cref);
1290  $resql2 = $db->query($sql);
1291  if (!$resql2) {
1292  dol_print_error($db);
1293  }
1294 
1295  $nbcontratsmodifie++;
1296  }
1297  $i++;
1298  }
1299 
1300  $db->commit();
1301 
1302  if ($nbcontratsmodifie) {
1303  print $langs->trans('MigrationContractsInvalidDatesNumber', $nbcontratsmodifie)."<br>\n";
1304  } else {
1305  print $langs->trans('MigrationContractsInvalidDatesNothingToUpdate')."<br>\n";
1306  }
1307  }
1308  } else {
1309  dol_print_error($db);
1310  }
1311 
1312  print '</td></tr>';
1313 }
1314 
1323 function migrate_contracts_date3($db, $langs, $conf)
1324 {
1325  print '<tr><td colspan="4">';
1326 
1327  print '<br>';
1328  print '<b>'.$langs->trans('MigrationContractsIncoherentCreationDateUpdate')."</b><br>\n";
1329 
1330  $sql = "update ".MAIN_DB_PREFIX."contrat set datec=date_contrat where datec is null or datec > date_contrat";
1331  dolibarr_install_syslog("upgrade2::migrate_contracts_date3");
1332  $resql = $db->query($sql);
1333  if (!$resql) {
1334  dol_print_error($db);
1335  }
1336  if ($db->affected_rows($resql) > 0) {
1337  print $langs->trans('MigrationContractsIncoherentCreationDateUpdateSuccess')."<br>\n";
1338  } else {
1339  print $langs->trans('MigrationContractsIncoherentCreationDateNothingToUpdate')."<br>\n";
1340  }
1341 
1342  print '</td></tr>';
1343 }
1344 
1353 function migrate_contracts_open($db, $langs, $conf)
1354 {
1355  print '<tr><td colspan="4">';
1356 
1357  print '<br>';
1358  print '<b>'.$langs->trans('MigrationReopeningContracts')."</b><br>\n";
1359 
1360  $sql = "SELECT c.rowid as cref FROM ".MAIN_DB_PREFIX."contrat as c, ".MAIN_DB_PREFIX."contratdet as cd";
1361  $sql .= " WHERE cd.statut = 4 AND c.statut=2 AND c.rowid=cd.fk_contrat";
1362  dolibarr_install_syslog("upgrade2::migrate_contracts_open");
1363  $resql = $db->query($sql);
1364  if (!$resql) {
1365  dol_print_error($db);
1366  }
1367  if ($db->affected_rows($resql) > 0) {
1368  $i = 0;
1369  $row = array();
1370  $num = $db->num_rows($resql);
1371 
1372  if ($num) {
1373  $nbcontratsmodifie = 0;
1374  $db->begin();
1375 
1376  while ($i < $num) {
1377  $obj = $db->fetch_object($resql);
1378 
1379  print $langs->trans('MigrationReopenThisContract', $obj->cref)."<br>\n";
1380  $sql = "UPDATE ".MAIN_DB_PREFIX."contrat";
1381  $sql .= " SET statut = 1";
1382  $sql .= " WHERE rowid = ".((int) $obj->cref);
1383  $resql2 = $db->query($sql);
1384  if (!$resql2) {
1385  dol_print_error($db);
1386  }
1387 
1388  $nbcontratsmodifie++;
1389 
1390  $i++;
1391  }
1392 
1393  $db->commit();
1394 
1395  if ($nbcontratsmodifie) {
1396  print $langs->trans('MigrationReopenedContractsNumber', $nbcontratsmodifie)."<br>\n";
1397  } else {
1398  print $langs->trans('MigrationReopeningContractsNothingToUpdate')."<br>\n";
1399  }
1400  }
1401  } else {
1402  print $langs->trans('MigrationReopeningContractsNothingToUpdate')."<br>\n";
1403  }
1404 
1405  print '</td></tr>';
1406 }
1407 
1416 function migrate_paiementfourn_facturefourn($db, $langs, $conf)
1417 {
1418  global $bc;
1419 
1420  print '<tr><td colspan="4">';
1421  print '<br>';
1422  print '<b>'.$langs->trans('SuppliersInvoices')."</b><br>\n";
1423  print '</td></tr>';
1424 
1425  $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiementfourn", "fk_facture_fourn");
1426  $obj = $db->fetch_object($result);
1427  if ($obj) {
1428  $error = 0;
1429  $nb = 0;
1430 
1431  $select_sql = 'SELECT rowid, fk_facture_fourn, amount';
1432  $select_sql .= ' FROM '.MAIN_DB_PREFIX.'paiementfourn';
1433  $select_sql .= ' WHERE fk_facture_fourn IS NOT NULL';
1434 
1435  dolibarr_install_syslog("upgrade2::migrate_paiementfourn_facturefourn");
1436  $select_resql = $db->query($select_sql);
1437  if ($select_resql) {
1438  $select_num = $db->num_rows($select_resql);
1439  $i = 0;
1440 
1441  // Pour chaque paiement fournisseur, on insere une ligne dans paiementfourn_facturefourn
1442  while (($i < $select_num) && (!$error)) {
1443  $select_obj = $db->fetch_object($select_resql);
1444 
1445  // Verifier si la ligne est deja dans la nouvelle table. On ne veut pas inserer de doublons.
1446  $check_sql = 'SELECT fk_paiementfourn, fk_facturefourn';
1447  $check_sql .= ' FROM '.MAIN_DB_PREFIX.'paiementfourn_facturefourn';
1448  $check_sql .= ' WHERE fk_paiementfourn = '.((int) $select_obj->rowid).' AND fk_facturefourn = '.((int) $select_obj->fk_facture_fourn);
1449  $check_resql = $db->query($check_sql);
1450  if ($check_resql) {
1451  $check_num = $db->num_rows($check_resql);
1452  if ($check_num == 0) {
1453  $db->begin();
1454 
1455  if ($nb == 0) {
1456  print '<tr><td colspan="4" class="nowrap"><b>'.$langs->trans('SuppliersInvoices').'</b></td></tr>';
1457  print '<tr><td>fk_paiementfourn</td><td>fk_facturefourn</td><td>'.$langs->trans('Amount').'</td><td>&nbsp;</td></tr>';
1458  }
1459 
1460  print '<tr class="oddeven">';
1461  print '<td>'.$select_obj->rowid.'</td><td>'.$select_obj->fk_facture_fourn.'</td><td>'.$select_obj->amount.'</td>';
1462 
1463  $insert_sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiementfourn_facturefourn SET ';
1464  $insert_sql .= ' fk_paiementfourn = \''.$select_obj->rowid.'\',';
1465  $insert_sql .= ' fk_facturefourn = \''.$select_obj->fk_facture_fourn.'\',';
1466  $insert_sql .= ' amount = \''.$select_obj->amount.'\'';
1467  $insert_resql = $db->query($insert_sql);
1468 
1469  if ($insert_resql) {
1470  $nb++;
1471  print '<td><span class="ok">'.$langs->trans("OK").'</span></td>';
1472  } else {
1473  print '<td><span class="error">Error on insert</span></td>';
1474  $error++;
1475  }
1476  print '</tr>';
1477  }
1478  } else {
1479  $error++;
1480  }
1481  $i++;
1482  }
1483  } else {
1484  $error++;
1485  }
1486 
1487  if (!$error) {
1488  if (!$nb) {
1489  print '<tr><td>'.$langs->trans("AlreadyDone").'</td></tr>';
1490  }
1491  $db->commit();
1492 
1493  $sql = "ALTER TABLE ".MAIN_DB_PREFIX."paiementfourn DROP COLUMN fk_facture_fourn";
1494  $db->query($sql);
1495  } else {
1496  print '<tr><td>'.$langs->trans("Error").'</td></tr>';
1497  $db->rollback();
1498  }
1499  } else {
1500  print '<tr><td>'.$langs->trans("AlreadyDone").'</td></tr>';
1501  }
1502 }
1503 
1512 function migrate_price_facture($db, $langs, $conf)
1513 {
1514  $err = 0;
1515 
1516  $tmpmysoc = new Societe($db);
1517  $tmpmysoc->setMysoc($conf);
1518 
1519  $db->begin();
1520 
1521  print '<tr><td colspan="4">';
1522 
1523  print '<br>';
1524  print '<b>'.$langs->trans('MigrationInvoice')."</b><br>\n";
1525 
1526  // List of invoice lines not up to date
1527  $sql = "SELECT fd.rowid, fd.qty, fd.subprice, fd.remise_percent, fd.tva_tx as vatrate, fd.total_ttc, fd.info_bits,";
1528  $sql .= " f.rowid as facid, f.remise_percent as remise_percent_global, f.total_ttc as total_ttc_f";
1529  $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd, ".MAIN_DB_PREFIX."facture as f";
1530  $sql .= " WHERE fd.fk_facture = f.rowid";
1531  $sql .= " AND (((fd.total_ttc = 0 AND fd.remise_percent != 100) or fd.total_ttc IS NULL) or f.total_ttc IS NULL)";
1532  //print $sql;
1533 
1534  dolibarr_install_syslog("upgrade2::migrate_price_facture");
1535  $resql = $db->query($sql);
1536  if ($resql) {
1537  $num = $db->num_rows($resql);
1538  $i = 0;
1539  if ($num) {
1540  while ($i < $num) {
1541  $obj = $db->fetch_object($resql);
1542 
1543  $rowid = $obj->rowid;
1544  $qty = $obj->qty;
1545  $pu = $obj->subprice;
1546  $vatrate = $obj->vatrate;
1547  $remise_percent = $obj->remise_percent;
1548  $remise_percent_global = $obj->remise_percent_global;
1549  $total_ttc_f = $obj->total_ttc_f;
1550  $info_bits = $obj->info_bits;
1551 
1552  // On met a jour les 3 nouveaux champs
1553  $facligne = new FactureLigne($db);
1554  $facligne->fetch($rowid);
1555 
1556  $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, $remise_percent_global, 'HT', $info_bits, $facligne->product_type, $tmpmysoc);
1557  $total_ht = $result[0];
1558  $total_tva = $result[1];
1559  $total_ttc = $result[2];
1560 
1561  $facligne->total_ht = $total_ht;
1562  $facligne->total_tva = $total_tva;
1563  $facligne->total_ttc = $total_ttc;
1564 
1565  dolibarr_install_syslog("upgrade2: line ".$rowid.": facid=".$obj->facid." pu=".$pu." qty=".$qty." vatrate=".$vatrate." remise_percent=".$remise_percent." remise_global=".$remise_percent_global." -> ".$total_ht.", ".$total_tva.", ".$total_ttc);
1566  print ". ";
1567  $facligne->update_total();
1568 
1569 
1570  /* On touche a facture mere uniquement si total_ttc = 0 */
1571  if (!$total_ttc_f) {
1572  $facture = new Facture($db);
1573  $facture->id = $obj->facid;
1574 
1575  if ($facture->fetch($facture->id) >= 0) {
1576  if ($facture->update_price() > 0) {
1577  //print $facture->id;
1578  } else {
1579  print "Error id=".$facture->id;
1580  $err++;
1581  }
1582  } else {
1583  print "Error #3";
1584  $err++;
1585  }
1586  }
1587  print " ";
1588 
1589  $i++;
1590  }
1591  } else {
1592  print $langs->trans("AlreadyDone");
1593  }
1594  $db->free($resql);
1595 
1596  $db->commit();
1597  } else {
1598  print "Error #1 ".$db->error();
1599  $err++;
1600 
1601  $db->rollback();
1602  }
1603 
1604  print '<br>';
1605 
1606  print '</td></tr>';
1607 }
1608 
1617 function migrate_price_propal($db, $langs, $conf)
1618 {
1619  $tmpmysoc = new Societe($db);
1620  $tmpmysoc->setMysoc($conf);
1621 
1622  $db->begin();
1623 
1624  print '<tr><td colspan="4">';
1625 
1626  print '<br>';
1627  print '<b>'.$langs->trans('MigrationProposal')."</b><br>\n";
1628 
1629  // List of proposal lines not up to date
1630  $sql = "SELECT pd.rowid, pd.qty, pd.subprice, pd.remise_percent, pd.tva_tx as vatrate, pd.info_bits,";
1631  $sql .= " p.rowid as propalid, p.remise_percent as remise_percent_global";
1632  $sql .= " FROM ".MAIN_DB_PREFIX."propaldet as pd, ".MAIN_DB_PREFIX."propal as p";
1633  $sql .= " WHERE pd.fk_propal = p.rowid";
1634  $sql .= " AND ((pd.total_ttc = 0 AND pd.remise_percent != 100) or pd.total_ttc IS NULL)";
1635 
1636  dolibarr_install_syslog("upgrade2::migrate_price_propal");
1637  $resql = $db->query($sql);
1638  if ($resql) {
1639  $num = $db->num_rows($resql);
1640  $i = 0;
1641  if ($num) {
1642  while ($i < $num) {
1643  $obj = $db->fetch_object($resql);
1644 
1645  $rowid = $obj->rowid;
1646  $qty = $obj->qty;
1647  $pu = $obj->subprice;
1648  $vatrate = $obj->vatrate;
1649  $remise_percent = $obj->remise_percent;
1650  $remise_percent_global = $obj->remise_percent_global;
1651  $info_bits = $obj->info_bits;
1652 
1653  // On met a jour les 3 nouveaux champs
1654  $propalligne = new PropaleLigne($db);
1655  $propalligne->fetch($rowid);
1656 
1657  $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, $remise_percent_global, 'HT', $info_bits, $propalligne->product_type, $tmpmysoc);
1658  $total_ht = $result[0];
1659  $total_tva = $result[1];
1660  $total_ttc = $result[2];
1661 
1662  $propalligne->total_ht = $total_ht;
1663  $propalligne->total_tva = $total_tva;
1664  $propalligne->total_ttc = $total_ttc;
1665 
1666  dolibarr_install_syslog("upgrade2: Line ".$rowid.": propalid=".$obj->rowid." pu=".$pu." qty=".$qty." vatrate=".$vatrate." remise_percent=".$remise_percent." remise_global=".$remise_percent_global." -> ".$total_ht.", ".$total_tva.", ".$total_ttc);
1667  print ". ";
1668  $propalligne->update_total();
1669 
1670 
1671  /* On touche pas a propal mere
1672  $propal = new Propal($db);
1673  $propal->id=$obj->rowid;
1674  if ( $propal->fetch($propal->id) >= 0 )
1675  {
1676  if ( $propal->update_price() > 0 )
1677  {
1678  print ". ";
1679  }
1680  else
1681  {
1682  print "Error id=".$propal->id;
1683  }
1684  }
1685  else
1686  {
1687  print "Error #3";
1688  }
1689  */
1690  $i++;
1691  }
1692  } else {
1693  print $langs->trans("AlreadyDone");
1694  }
1695 
1696  $db->free($resql);
1697 
1698  $db->commit();
1699  } else {
1700  print "Error #1 ".$db->error();
1701 
1702  $db->rollback();
1703  }
1704 
1705  print '<br>';
1706 
1707  print '</td></tr>';
1708 }
1709 
1718 function migrate_price_contrat($db, $langs, $conf)
1719 {
1720  $db->begin();
1721 
1722  $tmpmysoc = new Societe($db);
1723  $tmpmysoc->setMysoc($conf);
1724  if (empty($tmpmysoc->country_id)) {
1725  $tmpmysoc->country_id = 0; // Ti not have this set to '' or will make sql syntax error.
1726  }
1727 
1728  print '<tr><td colspan="4">';
1729 
1730  print '<br>';
1731  print '<b>'.$langs->trans('MigrationContract')."</b><br>\n";
1732 
1733  // List of contract lines not up to date
1734  $sql = "SELECT cd.rowid, cd.qty, cd.subprice, cd.remise_percent, cd.tva_tx as vatrate, cd.info_bits,";
1735  $sql .= " c.rowid as contratid";
1736  $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."contrat as c";
1737  $sql .= " WHERE cd.fk_contrat = c.rowid";
1738  $sql .= " AND ((cd.total_ttc = 0 AND cd.remise_percent != 100 AND cd.subprice > 0) or cd.total_ttc IS NULL)";
1739 
1740  dolibarr_install_syslog("upgrade2::migrate_price_contrat");
1741  $resql = $db->query($sql);
1742  if ($resql) {
1743  $num = $db->num_rows($resql);
1744  $i = 0;
1745  if ($num) {
1746  while ($i < $num) {
1747  $obj = $db->fetch_object($resql);
1748 
1749  $rowid = $obj->rowid;
1750  $qty = $obj->qty;
1751  $pu = $obj->subprice;
1752  $vatrate = $obj->vatrate;
1753  $remise_percent = $obj->remise_percent;
1754  $info_bits = $obj->info_bits;
1755 
1756  // On met a jour les 3 nouveaux champs
1757  $contratligne = new ContratLigne($db);
1758  //$contratligne->fetch($rowid); Non requis car le update_total ne met a jour que chp redefinis
1759  $contratligne->fetch($rowid);
1760 
1761  $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, 0, 'HT', $info_bits, $contratligne->product_type, $tmpmysoc);
1762  $total_ht = $result[0];
1763  $total_tva = $result[1];
1764  $total_ttc = $result[2];
1765 
1766  $contratligne->total_ht = $total_ht;
1767  $contratligne->total_tva = $total_tva;
1768  $contratligne->total_ttc = $total_ttc;
1769 
1770  dolibarr_install_syslog("upgrade2: Line ".$rowid.": contratdetid=".$obj->rowid." pu=".$pu." qty=".$qty." vatrate=".$vatrate." remise_percent=".$remise_percent." -> ".$total_ht.", ".$total_tva." , ".$total_ttc);
1771  print ". ";
1772  $contratligne->update_total();
1773 
1774  $i++;
1775  }
1776  } else {
1777  print $langs->trans("AlreadyDone");
1778  }
1779 
1780  $db->free($resql);
1781 
1782  $db->commit();
1783  } else {
1784  print "Error #1 ".$db->error();
1785 
1786  $db->rollback();
1787  }
1788 
1789  print '<br>';
1790 
1791  print '</td></tr>';
1792 }
1793 
1802 function migrate_price_commande($db, $langs, $conf)
1803 {
1804  $db->begin();
1805 
1806  $tmpmysoc = new Societe($db);
1807  $tmpmysoc->setMysoc($conf);
1808 
1809  print '<tr><td colspan="4">';
1810 
1811  print '<br>';
1812  print '<b>'.$langs->trans('MigrationOrder')."</b><br>\n";
1813 
1814  // List of sales orders lines not up to date
1815  $sql = "SELECT cd.rowid, cd.qty, cd.subprice, cd.remise_percent, cd.tva_tx as vatrate, cd.info_bits,";
1816  $sql .= " c.rowid as commandeid, c.remise_percent as remise_percent_global";
1817  $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd, ".MAIN_DB_PREFIX."commande as c";
1818  $sql .= " WHERE cd.fk_commande = c.rowid";
1819  $sql .= " AND ((cd.total_ttc = 0 AND cd.remise_percent != 100) or cd.total_ttc IS NULL)";
1820 
1821  dolibarr_install_syslog("upgrade2::migrate_price_commande");
1822  $resql = $db->query($sql);
1823  if ($resql) {
1824  $num = $db->num_rows($resql);
1825  $i = 0;
1826  if ($num) {
1827  while ($i < $num) {
1828  $obj = $db->fetch_object($resql);
1829 
1830  $rowid = $obj->rowid;
1831  $qty = $obj->qty;
1832  $pu = $obj->subprice;
1833  $vatrate = $obj->vatrate;
1834  $remise_percent = $obj->remise_percent;
1835  $remise_percent_global = $obj->remise_percent_global;
1836  $info_bits = $obj->info_bits;
1837 
1838  // On met a jour les 3 nouveaux champs
1839  $commandeligne = new OrderLine($db);
1840  $commandeligne->fetch($rowid);
1841 
1842  $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, $remise_percent_global, 'HT', $info_bits, $commandeligne->product_type, $tmpmysoc);
1843  $total_ht = $result[0];
1844  $total_tva = $result[1];
1845  $total_ttc = $result[2];
1846 
1847  $commandeligne->total_ht = $total_ht;
1848  $commandeligne->total_tva = $total_tva;
1849  $commandeligne->total_ttc = $total_ttc;
1850 
1851  dolibarr_install_syslog("upgrade2: Line ".$rowid." : commandeid=".$obj->rowid." pu=".$pu." qty=".$qty." vatrate=".$vatrate." remise_percent=".$remise_percent." remise_global=".$remise_percent_global." -> ".$total_ht.", ".$total_tva.", ".$total_ttc);
1852  print ". ";
1853  $commandeligne->update_total();
1854 
1855  /* On touche pas a facture mere
1856  $commande = new Commande($db);
1857  $commande->id = $obj->rowid;
1858  if ( $commande->fetch($commande->id) >= 0 )
1859  {
1860  if ( $commande->update_price() > 0 )
1861  {
1862  print ". ";
1863  }
1864  else
1865  {
1866  print "Error id=".$commande->id;
1867  }
1868  }
1869  else
1870  {
1871  print "Error #3";
1872  }
1873  */
1874  $i++;
1875  }
1876  } else {
1877  print $langs->trans("AlreadyDone");
1878  }
1879 
1880  $db->free($resql);
1881 
1882  /*
1883  $sql = "DELETE FROM ".MAIN_DB_PREFIX."commandedet";
1884  $sql.= " WHERE price = 0 and total_ttc = 0 and total_tva = 0 and total_ht = 0 AND remise_percent = 0";
1885  $resql=$db->query($sql);
1886  if (! $resql)
1887  {
1888  dol_print_error($db);
1889  }
1890  */
1891 
1892  $db->commit();
1893  } else {
1894  print "Error #1 ".$db->error();
1895 
1896  $db->rollback();
1897  }
1898 
1899  print '<br>';
1900 
1901  print '</td></tr>';
1902 }
1903 
1912 function migrate_price_commande_fournisseur($db, $langs, $conf)
1913 {
1914  $db->begin();
1915 
1916  $tmpmysoc = new Societe($db);
1917  $tmpmysoc->setMysoc($conf);
1918 
1919  print '<tr><td colspan="4">';
1920 
1921  print '<br>';
1922  print '<b>'.$langs->trans('MigrationSupplierOrder')."</b><br>\n";
1923 
1924  // List of purchase order lines not up to date
1925  $sql = "SELECT cd.rowid, cd.qty, cd.subprice, cd.remise_percent, cd.tva_tx as vatrate, cd.info_bits,";
1926  $sql .= " c.rowid as commandeid, c.remise_percent as remise_percent_global";
1927  $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as cd, ".MAIN_DB_PREFIX."commande_fournisseur as c";
1928  $sql .= " WHERE cd.fk_commande = c.rowid";
1929  $sql .= " AND ((cd.total_ttc = 0 AND cd.remise_percent != 100) or cd.total_ttc IS NULL)";
1930 
1931  dolibarr_install_syslog("upgrade2::migrate_price_commande_fournisseur");
1932  $resql = $db->query($sql);
1933  if ($resql) {
1934  $num = $db->num_rows($resql);
1935  $i = 0;
1936  if ($num) {
1937  while ($i < $num) {
1938  $obj = $db->fetch_object($resql);
1939 
1940  $rowid = $obj->rowid;
1941  $qty = $obj->qty;
1942  $pu = $obj->subprice;
1943  $vatrate = $obj->vatrate;
1944  $remise_percent = $obj->remise_percent;
1945  $remise_percent_global = $obj->remise_percent_global;
1946  $info_bits = $obj->info_bits;
1947 
1948  // On met a jour les 3 nouveaux champs
1949  $commandeligne = new CommandeFournisseurLigne($db);
1950  $commandeligne->fetch($rowid);
1951 
1952  $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, $remise_percent_global, 'HT', $info_bits, $commandeligne->product_type, $tmpsoc);
1953  $total_ht = $result[0];
1954  $total_tva = $result[1];
1955  $total_ttc = $result[2];
1956 
1957  $commandeligne->total_ht = $total_ht;
1958  $commandeligne->total_tva = $total_tva;
1959  $commandeligne->total_ttc = $total_ttc;
1960 
1961  dolibarr_install_syslog("upgrade2: Line ".$rowid.": commandeid=".$obj->rowid." pu=".$pu." qty=".$qty." vatrate=".$vatrate." remise_percent=".$remise_percent." remise_global=".$remise_percent_global." -> ".$total_ht.", ".$total_tva.", ".$total_ttc);
1962  print ". ";
1963  $commandeligne->update_total();
1964 
1965  /* On touche pas a facture mere
1966  $commande = new Commande($db);
1967  $commande->id = $obj->rowid;
1968  if ( $commande->fetch($commande->id) >= 0 )
1969  {
1970  if ( $commande->update_price() > 0 )
1971  {
1972  print ". ";
1973  }
1974  else
1975  {
1976  print "Error id=".$commande->id;
1977  }
1978  }
1979  else
1980  {
1981  print "Error #3";
1982  }
1983  */
1984  $i++;
1985  }
1986  } else {
1987  print $langs->trans("AlreadyDone");
1988  }
1989 
1990  $db->free($resql);
1991 
1992  /*
1993  $sql = "DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseurdet";
1994  $sql.= " WHERE subprice = 0 and total_ttc = 0 and total_tva = 0 and total_ht = 0";
1995  $resql=$db->query($sql);
1996  if (! $resql)
1997  {
1998  dol_print_error($db);
1999  }
2000  */
2001 
2002  $db->commit();
2003  } else {
2004  print "Error #1 ".$db->error();
2005 
2006  $db->rollback();
2007  }
2008 
2009  print '<br>';
2010 
2011  print '</td></tr>';
2012 }
2013 
2022 function migrate_modeles($db, $langs, $conf)
2023 {
2024  //print '<br>';
2025  //print '<b>'.$langs->trans('UpdateModelsTable')."</b><br>\n";
2026 
2027  dolibarr_install_syslog("upgrade2::migrate_modeles");
2028 
2029  if (isModEnabled('facture')) {
2030  include_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
2031  $modellist = ModelePDFFactures::liste_modeles($db);
2032  if (count($modellist) == 0) {
2033  // Aucun model par defaut.
2034  $sql = " insert into ".MAIN_DB_PREFIX."document_model(nom,type) values('crabe','invoice')";
2035  $resql = $db->query($sql);
2036  if (!$resql) {
2037  dol_print_error($db);
2038  }
2039  }
2040  }
2041 
2042  if (isModEnabled('commande')) {
2043  include_once DOL_DOCUMENT_ROOT.'/core/modules/commande/modules_commande.php';
2044  $modellist = ModelePDFCommandes::liste_modeles($db);
2045  if (count($modellist) == 0) {
2046  // Aucun model par defaut.
2047  $sql = " insert into ".MAIN_DB_PREFIX."document_model(nom,type) values('einstein','order')";
2048  $resql = $db->query($sql);
2049  if (!$resql) {
2050  dol_print_error($db);
2051  }
2052  }
2053  }
2054 
2055  if (isModEnabled("expedition")) {
2056  include_once DOL_DOCUMENT_ROOT.'/core/modules/expedition/modules_expedition.php';
2057  $modellist = ModelePDFExpedition::liste_modeles($db);
2058  if (count($modellist) == 0) {
2059  // Aucun model par defaut.
2060  $sql = " insert into ".MAIN_DB_PREFIX."document_model(nom,type) values('rouget','shipping')";
2061  $resql = $db->query($sql);
2062  if (!$resql) {
2063  dol_print_error($db);
2064  }
2065  }
2066  }
2067 
2068  //print $langs->trans("AlreadyDone");
2069 }
2070 
2071 
2080 function migrate_commande_expedition($db, $langs, $conf)
2081 {
2082  dolibarr_install_syslog("upgrade2::migrate_commande_expedition");
2083 
2084  print '<tr><td colspan="4">';
2085 
2086  print '<br>';
2087  print '<b>'.$langs->trans('MigrationShipmentOrderMatching')."</b><br>\n";
2088 
2089  $result = $db->DDLDescTable(MAIN_DB_PREFIX."expedition", "fk_commande");
2090  $obj = $db->fetch_object($result);
2091  if ($obj) {
2092  $error = 0;
2093 
2094  $db->begin();
2095 
2096  $sql = "SELECT e.rowid, e.fk_commande FROM ".MAIN_DB_PREFIX."expedition as e";
2097  $resql = $db->query($sql);
2098  if ($resql) {
2099  $i = 0;
2100  $num = $db->num_rows($resql);
2101 
2102  if ($num) {
2103  while ($i < $num) {
2104  $obj = $db->fetch_object($resql);
2105 
2106  $sql = "INSERT INTO ".MAIN_DB_PREFIX."co_exp (fk_expedition,fk_commande)";
2107  $sql .= " VALUES (".((int) $obj->rowid).", ".((int) $obj->fk_commande).")";
2108  $resql2 = $db->query($sql);
2109 
2110  if (!$resql2) {
2111  $error++;
2112  dol_print_error($db);
2113  }
2114  print ". ";
2115  $i++;
2116  }
2117  }
2118 
2119  if ($error == 0) {
2120  $db->commit();
2121  $sql = "ALTER TABLE ".MAIN_DB_PREFIX."expedition DROP COLUMN fk_commande";
2122  print $langs->trans('FieldRenamed')."<br>\n";
2123  $db->query($sql);
2124  } else {
2125  $db->rollback();
2126  }
2127  } else {
2128  dol_print_error($db);
2129  $db->rollback();
2130  }
2131  } else {
2132  print $langs->trans('AlreadyDone')."<br>\n";
2133  }
2134  print '</td></tr>';
2135 }
2136 
2145 function migrate_commande_livraison($db, $langs, $conf)
2146 {
2147  dolibarr_install_syslog("upgrade2::migrate_commande_livraison");
2148 
2149  print '<tr><td colspan="4">';
2150 
2151  print '<br>';
2152  print '<b>'.$langs->trans('MigrationDeliveryOrderMatching')."</b><br>\n";
2153 
2154  $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraison", "fk_commande");
2155  $obj = $db->fetch_object($result);
2156  if ($obj) {
2157  $error = 0;
2158 
2159  $db->begin();
2160 
2161  $sql = "SELECT l.rowid, l.fk_commande,";
2162  $sql .= " c.ref_client, c.date_livraison as delivery_date";
2163  $sql .= " FROM ".MAIN_DB_PREFIX."livraison as l, ".MAIN_DB_PREFIX."commande as c";
2164  $sql .= " WHERE c.rowid = l.fk_commande";
2165  $resql = $db->query($sql);
2166  if ($resql) {
2167  $i = 0;
2168  $num = $db->num_rows($resql);
2169 
2170  if ($num) {
2171  while ($i < $num) {
2172  $obj = $db->fetch_object($resql);
2173 
2174  $sql = "INSERT INTO ".MAIN_DB_PREFIX."co_liv (fk_livraison,fk_commande)";
2175  $sql .= " VALUES (".((int) $obj->rowid).", ".((int) $obj->fk_commande).")";
2176  $resql2 = $db->query($sql);
2177 
2178  if ($resql2) {
2179  $delivery_date = $db->jdate($obj->delivery_date);
2180 
2181  $sqlu = "UPDATE ".MAIN_DB_PREFIX."livraison SET";
2182  $sqlu .= " ref_client = '".$db->escape($obj->ref_client)."'";
2183  $sqlu .= ", date_livraison = '".$db->idate($delivery_date)."'";
2184  $sqlu .= " WHERE rowid = ".((int) $obj->rowid);
2185  $resql3 = $db->query($sqlu);
2186  if (!$resql3) {
2187  $error++;
2188  dol_print_error($db);
2189  }
2190  } else {
2191  $error++;
2192  dol_print_error($db);
2193  }
2194  print ". ";
2195  $i++;
2196  }
2197  }
2198 
2199  if ($error == 0) {
2200  $db->commit();
2201  $sql = "ALTER TABLE ".MAIN_DB_PREFIX."livraison DROP COLUMN fk_commande";
2202  print $langs->trans('FieldRenamed')."<br>\n";
2203  $db->query($sql);
2204  } else {
2205  $db->rollback();
2206  }
2207  } else {
2208  dol_print_error($db);
2209  $db->rollback();
2210  }
2211  } else {
2212  print $langs->trans('AlreadyDone')."<br>\n";
2213  }
2214  print '</td></tr>';
2215 }
2216 
2225 function migrate_detail_livraison($db, $langs, $conf)
2226 {
2227  dolibarr_install_syslog("upgrade2::migrate_detail_livraison");
2228 
2229  print '<tr><td colspan="4">';
2230 
2231  print '<br>';
2232  print '<b>'.$langs->trans('MigrationDeliveryDetail')."</b><br>\n";
2233 
2234  // This is done if field fk_commande_ligne exists.
2235  // If not this means migration was already done.
2236  $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraisondet", "fk_commande_ligne");
2237  $obj = $db->fetch_object($result);
2238  if ($obj) {
2239  $error = 0;
2240 
2241  $db->begin();
2242 
2243  $sql = "SELECT cd.rowid, cd.fk_product, cd.description, cd.subprice, cd.total_ht";
2244  $sql .= ", ld.fk_livraison";
2245  $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd, ".MAIN_DB_PREFIX."livraisondet as ld";
2246  $sql .= " WHERE ld.fk_commande_ligne = cd.rowid";
2247  $resql = $db->query($sql);
2248  if ($resql) {
2249  $i = 0;
2250  $num = $db->num_rows($resql);
2251 
2252  if ($num) {
2253  while ($i < $num) {
2254  $obj = $db->fetch_object($resql);
2255 
2256  $sql = "UPDATE ".MAIN_DB_PREFIX."livraisondet SET";
2257  $sql .= " fk_product = ".((int) $obj->fk_product);
2258  $sql .= ",description = '".$db->escape($obj->description)."'";
2259  $sql .= ",subprice = ".price2num($obj->subprice);
2260  $sql .= ",total_ht = ".price2num($obj->total_ht);
2261  $sql .= " WHERE fk_commande_ligne = ".((int) $obj->rowid);
2262  $resql2 = $db->query($sql);
2263 
2264  if ($resql2) {
2265  $sql = "SELECT total_ht";
2266  $sql .= " FROM ".MAIN_DB_PREFIX."livraison";
2267  $sql .= " WHERE rowid = ".((int) $obj->fk_livraison);
2268  $resql3 = $db->query($sql);
2269 
2270  if ($resql3) {
2271  $obju = $db->fetch_object($resql3);
2272  $total_ht = $obju->total_ht + $obj->total_ht;
2273 
2274  $sqlu = "UPDATE ".MAIN_DB_PREFIX."livraison SET";
2275  $sqlu .= " total_ht = ".price2num($total_ht, 'MT');
2276  $sqlu .= " WHERE rowid = ".((int) $obj->fk_livraison);
2277  $resql4 = $db->query($sqlu);
2278  if (!$resql4) {
2279  $error++;
2280  dol_print_error($db);
2281  }
2282  } else {
2283  $error++;
2284  dol_print_error($db);
2285  }
2286  } else {
2287  $error++;
2288  dol_print_error($db);
2289  }
2290  print ". ";
2291  $i++;
2292  }
2293  }
2294 
2295  if ($error == 0) {
2296  $db->commit();
2297  $sql = "ALTER TABLE ".MAIN_DB_PREFIX."livraisondet CHANGE fk_commande_ligne fk_origin_line integer";
2298  print $langs->trans('FieldRenamed')."<br>\n";
2299  $db->query($sql);
2300  } else {
2301  $db->rollback();
2302  }
2303  } else {
2304  dol_print_error($db);
2305  $db->rollback();
2306  }
2307  } else {
2308  $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraisondet", "fk_origin_line");
2309  $obj = $db->fetch_object($result);
2310  if (!$obj) {
2311  $sql = "ALTER TABLE ".MAIN_DB_PREFIX."livraisondet ADD COLUMN fk_origin_line integer after fk_livraison";
2312  $db->query($sql);
2313  }
2314  print $langs->trans('AlreadyDone')."<br>\n";
2315  }
2316  print '</td></tr>';
2317 }
2318 
2327 function migrate_stocks($db, $langs, $conf)
2328 {
2329  dolibarr_install_syslog("upgrade2::migrate_stocks");
2330 
2331  print '<tr><td colspan="4">';
2332 
2333  print '<br>';
2334  print '<b>'.$langs->trans('MigrationStockDetail')."</b><br>\n";
2335 
2336  $error = 0;
2337 
2338  $db->begin();
2339 
2340  $sql = "SELECT SUM(reel) as total, fk_product";
2341  $sql .= " FROM ".MAIN_DB_PREFIX."product_stock as ps";
2342  $sql .= " GROUP BY fk_product";
2343  $resql = $db->query($sql);
2344  if ($resql) {
2345  $i = 0;
2346  $num = $db->num_rows($resql);
2347 
2348  if ($num) {
2349  while ($i < $num) {
2350  $obj = $db->fetch_object($resql);
2351 
2352  $sql = "UPDATE ".MAIN_DB_PREFIX."product SET";
2353  $sql .= " stock = ".price2num($obj->total, 'MS');
2354  $sql .= " WHERE rowid = ".((int) $obj->fk_product);
2355 
2356  $resql2 = $db->query($sql);
2357  if ($resql2) {
2358  } else {
2359  $error++;
2360  dol_print_error($db);
2361  }
2362  print ". ";
2363  $i++;
2364  }
2365  }
2366 
2367  if ($error == 0) {
2368  $db->commit();
2369  } else {
2370  $db->rollback();
2371  }
2372  } else {
2373  dol_print_error($db);
2374  $db->rollback();
2375  }
2376 
2377  print '</td></tr>';
2378 }
2379 
2389 function migrate_menus($db, $langs, $conf)
2390 {
2391  dolibarr_install_syslog("upgrade2::migrate_menus");
2392 
2393  print '<tr><td colspan="4">';
2394 
2395  print '<br>';
2396  print '<b>'.$langs->trans('MigrationMenusDetail')."</b><br>\n";
2397 
2398  $error = 0;
2399 
2400  if ($db->DDLInfoTable(MAIN_DB_PREFIX."menu_constraint")) {
2401  $db->begin();
2402 
2403  $sql = "SELECT m.rowid, mc.action";
2404  $sql .= " FROM ".MAIN_DB_PREFIX."menu_constraint as mc, ".MAIN_DB_PREFIX."menu_const as md, ".MAIN_DB_PREFIX."menu as m";
2405  $sql .= " WHERE md.fk_menu = m.rowid AND md.fk_constraint = mc.rowid";
2406  $sql .= " AND m.enabled = '1'";
2407  $resql = $db->query($sql);
2408  if ($resql) {
2409  $i = 0;
2410  $num = $db->num_rows($resql);
2411  if ($num) {
2412  while ($i < $num) {
2413  $obj = $db->fetch_object($resql);
2414 
2415  $sql = "UPDATE ".MAIN_DB_PREFIX."menu SET";
2416  $sql .= " enabled = '".$db->escape($obj->action)."'";
2417  $sql .= " WHERE rowid = ".((int) $obj->rowid);
2418  $sql .= " AND enabled = '1'";
2419 
2420  $resql2 = $db->query($sql);
2421  if ($resql2) {
2422  } else {
2423  $error++;
2424  dol_print_error($db);
2425  }
2426  print ". ";
2427  $i++;
2428  }
2429  }
2430 
2431  if ($error == 0) {
2432  $db->commit();
2433  } else {
2434  $db->rollback();
2435  }
2436  } else {
2437  dol_print_error($db);
2438  $db->rollback();
2439  }
2440  } else {
2441  print $langs->trans('AlreadyDone')."<br>\n";
2442  }
2443 
2444  print '</td></tr>';
2445 }
2446 
2456 function migrate_commande_deliveryaddress($db, $langs, $conf)
2457 {
2458  dolibarr_install_syslog("upgrade2::migrate_commande_deliveryaddress");
2459 
2460  print '<tr><td colspan="4">';
2461 
2462  print '<br>';
2463  print '<b>'.$langs->trans('MigrationDeliveryAddress')."</b><br>\n";
2464 
2465  $error = 0;
2466 
2467  if ($db->DDLInfoTable(MAIN_DB_PREFIX."co_exp")) {
2468  $db->begin();
2469 
2470  $sql = "SELECT c.fk_adresse_livraison, ce.fk_expedition";
2471  $sql .= " FROM ".MAIN_DB_PREFIX."commande as c";
2472  $sql .= ", ".MAIN_DB_PREFIX."co_exp as ce";
2473  $sql .= " WHERE c.rowid = ce.fk_commande";
2474  $sql .= " AND c.fk_adresse_livraison IS NOT NULL AND c.fk_adresse_livraison != 0";
2475 
2476  $resql = $db->query($sql);
2477  if ($resql) {
2478  $i = 0;
2479  $num = $db->num_rows($resql);
2480 
2481  if ($num) {
2482  while ($i < $num) {
2483  $obj = $db->fetch_object($resql);
2484 
2485  $sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET";
2486  $sql .= " fk_adresse_livraison = '".$db->escape($obj->fk_adresse_livraison)."'";
2487  $sql .= " WHERE rowid = ".((int) $obj->fk_expedition);
2488 
2489  $resql2 = $db->query($sql);
2490  if (!$resql2) {
2491  $error++;
2492  dol_print_error($db);
2493  }
2494  print ". ";
2495  $i++;
2496  }
2497  } else {
2498  print $langs->trans('AlreadyDone')."<br>\n";
2499  }
2500 
2501  if ($error == 0) {
2502  $db->commit();
2503  } else {
2504  $db->rollback();
2505  }
2506  } else {
2507  dol_print_error($db);
2508  $db->rollback();
2509  }
2510  } else {
2511  print $langs->trans('AlreadyDone')."<br>\n";
2512  }
2513 
2514  print '</td></tr>';
2515 }
2516 
2526 function migrate_restore_missing_links($db, $langs, $conf)
2527 {
2528  dolibarr_install_syslog("upgrade2::migrate_restore_missing_links");
2529 
2530  if (($db->type == 'mysql' || $db->type == 'mysqli')) {
2531  if (versioncompare($db->getVersionArray(), array(4, 0)) < 0) {
2532  dolibarr_install_syslog("upgrade2::migrate_restore_missing_links Version of database too old to make this migrate action");
2533  return 0;
2534  }
2535  }
2536  print '<tr><td colspan="4">';
2537 
2538  print '<br>';
2539  print '<b>'.$langs->trans('MigrationFixData')."</b> (1)<br>\n";
2540 
2541  $error = 0;
2542 
2543 
2544  // Restore missing link for this cross foreign key (link 1 <=> 1). Direction 1.
2545  $table1 = 'facturedet'; $field1 = 'fk_remise_except';
2546  $table2 = 'societe_remise_except'; $field2 = 'fk_facture_line';
2547 
2548  $db->begin();
2549 
2550  $sql = "SELECT t1.rowid, t1.".$field1." as field";
2551  $sql .= " FROM ".MAIN_DB_PREFIX.$table1." as t1";
2552  $sql .= " WHERE t1.".$field1." IS NOT NULL AND t1.".$field1." NOT IN";
2553  $sql .= " (SELECT t2.rowid FROM ".MAIN_DB_PREFIX.$table2." as t2";
2554  $sql .= " WHERE t1.rowid = t2.".$field2.")";
2555 
2556  dolibarr_install_syslog("upgrade2::migrate_restore_missing_links DIRECTION 1");
2557  $resql = $db->query($sql);
2558  if ($resql) {
2559  $i = 0;
2560  $num = $db->num_rows($resql);
2561 
2562  if ($num) {
2563  while ($i < $num) {
2564  $obj = $db->fetch_object($resql);
2565 
2566  print 'Line '.$obj->rowid.' in '.$table1.' is linked to record '.$obj->field.' in '.$table2.' that has no link to '.$table1.'. We fix this.<br>';
2567  $sql = "UPDATE ".MAIN_DB_PREFIX.$table2." SET";
2568  $sql .= " ".$field2." = '".$db->escape($obj->rowid)."'";
2569  $sql .= " WHERE rowid = ".((int) $obj->field);
2570 
2571  $resql2 = $db->query($sql);
2572  if (!$resql2) {
2573  $error++;
2574  dol_print_error($db);
2575  }
2576  //print ". ";
2577  $i++;
2578  }
2579  } else {
2580  print $langs->trans('AlreadyDone')."<br>\n";
2581  }
2582 
2583  if ($error == 0) {
2584  $db->commit();
2585  } else {
2586  $db->rollback();
2587  }
2588  } else {
2589  dol_print_error($db);
2590  $db->rollback();
2591  }
2592 
2593  print '</td></tr>';
2594 
2595 
2596  print '<tr><td colspan="4">';
2597 
2598  print '<br>';
2599  print '<b>'.$langs->trans('MigrationFixData')."</b> (2)<br>\n";
2600 
2601  // Restore missing link for this cross foreign key (link 1 <=> 1). Direction 2.
2602  $table2 = 'facturedet'; $field2 = 'fk_remise_except';
2603  $table1 = 'societe_remise_except'; $field1 = 'fk_facture_line';
2604 
2605  $db->begin();
2606 
2607  $sql = "SELECT t1.rowid, t1.".$field1." as field";
2608  $sql .= " FROM ".MAIN_DB_PREFIX.$table1." as t1";
2609  $sql .= " WHERE t1.".$field1." IS NOT NULL AND t1.".$field1." NOT IN";
2610  $sql .= " (SELECT t2.rowid FROM ".MAIN_DB_PREFIX.$table2." as t2";
2611  $sql .= " WHERE t1.rowid = t2.".$field2.")";
2612 
2613  dolibarr_install_syslog("upgrade2::migrate_restore_missing_links DIRECTION 2");
2614  $resql = $db->query($sql);
2615  if ($resql) {
2616  $i = 0;
2617  $num = $db->num_rows($resql);
2618 
2619  if ($num) {
2620  while ($i < $num) {
2621  $obj = $db->fetch_object($resql);
2622 
2623  print 'Line '.$obj->rowid.' in '.$table1.' is linked to record '.$obj->field.' in '.$table2.' that has no link to '.$table1.'. We fix this.<br>';
2624  $sql = "UPDATE ".MAIN_DB_PREFIX.$table2." SET";
2625  $sql .= " ".$field2." = '".$db->escape($obj->rowid)."'";
2626  $sql .= " WHERE rowid = ".((int) $obj->field);
2627 
2628  $resql2 = $db->query($sql);
2629  if (!$resql2) {
2630  $error++;
2631  dol_print_error($db);
2632  }
2633  //print ". ";
2634  $i++;
2635  }
2636  } else {
2637  print $langs->trans('AlreadyDone')."<br>\n";
2638  }
2639 
2640  if ($error == 0) {
2641  $db->commit();
2642  } else {
2643  $db->rollback();
2644  }
2645  } else {
2646  dol_print_error($db);
2647  $db->rollback();
2648  }
2649 
2650  print '</td></tr>';
2651 }
2652 
2661 function migrate_project_user_resp($db, $langs, $conf)
2662 {
2663  dolibarr_install_syslog("upgrade2::migrate_project_user_resp");
2664 
2665  print '<tr><td colspan="4">';
2666 
2667  print '<br>';
2668  print '<b>'.$langs->trans('MigrationProjectUserResp')."</b><br>\n";
2669 
2670  $result = $db->DDLDescTable(MAIN_DB_PREFIX."projet", "fk_user_resp");
2671  $obj = $db->fetch_object($result);
2672  if ($obj) {
2673  $error = 0;
2674 
2675  $db->begin();
2676 
2677  $sql = "SELECT rowid, fk_user_resp FROM ".MAIN_DB_PREFIX."projet";
2678  $resql = $db->query($sql);
2679  if ($resql) {
2680  $i = 0;
2681  $num = $db->num_rows($resql);
2682 
2683  if ($num) {
2684  while ($i < $num) {
2685  $obj = $db->fetch_object($resql);
2686 
2687  $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."element_contact (";
2688  $sql2 .= "datecreate";
2689  $sql2 .= ", statut";
2690  $sql2 .= ", element_id";
2691  $sql2 .= ", fk_c_type_contact";
2692  $sql2 .= ", fk_socpeople";
2693  $sql2 .= ") VALUES (";
2694  $sql2 .= "'".$db->idate(dol_now())."'";
2695  $sql2 .= ", '4'";
2696  $sql2 .= ", ".$obj->rowid;
2697  $sql2 .= ", '160'";
2698  $sql2 .= ", ".$obj->fk_user_resp;
2699  $sql2 .= ")";
2700 
2701  if ($obj->fk_user_resp > 0) {
2702  $resql2 = $db->query($sql2);
2703  if (!$resql2) {
2704  $error++;
2705  dol_print_error($db);
2706  }
2707  }
2708  print ". ";
2709 
2710  $i++;
2711  }
2712  }
2713 
2714  if ($error == 0) {
2715  $sqlDrop = "ALTER TABLE ".MAIN_DB_PREFIX."projet DROP COLUMN fk_user_resp";
2716  if ($db->query($sqlDrop)) {
2717  $db->commit();
2718  } else {
2719  $db->rollback();
2720  }
2721  } else {
2722  $db->rollback();
2723  }
2724  } else {
2725  dol_print_error($db);
2726  $db->rollback();
2727  }
2728  } else {
2729  print $langs->trans('AlreadyDone')."<br>\n";
2730  }
2731  print '</td></tr>';
2732 }
2733 
2742 function migrate_project_task_actors($db, $langs, $conf)
2743 {
2744  dolibarr_install_syslog("upgrade2::migrate_project_task_actors");
2745 
2746  print '<tr><td colspan="4">';
2747 
2748  print '<br>';
2749  print '<b>'.$langs->trans('MigrationProjectTaskActors')."</b><br>\n";
2750 
2751  if ($db->DDLInfoTable(MAIN_DB_PREFIX."projet_task_actors")) {
2752  $error = 0;
2753 
2754  $db->begin();
2755 
2756  $sql = "SELECT fk_projet_task as fk_project_task, fk_user FROM ".MAIN_DB_PREFIX."projet_task_actors";
2757  $resql = $db->query($sql);
2758  if ($resql) {
2759  $i = 0;
2760  $num = $db->num_rows($resql);
2761 
2762  if ($num) {
2763  while ($i < $num) {
2764  $obj = $db->fetch_object($resql);
2765 
2766  $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."element_contact (";
2767  $sql2 .= "datecreate";
2768  $sql2 .= ", statut";
2769  $sql2 .= ", element_id";
2770  $sql2 .= ", fk_c_type_contact";
2771  $sql2 .= ", fk_socpeople";
2772  $sql2 .= ") VALUES (";
2773  $sql2 .= "'".$db->idate(dol_now())."'";
2774  $sql2 .= ", '4'";
2775  $sql2 .= ", ".$obj->fk_project_task;
2776  $sql2 .= ", '180'";
2777  $sql2 .= ", ".$obj->fk_user;
2778  $sql2 .= ")";
2779 
2780  $resql2 = $db->query($sql2);
2781 
2782  if (!$resql2) {
2783  $error++;
2784  dol_print_error($db);
2785  }
2786  print ". ";
2787  $i++;
2788  }
2789  }
2790 
2791  if ($error == 0) {
2792  $sqlDrop = "DROP TABLE ".MAIN_DB_PREFIX."projet_task_actors";
2793  if ($db->query($sqlDrop)) {
2794  $db->commit();
2795  } else {
2796  $db->rollback();
2797  }
2798  } else {
2799  $db->rollback();
2800  }
2801  } else {
2802  dol_print_error($db);
2803  $db->rollback();
2804  }
2805  } else {
2806  print $langs->trans('AlreadyDone')."<br>\n";
2807  }
2808  print '</td></tr>';
2809 }
2810 
2824 function migrate_relationship_tables($db, $langs, $conf, $table, $fk_source, $sourcetype, $fk_target, $targettype)
2825 {
2826  print '<tr><td colspan="4">';
2827 
2828  print '<br>';
2829  print '<b>'.$langs->trans('MigrationRelationshipTables', MAIN_DB_PREFIX.$table)."</b><br>\n";
2830 
2831  $error = 0;
2832 
2833  if ($db->DDLInfoTable(MAIN_DB_PREFIX.$table)) {
2834  dolibarr_install_syslog("upgrade2::migrate_relationship_tables table = ".MAIN_DB_PREFIX.$table);
2835 
2836  $db->begin();
2837 
2838  $sqlSelect = "SELECT ".$fk_source.", ".$fk_target;
2839  $sqlSelect .= " FROM ".MAIN_DB_PREFIX.$table;
2840 
2841  $resql = $db->query($sqlSelect);
2842  if ($resql) {
2843  $i = 0;
2844  $num = $db->num_rows($resql);
2845 
2846  if ($num) {
2847  while ($i < $num) {
2848  $obj = $db->fetch_object($resql);
2849 
2850  $sqlInsert = "INSERT INTO ".MAIN_DB_PREFIX."element_element (";
2851  $sqlInsert .= "fk_source";
2852  $sqlInsert .= ", sourcetype";
2853  $sqlInsert .= ", fk_target";
2854  $sqlInsert .= ", targettype";
2855  $sqlInsert .= ") VALUES (";
2856  $sqlInsert .= $obj->$fk_source;
2857  $sqlInsert .= ", '".$db->escape($sourcetype)."'";
2858  $sqlInsert .= ", ".$obj->$fk_target;
2859  $sqlInsert .= ", '".$db->escape($targettype)."'";
2860  $sqlInsert .= ")";
2861 
2862  $result = $db->query($sqlInsert);
2863  if (!$result) {
2864  $error++;
2865  dol_print_error($db);
2866  }
2867  print ". ";
2868  $i++;
2869  }
2870  } else {
2871  print $langs->trans('AlreadyDone')."<br>\n";
2872  }
2873 
2874  if ($error == 0) {
2875  $sqlDrop = "DROP TABLE ".MAIN_DB_PREFIX.$table;
2876  if ($db->query($sqlDrop)) {
2877  $db->commit();
2878  } else {
2879  $db->rollback();
2880  }
2881  } else {
2882  $db->rollback();
2883  }
2884  } else {
2885  dol_print_error($db);
2886  $db->rollback();
2887  }
2888  } else {
2889  print $langs->trans('AlreadyDone')."<br>\n";
2890  }
2891 
2892  print '</td></tr>';
2893 }
2894 
2903 function migrate_project_task_time($db, $langs, $conf)
2904 {
2905  dolibarr_install_syslog("upgrade2::migrate_project_task_time");
2906 
2907  print '<tr><td colspan="4">';
2908 
2909  print '<br>';
2910  print '<b>'.$langs->trans('MigrationProjectTaskTime')."</b><br>\n";
2911 
2912  $error = 0;
2913 
2914  $db->begin();
2915 
2916  $sql = "SELECT rowid, fk_task, task_duration";
2917  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time";
2918  $resql = $db->query($sql);
2919  if ($resql) {
2920  $i = 0;
2921  $num = $db->num_rows($resql);
2922 
2923  if ($num) {
2924  $totaltime = array();
2925  $oldtime = 0;
2926 
2927  while ($i < $num) {
2928  $obj = $db->fetch_object($resql);
2929 
2930  if ($obj->task_duration > 0) {
2931  // convert to second
2932  // only for int time and float time ex: 1,75 for 1h45
2933  list($hour, $min) = explode('.', $obj->task_duration);
2934  $hour = $hour * 60 * 60;
2935  $min = ($min / 100) * 60 * 60;
2936  $newtime = $hour + $min;
2937 
2938  $sql2 = "UPDATE ".MAIN_DB_PREFIX."projet_task_time SET";
2939  $sql2 .= " task_duration = ".((int) $newtime);
2940  $sql2 .= " WHERE rowid = ".((int) $obj->rowid);
2941 
2942  $resql2 = $db->query($sql2);
2943  if (!$resql2) {
2944  $error++;
2945  dol_print_error($db);
2946  }
2947  print ". ";
2948  $oldtime++;
2949  if (!empty($totaltime[$obj->fk_task])) {
2950  $totaltime[$obj->fk_task] += $newtime;
2951  } else {
2952  $totaltime[$obj->fk_task] = $newtime;
2953  }
2954  } else {
2955  if (!empty($totaltime[$obj->fk_task])) {
2956  $totaltime[$obj->fk_task] += $obj->task_duration;
2957  } else {
2958  $totaltime[$obj->fk_task] = $obj->task_duration;
2959  }
2960  }
2961 
2962  $i++;
2963  }
2964 
2965  if ($error == 0) {
2966  if ($oldtime > 0) {
2967  foreach ($totaltime as $taskid => $total_duration) {
2968  $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task SET";
2969  $sql .= " duration_effective = ".((int) $total_duration);
2970  $sql .= " WHERE rowid = ".((int) $taskid);
2971 
2972  $resql = $db->query($sql);
2973  if (!$resql) {
2974  $error++;
2975  dol_print_error($db);
2976  }
2977  }
2978  } else {
2979  print $langs->trans('AlreadyDone')."<br>\n";
2980  }
2981  } else {
2982  dol_print_error($db);
2983  }
2984  } else {
2985  print $langs->trans('AlreadyDone')."<br>\n";
2986  }
2987  } else {
2988  dol_print_error($db);
2989  }
2990 
2991  if ($error == 0) {
2992  $db->commit();
2993  } else {
2994  $db->rollback();
2995  }
2996 
2997  print '</td></tr>';
2998 }
2999 
3008 function migrate_customerorder_shipping($db, $langs, $conf)
3009 {
3010  print '<tr><td colspan="4">';
3011 
3012  print '<br>';
3013  print '<b>'.$langs->trans('MigrationCustomerOrderShipping')."</b><br>\n";
3014 
3015  $error = 0;
3016 
3017  $result1 = $db->DDLDescTable(MAIN_DB_PREFIX."expedition", "ref_customer");
3018  $result2 = $db->DDLDescTable(MAIN_DB_PREFIX."expedition", "date_delivery");
3019  $obj1 = $db->fetch_object($result1);
3020  $obj2 = $db->fetch_object($result2);
3021  if (!$obj1 && !$obj2) {
3022  dolibarr_install_syslog("upgrade2::migrate_customerorder_shipping");
3023 
3024  $db->begin();
3025 
3026  $sqlAdd1 = "ALTER TABLE ".MAIN_DB_PREFIX."expedition ADD COLUMN ref_customer varchar(30) AFTER entity";
3027  $sqlAdd2 = "ALTER TABLE ".MAIN_DB_PREFIX."expedition ADD COLUMN date_delivery date DEFAULT NULL AFTER date_expedition";
3028 
3029  if ($db->query($sqlAdd1) && $db->query($sqlAdd2)) {
3030  $sqlSelect = "SELECT e.rowid as shipping_id, c.ref_client, c.date_livraison as delivery_date";
3031  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."expedition as e";
3032  $sqlSelect .= ", ".MAIN_DB_PREFIX."element_element as el";
3033  $sqlSelect .= " LEFT JOIN ".MAIN_DB_PREFIX."commande as c ON c.rowid = el.fk_source AND el.sourcetype = 'commande'";
3034  $sqlSelect .= " WHERE e.rowid = el.fk_target";
3035  $sqlSelect .= " AND el.targettype = 'shipping'";
3036 
3037  $resql = $db->query($sqlSelect);
3038  if ($resql) {
3039  $i = 0;
3040  $num = $db->num_rows($resql);
3041 
3042  if ($num) {
3043  while ($i < $num) {
3044  $obj = $db->fetch_object($resql);
3045 
3046  $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."expedition SET";
3047  $sqlUpdate .= " ref_customer = '".$db->escape($obj->ref_client)."'";
3048  $sqlUpdate .= ", date_delivery = '".$db->escape($obj->delivery_date ? $obj->delivery_date : 'null')."'";
3049  $sqlUpdate .= " WHERE rowid = ".((int) $obj->shipping_id);
3050 
3051  $result = $db->query($sqlUpdate);
3052  if (!$result) {
3053  $error++;
3054  dol_print_error($db);
3055  }
3056  print ". ";
3057  $i++;
3058  }
3059  } else {
3060  print $langs->trans('AlreadyDone')."<br>\n";
3061  }
3062 
3063  if ($error == 0) {
3064  $db->commit();
3065  } else {
3066  dol_print_error($db);
3067  $db->rollback();
3068  }
3069  } else {
3070  dol_print_error($db);
3071  $db->rollback();
3072  }
3073  } else {
3074  dol_print_error($db);
3075  $db->rollback();
3076  }
3077  } else {
3078  print $langs->trans('AlreadyDone')."<br>\n";
3079  }
3080 
3081  print '</td></tr>';
3082 }
3083 
3092 function migrate_shipping_delivery($db, $langs, $conf)
3093 {
3094  print '<tr><td colspan="4">';
3095 
3096  print '<br>';
3097  print '<b>'.$langs->trans('MigrationShippingDelivery')."</b><br>\n";
3098 
3099  $error = 0;
3100 
3101  $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraison", "fk_expedition");
3102  $obj = $db->fetch_object($result);
3103  if ($obj) {
3104  dolibarr_install_syslog("upgrade2::migrate_shipping_delivery");
3105 
3106  $db->begin();
3107 
3108  $sqlSelect = "SELECT rowid, fk_expedition";
3109  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."livraison";
3110  $sqlSelect .= " WHERE fk_expedition is not null";
3111 
3112  $resql = $db->query($sqlSelect);
3113  if ($resql) {
3114  $i = 0;
3115  $num = $db->num_rows($resql);
3116 
3117  if ($num) {
3118  while ($i < $num) {
3119  $obj = $db->fetch_object($resql);
3120 
3121  $sqlInsert = "INSERT INTO ".MAIN_DB_PREFIX."element_element (";
3122  $sqlInsert .= "fk_source";
3123  $sqlInsert .= ", sourcetype";
3124  $sqlInsert .= ", fk_target";
3125  $sqlInsert .= ", targettype";
3126  $sqlInsert .= ") VALUES (";
3127  $sqlInsert .= $obj->fk_expedition;
3128  $sqlInsert .= ", 'shipping'";
3129  $sqlInsert .= ", ".$obj->rowid;
3130  $sqlInsert .= ", 'delivery'";
3131  $sqlInsert .= ")";
3132 
3133  $result = $db->query($sqlInsert);
3134  if ($result) {
3135  $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."livraison SET fk_expedition = NULL";
3136  $sqlUpdate .= " WHERE rowid = ".((int) $obj->rowid);
3137 
3138  $result = $db->query($sqlUpdate);
3139  if (!$result) {
3140  $error++;
3141  dol_print_error($db);
3142  }
3143  print ". ";
3144  } else {
3145  $error++;
3146  dol_print_error($db);
3147  }
3148  $i++;
3149  }
3150  } else {
3151  print $langs->trans('AlreadyDone')."<br>\n";
3152  }
3153 
3154  if ($error == 0) {
3155  $sqlDelete = "DELETE FROM ".MAIN_DB_PREFIX."element_element WHERE sourcetype = 'commande' AND targettype = 'delivery'";
3156  $db->query($sqlDelete);
3157 
3158  $db->commit();
3159 
3160  // DDL commands must not be inside a transaction
3161  $sqlDrop = "ALTER TABLE ".MAIN_DB_PREFIX."livraison DROP COLUMN fk_expedition";
3162  $db->query($sqlDrop);
3163  } else {
3164  dol_print_error($db);
3165  $db->rollback();
3166  }
3167  } else {
3168  dol_print_error($db);
3169  $db->rollback();
3170  }
3171  } else {
3172  print $langs->trans('AlreadyDone')."<br>\n";
3173  }
3174 
3175  print '</td></tr>';
3176 }
3177 
3187 function migrate_shipping_delivery2($db, $langs, $conf)
3188 {
3189  print '<tr><td colspan="4">';
3190 
3191  print '<br>';
3192  print '<b>'.$langs->trans('MigrationShippingDelivery2')."</b><br>\n";
3193 
3194  $error = 0;
3195 
3196  dolibarr_install_syslog("upgrade2::migrate_shipping_delivery2");
3197 
3198  $db->begin();
3199 
3200  $sqlSelect = "SELECT l.rowid as delivery_id, e.ref_customer, e.date_delivery";
3201  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."livraison as l,";
3202  $sqlSelect .= " ".MAIN_DB_PREFIX."element_element as el,";
3203  $sqlSelect .= " ".MAIN_DB_PREFIX."expedition as e";
3204  $sqlSelect .= " WHERE l.rowid = el.fk_target";
3205  $sqlSelect .= " AND el.targettype = 'delivery'";
3206  $sqlSelect .= " AND e.rowid = el.fk_source AND el.sourcetype = 'shipping'";
3207  $sqlSelect .= " AND (e.ref_customer IS NOT NULL OR e.date_delivery IS NOT NULL)"; // Useless to process this record if both are null
3208  // Add condition to know if we never migrate this record
3209  $sqlSelect .= " AND (l.ref_customer IS NULL".($db->type != 'pgsql' ? " or l.ref_customer = ''" : "").")";
3210  $sqlSelect .= " AND (l.date_delivery IS NULL".($db->type != 'pgsql' ? " or l.date_delivery = ''" : "").")";
3211 
3212  $resql = $db->query($sqlSelect);
3213  if ($resql) {
3214  $i = 0;
3215  $num = $db->num_rows($resql);
3216 
3217  if ($num) {
3218  while ($i < $num) {
3219  $obj = $db->fetch_object($resql);
3220 
3221  $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."livraison SET";
3222  $sqlUpdate .= " ref_customer = '".$db->escape($obj->ref_customer)."',";
3223  $sqlUpdate .= " date_delivery = ".($obj->date_delivery ? "'".$db->escape($obj->date_delivery)."'" : 'null');
3224  $sqlUpdate .= " WHERE rowid = ".((int) $obj->delivery_id);
3225 
3226  $result = $db->query($sqlUpdate);
3227  if (!$result) {
3228  $error++;
3229  dol_print_error($db);
3230  }
3231  print ". ";
3232  $i++;
3233  }
3234  } else {
3235  print $langs->trans('AlreadyDone')."<br>\n";
3236  }
3237 
3238  if ($error == 0) {
3239  $db->commit();
3240  } else {
3241  dol_print_error($db);
3242  $db->rollback();
3243  }
3244  } else {
3245  dol_print_error($db);
3246  $db->rollback();
3247  }
3248 
3249  print '</td></tr>';
3250 }
3251 
3260 function migrate_actioncomm_element($db, $langs, $conf)
3261 {
3262  print '<tr><td colspan="4">';
3263 
3264  print '<br>';
3265  print '<b>'.$langs->trans('MigrationActioncommElement')."</b><br>\n";
3266 
3267  $elements = array(
3268  'propal' => 'propalrowid',
3269  'order' => 'fk_commande',
3270  'invoice' => 'fk_facture',
3271  'contract' => 'fk_contract',
3272  'order_supplier' => 'fk_supplier_order',
3273  'invoice_supplier' => 'fk_supplier_invoice'
3274  );
3275 
3276  foreach ($elements as $type => $field) {
3277  $result = $db->DDLDescTable(MAIN_DB_PREFIX."actioncomm", $field);
3278  $obj = $db->fetch_object($result);
3279  if ($obj) {
3280  dolibarr_install_syslog("upgrade2::migrate_actioncomm_element field=".$field);
3281 
3282  $db->begin();
3283 
3284  $sql = "UPDATE ".MAIN_DB_PREFIX."actioncomm SET ";
3285  $sql .= "fk_element = ".$field.", elementtype = '".$db->escape($type)."'";
3286  $sql .= " WHERE ".$field." IS NOT NULL";
3287  $sql .= " AND fk_element IS NULL";
3288  $sql .= " AND elementtype IS NULL";
3289 
3290  $resql = $db->query($sql);
3291  if ($resql) {
3292  $db->commit();
3293 
3294  // DDL commands must not be inside a transaction
3295  // We will drop at next version because a migrate should be runnable several times if it fails.
3296  //$sqlDrop = "ALTER TABLE ".MAIN_DB_PREFIX."actioncomm DROP COLUMN ".$field;
3297  //$db->query($sqlDrop);
3298  //print ". ";
3299  } else {
3300  dol_print_error($db);
3301  $db->rollback();
3302  }
3303  } else {
3304  print $langs->trans('AlreadyDone')."<br>\n";
3305  }
3306  }
3307 
3308  print '</td></tr>';
3309 }
3310 
3319 function migrate_mode_reglement($db, $langs, $conf)
3320 {
3321  print '<tr><td colspan="4">';
3322 
3323  print '<br>';
3324  print '<b>'.$langs->trans('MigrationPaymentMode')."</b><br>\n";
3325 
3326  $elements = array(
3327  'old_id' => array(5, 8, 9, 10, 11),
3328  'new_id' => array(50, 51, 52, 53, 54),
3329  'code' => array('VAD', 'TRA', 'LCR', 'FAC', 'PRO'),
3330  'tables' => array('commande_fournisseur', 'commande', 'facture_rec', 'facture', 'propal')
3331  );
3332  $count = 0;
3333 
3334  foreach ($elements['old_id'] as $key => $old_id) {
3335  $error = 0;
3336 
3337  dolibarr_install_syslog("upgrade2::migrate_mode_reglement code=".$elements['code'][$key]);
3338 
3339  $sqlSelect = "SELECT id";
3340  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."c_paiement";
3341  $sqlSelect .= " WHERE id = ".((int) $old_id);
3342  $sqlSelect .= " AND code = '".$db->escape($elements['code'][$key])."'";
3343 
3344  $resql = $db->query($sqlSelect);
3345  if ($resql) {
3346  $num = $db->num_rows($resql);
3347  if ($num) {
3348  $count++;
3349 
3350  $db->begin();
3351 
3352  $sqla = "UPDATE ".MAIN_DB_PREFIX."paiement SET";
3353  $sqla .= " fk_paiement = ".((int) $elements['new_id'][$key]);
3354  $sqla .= " WHERE fk_paiement = ".((int) $old_id);
3355  $sqla .= " AND fk_paiement IN (SELECT id FROM ".MAIN_DB_PREFIX."c_paiement WHERE id = ".((int) $old_id)." AND code = '".$db->escape($elements['code'][$key])."')";
3356  $resqla = $db->query($sqla);
3357 
3358  $sql = "UPDATE ".MAIN_DB_PREFIX."c_paiement SET";
3359  $sql .= " id = ".((int) $elements['new_id'][$key]);
3360  $sql .= " WHERE id = ".((int) $old_id);
3361  $sql .= " AND code = '".$db->escape($elements['code'][$key])."'";
3362  $resql = $db->query($sql);
3363 
3364  if ($resqla && $resql) {
3365  foreach ($elements['tables'] as $table) {
3366  $sql = "UPDATE ".MAIN_DB_PREFIX.$table." SET ";
3367  $sql .= "fk_mode_reglement = ".((int) $elements['new_id'][$key]);
3368  $sql .= " WHERE fk_mode_reglement = ".((int) $old_id);
3369 
3370  $resql = $db->query($sql);
3371  if (!$resql) {
3372  dol_print_error($db);
3373  $error++;
3374  }
3375  print ". ";
3376  }
3377 
3378  if (!$error) {
3379  $db->commit();
3380  } else {
3381  dol_print_error($db);
3382  $db->rollback();
3383  }
3384  } else {
3385  dol_print_error($db);
3386  $db->rollback();
3387  }
3388  }
3389  }
3390  }
3391 
3392  if ($count == 0) {
3393  print $langs->trans('AlreadyDone')."<br>\n";
3394  }
3395 
3396 
3397  print '</td></tr>';
3398 }
3399 
3400 
3409 function migrate_clean_association($db, $langs, $conf)
3410 {
3411  $result = $db->DDLDescTable(MAIN_DB_PREFIX."categorie_association");
3412  if ($result) { // result defined for version 3.2 or -
3413  $obj = $db->fetch_object($result);
3414  if ($obj) { // It table categorie_association exists
3415  $couples = array();
3416  $filles = array();
3417  $sql = "SELECT fk_categorie_mere, fk_categorie_fille";
3418  $sql .= " FROM ".MAIN_DB_PREFIX."categorie_association";
3419  dolibarr_install_syslog("upgrade: search duplicate");
3420  $resql = $db->query($sql);
3421  if ($resql) {
3422  $num = $db->num_rows($resql);
3423  while ($obj = $db->fetch_object($resql)) {
3424  if (!isset($filles[$obj->fk_categorie_fille])) { // Only one record as child (a child has only on parent).
3425  if ($obj->fk_categorie_mere != $obj->fk_categorie_fille) {
3426  $filles[$obj->fk_categorie_fille] = 1; // Set record for this child
3427  $couples[$obj->fk_categorie_mere.'_'.$obj->fk_categorie_fille] = array('mere'=>$obj->fk_categorie_mere, 'fille'=>$obj->fk_categorie_fille);
3428  }
3429  }
3430  }
3431 
3432  dolibarr_install_syslog("upgrade: result is num=".$num." count(couples)=".count($couples));
3433 
3434  // If there is duplicates couples or child with two parents
3435  if (count($couples) > 0 && $num > count($couples)) {
3436  $error = 0;
3437 
3438  $db->begin();
3439 
3440  // We delete all
3441  $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_association";
3442  dolibarr_install_syslog("upgrade: delete association");
3443  $resqld = $db->query($sql);
3444  if ($resqld) {
3445  // And we insert only each record once
3446  foreach ($couples as $key => $val) {
3447  $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie_association(fk_categorie_mere,fk_categorie_fille)";
3448  $sql .= " VALUES(".((int) $val['mere']).", ".((int) $val['fille']).")";
3449  dolibarr_install_syslog("upgrade: insert association");
3450  $resqli = $db->query($sql);
3451  if (!$resqli) {
3452  $error++;
3453  }
3454  }
3455  }
3456 
3457  if (!$error) {
3458  print '<tr><td>'.$langs->trans("MigrationCategorieAssociation").'</td>';
3459  print '<td class="right">'.$langs->trans("RemoveDuplicates").' '.$langs->trans("Success").' ('.$num.'=>'.count($couples).')</td></tr>';
3460  $db->commit();
3461  } else {
3462  print '<tr><td>'.$langs->trans("MigrationCategorieAssociation").'</td>';
3463  print '<td class="right">'.$langs->trans("RemoveDuplicates").' '.$langs->trans("Failed").'</td></tr>';
3464  $db->rollback();
3465  }
3466  }
3467  } else {
3468  print '<tr><td>'.$langs->trans("Error").'</td>';
3469  print '<td class="right"><div class="error">'.$db->lasterror().'</div></td></tr>';
3470  }
3471  }
3472  }
3473 }
3474 
3475 
3484 function migrate_categorie_association($db, $langs, $conf)
3485 {
3486  print '<tr><td colspan="4">';
3487 
3488  print '<br>';
3489  print '<b>'.$langs->trans('MigrationCategorieAssociation')."</b><br>\n";
3490 
3491  $error = 0;
3492 
3493  if ($db->DDLInfoTable(MAIN_DB_PREFIX."categorie_association")) {
3494  dolibarr_install_syslog("upgrade2::migrate_categorie_association");
3495 
3496  $db->begin();
3497 
3498  $sqlSelect = "SELECT fk_categorie_mere, fk_categorie_fille";
3499  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."categorie_association";
3500 
3501  $resql = $db->query($sqlSelect);
3502  if ($resql) {
3503  $i = 0;
3504  $num = $db->num_rows($resql);
3505 
3506  if ($num) {
3507  while ($i < $num) {
3508  $obj = $db->fetch_object($resql);
3509 
3510  $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."categorie SET ";
3511  $sqlUpdate .= "fk_parent = ".((int) $obj->fk_categorie_mere);
3512  $sqlUpdate .= " WHERE rowid = ".((int) $obj->fk_categorie_fille);
3513 
3514  $result = $db->query($sqlUpdate);
3515  if (!$result) {
3516  $error++;
3517  dol_print_error($db);
3518  }
3519  print ". ";
3520  $i++;
3521  }
3522  } else {
3523  print $langs->trans('AlreadyDone')."<br>\n";
3524  }
3525 
3526  if (!$error) {
3527  $db->commit();
3528  } else {
3529  $db->rollback();
3530  }
3531  } else {
3532  dol_print_error($db);
3533  $db->rollback();
3534  }
3535  } else {
3536  print $langs->trans('AlreadyDone')."<br>\n";
3537  }
3538 
3539  print '</td></tr>';
3540 }
3541 
3550 function migrate_event_assignement($db, $langs, $conf)
3551 {
3552  print '<tr><td colspan="4">';
3553 
3554  print '<br>';
3555  print '<b>'.$langs->trans('MigrationEvents')."</b><br>\n";
3556 
3557  $error = 0;
3558 
3559  dolibarr_install_syslog("upgrade2::migrate_event_assignement");
3560 
3561  $db->begin();
3562 
3563  $sqlSelect = "SELECT a.id, a.fk_user_action";
3564  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."actioncomm as a";
3565  $sqlSelect .= " LEFT JOIN ".MAIN_DB_PREFIX."actioncomm_resources as ar ON ar.fk_actioncomm = a.id AND ar.element_type = 'user' AND ar.fk_element = a.fk_user_action";
3566  $sqlSelect .= " WHERE fk_user_action > 0 AND fk_user_action NOT IN (SELECT fk_element FROM ".MAIN_DB_PREFIX."actioncomm_resources as ar WHERE ar.fk_actioncomm = a.id AND ar.element_type = 'user')";
3567  $sqlSelect .= " ORDER BY a.id";
3568  //print $sqlSelect;
3569 
3570  $resql = $db->query($sqlSelect);
3571  if ($resql) {
3572  $i = 0;
3573  $num = $db->num_rows($resql);
3574 
3575  if ($num) {
3576  while ($i < $num) {
3577  $obj = $db->fetch_object($resql);
3578 
3579  $sqlUpdate = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element) ";
3580  $sqlUpdate .= "VALUES(".((int) $obj->id).", 'user', ".((int) $obj->fk_user_action).")";
3581 
3582  $result = $db->query($sqlUpdate);
3583  if (!$result) {
3584  $error++;
3585  dol_print_error($db);
3586  }
3587  print ". ";
3588  $i++;
3589  }
3590  } else {
3591  print $langs->trans('AlreadyDone')."<br>\n";
3592  }
3593 
3594  if (!$error) {
3595  $db->commit();
3596  } else {
3597  $db->rollback();
3598  }
3599  } else {
3600  dol_print_error($db);
3601  $db->rollback();
3602  }
3603 
3604 
3605  print '</td></tr>';
3606 }
3607 
3616 function migrate_event_assignement_contact($db, $langs, $conf)
3617 {
3618  print '<tr><td colspan="4">';
3619 
3620  print '<br>';
3621  print '<b>'.$langs->trans('MigrationEventsContact')."</b><br>\n";
3622 
3623  $error = 0;
3624 
3625  dolibarr_install_syslog("upgrade2::migrate_event_assignement");
3626 
3627  $db->begin();
3628 
3629  $sqlSelect = "SELECT a.id, a.fk_contact";
3630  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."actioncomm as a";
3631  $sqlSelect .= " LEFT JOIN ".MAIN_DB_PREFIX."actioncomm_resources as ar ON ar.fk_actioncomm = a.id AND ar.element_type = 'socpeople' AND ar.fk_element = a.fk_contact";
3632  $sqlSelect .= " WHERE fk_contact > 0 AND fk_contact NOT IN (SELECT fk_element FROM ".MAIN_DB_PREFIX."actioncomm_resources as ar WHERE ar.fk_actioncomm = a.id AND ar.element_type = 'socpeople')";
3633  $sqlSelect .= " ORDER BY a.id";
3634  //print $sqlSelect;
3635 
3636  $resql = $db->query($sqlSelect);
3637  if ($resql) {
3638  $i = 0;
3639  $num = $db->num_rows($resql);
3640 
3641  if ($num) {
3642  while ($i < $num) {
3643  $obj = $db->fetch_object($resql);
3644 
3645  $sqlUpdate = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element) ";
3646  $sqlUpdate .= "VALUES(".((int) $obj->id).", 'socpeople', ".((int) $obj->fk_contact).")";
3647 
3648  $result = $db->query($sqlUpdate);
3649  if (!$result) {
3650  $error++;
3651  dol_print_error($db);
3652  }
3653  print ". ";
3654  $i++;
3655  }
3656  } else {
3657  print $langs->trans('AlreadyDone')."<br>\n";
3658  }
3659 
3660  if (!$error) {
3661  $db->commit();
3662  } else {
3663  $db->rollback();
3664  }
3665  } else {
3666  dol_print_error($db);
3667  $db->rollback();
3668  }
3669 
3670 
3671  print '</td></tr>';
3672 }
3673 
3674 
3683 function migrate_reset_blocked_log($db, $langs, $conf)
3684 {
3685  global $user;
3686 
3687  require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
3688 
3689  print '<tr><td colspan="4">';
3690 
3691  print '<br>';
3692  print '<b>'.$langs->trans('MigrationResetBlockedLog')."</b><br>\n";
3693 
3694  $error = 0;
3695 
3696  dolibarr_install_syslog("upgrade2::migrate_reset_blocked_log");
3697 
3698  $db->begin();
3699 
3700  $sqlSelect = "SELECT DISTINCT entity";
3701  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."blockedlog";
3702 
3703  //print $sqlSelect;
3704 
3705  $resql = $db->query($sqlSelect);
3706  if ($resql) {
3707  $i = 0;
3708  $num = $db->num_rows($resql);
3709 
3710  if ($num) {
3711  while ($i < $num) {
3712  $obj = $db->fetch_object($resql);
3713 
3714  print 'Process entity '.$obj->entity;
3715 
3716  $sqlSearch = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."blockedlog WHERE action = 'MODULE_SET' and entity = ".((int) $obj->entity);
3717  $resqlSearch = $db->query($sqlSearch);
3718  if ($resqlSearch) {
3719  $objSearch = $db->fetch_object($resqlSearch);
3720  //var_dump($objSearch);
3721  if ($objSearch && $objSearch->nb == 0) {
3722  print ' - Record for entity must be reset...';
3723 
3724  $sqlUpdate = "DELETE FROM ".MAIN_DB_PREFIX."blockedlog";
3725  $sqlUpdate .= " WHERE entity = ".((int) $obj->entity);
3726  $resqlUpdate = $db->query($sqlUpdate);
3727  if (!$resqlUpdate) {
3728  $error++;
3729  dol_print_error($db);
3730  } else {
3731  // Add set line
3732  $object = new stdClass();
3733  $object->id = 1;
3734  $object->element = 'module';
3735  $object->ref = 'systemevent';
3736  $object->entity = $obj->entity;
3737  $object->date = dol_now();
3738 
3739  $b = new BlockedLog($db);
3740  $b->setObjectData($object, 'MODULE_SET', 0);
3741 
3742  $res = $b->create($user);
3743  if ($res <= 0) {
3744  $error++;
3745  }
3746  }
3747  } else {
3748  print ' - '.$langs->trans('AlreadyInV7').'<br>';
3749  }
3750  } else {
3751  dol_print_error($db);
3752  }
3753 
3754  $i++;
3755  }
3756  } else {
3757  print $langs->trans('NothingToDo')."<br>\n";
3758  }
3759 
3760  if (!$error) {
3761  $db->commit();
3762  } else {
3763  $db->rollback();
3764  }
3765  } else {
3766  dol_print_error($db);
3767  $db->rollback();
3768  }
3769 
3770  print '</td></tr>';
3771 }
3772 
3773 
3782 function migrate_remise_entity($db, $langs, $conf)
3783 {
3784  print '<tr><td colspan="4">';
3785 
3786  print '<br>';
3787  print '<b>'.$langs->trans('MigrationRemiseEntity')."</b><br>\n";
3788 
3789  $error = 0;
3790 
3791  dolibarr_install_syslog("upgrade2::migrate_remise_entity");
3792 
3793  $db->begin();
3794 
3795  $sqlSelect = "SELECT sr.rowid, s.entity";
3796  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."societe_remise as sr, ".MAIN_DB_PREFIX."societe as s";
3797  $sqlSelect .= " WHERE sr.fk_soc = s.rowid and sr.entity != s.entity";
3798 
3799  //print $sqlSelect;
3800 
3801  $resql = $db->query($sqlSelect);
3802  if ($resql) {
3803  $i = 0;
3804  $num = $db->num_rows($resql);
3805 
3806  if ($num) {
3807  while ($i < $num) {
3808  $obj = $db->fetch_object($resql);
3809 
3810  $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."societe_remise SET";
3811  $sqlUpdate .= " entity = ".$obj->entity;
3812  $sqlUpdate .= " WHERE rowid = ".((int) $obj->rowid);
3813 
3814  $result = $db->query($sqlUpdate);
3815  if (!$result) {
3816  $error++;
3817  dol_print_error($db);
3818  }
3819 
3820  print ". ";
3821  $i++;
3822  }
3823  } else {
3824  print $langs->trans('AlreadyDone')."<br>\n";
3825  }
3826 
3827  if (!$error) {
3828  $db->commit();
3829  } else {
3830  $db->rollback();
3831  }
3832  } else {
3833  dol_print_error($db);
3834  $db->rollback();
3835  }
3836 
3837  print '</td></tr>';
3838 }
3839 
3848 function migrate_remise_except_entity($db, $langs, $conf)
3849 {
3850  print '<tr><td colspan="4">';
3851 
3852  print '<br>';
3853  print '<b>'.$langs->trans('MigrationRemiseExceptEntity')."</b><br>\n";
3854 
3855  $error = 0;
3856 
3857  dolibarr_install_syslog("upgrade2::migrate_remise_except_entity");
3858 
3859  $db->begin();
3860 
3861  $sqlSelect = "SELECT sr.rowid, sr.fk_soc, sr.fk_facture_source, sr.fk_facture, sr.fk_facture_line";
3862  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as sr";
3863  //print $sqlSelect;
3864 
3865  $resql = $db->query($sqlSelect);
3866  if ($resql) {
3867  $i = 0;
3868  $num = $db->num_rows($resql);
3869 
3870  if ($num) {
3871  while ($i < $num) {
3872  $obj = $db->fetch_object($resql);
3873 
3874  if (!empty($obj->fk_facture_source) || !empty($obj->fk_facture)) {
3875  $fk_facture = (!empty($obj->fk_facture_source) ? $obj->fk_facture_source : $obj->fk_facture);
3876 
3877  $sqlSelect2 = "SELECT f.entity";
3878  $sqlSelect2 .= " FROM ".MAIN_DB_PREFIX."facture as f";
3879  $sqlSelect2 .= " WHERE f.rowid = ".((int) $fk_facture);
3880  } elseif (!empty($obj->fk_facture_line)) {
3881  $sqlSelect2 = "SELECT f.entity";
3882  $sqlSelect2 .= " FROM ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."facturedet as fd";
3883  $sqlSelect2 .= " WHERE fd.rowid = ".((int) $obj->fk_facture_line);
3884  $sqlSelect2 .= " AND fd.fk_facture = f.rowid";
3885  } else {
3886  $sqlSelect2 = "SELECT s.entity";
3887  $sqlSelect2 .= " FROM ".MAIN_DB_PREFIX."societe as s";
3888  $sqlSelect2 .= " WHERE s.rowid = ".((int) $obj->fk_soc);
3889  }
3890 
3891  $resql2 = $db->query($sqlSelect2);
3892  if ($resql2) {
3893  if ($db->num_rows($resql2) > 0) {
3894  $obj2 = $db->fetch_object($resql2);
3895 
3896  $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."societe_remise_except SET";
3897  $sqlUpdate .= " entity = ".((int) $obj2->entity);
3898  $sqlUpdate .= " WHERE rowid = ".((int) $obj->rowid);
3899 
3900  $result = $db->query($sqlUpdate);
3901  if (!$result) {
3902  $error++;
3903  dol_print_error($db);
3904  }
3905  }
3906  } else {
3907  $error++;
3908  dol_print_error($db);
3909  }
3910 
3911  print ". ";
3912  $i++;
3913  }
3914  } else {
3915  print $langs->trans('AlreadyDone')."<br>\n";
3916  }
3917 
3918  if (!$error) {
3919  $db->commit();
3920  } else {
3921  $db->rollback();
3922  }
3923  } else {
3924  dol_print_error($db);
3925  $db->rollback();
3926  }
3927 
3928 
3929  print '</td></tr>';
3930 }
3931 
3940 function migrate_user_rights_entity($db, $langs, $conf)
3941 {
3942  print '<tr><td colspan="4">';
3943 
3944  print '<b>'.$langs->trans('MigrationUserRightsEntity')."</b><br>\n";
3945 
3946  $error = 0;
3947 
3948  dolibarr_install_syslog("upgrade2::migrate_user_rights_entity");
3949 
3950  $db->begin();
3951 
3952  $sqlSelect = "SELECT u.rowid, u.entity";
3953  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."user as u";
3954  $sqlSelect .= " WHERE u.entity > 1";
3955  //print $sqlSelect;
3956 
3957  $resql = $db->query($sqlSelect);
3958  if ($resql) {
3959  $i = 0;
3960  $num = $db->num_rows($resql);
3961 
3962  if ($num) {
3963  while ($i < $num) {
3964  $obj = $db->fetch_object($resql);
3965 
3966  $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."user_rights SET";
3967  $sqlUpdate .= " entity = ".((int) $obj->entity);
3968  $sqlUpdate .= " WHERE fk_user = ".((int) $obj->rowid);
3969 
3970  $result = $db->query($sqlUpdate);
3971  if (!$result) {
3972  $error++;
3973  dol_print_error($db);
3974  }
3975 
3976  print ". ";
3977  $i++;
3978  }
3979  } else {
3980  print $langs->trans('AlreadyDone')."<br>\n";
3981  }
3982 
3983  if (!$error) {
3984  $db->commit();
3985  } else {
3986  $db->rollback();
3987  }
3988  } else {
3989  dol_print_error($db);
3990  $db->rollback();
3991  }
3992 
3993 
3994  print '</td></tr>';
3995 }
3996 
4005 function migrate_usergroup_rights_entity($db, $langs, $conf)
4006 {
4007  print '<tr><td colspan="4">';
4008 
4009  print '<b>'.$langs->trans('MigrationUserGroupRightsEntity')."</b><br>\n";
4010 
4011  $error = 0;
4012 
4013  dolibarr_install_syslog("upgrade2::migrate_usergroup_rights_entity");
4014 
4015  $db->begin();
4016 
4017  $sqlSelect = "SELECT u.rowid, u.entity";
4018  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."usergroup as u";
4019  $sqlSelect .= " WHERE u.entity > 1";
4020  //print $sqlSelect;
4021 
4022  $resql = $db->query($sqlSelect);
4023  if ($resql) {
4024  $i = 0;
4025  $num = $db->num_rows($resql);
4026 
4027  if ($num) {
4028  while ($i < $num) {
4029  $obj = $db->fetch_object($resql);
4030 
4031  $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."usergroup_rights SET";
4032  $sqlUpdate .= " entity = ".((int) $obj->entity);
4033  $sqlUpdate .= " WHERE fk_usergroup = ".((int) $obj->rowid);
4034 
4035  $result = $db->query($sqlUpdate);
4036  if (!$result) {
4037  $error++;
4038  dol_print_error($db);
4039  }
4040 
4041  print ". ";
4042  $i++;
4043  }
4044  } else {
4045  print $langs->trans('AlreadyDone')."<br>\n";
4046  }
4047 
4048  if (!$error) {
4049  $db->commit();
4050  } else {
4051  $db->rollback();
4052  }
4053  } else {
4054  dol_print_error($db);
4055  $db->rollback();
4056  }
4057 
4058 
4059  print '</td></tr>';
4060 }
4061 
4072 function migrate_rename_directories($db, $langs, $conf, $oldname, $newname)
4073 {
4074  dolibarr_install_syslog("upgrade2::migrate_rename_directories");
4075 
4076  if (is_dir(DOL_DATA_ROOT.$oldname) && !file_exists(DOL_DATA_ROOT.$newname)) {
4077  dolibarr_install_syslog("upgrade2::migrate_rename_directories move ".DOL_DATA_ROOT.$oldname.' into '.DOL_DATA_ROOT.$newname);
4078  @rename(DOL_DATA_ROOT.$oldname, DOL_DATA_ROOT.$newname);
4079  }
4080 }
4081 
4082 
4091 function migrate_delete_old_files($db, $langs, $conf)
4092 {
4093  $ret = true;
4094 
4095  dolibarr_install_syslog("upgrade2::migrate_delete_old_files");
4096 
4097  // List of files to delete
4098  $filetodeletearray = array(
4099  '/core/triggers/interface_demo.class.php',
4100  '/core/menus/barre_left/default.php',
4101  '/core/menus/barre_top/default.php',
4102  '/core/modules/modComptabiliteExpert.class.php',
4103  '/core/modules/modCommercial.class.php',
4104  '/core/modules/modProduit.class.php',
4105  '/core/modules/modSkype.class.php',
4106  '/phenix/inc/triggers/interface_modPhenix_Phenixsynchro.class.php',
4107  '/webcalendar/inc/triggers/interface_modWebcalendar_webcalsynchro.class.php',
4108  '/core/triggers/interface_modWebcalendar_Webcalsynchro.class.php',
4109  '/core/triggers/interface_modCommande_Ecotax.class.php',
4110  '/core/triggers/interface_modCommande_fraisport.class.php',
4111  '/core/triggers/interface_modPropale_PropalWorkflow.class.php',
4112  '/core/triggers/interface_99_modWebhook_WebhookTriggers.class.php',
4113  '/core/triggers/interface_99_modZapier_ZapierTriggers.class.php',
4114  '/core/menus/smartphone/iphone.lib.php',
4115  '/core/menus/smartphone/iphone_backoffice.php',
4116  '/core/menus/smartphone/iphone_frontoffice.php',
4117  '/core/menus/standard/auguria_backoffice.php',
4118  '/core/menus/standard/auguria_frontoffice.php',
4119  '/core/menus/standard/eldy_backoffice.php',
4120  '/core/menus/standard/eldy_frontoffice.php',
4121  '/core/modules/mailings/contacts2.modules.php',
4122  '/core/modules/mailings/contacts3.modules.php',
4123  '/core/modules/mailings/contacts4.modules.php',
4124  '/core/modules/mailings/framboise.modules.php',
4125  '/core/modules/mailings/dolibarr_services_expired.modules.php',
4126  '/core/modules/mailings/peche.modules.php',
4127  '/core/modules/mailings/poire.modules.php',
4128  '/core/modules/mailings/kiwi.modules.php',
4129  '/core/modules/facture/pdf_crabe.modules.php',
4130  '/core/modules/facture/pdf_oursin.modules.php',
4131  '/core/modules/export/export_excel.modules.php',
4132  '/core/modules/export/export_excel2007new.modules.php',
4133  '/core/boxes/box_members.php',
4134 
4135  '/api/class/api_generic.class.php',
4136  '/asterisk/cidlookup.php',
4137  '/categories/class/api_category.class.php',
4138  '/categories/class/api_deprecated_category.class.php',
4139  '/compta/facture/class/api_invoice.class.php',
4140  '/commande/class/api_commande.class.php',
4141  '/user/class/api_user.class.php',
4142  '/product/class/api_product.class.php',
4143  '/societe/class/api_contact.class.php',
4144  '/societe/class/api_thirdparty.class.php',
4145  '/support/online.php',
4146  '/takepos/class/actions_takepos.class.php',
4147 
4148  '/install/mysql/tables/llx_c_ticketsup_category.key.sql',
4149  '/install/mysql/tables/llx_c_ticketsup_category.sql',
4150  '/install/mysql/tables/llx_c_ticketsup_severity.key.sql',
4151  '/install/mysql/tables/llx_c_ticketsup_severity.sql',
4152  '/install/mysql/tables/llx_c_ticketsup_type.key.sql',
4153  '/install/mysql/tables/llx_c_ticketsup_type.sql'
4154  );
4155 
4156  foreach ($filetodeletearray as $filetodelete) {
4157  //print '<b>'DOL_DOCUMENT_ROOT.$filetodelete."</b><br>\n";
4158  if (file_exists(DOL_DOCUMENT_ROOT.$filetodelete)) {
4159  $result = dol_delete_file(DOL_DOCUMENT_ROOT.$filetodelete, 0, 0, 0, null, true, false);
4160  if (!$result) {
4161  $langs->load("errors");
4162  print '<div class="error">'.$langs->trans("Error").': '.$langs->trans("ErrorFailToDeleteFile", DOL_DOCUMENT_ROOT.$filetodelete);
4163  print ' '.$langs->trans("RemoveItManuallyAndPressF5ToContinue").'</div>';
4164  } else {
4165  //print $langs->trans("FileWasRemoved", $filetodelete).'<br>';
4166  }
4167  }
4168  }
4169 
4170  return $ret;
4171 }
4172 
4181 function migrate_delete_old_dir($db, $langs, $conf)
4182 {
4183  $ret = true;
4184 
4185  dolibarr_install_syslog("upgrade2::migrate_delete_old_dir");
4186 
4187  // List of files to delete
4188  $filetodeletearray = array(
4189  DOL_DOCUMENT_ROOT.'/core/modules/facture/terre',
4190  DOL_DOCUMENT_ROOT.'/core/modules/facture/mercure',
4191  );
4192 
4193  // On linux, we can also removed old directory with a different case than new directory.
4194  if (!empty($_SERVER["WINDIR"])) {
4195  $filetodeletearray[] = DOL_DOCUMENT_ROOT.'/includes/phpoffice/PhpSpreadsheet';
4196  }
4197 
4198  foreach ($filetodeletearray as $filetodelete) {
4199  //print '<b>'.$filetodelete."</b><br>\n";
4200  if (file_exists($filetodelete)) {
4201  $result = dol_delete_dir_recursive($filetodelete);
4202  }
4203  if (!$result) {
4204  $langs->load("errors");
4205  print '<div class="error">'.$langs->trans("Error").': '.$langs->trans("ErrorFailToDeleteDir", $filetodelete);
4206  print ' '.$langs->trans("RemoveItManuallyAndPressF5ToContinue").'</div>';
4207  }
4208  }
4209 
4210  return $ret;
4211 }
4212 
4213 
4226 function migrate_reload_modules($db, $langs, $conf, $listofmodule = array(), $force = 0)
4227 {
4228  if (count($listofmodule) == 0) {
4229  return;
4230  }
4231 
4232  dolibarr_install_syslog("upgrade2::migrate_reload_modules force=".$force.", listofmodule=".join(',', array_keys($listofmodule)));
4233 
4234  foreach ($listofmodule as $moduletoreload => $reloadmode) { // reloadmodule can be 'noboxes', 'newboxdefonly', 'forceactivate'
4235  if (empty($moduletoreload) || (empty($conf->global->$moduletoreload) && !$force)) {
4236  continue; // Discard reload if module not enabled
4237  }
4238 
4239  $mod = null;
4240 
4241  if ($moduletoreload == 'MAIN_MODULE_AGENDA') {
4242  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Agenda module");
4243  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modAgenda.class.php';
4244  if ($res) {
4245  $mod = new modAgenda($db);
4246  $mod->remove('noboxes');
4247  $mod->init($reloadmode);
4248  }
4249  } elseif ($moduletoreload == 'MAIN_MODULE_API') {
4250  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Rest API module");
4251  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modApi.class.php';
4252  if ($res) {
4253  $mod = new modApi($db);
4254  //$mod->remove('noboxes');
4255  $mod->init($reloadmode);
4256  }
4257  } elseif ($moduletoreload == 'MAIN_MODULE_BARCODE') {
4258  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Barcode module");
4259  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modBarcode.class.php';
4260  if ($res) {
4261  $mod = new modBarcode($db);
4262  $mod->remove('noboxes');
4263  $mod->init($reloadmode);
4264  }
4265  } elseif ($moduletoreload == 'MAIN_MODULE_BLOCKEDLOG') {
4266  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate BlockedLog module");
4267  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modBlockedLog.class.php';
4268  if ($res) {
4269  $mod = new modBlockedLog($db);
4270  // For this module we only reload menus.
4271  $mod->delete_menus();
4272  $mod->insert_menus();
4273  }
4274  } elseif ($moduletoreload == 'MAIN_MODULE_CRON') {
4275  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Cron module");
4276  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modCron.class.php';
4277  if ($res) {
4278  $mod = new modCron($db);
4279  $mod->remove('noboxes');
4280  $mod->init($reloadmode);
4281  }
4282  } elseif ($moduletoreload == 'MAIN_MODULE_EXTERNALSITE') {
4283  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate ExternalSite module");
4284  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modExternalSite.class.php';
4285  if ($res) {
4286  $mod = new modExternalSite($db);
4287  $mod->remove('noboxes');
4288  $mod->init($reloadmode);
4289  }
4290  } elseif ($moduletoreload == 'MAIN_MODULE_SOCIETE') {
4291  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Societe module");
4292  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modSociete.class.php';
4293  if ($res) {
4294  $mod = new modSociete($db);
4295  $mod->remove('noboxes');
4296  $mod->init($reloadmode);
4297  }
4298  } elseif ($moduletoreload == 'MAIN_MODULE_PRODUIT') { // Permission has changed into 2.7
4299  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Produit module");
4300  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modProduct.class.php';
4301  if ($res) {
4302  $mod = new modProduct($db);
4303  //$mod->remove('noboxes');
4304  $mod->init($reloadmode);
4305  }
4306  } elseif ($moduletoreload == 'MAIN_MODULE_SERVICE') { // Permission has changed into 2.7
4307  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Service module");
4308  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modService.class.php';
4309  if ($res) {
4310  $mod = new modService($db);
4311  //$mod->remove('noboxes');
4312  $mod->init($reloadmode);
4313  }
4314  } elseif ($moduletoreload == 'MAIN_MODULE_COMMANDE') { // Permission has changed into 2.9
4315  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Commande module");
4316  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modCommande.class.php';
4317  if ($res) {
4318  $mod = new modCommande($db);
4319  //$mod->remove('noboxes');
4320  $mod->init($reloadmode);
4321  }
4322  } elseif ($moduletoreload == 'MAIN_MODULE_FACTURE') { // Permission has changed into 2.9
4323  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Facture module");
4324  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modFacture.class.php';
4325  if ($res) {
4326  $mod = new modFacture($db);
4327  //$mod->remove('noboxes');
4328  $mod->init($reloadmode);
4329  }
4330  } elseif ($moduletoreload == 'MAIN_MODULE_FOURNISSEUR') { // Permission has changed into 2.9
4331  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Fournisseur module");
4332  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modFournisseur.class.php';
4333  if ($res) {
4334  $mod = new modFournisseur($db);
4335  //$mod->remove('noboxes');
4336  $mod->init($reloadmode);
4337  }
4338  } elseif ($moduletoreload == 'MAIN_MODULE_HOLIDAY') { // Permission and tabs has changed into 3.8
4339  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Leave Request module");
4340  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modHoliday.class.php';
4341  if ($res) {
4342  $mod = new modHoliday($db);
4343  $mod->remove('noboxes');
4344  $mod->init($reloadmode);
4345  }
4346  } elseif ($moduletoreload == 'MAIN_MODULE_DEPLACEMENT') { // Permission has changed into 3.0
4347  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Deplacement module");
4348  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modDeplacement.class.php';
4349  if ($res) {
4350  $mod = new modDeplacement($db);
4351  //$mod->remove('noboxes');
4352  $mod->init($reloadmode);
4353  }
4354  } elseif ($moduletoreload == 'MAIN_MODULE_EXPENSEREPORT') {
4355  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Expense Report module");
4356  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modExpenseReport.class.php';
4357  if ($res) {
4358  $mod = new modExpenseReport($db);
4359  //$mod->remove('noboxes');
4360  $mod->init($reloadmode);
4361  }
4362  } elseif ($moduletoreload == 'MAIN_MODULE_DON') { // Permission has changed into 3.0
4363  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Don module");
4364  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modDon.class.php';
4365  if ($res) {
4366  $mod = new modDon($db);
4367  //$mod->remove('noboxes');
4368  $mod->init($reloadmode);
4369  }
4370  } elseif ($moduletoreload == 'MAIN_MODULE_ECM') { // Permission has changed into 3.0 and 3.1
4371  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate ECM module");
4372  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modECM.class.php';
4373  if ($res) {
4374  $mod = new modECM($db);
4375  $mod->remove('noboxes'); // We need to remove because a permission id has been removed
4376  $mod->init($reloadmode);
4377  }
4378  } elseif ($moduletoreload == 'MAIN_MODULE_KNOWLEDGEMANAGEMENT') { // Permission has changed into 3.0 and 3.1
4379  dolibarr_install_syslog("upgrade2::migrate_reload_modules Knowledge Management");
4380  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modKnowledgeManagement.class.php';
4381  if ($res) {
4382  $mod = new modKnowledgeManagement($db);
4383  $mod->remove('noboxes'); // We need to remove because a permission id has been removed
4384  $mod->init($reloadmode);
4385  }
4386  } elseif ($moduletoreload == 'MAIN_MODULE_EVENTORGANIZATION') { // Permission has changed into 3.0 and 3.1
4387  dolibarr_install_syslog("upgrade2::migrate_reload_modules EventOrganization");
4388  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modEventOrganization.class.php';
4389  if ($res) {
4390  $mod = new modEventOrganization($db);
4391  $mod->remove('noboxes'); // We need to remove because a permission id has been removed
4392  $mod->init($reloadmode);
4393  }
4394  } elseif ($moduletoreload == 'MAIN_MODULE_PAYBOX') { // Permission has changed into 3.0
4395  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Paybox module");
4396  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modPaybox.class.php';
4397  if ($res) {
4398  $mod = new modPaybox($db);
4399  $mod->remove('noboxes'); // We need to remove because id of module has changed
4400  $mod->init($reloadmode);
4401  }
4402  } elseif ($moduletoreload == 'MAIN_MODULE_SUPPLIERPROPOSAL') { // Module after 3.5
4403  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Supplier Proposal module");
4404  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modSupplierProposal.class.php';
4405  if ($res) {
4406  $mod = new modSupplierProposal($db);
4407  $mod->remove('noboxes'); // We need to remove because id of module has changed
4408  $mod->init($reloadmode);
4409  }
4410  } elseif ($moduletoreload == 'MAIN_MODULE_OPENSURVEY') { // Permission has changed into 3.0
4411  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Opensurvey module");
4412  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modOpenSurvey.class.php';
4413  if ($res) {
4414  $mod = new modOpenSurvey($db);
4415  $mod->remove('noboxes'); // We need to remove because menu entries has changed
4416  $mod->init($reloadmode);
4417  }
4418  } elseif ($moduletoreload == 'MAIN_MODULE_PRODUCTBATCH') { // Permission has changed into 10.0
4419  dolibarr_install_syslog("upgrade2::migrate_reload_modules ProductBatch module");
4420  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modProductBatch.class.php';
4421  if ($res) {
4422  $mod = new modProductBatch($db);
4423  $mod->remove('noboxes'); // We need to remove because menu entries has changed
4424  $mod->init($reloadmode);
4425  }
4426  } elseif ($moduletoreload == 'MAIN_MODULE_TAKEPOS') { // Permission has changed into 10.0
4427  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Takepos module");
4428  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modTakePos.class.php';
4429  if ($res) {
4430  $mod = new modTakePos($db);
4431  $mod->remove('noboxes'); // We need to remove because menu entries has changed
4432  $mod->init($reloadmode);
4433  }
4434  } else { // Other generic cases/modules
4435  $reg = array();
4436  $tmp = preg_match('/MAIN_MODULE_([a-zA-Z0-9]+)/', $moduletoreload, $reg);
4437  if (!empty($reg[1])) {
4438  if (strtoupper($moduletoreload) == $moduletoreload) { // If key is un uppercase
4439  $moduletoreloadshort = ucfirst(strtolower($reg[1]));
4440  } else // If key is a mix of up and low case
4441  {
4442  $moduletoreloadshort = $reg[1];
4443  }
4444 
4445  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate module ".$moduletoreloadshort." with mode ".$reloadmode);
4446  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/mod'.$moduletoreloadshort.'.class.php';
4447  if ($res) {
4448  $classname = 'mod'.$moduletoreloadshort;
4449  $mod = new $classname($db);
4450 
4451  //$mod->remove('noboxes');
4452  $mod->delete_menus(); // We must delete to be sure it is inserted with new values
4453  $mod->init($reloadmode);
4454  } else {
4455  dolibarr_install_syslog('Failed to include '.DOL_DOCUMENT_ROOT.'/core/modules/mod'.$moduletoreloadshort.'.class.php');
4456 
4457  $res = @dol_include_once(strtolower($moduletoreloadshort).'/core/modules/mod'.$moduletoreloadshort.'.class.php');
4458  if ($res) {
4459  $classname = 'mod'.$moduletoreloadshort;
4460  $mod = new $classname($db);
4461  $mod->init($reloadmode);
4462  } else {
4463  dolibarr_install_syslog('Failed to include '.strtolower($moduletoreloadshort).'/core/modules/mod'.$moduletoreloadshort.'.class.php', LOG_ERR);
4464  print "Error, can't find module with name ".$moduletoreload."\n";
4465  return -1;
4466  }
4467  }
4468  } else {
4469  dolibarr_install_syslog("Error, can't find module with name ".$moduletoreload, LOG_ERR);
4470  print "Error, can't find module with name ".$moduletoreload."\n";
4471  return -1;
4472  }
4473  }
4474 
4475  if (!empty($mod) && is_object($mod)) {
4476  print '<tr class="trforrunsql"><td colspan="4">';
4477  print '<b>'.$langs->trans('Upgrade').'</b>: ';
4478  print $langs->trans('MigrationReloadModule').' '.$mod->getName(); // We keep getName outside of trans because getName is already encoded/translated
4479  print "<!-- (".$reloadmode.") -->";
4480  print "<br>\n";
4481  print '</td></tr>';
4482  }
4483  }
4484 
4485  return 1;
4486 }
4487 
4488 
4489 
4498 function migrate_reload_menu($db, $langs, $conf)
4499 {
4500  global $conf;
4501  dolibarr_install_syslog("upgrade2::migrate_reload_menu");
4502 
4503  // Define list of menu handlers to initialize
4504  $listofmenuhandler = array();
4505  if ($conf->global->MAIN_MENU_STANDARD == 'auguria_menu' || $conf->global->MAIN_MENU_SMARTPHONE == 'auguria_menu'
4506  || $conf->global->MAIN_MENUFRONT_STANDARD == 'auguria_menu' || $conf->global->MAIN_MENUFRONT_SMARTPHONE == 'auguria_menu') {
4507  $listofmenuhandler['auguria'] = 1; // We set here only dynamic menu handlers
4508  }
4509 
4510  foreach ($listofmenuhandler as $key => $val) {
4511  print '<tr class="trforrunsql"><td colspan="4">';
4512 
4513  //print "x".$key;
4514  print '<br>';
4515  print '<b>'.$langs->trans('Upgrade').'</b>: '.$langs->trans('MenuHandler')." ".$key."<br>\n";
4516 
4517  // Load sql ini_menu_handler.sql file
4518  $dir = DOL_DOCUMENT_ROOT."/core/menus/";
4519  $file = 'init_menu_'.$key.'.sql';
4520  if (file_exists($dir.$file)) {
4521  $result = run_sql($dir.$file, 1, '', 1, $key);
4522  }
4523 
4524  print '</td></tr>';
4525  }
4526 
4527  return 1;
4528 }
4529 
4536 {
4537  global $conf, $db, $langs, $user;
4538 
4539  print '<tr><td colspan="4">';
4540 
4541  print '<b>'.$langs->trans('MigrationUserPhotoPath')."</b><br>\n";
4542 
4543  include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
4544  $fuser = new User($db);
4545 
4546  if (!is_object($user)) {
4547  $user = $fuser; // To avoid error during migration
4548  }
4549 
4550  $sql = "SELECT rowid as uid from ".MAIN_DB_PREFIX."user"; // Get list of all users
4551  $resql = $db->query($sql);
4552  if ($resql) {
4553  while ($obj = $db->fetch_object($resql)) {
4554  $fuser->fetch($obj->uid);
4555  //echo '<hr>'.$fuser->id.' -> '.$fuser->entity;
4556  $entity = (empty($fuser->entity) ? 1 : $fuser->entity);
4557  if ($entity > 1) {
4558  $dir = DOL_DATA_ROOT.'/'.$entity.'/users';
4559  } else {
4560  $dir = $conf->user->multidir_output[$entity]; // $conf->user->multidir_output[] for each entity is construct by the multicompany module
4561  }
4562 
4563  if ($dir) {
4564  //print "Process user id ".$fuser->id."<br>\n";
4565  $origin = $dir.'/'.get_exdir($fuser->id, 2, 0, 1, $fuser, 'user'); // Use old behaviour to get x/y path
4566  $destin = $dir.'/'.$fuser->id;
4567 
4568  $origin_osencoded = dol_osencode($origin);
4569 
4570  dol_mkdir($destin);
4571 
4572  //echo '<hr>'.$origin.' -> '.$destin;
4573  if (dol_is_dir($origin)) {
4574  $handle = opendir($origin_osencoded);
4575  if (is_resource($handle)) {
4576  while (($file = readdir($handle)) !== false) {
4577  if ($file == '.' || $file == '..') {
4578  continue;
4579  }
4580 
4581  if (dol_is_dir($origin.'/'.$file)) { // it is a dir (like 'thumbs')
4582  $thumbs = opendir($origin_osencoded.'/'.$file);
4583  if (is_resource($thumbs)) {
4584  dol_mkdir($destin.'/'.$file);
4585  while (($thumb = readdir($thumbs)) !== false) {
4586  if (!dol_is_file($destin.'/'.$file.'/'.$thumb)) {
4587  if ($thumb == '.' || $thumb == '..') {
4588  continue;
4589  }
4590 
4591  //print $origin.'/'.$file.'/'.$thumb.' -> '.$destin.'/'.$file.'/'.$thumb.'<br>'."\n";
4592  print '.';
4593  dol_copy($origin.'/'.$file.'/'.$thumb, $destin.'/'.$file.'/'.$thumb, 0, 0);
4594  //var_dump('aaa');exit;
4595  }
4596  }
4597  // dol_delete_dir($origin.'/'.$file);
4598  }
4599  } else { // it is a file
4600  if (!dol_is_file($destin.'/'.$file)) {
4601  //print $origin.'/'.$file.' -> '.$destin.'/'.$file.'<br>'."\n";
4602  print '.';
4603  dol_copy($origin.'/'.$file, $destin.'/'.$file, 0, 0);
4604  //var_dump('eee');exit;
4605  }
4606  }
4607  }
4608  }
4609  }
4610  }
4611  }
4612  }
4613 
4614  print '</td></tr>';
4615 }
4616 
4623 {
4624  global $conf, $db, $langs, $user;
4625 
4626  print '<tr><td colspan="4">';
4627 
4628  print '<b>'.$langs->trans('MigrationUserPhotoPath')."</b><br>\n";
4629 
4630  include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
4631  $fuser = new User($db);
4632 
4633  if (!is_object($user)) {
4634  $user = $fuser; // To avoid error during migration
4635  }
4636 
4637  $sql = "SELECT rowid as uid from ".MAIN_DB_PREFIX."user"; // Get list of all users
4638  $resql = $db->query($sql);
4639  if ($resql) {
4640  while ($obj = $db->fetch_object($resql)) {
4641  $fuser->fetch($obj->uid);
4642  //echo '<hr>'.$fuser->id.' -> '.$fuser->entity;
4643  $entity = (empty($fuser->entity) ? 1 : $fuser->entity);
4644  if ($entity > 1) {
4645  $dir = DOL_DATA_ROOT.'/'.$entity.'/users';
4646  } else {
4647  $dir = DOL_DATA_ROOT.'/users';
4648  }
4649 
4650  if ($dir) {
4651  //print "Process user id ".$fuser->id."<br>\n";
4652  $origin = $dir.'/'.$fuser->id;
4653  $destin = $dir.'/'.$fuser->id.'/photos';
4654 
4655  $origin_osencoded = dol_osencode($origin);
4656 
4657  dol_mkdir($destin);
4658 
4659  //echo '<hr>'.$origin.' -> '.$destin;
4660  if (dol_is_dir($origin)) {
4661  $handle = opendir($origin_osencoded);
4662  if (is_resource($handle)) {
4663  while (($file = readdir($handle)) !== false) {
4664  if ($file == '.' || $file == '..' || $file == 'photos') {
4665  continue;
4666  }
4667  if (!empty($fuser->photo) && ($file != $fuser->photo && $file != 'thumbs')) {
4668  continue;
4669  }
4670 
4671  if (dol_is_dir($origin.'/'.$file)) { // it is a dir (like 'thumbs')
4672  $thumbs = opendir($origin_osencoded.'/'.$file);
4673  if (is_resource($thumbs)) {
4674  dol_mkdir($destin.'/'.$file);
4675  while (($thumb = readdir($thumbs)) !== false) {
4676  if (!dol_is_file($destin.'/'.$file.'/'.$thumb)) {
4677  if ($thumb == '.' || $thumb == '..') {
4678  continue;
4679  }
4680 
4681  //print $origin.'/'.$file.'/'.$thumb.' -> '.$destin.'/'.$file.'/'.$thumb.'<br>'."\n";
4682  print '.';
4683  dol_copy($origin.'/'.$file.'/'.$thumb, $destin.'/'.$file.'/'.$thumb, 0, 0);
4684  }
4685  }
4686  // dol_delete_dir($origin.'/'.$file);
4687  }
4688  } else { // it is a file
4689  if (!dol_is_file($destin.'/'.$file)) {
4690  //print $origin.'/'.$file.' -> '.$destin.'/'.$file.'<br>'."\n";
4691  print '.';
4692  dol_copy($origin.'/'.$file, $destin.'/'.$file, 0, 0);
4693  }
4694  }
4695  }
4696  }
4697  }
4698  }
4699  }
4700  }
4701 
4702  print '</td></tr>';
4703 }
4704 
4705 
4706 /* A faire egalement: Modif statut paye et fk_facture des factures payes completement
4707 
4708 On recherche facture incorrecte:
4709 select f.rowid, f.total_ttc as t1, sum(pf.amount) as t2 from llx_facture as f, llx_paiement_facture as pf where pf.fk_facture=f.rowid and f.fk_statut in(2,3) and paye=0 and close_code is null group by f.rowid
4710 having f.total_ttc = sum(pf.amount)
4711 
4712 On les corrige:
4713 update llx_facture set paye=1, fk_statut=2 where close_code is null
4714 and rowid in (...)
4715 */
4716 
4724 {
4725  global $db, $langs;
4726  // skype,twitter,facebook,linkedin,instagram,snapchat,googleplus,youtube,whatsapp
4727  $error = 0;
4728  $db->begin();
4729  print '<tr><td colspan="4">';
4730  $sql = 'SELECT rowid, socialnetworks';
4731  $sql .= ', skype, twitter, facebook, linkedin, instagram, snapchat, googleplus, youtube, whatsapp FROM '.MAIN_DB_PREFIX.'user WHERE';
4732  $sql .= " skype IS NOT NULL OR skype <> ''";
4733  $sql .= " OR twitter IS NOT NULL OR twitter <> ''";
4734  $sql .= " OR facebook IS NOT NULL OR facebook <> ''";
4735  $sql .= " OR linkedin IS NOT NULL OR linkedin <> ''";
4736  $sql .= " OR instagram IS NOT NULL OR instagram <> ''";
4737  $sql .= " OR snapchat IS NOT NULL OR snapchat <> ''";
4738  $sql .= " OR googleplus IS NOT NULL OR googleplus <> ''";
4739  $sql .= " OR youtube IS NOT NULL OR youtube <> ''";
4740  $sql .= " OR whatsapp IS NOT NULL OR whatsapp <> ''";
4741  //print $sql;
4742  $resql = $db->query($sql);
4743  if ($resql) {
4744  while ($obj = $db->fetch_object($resql)) {
4745  $arraysocialnetworks = array();
4746  if (!empty($obj->skype)) {
4747  $arraysocialnetworks['skype'] = $obj->skype;
4748  }
4749  if (!empty($obj->twitter)) {
4750  $arraysocialnetworks['twitter'] = $obj->twitter;
4751  }
4752  if (!empty($obj->facebook)) {
4753  $arraysocialnetworks['facebook'] = $obj->facebook;
4754  }
4755  if (!empty($obj->linkedin)) {
4756  $arraysocialnetworks['linkedin'] = $obj->linkedin;
4757  }
4758  if (!empty($obj->instagram)) {
4759  $arraysocialnetworks['instagram'] = $obj->instagram;
4760  }
4761  if (!empty($obj->snapchat)) {
4762  $arraysocialnetworks['snapchat'] = $obj->snapchat;
4763  }
4764  if (!empty($obj->googleplus)) {
4765  $arraysocialnetworks['googleplus'] = $obj->googleplus;
4766  }
4767  if (!empty($obj->youtube)) {
4768  $arraysocialnetworks['youtube'] = $obj->youtube;
4769  }
4770  if (!empty($obj->whatsapp)) {
4771  $arraysocialnetworks['whatsapp'] = $obj->whatsapp;
4772  }
4773  if ($obj->socialnetworks == '' || is_null($obj->socialnetworks)) {
4774  $obj->socialnetworks = '[]';
4775  }
4776  $socialnetworks = array_merge($arraysocialnetworks, json_decode($obj->socialnetworks, true));
4777  $sqlupd = 'UPDATE '.MAIN_DB_PREFIX."user SET socialnetworks='".$db->escape(json_encode($socialnetworks, true))."'";
4778  $sqlupd .= ', skype=null';
4779  $sqlupd .= ', twitter=null';
4780  $sqlupd .= ', facebook=null';
4781  $sqlupd .= ', linkedin=null';
4782  $sqlupd .= ', instagram=null';
4783  $sqlupd .= ', snapchat=null';
4784  $sqlupd .= ', googleplus=null';
4785  $sqlupd .= ', youtube=null';
4786  $sqlupd .= ', whatsapp=null';
4787  $sqlupd .= ' WHERE rowid = '.((int) $obj->rowid);
4788  //print $sqlupd."<br>";
4789  $resqlupd = $db->query($sqlupd);
4790  if (!$resqlupd) {
4791  dol_print_error($db);
4792  $error++;
4793  }
4794  }
4795  } else {
4796  $error++;
4797  }
4798  if (!$error) {
4799  $db->commit();
4800  } else {
4801  dol_print_error($db);
4802  $db->rollback();
4803  }
4804  print '<b>'.$langs->trans('MigrationFieldsSocialNetworks', 'Users')."</b><br>\n";
4805  print '</td></tr>';
4806 }
4807 
4815 {
4816  global $db, $langs;
4817 
4818  print '<tr><td colspan="4">';
4819  $error = 0;
4820  $db->begin();
4821  print '<tr><td colspan="4">';
4822  $sql = 'SELECT rowid, socialnetworks';
4823  $sql .= ', skype, twitter, facebook, linkedin, instagram, snapchat, googleplus, youtube, whatsapp FROM '.MAIN_DB_PREFIX.'adherent WHERE ';
4824  $sql .= " skype IS NOT NULL OR skype <> ''";
4825  $sql .= " OR twitter IS NOT NULL OR twitter <> ''";
4826  $sql .= " OR facebook IS NOT NULL OR facebook <> ''";
4827  $sql .= " OR linkedin IS NOT NULL OR linkedin <> ''";
4828  $sql .= " OR instagram IS NOT NULL OR instagram <> ''";
4829  $sql .= " OR snapchat IS NOT NULL OR snapchat <> ''";
4830  $sql .= " OR googleplus IS NOT NULL OR googleplus <> ''";
4831  $sql .= " OR youtube IS NOT NULL OR youtube <> ''";
4832  $sql .= " OR whatsapp IS NOT NULL OR whatsapp <> ''";
4833  //print $sql;
4834  $resql = $db->query($sql);
4835  if ($resql) {
4836  while ($obj = $db->fetch_object($resql)) {
4837  $arraysocialnetworks = array();
4838  if (!empty($obj->skype)) {
4839  $arraysocialnetworks['skype'] = $obj->skype;
4840  }
4841  if (!empty($obj->twitter)) {
4842  $arraysocialnetworks['twitter'] = $obj->twitter;
4843  }
4844  if (!empty($obj->facebook)) {
4845  $arraysocialnetworks['facebook'] = $obj->facebook;
4846  }
4847  if (!empty($obj->linkedin)) {
4848  $arraysocialnetworks['linkedin'] = $obj->linkedin;
4849  }
4850  if (!empty($obj->instagram)) {
4851  $arraysocialnetworks['instagram'] = $obj->instagram;
4852  }
4853  if (!empty($obj->snapchat)) {
4854  $arraysocialnetworks['snapchat'] = $obj->snapchat;
4855  }
4856  if (!empty($obj->googleplus)) {
4857  $arraysocialnetworks['googleplus'] = $obj->googleplus;
4858  }
4859  if (!empty($obj->youtube)) {
4860  $arraysocialnetworks['youtube'] = $obj->youtube;
4861  }
4862  if (!empty($obj->whatsapp)) {
4863  $arraysocialnetworks['whatsapp'] = $obj->whatsapp;
4864  }
4865  if ($obj->socialnetworks == '' || is_null($obj->socialnetworks)) {
4866  $obj->socialnetworks = '[]';
4867  }
4868  $socialnetworks = array_merge($arraysocialnetworks, json_decode($obj->socialnetworks, true));
4869  $sqlupd = 'UPDATE '.MAIN_DB_PREFIX."adherent SET socialnetworks='".$db->escape(json_encode($socialnetworks, true))."'";
4870  $sqlupd .= ', skype=null';
4871  $sqlupd .= ', twitter=null';
4872  $sqlupd .= ', facebook=null';
4873  $sqlupd .= ', linkedin=null';
4874  $sqlupd .= ', instagram=null';
4875  $sqlupd .= ', snapchat=null';
4876  $sqlupd .= ', googleplus=null';
4877  $sqlupd .= ', youtube=null';
4878  $sqlupd .= ', whatsapp=null';
4879  $sqlupd .= ' WHERE rowid = '.((int) $obj->rowid);
4880  //print $sqlupd."<br>";
4881  $resqlupd = $db->query($sqlupd);
4882  if (!$resqlupd) {
4883  dol_print_error($db);
4884  $error++;
4885  }
4886  }
4887  } else {
4888  $error++;
4889  }
4890  if (!$error) {
4891  $db->commit();
4892  } else {
4893  dol_print_error($db);
4894  $db->rollback();
4895  }
4896  print '<b>'.$langs->trans('MigrationFieldsSocialNetworks', 'Members')."</b><br>\n";
4897  print '</td></tr>';
4898 }
4899 
4907 {
4908  global $db, $langs;
4909  // jabberid,skype,twitter,facebook,linkedin,instagram,snapchat,googleplus,youtube,whatsapp
4910  $error = 0;
4911  $db->begin();
4912  print '<tr><td colspan="4">';
4913  $sql = 'SELECT rowid, socialnetworks';
4914  $sql .= ', jabberid, skype, twitter, facebook, linkedin, instagram, snapchat, googleplus, youtube, whatsapp FROM '.MAIN_DB_PREFIX.'socpeople WHERE';
4915  $sql .= " jabberid IS NOT NULL OR jabberid <> ''";
4916  $sql .= " OR skype IS NOT NULL OR skype <> ''";
4917  $sql .= " OR twitter IS NOT NULL OR twitter <> ''";
4918  $sql .= " OR facebook IS NOT NULL OR facebook <> ''";
4919  $sql .= " OR linkedin IS NOT NULL OR linkedin <> ''";
4920  $sql .= " OR instagram IS NOT NULL OR instagram <> ''";
4921  $sql .= " OR snapchat IS NOT NULL OR snapchat <> ''";
4922  $sql .= " OR googleplus IS NOT NULL OR googleplus <> ''";
4923  $sql .= " OR youtube IS NOT NULL OR youtube <> ''";
4924  $sql .= " OR whatsapp IS NOT NULL OR whatsapp <> ''";
4925  //print $sql;
4926  $resql = $db->query($sql);
4927  if ($resql) {
4928  while ($obj = $db->fetch_object($resql)) {
4929  $arraysocialnetworks = array();
4930  if (!empty($obj->jabberid)) {
4931  $arraysocialnetworks['jabber'] = $obj->jabberid;
4932  }
4933  if (!empty($obj->skype)) {
4934  $arraysocialnetworks['skype'] = $obj->skype;
4935  }
4936  if (!empty($obj->twitter)) {
4937  $arraysocialnetworks['twitter'] = $obj->twitter;
4938  }
4939  if (!empty($obj->facebook)) {
4940  $arraysocialnetworks['facebook'] = $obj->facebook;
4941  }
4942  if (!empty($obj->linkedin)) {
4943  $arraysocialnetworks['linkedin'] = $obj->linkedin;
4944  }
4945  if (!empty($obj->instagram)) {
4946  $arraysocialnetworks['instagram'] = $obj->instagram;
4947  }
4948  if (!empty($obj->snapchat)) {
4949  $arraysocialnetworks['snapchat'] = $obj->snapchat;
4950  }
4951  if (!empty($obj->googleplus)) {
4952  $arraysocialnetworks['googleplus'] = $obj->googleplus;
4953  }
4954  if (!empty($obj->youtube)) {
4955  $arraysocialnetworks['youtube'] = $obj->youtube;
4956  }
4957  if (!empty($obj->whatsapp)) {
4958  $arraysocialnetworks['whatsapp'] = $obj->whatsapp;
4959  }
4960  if ($obj->socialnetworks == '' || is_null($obj->socialnetworks)) {
4961  $obj->socialnetworks = '[]';
4962  }
4963  $socialnetworks = array_merge($arraysocialnetworks, json_decode($obj->socialnetworks, true));
4964  $sqlupd = 'UPDATE '.MAIN_DB_PREFIX."socpeople SET socialnetworks='".$db->escape(json_encode($socialnetworks, true))."'";
4965  $sqlupd .= ', jabberid=null';
4966  $sqlupd .= ', skype=null';
4967  $sqlupd .= ', twitter=null';
4968  $sqlupd .= ', facebook=null';
4969  $sqlupd .= ', linkedin=null';
4970  $sqlupd .= ', instagram=null';
4971  $sqlupd .= ', snapchat=null';
4972  $sqlupd .= ', googleplus=null';
4973  $sqlupd .= ', youtube=null';
4974  $sqlupd .= ', whatsapp=null';
4975  $sqlupd .= ' WHERE rowid = '.((int) $obj->rowid);
4976  //print $sqlupd."<br>";
4977  $resqlupd = $db->query($sqlupd);
4978  if (!$resqlupd) {
4979  dol_print_error($db);
4980  $error++;
4981  }
4982  }
4983  } else {
4984  $error++;
4985  }
4986  if (!$error) {
4987  $db->commit();
4988  } else {
4989  dol_print_error($db);
4990  $db->rollback();
4991  }
4992  print '<b>'.$langs->trans('MigrationFieldsSocialNetworks', 'Contacts')."</b><br>\n";
4993  print '</td></tr>';
4994 }
4995 
5003 {
5004  global $db, $langs;
5005  // skype,twitter,facebook,linkedin,instagram,snapchat,googleplus,youtube,whatsapp
5006  $error = 0;
5007  $db->begin();
5008  print '<tr><td colspan="4">';
5009  $sql = 'SELECT rowid, socialnetworks';
5010  $sql .= ', skype, twitter, facebook, linkedin, instagram, snapchat, googleplus, youtube, whatsapp FROM '.MAIN_DB_PREFIX.'societe WHERE ';
5011  $sql .= " skype IS NOT NULL OR skype <> ''";
5012  $sql .= " OR twitter IS NOT NULL OR twitter <> ''";
5013  $sql .= " OR facebook IS NOT NULL OR facebook <> ''";
5014  $sql .= " OR linkedin IS NOT NULL OR linkedin <> ''";
5015  $sql .= " OR instagram IS NOT NULL OR instagram <> ''";
5016  $sql .= " OR snapchat IS NOT NULL OR snapchat <> ''";
5017  $sql .= " OR googleplus IS NOT NULL OR googleplus <> ''";
5018  $sql .= " OR youtube IS NOT NULL OR youtube <> ''";
5019  $sql .= " OR whatsapp IS NOT NULL OR whatsapp <> ''";
5020  //print $sql;
5021  $resql = $db->query($sql);
5022  if ($resql) {
5023  while ($obj = $db->fetch_object($resql)) {
5024  $arraysocialnetworks = array();
5025  if (!empty($obj->skype)) {
5026  $arraysocialnetworks['skype'] = $obj->skype;
5027  }
5028  if (!empty($obj->twitter)) {
5029  $arraysocialnetworks['twitter'] = $obj->twitter;
5030  }
5031  if (!empty($obj->facebook)) {
5032  $arraysocialnetworks['facebook'] = $obj->facebook;
5033  }
5034  if (!empty($obj->linkedin)) {
5035  $arraysocialnetworks['linkedin'] = $obj->linkedin;
5036  }
5037  if (!empty($obj->instagram)) {
5038  $arraysocialnetworks['instagram'] = $obj->instagram;
5039  }
5040  if (!empty($obj->snapchat)) {
5041  $arraysocialnetworks['snapchat'] = $obj->snapchat;
5042  }
5043  if (!empty($obj->googleplus)) {
5044  $arraysocialnetworks['googleplus'] = $obj->googleplus;
5045  }
5046  if (!empty($obj->youtube)) {
5047  $arraysocialnetworks['youtube'] = $obj->youtube;
5048  }
5049  if (!empty($obj->whatsapp)) {
5050  $arraysocialnetworks['whatsapp'] = $obj->whatsapp;
5051  }
5052  if ($obj->socialnetworks == '' || is_null($obj->socialnetworks)) {
5053  $obj->socialnetworks = '[]';
5054  }
5055  $socialnetworks = array_merge($arraysocialnetworks, json_decode($obj->socialnetworks, true));
5056  $sqlupd = 'UPDATE '.MAIN_DB_PREFIX."societe SET socialnetworks='".$db->escape(json_encode($socialnetworks, true))."'";
5057  $sqlupd .= ', skype=null';
5058  $sqlupd .= ', twitter=null';
5059  $sqlupd .= ', facebook=null';
5060  $sqlupd .= ', linkedin=null';
5061  $sqlupd .= ', instagram=null';
5062  $sqlupd .= ', snapchat=null';
5063  $sqlupd .= ', googleplus=null';
5064  $sqlupd .= ', youtube=null';
5065  $sqlupd .= ', whatsapp=null';
5066  $sqlupd .= ' WHERE rowid = '.((int) $obj->rowid);
5067  //print $sqlupd."<br>";
5068  $resqlupd = $db->query($sqlupd);
5069  if (!$resqlupd) {
5070  dol_print_error($db);
5071  $error++;
5072  }
5073  }
5074  } else {
5075  $error++;
5076  }
5077  if (!$error) {
5078  $db->commit();
5079  } else {
5080  dol_print_error($db);
5081  $db->rollback();
5082  }
5083  print '<b>'.$langs->trans('MigrationFieldsSocialNetworks', 'Thirdparties')."</b><br>\n";
5084  print '</td></tr>';
5085 }
5086 
5087 
5094 function migrate_export_import_profiles($mode = 'export')
5095 {
5096  global $db, $langs;
5097 
5098  $error = 0;
5099  $resultstring = '';
5100 
5101  $db->begin();
5102 
5103  print '<tr class="trforrunsql"><td colspan="4">';
5104  $sql = 'SELECT rowid, field';
5105  if ($mode == 'export') {
5106  $sql .= ', filter';
5107  }
5108  $sql .= ' FROM '.MAIN_DB_PREFIX.$mode.'_model WHERE';
5109  $sql .= " type LIKE 'propale_%' OR type LIKE 'commande_%' OR type LIKE 'facture_%'";
5110  //print $sql;
5111  $resql = $db->query($sql);
5112  if ($resql) {
5113  while ($obj = $db->fetch_object($resql)) {
5114  $oldfield = $obj->field;
5115  $newfield = str_replace(array(',f.facnumber', 'f.facnumber,', 'f.total,', 'f.tva,'), array(',f.ref', 'f.ref,', 'f.total_ht,', 'f.total_tva,'), $oldfield);
5116 
5117  if ($mode == 'export') {
5118  $oldfilter = $obj->filter;
5119  $newfilter = str_replace(array('f.facnumber=', 'f.total=', 'f.tva='), array('f.ref=', 'f.total_ht=', 'f.total_tva='), $oldfilter);
5120  } else {
5121  $oldfilter = '';
5122  $newfilter = '';
5123  }
5124 
5125  if ($oldfield != $newfield || $oldfilter != $newfilter) {
5126  $sqlupd = 'UPDATE '.MAIN_DB_PREFIX.$mode."_model SET field = '".$db->escape($newfield)."'";
5127  if ($mode == 'export') {
5128  $sqlupd .= ", filter = '".$db->escape($newfilter)."'";
5129  }
5130  $sqlupd .= ' WHERE rowid = '.((int) $obj->rowid);
5131  $resultstring .= '<tr class="trforrunsql" style=""><td class="wordbreak" colspan="4">'.$sqlupd."</td></tr>\n";
5132  $resqlupd = $db->query($sqlupd);
5133  if (!$resqlupd) {
5134  dol_print_error($db);
5135  $error++;
5136  }
5137  }
5138  }
5139  } else {
5140  $error++;
5141  }
5142  if (!$error) {
5143  $db->commit();
5144  } else {
5145  dol_print_error($db);
5146  $db->rollback();
5147  }
5148  print '<b>'.$langs->trans('MigrationImportOrExportProfiles', $mode)."</b><br>\n";
5149  print '</td></tr>';
5150 
5151  if ($resultstring) {
5152  print $resultstring;
5153  } else {
5154  print '<tr class="trforrunsql" style=""><td class="wordbreak" colspan="4">'.$langs->trans("NothingToDo")."</td></tr>\n";
5155  }
5156 }
5157 
5164 {
5165 
5166  global $db, $langs;
5167 
5168  $error = 0;
5169  $resultstring = '';
5170 
5171  $db->begin();
5172  print '<tr class="trforrunsql"><td colspan="4">';
5173  print '<b>'.$langs->trans('MigrationContractLineRank')."</b><br>\n";
5174 
5175  $sql = "SELECT c.rowid as cid ,cd.rowid as cdid,cd.rang FROM ".$db->prefix()."contratdet as cd INNER JOIN ".$db->prefix()."contrat as c ON c.rowid=cd.fk_contrat AND cd.rang=0";
5176  $sql .=" ORDER BY c.rowid,cd.rowid";
5177 
5178  $resql = $db->query($sql);
5179  if ($resql) {
5180  $currentRank=0;
5181  $current_contract=0;
5182  while ($obj = $db->fetch_object($resql)) {
5183  if (empty($current_contract) || $current_contract==$obj->cid) {
5184  $currentRank++;
5185  } else {
5186  $currentRank=1;
5187  }
5188 
5189  $sqlUpd = "UPDATE ".$db->prefix()."contratdet SET rang=".(int) $currentRank." WHERE rowid=".(int) $obj->cdid;
5190  $resultstring = '.';
5191  print $resultstring;
5192  $resqlUpd = $db->query($sqlUpd);
5193  if (!$resqlUpd) {
5194  dol_print_error($db);
5195  $error++;
5196  }
5197 
5198  $current_contract = $obj->cid;
5199  }
5200  } else {
5201  $error++;
5202  }
5203  if (!$error) {
5204  $db->commit();
5205  } else {
5206  $db->rollback();
5207  }
5208 
5209  print '</td></tr>';
5210 
5211  if (!$resultstring) {
5212  print '<tr class="trforrunsql" style=""><td class="wordbreak" colspan="4">'.$langs->trans("NothingToDo")."</td></tr>\n";
5213  }
5214 }
run_sql($sqlfile, $silent=1, $entity='', $usesavepoint=1, $handler='', $okerror='default', $linelengthlimit=32768, $nocommentremoval=0, $offsetforchartofaccount=0, $colspan=0, $onlysqltoimportwebsite=0)
Launch a sql file.
Definition: admin.lib.php:167
versioncompare($versionarray1, $versionarray2)
Compare 2 versions (stored into 2 arrays).
Definition: admin.lib.php:66
Class to manage Blocked Log.
Class to manage line orders.
Class to manage lines of contracts.
Class to manage invoices.
Class to manage invoice lines.
Class to manage hooks.
static liste_modeles($db, $maxfilenamelength=0)
Return list of active generation modules.
static liste_modeles($db, $maxfilenamelength=0)
Return list of active generation modules.
Class to manage order lines.
Class to manage commercial proposal lines.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
Definition: user.class.php:45
Class to describe and enable/disable module Agenda.
Description and activation class for module Api.
Class to describe Barcode.
Class to describe a BlockedLog module.
Class to describe module Sales Orders.
Class to describe a Cron module.
Class to describe and enable module Deplacement.
Class to describe and enable module Donation.
Description and activation class for module ECM.
Description and activation class for module EventOrganization This module is base on this specificati...
Description and activation class for module ExpenseReport.
Description and activation class for module ExternalSite.
Class to describe module customer invoices.
Description and activation class for module Fournisseur.
Description and activation class for module holiday.
Description and activation class for module KnowledgeManagement.
Description and activation class for module opensurvey.
Description and activation class for module productdluo.
Class descriptor of Product module.
Class to describe and enable module Service.
Class to describe and enable module Societe.
Class to describe and enable module SupplierProposal.
Class to describe and enable module TakePos.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
Definition: files.lib.php:1401
dol_copy($srcfile, $destfile, $newmask=0, $overwriteifexists=1)
Copy a file to another file.
Definition: files.lib.php:712
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
Definition: files.lib.php:1250
dol_is_file($pathoffile)
Return if path is a file.
Definition: files.lib.php:480
dolCopyDir($srcfile, $destfile, $newmask, $overwriteifexists, $arrayreplacement=null, $excludesubdir=0, $excludefileext=null)
Copy a dir to another dir.
Definition: files.lib.php:772
dol_is_dir($folder)
Test if filename is a directory.
Definition: files.lib.php:450
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_now($mode='auto')
Return date for now.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
isModEnabled($module)
Is Dolibarr module enabled.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
getDoliDBInstance($type, $host, $user, $pass, $name, $port)
Return a DoliDB instance (database handler).
pHeader($subtitle, $next, $action='set', $param='', $forcejqueryurl='', $csstable='main-inside')
Show HTML header of install pages.
Definition: inc.php:401
pFooter($nonext=0, $setuplang='', $jscheckfunction='', $withpleasewait=0, $morehtml='')
Print HTML footer of install pages.
Definition: inc.php:490
dolibarr_install_syslog($message, $level=LOG_DEBUG)
Log function for install pages.
Definition: inc.php:551
div float
Buy price without taxes.
Definition: style.css.php:913
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller='', $localtaxes_array='', $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code='')
Calculate totals (net, vat, ...) of a line.
Definition: price.lib.php:86
dol_decode($chain, $key='1')
Decode a base 64 encoded + specific delta change.
migrate_links_transfert($db, $langs, $conf)
Function to migrate links into llx_bank_url.
Definition: upgrade2.php:1139
migrate_paiements_orphelins_1($db, $langs, $conf)
Corrige paiement orphelins (liens paumes suite a bugs) Pour verifier s'il reste des orphelins: select...
Definition: upgrade2.php:820
migrate_user_rights_entity($db, $langs, $conf)
Migrate to add entity value into llx_user_rights.
Definition: upgrade2.php:3940
migrate_reload_modules($db, $langs, $conf, $listofmodule=array(), $force=0)
Disable/Reenable features modules.
Definition: upgrade2.php:4226
migrate_project_task_time($db, $langs, $conf)
Migrate duration in seconds.
Definition: upgrade2.php:2903
migrate_contracts_date3($db, $langs, $conf)
Mise a jour des dates de creation de contrat.
Definition: upgrade2.php:1323
migrate_restore_missing_links($db, $langs, $conf)
Migration du champ fk_remise_except dans llx_facturedet doit correspondre a lien dans llx_societe_rem...
Definition: upgrade2.php:2526
migrate_rename_directories($db, $langs, $conf, $oldname, $newname)
Migration directory.
Definition: upgrade2.php:4072
migrate_project_task_actors($db, $langs, $conf)
Migration de la table llx_projet_task_actors vers llx_element_contact.
Definition: upgrade2.php:2742
migrate_commande_deliveryaddress($db, $langs, $conf)
Migration du champ fk_adresse_livraison dans expedition 2.6 -> 2.7.
Definition: upgrade2.php:2456
migrate_project_user_resp($db, $langs, $conf)
Migration du champ fk_user_resp de llx_projet vers llx_element_contact.
Definition: upgrade2.php:2661
migrate_contracts_open($db, $langs, $conf)
Reouverture des contrats qui ont au moins une ligne non fermee.
Definition: upgrade2.php:1353
migrate_shipping_delivery($db, $langs, $conf)
Migrate link stored into fk_expedition into llx_element_element.
Definition: upgrade2.php:3092
migrate_delete_old_dir($db, $langs, $conf)
Remove deprecated directories.
Definition: upgrade2.php:4181
if($db->connected) if($ret) migrate_paiements($db, $langs, $conf)
Reporte liens vers une facture de paiements sur table de jointure (lien n-n paiements factures)
Definition: upgrade2.php:741
migrate_price_propal($db, $langs, $conf)
Update total of proposal lines.
Definition: upgrade2.php:1617
migrate_stocks($db, $langs, $conf)
Migration du champ stock dans produits.
Definition: upgrade2.php:2327
migrate_paiementfourn_facturefourn($db, $langs, $conf)
Factures fournisseurs.
Definition: upgrade2.php:1416
migrate_contacts_socialnetworks()
Migrate contacts fields facebook and co to socialnetworks Can be called only when version is 10....
Definition: upgrade2.php:4906
migrate_customerorder_shipping($db, $langs, $conf)
Migrate order ref_customer and date_delivery fields to llx_expedition.
Definition: upgrade2.php:3008
migrate_detail_livraison($db, $langs, $conf)
Migration des details commandes dans les details livraisons.
Definition: upgrade2.php:2225
migrate_remise_except_entity($db, $langs, $conf)
Migrate to add entity value into llx_societe_remise_except.
Definition: upgrade2.php:3848
migrate_paiements_orphelins_2($db, $langs, $conf)
Corrige paiement orphelins (liens paumes suite a bugs) Pour verifier s'il reste des orphelins: select...
Definition: upgrade2.php:930
migrate_price_contrat($db, $langs, $conf)
Update total of contract lines.
Definition: upgrade2.php:1718
migrate_users_socialnetworks()
Migrate users fields facebook and co to socialnetworks.
Definition: upgrade2.php:4723
migrate_user_photospath2()
Migrate file from old path users/99/file.jpg into users/99/photos/file.jpg.
Definition: upgrade2.php:4622
migrate_contracts_det($db, $langs, $conf)
Mise a jour des contrats (gestion du contrat + detail de contrat)
Definition: upgrade2.php:1054
migrate_price_facture($db, $langs, $conf)
Update total of invoice lines.
Definition: upgrade2.php:1512
migrate_usergroup_rights_entity($db, $langs, $conf)
Migrate to add entity value into llx_usergroup_rights.
Definition: upgrade2.php:4005
migrate_relationship_tables($db, $langs, $conf, $table, $fk_source, $sourcetype, $fk_target, $targettype)
Migration des tables de relation.
Definition: upgrade2.php:2824
migrate_modeles($db, $langs, $conf)
Mise a jour des modeles selectionnes.
Definition: upgrade2.php:2022
migrate_reset_blocked_log($db, $langs, $conf)
Migrate to reset the blocked log for V7+ algorithm.
Definition: upgrade2.php:3683
migrate_mode_reglement($db, $langs, $conf)
Migrate link stored into fk_mode_reglement.
Definition: upgrade2.php:3319
migrate_export_import_profiles($mode='export')
Migrate export and import profiles to fix field name that was renamed.
Definition: upgrade2.php:5094
migrate_contracts_date2($db, $langs, $conf)
Update contracts with date min real if service date is lower.
Definition: upgrade2.php:1255
migrate_commande_expedition($db, $langs, $conf)
Correspondance des expeditions et des commandes clients dans la table llx_co_exp.
Definition: upgrade2.php:2080
migrate_actioncomm_element($db, $langs, $conf)
Migrate link stored into fk_xxxx into fk_element and elementtype.
Definition: upgrade2.php:3260
migrate_user_photospath()
Migrate file from old path to new one for users.
Definition: upgrade2.php:4535
migrate_thirdparties_socialnetworks()
Migrate thirdparties fields facebook and co to socialnetworks Can be called only when version is 10....
Definition: upgrade2.php:5002
migrate_event_assignement($db, $langs, $conf)
Migrate event assignement to owner.
Definition: upgrade2.php:3550
migrate_clean_association($db, $langs, $conf)
Delete duplicates in table categorie_association.
Definition: upgrade2.php:3409
migrate_commande_livraison($db, $langs, $conf)
Correspondance des livraisons et des commandes clients dans la table llx_co_liv.
Definition: upgrade2.php:2145
migrate_event_assignement_contact($db, $langs, $conf)
Migrate event assignement to owner.
Definition: upgrade2.php:3616
migrate_contracts_date1($db, $langs, $conf)
Mise a jour des date de contrats non renseignees.
Definition: upgrade2.php:1213
migrate_remise_entity($db, $langs, $conf)
Migrate to add entity value into llx_societe_remise.
Definition: upgrade2.php:3782
migrate_contractdet_rank()
Migrate Rank into contract line.
Definition: upgrade2.php:5163
migrate_menus($db, $langs, $conf)
Migration of menus (use only 1 table instead of 3) 2.6 -> 2.7.
Definition: upgrade2.php:2389
migrate_categorie_association($db, $langs, $conf)
Migrate categorie association.
Definition: upgrade2.php:3484
migrate_delete_old_files($db, $langs, $conf)
Delete deprecated files.
Definition: upgrade2.php:4091
migrate_price_commande_fournisseur($db, $langs, $conf)
Update total of purchase order lines.
Definition: upgrade2.php:1912
migrate_members_socialnetworks()
Migrate members fields facebook and co to socialnetworks Can be called only when version is 10....
Definition: upgrade2.php:4814
migrate_reload_menu($db, $langs, $conf)
Reload SQL menu file (if dynamic menus, if modified by version)
Definition: upgrade2.php:4498
migrate_price_commande($db, $langs, $conf)
Update total of sales order lines.
Definition: upgrade2.php:1802
migrate_shipping_delivery2($db, $langs, $conf)
We try to complete field ref_customer and date_delivery that are empty into llx_livraison.
Definition: upgrade2.php:3187