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