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