/
upgrade2.php
5072 lines (4322 loc) · 151 KB
/
upgrade2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
* Copyright (C) 2005-2018 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2011 Regis Houssin <regis.houssin@inodbox.com>
* Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2015-2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Upgrade2 scripts can be ran from command line with syntax:
*
* cd htdocs/install
* php upgrade.php 3.4.0 3.5.0 [dirmodule|ignoredbversion]
* php upgrade2.php 3.4.0 3.5.0 [MAIN_MODULE_NAME1_TO_ENABLE,MAIN_MODULE_NAME2_TO_ENABLE]
*
* And for final step:
* php step5.php 3.4.0 3.5.0
*
* Return code is 0 if OK, >0 if error
*
* Note: To just enable a module from command line, use this syntax:
* php upgrade2.php 0.0.0 0.0.0 [MAIN_MODULE_NAME1_TO_ENABLE,MAIN_MODULE_NAME2_TO_ENABLE]
*/
/**
* \file htdocs/install/upgrade2.php
* \brief Upgrade some data
*/
include_once 'inc.php';
if (!file_exists($conffile)) {
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").';
}
require_once $conffile;
require_once $dolibarr_main_document_root.'/compta/facture/class/facture.class.php';
require_once $dolibarr_main_document_root.'/comm/propal/class/propal.class.php';
require_once $dolibarr_main_document_root.'/contrat/class/contrat.class.php';
require_once $dolibarr_main_document_root.'/commande/class/commande.class.php';
require_once $dolibarr_main_document_root.'/fourn/class/fournisseur.commande.class.php';
require_once $dolibarr_main_document_root.'/core/lib/price.lib.php';
require_once $dolibarr_main_document_root.'/core/class/menubase.class.php';
require_once $dolibarr_main_document_root.'/core/lib/files.lib.php';
global $langs;
$grant_query = '';
$step = 2;
$error = 0;
// Cette page peut etre longue. On augmente le delai autorise.
// Ne fonctionne que si on est pas en safe_mode.
$err = error_reporting();
error_reporting(0);
if (!empty($conf->global->MAIN_OVERRIDE_TIME_LIMIT)) {
@set_time_limit((int) $conf->global->MAIN_OVERRIDE_TIME_LIMIT);
} else {
@set_time_limit(600);
}
error_reporting($err);
$setuplang = GETPOST("selectlang", 'aZ09', 3) ?GETPOST("selectlang", 'aZ09', 3) : 'auto';
$langs->setDefaultLang($setuplang);
$versionfrom = GETPOST("versionfrom", 'alpha', 3) ?GETPOST("versionfrom", 'alpha', 3) : (empty($argv[1]) ? '' : $argv[1]);
$versionto = GETPOST("versionto", 'alpha', 3) ?GETPOST("versionto", 'alpha', 3) : (empty($argv[2]) ? '' : $argv[2]);
$enablemodules = GETPOST("enablemodules", 'alpha', 3) ?GETPOST("enablemodules", 'alpha', 3) : (empty($argv[3]) ? '' : $argv[3]);
$langs->loadLangs(array("admin", "install", "bills", "suppliers"));
if ($dolibarr_main_db_type == 'mysqli') {
$choix = 1;
}
if ($dolibarr_main_db_type == 'pgsql') {
$choix = 2;
}
if ($dolibarr_main_db_type == 'mssql') {
$choix = 3;
}
dolibarr_install_syslog("--- upgrade2: entering upgrade2.php page ".$versionfrom." ".$versionto." ".$enablemodules);
if (!is_object($conf)) {
dolibarr_install_syslog("upgrade2: conf file not initialized", LOG_ERR);
}
/*
* View
*/
if ((!$versionfrom || preg_match('/version/', $versionfrom)) && (!$versionto || preg_match('/version/', $versionto))) {
print 'Error: Parameter versionfrom or versionto missing or having a bad format.'."\n";
print 'Upgrade must be ran from command line with parameters or called from page install/index.php (like a first install)'."\n";
// Test if batch mode
$sapi_type = php_sapi_name();
$script_file = basename(__FILE__);
$path = __DIR__.'/';
if (substr($sapi_type, 0, 3) == 'cli') {
print 'Syntax from command line: '.$script_file." x.y.z a.b.c [MAIN_MODULE_NAME1_TO_ENABLE,MAIN_MODULE_NAME2_TO_ENABLE...]\n";
}
exit;
}
pHeader('', 'step5', GETPOST('action', 'aZ09') ?GETPOST('action', 'aZ09') : 'upgrade', 'versionfrom='.$versionfrom.'&versionto='.$versionto, '', 'main-inside main-inside-borderbottom');
if (!GETPOST('action', 'aZ09') || preg_match('/upgrade/i', GETPOST('action', 'aZ09'))) {
print '<h3><img class="valignmiddle inline-block paddingright" src="../theme/common/octicons/build/svg/database.svg" width="20" alt="Database"> ';
print '<span class="inline-block">'.$langs->trans('DataMigration').'</span></h3>';
print '<table border="0" width="100%">';
// If password is encoded, we decode it
if (preg_match('/crypted:/i', $dolibarr_main_db_pass) || !empty($dolibarr_main_db_encrypted_pass)) {
require_once $dolibarr_main_document_root.'/core/lib/security.lib.php';
if (preg_match('/crypted:/i', $dolibarr_main_db_pass)) {
$dolibarr_main_db_pass = preg_replace('/crypted:/i', '', $dolibarr_main_db_pass);
$dolibarr_main_db_pass = dol_decode($dolibarr_main_db_pass);
$dolibarr_main_db_encrypted_pass = $dolibarr_main_db_pass; // We need to set this as it is used to know the password was initially crypted
} else {
$dolibarr_main_db_pass = dol_decode($dolibarr_main_db_encrypted_pass);
}
}
// $conf is already instancied inside inc.php
$conf->db->type = $dolibarr_main_db_type;
$conf->db->host = $dolibarr_main_db_host;
$conf->db->port = $dolibarr_main_db_port;
$conf->db->name = $dolibarr_main_db_name;
$conf->db->user = $dolibarr_main_db_user;
$conf->db->pass = $dolibarr_main_db_pass;
$db = getDoliDBInstance($conf->db->type, $conf->db->host, $conf->db->user, $conf->db->pass, $conf->db->name, $conf->db->port);
if (!$db->connected) {
print '<tr><td colspan="4">'.$langs->trans("ErrorFailedToConnectToDatabase", $conf->db->name).'</td><td class="right">'.$langs->trans('Error').'</td></tr>';
dolibarr_install_syslog('upgrade2: failed to connect to database :'.$conf->db->name.' on '.$conf->db->host.' for user '.$conf->db->user, LOG_ERR);
$error++;
}
if (!$error) {
if ($db->database_selected) {
dolibarr_install_syslog('upgrade2: database connection successful :'.$dolibarr_main_db_name);
} else {
$error++;
}
}
if (empty($dolibarr_main_db_encryption)) {
$dolibarr_main_db_encryption = 0;
}
$conf->db->dolibarr_main_db_encryption = $dolibarr_main_db_encryption;
if (empty($dolibarr_main_db_cryptkey)) {
$dolibarr_main_db_cryptkey = '';
}
$conf->db->dolibarr_main_db_cryptkey = $dolibarr_main_db_cryptkey;
// Chargement config
if (!$error) {
$conf->setValues($db);
// Reset forced setup after the setValues
if (defined('SYSLOG_FILE')) {
$conf->global->SYSLOG_FILE = constant('SYSLOG_FILE');
}
$conf->global->MAIN_ENABLE_LOG_TO_HTML = 1;
}
// Create the global $hookmanager object
include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
$hookmanager = new HookManager($db);
$hookmanager->initHooks(array('upgrade'));
/***************************************************************************************
*
* Migration of data
*
***************************************************************************************/
$db->begin();
if (!$error) {
// Current version is $conf->global->MAIN_VERSION_LAST_UPGRADE
// Version to install is DOL_VERSION
$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 : ''));
// Chaque action de migration doit renvoyer une ligne sur 4 colonnes avec
// dans la 1ere colonne, la description de l'action a faire
// dans la 4eme colonne, le texte 'OK' si fait ou 'AlreadyDone' si rien n'est fait ou 'Error'
$versiontoarray = explode('.', $versionto);
$versionranarray = explode('.', DOL_VERSION);
// Force to execute this at begin to avoid the new core code into Dolibarr to be broken.
$sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN birth date';
$db->query($sql, 1);
$sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN dateemployment date';
$db->query($sql, 1);
$sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN dateemploymentend date';
$db->query($sql, 1);
$sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN default_range integer';
$db->query($sql, 1);
$sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user ADD COLUMN default_c_exp_tax_cat integer';
$db->query($sql, 1);
$sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'extrafields ADD COLUMN langs varchar(24)';
$db->query($sql, 1);
$sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'extrafields ADD COLUMN fieldcomputed text';
$db->query($sql, 1);
$sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'extrafields ADD COLUMN fielddefault varchar(255)';
$db->query($sql, 1);
$sql = 'ALTER TABLE '.MAIN_DB_PREFIX."extrafields ADD COLUMN enabled varchar(255) DEFAULT '1'";
$db->query($sql, 1);
$sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'extrafields ADD COLUMN help text';
$db->query($sql, 1);
$sql = 'ALTER TABLE '.MAIN_DB_PREFIX.'user_rights ADD COLUMN entity integer DEFAULT 1 NOT NULL';
$db->query($sql, 1);
$afterversionarray = explode('.', '2.0.0');
$beforeversionarray = explode('.', '2.7.9');
if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
// Script pour V2 -> V2.1
migrate_paiements($db, $langs, $conf);
migrate_contracts_det($db, $langs, $conf);
migrate_contracts_date1($db, $langs, $conf);
migrate_contracts_date2($db, $langs, $conf);
migrate_contracts_date3($db, $langs, $conf);
migrate_contracts_open($db, $langs, $conf);
migrate_modeles($db, $langs, $conf);
migrate_price_propal($db, $langs, $conf);
migrate_price_commande($db, $langs, $conf);
migrate_price_commande_fournisseur($db, $langs, $conf);
migrate_price_contrat($db, $langs, $conf);
migrate_paiementfourn_facturefourn($db, $langs, $conf);
// Script pour V2.1 -> V2.2
migrate_paiements_orphelins_1($db, $langs, $conf);
migrate_paiements_orphelins_2($db, $langs, $conf);
migrate_links_transfert($db, $langs, $conf);
// Script pour V2.2 -> V2.4
migrate_commande_expedition($db, $langs, $conf);
migrate_commande_livraison($db, $langs, $conf);
migrate_detail_livraison($db, $langs, $conf);
// Script pour V2.5 -> V2.6
migrate_stocks($db, $langs, $conf);
// Script pour V2.6 -> V2.7
migrate_menus($db, $langs, $conf);
migrate_commande_deliveryaddress($db, $langs, $conf);
migrate_restore_missing_links($db, $langs, $conf);
migrate_rename_directories($db, $langs, $conf, '/compta', '/banque');
migrate_rename_directories($db, $langs, $conf, '/societe', '/mycompany');
}
// Script for 2.8
$afterversionarray = explode('.', '2.7.9');
$beforeversionarray = explode('.', '2.8.9');
//print $versionto.' '.versioncompare($versiontoarray,$afterversionarray).' '.versioncompare($versiontoarray,$beforeversionarray);
if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
migrate_price_facture($db, $langs, $conf); // Code of this function works for 2.8+ because need a field tva_tx
migrate_relationship_tables($db, $langs, $conf, 'co_exp', 'fk_commande', 'commande', 'fk_expedition', 'shipping');
migrate_relationship_tables($db, $langs, $conf, 'pr_exp', 'fk_propal', 'propal', 'fk_expedition', 'shipping');
migrate_relationship_tables($db, $langs, $conf, 'pr_liv', 'fk_propal', 'propal', 'fk_livraison', 'delivery');
migrate_relationship_tables($db, $langs, $conf, 'co_liv', 'fk_commande', 'commande', 'fk_livraison', 'delivery');
migrate_relationship_tables($db, $langs, $conf, 'co_pr', 'fk_propale', 'propal', 'fk_commande', 'commande');
migrate_relationship_tables($db, $langs, $conf, 'fa_pr', 'fk_propal', 'propal', 'fk_facture', 'facture');
migrate_relationship_tables($db, $langs, $conf, 'co_fa', 'fk_commande', 'commande', 'fk_facture', 'facture');
migrate_project_user_resp($db, $langs, $conf);
migrate_project_task_actors($db, $langs, $conf);
}
// Script for 2.9
$afterversionarray = explode('.', '2.8.9');
$beforeversionarray = explode('.', '2.9.9');
if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
migrate_project_task_time($db, $langs, $conf);
migrate_customerorder_shipping($db, $langs, $conf);
migrate_shipping_delivery($db, $langs, $conf);
migrate_shipping_delivery2($db, $langs, $conf);
}
// Script for 3.0
$afterversionarray = explode('.', '2.9.9');
$beforeversionarray = explode('.', '3.0.9');
if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
// No particular code
}
// Script for 3.1
$afterversionarray = explode('.', '3.0.9');
$beforeversionarray = explode('.', '3.1.9');
if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
migrate_rename_directories($db, $langs, $conf, '/rss', '/externalrss');
migrate_actioncomm_element($db, $langs, $conf);
}
// Script for 3.2
$afterversionarray = explode('.', '3.1.9');
$beforeversionarray = explode('.', '3.2.9');
if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
migrate_price_contrat($db, $langs, $conf);
migrate_mode_reglement($db, $langs, $conf);
migrate_clean_association($db, $langs, $conf);
}
// Script for 3.3
$afterversionarray = explode('.', '3.2.9');
$beforeversionarray = explode('.', '3.3.9');
if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
migrate_categorie_association($db, $langs, $conf);
}
// Script for 3.4
// No specific scripts
// Tasks to do always and only into last targeted version
$afterversionarray = explode('.', '3.6.9'); // target is after this
$beforeversionarray = explode('.', '3.7.9'); // target is before this
if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
migrate_event_assignement($db, $langs, $conf);
}
// Scripts for 3.9
$afterversionarray = explode('.', '3.7.9');
$beforeversionarray = explode('.', '3.8.9');
if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
// No particular code
}
// Scripts for 4.0
$afterversionarray = explode('.', '3.9.9');
$beforeversionarray = explode('.', '4.0.9');
if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
migrate_rename_directories($db, $langs, $conf, '/fckeditor', '/medias');
}
// Scripts for 5.0
$afterversionarray = explode('.', '4.0.9');
$beforeversionarray = explode('.', '5.0.9');
if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
// Migrate to add entity value into llx_societe_remise
migrate_remise_entity($db, $langs, $conf);
// Migrate to add entity value into llx_societe_remise_except
migrate_remise_except_entity($db, $langs, $conf);
}
// Scripts for 6.0
$afterversionarray = explode('.', '5.0.9');
$beforeversionarray = explode('.', '6.0.9');
if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
if (!empty($conf->multicompany->enabled)) {
global $multicompany_transverse_mode;
// Only if the transverse mode is not used
if (empty($multicompany_transverse_mode)) {
// Migrate to add entity value into llx_user_rights
migrate_user_rights_entity($db, $langs, $conf);
// Migrate to add entity value into llx_usergroup_rights
migrate_usergroup_rights_entity($db, $langs, $conf);
}
}
}
// Scripts for 7.0
$afterversionarray = explode('.', '6.0.9');
$beforeversionarray = explode('.', '7.0.9');
if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
// Migrate contact association
migrate_event_assignement_contact($db, $langs, $conf);
migrate_reset_blocked_log($db, $langs, $conf);
}
// Scripts for 8.0
$afterversionarray = explode('.', '7.0.9');
$beforeversionarray = explode('.', '8.0.9');
if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
migrate_rename_directories($db, $langs, $conf, '/contracts', '/contract');
}
// Scripts for 9.0
$afterversionarray = explode('.', '8.0.9');
$beforeversionarray = explode('.', '9.0.9');
if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
migrate_user_photospath();
}
// Scripts for 11.0
$afterversionarray = explode('.', '10.0.9');
$beforeversionarray = explode('.', '11.0.9');
if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
migrate_users_socialnetworks();
migrate_members_socialnetworks();
migrate_contacts_socialnetworks();
migrate_thirdparties_socialnetworks();
}
// Scripts for 14.0
$afterversionarray = explode('.', '13.0.9');
$beforeversionarray = explode('.', '14.0.9');
if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
migrate_export_import_profiles('export');
migrate_export_import_profiles('import');
}
// Scripts for 16.0
$afterversionarray = explode('.', '15.0.9');
$beforeversionarray = explode('.', '16.0.9');
if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
migrate_user_photospath2();
}
}
// Code executed only if migration is LAST ONE. Must always be done.
if (versioncompare($versiontoarray, $versionranarray) >= 0 || versioncompare($versiontoarray, $versionranarray) <= -3) {
// 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)
$listofmodule = array(
'MAIN_MODULE_ACCOUNTING'=>'newboxdefonly',
'MAIN_MODULE_AGENDA'=>'newboxdefonly',
'MAIN_MODULE_BOM'=>'menuonly',
'MAIN_MODULE_BANQUE'=>'menuonly',
'MAIN_MODULE_BARCODE'=>'newboxdefonly',
'MAIN_MODULE_CRON'=>'newboxdefonly',
'MAIN_MODULE_COMMANDE'=>'newboxdefonly',
'MAIN_MODULE_BLOCKEDLOG'=>'noboxes',
'MAIN_MODULE_DEPLACEMENT'=>'newboxdefonly',
'MAIN_MODULE_DON'=>'newboxdefonly',
'MAIN_MODULE_ECM'=>'newboxdefonly',
'MAIN_MODULE_EXTERNALSITE'=>'newboxdefonly',
'MAIN_MODULE_EXPENSEREPORT'=>'newboxdefonly',
'MAIN_MODULE_FACTURE'=>'newboxdefonly',
'MAIN_MODULE_FOURNISSEUR'=>'newboxdefonly',
'MAIN_MODULE_HOLIDAY'=>'newboxdefonly',
'MAIN_MODULE_MARGIN'=>'menuonly',
'MAIN_MODULE_MRP'=>'menuonly',
'MAIN_MODULE_OPENSURVEY'=>'newboxdefonly',
'MAIN_MODULE_PAYBOX'=>'newboxdefonly',
'MAIN_MODULE_PRINTING'=>'newboxdefonly',
'MAIN_MODULE_PRODUIT'=>'newboxdefonly',
'MAIN_MODULE_RECRUITMENT'=>'menuonly',
'MAIN_MODULE_RESOURCE'=>'noboxes',
'MAIN_MODULE_SALARIES'=>'newboxdefonly',
'MAIN_MODULE_SERVICE'=>'newboxdefonly',
'MAIN_MODULE_SYSLOG'=>'newboxdefonly',
'MAIN_MODULE_SOCIETE'=>'newboxdefonly',
'MAIN_MODULE_STRIPE'=>'menuonly',
'MAIN_MODULE_TICKET'=>'newboxdefonly',
'MAIN_MODULE_TAKEPOS'=>'newboxdefonly',
'MAIN_MODULE_USER'=>'newboxdefonly', //This one must be always done and only into last targeted version)
'MAIN_MODULE_VARIANTS'=>'newboxdefonly',
'MAIN_MODULE_WEBSITE'=>'newboxdefonly',
);
$result = migrate_reload_modules($db, $langs, $conf, $listofmodule);
if ($result < 0) {
$error++;
}
// Reload menus (this must be always and only into last targeted version)
$result = migrate_reload_menu($db, $langs, $conf);
if ($result < 0) {
$error++;
}
}
// Can force activation of some module during migration with parameter 'enablemodules=MAIN_MODULE_XXX,MAIN_MODULE_YYY,...'
// In most cases (online install or upgrade) $enablemodules is empty. Can be forced when ran from command line.
if (!$error && $enablemodules) {
// Reload modules (this must be always done and only into last targeted version)
$listofmodules = array();
$enablemodules = preg_replace('/enablemodules=/', '', $enablemodules);
$tmplistofmodules = explode(',', $enablemodules);
foreach ($tmplistofmodules as $value) {
$listofmodules[$value] = 'forceactivate';
}
$resultreloadmodules = migrate_reload_modules($db, $langs, $conf, $listofmodules, 1);
if ($resultreloadmodules < 0) {
$error++;
}
}
// Can call a dedicated external upgrade process
if (!$error) {
$parameters = array('versionfrom' => $versionfrom, 'versionto' => $versionto);
$object = new stdClass();
$action = "upgrade";
$reshook = $hookmanager->executeHooks('doUpgrade2', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
if ($hookmanager->resNbOfHooks > 0) {
if ($reshook < 0) {
print '<tr><td colspan="4">';
print '<b>'.$langs->trans('UpgradeExternalModule').'</b>: ';
print $hookmanager->error;
print "<!-- (".$reshook.") -->";
print '</td></tr>';
} else {
print '<tr class="trforrunsql"><td colspan="4">';
print '<b>'.$langs->trans('UpgradeExternalModule').'</b>: <span class="ok">OK</span>';
print "<!-- (".$reshook.") -->";
print '</td></tr>';
}
} else {
//if (! empty($conf->modules))
if (!empty($conf->modules_parts['hooks'])) { // If there is at least one module with one hook, we show message to say nothing was done
print '<tr class="trforrunsql"><td colspan="4">';
print '<b>'.$langs->trans('UpgradeExternalModule').'</b>: '.$langs->trans("None");
print '</td></tr>';
}
}
}
print '</table>';
if (!$error) {
// Set constant to ask to remake a new ping to inform about upgrade (if first ping was done and OK)
$sql = 'UPDATE '.MAIN_DB_PREFIX."const SET VALUE = 'torefresh' WHERE name = 'MAIN_FIRST_PING_OK_ID'";
$db->query($sql, 1);
}
// We always commit.
// Process is designed so we can run it several times whatever is situation.
$db->commit();
$db->close();
// Copy directory medias
$srcroot = DOL_DOCUMENT_ROOT.'/install/medias';
$destroot = DOL_DATA_ROOT.'/medias';
dolCopyDir($srcroot, $destroot, 0, 0);
// Actions for all versions (no database change but delete some files and directories)
migrate_delete_old_files($db, $langs, $conf);
migrate_delete_old_dir($db, $langs, $conf);
// Actions for all versions (no database change but create some directories)
dol_mkdir(DOL_DATA_ROOT.'/bank');
// Actions for all versions (no database change but rename some directories)
migrate_rename_directories($db, $langs, $conf, '/banque/bordereau', '/bank/checkdeposits');
$silent = 0;
if (!$silent) {
print '<table width="100%">';
print '<tr><td style="width: 30%">'.$langs->trans("MigrationFinished").'</td>';
print '<td class="right">';
if ($error == 0) {
//print '<span class="ok">'.$langs->trans("OK").'</span> - '; // $error = 0 does not mean there is no error (error are not always trapped)
} else {
print '<span class="error">'.$langs->trans("Error").'</span> - ';
}
//if (!empty($conf->use_javascript_ajax)) { // use_javascript_ajax is not defined
print '<script type="text/javascript">
jQuery(document).ready(function() {
function init_trrunsql()
{
console.log("toggle .trforrunsql");
jQuery(".trforrunsql").toggle();
}
init_trrunsql();
jQuery(".trforrunsqlshowhide").click(function() {
init_trrunsql();
});
});
</script>';
print '<a class="trforrunsqlshowhide" href="#">'.$langs->trans("ShowHideDetails").'</a>';
//}
print '</td></tr>'."\n";
print '</table>';
}
//print '<div><br>'.$langs->trans("MigrationFinished").'</div>';
} else {
print '<div class="error">'.$langs->trans('ErrorWrongParameters').'</div>';
$error++;
}
$ret = 0;
if ($error && isset($argv[1])) {
$ret = 1;
}
dolibarr_install_syslog("Exit ".$ret);
dolibarr_install_syslog("--- upgrade2: end");
pFooter($error ? 2 : 0, $setuplang);
if ($db->connected) {
$db->close();
}
// Return code if ran from command line
if ($ret) {
exit($ret);
}
/**
* Reporte liens vers une facture de paiements sur table de jointure (lien n-n paiements factures)
*
* @param DoliDB $db Database handler
* @param Translate $langs Object langs
* @param Conf $conf Object conf
* @return void
*/
function migrate_paiements($db, $langs, $conf)
{
print '<tr><td colspan="4">';
print '<br>';
print '<b>'.$langs->trans('MigrationPaymentsUpdate')."</b><br>\n";
$result = $db->DDLDescTable(MAIN_DB_PREFIX."paiement", "fk_facture");
$obj = $db->fetch_object($result);
if ($obj) {
$sql = "SELECT p.rowid, p.fk_facture, p.amount";
$sql .= " FROM ".MAIN_DB_PREFIX."paiement as p";
$sql .= " WHERE p.fk_facture > 0";
$resql = $db->query($sql);
dolibarr_install_syslog("upgrade2::migrate_paiements");
if ($resql) {
$i = 0;
$row = array();
$num = $db->num_rows($resql);
while ($i < $num) {
$obj = $db->fetch_object($resql);
$row[$i][0] = $obj->rowid;
$row[$i][1] = $obj->fk_facture;
$row[$i][2] = $obj->amount;
$i++;
}
} else {
dol_print_error($db);
}
if ($num) {
print $langs->trans('MigrationPaymentsNumberToUpdate', $num)."<br>\n";
if ($db->begin()) {
$res = 0;
$num = count($row);
for ($i = 0; $i < $num; $i++) {
$sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount)";
$sql .= " VALUES (".((int) $row[$i][1]).",".((int) $row[$i][0]).",".((float) $row[$i][2]).")";
$res += $db->query($sql);
$sql = "UPDATE ".MAIN_DB_PREFIX."paiement SET fk_facture = 0 WHERE rowid = ".((int) $row[$i][0]);
$res += $db->query($sql);
print $langs->trans('MigrationProcessPaymentUpdate', $row[$i][0])."<br>\n";
}
}
if ($res == (2 * count($row))) {
$db->commit();
print $langs->trans('MigrationSuccessfullUpdate')."<br>";
} else {
$db->rollback();
print $langs->trans('MigrationUpdateFailed').'<br>';
}
} else {
print $langs->trans('MigrationPaymentsNothingToUpdate')."<br>\n";
}
} else {
print $langs->trans('MigrationPaymentsNothingToUpdate')."<br>\n";
}
print '</td></tr>';
}
/**
* Corrige paiement orphelins (liens paumes suite a bugs)
* Pour verifier s'il reste des orphelins:
* select * from llx_paiement as p left join llx_paiement_facture as pf on pf.fk_paiement=p.rowid WHERE pf.rowid IS NULL AND (p.fk_facture = 0 OR p.fk_facture IS NULL)
*
* @param DoliDB $db Database handler
* @param Translate $langs Object langs
* @param Conf $conf Object conf
* @return void
*/
function migrate_paiements_orphelins_1($db, $langs, $conf)
{
print '<tr><td colspan="4">';
print '<br>';
print '<b>'.$langs->trans('MigrationPaymentsUpdate')."</b><br>\n";
$result = $db->DDLDescTable(MAIN_DB_PREFIX."paiement", "fk_facture");
$obj = $db->fetch_object($result);
if ($obj) {
// Tous les enregistrements qui sortent de cette requete devrait avoir un pere dans llx_paiement_facture
$sql = "SELECT distinct p.rowid, p.datec, p.amount as pamount, bu.fk_bank, b.amount as bamount,";
$sql .= " bu2.url_id as socid";
$sql .= " FROM (".MAIN_DB_PREFIX."paiement as p, ".MAIN_DB_PREFIX."bank_url as bu, ".MAIN_DB_PREFIX."bank as b)";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON pf.fk_paiement = p.rowid";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu2 ON (bu.fk_bank=bu2.fk_bank AND bu2.type = 'company')";
$sql .= " WHERE pf.rowid IS NULL AND (p.rowid=bu.url_id AND bu.type='payment') AND bu.fk_bank = b.rowid";
$sql .= " AND b.rappro = 1";
$sql .= " AND (p.fk_facture = 0 OR p.fk_facture IS NULL)";
$resql = $db->query($sql);
dolibarr_install_syslog("upgrade2::migrate_paiements_orphelins_1");
$row = array();
if ($resql) {
$i = $j = 0;
$num = $db->num_rows($resql);
while ($i < $num) {
$obj = $db->fetch_object($resql);
if ($obj->pamount == $obj->bamount && $obj->socid) { // Pour etre sur d'avoir bon cas
$row[$j]['paymentid'] = $obj->rowid; // paymentid
$row[$j]['pamount'] = $obj->pamount;
$row[$j]['fk_bank'] = $obj->fk_bank;
$row[$j]['bamount'] = $obj->bamount;
$row[$j]['socid'] = $obj->socid;
$row[$j]['datec'] = $obj->datec;
$j++;
}
$i++;
}
} else {
dol_print_error($db);
}
if (count($row)) {
print $langs->trans('OrphelinsPaymentsDetectedByMethod', 1).': '.count($row)."<br>\n";
$db->begin();
$res = 0;
$num = count($row);
for ($i = 0; $i < $num; $i++) {
if ($conf->global->MAIN_FEATURES_LEVEL == 2) {
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>';
}
// On cherche facture sans lien paiement et du meme montant et pour meme societe.
$sql = " SELECT distinct f.rowid from ".MAIN_DB_PREFIX."facture as f";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture";
$sql .= " WHERE f.fk_statut in (2,3) AND fk_soc = ".((int) $row[$i]['socid'])." AND total_ttc = ".((float) $row[$i]['pamount']);
$sql .= " AND pf.fk_facture IS NULL";
$sql .= " ORDER BY f.fk_statut";
//print $sql.'<br>';
$resql = $db->query($sql);
if ($resql) {
$num = $db->num_rows($resql);
//print 'Nb of invoice found for this amount and company :'.$num.'<br>';
if ($num >= 1) {
$obj = $db->fetch_object($resql);
$facid = $obj->rowid;
$sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount)";
$sql .= " VALUES (".((int) $facid).",".((int) $row[$i]['paymentid']).", ".((float) $row[$i]['pamount']).")";
$res += $db->query($sql);
print $langs->trans('MigrationProcessPaymentUpdate', 'facid='.$facid.'-paymentid='.$row[$i]['paymentid'].'-amount='.$row[$i]['pamount'])."<br>\n";
}
} else {
print 'ERROR';
}
}
if ($res > 0) {
print $langs->trans('MigrationSuccessfullUpdate')."<br>";
} else {
print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
}
$db->commit();
} else {
print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
}
} else {
print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
}
print '</td></tr>';
}
/**
* Corrige paiement orphelins (liens paumes suite a bugs)
* Pour verifier s'il reste des orphelins:
* select * from llx_paiement as p left join llx_paiement_facture as pf on pf.fk_paiement=p.rowid WHERE pf.rowid IS NULL AND (p.fk_facture = 0 OR p.fk_facture IS NULL)
*
* @param DoliDB $db Database handler
* @param Translate $langs Object langs
* @param Conf $conf Object conf
* @return void
*/
function migrate_paiements_orphelins_2($db, $langs, $conf)
{
print '<tr><td colspan="4">';
print '<br>';
print '<b>'.$langs->trans('MigrationPaymentsUpdate')."</b><br>\n";
$result = $db->DDLDescTable(MAIN_DB_PREFIX."paiement", "fk_facture");
$obj = $db->fetch_object($result);
if ($obj) {
// Tous les enregistrements qui sortent de cette requete devrait avoir un pere dans llx_paiement_facture
$sql = "SELECT distinct p.rowid, p.datec, p.amount as pamount, bu.fk_bank, b.amount as bamount,";
$sql .= " bu2.url_id as socid";
$sql .= " FROM (".MAIN_DB_PREFIX."paiement as p, ".MAIN_DB_PREFIX."bank_url as bu, ".MAIN_DB_PREFIX."bank as b)";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON pf.fk_paiement = p.rowid";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu2 ON (bu.fk_bank = bu2.fk_bank AND bu2.type = 'company')";
$sql .= " WHERE pf.rowid IS NULL AND (p.fk_bank = bu.fk_bank AND bu.type = 'payment') AND bu.fk_bank = b.rowid";
$sql .= " AND (p.fk_facture = 0 OR p.fk_facture IS NULL)";
$resql = $db->query($sql);
dolibarr_install_syslog("upgrade2::migrate_paiements_orphelins_2");
$row = array();
if ($resql) {
$i = $j = 0;
$num = $db->num_rows($resql);
while ($i < $num) {
$obj = $db->fetch_object($resql);
if ($obj->pamount == $obj->bamount && $obj->socid) { // Pour etre sur d'avoir bon cas
$row[$j]['paymentid'] = $obj->rowid; // paymentid
$row[$j]['pamount'] = $obj->pamount;
$row[$j]['fk_bank'] = $obj->fk_bank;
$row[$j]['bamount'] = $obj->bamount;
$row[$j]['socid'] = $obj->socid;
$row[$j]['datec'] = $obj->datec;
$j++;
}
$i++;
}
} else {
dol_print_error($db);
}
$nberr = 0;
$num = count($row);
if ($num) {
print $langs->trans('OrphelinsPaymentsDetectedByMethod', 2).': '.count($row)."<br>\n";
$db->begin();
$res = 0;
for ($i = 0; $i < $num; $i++) {
if ($conf->global->MAIN_FEATURES_LEVEL == 2) {
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>';
}
// On cherche facture sans lien paiement et du meme montant et pour meme societe.
$sql = " SELECT distinct f.rowid from ".MAIN_DB_PREFIX."facture as f";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture";
$sql .= " WHERE f.fk_statut in (2,3) AND fk_soc = ".((int) $row[$i]['socid'])." AND total_ttc = ".((float) $row[$i]['pamount']);
$sql .= " AND pf.fk_facture IS NULL";
$sql .= " ORDER BY f.fk_statut";
//print $sql.'<br>';
$resql = $db->query($sql);
if ($resql) {
$num = $db->num_rows($resql);
//print 'Nb of invoice found for this amount and company :'.$num.'<br>';
if ($num >= 1) {
$obj = $db->fetch_object($resql);
$facid = $obj->rowid;
$sql = "INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (fk_facture, fk_paiement, amount)";
$sql .= " VALUES (".((int) $facid).",".((int) $row[$i]['paymentid']).", ".((float) $row[$i]['pamount']).")";
$res += $db->query($sql);
print $langs->trans('MigrationProcessPaymentUpdate', 'facid='.$facid.'-paymentid='.$row[$i]['paymentid'].'-amount='.$row[$i]['pamount'])."<br>\n";
}
} else {
print 'ERROR';
$nberr++;
}
}
if ($res > 0) {
print $langs->trans('MigrationSuccessfullUpdate')."<br>";
} else {
print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
}
$db->commit();
} else {
print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
}
// Delete obsolete fields fk_facture
$db->begin();
$sql = "ALTER TABLE ".MAIN_DB_PREFIX."paiement DROP COLUMN fk_facture";
$db->query($sql);
if (!$nberr) {
$db->commit();
} else {
print 'ERROR';
$db->rollback();
}
} else {
print $langs->trans('MigrationPaymentsNothingUpdatable')."<br>\n";
}
print '</td></tr>';
}
/**
* Mise a jour des contrats (gestion du contrat + detail de contrat)
*
* @param DoliDB $db Database handler
* @param Translate $langs Object langs
* @param Conf $conf Object conf
* @return void
*/
function migrate_contracts_det($db, $langs, $conf)
{
print '<tr><td colspan="4">';
$nberr = 0;
print '<br>';
print '<b>'.$langs->trans('MigrationContractsUpdate')."</b><br>\n";
$sql = "SELECT c.rowid as cref, c.date_contrat, c.statut, c.fk_product, c.fk_facture, c.fk_user_author,";
$sql .= " p.ref, p.label, p.description, p.price, p.tva_tx, p.duration, cd.rowid";
$sql .= " FROM ".MAIN_DB_PREFIX."contrat as c";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p";
$sql .= " ON c.fk_product = p.rowid";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."contratdet as cd";
$sql .= " ON c.rowid=cd.fk_contrat";
$sql .= " WHERE cd.rowid IS NULL AND p.rowid IS NOT NULL";
$resql = $db->query($sql);
dolibarr_install_syslog("upgrade2::migrate_contracts_det");
if ($resql) {
$i = 0;
$row = array();
$num = $db->num_rows($resql);
if ($num) {