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