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