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