OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 81 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 81 Rev 83
Line 11... Line 11...
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.erp.utils.correct;
14
 package org.openconcerto.erp.utils.correct;
15
 
15
 
-
 
16
import org.openconcerto.erp.config.Log;
16
import org.openconcerto.sql.changer.Changer;
17
import org.openconcerto.sql.changer.Changer;
17
import org.openconcerto.sql.model.DBRoot;
18
import org.openconcerto.sql.model.DBRoot;
18
import org.openconcerto.sql.model.DBSystemRoot;
19
import org.openconcerto.sql.model.DBSystemRoot;
19
import org.openconcerto.sql.model.SQLBase;
20
import org.openconcerto.sql.model.SQLBase;
20
import org.openconcerto.sql.model.SQLField;
21
import org.openconcerto.sql.model.SQLField;
21
import org.openconcerto.sql.model.SQLName;
-
 
22
import org.openconcerto.sql.model.SQLSelect;
22
import org.openconcerto.sql.model.SQLSelect;
23
import org.openconcerto.sql.model.SQLSelect.ArchiveMode;
23
import org.openconcerto.sql.model.SQLSelect.ArchiveMode;
24
import org.openconcerto.sql.model.SQLSystem;
24
import org.openconcerto.sql.model.SQLSystem;
25
import org.openconcerto.sql.model.SQLTable;
25
import org.openconcerto.sql.model.SQLTable;
26
import org.openconcerto.sql.model.Where;
26
import org.openconcerto.sql.model.Where;
Line 70... Line 70...
70
            selUnfixable.setArchivedPolicy(ArchiveMode.BOTH);
70
            selUnfixable.setArchivedPolicy(ArchiveMode.BOTH);
71
 
71
 
72
            final String selFixableUnbalanced = "( " + selUnbalanced.asString() + "\nEXCEPT\n" + selUnfixable.asString() + " )";
72
            final String selFixableUnbalanced = "( " + selUnbalanced.asString() + "\nEXCEPT\n" + selUnfixable.asString() + " )";
73
 
73
 
74
            final UpdateBuilder updateUnbalanced = new UpdateBuilder(ecritureT);
74
            final UpdateBuilder updateUnbalanced = new UpdateBuilder(ecritureT);
75
            // FIXME
-
 
76
            updateUnbalanced.addRawTable(selFixableUnbalanced, SQLBase.quoteIdentifier("semiArchivedMvt"));
-
 
77
            updateUnbalanced.setWhere(Where.quote("%i = %f", new SQLName("semiArchivedMvt", "ID_MOUVEMENT"), ecritureMvtFF));
75
            updateUnbalanced.addVirtualJoin(selFixableUnbalanced, "semiArchivedMvt", false, ecritureMvtFF.getName(), ecritureMvtFF.getName());
78
            updateUnbalanced.set(ecritureT.getArchiveField().getName(), "0");
76
            updateUnbalanced.set(ecritureT.getArchiveField().getName(), "0");
79
 
77
 
80
            getDS().execute(updateUnbalanced.asString());
78
            getDS().execute(updateUnbalanced.asString());
81
        }
79
        }
82
 
80
 
83
        // match SAISIE_KM_ELEMENT with their lost ECRITURE
81
        // match SAISIE_KM_ELEMENT with their lost ECRITURE
-
 
