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