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