82
        if (getSyntax().getSystem() == SQLSystem.H2) {
-
 
83
            Log.get().warning("Matching SAISIE_KM_ELEMENT with their lost ECRITURE unsupported");
84
        {
84
        } else {
85
            final SQLTable saisieKmElemT = societeRoot.getGraph().findReferentTable(ecritureT, "SAISIE_KM_ELEMENT");
85
            final SQLTable saisieKmElemT = societeRoot.getGraph().findReferentTable(ecritureT, "SAISIE_KM_ELEMENT");
86
            final SQLTable saisieKmT = saisieKmElemT.getForeignTable("ID_SAISIE_KM");
86
            final SQLTable saisieKmT = saisieKmElemT.getForeignTable("ID_SAISIE_KM");
87
            // select ECRITURE which can be identified in a MOUVEMENT by its CREDIT/DEBIT and isn't
87
            // select ECRITURE which can be identified in a MOUVEMENT by its CREDIT/DEBIT and isn't
88
            // already linked to a SAISIE_KM_ELEMENT
88
            // already linked to a SAISIE_KM_ELEMENT
89
            final SQLSelect selIdentifiableNonUsed = new SQLSelect();
89
            final SQLSelect selIdentifiableNonUsed = new SQLSelect();
Line 104... Line 104...
104
            for (final String uniqField : uniqueFields)
104
            for (final String uniqField : uniqueFields)
105
                selIdentifiableNonUsed.addGroupBy(ecritureT.getField(uniqField));
105
                selIdentifiableNonUsed.addGroupBy(ecritureT.getField(uniqField));
106
            selIdentifiableNonUsed.setHaving(Where.createRaw("count(*) = 1"));
106
            selIdentifiableNonUsed.setHaving(Where.createRaw("count(*) = 1"));
107
 
107
 
108
            final UpdateBuilder update = new UpdateBuilder(saisieKmElemT);
108
            final UpdateBuilder update = new UpdateBuilder(saisieKmElemT);
109
            // FIXME
-
 
110
            update.addTable(saisieKmT);
109
            update.addForwardVirtualJoin(saisieKmT, "ID_SAISIE_KM");
111
            update.addRawTable("( " + selIdentifiableNonUsed.asString() + " )", "e");
110
            update.addRawTable("( " + selIdentifiableNonUsed.asString() + " )", "e");
112
 
111
 
113
            final Where joinSaisieKmW = new Where(saisieKmElemT.getField("ID_SAISIE_KM"), "=", saisieKmT.getKey());
-
 
114
            Where joinEcritureW = null;
112
            Where joinEcritureW = null;
115
            for (final String uniqField : uniqueFields) {
113
            for (final String uniqField : uniqueFields) {
116
                final SQLTable t = uniqField.equals("ID_MOUVEMENT") ? saisieKmT : saisieKmElemT;
114
                final SQLTable t = uniqField.equals("ID_MOUVEMENT") ? saisieKmT : saisieKmElemT;
117
                joinEcritureW = Where.quote("e." + SQLBase.quoteIdentifier(uniqField) + "= %f", t.getField(uniqField)).and(joinEcritureW);
115
                joinEcritureW = Where.quote("e." + SQLBase.quoteIdentifier(uniqField) + "= %f", t.getField(uniqField)).and(joinEcritureW);
118
            }
116
            }
119
            final Where dontOverwrite = new Where(saisieKmElemT.getField("ID_ECRITURE"), Where.NULL_IS_DATA_EQ, ecritureT.getUndefinedIDNumber());
117
            final Where dontOverwrite = new Where(saisieKmElemT.getField("ID_ECRITURE"), Where.NULL_IS_DATA_EQ, ecritureT.getUndefinedIDNumber());
120
            final Where dontUpdateUndef = new Where(saisieKmElemT.getKey(), Where.NULL_IS_DATA_NEQ, saisieKmElemT.getUndefinedIDNumber());
118
            final Where dontUpdateUndef = new Where(saisieKmElemT.getKey(), Where.NULL_IS_DATA_NEQ, saisieKmElemT.getUndefinedIDNumber());
121
            final Where unarchived = new Where(saisieKmElemT.getArchiveField(), "=", 0);
119
            final Where unarchived = new Where(saisieKmElemT.getArchiveField(), "=", 0);
122
            update.setWhere(joinSaisieKmW.and(joinEcritureW).and(dontOverwrite).and(dontUpdateUndef).and(unarchived));
120
            update.setWhere(joinEcritureW.and(dontOverwrite).and(dontUpdateUndef).and(unarchived));
123
 
121
 
124
            update.set("ID_ECRITURE", "e." + SQLBase.quoteIdentifier(uniqueIDAlias));
122
            update.set("ID_ECRITURE", "e." + SQLBase.quoteIdentifier(uniqueIDAlias));
125
 
123
 
126
            getDS().execute(update.asString());
124
            getDS().execute(update.asString());
127
        }
125
        }