dolibarr  16.0.5
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 (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 (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 (!empty($conf->multicompany->enabled)) {
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) {
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) {
484  }
485  }
486 
487 
488  // Code executed only if migration is LAST ONE. Must always be done.
489  if (versioncompare($versiontoarray, $versionranarray) >= 0 || versioncompare($versiontoarray, $versionranarray) <= -3) {
490  // 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)
491  $listofmodule = array(
492  'MAIN_MODULE_ACCOUNTING'=>'newboxdefonly',
493  'MAIN_MODULE_AGENDA'=>'newboxdefonly',
494  'MAIN_MODULE_BOM'=>'menuonly',
495  'MAIN_MODULE_BANQUE'=>'menuonly',
496  'MAIN_MODULE_BARCODE'=>'newboxdefonly',
497  'MAIN_MODULE_CRON'=>'newboxdefonly',
498  'MAIN_MODULE_COMMANDE'=>'newboxdefonly',
499  'MAIN_MODULE_BLOCKEDLOG'=>'noboxes',
500  'MAIN_MODULE_DEPLACEMENT'=>'newboxdefonly',
501  'MAIN_MODULE_DON'=>'newboxdefonly',
502  'MAIN_MODULE_ECM'=>'newboxdefonly',
503  'MAIN_MODULE_EXTERNALSITE'=>'newboxdefonly',
504  'MAIN_MODULE_EXPENSEREPORT'=>'newboxdefonly',
505  'MAIN_MODULE_FACTURE'=>'newboxdefonly',
506  'MAIN_MODULE_FOURNISSEUR'=>'newboxdefonly',
507  'MAIN_MODULE_HOLIDAY'=>'newboxdefonly',
508  'MAIN_MODULE_MARGIN'=>'menuonly',
509  'MAIN_MODULE_MRP'=>'menuonly',
510  'MAIN_MODULE_OPENSURVEY'=>'newboxdefonly',
511  'MAIN_MODULE_PAYBOX'=>'newboxdefonly',
512  'MAIN_MODULE_PRINTING'=>'newboxdefonly',
513  'MAIN_MODULE_PRODUIT'=>'newboxdefonly',
514  'MAIN_MODULE_RECRUITMENT'=>'menuonly',
515  'MAIN_MODULE_RESOURCE'=>'noboxes',
516  'MAIN_MODULE_SALARIES'=>'newboxdefonly',
517  'MAIN_MODULE_SERVICE'=>'newboxdefonly',
518  'MAIN_MODULE_SYSLOG'=>'newboxdefonly',
519  'MAIN_MODULE_SOCIETE'=>'newboxdefonly',
520  'MAIN_MODULE_STRIPE'=>'menuonly',
521  'MAIN_MODULE_TICKET'=>'newboxdefonly',
522  'MAIN_MODULE_TAKEPOS'=>'newboxdefonly',
523  'MAIN_MODULE_USER'=>'newboxdefonly', //This one must be always done and only into last targeted version)
524  'MAIN_MODULE_VARIANTS'=>'newboxdefonly',
525  'MAIN_MODULE_WEBSITE'=>'newboxdefonly',
526  );
527 
528  $result = migrate_reload_modules($db, $langs, $conf, $listofmodule);
529  if ($result < 0) {
530  $error++;
531  }
532  // Reload menus (this must be always and only into last targeted version)
533  $result = migrate_reload_menu($db, $langs, $conf);
534  if ($result < 0) {
535  $error++;
536  }
537  }
538 
539  // Can force activation of some module during migration with parameter 'enablemodules=MAIN_MODULE_XXX,MAIN_MODULE_YYY,...'
540  // In most cases (online install or upgrade) $enablemodules is empty. Can be forced when ran from command line.
541  if (!$error && $enablemodules) {
542  // Reload modules (this must be always done and only into last targeted version)
543  $listofmodules = array();
544  $enablemodules = preg_replace('/enablemodules=/', '', $enablemodules);
545  $tmplistofmodules = explode(',', $enablemodules);
546  foreach ($tmplistofmodules as $value) {
547  $listofmodules[$value] = 'forceactivate';
548  }
549 
550  $resultreloadmodules = migrate_reload_modules($db, $langs, $conf, $listofmodules, 1);
551  if ($resultreloadmodules < 0) {
552  $error++;
553  }
554  }
555 
556 
557  // Can call a dedicated external upgrade process with hook doUpgradeAfterDB()
558  if (!$error) {
559  $parameters = array('versionfrom' => $versionfrom, 'versionto' => $versionto, 'conf'=>$conf);
560  $object = new stdClass();
561  $action = "upgrade";
562  $reshook = $hookmanager->executeHooks('doUpgradeAfterDB', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
563  if ($hookmanager->resNbOfHooks > 0) {
564  if ($reshook < 0) {
565  print '<tr><td colspan="4">';
566  print '<b>'.$langs->trans('UpgradeExternalModule').'</b>: ';
567  print $hookmanager->error;
568  print "<!-- (".$reshook.") -->";
569  print '</td></tr>';
570  } else {
571  print '<tr class="trforrunsql"><td colspan="4">';
572  print '<b>'.$langs->trans('UpgradeExternalModule').' (DB)</b>: <span class="ok">OK</span>';
573  print "<!-- (".$reshook.") -->";
574  print '</td></tr>';
575  }
576  } else {
577  //if (! empty($conf->modules))
578  if (!empty($conf->modules_parts['hooks'])) { // If there is at least one module with one hook, we show message to say nothing was done
579  print '<tr class="trforrunsql"><td colspan="4">';
580  print '<b>'.$langs->trans('UpgradeExternalModule').'</b>: '.$langs->trans("NodoUpgradeAfterDB");
581  print '</td></tr>';
582  }
583  }
584  }
585  }
586 
587  print '</table>';
588 
589  if (!$error) {
590  // Set constant to ask to remake a new ping to inform about upgrade (if first ping was done and OK)
591  $sql = 'UPDATE '.MAIN_DB_PREFIX."const SET VALUE = 'torefresh' WHERE name = 'MAIN_FIRST_PING_OK_ID'";
592  $db->query($sql, 1);
593  }
594 
595  // We always commit.
596  // Process is designed so we can run it several times whatever is situation.
597  $db->commit();
598 
599 
600  /***************************************************************************************
601  *
602  * Migration of files
603  *
604  ***************************************************************************************/
605 
606  foreach ($listofentities as $entity) {
607  // Set $conf context for entity
608  $conf->setEntityValues($db, $entity);
609  // Reset forced setup after the setValues
610  if (defined('SYSLOG_FILE')) {
611  $conf->global->SYSLOG_FILE = constant('SYSLOG_FILE');
612  }
613  $conf->global->MAIN_ENABLE_LOG_TO_HTML = 1;
614 
615 
616  // Copy directory medias
617  $srcroot = DOL_DOCUMENT_ROOT.'/install/medias';
618  $destroot = DOL_DATA_ROOT.'/medias';
619  dolCopyDir($srcroot, $destroot, 0, 0);
620 
621 
622  // Actions for all versions (no database change but delete some files and directories)
623  migrate_delete_old_files($db, $langs, $conf);
624  migrate_delete_old_dir($db, $langs, $conf);
625  // Actions for all versions (no database change but create some directories)
626  dol_mkdir(DOL_DATA_ROOT.'/bank');
627  // Actions for all versions (no database change but rename some directories)
628  migrate_rename_directories($db, $langs, $conf, '/banque/bordereau', '/bank/checkdeposits');
629 
630 
631  $parameters = array('versionfrom' => $versionfrom, 'versionto' => $versionto, 'conf'=>$conf);
632  $object = new stdClass();
633  $action = "upgrade";
634  $reshook = $hookmanager->executeHooks('doUpgradeAfterFiles', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
635  if ($hookmanager->resNbOfHooks > 0) {
636  if ($reshook < 0) {
637  print '<tr><td colspan="4">';
638  print '<b>'.$langs->trans('UpgradeExternalModule').'</b>: ';
639  print $hookmanager->error;
640  print "<!-- (".$reshook.") -->";
641  print '</td></tr>';
642  } else {
643  print '<tr class="trforrunsql"><td colspan="4">';
644  print '<b>'.$langs->trans('UpgradeExternalModule').' (Files)</b>: <span class="ok">OK</span>';
645  print "<!-- (".$reshook.") -->";
646  print '</td></tr>';
647  }
648  } else {
649  //if (! empty($conf->modules))
650  if (!empty($conf->modules_parts['hooks'])) { // If there is at least one module with one hook, we show message to say nothing was done
651  print '<tr class="trforrunsql"><td colspan="4">';
652  print '<b>'.$langs->trans('UpgradeExternalModule').'</b>: '.$langs->trans("NodoUpgradeAfterFiles");
653  print '</td></tr>';
654  }
655  }
656  }
657 
658  $db->close();
659 
660  $silent = 0;
661  if (!$silent) {
662  print '<table width="100%">';
663  print '<tr><td style="width: 30%">'.$langs->trans("MigrationFinished").'</td>';
664  print '<td class="right">';
665  if ($error == 0) {
666  //print '<span class="ok">'.$langs->trans("OK").'</span> - '; // $error = 0 does not mean there is no error (error are not always trapped)
667  } else {
668  print '<span class="error">'.$langs->trans("Error").'</span> - ';
669  }
670 
671  //if (!empty($conf->use_javascript_ajax)) { // use_javascript_ajax is not defined
672  print '<script type="text/javascript">
673  jQuery(document).ready(function() {
674  function init_trrunsql()
675  {
676  console.log("toggle .trforrunsql");
677  jQuery(".trforrunsql").toggle();
678  }
679  init_trrunsql();
680  jQuery(".trforrunsqlshowhide").click(function() {
681  init_trrunsql();
682  });
683  });
684  </script>';
685  print '<a class="trforrunsqlshowhide" href="#">'.$langs->trans("ShowHideDetails").'</a>';
686  //}
687 
688  print '</td></tr>'."\n";
689  print '</table>';
690  }
691 
692  //print '<div><br>'.$langs->trans("MigrationFinished").'</div>';
693 } else {
694  print '<div class="error">'.$langs->trans('ErrorWrongParameters').'</div>';
695  $error++;
696 }
697 
698 $ret = 0;
699 if ($error && isset($argv[1])) {
700  $ret = 1;
701 }
702 dolibarr_install_syslog("Exit ".$ret);
703 
704 dolibarr_install_syslog("--- upgrade2: end");
705 pFooter($error ? 2 : 0, $setuplang);
706 
707 if ($db->connected) {
708  $db->close();
709 }
710 
711 // Return code if ran from command line
712 if ($ret) {
713  exit($ret);
714 }
715 
716 
717 
726 function migrate_paiements($db, $langs, $conf)
727 {
728  print '<tr><td colspan="4">';
729 
730  print '<br>';
731  print '<b>'.$langs->trans('MigrationPaymentsUpdate')."</b><br>\n";
732 
733  $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiement", "fk_facture");
734  $obj = $db->fetch_object($result);
735  if ($obj) {
736  $sql = "SELECT p.rowid, p.fk_facture, p.amount";
737  $sql .= " FROM ".MAIN_DB_PREFIX."paiement as p";
738  $sql .= " WHERE p.fk_facture > 0";
739 
740  $resql = $db->query($sql);
741 
742  dolibarr_install_syslog("upgrade2::migrate_paiements");
743  if ($resql) {
744  $i = 0;
745  $row = array();
746  $num = $db->num_rows($resql);
747 
748  while ($i < $num) {
749  $obj = $db->fetch_object($resql);
750  $row[$i][0] = $obj->rowid;
751  $row[$i][1] = $obj->fk_facture;
752  $row[$i][2] = $obj->amount;
753  $i++;
754  }
755  } else {
756  dol_print_error($db);
757  }
758 
759  if ($num) {
760  print $langs->trans('MigrationPaymentsNumberToUpdate', $num)."<br>\n";
761  if ($db->begin()) {
762  $res = 0;
763  $num = count($row);
764  for ($i = 0; $i < $num; $i++) {
765  $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount)";
766  $sql .= " VALUES (".((int) $row[$i][1]).",".((int) $row[$i][0]).",".((float) $row[$i][2]).")";
767 
768  $res += $db->query($sql);
769 
770  $sql = "UPDATE ".MAIN_DB_PREFIX."paiement SET fk_facture = 0 WHERE rowid = ".((int) $row[$i][0]);
771 
772  $res += $db->query($sql);
773 
774  print $langs->trans('MigrationProcessPaymentUpdate', $row[$i][0])."<br>\n";
775  }
776  }
777 
778  if ($res == (2 * count($row))) {
779  $db->commit();
780  print $langs->trans('MigrationSuccessfullUpdate')."<br>";
781  } else {
782  $db->rollback();
783  print $langs->trans('MigrationUpdateFailed').'<br>';
784  }
785  } else {
786  print $langs->trans('MigrationPaymentsNothingToUpdate')."<br>\n";
787  }
788  } else {
789  print $langs->trans('MigrationPaymentsNothingToUpdate')."<br>\n";
790  }
791 
792  print '</td></tr>';
793 }
794 
805 function migrate_paiements_orphelins_1($db, $langs, $conf)
806 {
807  print '<tr><td colspan="4">';
808 
809  print '<br>';
810  print '<b>'.$langs->trans('MigrationPaymentsUpdate')."</b><br>\n";
811 
812  $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiement", "fk_facture");
813  $obj = $db->fetch_object($result);
814  if ($obj) {
815  // Tous les enregistrements qui sortent de cette requete devrait avoir un pere dans llx_paiement_facture
816  $sql = "SELECT distinct p.rowid, p.datec, p.amount as pamount, bu.fk_bank, b.amount as bamount,";
817  $sql .= " bu2.url_id as socid";
818  $sql .= " FROM (".MAIN_DB_PREFIX."paiement as p, ".MAIN_DB_PREFIX."bank_url as bu, ".MAIN_DB_PREFIX."bank as b)";
819  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON pf.fk_paiement = p.rowid";
820  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu2 ON (bu.fk_bank=bu2.fk_bank AND bu2.type = 'company')";
821  $sql .= " WHERE pf.rowid IS NULL AND (p.rowid=bu.url_id AND bu.type='payment') AND bu.fk_bank = b.rowid";
822  $sql .= " AND b.rappro = 1";
823  $sql .= " AND (p.fk_facture = 0 OR p.fk_facture IS NULL)";
824 
825  $resql = $db->query($sql);
826 
827  dolibarr_install_syslog("upgrade2::migrate_paiements_orphelins_1");
828  $row = array();
829  if ($resql) {
830  $i = $j = 0;
831  $num = $db->num_rows($resql);
832 
833  while ($i < $num) {
834  $obj = $db->fetch_object($resql);
835  if ($obj->pamount == $obj->bamount && $obj->socid) { // Pour etre sur d'avoir bon cas
836  $row[$j]['paymentid'] = $obj->rowid; // paymentid
837  $row[$j]['pamount'] = $obj->pamount;
838  $row[$j]['fk_bank'] = $obj->fk_bank;
839  $row[$j]['bamount'] = $obj->bamount;
840  $row[$j]['socid'] = $obj->socid;
841  $row[$j]['datec'] = $obj->datec;
842  $j++;
843  }
844  $i++;
845  }
846  } else {
847  dol_print_error($db);
848  }
849 
850  if (count($row)) {
851  print $langs->trans('OrphelinsPaymentsDetectedByMethod', 1).': '.count($row)."<br>\n";
852  $db->begin();
853 
854  $res = 0;
855  $num = count($row);
856  for ($i = 0; $i < $num; $i++) {
857  if ($conf->global->MAIN_FEATURES_LEVEL == 2) {
858  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>';
859  }
860 
861  // On cherche facture sans lien paiement et du meme montant et pour meme societe.
862  $sql = " SELECT distinct f.rowid from ".MAIN_DB_PREFIX."facture as f";
863  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture";
864  $sql .= " WHERE f.fk_statut in (2,3) AND fk_soc = ".((int) $row[$i]['socid'])." AND total_ttc = ".((float) $row[$i]['pamount']);
865  $sql .= " AND pf.fk_facture IS NULL";
866  $sql .= " ORDER BY f.fk_statut";
867  //print $sql.'<br>';
868  $resql = $db->query($sql);
869  if ($resql) {
870  $num = $db->num_rows($resql);
871  //print 'Nb of invoice found for this amount and company :'.$num.'<br>';
872  if ($num >= 1) {
873  $obj = $db->fetch_object($resql);
874  $facid = $obj->rowid;
875 
876  $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount)";
877  $sql .= " VALUES (".((int) $facid).",".((int) $row[$i]['paymentid']).", ".((float) $row[$i]['pamount']).")";
878 
879  $res += $db->query($sql);
880 
881  print $langs->trans('MigrationProcessPaymentUpdate', 'facid='.$facid.'-paymentid='.$row[$i]['paymentid'].'-amount='.$row[$i]['pamount'])."<br>\n";
882  }
883  } else {
884  print 'ERROR';
885  }
886  }
887 
888  if ($res > 0) {
889  print $langs->trans('MigrationSuccessfullUpdate')."<br>";
890  } else {
891  print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
892  }
893 
894  $db->commit();
895  } else {
896  print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
897  }
898  } else {
899  print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
900  }
901 
902  print '</td></tr>';
903 }
904 
915 function migrate_paiements_orphelins_2($db, $langs, $conf)
916 {
917  print '<tr><td colspan="4">';
918 
919  print '<br>';
920  print '<b>'.$langs->trans('MigrationPaymentsUpdate')."</b><br>\n";
921 
922  $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiement", "fk_facture");
923  $obj = $db->fetch_object($result);
924  if ($obj) {
925  // Tous les enregistrements qui sortent de cette requete devrait avoir un pere dans llx_paiement_facture
926  $sql = "SELECT distinct p.rowid, p.datec, p.amount as pamount, bu.fk_bank, b.amount as bamount,";
927  $sql .= " bu2.url_id as socid";
928  $sql .= " FROM (".MAIN_DB_PREFIX."paiement as p, ".MAIN_DB_PREFIX."bank_url as bu, ".MAIN_DB_PREFIX."bank as b)";
929  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON pf.fk_paiement = p.rowid";
930  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu2 ON (bu.fk_bank = bu2.fk_bank AND bu2.type = 'company')";
931  $sql .= " WHERE pf.rowid IS NULL AND (p.fk_bank = bu.fk_bank AND bu.type = 'payment') AND bu.fk_bank = b.rowid";
932  $sql .= " AND (p.fk_facture = 0 OR p.fk_facture IS NULL)";
933 
934  $resql = $db->query($sql);
935 
936  dolibarr_install_syslog("upgrade2::migrate_paiements_orphelins_2");
937  $row = array();
938  if ($resql) {
939  $i = $j = 0;
940  $num = $db->num_rows($resql);
941 
942  while ($i < $num) {
943  $obj = $db->fetch_object($resql);
944  if ($obj->pamount == $obj->bamount && $obj->socid) { // Pour etre sur d'avoir bon cas
945  $row[$j]['paymentid'] = $obj->rowid; // paymentid
946  $row[$j]['pamount'] = $obj->pamount;
947  $row[$j]['fk_bank'] = $obj->fk_bank;
948  $row[$j]['bamount'] = $obj->bamount;
949  $row[$j]['socid'] = $obj->socid;
950  $row[$j]['datec'] = $obj->datec;
951  $j++;
952  }
953  $i++;
954  }
955  } else {
956  dol_print_error($db);
957  }
958 
959  $nberr = 0;
960 
961  $num = count($row);
962  if ($num) {
963  print $langs->trans('OrphelinsPaymentsDetectedByMethod', 2).': '.count($row)."<br>\n";
964  $db->begin();
965 
966  $res = 0;
967  for ($i = 0; $i < $num; $i++) {
968  if ($conf->global->MAIN_FEATURES_LEVEL == 2) {
969  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>';
970  }
971 
972  // On cherche facture sans lien paiement et du meme montant et pour meme societe.
973  $sql = " SELECT distinct f.rowid from ".MAIN_DB_PREFIX."facture as f";
974  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture";
975  $sql .= " WHERE f.fk_statut in (2,3) AND fk_soc = ".((int) $row[$i]['socid'])." AND total_ttc = ".((float) $row[$i]['pamount']);
976  $sql .= " AND pf.fk_facture IS NULL";
977  $sql .= " ORDER BY f.fk_statut";
978  //print $sql.'<br>';
979  $resql = $db->query($sql);
980  if ($resql) {
981  $num = $db->num_rows($resql);
982  //print 'Nb of invoice found for this amount and company :'.$num.'<br>';
983  if ($num >= 1) {
984  $obj = $db->fetch_object($resql);
985  $facid = $obj->rowid;
986 
987  $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount)";
988  $sql .= " VALUES (".((int) $facid).",".((int) $row[$i]['paymentid']).", ".((float) $row[$i]['pamount']).")";
989 
990  $res += $db->query($sql);
991 
992  print $langs->trans('MigrationProcessPaymentUpdate', 'facid='.$facid.'-paymentid='.$row[$i]['paymentid'].'-amount='.$row[$i]['pamount'])."<br>\n";
993  }
994  } else {
995  print 'ERROR';
996  $nberr++;
997  }
998  }
999 
1000  if ($res > 0) {
1001  print $langs->trans('MigrationSuccessfullUpdate')."<br>";
1002  } else {
1003  print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
1004  }
1005 
1006  $db->commit();
1007  } else {
1008  print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
1009  }
1010 
1011  // Delete obsolete fields fk_facture
1012  $db->begin();
1013 
1014  $sql = "ALTER TABLE ".MAIN_DB_PREFIX."paiement DROP COLUMN fk_facture";
1015  $db->query($sql);
1016 
1017  if (!$nberr) {
1018  $db->commit();
1019  } else {
1020  print 'ERROR';
1021  $db->rollback();
1022  }
1023  } else {
1024  print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
1025  }
1026 
1027  print '</td></tr>';
1028 }
1029 
1030 
1039 function migrate_contracts_det($db, $langs, $conf)
1040 {
1041  print '<tr><td colspan="4">';
1042 
1043  $nberr = 0;
1044 
1045  print '<br>';
1046  print '<b>'.$langs->trans('MigrationContractsUpdate')."</b><br>\n";
1047 
1048  $sql = "SELECT c.rowid as cref, c.date_contrat, c.statut, c.fk_product, c.fk_facture, c.fk_user_author,";
1049  $sql .= " p.ref, p.label, p.description, p.price, p.tva_tx, p.duration, cd.rowid";
1050  $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c";
1051  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p";
1052  $sql .= " ON c.fk_product = p.rowid";
1053  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."contratdet as cd";
1054  $sql .= " ON c.rowid=cd.fk_contrat";
1055  $sql .= " WHERE cd.rowid IS NULL AND p.rowid IS NOT NULL";
1056  $resql = $db->query($sql);
1057 
1058  dolibarr_install_syslog("upgrade2::migrate_contracts_det");
1059  if ($resql) {
1060  $i = 0;
1061  $row = array();
1062  $num = $db->num_rows($resql);
1063 
1064  if ($num) {
1065  print $langs->trans('MigrationContractsNumberToUpdate', $num)."<br>\n";
1066  $db->begin();
1067 
1068  while ($i < $num) {
1069  $obj = $db->fetch_object($resql);
1070 
1071  $sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet (";
1072  $sql .= "fk_contrat, fk_product, statut, label, description,";
1073  $sql .= "date_ouverture_prevue, date_ouverture, date_fin_validite, tva_tx, qty,";
1074  $sql .= "subprice, price_ht, fk_user_author, fk_user_ouverture)";
1075  $sql .= " VALUES (";
1076  $sql .= ((int) $obj->cref).", ".($obj->fk_product ? ((int) $obj->fk_product) : 0).", ";
1077  $sql .= "0, ";
1078  $sql .= "'".$db->escape($obj->label)."', null, ";
1079  $sql .= ($obj->date_contrat ? "'".$db->idate($db->jdate($obj->date_contrat))."'" : "null").", ";
1080  $sql .= "null, ";
1081  $sql .= "null, ";
1082  $sql .= ((float) $obj->tva_tx).", 1, ";
1083  $sql .= ((float) $obj->price).", ".((float) $obj->price).", ".((int) $obj->fk_user_author).",";
1084  $sql .= "null";
1085  $sql .= ")";
1086 
1087  if ($db->query($sql)) {
1088  print $langs->trans('MigrationContractsLineCreation', $obj->cref)."<br>\n";
1089  } else {
1090  dol_print_error($db);
1091  $nberr++;
1092  }
1093 
1094  $i++;
1095  }
1096 
1097  if (!$nberr) {
1098  // $db->rollback();
1099  $db->commit();
1100  print $langs->trans('MigrationSuccessfullUpdate')."<br>";
1101  } else {
1102  $db->rollback();
1103  print $langs->trans('MigrationUpdateFailed').'<br>';
1104  }
1105  } else {
1106  print $langs->trans('MigrationContractsNothingToUpdate')."<br>\n";
1107  }
1108  } else {
1109  print $langs->trans('MigrationContractsFieldDontExist')."<br>\n";
1110  // dol_print_error($db);
1111  }
1112 
1113  print '</td></tr>';
1114 }
1115 
1124 function migrate_links_transfert($db, $langs, $conf)
1125 {
1126  print '<tr><td colspan="4">';
1127 
1128  $nberr = 0;
1129 
1130  print '<br>';
1131  print '<b>'.$langs->trans('MigrationBankTransfertsUpdate')."</b><br>\n";
1132 
1133  $sql = "SELECT ba.rowid as barowid, bb.rowid as bbrowid";
1134  $sql .= " FROM ".MAIN_DB_PREFIX."bank as bb, ".MAIN_DB_PREFIX."bank as ba";
1135  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu ON bu.fk_bank = ba.rowid";
1136  $sql .= " WHERE ba.amount = -bb.amount AND ba.fk_account <> bb.fk_account";
1137  $sql .= " AND ba.datev = bb.datev AND ba.datec = bb.datec";
1138  $sql .= " AND bu.fk_bank IS NULL";
1139  $resql = $db->query($sql);
1140 
1141  dolibarr_install_syslog("upgrade2::migrate_links_transfert");
1142  if ($resql) {
1143  $i = 0;
1144  $row = array();
1145  $num = $db->num_rows($resql);
1146 
1147  if ($num) {
1148  print $langs->trans('MigrationBankTransfertsToUpdate', $num)."<br>\n";
1149  $db->begin();
1150 
1151  while ($i < $num) {
1152  $obj = $db->fetch_object($resql);
1153 
1154  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_url (";
1155  $sql .= "fk_bank, url_id, url, label, type";
1156  $sql .= ")";
1157  $sql .= " VALUES (";
1158  $sql .= $obj->barowid.",".$obj->bbrowid.", '/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert'";
1159  $sql .= ")";
1160 
1161  //print $sql.'<br>';
1162  dolibarr_install_syslog("migrate_links_transfert");
1163 
1164  if (!$db->query($sql)) {
1165  dol_print_error($db);
1166  $nberr++;
1167  }
1168 
1169  $i++;
1170  }
1171 
1172  if (!$nberr) {
1173  // $db->rollback();
1174  $db->commit();
1175  print $langs->trans('MigrationSuccessfullUpdate')."<br>";
1176  } else {
1177  $db->rollback();
1178  print $langs->trans('MigrationUpdateFailed').'<br>';
1179  }
1180  } else {
1181  print $langs->trans('MigrationBankTransfertsNothingToUpdate')."<br>\n";
1182  }
1183  } else {
1184  dol_print_error($db);
1185  }
1186 
1187  print '</td></tr>';
1188 }
1189 
1198 function migrate_contracts_date1($db, $langs, $conf)
1199 {
1200  print '<tr><td colspan="4">';
1201 
1202  print '<br>';
1203  print '<b>'.$langs->trans('MigrationContractsEmptyDatesUpdate')."</b><br>\n";
1204 
1205  $sql = "update ".MAIN_DB_PREFIX."contrat set date_contrat=tms where date_contrat is null";
1206  dolibarr_install_syslog("upgrade2::migrate_contracts_date1");
1207  $resql = $db->query($sql);
1208  if (!$resql) {
1209  dol_print_error($db);
1210  }
1211  if ($db->affected_rows($resql) > 0) {
1212  print $langs->trans('MigrationContractsEmptyDatesUpdateSuccess')."<br>\n";
1213  } else {
1214  print $langs->trans('MigrationContractsEmptyDatesNothingToUpdate')."<br>\n";
1215  }
1216 
1217  $sql = "update ".MAIN_DB_PREFIX."contrat set datec=tms where datec is null";
1218  dolibarr_install_syslog("upgrade2::migrate_contracts_date1");
1219  $resql = $db->query($sql);
1220  if (!$resql) {
1221  dol_print_error($db);
1222  }
1223  if ($db->affected_rows($resql) > 0) {
1224  print $langs->trans('MigrationContractsEmptyCreationDatesUpdateSuccess')."<br>\n";
1225  } else {
1226  print $langs->trans('MigrationContractsEmptyCreationDatesNothingToUpdate')."<br>\n";
1227  }
1228 
1229  print '</td></tr>';
1230 }
1231 
1240 function migrate_contracts_date2($db, $langs, $conf)
1241 {
1242  print '<tr><td colspan="4">';
1243 
1244  $nberr = 0;
1245 
1246  print '<br>';
1247  print '<b>'.$langs->trans('MigrationContractsInvalidDatesUpdate')."</b><br>\n";
1248 
1249  $sql = "SELECT c.rowid as cref, c.datec, c.date_contrat, MIN(cd.date_ouverture) as datemin";
1250  $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c,";
1251  $sql .= " ".MAIN_DB_PREFIX."contratdet as cd";
1252  $sql .= " WHERE c.rowid=cd.fk_contrat AND cd.date_ouverture IS NOT NULL";
1253  $sql .= " GROUP BY c.rowid, c.date_contrat";
1254  $resql = $db->query($sql);
1255 
1256  dolibarr_install_syslog("upgrade2::migrate_contracts_date2");
1257  if ($resql) {
1258  $i = 0;
1259  $row = array();
1260  $num = $db->num_rows($resql);
1261 
1262  if ($num) {
1263  $nbcontratsmodifie = 0;
1264  $db->begin();
1265 
1266  while ($i < $num) {
1267  $obj = $db->fetch_object($resql);
1268  if ($obj->date_contrat > $obj->datemin) {
1269  $datemin = $db->jdate($obj->datemin);
1270 
1271  print $langs->trans('MigrationContractsInvalidDateFix', $obj->cref, $obj->date_contrat, $obj->datemin)."<br>\n";
1272  $sql = "UPDATE ".MAIN_DB_PREFIX."contrat";
1273  $sql .= " SET date_contrat='".$db->idate($datemin)."'";
1274  $sql .= " WHERE rowid = ".((int) $obj->cref);
1275  $resql2 = $db->query($sql);
1276  if (!$resql2) {
1277  dol_print_error($db);
1278  }
1279 
1280  $nbcontratsmodifie++;
1281  }
1282  $i++;
1283  }
1284 
1285  $db->commit();
1286 
1287  if ($nbcontratsmodifie) {
1288  print $langs->trans('MigrationContractsInvalidDatesNumber', $nbcontratsmodifie)."<br>\n";
1289  } else {
1290  print $langs->trans('MigrationContractsInvalidDatesNothingToUpdate')."<br>\n";
1291  }
1292  }
1293  } else {
1294  dol_print_error($db);
1295  }
1296 
1297  print '</td></tr>';
1298 }
1299 
1308 function migrate_contracts_date3($db, $langs, $conf)
1309 {
1310  print '<tr><td colspan="4">';
1311 
1312  print '<br>';
1313  print '<b>'.$langs->trans('MigrationContractsIncoherentCreationDateUpdate')."</b><br>\n";
1314 
1315  $sql = "update ".MAIN_DB_PREFIX."contrat set datec=date_contrat where datec is null or datec > date_contrat";
1316  dolibarr_install_syslog("upgrade2::migrate_contracts_date3");
1317  $resql = $db->query($sql);
1318  if (!$resql) {
1319  dol_print_error($db);
1320  }
1321  if ($db->affected_rows($resql) > 0) {
1322  print $langs->trans('MigrationContractsIncoherentCreationDateUpdateSuccess')."<br>\n";
1323  } else {
1324  print $langs->trans('MigrationContractsIncoherentCreationDateNothingToUpdate')."<br>\n";
1325  }
1326 
1327  print '</td></tr>';
1328 }
1329 
1338 function migrate_contracts_open($db, $langs, $conf)
1339 {
1340  print '<tr><td colspan="4">';
1341 
1342  print '<br>';
1343  print '<b>'.$langs->trans('MigrationReopeningContracts')."</b><br>\n";
1344 
1345  $sql = "SELECT c.rowid as cref FROM ".MAIN_DB_PREFIX."contrat as c, ".MAIN_DB_PREFIX."contratdet as cd";
1346  $sql .= " WHERE cd.statut = 4 AND c.statut=2 AND c.rowid=cd.fk_contrat";
1347  dolibarr_install_syslog("upgrade2::migrate_contracts_open");
1348  $resql = $db->query($sql);
1349  if (!$resql) {
1350  dol_print_error($db);
1351  }
1352  if ($db->affected_rows($resql) > 0) {
1353  $i = 0;
1354  $row = array();
1355  $num = $db->num_rows($resql);
1356 
1357  if ($num) {
1358  $nbcontratsmodifie = 0;
1359  $db->begin();
1360 
1361  while ($i < $num) {
1362  $obj = $db->fetch_object($resql);
1363 
1364  print $langs->trans('MigrationReopenThisContract', $obj->cref)."<br>\n";
1365  $sql = "UPDATE ".MAIN_DB_PREFIX."contrat";
1366  $sql .= " SET statut = 1";
1367  $sql .= " WHERE rowid = ".((int) $obj->cref);
1368  $resql2 = $db->query($sql);
1369  if (!$resql2) {
1370  dol_print_error($db);
1371  }
1372 
1373  $nbcontratsmodifie++;
1374 
1375  $i++;
1376  }
1377 
1378  $db->commit();
1379 
1380  if ($nbcontratsmodifie) {
1381  print $langs->trans('MigrationReopenedContractsNumber', $nbcontratsmodifie)."<br>\n";
1382  } else {
1383  print $langs->trans('MigrationReopeningContractsNothingToUpdate')."<br>\n";
1384  }
1385  }
1386  } else {
1387  print $langs->trans('MigrationReopeningContractsNothingToUpdate')."<br>\n";
1388  }
1389 
1390  print '</td></tr>';
1391 }
1392 
1401 function migrate_paiementfourn_facturefourn($db, $langs, $conf)
1402 {
1403  global $bc;
1404 
1405  print '<tr><td colspan="4">';
1406  print '<br>';
1407  print '<b>'.$langs->trans('SuppliersInvoices')."</b><br>\n";
1408  print '</td></tr>';
1409 
1410  $result = $db->DDLDescTable(MAIN_DB_PREFIX."paiementfourn", "fk_facture_fourn");
1411  $obj = $db->fetch_object($result);
1412  if ($obj) {
1413  $error = 0;
1414  $nb = 0;
1415 
1416  $select_sql = 'SELECT rowid, fk_facture_fourn, amount';
1417  $select_sql .= ' FROM '.MAIN_DB_PREFIX.'paiementfourn';
1418  $select_sql .= ' WHERE fk_facture_fourn IS NOT NULL';
1419 
1420  dolibarr_install_syslog("upgrade2::migrate_paiementfourn_facturefourn");
1421  $select_resql = $db->query($select_sql);
1422  if ($select_resql) {
1423  $select_num = $db->num_rows($select_resql);
1424  $i = 0;
1425 
1426  // Pour chaque paiement fournisseur, on insere une ligne dans paiementfourn_facturefourn
1427  while (($i < $select_num) && (!$error)) {
1428  $select_obj = $db->fetch_object($select_resql);
1429 
1430  // Verifier si la ligne est deja dans la nouvelle table. On ne veut pas inserer de doublons.
1431  $check_sql = 'SELECT fk_paiementfourn, fk_facturefourn';
1432  $check_sql .= ' FROM '.MAIN_DB_PREFIX.'paiementfourn_facturefourn';
1433  $check_sql .= ' WHERE fk_paiementfourn = '.((int) $select_obj->rowid).' AND fk_facturefourn = '.((int) $select_obj->fk_facture_fourn);
1434  $check_resql = $db->query($check_sql);
1435  if ($check_resql) {
1436  $check_num = $db->num_rows($check_resql);
1437  if ($check_num == 0) {
1438  $db->begin();
1439 
1440  if ($nb == 0) {
1441  print '<tr><td colspan="4" class="nowrap"><b>'.$langs->trans('SuppliersInvoices').'</b></td></tr>';
1442  print '<tr><td>fk_paiementfourn</td><td>fk_facturefourn</td><td>'.$langs->trans('Amount').'</td><td>&nbsp;</td></tr>';
1443  }
1444 
1445  print '<tr class="oddeven">';
1446  print '<td>'.$select_obj->rowid.'</td><td>'.$select_obj->fk_facture_fourn.'</td><td>'.$select_obj->amount.'</td>';
1447 
1448  $insert_sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiementfourn_facturefourn SET ';
1449  $insert_sql .= ' fk_paiementfourn = \''.$select_obj->rowid.'\',';
1450  $insert_sql .= ' fk_facturefourn = \''.$select_obj->fk_facture_fourn.'\',';
1451  $insert_sql .= ' amount = \''.$select_obj->amount.'\'';
1452  $insert_resql = $db->query($insert_sql);
1453 
1454  if ($insert_resql) {
1455  $nb++;
1456  print '<td><span class="ok">'.$langs->trans("OK").'</span></td>';
1457  } else {
1458  print '<td><span class="error">Error on insert</span></td>';
1459  $error++;
1460  }
1461  print '</tr>';
1462  }
1463  } else {
1464  $error++;
1465  }
1466  $i++;
1467  }
1468  } else {
1469  $error++;
1470  }
1471 
1472  if (!$error) {
1473  if (!$nb) {
1474  print '<tr><td>'.$langs->trans("AlreadyDone").'</td></tr>';
1475  }
1476  $db->commit();
1477 
1478  $sql = "ALTER TABLE ".MAIN_DB_PREFIX."paiementfourn DROP COLUMN fk_facture_fourn";
1479  $db->query($sql);
1480  } else {
1481  print '<tr><td>'.$langs->trans("Error").'</td></tr>';
1482  $db->rollback();
1483  }
1484  } else {
1485  print '<tr><td>'.$langs->trans("AlreadyDone").'</td></tr>';
1486  }
1487 }
1488 
1497 function migrate_price_facture($db, $langs, $conf)
1498 {
1499  $err = 0;
1500 
1501  $tmpmysoc = new Societe($db);
1502  $tmpmysoc->setMysoc($conf);
1503 
1504  $db->begin();
1505 
1506  print '<tr><td colspan="4">';
1507 
1508  print '<br>';
1509  print '<b>'.$langs->trans('MigrationInvoice')."</b><br>\n";
1510 
1511  // List of invoice lines not up to date
1512  $sql = "SELECT fd.rowid, fd.qty, fd.subprice, fd.remise_percent, fd.tva_tx as vatrate, fd.total_ttc, fd.info_bits,";
1513  $sql .= " f.rowid as facid, f.remise_percent as remise_percent_global, f.total_ttc as total_ttc_f";
1514  $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd, ".MAIN_DB_PREFIX."facture as f";
1515  $sql .= " WHERE fd.fk_facture = f.rowid";
1516  $sql .= " AND (((fd.total_ttc = 0 AND fd.remise_percent != 100) or fd.total_ttc IS NULL) or f.total_ttc IS NULL)";
1517  //print $sql;
1518 
1519  dolibarr_install_syslog("upgrade2::migrate_price_facture");
1520  $resql = $db->query($sql);
1521  if ($resql) {
1522  $num = $db->num_rows($resql);
1523  $i = 0;
1524  if ($num) {
1525  while ($i < $num) {
1526  $obj = $db->fetch_object($resql);
1527 
1528  $rowid = $obj->rowid;
1529  $qty = $obj->qty;
1530  $pu = $obj->subprice;
1531  $vatrate = $obj->vatrate;
1532  $remise_percent = $obj->remise_percent;
1533  $remise_percent_global = $obj->remise_percent_global;
1534  $total_ttc_f = $obj->total_ttc_f;
1535  $info_bits = $obj->info_bits;
1536 
1537  // On met a jour les 3 nouveaux champs
1538  $facligne = new FactureLigne($db);
1539  $facligne->fetch($rowid);
1540 
1541  $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, $remise_percent_global, 'HT', $info_bits, $facligne->product_type, $tmpmysoc);
1542  $total_ht = $result[0];
1543  $total_tva = $result[1];
1544  $total_ttc = $result[2];
1545 
1546  $facligne->total_ht = $total_ht;
1547  $facligne->total_tva = $total_tva;
1548  $facligne->total_ttc = $total_ttc;
1549 
1550  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);
1551  print ". ";
1552  $facligne->update_total();
1553 
1554 
1555  /* On touche a facture mere uniquement si total_ttc = 0 */
1556  if (!$total_ttc_f) {
1557  $facture = new Facture($db);
1558  $facture->id = $obj->facid;
1559 
1560  if ($facture->fetch($facture->id) >= 0) {
1561  if ($facture->update_price() > 0) {
1562  //print $facture->id;
1563  } else {
1564  print "Error id=".$facture->id;
1565  $err++;
1566  }
1567  } else {
1568  print "Error #3";
1569  $err++;
1570  }
1571  }
1572  print " ";
1573 
1574  $i++;
1575  }
1576  } else {
1577  print $langs->trans("AlreadyDone");
1578  }
1579  $db->free($resql);
1580 
1581  $db->commit();
1582  } else {
1583  print "Error #1 ".$db->error();
1584  $err++;
1585 
1586  $db->rollback();
1587  }
1588 
1589  print '<br>';
1590 
1591  print '</td></tr>';
1592 }
1593 
1602 function migrate_price_propal($db, $langs, $conf)
1603 {
1604  $tmpmysoc = new Societe($db);
1605  $tmpmysoc->setMysoc($conf);
1606 
1607  $db->begin();
1608 
1609  print '<tr><td colspan="4">';
1610 
1611  print '<br>';
1612  print '<b>'.$langs->trans('MigrationProposal')."</b><br>\n";
1613 
1614  // List of proposal lines not up to date
1615  $sql = "SELECT pd.rowid, pd.qty, pd.subprice, pd.remise_percent, pd.tva_tx as vatrate, pd.info_bits,";
1616  $sql .= " p.rowid as propalid, p.remise_percent as remise_percent_global";
1617  $sql .= " FROM ".MAIN_DB_PREFIX."propaldet as pd, ".MAIN_DB_PREFIX."propal as p";
1618  $sql .= " WHERE pd.fk_propal = p.rowid";
1619  $sql .= " AND ((pd.total_ttc = 0 AND pd.remise_percent != 100) or pd.total_ttc IS NULL)";
1620 
1621  dolibarr_install_syslog("upgrade2::migrate_price_propal");
1622  $resql = $db->query($sql);
1623  if ($resql) {
1624  $num = $db->num_rows($resql);
1625  $i = 0;
1626  if ($num) {
1627  while ($i < $num) {
1628  $obj = $db->fetch_object($resql);
1629 
1630  $rowid = $obj->rowid;
1631  $qty = $obj->qty;
1632  $pu = $obj->subprice;
1633  $vatrate = $obj->vatrate;
1634  $remise_percent = $obj->remise_percent;
1635  $remise_percent_global = $obj->remise_percent_global;
1636  $info_bits = $obj->info_bits;
1637 
1638  // On met a jour les 3 nouveaux champs
1639  $propalligne = new PropaleLigne($db);
1640  $propalligne->fetch($rowid);
1641 
1642  $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, $remise_percent_global, 'HT', $info_bits, $propalligne->product_type, $tmpmysoc);
1643  $total_ht = $result[0];
1644  $total_tva = $result[1];
1645  $total_ttc = $result[2];
1646 
1647  $propalligne->total_ht = $total_ht;
1648  $propalligne->total_tva = $total_tva;
1649  $propalligne->total_ttc = $total_ttc;
1650 
1651  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);
1652  print ". ";
1653  $propalligne->update_total();
1654 
1655 
1656  /* On touche pas a propal mere
1657  $propal = new Propal($db);
1658  $propal->id=$obj->rowid;
1659  if ( $propal->fetch($propal->id) >= 0 )
1660  {
1661  if ( $propal->update_price() > 0 )
1662  {
1663  print ". ";
1664  }
1665  else
1666  {
1667  print "Error id=".$propal->id;
1668  }
1669  }
1670  else
1671  {
1672  print "Error #3";
1673  }
1674  */
1675  $i++;
1676  }
1677  } else {
1678  print $langs->trans("AlreadyDone");
1679  }
1680 
1681  $db->free($resql);
1682 
1683  $db->commit();
1684  } else {
1685  print "Error #1 ".$db->error();
1686 
1687  $db->rollback();
1688  }
1689 
1690  print '<br>';
1691 
1692  print '</td></tr>';
1693 }
1694 
1703 function migrate_price_contrat($db, $langs, $conf)
1704 {
1705  $db->begin();
1706 
1707  $tmpmysoc = new Societe($db);
1708  $tmpmysoc->setMysoc($conf);
1709  if (empty($tmpmysoc->country_id)) {
1710  $tmpmysoc->country_id = 0; // Ti not have this set to '' or will make sql syntax error.
1711  }
1712 
1713  print '<tr><td colspan="4">';
1714 
1715  print '<br>';
1716  print '<b>'.$langs->trans('MigrationContract')."</b><br>\n";
1717 
1718  // List of contract lines not up to date
1719  $sql = "SELECT cd.rowid, cd.qty, cd.subprice, cd.remise_percent, cd.tva_tx as vatrate, cd.info_bits,";
1720  $sql .= " c.rowid as contratid";
1721  $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."contrat as c";
1722  $sql .= " WHERE cd.fk_contrat = c.rowid";
1723  $sql .= " AND ((cd.total_ttc = 0 AND cd.remise_percent != 100 AND cd.subprice > 0) or cd.total_ttc IS NULL)";
1724 
1725  dolibarr_install_syslog("upgrade2::migrate_price_contrat");
1726  $resql = $db->query($sql);
1727  if ($resql) {
1728  $num = $db->num_rows($resql);
1729  $i = 0;
1730  if ($num) {
1731  while ($i < $num) {
1732  $obj = $db->fetch_object($resql);
1733 
1734  $rowid = $obj->rowid;
1735  $qty = $obj->qty;
1736  $pu = $obj->subprice;
1737  $vatrate = $obj->vatrate;
1738  $remise_percent = $obj->remise_percent;
1739  $info_bits = $obj->info_bits;
1740 
1741  // On met a jour les 3 nouveaux champs
1742  $contratligne = new ContratLigne($db);
1743  //$contratligne->fetch($rowid); Non requis car le update_total ne met a jour que chp redefinis
1744  $contratligne->fetch($rowid);
1745 
1746  $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, 0, 'HT', $info_bits, $contratligne->product_type, $tmpmysoc);
1747  $total_ht = $result[0];
1748  $total_tva = $result[1];
1749  $total_ttc = $result[2];
1750 
1751  $contratligne->total_ht = $total_ht;
1752  $contratligne->total_tva = $total_tva;
1753  $contratligne->total_ttc = $total_ttc;
1754 
1755  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);
1756  print ". ";
1757  $contratligne->update_total();
1758 
1759  $i++;
1760  }
1761  } else {
1762  print $langs->trans("AlreadyDone");
1763  }
1764 
1765  $db->free($resql);
1766 
1767  $db->commit();
1768  } else {
1769  print "Error #1 ".$db->error();
1770 
1771  $db->rollback();
1772  }
1773 
1774  print '<br>';
1775 
1776  print '</td></tr>';
1777 }
1778 
1787 function migrate_price_commande($db, $langs, $conf)
1788 {
1789  $db->begin();
1790 
1791  $tmpmysoc = new Societe($db);
1792  $tmpmysoc->setMysoc($conf);
1793 
1794  print '<tr><td colspan="4">';
1795 
1796  print '<br>';
1797  print '<b>'.$langs->trans('MigrationOrder')."</b><br>\n";
1798 
1799  // List of sales orders lines not up to date
1800  $sql = "SELECT cd.rowid, cd.qty, cd.subprice, cd.remise_percent, cd.tva_tx as vatrate, cd.info_bits,";
1801  $sql .= " c.rowid as commandeid, c.remise_percent as remise_percent_global";
1802  $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd, ".MAIN_DB_PREFIX."commande as c";
1803  $sql .= " WHERE cd.fk_commande = c.rowid";
1804  $sql .= " AND ((cd.total_ttc = 0 AND cd.remise_percent != 100) or cd.total_ttc IS NULL)";
1805 
1806  dolibarr_install_syslog("upgrade2::migrate_price_commande");
1807  $resql = $db->query($sql);
1808  if ($resql) {
1809  $num = $db->num_rows($resql);
1810  $i = 0;
1811  if ($num) {
1812  while ($i < $num) {
1813  $obj = $db->fetch_object($resql);
1814 
1815  $rowid = $obj->rowid;
1816  $qty = $obj->qty;
1817  $pu = $obj->subprice;
1818  $vatrate = $obj->vatrate;
1819  $remise_percent = $obj->remise_percent;
1820  $remise_percent_global = $obj->remise_percent_global;
1821  $info_bits = $obj->info_bits;
1822 
1823  // On met a jour les 3 nouveaux champs
1824  $commandeligne = new OrderLine($db);
1825  $commandeligne->fetch($rowid);
1826 
1827  $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, $remise_percent_global, 'HT', $info_bits, $commandeligne->product_type, $tmpmysoc);
1828  $total_ht = $result[0];
1829  $total_tva = $result[1];
1830  $total_ttc = $result[2];
1831 
1832  $commandeligne->total_ht = $total_ht;
1833  $commandeligne->total_tva = $total_tva;
1834  $commandeligne->total_ttc = $total_ttc;
1835 
1836  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);
1837  print ". ";
1838  $commandeligne->update_total();
1839 
1840  /* On touche pas a facture mere
1841  $commande = new Commande($db);
1842  $commande->id = $obj->rowid;
1843  if ( $commande->fetch($commande->id) >= 0 )
1844  {
1845  if ( $commande->update_price() > 0 )
1846  {
1847  print ". ";
1848  }
1849  else
1850  {
1851  print "Error id=".$commande->id;
1852  }
1853  }
1854  else
1855  {
1856  print "Error #3";
1857  }
1858  */
1859  $i++;
1860  }
1861  } else {
1862  print $langs->trans("AlreadyDone");
1863  }
1864 
1865  $db->free($resql);
1866 
1867  /*
1868  $sql = "DELETE FROM ".MAIN_DB_PREFIX."commandedet";
1869  $sql.= " WHERE price = 0 and total_ttc = 0 and total_tva = 0 and total_ht = 0 AND remise_percent = 0";
1870  $resql=$db->query($sql);
1871  if (! $resql)
1872  {
1873  dol_print_error($db);
1874  }
1875  */
1876 
1877  $db->commit();
1878  } else {
1879  print "Error #1 ".$db->error();
1880 
1881  $db->rollback();
1882  }
1883 
1884  print '<br>';
1885 
1886  print '</td></tr>';
1887 }
1888 
1897 function migrate_price_commande_fournisseur($db, $langs, $conf)
1898 {
1899  $db->begin();
1900 
1901  $tmpmysoc = new Societe($db);
1902  $tmpmysoc->setMysoc($conf);
1903 
1904  print '<tr><td colspan="4">';
1905 
1906  print '<br>';
1907  print '<b>'.$langs->trans('MigrationSupplierOrder')."</b><br>\n";
1908 
1909  // List of purchase order lines not up to date
1910  $sql = "SELECT cd.rowid, cd.qty, cd.subprice, cd.remise_percent, cd.tva_tx as vatrate, cd.info_bits,";
1911  $sql .= " c.rowid as commandeid, c.remise_percent as remise_percent_global";
1912  $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as cd, ".MAIN_DB_PREFIX."commande_fournisseur as c";
1913  $sql .= " WHERE cd.fk_commande = c.rowid";
1914  $sql .= " AND ((cd.total_ttc = 0 AND cd.remise_percent != 100) or cd.total_ttc IS NULL)";
1915 
1916  dolibarr_install_syslog("upgrade2::migrate_price_commande_fournisseur");
1917  $resql = $db->query($sql);
1918  if ($resql) {
1919  $num = $db->num_rows($resql);
1920  $i = 0;
1921  if ($num) {
1922  while ($i < $num) {
1923  $obj = $db->fetch_object($resql);
1924 
1925  $rowid = $obj->rowid;
1926  $qty = $obj->qty;
1927  $pu = $obj->subprice;
1928  $vatrate = $obj->vatrate;
1929  $remise_percent = $obj->remise_percent;
1930  $remise_percent_global = $obj->remise_percent_global;
1931  $info_bits = $obj->info_bits;
1932 
1933  // On met a jour les 3 nouveaux champs
1934  $commandeligne = new CommandeFournisseurLigne($db);
1935  $commandeligne->fetch($rowid);
1936 
1937  $result = calcul_price_total($qty, $pu, $remise_percent, $vatrate, 0, 0, $remise_percent_global, 'HT', $info_bits, $commandeligne->product_type, $tmpsoc);
1938  $total_ht = $result[0];
1939  $total_tva = $result[1];
1940  $total_ttc = $result[2];
1941 
1942  $commandeligne->total_ht = $total_ht;
1943  $commandeligne->total_tva = $total_tva;
1944  $commandeligne->total_ttc = $total_ttc;
1945 
1946  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);
1947  print ". ";
1948  $commandeligne->update_total();
1949 
1950  /* On touche pas a facture mere
1951  $commande = new Commande($db);
1952  $commande->id = $obj->rowid;
1953  if ( $commande->fetch($commande->id) >= 0 )
1954  {
1955  if ( $commande->update_price() > 0 )
1956  {
1957  print ". ";
1958  }
1959  else
1960  {
1961  print "Error id=".$commande->id;
1962  }
1963  }
1964  else
1965  {
1966  print "Error #3";
1967  }
1968  */
1969  $i++;
1970  }
1971  } else {
1972  print $langs->trans("AlreadyDone");
1973  }
1974 
1975  $db->free($resql);
1976 
1977  /*
1978  $sql = "DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseurdet";
1979  $sql.= " WHERE subprice = 0 and total_ttc = 0 and total_tva = 0 and total_ht = 0";
1980  $resql=$db->query($sql);
1981  if (! $resql)
1982  {
1983  dol_print_error($db);
1984  }
1985  */
1986 
1987  $db->commit();
1988  } else {
1989  print "Error #1 ".$db->error();
1990 
1991  $db->rollback();
1992  }
1993 
1994  print '<br>';
1995 
1996  print '</td></tr>';
1997 }
1998 
2007 function migrate_modeles($db, $langs, $conf)
2008 {
2009  //print '<br>';
2010  //print '<b>'.$langs->trans('UpdateModelsTable')."</b><br>\n";
2011 
2012  dolibarr_install_syslog("upgrade2::migrate_modeles");
2013 
2014  if (isModEnabled('facture')) {
2015  include_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
2016  $modellist = ModelePDFFactures::liste_modeles($db);
2017  if (count($modellist) == 0) {
2018  // Aucun model par defaut.
2019  $sql = " insert into ".MAIN_DB_PREFIX."document_model(nom,type) values('crabe','invoice')";
2020  $resql = $db->query($sql);
2021  if (!$resql) {
2022  dol_print_error($db);
2023  }
2024  }
2025  }
2026 
2027  if (!empty($conf->commande->enabled)) {
2028  include_once DOL_DOCUMENT_ROOT.'/core/modules/commande/modules_commande.php';
2029  $modellist = ModelePDFCommandes::liste_modeles($db);
2030  if (count($modellist) == 0) {
2031  // Aucun model par defaut.
2032  $sql = " insert into ".MAIN_DB_PREFIX."document_model(nom,type) values('einstein','order')";
2033  $resql = $db->query($sql);
2034  if (!$resql) {
2035  dol_print_error($db);
2036  }
2037  }
2038  }
2039 
2040  if (!empty($conf->expedition->enabled)) {
2041  include_once DOL_DOCUMENT_ROOT.'/core/modules/expedition/modules_expedition.php';
2042  $modellist = ModelePDFExpedition::liste_modeles($db);
2043  if (count($modellist) == 0) {
2044  // Aucun model par defaut.
2045  $sql = " insert into ".MAIN_DB_PREFIX."document_model(nom,type) values('rouget','shipping')";
2046  $resql = $db->query($sql);
2047  if (!$resql) {
2048  dol_print_error($db);
2049  }
2050  }
2051  }
2052 
2053  //print $langs->trans("AlreadyDone");
2054 }
2055 
2056 
2065 function migrate_commande_expedition($db, $langs, $conf)
2066 {
2067  dolibarr_install_syslog("upgrade2::migrate_commande_expedition");
2068 
2069  print '<tr><td colspan="4">';
2070 
2071  print '<br>';
2072  print '<b>'.$langs->trans('MigrationShipmentOrderMatching')."</b><br>\n";
2073 
2074  $result = $db->DDLDescTable(MAIN_DB_PREFIX."expedition", "fk_commande");
2075  $obj = $db->fetch_object($result);
2076  if ($obj) {
2077  $error = 0;
2078 
2079  $db->begin();
2080 
2081  $sql = "SELECT e.rowid, e.fk_commande FROM ".MAIN_DB_PREFIX."expedition as e";
2082  $resql = $db->query($sql);
2083  if ($resql) {
2084  $i = 0;
2085  $num = $db->num_rows($resql);
2086 
2087  if ($num) {
2088  while ($i < $num) {
2089  $obj = $db->fetch_object($resql);
2090 
2091  $sql = "INSERT INTO ".MAIN_DB_PREFIX."co_exp (fk_expedition,fk_commande)";
2092  $sql .= " VALUES (".((int) $obj->rowid).", ".((int) $obj->fk_commande).")";
2093  $resql2 = $db->query($sql);
2094 
2095  if (!$resql2) {
2096  $error++;
2097  dol_print_error($db);
2098  }
2099  print ". ";
2100  $i++;
2101  }
2102  }
2103 
2104  if ($error == 0) {
2105  $db->commit();
2106  $sql = "ALTER TABLE ".MAIN_DB_PREFIX."expedition DROP COLUMN fk_commande";
2107  print $langs->trans('FieldRenamed')."<br>\n";
2108  $db->query($sql);
2109  } else {
2110  $db->rollback();
2111  }
2112  } else {
2113  dol_print_error($db);
2114  $db->rollback();
2115  }
2116  } else {
2117  print $langs->trans('AlreadyDone')."<br>\n";
2118  }
2119  print '</td></tr>';
2120 }
2121 
2130 function migrate_commande_livraison($db, $langs, $conf)
2131 {
2132  dolibarr_install_syslog("upgrade2::migrate_commande_livraison");
2133 
2134  print '<tr><td colspan="4">';
2135 
2136  print '<br>';
2137  print '<b>'.$langs->trans('MigrationDeliveryOrderMatching')."</b><br>\n";
2138 
2139  $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraison", "fk_commande");
2140  $obj = $db->fetch_object($result);
2141  if ($obj) {
2142  $error = 0;
2143 
2144  $db->begin();
2145 
2146  $sql = "SELECT l.rowid, l.fk_commande,";
2147  $sql .= " c.ref_client, c.date_livraison as delivery_date";
2148  $sql .= " FROM ".MAIN_DB_PREFIX."livraison as l, ".MAIN_DB_PREFIX."commande as c";
2149  $sql .= " WHERE c.rowid = l.fk_commande";
2150  $resql = $db->query($sql);
2151  if ($resql) {
2152  $i = 0;
2153  $num = $db->num_rows($resql);
2154 
2155  if ($num) {
2156  while ($i < $num) {
2157  $obj = $db->fetch_object($resql);
2158 
2159  $sql = "INSERT INTO ".MAIN_DB_PREFIX."co_liv (fk_livraison,fk_commande)";
2160  $sql .= " VALUES (".((int) $obj->rowid).", ".((int) $obj->fk_commande).")";
2161  $resql2 = $db->query($sql);
2162 
2163  if ($resql2) {
2164  $delivery_date = $db->jdate($obj->delivery_date);
2165 
2166  $sqlu = "UPDATE ".MAIN_DB_PREFIX."livraison SET";
2167  $sqlu .= " ref_client = '".$db->escape($obj->ref_client)."'";
2168  $sqlu .= ", date_livraison = '".$db->idate($delivery_date)."'";
2169  $sqlu .= " WHERE rowid = ".((int) $obj->rowid);
2170  $resql3 = $db->query($sqlu);
2171  if (!$resql3) {
2172  $error++;
2173  dol_print_error($db);
2174  }
2175  } else {
2176  $error++;
2177  dol_print_error($db);
2178  }
2179  print ". ";
2180  $i++;
2181  }
2182  }
2183 
2184  if ($error == 0) {
2185  $db->commit();
2186  $sql = "ALTER TABLE ".MAIN_DB_PREFIX."livraison DROP COLUMN fk_commande";
2187  print $langs->trans('FieldRenamed')."<br>\n";
2188  $db->query($sql);
2189  } else {
2190  $db->rollback();
2191  }
2192  } else {
2193  dol_print_error($db);
2194  $db->rollback();
2195  }
2196  } else {
2197  print $langs->trans('AlreadyDone')."<br>\n";
2198  }
2199  print '</td></tr>';
2200 }
2201 
2210 function migrate_detail_livraison($db, $langs, $conf)
2211 {
2212  dolibarr_install_syslog("upgrade2::migrate_detail_livraison");
2213 
2214  print '<tr><td colspan="4">';
2215 
2216  print '<br>';
2217  print '<b>'.$langs->trans('MigrationDeliveryDetail')."</b><br>\n";
2218 
2219  // This is done if field fk_commande_ligne exists.
2220  // If not this means migration was already done.
2221  $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraisondet", "fk_commande_ligne");
2222  $obj = $db->fetch_object($result);
2223  if ($obj) {
2224  $error = 0;
2225 
2226  $db->begin();
2227 
2228  $sql = "SELECT cd.rowid, cd.fk_product, cd.description, cd.subprice, cd.total_ht";
2229  $sql .= ", ld.fk_livraison";
2230  $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as cd, ".MAIN_DB_PREFIX."livraisondet as ld";
2231  $sql .= " WHERE ld.fk_commande_ligne = cd.rowid";
2232  $resql = $db->query($sql);
2233  if ($resql) {
2234  $i = 0;
2235  $num = $db->num_rows($resql);
2236 
2237  if ($num) {
2238  while ($i < $num) {
2239  $obj = $db->fetch_object($resql);
2240 
2241  $sql = "UPDATE ".MAIN_DB_PREFIX."livraisondet SET";
2242  $sql .= " fk_product = ".((int) $obj->fk_product);
2243  $sql .= ",description = '".$db->escape($obj->description)."'";
2244  $sql .= ",subprice = ".price2num($obj->subprice);
2245  $sql .= ",total_ht = ".price2num($obj->total_ht);
2246  $sql .= " WHERE fk_commande_ligne = ".((int) $obj->rowid);
2247  $resql2 = $db->query($sql);
2248 
2249  if ($resql2) {
2250  $sql = "SELECT total_ht";
2251  $sql .= " FROM ".MAIN_DB_PREFIX."livraison";
2252  $sql .= " WHERE rowid = ".((int) $obj->fk_livraison);
2253  $resql3 = $db->query($sql);
2254 
2255  if ($resql3) {
2256  $obju = $db->fetch_object($resql3);
2257  $total_ht = $obju->total_ht + $obj->total_ht;
2258 
2259  $sqlu = "UPDATE ".MAIN_DB_PREFIX."livraison SET";
2260  $sqlu .= " total_ht = ".price2num($total_ht, 'MT');
2261  $sqlu .= " WHERE rowid = ".((int) $obj->fk_livraison);
2262  $resql4 = $db->query($sqlu);
2263  if (!$resql4) {
2264  $error++;
2265  dol_print_error($db);
2266  }
2267  } else {
2268  $error++;
2269  dol_print_error($db);
2270  }
2271  } else {
2272  $error++;
2273  dol_print_error($db);
2274  }
2275  print ". ";
2276  $i++;
2277  }
2278  }
2279 
2280  if ($error == 0) {
2281  $db->commit();
2282  $sql = "ALTER TABLE ".MAIN_DB_PREFIX."livraisondet CHANGE fk_commande_ligne fk_origin_line integer";
2283  print $langs->trans('FieldRenamed')."<br>\n";
2284  $db->query($sql);
2285  } else {
2286  $db->rollback();
2287  }
2288  } else {
2289  dol_print_error($db);
2290  $db->rollback();
2291  }
2292  } else {
2293  $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraisondet", "fk_origin_line");
2294  $obj = $db->fetch_object($result);
2295  if (!$obj) {
2296  $sql = "ALTER TABLE ".MAIN_DB_PREFIX."livraisondet ADD COLUMN fk_origin_line integer after fk_livraison";
2297  $db->query($sql);
2298  }
2299  print $langs->trans('AlreadyDone')."<br>\n";
2300  }
2301  print '</td></tr>';
2302 }
2303 
2312 function migrate_stocks($db, $langs, $conf)
2313 {
2314  dolibarr_install_syslog("upgrade2::migrate_stocks");
2315 
2316  print '<tr><td colspan="4">';
2317 
2318  print '<br>';
2319  print '<b>'.$langs->trans('MigrationStockDetail')."</b><br>\n";
2320 
2321  $error = 0;
2322 
2323  $db->begin();
2324 
2325  $sql = "SELECT SUM(reel) as total, fk_product";
2326  $sql .= " FROM ".MAIN_DB_PREFIX."product_stock as ps";
2327  $sql .= " GROUP BY fk_product";
2328  $resql = $db->query($sql);
2329  if ($resql) {
2330  $i = 0;
2331  $num = $db->num_rows($resql);
2332 
2333  if ($num) {
2334  while ($i < $num) {
2335  $obj = $db->fetch_object($resql);
2336 
2337  $sql = "UPDATE ".MAIN_DB_PREFIX."product SET";
2338  $sql .= " stock = ".price2num($obj->total, 'MS');
2339  $sql .= " WHERE rowid = ".((int) $obj->fk_product);
2340 
2341  $resql2 = $db->query($sql);
2342  if ($resql2) {
2343  } else {
2344  $error++;
2345  dol_print_error($db);
2346  }
2347  print ". ";
2348  $i++;
2349  }
2350  }
2351 
2352  if ($error == 0) {
2353  $db->commit();
2354  } else {
2355  $db->rollback();
2356  }
2357  } else {
2358  dol_print_error($db);
2359  $db->rollback();
2360  }
2361 
2362  print '</td></tr>';
2363 }
2364 
2374 function migrate_menus($db, $langs, $conf)
2375 {
2376  dolibarr_install_syslog("upgrade2::migrate_menus");
2377 
2378  print '<tr><td colspan="4">';
2379 
2380  print '<br>';
2381  print '<b>'.$langs->trans('MigrationMenusDetail')."</b><br>\n";
2382 
2383  $error = 0;
2384 
2385  if ($db->DDLInfoTable(MAIN_DB_PREFIX."menu_constraint")) {
2386  $db->begin();
2387 
2388  $sql = "SELECT m.rowid, mc.action";
2389  $sql .= " FROM ".MAIN_DB_PREFIX."menu_constraint as mc, ".MAIN_DB_PREFIX."menu_const as md, ".MAIN_DB_PREFIX."menu as m";
2390  $sql .= " WHERE md.fk_menu = m.rowid AND md.fk_constraint = mc.rowid";
2391  $sql .= " AND m.enabled = '1'";
2392  $resql = $db->query($sql);
2393  if ($resql) {
2394  $i = 0;
2395  $num = $db->num_rows($resql);
2396  if ($num) {
2397  while ($i < $num) {
2398  $obj = $db->fetch_object($resql);
2399 
2400  $sql = "UPDATE ".MAIN_DB_PREFIX."menu SET";
2401  $sql .= " enabled = '".$db->escape($obj->action)."'";
2402  $sql .= " WHERE rowid = ".((int) $obj->rowid);
2403  $sql .= " AND enabled = '1'";
2404 
2405  $resql2 = $db->query($sql);
2406  if ($resql2) {
2407  } else {
2408  $error++;
2409  dol_print_error($db);
2410  }
2411  print ". ";
2412  $i++;
2413  }
2414  }
2415 
2416  if ($error == 0) {
2417  $db->commit();
2418  } else {
2419  $db->rollback();
2420  }
2421  } else {
2422  dol_print_error($db);
2423  $db->rollback();
2424  }
2425  } else {
2426  print $langs->trans('AlreadyDone')."<br>\n";
2427  }
2428 
2429  print '</td></tr>';
2430 }
2431 
2441 function migrate_commande_deliveryaddress($db, $langs, $conf)
2442 {
2443  dolibarr_install_syslog("upgrade2::migrate_commande_deliveryaddress");
2444 
2445  print '<tr><td colspan="4">';
2446 
2447  print '<br>';
2448  print '<b>'.$langs->trans('MigrationDeliveryAddress')."</b><br>\n";
2449 
2450  $error = 0;
2451 
2452  if ($db->DDLInfoTable(MAIN_DB_PREFIX."co_exp")) {
2453  $db->begin();
2454 
2455  $sql = "SELECT c.fk_adresse_livraison, ce.fk_expedition";
2456  $sql .= " FROM ".MAIN_DB_PREFIX."commande as c";
2457  $sql .= ", ".MAIN_DB_PREFIX."co_exp as ce";
2458  $sql .= " WHERE c.rowid = ce.fk_commande";
2459  $sql .= " AND c.fk_adresse_livraison IS NOT NULL AND c.fk_adresse_livraison != 0";
2460 
2461  $resql = $db->query($sql);
2462  if ($resql) {
2463  $i = 0;
2464  $num = $db->num_rows($resql);
2465 
2466  if ($num) {
2467  while ($i < $num) {
2468  $obj = $db->fetch_object($resql);
2469 
2470  $sql = "UPDATE ".MAIN_DB_PREFIX."expedition SET";
2471  $sql .= " fk_adresse_livraison = '".$db->escape($obj->fk_adresse_livraison)."'";
2472  $sql .= " WHERE rowid = ".((int) $obj->fk_expedition);
2473 
2474  $resql2 = $db->query($sql);
2475  if (!$resql2) {
2476  $error++;
2477  dol_print_error($db);
2478  }
2479  print ". ";
2480  $i++;
2481  }
2482  } else {
2483  print $langs->trans('AlreadyDone')."<br>\n";
2484  }
2485 
2486  if ($error == 0) {
2487  $db->commit();
2488  } else {
2489  $db->rollback();
2490  }
2491  } else {
2492  dol_print_error($db);
2493  $db->rollback();
2494  }
2495  } else {
2496  print $langs->trans('AlreadyDone')."<br>\n";
2497  }
2498 
2499  print '</td></tr>';
2500 }
2501 
2511 function migrate_restore_missing_links($db, $langs, $conf)
2512 {
2513  dolibarr_install_syslog("upgrade2::migrate_restore_missing_links");
2514 
2515  if (($db->type == 'mysql' || $db->type == 'mysqli')) {
2516  if (versioncompare($db->getVersionArray(), array(4, 0)) < 0) {
2517  dolibarr_install_syslog("upgrade2::migrate_restore_missing_links Version of database too old to make this migrate action");
2518  return 0;
2519  }
2520  }
2521  print '<tr><td colspan="4">';
2522 
2523  print '<br>';
2524  print '<b>'.$langs->trans('MigrationFixData')."</b> (1)<br>\n";
2525 
2526  $error = 0;
2527 
2528 
2529  // Restore missing link for this cross foreign key (link 1 <=> 1). Direction 1.
2530  $table1 = 'facturedet'; $field1 = 'fk_remise_except';
2531  $table2 = 'societe_remise_except'; $field2 = 'fk_facture_line';
2532 
2533  $db->begin();
2534 
2535  $sql = "SELECT t1.rowid, t1.".$field1." as field";
2536  $sql .= " FROM ".MAIN_DB_PREFIX.$table1." as t1";
2537  $sql .= " WHERE t1.".$field1." IS NOT NULL AND t1.".$field1." NOT IN";
2538  $sql .= " (SELECT t2.rowid FROM ".MAIN_DB_PREFIX.$table2." as t2";
2539  $sql .= " WHERE t1.rowid = t2.".$field2.")";
2540 
2541  dolibarr_install_syslog("upgrade2::migrate_restore_missing_links DIRECTION 1");
2542  $resql = $db->query($sql);
2543  if ($resql) {
2544  $i = 0;
2545  $num = $db->num_rows($resql);
2546 
2547  if ($num) {
2548  while ($i < $num) {
2549  $obj = $db->fetch_object($resql);
2550 
2551  print 'Line '.$obj->rowid.' in '.$table1.' is linked to record '.$obj->field.' in '.$table2.' that has no link to '.$table1.'. We fix this.<br>';
2552  $sql = "UPDATE ".MAIN_DB_PREFIX.$table2." SET";
2553  $sql .= " ".$field2." = '".$db->escape($obj->rowid)."'";
2554  $sql .= " WHERE rowid = ".((int) $obj->field);
2555 
2556  $resql2 = $db->query($sql);
2557  if (!$resql2) {
2558  $error++;
2559  dol_print_error($db);
2560  }
2561  //print ". ";
2562  $i++;
2563  }
2564  } else {
2565  print $langs->trans('AlreadyDone')."<br>\n";
2566  }
2567 
2568  if ($error == 0) {
2569  $db->commit();
2570  } else {
2571  $db->rollback();
2572  }
2573  } else {
2574  dol_print_error($db);
2575  $db->rollback();
2576  }
2577 
2578  print '</td></tr>';
2579 
2580 
2581  print '<tr><td colspan="4">';
2582 
2583  print '<br>';
2584  print '<b>'.$langs->trans('MigrationFixData')."</b> (2)<br>\n";
2585 
2586  // Restore missing link for this cross foreign key (link 1 <=> 1). Direction 2.
2587  $table2 = 'facturedet'; $field2 = 'fk_remise_except';
2588  $table1 = 'societe_remise_except'; $field1 = 'fk_facture_line';
2589 
2590  $db->begin();
2591 
2592  $sql = "SELECT t1.rowid, t1.".$field1." as field";
2593  $sql .= " FROM ".MAIN_DB_PREFIX.$table1." as t1";
2594  $sql .= " WHERE t1.".$field1." IS NOT NULL AND t1.".$field1." NOT IN";
2595  $sql .= " (SELECT t2.rowid FROM ".MAIN_DB_PREFIX.$table2." as t2";
2596  $sql .= " WHERE t1.rowid = t2.".$field2.")";
2597 
2598  dolibarr_install_syslog("upgrade2::migrate_restore_missing_links DIRECTION 2");
2599  $resql = $db->query($sql);
2600  if ($resql) {
2601  $i = 0;
2602  $num = $db->num_rows($resql);
2603 
2604  if ($num) {
2605  while ($i < $num) {
2606  $obj = $db->fetch_object($resql);
2607 
2608  print 'Line '.$obj->rowid.' in '.$table1.' is linked to record '.$obj->field.' in '.$table2.' that has no link to '.$table1.'. We fix this.<br>';
2609  $sql = "UPDATE ".MAIN_DB_PREFIX.$table2." SET";
2610  $sql .= " ".$field2." = '".$db->escape($obj->rowid)."'";
2611  $sql .= " WHERE rowid = ".((int) $obj->field);
2612 
2613  $resql2 = $db->query($sql);
2614  if (!$resql2) {
2615  $error++;
2616  dol_print_error($db);
2617  }
2618  //print ". ";
2619  $i++;
2620  }
2621  } else {
2622  print $langs->trans('AlreadyDone')."<br>\n";
2623  }
2624 
2625  if ($error == 0) {
2626  $db->commit();
2627  } else {
2628  $db->rollback();
2629  }
2630  } else {
2631  dol_print_error($db);
2632  $db->rollback();
2633  }
2634 
2635  print '</td></tr>';
2636 }
2637 
2646 function migrate_project_user_resp($db, $langs, $conf)
2647 {
2648  dolibarr_install_syslog("upgrade2::migrate_project_user_resp");
2649 
2650  print '<tr><td colspan="4">';
2651 
2652  print '<br>';
2653  print '<b>'.$langs->trans('MigrationProjectUserResp')."</b><br>\n";
2654 
2655  $result = $db->DDLDescTable(MAIN_DB_PREFIX."projet", "fk_user_resp");
2656  $obj = $db->fetch_object($result);
2657  if ($obj) {
2658  $error = 0;
2659 
2660  $db->begin();
2661 
2662  $sql = "SELECT rowid, fk_user_resp FROM ".MAIN_DB_PREFIX."projet";
2663  $resql = $db->query($sql);
2664  if ($resql) {
2665  $i = 0;
2666  $num = $db->num_rows($resql);
2667 
2668  if ($num) {
2669  while ($i < $num) {
2670  $obj = $db->fetch_object($resql);
2671 
2672  $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."element_contact (";
2673  $sql2 .= "datecreate";
2674  $sql2 .= ", statut";
2675  $sql2 .= ", element_id";
2676  $sql2 .= ", fk_c_type_contact";
2677  $sql2 .= ", fk_socpeople";
2678  $sql2 .= ") VALUES (";
2679  $sql2 .= "'".$db->idate(dol_now())."'";
2680  $sql2 .= ", '4'";
2681  $sql2 .= ", ".$obj->rowid;
2682  $sql2 .= ", '160'";
2683  $sql2 .= ", ".$obj->fk_user_resp;
2684  $sql2 .= ")";
2685 
2686  if ($obj->fk_user_resp > 0) {
2687  $resql2 = $db->query($sql2);
2688  if (!$resql2) {
2689  $error++;
2690  dol_print_error($db);
2691  }
2692  }
2693  print ". ";
2694 
2695  $i++;
2696  }
2697  }
2698 
2699  if ($error == 0) {
2700  $sqlDrop = "ALTER TABLE ".MAIN_DB_PREFIX."projet DROP COLUMN fk_user_resp";
2701  if ($db->query($sqlDrop)) {
2702  $db->commit();
2703  } else {
2704  $db->rollback();
2705  }
2706  } else {
2707  $db->rollback();
2708  }
2709  } else {
2710  dol_print_error($db);
2711  $db->rollback();
2712  }
2713  } else {
2714  print $langs->trans('AlreadyDone')."<br>\n";
2715  }
2716  print '</td></tr>';
2717 }
2718 
2727 function migrate_project_task_actors($db, $langs, $conf)
2728 {
2729  dolibarr_install_syslog("upgrade2::migrate_project_task_actors");
2730 
2731  print '<tr><td colspan="4">';
2732 
2733  print '<br>';
2734  print '<b>'.$langs->trans('MigrationProjectTaskActors')."</b><br>\n";
2735 
2736  if ($db->DDLInfoTable(MAIN_DB_PREFIX."projet_task_actors")) {
2737  $error = 0;
2738 
2739  $db->begin();
2740 
2741  $sql = "SELECT fk_projet_task as fk_project_task, fk_user FROM ".MAIN_DB_PREFIX."projet_task_actors";
2742  $resql = $db->query($sql);
2743  if ($resql) {
2744  $i = 0;
2745  $num = $db->num_rows($resql);
2746 
2747  if ($num) {
2748  while ($i < $num) {
2749  $obj = $db->fetch_object($resql);
2750 
2751  $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."element_contact (";
2752  $sql2 .= "datecreate";
2753  $sql2 .= ", statut";
2754  $sql2 .= ", element_id";
2755  $sql2 .= ", fk_c_type_contact";
2756  $sql2 .= ", fk_socpeople";
2757  $sql2 .= ") VALUES (";
2758  $sql2 .= "'".$db->idate(dol_now())."'";
2759  $sql2 .= ", '4'";
2760  $sql2 .= ", ".$obj->fk_project_task;
2761  $sql2 .= ", '180'";
2762  $sql2 .= ", ".$obj->fk_user;
2763  $sql2 .= ")";
2764 
2765  $resql2 = $db->query($sql2);
2766 
2767  if (!$resql2) {
2768  $error++;
2769  dol_print_error($db);
2770  }
2771  print ". ";
2772  $i++;
2773  }
2774  }
2775 
2776  if ($error == 0) {
2777  $sqlDrop = "DROP TABLE ".MAIN_DB_PREFIX."projet_task_actors";
2778  if ($db->query($sqlDrop)) {
2779  $db->commit();
2780  } else {
2781  $db->rollback();
2782  }
2783  } else {
2784  $db->rollback();
2785  }
2786  } else {
2787  dol_print_error($db);
2788  $db->rollback();
2789  }
2790  } else {
2791  print $langs->trans('AlreadyDone')."<br>\n";
2792  }
2793  print '</td></tr>';
2794 }
2795 
2809 function migrate_relationship_tables($db, $langs, $conf, $table, $fk_source, $sourcetype, $fk_target, $targettype)
2810 {
2811  print '<tr><td colspan="4">';
2812 
2813  print '<br>';
2814  print '<b>'.$langs->trans('MigrationRelationshipTables', MAIN_DB_PREFIX.$table)."</b><br>\n";
2815 
2816  $error = 0;
2817 
2818  if ($db->DDLInfoTable(MAIN_DB_PREFIX.$table)) {
2819  dolibarr_install_syslog("upgrade2::migrate_relationship_tables table = ".MAIN_DB_PREFIX.$table);
2820 
2821  $db->begin();
2822 
2823  $sqlSelect = "SELECT ".$fk_source.", ".$fk_target;
2824  $sqlSelect .= " FROM ".MAIN_DB_PREFIX.$table;
2825 
2826  $resql = $db->query($sqlSelect);
2827  if ($resql) {
2828  $i = 0;
2829  $num = $db->num_rows($resql);
2830 
2831  if ($num) {
2832  while ($i < $num) {
2833  $obj = $db->fetch_object($resql);
2834 
2835  $sqlInsert = "INSERT INTO ".MAIN_DB_PREFIX."element_element (";
2836  $sqlInsert .= "fk_source";
2837  $sqlInsert .= ", sourcetype";
2838  $sqlInsert .= ", fk_target";
2839  $sqlInsert .= ", targettype";
2840  $sqlInsert .= ") VALUES (";
2841  $sqlInsert .= $obj->$fk_source;
2842  $sqlInsert .= ", '".$db->escape($sourcetype)."'";
2843  $sqlInsert .= ", ".$obj->$fk_target;
2844  $sqlInsert .= ", '".$db->escape($targettype)."'";
2845  $sqlInsert .= ")";
2846 
2847  $result = $db->query($sqlInsert);
2848  if (!$result) {
2849  $error++;
2850  dol_print_error($db);
2851  }
2852  print ". ";
2853  $i++;
2854  }
2855  } else {
2856  print $langs->trans('AlreadyDone')."<br>\n";
2857  }
2858 
2859  if ($error == 0) {
2860  $sqlDrop = "DROP TABLE ".MAIN_DB_PREFIX.$table;
2861  if ($db->query($sqlDrop)) {
2862  $db->commit();
2863  } else {
2864  $db->rollback();
2865  }
2866  } else {
2867  $db->rollback();
2868  }
2869  } else {
2870  dol_print_error($db);
2871  $db->rollback();
2872  }
2873  } else {
2874  print $langs->trans('AlreadyDone')."<br>\n";
2875  }
2876 
2877  print '</td></tr>';
2878 }
2879 
2888 function migrate_project_task_time($db, $langs, $conf)
2889 {
2890  dolibarr_install_syslog("upgrade2::migrate_project_task_time");
2891 
2892  print '<tr><td colspan="4">';
2893 
2894  print '<br>';
2895  print '<b>'.$langs->trans('MigrationProjectTaskTime')."</b><br>\n";
2896 
2897  $error = 0;
2898 
2899  $db->begin();
2900 
2901  $sql = "SELECT rowid, fk_task, task_duration";
2902  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time";
2903  $resql = $db->query($sql);
2904  if ($resql) {
2905  $i = 0;
2906  $num = $db->num_rows($resql);
2907 
2908  if ($num) {
2909  $totaltime = array();
2910  $oldtime = 0;
2911 
2912  while ($i < $num) {
2913  $obj = $db->fetch_object($resql);
2914 
2915  if ($obj->task_duration > 0) {
2916  // convert to second
2917  // only for int time and float time ex: 1,75 for 1h45
2918  list($hour, $min) = explode('.', $obj->task_duration);
2919  $hour = $hour * 60 * 60;
2920  $min = ($min / 100) * 60 * 60;
2921  $newtime = $hour + $min;
2922 
2923  $sql2 = "UPDATE ".MAIN_DB_PREFIX."projet_task_time SET";
2924  $sql2 .= " task_duration = ".((int) $newtime);
2925  $sql2 .= " WHERE rowid = ".((int) $obj->rowid);
2926 
2927  $resql2 = $db->query($sql2);
2928  if (!$resql2) {
2929  $error++;
2930  dol_print_error($db);
2931  }
2932  print ". ";
2933  $oldtime++;
2934  if (!empty($totaltime[$obj->fk_task])) {
2935  $totaltime[$obj->fk_task] += $newtime;
2936  } else {
2937  $totaltime[$obj->fk_task] = $newtime;
2938  }
2939  } else {
2940  if (!empty($totaltime[$obj->fk_task])) {
2941  $totaltime[$obj->fk_task] += $obj->task_duration;
2942  } else {
2943  $totaltime[$obj->fk_task] = $obj->task_duration;
2944  }
2945  }
2946 
2947  $i++;
2948  }
2949 
2950  if ($error == 0) {
2951  if ($oldtime > 0) {
2952  foreach ($totaltime as $taskid => $total_duration) {
2953  $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task SET";
2954  $sql .= " duration_effective = ".((int) $total_duration);
2955  $sql .= " WHERE rowid = ".((int) $taskid);
2956 
2957  $resql = $db->query($sql);
2958  if (!$resql) {
2959  $error++;
2960  dol_print_error($db);
2961  }
2962  }
2963  } else {
2964  print $langs->trans('AlreadyDone')."<br>\n";
2965  }
2966  } else {
2967  dol_print_error($db);
2968  }
2969  } else {
2970  print $langs->trans('AlreadyDone')."<br>\n";
2971  }
2972  } else {
2973  dol_print_error($db);
2974  }
2975 
2976  if ($error == 0) {
2977  $db->commit();
2978  } else {
2979  $db->rollback();
2980  }
2981 
2982  print '</td></tr>';
2983 }
2984 
2993 function migrate_customerorder_shipping($db, $langs, $conf)
2994 {
2995  print '<tr><td colspan="4">';
2996 
2997  print '<br>';
2998  print '<b>'.$langs->trans('MigrationCustomerOrderShipping')."</b><br>\n";
2999 
3000  $error = 0;
3001 
3002  $result1 = $db->DDLDescTable(MAIN_DB_PREFIX."expedition", "ref_customer");
3003  $result2 = $db->DDLDescTable(MAIN_DB_PREFIX."expedition", "date_delivery");
3004  $obj1 = $db->fetch_object($result1);
3005  $obj2 = $db->fetch_object($result2);
3006  if (!$obj1 && !$obj2) {
3007  dolibarr_install_syslog("upgrade2::migrate_customerorder_shipping");
3008 
3009  $db->begin();
3010 
3011  $sqlAdd1 = "ALTER TABLE ".MAIN_DB_PREFIX."expedition ADD COLUMN ref_customer varchar(30) AFTER entity";
3012  $sqlAdd2 = "ALTER TABLE ".MAIN_DB_PREFIX."expedition ADD COLUMN date_delivery date DEFAULT NULL AFTER date_expedition";
3013 
3014  if ($db->query($sqlAdd1) && $db->query($sqlAdd2)) {
3015  $sqlSelect = "SELECT e.rowid as shipping_id, c.ref_client, c.date_livraison as delivery_date";
3016  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."expedition as e";
3017  $sqlSelect .= ", ".MAIN_DB_PREFIX."element_element as el";
3018  $sqlSelect .= " LEFT JOIN ".MAIN_DB_PREFIX."commande as c ON c.rowid = el.fk_source AND el.sourcetype = 'commande'";
3019  $sqlSelect .= " WHERE e.rowid = el.fk_target";
3020  $sqlSelect .= " AND el.targettype = 'shipping'";
3021 
3022  $resql = $db->query($sqlSelect);
3023  if ($resql) {
3024  $i = 0;
3025  $num = $db->num_rows($resql);
3026 
3027  if ($num) {
3028  while ($i < $num) {
3029  $obj = $db->fetch_object($resql);
3030 
3031  $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."expedition SET";
3032  $sqlUpdate .= " ref_customer = '".$db->escape($obj->ref_client)."'";
3033  $sqlUpdate .= ", date_delivery = '".$db->escape($obj->delivery_date ? $obj->delivery_date : 'null')."'";
3034  $sqlUpdate .= " WHERE rowid = ".((int) $obj->shipping_id);
3035 
3036  $result = $db->query($sqlUpdate);
3037  if (!$result) {
3038  $error++;
3039  dol_print_error($db);
3040  }
3041  print ". ";
3042  $i++;
3043  }
3044  } else {
3045  print $langs->trans('AlreadyDone')."<br>\n";
3046  }
3047 
3048  if ($error == 0) {
3049  $db->commit();
3050  } else {
3051  dol_print_error($db);
3052  $db->rollback();
3053  }
3054  } else {
3055  dol_print_error($db);
3056  $db->rollback();
3057  }
3058  } else {
3059  dol_print_error($db);
3060  $db->rollback();
3061  }
3062  } else {
3063  print $langs->trans('AlreadyDone')."<br>\n";
3064  }
3065 
3066  print '</td></tr>';
3067 }
3068 
3077 function migrate_shipping_delivery($db, $langs, $conf)
3078 {
3079  print '<tr><td colspan="4">';
3080 
3081  print '<br>';
3082  print '<b>'.$langs->trans('MigrationShippingDelivery')."</b><br>\n";
3083 
3084  $error = 0;
3085 
3086  $result = $db->DDLDescTable(MAIN_DB_PREFIX."livraison", "fk_expedition");
3087  $obj = $db->fetch_object($result);
3088  if ($obj) {
3089  dolibarr_install_syslog("upgrade2::migrate_shipping_delivery");
3090 
3091  $db->begin();
3092 
3093  $sqlSelect = "SELECT rowid, fk_expedition";
3094  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."livraison";
3095  $sqlSelect .= " WHERE fk_expedition is not null";
3096 
3097  $resql = $db->query($sqlSelect);
3098  if ($resql) {
3099  $i = 0;
3100  $num = $db->num_rows($resql);
3101 
3102  if ($num) {
3103  while ($i < $num) {
3104  $obj = $db->fetch_object($resql);
3105 
3106  $sqlInsert = "INSERT INTO ".MAIN_DB_PREFIX."element_element (";
3107  $sqlInsert .= "fk_source";
3108  $sqlInsert .= ", sourcetype";
3109  $sqlInsert .= ", fk_target";
3110  $sqlInsert .= ", targettype";
3111  $sqlInsert .= ") VALUES (";
3112  $sqlInsert .= $obj->fk_expedition;
3113  $sqlInsert .= ", 'shipping'";
3114  $sqlInsert .= ", ".$obj->rowid;
3115  $sqlInsert .= ", 'delivery'";
3116  $sqlInsert .= ")";
3117 
3118  $result = $db->query($sqlInsert);
3119  if ($result) {
3120  $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."livraison SET fk_expedition = NULL";
3121  $sqlUpdate .= " WHERE rowid = ".((int) $obj->rowid);
3122 
3123  $result = $db->query($sqlUpdate);
3124  if (!$result) {
3125  $error++;
3126  dol_print_error($db);
3127  }
3128  print ". ";
3129  } else {
3130  $error++;
3131  dol_print_error($db);
3132  }
3133  $i++;
3134  }
3135  } else {
3136  print $langs->trans('AlreadyDone')."<br>\n";
3137  }
3138 
3139  if ($error == 0) {
3140  $sqlDelete = "DELETE FROM ".MAIN_DB_PREFIX."element_element WHERE sourcetype = 'commande' AND targettype = 'delivery'";
3141  $db->query($sqlDelete);
3142 
3143  $db->commit();
3144 
3145  // DDL commands must not be inside a transaction
3146  $sqlDrop = "ALTER TABLE ".MAIN_DB_PREFIX."livraison DROP COLUMN fk_expedition";
3147  $db->query($sqlDrop);
3148  } else {
3149  dol_print_error($db);
3150  $db->rollback();
3151  }
3152  } else {
3153  dol_print_error($db);
3154  $db->rollback();
3155  }
3156  } else {
3157  print $langs->trans('AlreadyDone')."<br>\n";
3158  }
3159 
3160  print '</td></tr>';
3161 }
3162 
3172 function migrate_shipping_delivery2($db, $langs, $conf)
3173 {
3174  print '<tr><td colspan="4">';
3175 
3176  print '<br>';
3177  print '<b>'.$langs->trans('MigrationShippingDelivery2')."</b><br>\n";
3178 
3179  $error = 0;
3180 
3181  dolibarr_install_syslog("upgrade2::migrate_shipping_delivery2");
3182 
3183  $db->begin();
3184 
3185  $sqlSelect = "SELECT l.rowid as delivery_id, e.ref_customer, e.date_delivery";
3186  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."livraison as l,";
3187  $sqlSelect .= " ".MAIN_DB_PREFIX."element_element as el,";
3188  $sqlSelect .= " ".MAIN_DB_PREFIX."expedition as e";
3189  $sqlSelect .= " WHERE l.rowid = el.fk_target";
3190  $sqlSelect .= " AND el.targettype = 'delivery'";
3191  $sqlSelect .= " AND e.rowid = el.fk_source AND el.sourcetype = 'shipping'";
3192  $sqlSelect .= " AND (e.ref_customer IS NOT NULL OR e.date_delivery IS NOT NULL)"; // Useless to process this record if both are null
3193  // Add condition to know if we never migrate this record
3194  $sqlSelect .= " AND (l.ref_customer IS NULL".($db->type != 'pgsql' ? " or l.ref_customer = ''" : "").")";
3195  $sqlSelect .= " AND (l.date_delivery IS NULL".($db->type != 'pgsql' ? " or l.date_delivery = ''" : "").")";
3196 
3197  $resql = $db->query($sqlSelect);
3198  if ($resql) {
3199  $i = 0;
3200  $num = $db->num_rows($resql);
3201 
3202  if ($num) {
3203  while ($i < $num) {
3204  $obj = $db->fetch_object($resql);
3205 
3206  $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."livraison SET";
3207  $sqlUpdate .= " ref_customer = '".$db->escape($obj->ref_customer)."',";
3208  $sqlUpdate .= " date_delivery = ".($obj->date_delivery ? "'".$db->escape($obj->date_delivery)."'" : 'null');
3209  $sqlUpdate .= " WHERE rowid = ".((int) $obj->delivery_id);
3210 
3211  $result = $db->query($sqlUpdate);
3212  if (!$result) {
3213  $error++;
3214  dol_print_error($db);
3215  }
3216  print ". ";
3217  $i++;
3218  }
3219  } else {
3220  print $langs->trans('AlreadyDone')."<br>\n";
3221  }
3222 
3223  if ($error == 0) {
3224  $db->commit();
3225  } else {
3226  dol_print_error($db);
3227  $db->rollback();
3228  }
3229  } else {
3230  dol_print_error($db);
3231  $db->rollback();
3232  }
3233 
3234  print '</td></tr>';
3235 }
3236 
3245 function migrate_actioncomm_element($db, $langs, $conf)
3246 {
3247  print '<tr><td colspan="4">';
3248 
3249  print '<br>';
3250  print '<b>'.$langs->trans('MigrationActioncommElement')."</b><br>\n";
3251 
3252  $elements = array(
3253  'propal' => 'propalrowid',
3254  'order' => 'fk_commande',
3255  'invoice' => 'fk_facture',
3256  'contract' => 'fk_contract',
3257  'order_supplier' => 'fk_supplier_order',
3258  'invoice_supplier' => 'fk_supplier_invoice'
3259  );
3260 
3261  foreach ($elements as $type => $field) {
3262  $result = $db->DDLDescTable(MAIN_DB_PREFIX."actioncomm", $field);
3263  $obj = $db->fetch_object($result);
3264  if ($obj) {
3265  dolibarr_install_syslog("upgrade2::migrate_actioncomm_element field=".$field);
3266 
3267  $db->begin();
3268 
3269  $sql = "UPDATE ".MAIN_DB_PREFIX."actioncomm SET ";
3270  $sql .= "fk_element = ".$field.", elementtype = '".$db->escape($type)."'";
3271  $sql .= " WHERE ".$field." IS NOT NULL";
3272  $sql .= " AND fk_element IS NULL";
3273  $sql .= " AND elementtype IS NULL";
3274 
3275  $resql = $db->query($sql);
3276  if ($resql) {
3277  $db->commit();
3278 
3279  // DDL commands must not be inside a transaction
3280  // We will drop at next version because a migrate should be runnable several times if it fails.
3281  //$sqlDrop = "ALTER TABLE ".MAIN_DB_PREFIX."actioncomm DROP COLUMN ".$field;
3282  //$db->query($sqlDrop);
3283  //print ". ";
3284  } else {
3285  dol_print_error($db);
3286  $db->rollback();
3287  }
3288  } else {
3289  print $langs->trans('AlreadyDone')."<br>\n";
3290  }
3291  }
3292 
3293  print '</td></tr>';
3294 }
3295 
3304 function migrate_mode_reglement($db, $langs, $conf)
3305 {
3306  print '<tr><td colspan="4">';
3307 
3308  print '<br>';
3309  print '<b>'.$langs->trans('MigrationPaymentMode')."</b><br>\n";
3310 
3311  $elements = array(
3312  'old_id' => array(5, 8, 9, 10, 11),
3313  'new_id' => array(50, 51, 52, 53, 54),
3314  'code' => array('VAD', 'TRA', 'LCR', 'FAC', 'PRO'),
3315  'tables' => array('commande_fournisseur', 'commande', 'facture_rec', 'facture', 'propal')
3316  );
3317  $count = 0;
3318 
3319  foreach ($elements['old_id'] as $key => $old_id) {
3320  $error = 0;
3321 
3322  dolibarr_install_syslog("upgrade2::migrate_mode_reglement code=".$elements['code'][$key]);
3323 
3324  $sqlSelect = "SELECT id";
3325  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."c_paiement";
3326  $sqlSelect .= " WHERE id = ".((int) $old_id);
3327  $sqlSelect .= " AND code = '".$db->escape($elements['code'][$key])."'";
3328 
3329  $resql = $db->query($sqlSelect);
3330  if ($resql) {
3331  $num = $db->num_rows($resql);
3332  if ($num) {
3333  $count++;
3334 
3335  $db->begin();
3336 
3337  $sqla = "UPDATE ".MAIN_DB_PREFIX."paiement SET";
3338  $sqla .= " fk_paiement = ".((int) $elements['new_id'][$key]);
3339  $sqla .= " WHERE fk_paiement = ".((int) $old_id);
3340  $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])."')";
3341  $resqla = $db->query($sqla);
3342 
3343  $sql = "UPDATE ".MAIN_DB_PREFIX."c_paiement SET";
3344  $sql .= " id = ".((int) $elements['new_id'][$key]);
3345  $sql .= " WHERE id = ".((int) $old_id);
3346  $sql .= " AND code = '".$db->escape($elements['code'][$key])."'";
3347  $resql = $db->query($sql);
3348 
3349  if ($resqla && $resql) {
3350  foreach ($elements['tables'] as $table) {
3351  $sql = "UPDATE ".MAIN_DB_PREFIX.$table." SET ";
3352  $sql .= "fk_mode_reglement = ".((int) $elements['new_id'][$key]);
3353  $sql .= " WHERE fk_mode_reglement = ".((int) $old_id);
3354 
3355  $resql = $db->query($sql);
3356  if (!$resql) {
3357  dol_print_error($db);
3358  $error++;
3359  }
3360  print ". ";
3361  }
3362 
3363  if (!$error) {
3364  $db->commit();
3365  } else {
3366  dol_print_error($db);
3367  $db->rollback();
3368  }
3369  } else {
3370  dol_print_error($db);
3371  $db->rollback();
3372  }
3373  }
3374  }
3375  }
3376 
3377  if ($count == 0) {
3378  print $langs->trans('AlreadyDone')."<br>\n";
3379  }
3380 
3381 
3382  print '</td></tr>';
3383 }
3384 
3385 
3394 function migrate_clean_association($db, $langs, $conf)
3395 {
3396  $result = $db->DDLDescTable(MAIN_DB_PREFIX."categorie_association");
3397  if ($result) { // result defined for version 3.2 or -
3398  $obj = $db->fetch_object($result);
3399  if ($obj) { // It table categorie_association exists
3400  $couples = array();
3401  $filles = array();
3402  $sql = "SELECT fk_categorie_mere, fk_categorie_fille";
3403  $sql .= " FROM ".MAIN_DB_PREFIX."categorie_association";
3404  dolibarr_install_syslog("upgrade: search duplicate");
3405  $resql = $db->query($sql);
3406  if ($resql) {
3407  $num = $db->num_rows($resql);
3408  while ($obj = $db->fetch_object($resql)) {
3409  if (!isset($filles[$obj->fk_categorie_fille])) { // Only one record as child (a child has only on parent).
3410  if ($obj->fk_categorie_mere != $obj->fk_categorie_fille) {
3411  $filles[$obj->fk_categorie_fille] = 1; // Set record for this child
3412  $couples[$obj->fk_categorie_mere.'_'.$obj->fk_categorie_fille] = array('mere'=>$obj->fk_categorie_mere, 'fille'=>$obj->fk_categorie_fille);
3413  }
3414  }
3415  }
3416 
3417  dolibarr_install_syslog("upgrade: result is num=".$num." count(couples)=".count($couples));
3418 
3419  // If there is duplicates couples or child with two parents
3420  if (count($couples) > 0 && $num > count($couples)) {
3421  $error = 0;
3422 
3423  $db->begin();
3424 
3425  // We delete all
3426  $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_association";
3427  dolibarr_install_syslog("upgrade: delete association");
3428  $resqld = $db->query($sql);
3429  if ($resqld) {
3430  // And we insert only each record once
3431  foreach ($couples as $key => $val) {
3432  $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie_association(fk_categorie_mere,fk_categorie_fille)";
3433  $sql .= " VALUES(".((int) $val['mere']).", ".((int) $val['fille']).")";
3434  dolibarr_install_syslog("upgrade: insert association");
3435  $resqli = $db->query($sql);
3436  if (!$resqli) {
3437  $error++;
3438  }
3439  }
3440  }
3441 
3442  if (!$error) {
3443  print '<tr><td>'.$langs->trans("MigrationCategorieAssociation").'</td>';
3444  print '<td class="right">'.$langs->trans("RemoveDuplicates").' '.$langs->trans("Success").' ('.$num.'=>'.count($couples).')</td></tr>';
3445  $db->commit();
3446  } else {
3447  print '<tr><td>'.$langs->trans("MigrationCategorieAssociation").'</td>';
3448  print '<td class="right">'.$langs->trans("RemoveDuplicates").' '.$langs->trans("Failed").'</td></tr>';
3449  $db->rollback();
3450  }
3451  }
3452  } else {
3453  print '<tr><td>'.$langs->trans("Error").'</td>';
3454  print '<td class="right"><div class="error">'.$db->lasterror().'</div></td></tr>';
3455  }
3456  }
3457  }
3458 }
3459 
3460 
3469 function migrate_categorie_association($db, $langs, $conf)
3470 {
3471  print '<tr><td colspan="4">';
3472 
3473  print '<br>';
3474  print '<b>'.$langs->trans('MigrationCategorieAssociation')."</b><br>\n";
3475 
3476  $error = 0;
3477 
3478  if ($db->DDLInfoTable(MAIN_DB_PREFIX."categorie_association")) {
3479  dolibarr_install_syslog("upgrade2::migrate_categorie_association");
3480 
3481  $db->begin();
3482 
3483  $sqlSelect = "SELECT fk_categorie_mere, fk_categorie_fille";
3484  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."categorie_association";
3485 
3486  $resql = $db->query($sqlSelect);
3487  if ($resql) {
3488  $i = 0;
3489  $num = $db->num_rows($resql);
3490 
3491  if ($num) {
3492  while ($i < $num) {
3493  $obj = $db->fetch_object($resql);
3494 
3495  $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."categorie SET ";
3496  $sqlUpdate .= "fk_parent = ".((int) $obj->fk_categorie_mere);
3497  $sqlUpdate .= " WHERE rowid = ".((int) $obj->fk_categorie_fille);
3498 
3499  $result = $db->query($sqlUpdate);
3500  if (!$result) {
3501  $error++;
3502  dol_print_error($db);
3503  }
3504  print ". ";
3505  $i++;
3506  }
3507  } else {
3508  print $langs->trans('AlreadyDone')."<br>\n";
3509  }
3510 
3511  if (!$error) {
3512  $db->commit();
3513  } else {
3514  $db->rollback();
3515  }
3516  } else {
3517  dol_print_error($db);
3518  $db->rollback();
3519  }
3520  } else {
3521  print $langs->trans('AlreadyDone')."<br>\n";
3522  }
3523 
3524  print '</td></tr>';
3525 }
3526 
3535 function migrate_event_assignement($db, $langs, $conf)
3536 {
3537  print '<tr><td colspan="4">';
3538 
3539  print '<br>';
3540  print '<b>'.$langs->trans('MigrationEvents')."</b><br>\n";
3541 
3542  $error = 0;
3543 
3544  dolibarr_install_syslog("upgrade2::migrate_event_assignement");
3545 
3546  $db->begin();
3547 
3548  $sqlSelect = "SELECT a.id, a.fk_user_action";
3549  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."actioncomm as a";
3550  $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";
3551  $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')";
3552  $sqlSelect .= " ORDER BY a.id";
3553  //print $sqlSelect;
3554 
3555  $resql = $db->query($sqlSelect);
3556  if ($resql) {
3557  $i = 0;
3558  $num = $db->num_rows($resql);
3559 
3560  if ($num) {
3561  while ($i < $num) {
3562  $obj = $db->fetch_object($resql);
3563 
3564  $sqlUpdate = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element) ";
3565  $sqlUpdate .= "VALUES(".((int) $obj->id).", 'user', ".((int) $obj->fk_user_action).")";
3566 
3567  $result = $db->query($sqlUpdate);
3568  if (!$result) {
3569  $error++;
3570  dol_print_error($db);
3571  }
3572  print ". ";
3573  $i++;
3574  }
3575  } else {
3576  print $langs->trans('AlreadyDone')."<br>\n";
3577  }
3578 
3579  if (!$error) {
3580  $db->commit();
3581  } else {
3582  $db->rollback();
3583  }
3584  } else {
3585  dol_print_error($db);
3586  $db->rollback();
3587  }
3588 
3589 
3590  print '</td></tr>';
3591 }
3592 
3601 function migrate_event_assignement_contact($db, $langs, $conf)
3602 {
3603  print '<tr><td colspan="4">';
3604 
3605  print '<br>';
3606  print '<b>'.$langs->trans('MigrationEventsContact')."</b><br>\n";
3607 
3608  $error = 0;
3609 
3610  dolibarr_install_syslog("upgrade2::migrate_event_assignement");
3611 
3612  $db->begin();
3613 
3614  $sqlSelect = "SELECT a.id, a.fk_contact";
3615  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."actioncomm as a";
3616  $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";
3617  $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')";
3618  $sqlSelect .= " ORDER BY a.id";
3619  //print $sqlSelect;
3620 
3621  $resql = $db->query($sqlSelect);
3622  if ($resql) {
3623  $i = 0;
3624  $num = $db->num_rows($resql);
3625 
3626  if ($num) {
3627  while ($i < $num) {
3628  $obj = $db->fetch_object($resql);
3629 
3630  $sqlUpdate = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm_resources(fk_actioncomm, element_type, fk_element) ";
3631  $sqlUpdate .= "VALUES(".((int) $obj->id).", 'socpeople', ".((int) $obj->fk_contact).")";
3632 
3633  $result = $db->query($sqlUpdate);
3634  if (!$result) {
3635  $error++;
3636  dol_print_error($db);
3637  }
3638  print ". ";
3639  $i++;
3640  }
3641  } else {
3642  print $langs->trans('AlreadyDone')."<br>\n";
3643  }
3644 
3645  if (!$error) {
3646  $db->commit();
3647  } else {
3648  $db->rollback();
3649  }
3650  } else {
3651  dol_print_error($db);
3652  $db->rollback();
3653  }
3654 
3655 
3656  print '</td></tr>';
3657 }
3658 
3659 
3668 function migrate_reset_blocked_log($db, $langs, $conf)
3669 {
3670  global $user;
3671 
3672  require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
3673 
3674  print '<tr><td colspan="4">';
3675 
3676  print '<br>';
3677  print '<b>'.$langs->trans('MigrationResetBlockedLog')."</b><br>\n";
3678 
3679  $error = 0;
3680 
3681  dolibarr_install_syslog("upgrade2::migrate_reset_blocked_log");
3682 
3683  $db->begin();
3684 
3685  $sqlSelect = "SELECT DISTINCT entity";
3686  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."blockedlog";
3687 
3688  //print $sqlSelect;
3689 
3690  $resql = $db->query($sqlSelect);
3691  if ($resql) {
3692  $i = 0;
3693  $num = $db->num_rows($resql);
3694 
3695  if ($num) {
3696  while ($i < $num) {
3697  $obj = $db->fetch_object($resql);
3698 
3699  print 'Process entity '.$obj->entity;
3700 
3701  $sqlSearch = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."blockedlog WHERE action = 'MODULE_SET' and entity = ".((int) $obj->entity);
3702  $resqlSearch = $db->query($sqlSearch);
3703  if ($resqlSearch) {
3704  $objSearch = $db->fetch_object($resqlSearch);
3705  //var_dump($objSearch);
3706  if ($objSearch && $objSearch->nb == 0) {
3707  print ' - Record for entity must be reset...';
3708 
3709  $sqlUpdate = "DELETE FROM ".MAIN_DB_PREFIX."blockedlog";
3710  $sqlUpdate .= " WHERE entity = ".((int) $obj->entity);
3711  $resqlUpdate = $db->query($sqlUpdate);
3712  if (!$resqlUpdate) {
3713  $error++;
3714  dol_print_error($db);
3715  } else {
3716  // Add set line
3717  $object = new stdClass();
3718  $object->id = 1;
3719  $object->element = 'module';
3720  $object->ref = 'systemevent';
3721  $object->entity = $obj->entity;
3722  $object->date = dol_now();
3723 
3724  $b = new BlockedLog($db);
3725  $b->setObjectData($object, 'MODULE_SET', 0);
3726 
3727  $res = $b->create($user);
3728  if ($res <= 0) {
3729  $error++;
3730  }
3731  }
3732  } else {
3733  print ' - '.$langs->trans('AlreadyInV7').'<br>';
3734  }
3735  } else {
3736  dol_print_error($db);
3737  }
3738 
3739  $i++;
3740  }
3741  } else {
3742  print $langs->trans('NothingToDo')."<br>\n";
3743  }
3744 
3745  if (!$error) {
3746  $db->commit();
3747  } else {
3748  $db->rollback();
3749  }
3750  } else {
3751  dol_print_error($db);
3752  $db->rollback();
3753  }
3754 
3755  print '</td></tr>';
3756 }
3757 
3758 
3767 function migrate_remise_entity($db, $langs, $conf)
3768 {
3769  print '<tr><td colspan="4">';
3770 
3771  print '<br>';
3772  print '<b>'.$langs->trans('MigrationRemiseEntity')."</b><br>\n";
3773 
3774  $error = 0;
3775 
3776  dolibarr_install_syslog("upgrade2::migrate_remise_entity");
3777 
3778  $db->begin();
3779 
3780  $sqlSelect = "SELECT sr.rowid, s.entity";
3781  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."societe_remise as sr, ".MAIN_DB_PREFIX."societe as s";
3782  $sqlSelect .= " WHERE sr.fk_soc = s.rowid and sr.entity != s.entity";
3783 
3784  //print $sqlSelect;
3785 
3786  $resql = $db->query($sqlSelect);
3787  if ($resql) {
3788  $i = 0;
3789  $num = $db->num_rows($resql);
3790 
3791  if ($num) {
3792  while ($i < $num) {
3793  $obj = $db->fetch_object($resql);
3794 
3795  $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."societe_remise SET";
3796  $sqlUpdate .= " entity = ".$obj->entity;
3797  $sqlUpdate .= " WHERE rowid = ".((int) $obj->rowid);
3798 
3799  $result = $db->query($sqlUpdate);
3800  if (!$result) {
3801  $error++;
3802  dol_print_error($db);
3803  }
3804 
3805  print ". ";
3806  $i++;
3807  }
3808  } else {
3809  print $langs->trans('AlreadyDone')."<br>\n";
3810  }
3811 
3812  if (!$error) {
3813  $db->commit();
3814  } else {
3815  $db->rollback();
3816  }
3817  } else {
3818  dol_print_error($db);
3819  $db->rollback();
3820  }
3821 
3822  print '</td></tr>';
3823 }
3824 
3833 function migrate_remise_except_entity($db, $langs, $conf)
3834 {
3835  print '<tr><td colspan="4">';
3836 
3837  print '<br>';
3838  print '<b>'.$langs->trans('MigrationRemiseExceptEntity')."</b><br>\n";
3839 
3840  $error = 0;
3841 
3842  dolibarr_install_syslog("upgrade2::migrate_remise_except_entity");
3843 
3844  $db->begin();
3845 
3846  $sqlSelect = "SELECT sr.rowid, sr.fk_soc, sr.fk_facture_source, sr.fk_facture, sr.fk_facture_line";
3847  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as sr";
3848  //print $sqlSelect;
3849 
3850  $resql = $db->query($sqlSelect);
3851  if ($resql) {
3852  $i = 0;
3853  $num = $db->num_rows($resql);
3854 
3855  if ($num) {
3856  while ($i < $num) {
3857  $obj = $db->fetch_object($resql);
3858 
3859  if (!empty($obj->fk_facture_source) || !empty($obj->fk_facture)) {
3860  $fk_facture = (!empty($obj->fk_facture_source) ? $obj->fk_facture_source : $obj->fk_facture);
3861 
3862  $sqlSelect2 = "SELECT f.entity";
3863  $sqlSelect2 .= " FROM ".MAIN_DB_PREFIX."facture as f";
3864  $sqlSelect2 .= " WHERE f.rowid = ".((int) $fk_facture);
3865  } elseif (!empty($obj->fk_facture_line)) {
3866  $sqlSelect2 = "SELECT f.entity";
3867  $sqlSelect2 .= " FROM ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."facturedet as fd";
3868  $sqlSelect2 .= " WHERE fd.rowid = ".((int) $obj->fk_facture_line);
3869  $sqlSelect2 .= " AND fd.fk_facture = f.rowid";
3870  } else {
3871  $sqlSelect2 = "SELECT s.entity";
3872  $sqlSelect2 .= " FROM ".MAIN_DB_PREFIX."societe as s";
3873  $sqlSelect2 .= " WHERE s.rowid = ".((int) $obj->fk_soc);
3874  }
3875 
3876  $resql2 = $db->query($sqlSelect2);
3877  if ($resql2) {
3878  if ($db->num_rows($resql2) > 0) {
3879  $obj2 = $db->fetch_object($resql2);
3880 
3881  $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."societe_remise_except SET";
3882  $sqlUpdate .= " entity = ".((int) $obj2->entity);
3883  $sqlUpdate .= " WHERE rowid = ".((int) $obj->rowid);
3884 
3885  $result = $db->query($sqlUpdate);
3886  if (!$result) {
3887  $error++;
3888  dol_print_error($db);
3889  }
3890  }
3891  } else {
3892  $error++;
3893  dol_print_error($db);
3894  }
3895 
3896  print ". ";
3897  $i++;
3898  }
3899  } else {
3900  print $langs->trans('AlreadyDone')."<br>\n";
3901  }
3902 
3903  if (!$error) {
3904  $db->commit();
3905  } else {
3906  $db->rollback();
3907  }
3908  } else {
3909  dol_print_error($db);
3910  $db->rollback();
3911  }
3912 
3913 
3914  print '</td></tr>';
3915 }
3916 
3925 function migrate_user_rights_entity($db, $langs, $conf)
3926 {
3927  print '<tr><td colspan="4">';
3928 
3929  print '<b>'.$langs->trans('MigrationUserRightsEntity')."</b><br>\n";
3930 
3931  $error = 0;
3932 
3933  dolibarr_install_syslog("upgrade2::migrate_user_rights_entity");
3934 
3935  $db->begin();
3936 
3937  $sqlSelect = "SELECT u.rowid, u.entity";
3938  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."user as u";
3939  $sqlSelect .= " WHERE u.entity > 1";
3940  //print $sqlSelect;
3941 
3942  $resql = $db->query($sqlSelect);
3943  if ($resql) {
3944  $i = 0;
3945  $num = $db->num_rows($resql);
3946 
3947  if ($num) {
3948  while ($i < $num) {
3949  $obj = $db->fetch_object($resql);
3950 
3951  $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."user_rights SET";
3952  $sqlUpdate .= " entity = ".((int) $obj->entity);
3953  $sqlUpdate .= " WHERE fk_user = ".((int) $obj->rowid);
3954 
3955  $result = $db->query($sqlUpdate);
3956  if (!$result) {
3957  $error++;
3958  dol_print_error($db);
3959  }
3960 
3961  print ". ";
3962  $i++;
3963  }
3964  } else {
3965  print $langs->trans('AlreadyDone')."<br>\n";
3966  }
3967 
3968  if (!$error) {
3969  $db->commit();
3970  } else {
3971  $db->rollback();
3972  }
3973  } else {
3974  dol_print_error($db);
3975  $db->rollback();
3976  }
3977 
3978 
3979  print '</td></tr>';
3980 }
3981 
3990 function migrate_usergroup_rights_entity($db, $langs, $conf)
3991 {
3992  print '<tr><td colspan="4">';
3993 
3994  print '<b>'.$langs->trans('MigrationUserGroupRightsEntity')."</b><br>\n";
3995 
3996  $error = 0;
3997 
3998  dolibarr_install_syslog("upgrade2::migrate_usergroup_rights_entity");
3999 
4000  $db->begin();
4001 
4002  $sqlSelect = "SELECT u.rowid, u.entity";
4003  $sqlSelect .= " FROM ".MAIN_DB_PREFIX."usergroup as u";
4004  $sqlSelect .= " WHERE u.entity > 1";
4005  //print $sqlSelect;
4006 
4007  $resql = $db->query($sqlSelect);
4008  if ($resql) {
4009  $i = 0;
4010  $num = $db->num_rows($resql);
4011 
4012  if ($num) {
4013  while ($i < $num) {
4014  $obj = $db->fetch_object($resql);
4015 
4016  $sqlUpdate = "UPDATE ".MAIN_DB_PREFIX."usergroup_rights SET";
4017  $sqlUpdate .= " entity = ".((int) $obj->entity);
4018  $sqlUpdate .= " WHERE fk_usergroup = ".((int) $obj->rowid);
4019 
4020  $result = $db->query($sqlUpdate);
4021  if (!$result) {
4022  $error++;
4023  dol_print_error($db);
4024  }
4025 
4026  print ". ";
4027  $i++;
4028  }
4029  } else {
4030  print $langs->trans('AlreadyDone')."<br>\n";
4031  }
4032 
4033  if (!$error) {
4034  $db->commit();
4035  } else {
4036  $db->rollback();
4037  }
4038  } else {
4039  dol_print_error($db);
4040  $db->rollback();
4041  }
4042 
4043 
4044  print '</td></tr>';
4045 }
4046 
4057 function migrate_rename_directories($db, $langs, $conf, $oldname, $newname)
4058 {
4059  dolibarr_install_syslog("upgrade2::migrate_rename_directories");
4060 
4061  if (is_dir(DOL_DATA_ROOT.$oldname) && !file_exists(DOL_DATA_ROOT.$newname)) {
4062  dolibarr_install_syslog("upgrade2::migrate_rename_directories move ".DOL_DATA_ROOT.$oldname.' into '.DOL_DATA_ROOT.$newname);
4063  @rename(DOL_DATA_ROOT.$oldname, DOL_DATA_ROOT.$newname);
4064  }
4065 }
4066 
4067 
4076 function migrate_delete_old_files($db, $langs, $conf)
4077 {
4078  $result = true;
4079 
4080  dolibarr_install_syslog("upgrade2::migrate_delete_old_files");
4081 
4082  // List of files to delete
4083  $filetodeletearray = array(
4084  '/core/triggers/interface_demo.class.php',
4085  '/core/menus/barre_left/default.php',
4086  '/core/menus/barre_top/default.php',
4087  '/core/modules/modComptabiliteExpert.class.php',
4088  '/core/modules/modCommercial.class.php',
4089  '/core/modules/modProduit.class.php',
4090  '/core/modules/modSkype.class.php',
4091  '/phenix/inc/triggers/interface_modPhenix_Phenixsynchro.class.php',
4092  '/webcalendar/inc/triggers/interface_modWebcalendar_webcalsynchro.class.php',
4093  '/core/triggers/interface_modWebcalendar_Webcalsynchro.class.php',
4094  '/core/triggers/interface_modCommande_Ecotax.class.php',
4095  '/core/triggers/interface_modCommande_fraisport.class.php',
4096  '/core/triggers/interface_modPropale_PropalWorkflow.class.php',
4097  '/core/menus/smartphone/iphone.lib.php',
4098  '/core/menus/smartphone/iphone_backoffice.php',
4099  '/core/menus/smartphone/iphone_frontoffice.php',
4100  '/core/menus/standard/auguria_backoffice.php',
4101  '/core/menus/standard/auguria_frontoffice.php',
4102  '/core/menus/standard/eldy_backoffice.php',
4103  '/core/menus/standard/eldy_frontoffice.php',
4104  '/core/modules/mailings/contacts2.modules.php',
4105  '/core/modules/mailings/contacts3.modules.php',
4106  '/core/modules/mailings/contacts4.modules.php',
4107  '/core/modules/mailings/framboise.modules.php',
4108  '/core/modules/mailings/dolibarr_services_expired.modules.php',
4109  '/core/modules/mailings/peche.modules.php',
4110  '/core/modules/mailings/poire.modules.php',
4111  '/core/modules/mailings/kiwi.modules.php',
4112  '/core/modules/facture/pdf_crabe.modules.php',
4113  '/core/modules/facture/pdf_oursin.modules.php',
4114  '/core/modules/export/export_excel.modules.php',
4115  '/core/modules/export/export_excel2007new.modules.php',
4116  '/core/boxes/box_members.php',
4117 
4118  '/api/class/api_generic.class.php',
4119  '/categories/class/api_category.class.php',
4120  '/categories/class/api_deprecated_category.class.php',
4121  '/compta/facture/class/api_invoice.class.php',
4122  '/commande/class/api_commande.class.php',
4123  '/user/class/api_user.class.php',
4124  '/product/class/api_product.class.php',
4125  '/societe/class/api_contact.class.php',
4126  '/societe/class/api_thirdparty.class.php',
4127  '/support/online.php',
4128  '/takepos/class/actions_takepos.class.php',
4129 
4130  '/install/mysql/tables/llx_c_ticketsup_category.key.sql',
4131  '/install/mysql/tables/llx_c_ticketsup_category.sql',
4132  '/install/mysql/tables/llx_c_ticketsup_severity.key.sql',
4133  '/install/mysql/tables/llx_c_ticketsup_severity.sql',
4134  '/install/mysql/tables/llx_c_ticketsup_type.key.sql',
4135  '/install/mysql/tables/llx_c_ticketsup_type.sql'
4136  );
4137 
4138  foreach ($filetodeletearray as $filetodelete) {
4139  //print '<b>'DOL_DOCUMENT_ROOT.$filetodelete."</b><br>\n";
4140  $result = 1;
4141  if (file_exists(DOL_DOCUMENT_ROOT.$filetodelete)) {
4142  $result = dol_delete_file(DOL_DOCUMENT_ROOT.$filetodelete, 0, 0, 0, null, true, false);
4143  if (!$result) {
4144  $langs->load("errors");
4145  print '<div class="error">'.$langs->trans("Error").': '.$langs->trans("ErrorFailToDeleteFile", DOL_DOCUMENT_ROOT.$filetodelete);
4146  print ' '.$langs->trans("RemoveItManuallyAndPressF5ToContinue").'</div>';
4147  } else {
4148  //print $langs->trans("FileWasRemoved", $filetodelete).'<br>';
4149  }
4150  }
4151  }
4152  return $result;
4153 }
4154 
4163 function migrate_delete_old_dir($db, $langs, $conf)
4164 {
4165  $result = true;
4166 
4167  dolibarr_install_syslog("upgrade2::migrate_delete_old_dir");
4168 
4169  // List of files to delete
4170  $filetodeletearray = array(
4171  DOL_DOCUMENT_ROOT.'/core/modules/facture/terre',
4172  DOL_DOCUMENT_ROOT.'/core/modules/facture/mercure',
4173  );
4174 
4175  // On linux, we can also removed old directory with a different case than new directory.
4176  if (!empty($_SERVER["WINDIR"])) {
4177  $filetodeletearray[] = DOL_DOCUMENT_ROOT.'/includes/phpoffice/PhpSpreadsheet';
4178  }
4179 
4180  foreach ($filetodeletearray as $filetodelete) {
4181  //print '<b>'.$filetodelete."</b><br>\n";
4182  if (file_exists($filetodelete)) {
4183  $result = dol_delete_dir_recursive($filetodelete);
4184  }
4185  if (!$result) {
4186  $langs->load("errors");
4187  print '<div class="error">'.$langs->trans("Error").': '.$langs->trans("ErrorFailToDeleteDir", $filetodelete);
4188  print ' '.$langs->trans("RemoveItManuallyAndPressF5ToContinue").'</div>';
4189  }
4190  }
4191  return $result;
4192 }
4193 
4194 
4207 function migrate_reload_modules($db, $langs, $conf, $listofmodule = array(), $force = 0)
4208 {
4209  if (count($listofmodule) == 0) {
4210  return;
4211  }
4212 
4213  dolibarr_install_syslog("upgrade2::migrate_reload_modules force=".$force.", listofmodule=".join(',', array_keys($listofmodule)));
4214 
4215  foreach ($listofmodule as $moduletoreload => $reloadmode) { // reloadmodule can be 'noboxes', 'newboxdefonly', 'forceactivate'
4216  if (empty($moduletoreload) || (empty($conf->global->$moduletoreload) && !$force)) {
4217  continue; // Discard reload if module not enabled
4218  }
4219 
4220  $mod = null;
4221 
4222  if ($moduletoreload == 'MAIN_MODULE_AGENDA') {
4223  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Agenda module");
4224  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modAgenda.class.php';
4225  if ($res) {
4226  $mod = new modAgenda($db);
4227  $mod->remove('noboxes');
4228  $mod->init($reloadmode);
4229  }
4230  } elseif ($moduletoreload == 'MAIN_MODULE_API') {
4231  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Rest API module");
4232  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modApi.class.php';
4233  if ($res) {
4234  $mod = new modApi($db);
4235  //$mod->remove('noboxes');
4236  $mod->init($reloadmode);
4237  }
4238  } elseif ($moduletoreload == 'MAIN_MODULE_BARCODE') {
4239  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Barcode module");
4240  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modBarcode.class.php';
4241  if ($res) {
4242  $mod = new modBarcode($db);
4243  $mod->remove('noboxes');
4244  $mod->init($reloadmode);
4245  }
4246  } elseif ($moduletoreload == 'MAIN_MODULE_BLOCKEDLOG') {
4247  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate BlockedLog module");
4248  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modBlockedLog.class.php';
4249  if ($res) {
4250  $mod = new modBlockedLog($db);
4251  // For this module we only reload menus.
4252  $mod->delete_menus();
4253  $mod->insert_menus();
4254  }
4255  } elseif ($moduletoreload == 'MAIN_MODULE_CRON') {
4256  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Cron module");
4257  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modCron.class.php';
4258  if ($res) {
4259  $mod = new modCron($db);
4260  $mod->remove('noboxes');
4261  $mod->init($reloadmode);
4262  }
4263  } elseif ($moduletoreload == 'MAIN_MODULE_EXTERNALSITE') {
4264  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate ExternalSite module");
4265  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modExternalSite.class.php';
4266  if ($res) {
4267  $mod = new modExternalSite($db);
4268  $mod->remove('noboxes');
4269  $mod->init($reloadmode);
4270  }
4271  } elseif ($moduletoreload == 'MAIN_MODULE_SOCIETE') {
4272  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Societe module");
4273  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modSociete.class.php';
4274  if ($res) {
4275  $mod = new modSociete($db);
4276  $mod->remove('noboxes');
4277  $mod->init($reloadmode);
4278  }
4279  } elseif ($moduletoreload == 'MAIN_MODULE_PRODUIT') { // Permission has changed into 2.7
4280  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Produit module");
4281  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modProduct.class.php';
4282  if ($res) {
4283  $mod = new modProduct($db);
4284  //$mod->remove('noboxes');
4285  $mod->init($reloadmode);
4286  }
4287  } elseif ($moduletoreload == 'MAIN_MODULE_SERVICE') { // Permission has changed into 2.7
4288  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Service module");
4289  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modService.class.php';
4290  if ($res) {
4291  $mod = new modService($db);
4292  //$mod->remove('noboxes');
4293  $mod->init($reloadmode);
4294  }
4295  } elseif ($moduletoreload == 'MAIN_MODULE_COMMANDE') { // Permission has changed into 2.9
4296  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Commande module");
4297  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modCommande.class.php';
4298  if ($res) {
4299  $mod = new modCommande($db);
4300  //$mod->remove('noboxes');
4301  $mod->init($reloadmode);
4302  }
4303  } elseif ($moduletoreload == 'MAIN_MODULE_FACTURE') { // Permission has changed into 2.9
4304  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Facture module");
4305  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modFacture.class.php';
4306  if ($res) {
4307  $mod = new modFacture($db);
4308  //$mod->remove('noboxes');
4309  $mod->init($reloadmode);
4310  }
4311  } elseif ($moduletoreload == 'MAIN_MODULE_FOURNISSEUR') { // Permission has changed into 2.9
4312  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Fournisseur module");
4313  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modFournisseur.class.php';
4314  if ($res) {
4315  $mod = new modFournisseur($db);
4316  //$mod->remove('noboxes');
4317  $mod->init($reloadmode);
4318  }
4319  } elseif ($moduletoreload == 'MAIN_MODULE_HOLIDAY') { // Permission and tabs has changed into 3.8
4320  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Leave Request module");
4321  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modHoliday.class.php';
4322  if ($res) {
4323  $mod = new modHoliday($db);
4324  $mod->remove('noboxes');
4325  $mod->init($reloadmode);
4326  }
4327  } elseif ($moduletoreload == 'MAIN_MODULE_DEPLACEMENT') { // Permission has changed into 3.0
4328  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Deplacement module");
4329  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modDeplacement.class.php';
4330  if ($res) {
4331  $mod = new modDeplacement($db);
4332  //$mod->remove('noboxes');
4333  $mod->init($reloadmode);
4334  }
4335  } elseif ($moduletoreload == 'MAIN_MODULE_EXPENSEREPORT') {
4336  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Expense Report module");
4337  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modExpenseReport.class.php';
4338  if ($res) {
4339  $mod = new modExpenseReport($db);
4340  //$mod->remove('noboxes');
4341  $mod->init($reloadmode);
4342  }
4343  } elseif ($moduletoreload == 'MAIN_MODULE_DON') { // Permission has changed into 3.0
4344  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Don module");
4345  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modDon.class.php';
4346  if ($res) {
4347  $mod = new modDon($db);
4348  //$mod->remove('noboxes');
4349  $mod->init($reloadmode);
4350  }
4351  } elseif ($moduletoreload == 'MAIN_MODULE_ECM') { // Permission has changed into 3.0 and 3.1
4352  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate ECM module");
4353  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modECM.class.php';
4354  if ($res) {
4355  $mod = new modECM($db);
4356  $mod->remove('noboxes'); // We need to remove because a permission id has been removed
4357  $mod->init($reloadmode);
4358  }
4359  } elseif ($moduletoreload == 'MAIN_MODULE_KNOWLEDGEMANAGEMENT') { // Permission has changed into 3.0 and 3.1
4360  dolibarr_install_syslog("upgrade2::migrate_reload_modules Knowledge Management");
4361  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modKnowledgeManagement.class.php';
4362  if ($res) {
4363  $mod = new modKnowledgeManagement($db);
4364  $mod->remove('noboxes'); // We need to remove because a permission id has been removed
4365  $mod->init($reloadmode);
4366  }
4367  } elseif ($moduletoreload == 'MAIN_MODULE_EVENTORGANIZATION') { // Permission has changed into 3.0 and 3.1
4368  dolibarr_install_syslog("upgrade2::migrate_reload_modules EventOrganization");
4369  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modEventOrganization.class.php';
4370  if ($res) {
4371  $mod = new modEventOrganization($db);
4372  $mod->remove('noboxes'); // We need to remove because a permission id has been removed
4373  $mod->init($reloadmode);
4374  }
4375  } elseif ($moduletoreload == 'MAIN_MODULE_PAYBOX') { // Permission has changed into 3.0
4376  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Paybox module");
4377  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modPaybox.class.php';
4378  if ($res) {
4379  $mod = new modPaybox($db);
4380  $mod->remove('noboxes'); // We need to remove because id of module has changed
4381  $mod->init($reloadmode);
4382  }
4383  } elseif ($moduletoreload == 'MAIN_MODULE_SUPPLIERPROPOSAL') { // Module after 3.5
4384  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Supplier Proposal module");
4385  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modSupplierProposal.class.php';
4386  if ($res) {
4387  $mod = new modSupplierProposal($db);
4388  $mod->remove('noboxes'); // We need to remove because id of module has changed
4389  $mod->init($reloadmode);
4390  }
4391  } elseif ($moduletoreload == 'MAIN_MODULE_OPENSURVEY') { // Permission has changed into 3.0
4392  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Opensurvey module");
4393  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modOpenSurvey.class.php';
4394  if ($res) {
4395  $mod = new modOpenSurvey($db);
4396  $mod->remove('noboxes'); // We need to remove because menu entries has changed
4397  $mod->init($reloadmode);
4398  }
4399  } elseif ($moduletoreload == 'MAIN_MODULE_PRODUCTBATCH') { // Permission has changed into 10.0
4400  dolibarr_install_syslog("upgrade2::migrate_reload_modules ProductBatch module");
4401  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modProductBatch.class.php';
4402  if ($res) {
4403  $mod = new modProductBatch($db);
4404  $mod->remove('noboxes'); // We need to remove because menu entries has changed
4405  $mod->init($reloadmode);
4406  }
4407  } elseif ($moduletoreload == 'MAIN_MODULE_TAKEPOS') { // Permission has changed into 10.0
4408  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate Takepos module");
4409  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/modTakePos.class.php';
4410  if ($res) {
4411  $mod = new modTakePos($db);
4412  $mod->remove('noboxes'); // We need to remove because menu entries has changed
4413  $mod->init($reloadmode);
4414  }
4415  } else { // Other generic cases/modules
4416  $reg = array();
4417  $tmp = preg_match('/MAIN_MODULE_([a-zA-Z0-9]+)/', $moduletoreload, $reg);
4418  if (!empty($reg[1])) {
4419  if (strtoupper($moduletoreload) == $moduletoreload) { // If key is un uppercase
4420  $moduletoreloadshort = ucfirst(strtolower($reg[1]));
4421  } else // If key is a mix of up and low case
4422  {
4423  $moduletoreloadshort = $reg[1];
4424  }
4425 
4426  dolibarr_install_syslog("upgrade2::migrate_reload_modules Reactivate module ".$moduletoreloadshort." with mode ".$reloadmode);
4427  $res = @include_once DOL_DOCUMENT_ROOT.'/core/modules/mod'.$moduletoreloadshort.'.class.php';
4428  if ($res) {
4429  $classname = 'mod'.$moduletoreloadshort;
4430  $mod = new $classname($db);
4431 
4432  //$mod->remove('noboxes');
4433  $mod->delete_menus(); // We must delete to be sure it is inserted with new values
4434  $mod->init($reloadmode);
4435  } else {
4436  dolibarr_install_syslog('Failed to include '.DOL_DOCUMENT_ROOT.'/core/modules/mod'.$moduletoreloadshort.'.class.php');
4437 
4438  $res = @dol_include_once(strtolower($moduletoreloadshort).'/core/modules/mod'.$moduletoreloadshort.'.class.php');
4439  if ($res) {
4440  $classname = 'mod'.$moduletoreloadshort;
4441  $mod = new $classname($db);
4442  $mod->init($reloadmode);
4443  } else {
4444  dolibarr_install_syslog('Failed to include '.strtolower($moduletoreloadshort).'/core/modules/mod'.$moduletoreloadshort.'.class.php', LOG_ERR);
4445  print "Error, can't find module with name ".$moduletoreload."\n";
4446  return -1;
4447  }
4448  }
4449  } else {
4450  dolibarr_install_syslog("Error, can't find module with name ".$moduletoreload, LOG_ERR);
4451  print "Error, can't find module with name ".$moduletoreload."\n";
4452  return -1;
4453  }
4454  }
4455 
4456  if (!empty($mod) && is_object($mod)) {
4457  print '<tr class="trforrunsql"><td colspan="4">';
4458  print '<b>'.$langs->trans('Upgrade').'</b>: ';
4459  print $langs->trans('MigrationReloadModule').' '.$mod->getName(); // We keep getName outside of trans because getName is already encoded/translated
4460  print "<!-- (".$reloadmode.") -->";
4461  print "<br>\n";
4462  print '</td></tr>';
4463  }
4464  }
4465 
4466  return 1;
4467 }
4468 
4469 
4470 
4479 function migrate_reload_menu($db, $langs, $conf)
4480 {
4481  global $conf;
4482  dolibarr_install_syslog("upgrade2::migrate_reload_menu");
4483 
4484  // Define list of menu handlers to initialize
4485  $listofmenuhandler = array();
4486  if ($conf->global->MAIN_MENU_STANDARD == 'auguria_menu' || $conf->global->MAIN_MENU_SMARTPHONE == 'auguria_menu'
4487  || $conf->global->MAIN_MENUFRONT_STANDARD == 'auguria_menu' || $conf->global->MAIN_MENUFRONT_SMARTPHONE == 'auguria_menu') {
4488  $listofmenuhandler['auguria'] = 1; // We set here only dynamic menu handlers
4489  }
4490 
4491  foreach ($listofmenuhandler as $key => $val) {
4492  print '<tr class="trforrunsql"><td colspan="4">';
4493 
4494  //print "x".$key;
4495  print '<br>';
4496  print '<b>'.$langs->trans('Upgrade').'</b>: '.$langs->trans('MenuHandler')." ".$key."<br>\n";
4497 
4498  // Load sql ini_menu_handler.sql file
4499  $dir = DOL_DOCUMENT_ROOT."/core/menus/";
4500  $file = 'init_menu_'.$key.'.sql';
4501  if (file_exists($dir.$file)) {
4502  $result = run_sql($dir.$file, 1, '', 1, $key);
4503  }
4504 
4505  print '</td></tr>';
4506  }
4507 }
4508 
4515 {
4516  global $conf, $db, $langs, $user;
4517 
4518  print '<tr><td colspan="4">';
4519 
4520  print '<b>'.$langs->trans('MigrationUserPhotoPath')."</b><br>\n";
4521 
4522  include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
4523  $fuser = new User($db);
4524 
4525  if (!is_object($user)) {
4526  $user = $fuser; // To avoid error during migration
4527  }
4528 
4529  $sql = "SELECT rowid as uid from ".MAIN_DB_PREFIX."user"; // Get list of all users
4530  $resql = $db->query($sql);
4531  if ($resql) {
4532  while ($obj = $db->fetch_object($resql)) {
4533  $fuser->fetch($obj->uid);
4534  //echo '<hr>'.$fuser->id.' -> '.$fuser->entity;
4535  $entity = (empty($fuser->entity) ? 1 : $fuser->entity);
4536  if ($entity > 1) {
4537  $dir = DOL_DATA_ROOT.'/'.$entity.'/users';
4538  } else {
4539  $dir = $conf->user->multidir_output[$entity]; // $conf->user->multidir_output[] for each entity is construct by the multicompany module
4540  }
4541 
4542  if ($dir) {
4543  //print "Process user id ".$fuser->id."<br>\n";
4544  $origin = $dir.'/'.get_exdir($fuser->id, 2, 0, 1, $fuser, 'user'); // Use old behaviour to get x/y path
4545  $destin = $dir.'/'.$fuser->id;
4546 
4547  $origin_osencoded = dol_osencode($origin);
4548 
4549  dol_mkdir($destin);
4550 
4551  //echo '<hr>'.$origin.' -> '.$destin;
4552  if (dol_is_dir($origin)) {
4553  $handle = opendir($origin_osencoded);
4554  if (is_resource($handle)) {
4555  while (($file = readdir($handle)) !== false) {
4556  if ($file == '.' || $file == '..') {
4557  continue;
4558  }
4559 
4560  if (dol_is_dir($origin.'/'.$file)) { // it is a dir (like 'thumbs')
4561  $thumbs = opendir($origin_osencoded.'/'.$file);
4562  if (is_resource($thumbs)) {
4563  dol_mkdir($destin.'/'.$file);
4564  while (($thumb = readdir($thumbs)) !== false) {
4565  if (!dol_is_file($destin.'/'.$file.'/'.$thumb)) {
4566  if ($thumb == '.' || $thumb == '..') {
4567  continue;
4568  }
4569 
4570  //print $origin.'/'.$file.'/'.$thumb.' -> '.$destin.'/'.$file.'/'.$thumb.'<br>'."\n";
4571  print '.';
4572  dol_copy($origin.'/'.$file.'/'.$thumb, $destin.'/'.$file.'/'.$thumb, 0, 0);
4573  //var_dump('aaa');exit;
4574  }
4575  }
4576  // dol_delete_dir($origin.'/'.$file);
4577  }
4578  } else { // it is a file
4579  if (!dol_is_file($destin.'/'.$file)) {
4580  //print $origin.'/'.$file.' -> '.$destin.'/'.$file.'<br>'."\n";
4581  print '.';
4582  dol_copy($origin.'/'.$file, $destin.'/'.$file, 0, 0);
4583  //var_dump('eee');exit;
4584  }
4585  }
4586  }
4587  }
4588  }
4589  }
4590  }
4591  }
4592 
4593  print '</td></tr>';
4594 }
4595 
4602 {
4603  global $conf, $db, $langs, $user;
4604 
4605  print '<tr><td colspan="4">';
4606 
4607  print '<b>'.$langs->trans('MigrationUserPhotoPath')."</b><br>\n";
4608 
4609  include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
4610  $fuser = new User($db);
4611 
4612  if (!is_object($user)) {
4613  $user = $fuser; // To avoid error during migration
4614  }
4615 
4616  $sql = "SELECT rowid as uid from ".MAIN_DB_PREFIX."user"; // Get list of all users
4617  $resql = $db->query($sql);
4618  if ($resql) {
4619  while ($obj = $db->fetch_object($resql)) {
4620  $fuser->fetch($obj->uid);
4621  //echo '<hr>'.$fuser->id.' -> '.$fuser->entity;
4622  $entity = (empty($fuser->entity) ? 1 : $fuser->entity);
4623  if ($entity > 1) {
4624  $dir = DOL_DATA_ROOT.'/'.$entity.'/users';
4625  } else {
4626  $dir = DOL_DATA_ROOT.'/users';
4627  }
4628 
4629  if ($dir) {
4630  //print "Process user id ".$fuser->id."<br>\n";
4631  $origin = $dir.'/'.$fuser->id;
4632  $destin = $dir.'/'.$fuser->id.'/photos';
4633 
4634  $origin_osencoded = dol_osencode($origin);
4635 
4636  dol_mkdir($destin);
4637 
4638  //echo '<hr>'.$origin.' -> '.$destin;
4639  if (dol_is_dir($origin)) {
4640  $handle = opendir($origin_osencoded);
4641  if (is_resource($handle)) {
4642  while (($file = readdir($handle)) !== false) {
4643  if ($file == '.' || $file == '..' || $file == 'photos') {
4644  continue;
4645  }
4646  if (!empty($fuser->photo) && ($file != $fuser->photo && $file != 'thumbs')) {
4647  continue;
4648  }
4649 
4650  if (dol_is_dir($origin.'/'.$file)) { // it is a dir (like 'thumbs')
4651  $thumbs = opendir($origin_osencoded.'/'.$file);
4652  if (is_resource($thumbs)) {
4653  dol_mkdir($destin.'/'.$file);
4654  while (($thumb = readdir($thumbs)) !== false) {
4655  if (!dol_is_file($destin.'/'.$file.'/'.$thumb)) {
4656  if ($thumb == '.' || $thumb == '..') {
4657  continue;
4658  }
4659 
4660  //print $origin.'/'.$file.'/'.$thumb.' -> '.$destin.'/'.$file.'/'.$thumb.'<br>'."\n";
4661  print '.';
4662  dol_copy($origin.'/'.$file.'/'.$thumb, $destin.'/'.$file.'/'.$thumb, 0, 0);
4663  }
4664  }
4665  // dol_delete_dir($origin.'/'.$file);
4666  }
4667  } else { // it is a file
4668  if (!dol_is_file($destin.'/'.$file)) {
4669  //print $origin.'/'.$file.' -> '.$destin.'/'.$file.'<br>'."\n";
4670  print '.';
4671  dol_copy($origin.'/'.$file, $destin.'/'.$file, 0, 0);
4672  }
4673  }
4674  }
4675  }
4676  }
4677  }
4678  }
4679  }
4680 
4681  print '</td></tr>';
4682 }
4683 
4684 
4685 /* A faire egalement: Modif statut paye et fk_facture des factures payes completement
4686 
4687 On recherche facture incorrecte:
4688 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
4689 having f.total_ttc = sum(pf.amount)
4690 
4691 On les corrige:
4692 update llx_facture set paye=1, fk_statut=2 where close_code is null
4693 and rowid in (...)
4694 */
4695 
4703 {
4704  global $db, $langs;
4705  // skype,twitter,facebook,linkedin,instagram,snapchat,googleplus,youtube,whatsapp
4706  $error = 0;
4707  $db->begin();
4708  print '<tr><td colspan="4">';
4709  $sql = 'SELECT rowid, socialnetworks';
4710  $sql .= ', skype, twitter, facebook, linkedin, instagram, snapchat, googleplus, youtube, whatsapp FROM '.MAIN_DB_PREFIX.'user WHERE';
4711  $sql .= " skype IS NOT NULL OR skype <> ''";
4712  $sql .= " OR twitter IS NOT NULL OR twitter <> ''";
4713  $sql .= " OR facebook IS NOT NULL OR facebook <> ''";
4714  $sql .= " OR linkedin IS NOT NULL OR linkedin <> ''";
4715  $sql .= " OR instagram IS NOT NULL OR instagram <> ''";
4716  $sql .= " OR snapchat IS NOT NULL OR snapchat <> ''";
4717  $sql .= " OR googleplus IS NOT NULL OR googleplus <> ''";
4718  $sql .= " OR youtube IS NOT NULL OR youtube <> ''";
4719  $sql .= " OR whatsapp IS NOT NULL OR whatsapp <> ''";
4720  //print $sql;
4721  $resql = $db->query($sql);
4722  if ($resql) {
4723  while ($obj = $db->fetch_object($resql)) {
4724  $arraysocialnetworks = array();
4725  if (!empty($obj->skype)) {
4726  $arraysocialnetworks['skype'] = $obj->skype;
4727  }
4728  if (!empty($obj->twitter)) {
4729  $arraysocialnetworks['twitter'] = $obj->twitter;
4730  }
4731  if (!empty($obj->facebook)) {
4732  $arraysocialnetworks['facebook'] = $obj->facebook;
4733  }
4734  if (!empty($obj->linkedin)) {
4735  $arraysocialnetworks['linkedin'] = $obj->linkedin;
4736  }
4737  if (!empty($obj->instagram)) {
4738  $arraysocialnetworks['instagram'] = $obj->instagram;
4739  }
4740  if (!empty($obj->snapchat)) {
4741  $arraysocialnetworks['snapchat'] = $obj->snapchat;
4742  }
4743  if (!empty($obj->googleplus)) {
4744  $arraysocialnetworks['googleplus'] = $obj->googleplus;
4745  }
4746  if (!empty($obj->youtube)) {
4747  $arraysocialnetworks['youtube'] = $obj->youtube;
4748  }
4749  if (!empty($obj->whatsapp)) {
4750  $arraysocialnetworks['whatsapp'] = $obj->whatsapp;
4751  }
4752  if ($obj->socialnetworks == '' || is_null($obj->socialnetworks)) {
4753  $obj->socialnetworks = '[]';
4754  }
4755  $socialnetworks = array_merge($arraysocialnetworks, json_decode($obj->socialnetworks, true));
4756  $sqlupd = 'UPDATE '.MAIN_DB_PREFIX."user SET socialnetworks='".$db->escape(json_encode($socialnetworks, true))."'";
4757  $sqlupd .= ', skype=null';
4758  $sqlupd .= ', twitter=null';
4759  $sqlupd .= ', facebook=null';
4760  $sqlupd .= ', linkedin=null';
4761  $sqlupd .= ', instagram=null';
4762  $sqlupd .= ', snapchat=null';
4763  $sqlupd .= ', googleplus=null';
4764  $sqlupd .= ', youtube=null';
4765  $sqlupd .= ', whatsapp=null';
4766  $sqlupd .= ' WHERE rowid = '.((int) $obj->rowid);
4767  //print $sqlupd."<br>";
4768  $resqlupd = $db->query($sqlupd);
4769  if (!$resqlupd) {
4770  dol_print_error($db);
4771  $error++;
4772  }
4773  }
4774  } else {
4775  $error++;
4776  }
4777  if (!$error) {
4778  $db->commit();
4779  } else {
4780  dol_print_error($db);
4781  $db->rollback();
4782  }
4783  print '<b>'.$langs->trans('MigrationFieldsSocialNetworks', 'Users')."</b><br>\n";
4784  print '</td></tr>';
4785 }
4786 
4794 {
4795  global $db, $langs;
4796 
4797  print '<tr><td colspan="4">';
4798  $error = 0;
4799  $db->begin();
4800  print '<tr><td colspan="4">';
4801  $sql = 'SELECT rowid, socialnetworks';
4802  $sql .= ', skype, twitter, facebook, linkedin, instagram, snapchat, googleplus, youtube, whatsapp FROM '.MAIN_DB_PREFIX.'adherent WHERE ';
4803  $sql .= " skype IS NOT NULL OR skype <> ''";
4804  $sql .= " OR twitter IS NOT NULL OR twitter <> ''";
4805  $sql .= " OR facebook IS NOT NULL OR facebook <> ''";
4806  $sql .= " OR linkedin IS NOT NULL OR linkedin <> ''";
4807  $sql .= " OR instagram IS NOT NULL OR instagram <> ''";
4808  $sql .= " OR snapchat IS NOT NULL OR snapchat <> ''";
4809  $sql .= " OR googleplus IS NOT NULL OR googleplus <> ''";
4810  $sql .= " OR youtube IS NOT NULL OR youtube <> ''";
4811  $sql .= " OR whatsapp IS NOT NULL OR whatsapp <> ''";
4812  //print $sql;
4813  $resql = $db->query($sql);
4814  if ($resql) {
4815  while ($obj = $db->fetch_object($resql)) {
4816  $arraysocialnetworks = array();
4817  if (!empty($obj->skype)) {
4818  $arraysocialnetworks['skype'] = $obj->skype;
4819  }
4820  if (!empty($obj->twitter)) {
4821  $arraysocialnetworks['twitter'] = $obj->twitter;
4822  }
4823  if (!empty($obj->facebook)) {
4824  $arraysocialnetworks['facebook'] = $obj->facebook;
4825  }
4826  if (!empty($obj->linkedin)) {
4827  $arraysocialnetworks['linkedin'] = $obj->linkedin;
4828  }
4829  if (!empty($obj->instagram)) {
4830  $arraysocialnetworks['instagram'] = $obj->instagram;
4831  }
4832  if (!empty($obj->snapchat)) {
4833  $arraysocialnetworks['snapchat'] = $obj->snapchat;
4834  }
4835  if (!empty($obj->googleplus)) {
4836  $arraysocialnetworks['googleplus'] = $obj->googleplus;
4837  }
4838  if (!empty($obj->youtube)) {
4839  $arraysocialnetworks['youtube'] = $obj->youtube;
4840  }
4841  if (!empty($obj->whatsapp)) {
4842  $arraysocialnetworks['whatsapp'] = $obj->whatsapp;
4843  }
4844  if ($obj->socialnetworks == '' || is_null($obj->socialnetworks)) {
4845  $obj->socialnetworks = '[]';
4846  }
4847  $socialnetworks = array_merge($arraysocialnetworks, json_decode($obj->socialnetworks, true));
4848  $sqlupd = 'UPDATE '.MAIN_DB_PREFIX."adherent SET socialnetworks='".$db->escape(json_encode($socialnetworks, true))."'";
4849  $sqlupd .= ', skype=null';
4850  $sqlupd .= ', twitter=null';
4851  $sqlupd .= ', facebook=null';
4852  $sqlupd .= ', linkedin=null';
4853  $sqlupd .= ', instagram=null';
4854  $sqlupd .= ', snapchat=null';
4855  $sqlupd .= ', googleplus=null';
4856  $sqlupd .= ', youtube=null';
4857  $sqlupd .= ', whatsapp=null';
4858  $sqlupd .= ' WHERE rowid = '.((int) $obj->rowid);
4859  //print $sqlupd."<br>";
4860  $resqlupd = $db->query($sqlupd);
4861  if (!$resqlupd) {
4862  dol_print_error($db);
4863  $error++;
4864  }
4865  }
4866  } else {
4867  $error++;
4868  }
4869  if (!$error) {
4870  $db->commit();
4871  } else {
4872  dol_print_error($db);
4873  $db->rollback();
4874  }
4875  print '<b>'.$langs->trans('MigrationFieldsSocialNetworks', 'Members')."</b><br>\n";
4876  print '</td></tr>';
4877 }
4878 
4886 {
4887  global $db, $langs;
4888  // jabberid,skype,twitter,facebook,linkedin,instagram,snapchat,googleplus,youtube,whatsapp
4889  $error = 0;
4890  $db->begin();
4891  print '<tr><td colspan="4">';
4892  $sql = 'SELECT rowid, socialnetworks';
4893  $sql .= ', jabberid, skype, twitter, facebook, linkedin, instagram, snapchat, googleplus, youtube, whatsapp FROM '.MAIN_DB_PREFIX.'socpeople WHERE';
4894  $sql .= " jabberid IS NOT NULL OR jabberid <> ''";
4895  $sql .= " OR skype IS NOT NULL OR skype <> ''";
4896  $sql .= " OR twitter IS NOT NULL OR twitter <> ''";
4897  $sql .= " OR facebook IS NOT NULL OR facebook <> ''";
4898  $sql .= " OR linkedin IS NOT NULL OR linkedin <> ''";
4899  $sql .= " OR instagram IS NOT NULL OR instagram <> ''";
4900  $sql .= " OR snapchat IS NOT NULL OR snapchat <> ''";
4901  $sql .= " OR googleplus IS NOT NULL OR googleplus <> ''";
4902  $sql .= " OR youtube IS NOT NULL OR youtube <> ''";
4903  $sql .= " OR whatsapp IS NOT NULL OR whatsapp <> ''";
4904  //print $sql;
4905  $resql = $db->query($sql);
4906  if ($resql) {
4907  while ($obj = $db->fetch_object($resql)) {
4908  $arraysocialnetworks = array();
4909  if (!empty($obj->jabberid)) {
4910  $arraysocialnetworks['jabber'] = $obj->jabberid;
4911  }
4912  if (!empty($obj->skype)) {
4913  $arraysocialnetworks['skype'] = $obj->skype;
4914  }
4915  if (!empty($obj->twitter)) {
4916  $arraysocialnetworks['twitter'] = $obj->twitter;
4917  }
4918  if (!empty($obj->facebook)) {
4919  $arraysocialnetworks['facebook'] = $obj->facebook;
4920  }
4921  if (!empty($obj->linkedin)) {
4922  $arraysocialnetworks['linkedin'] = $obj->linkedin;
4923  }
4924  if (!empty($obj->instagram)) {
4925  $arraysocialnetworks['instagram'] = $obj->instagram;
4926  }
4927  if (!empty($obj->snapchat)) {
4928  $arraysocialnetworks['snapchat'] = $obj->snapchat;
4929  }
4930  if (!empty($obj->googleplus)) {
4931  $arraysocialnetworks['googleplus'] = $obj->googleplus;
4932  }
4933  if (!empty($obj->youtube)) {
4934  $arraysocialnetworks['youtube'] = $obj->youtube;
4935  }
4936  if (!empty($obj->whatsapp)) {
4937  $arraysocialnetworks['whatsapp'] = $obj->whatsapp;
4938  }
4939  if ($obj->socialnetworks == '' || is_null($obj->socialnetworks)) {
4940  $obj->socialnetworks = '[]';
4941  }
4942  $socialnetworks = array_merge($arraysocialnetworks, json_decode($obj->socialnetworks, true));
4943  $sqlupd = 'UPDATE '.MAIN_DB_PREFIX."socpeople SET socialnetworks='".$db->escape(json_encode($socialnetworks, true))."'";
4944  $sqlupd .= ', jabberid=null';
4945  $sqlupd .= ', skype=null';
4946  $sqlupd .= ', twitter=null';
4947  $sqlupd .= ', facebook=null';
4948  $sqlupd .= ', linkedin=null';
4949  $sqlupd .= ', instagram=null';
4950  $sqlupd .= ', snapchat=null';
4951  $sqlupd .= ', googleplus=null';
4952  $sqlupd .= ', youtube=null';
4953  $sqlupd .= ', whatsapp=null';
4954  $sqlupd .= ' WHERE rowid = '.((int) $obj->rowid);
4955  //print $sqlupd."<br>";
4956  $resqlupd = $db->query($sqlupd);
4957  if (!$resqlupd) {
4958  dol_print_error($db);
4959  $error++;
4960  }
4961  }
4962  } else {
4963  $error++;
4964  }
4965  if (!$error) {
4966  $db->commit();
4967  } else {
4968  dol_print_error($db);
4969  $db->rollback();
4970  }
4971  print '<b>'.$langs->trans('MigrationFieldsSocialNetworks', 'Contacts')."</b><br>\n";
4972  print '</td></tr>';
4973 }
4974 
4982 {
4983  global $db, $langs;
4984  // skype,twitter,facebook,linkedin,instagram,snapchat,googleplus,youtube,whatsapp
4985  $error = 0;
4986  $db->begin();
4987  print '<tr><td colspan="4">';
4988  $sql = 'SELECT rowid, socialnetworks';
4989  $sql .= ', skype, twitter, facebook, linkedin, instagram, snapchat, googleplus, youtube, whatsapp FROM '.MAIN_DB_PREFIX.'societe WHERE ';
4990  $sql .= " skype IS NOT NULL OR skype <> ''";
4991  $sql .= " OR twitter IS NOT NULL OR twitter <> ''";
4992  $sql .= " OR facebook IS NOT NULL OR facebook <> ''";
4993  $sql .= " OR linkedin IS NOT NULL OR linkedin <> ''";
4994  $sql .= " OR instagram IS NOT NULL OR instagram <> ''";
4995  $sql .= " OR snapchat IS NOT NULL OR snapchat <> ''";
4996  $sql .= " OR googleplus IS NOT NULL OR googleplus <> ''";
4997  $sql .= " OR youtube IS NOT NULL OR youtube <> ''";
4998  $sql .= " OR whatsapp IS NOT NULL OR whatsapp <> ''";
4999  //print $sql;
5000  $resql = $db->query($sql);
5001  if ($resql) {
5002  while ($obj = $db->fetch_object($resql)) {
5003  $arraysocialnetworks = array();
5004  if (!empty($obj->skype)) {
5005  $arraysocialnetworks['skype'] = $obj->skype;
5006  }
5007  if (!empty($obj->twitter)) {
5008  $arraysocialnetworks['twitter'] = $obj->twitter;
5009  }
5010  if (!empty($obj->facebook)) {
5011  $arraysocialnetworks['facebook'] = $obj->facebook;
5012  }
5013  if (!empty($obj->linkedin)) {
5014  $arraysocialnetworks['linkedin'] = $obj->linkedin;
5015  }
5016  if (!empty($obj->instagram)) {
5017  $arraysocialnetworks['instagram'] = $obj->instagram;
5018  }
5019  if (!empty($obj->snapchat)) {
5020  $arraysocialnetworks['snapchat'] = $obj->snapchat;
5021  }
5022  if (!empty($obj->googleplus)) {
5023  $arraysocialnetworks['googleplus'] = $obj->googleplus;
5024  }
5025  if (!empty($obj->youtube)) {
5026  $arraysocialnetworks['youtube'] = $obj->youtube;
5027  }
5028  if (!empty($obj->whatsapp)) {
5029  $arraysocialnetworks['whatsapp'] = $obj->whatsapp;
5030  }
5031  if ($obj->socialnetworks == '' || is_null($obj->socialnetworks)) {
5032  $obj->socialnetworks = '[]';
5033  }
5034  $socialnetworks = array_merge($arraysocialnetworks, json_decode($obj->socialnetworks, true));
5035  $sqlupd = 'UPDATE '.MAIN_DB_PREFIX."societe SET socialnetworks='".$db->escape(json_encode($socialnetworks, true))."'";
5036  $sqlupd .= ', skype=null';
5037  $sqlupd .= ', twitter=null';
5038  $sqlupd .= ', facebook=null';
5039  $sqlupd .= ', linkedin=null';
5040  $sqlupd .= ', instagram=null';
5041  $sqlupd .= ', snapchat=null';
5042  $sqlupd .= ', googleplus=null';
5043  $sqlupd .= ', youtube=null';
5044  $sqlupd .= ', whatsapp=null';
5045  $sqlupd .= ' WHERE rowid = '.((int) $obj->rowid);
5046  //print $sqlupd."<br>";
5047  $resqlupd = $db->query($sqlupd);
5048  if (!$resqlupd) {
5049  dol_print_error($db);
5050  $error++;
5051  }
5052  }
5053  } else {
5054  $error++;
5055  }
5056  if (!$error) {
5057  $db->commit();
5058  } else {
5059  dol_print_error($db);
5060  $db->rollback();
5061  }
5062  print '<b>'.$langs->trans('MigrationFieldsSocialNetworks', 'Thirdparties')."</b><br>\n";
5063  print '</td></tr>';
5064 }
5065 
5066 
5073 function migrate_export_import_profiles($mode = 'export')
5074 {
5075  global $db, $langs;
5076 
5077  $error = 0;
5078  $resultstring = '';
5079 
5080  $db->begin();
5081 
5082  print '<tr class="trforrunsql"><td colspan="4">';
5083  $sql = 'SELECT rowid, field';
5084  if ($mode == 'export') {
5085  $sql .= ', filter';
5086  }
5087  $sql .= ' FROM '.MAIN_DB_PREFIX.$mode.'_model WHERE';
5088  $sql .= " type LIKE 'propale_%' OR type LIKE 'commande_%' OR type LIKE 'facture_%'";
5089  //print $sql;
5090  $resql = $db->query($sql);
5091  if ($resql) {
5092  while ($obj = $db->fetch_object($resql)) {
5093  $oldfield = $obj->field;
5094  $newfield = str_replace(array(',f.facnumber', 'f.facnumber,', 'f.total,', 'f.tva,'), array(',f.ref', 'f.ref,', 'f.total_ht,', 'f.total_tva,'), $oldfield);
5095 
5096  if ($mode == 'export') {
5097  $oldfilter = $obj->filter;
5098  $newfilter = str_replace(array('f.facnumber=', 'f.total=', 'f.tva='), array('f.ref=', 'f.total_ht=', 'f.total_tva='), $oldfilter);
5099  } else {
5100  $oldfilter = '';
5101  $newfilter = '';
5102  }
5103 
5104  if ($oldfield != $newfield || $oldfilter != $newfilter) {
5105  $sqlupd = 'UPDATE '.MAIN_DB_PREFIX.$mode."_model SET field = '".$db->escape($newfield)."'";
5106  if ($mode == 'export') {
5107  $sqlupd .= ", filter = '".$db->escape($newfilter)."'";
5108  }
5109  $sqlupd .= ' WHERE rowid = '.((int) $obj->rowid);
5110  $resultstring .= '<tr class="trforrunsql" style=""><td class="wordbreak" colspan="4">'.$sqlupd."</td></tr>\n";
5111  $resqlupd = $db->query($sqlupd);
5112  if (!$resqlupd) {
5113  dol_print_error($db);
5114  $error++;
5115  }
5116  }
5117  }
5118  } else {
5119  $error++;
5120  }
5121  if (!$error) {
5122  $db->commit();
5123  } else {
5124  dol_print_error($db);
5125  $db->rollback();
5126  }
5127  print '<b>'.$langs->trans('MigrationImportOrExportProfiles', $mode)."</b><br>\n";
5128  print '</td></tr>';
5129 
5130  if ($resultstring) {
5131  print $resultstring;
5132  } else {
5133  print '<tr class="trforrunsql" style=""><td class="wordbreak" colspan="4">'.$langs->trans("NothingToDo")."</td></tr>\n";
5134  }
5135 }
modBarcode
Class to describe Barcode.
Definition: modBarcode.class.php:34
modHoliday
Description and activation class for module holiday.
Definition: modHoliday.class.php:37
modProduct
Class descriptor of Product module.
Definition: modProduct.class.php:39
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
migrate_paiementfourn_facturefourn
migrate_paiementfourn_facturefourn($db, $langs, $conf)
Factures fournisseurs.
Definition: upgrade2.php:1401
modExternalSite
Description and activation class for module ExternalSite.
Definition: modExternalSite.class.php:34
migrate_delete_old_dir
migrate_delete_old_dir($db, $langs, $conf)
Remove deprecated directories.
Definition: upgrade2.php:4163
migrate_modeles
migrate_modeles($db, $langs, $conf)
Mise a jour des modeles selectionnes.
Definition: upgrade2.php:2007
modBlockedLog
Class to describe a BlockedLog module.
Definition: modBlockedLog.class.php:30
versioncompare
versioncompare($versionarray1, $versionarray2)
Compare 2 versions (stored into 2 arrays).
Definition: admin.lib.php:66
migrate_customerorder_shipping
migrate_customerorder_shipping($db, $langs, $conf)
Migrate order ref_customer and date_delivery fields to llx_expedition.
Definition: upgrade2.php:2993
dol_delete_dir_recursive
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:1383
dol_osencode
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
Definition: functions.lib.php:8499
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
migrate_remise_entity
migrate_remise_entity($db, $langs, $conf)
Migrate to add entity value into llx_societe_remise.
Definition: upgrade2.php:3767
PropaleLigne
Class to manage commercial proposal lines.
Definition: propal.class.php:3878
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
modEventOrganization
Description and activation class for module EventOrganization This module is base on this specificati...
Definition: modEventOrganization.class.php:34
migrate_project_task_actors
migrate_project_task_actors($db, $langs, $conf)
Migration de la table llx_projet_task_actors vers llx_element_contact.
Definition: upgrade2.php:2727
dol_include_once
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
Definition: functions.lib.php:1033
migrate_mode_reglement
migrate_mode_reglement($db, $langs, $conf)
Migrate link stored into fk_mode_reglement.
Definition: upgrade2.php:3304
modFacture
Class to describe module customer invoices.
Definition: modFacture.class.php:37
ModelePDFFactures\liste_modeles
static liste_modeles($db, $maxfilenamelength=0)
Return list of active generation modules.
Definition: modules_facture.php:61
migrate_price_facture
migrate_price_facture($db, $langs, $conf)
Update total of invoice lines.
Definition: upgrade2.php:1497
migrate_shipping_delivery
migrate_shipping_delivery($db, $langs, $conf)
Migrate link stored into fk_expedition into llx_element_element.
Definition: upgrade2.php:3077
migrate_event_assignement_contact
migrate_event_assignement_contact($db, $langs, $conf)
Migrate event assignement to owner.
Definition: upgrade2.php:3601
migrate_detail_livraison
migrate_detail_livraison($db, $langs, $conf)
Migration des details commandes dans les details livraisons.
Definition: upgrade2.php:2210
migrate_price_commande_fournisseur
migrate_price_commande_fournisseur($db, $langs, $conf)
Update total of purchase order lines.
Definition: upgrade2.php:1897
migrate_contracts_date2
migrate_contracts_date2($db, $langs, $conf)
Update contracts with date min real if service date is lower.
Definition: upgrade2.php:1240
migrate_thirdparties_socialnetworks
migrate_thirdparties_socialnetworks()
Migrate thirdparties fields facebook and co to socialnetworks Can be called only when version is 10....
Definition: upgrade2.php:4981
Facture
Class to manage invoices.
Definition: facture.class.php:60
migrate_reload_modules
migrate_reload_modules($db, $langs, $conf, $listofmodule=array(), $force=0)
Disable/Reenable features modules.
Definition: upgrade2.php:4207
modService
Class to describe and enable module Service.
Definition: modService.class.php:36
getDoliDBInstance
getDoliDBInstance($type, $host, $user, $pass, $name, $port)
Return a DoliDB instance (database handler).
Definition: functions.lib.php:122
dol_is_file
dol_is_file($pathoffile)
Return if path is a file.
Definition: files.lib.php:477
migrate_price_commande
migrate_price_commande($db, $langs, $conf)
Update total of sales order lines.
Definition: upgrade2.php:1787
migrate_user_photospath
migrate_user_photospath()
Migrate file from old path to new one for users.
Definition: upgrade2.php:4514
migrate_commande_expedition
migrate_commande_expedition($db, $langs, $conf)
Correspondance des expeditions et des commandes clients dans la table llx_co_exp.
Definition: upgrade2.php:2065
migrate_remise_except_entity
migrate_remise_except_entity($db, $langs, $conf)
Migrate to add entity value into llx_societe_remise_except.
Definition: upgrade2.php:3833
migrate_contracts_open
migrate_contracts_open($db, $langs, $conf)
Reouverture des contrats qui ont au moins une ligne non fermee.
Definition: upgrade2.php:1338
migrate_relationship_tables
migrate_relationship_tables($db, $langs, $conf, $table, $fk_source, $sourcetype, $fk_target, $targettype)
Migration des tables de relation.
Definition: upgrade2.php:2809
migrate_paiements
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:726
calcul_price_total
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_delete_file
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:1231
FactureLigne
Class to manage invoice lines.
Definition: facture.class.php:5742
migrate_paiements_orphelins_1
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:805
get_exdir
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
Definition: functions.lib.php:6549
migrate_commande_livraison
migrate_commande_livraison($db, $langs, $conf)
Correspondance des livraisons et des commandes clients dans la table llx_co_liv.
Definition: upgrade2.php:2130
BlockedLog
Class to manage Blocked Log.
Definition: blockedlog.class.php:26
dol_copy
dol_copy($srcfile, $destfile, $newmask=0, $overwriteifexists=1)
Copy a file to another file.
Definition: files.lib.php:703
dolibarr_install_syslog
dolibarr_install_syslog($message, $level=LOG_DEBUG)
Log function for install pages.
Definition: inc.php:559
OrderLine
Class to manage order lines.
Definition: commande.class.php:4146
migrate_reload_menu
migrate_reload_menu($db, $langs, $conf)
Reload SQL menu file (if dynamic menus, if modified by version)
Definition: upgrade2.php:4479
migrate_rename_directories
migrate_rename_directories($db, $langs, $conf, $oldname, $newname)
Migration directory.
Definition: upgrade2.php:4057
dolCopyDir
dolCopyDir($srcfile, $destfile, $newmask, $overwriteifexists, $arrayreplacement=null, $excludesubdir=0)
Copy a dir to another dir.
Definition: files.lib.php:762
modProductBatch
Description and activation class for module productdluo.
Definition: modProductBatch.class.php:34
modSociete
Class to describe and enable module Societe.
Definition: modSociete.class.php:37
modKnowledgeManagement
Description and activation class for module KnowledgeManagement.
Definition: modKnowledgeManagement.class.php:34
migrate_contacts_socialnetworks
migrate_contacts_socialnetworks()
Migrate contacts fields facebook and co to socialnetworks Can be called only when version is 10....
Definition: upgrade2.php:4885
migrate_project_user_resp
migrate_project_user_resp($db, $langs, $conf)
Migration du champ fk_user_resp de llx_projet vers llx_element_contact.
Definition: upgrade2.php:2646
migrate_shipping_delivery2
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:3172
migrate_project_task_time
migrate_project_task_time($db, $langs, $conf)
Migrate duration in seconds.
Definition: upgrade2.php:2888
modApi
Description and activation class for module Api.
Definition: modApi.class.php:34
modAgenda
Class to describe and enable/disable module Agenda.
Definition: modAgenda.class.php:38
migrate_export_import_profiles
migrate_export_import_profiles($mode='export')
Migrate export and import profiles to fix field name that was renamed.
Definition: upgrade2.php:5073
modExpenseReport
Description and activation class for module ExpenseReport.
Definition: modExpenseReport.class.php:32
modCommande
Class to describe module customer orders.
Definition: modCommande.class.php:39
migrate_user_rights_entity
migrate_user_rights_entity($db, $langs, $conf)
Migrate to add entity value into llx_user_rights.
Definition: upgrade2.php:3925
pHeader
pHeader($subtitle, $next, $action='set', $param='', $forcejqueryurl='', $csstable='main-inside')
Show HTML header of install pages.
Definition: inc.php:409
pFooter
pFooter($nonext=0, $setuplang='', $jscheckfunction='', $withpleasewait=0, $morehtml='')
Print HTML footer of install pages.
Definition: inc.php:498
migrate_delete_old_files
migrate_delete_old_files($db, $langs, $conf)
Delete deprecated files.
Definition: upgrade2.php:4076
migrate_price_contrat
migrate_price_contrat($db, $langs, $conf)
Update total of contract lines.
Definition: upgrade2.php:1703
modDon
Class to describe and enable module Donation.
Definition: modDon.class.php:35
migrate_users_socialnetworks
migrate_users_socialnetworks()
Migrate users fields facebook and co to socialnetworks.
Definition: upgrade2.php:4702
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:105
User
Class to manage Dolibarr users.
Definition: user.class.php:44
migrate_categorie_association
migrate_categorie_association($db, $langs, $conf)
Migrate categorie association.
Definition: upgrade2.php:3469
migrate_links_transfert
migrate_links_transfert($db, $langs, $conf)
Function to migrate links into llx_bank_url.
Definition: upgrade2.php:1124
migrate_price_propal
migrate_price_propal($db, $langs, $conf)
Update total of proposal lines.
Definition: upgrade2.php:1602
ContratLigne
Class to manage lines of contracts.
Definition: contrat.class.php:2607
migrate_user_photospath2
migrate_user_photospath2()
Migrate file from old path users/99/file.jpg into users/99/photos/file.jpg.
Definition: upgrade2.php:4601
migrate_paiements_orphelins_2
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:915
migrate_members_socialnetworks
migrate_members_socialnetworks()
Migrate members fields facebook and co to socialnetworks Can be called only when version is 10....
Definition: upgrade2.php:4793
modDeplacement
Class to describe and enable module Deplacement.
Definition: modDeplacement.class.php:32
migrate_actioncomm_element
migrate_actioncomm_element($db, $langs, $conf)
Migrate link stored into fk_xxxx into fk_element and elementtype.
Definition: upgrade2.php:3245
migrate_reset_blocked_log
migrate_reset_blocked_log($db, $langs, $conf)
Migrate to reset the blocked log for V7+ algorithm.
Definition: upgrade2.php:3668
run_sql
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
modCron
Class to describe a Cron module.
Definition: modCron.class.php:33
migrate_stocks
migrate_stocks($db, $langs, $conf)
Migration du champ stock dans produits.
Definition: upgrade2.php:2312
modFournisseur
Description and activation class for module Fournisseur.
Definition: modFournisseur.class.php:35
migrate_contracts_det
migrate_contracts_det($db, $langs, $conf)
Mise a jour des contrats (gestion du contrat + detail de contrat)
Definition: upgrade2.php:1039
migrate_event_assignement
migrate_event_assignement($db, $langs, $conf)
Migrate event assignement to owner.
Definition: upgrade2.php:3535
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->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->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
migrate_contracts_date3
migrate_contracts_date3($db, $langs, $conf)
Mise a jour des dates de creation de contrat.
Definition: upgrade2.php:1308
dol_is_dir
dol_is_dir($folder)
Test if filename is a directory.
Definition: files.lib.php:447
modSupplierProposal
Class to describe and enable module SupplierProposal.
Definition: modSupplierProposal.class.php:37
modTakePos
Class to describe and enable module TakePos.
Definition: modTakePos.class.php:33
modOpenSurvey
Description and activation class for module opensurvey.
Definition: modOpenSurvey.class.php:32
dol_mkdir
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
Definition: functions.lib.php:6603
migrate_contracts_date1
migrate_contracts_date1($db, $langs, $conf)
Mise a jour des date de contrats non renseignees.
Definition: upgrade2.php:1198
ModelePDFCommandes\liste_modeles
static liste_modeles($db, $maxfilenamelength=0)
Return list of active generation modules.
Definition: modules_commande.php:51
migrate_clean_association
migrate_clean_association($db, $langs, $conf)
Delete duplicates in table categorie_association.
Definition: upgrade2.php:3394
migrate_menus
migrate_menus($db, $langs, $conf)
Migration of menus (use only 1 table instead of 3) 2.6 -> 2.7.
Definition: upgrade2.php:2374
modECM
Description and activation class for module ECM.
Definition: modECM.class.php:32
CommandeFournisseurLigne
Class to manage line orders.
Definition: fournisseur.commande.class.php:3550
migrate_commande_deliveryaddress
migrate_commande_deliveryaddress($db, $langs, $conf)
Migration du champ fk_adresse_livraison dans expedition 2.6 -> 2.7.
Definition: upgrade2.php:2441
HookManager
Class to manage hooks.
Definition: hookmanager.class.php:30
migrate_usergroup_rights_entity
migrate_usergroup_rights_entity($db, $langs, $conf)
Migrate to add entity value into llx_usergroup_rights.
Definition: upgrade2.php:3990
migrate_restore_missing_links
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:2511
float
div float
Buy price without taxes.
Definition: style.css.php:809
dol_decode
dol_decode($chain, $key='1')
Decode a base 64 encoded + specific delta change.
Definition: security.lib.php:69