OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Rev 182 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
142 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.config.update;
15
 
16
import org.openconcerto.erp.config.InstallationPanel;
177 ilm 17
import org.openconcerto.erp.core.common.element.BanqueSQLElement;
156 ilm 18
import org.openconcerto.erp.core.common.ui.AbstractVenteArticleItemTable;
174 ilm 19
import org.openconcerto.erp.core.customerrelationship.mail.EmailTemplateSQLElement;
151 ilm 20
import org.openconcerto.erp.core.finance.payment.element.SDDMessageSQLElement;
21
import org.openconcerto.erp.core.finance.payment.element.SEPAMandateSQLElement;
22
import org.openconcerto.erp.core.finance.payment.element.TypeReglementSQLElement;
23
import org.openconcerto.erp.core.sales.invoice.element.SaisieVenteFactureSQLElement;
144 ilm 24
import org.openconcerto.erp.core.sales.order.ui.TypeFactureCommandeClient;
25
import org.openconcerto.erp.core.sales.pos.element.TicketCaisseSQLElement;
26
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
149 ilm 27
import org.openconcerto.erp.core.sales.quote.element.DevisLogMailSQLElement;
174 ilm 28
import org.openconcerto.erp.core.supplychain.stock.element.ComposedItemStockUpdater;
156 ilm 29
import org.openconcerto.erp.core.supplychain.stock.element.DepotStockSQLElement;
30
import org.openconcerto.erp.core.supplychain.stock.element.StockItem;
144 ilm 31
import org.openconcerto.sql.changer.convert.AddMDFields;
180 ilm 32
import org.openconcerto.sql.model.AliasedTable;
142 ilm 33
import org.openconcerto.sql.model.DBRoot;
156 ilm 34
import org.openconcerto.sql.model.SQLField;
142 ilm 35
import org.openconcerto.sql.model.SQLField.Properties;
36
import org.openconcerto.sql.model.SQLName;
156 ilm 37
import org.openconcerto.sql.model.SQLRow;
38
import org.openconcerto.sql.model.SQLRowListRSH;
144 ilm 39
import org.openconcerto.sql.model.SQLRowValues;
156 ilm 40
import org.openconcerto.sql.model.SQLSelect;
144 ilm 41
import org.openconcerto.sql.model.SQLSyntax;
42
import org.openconcerto.sql.model.SQLSystem;
142 ilm 43
import org.openconcerto.sql.model.SQLTable;
44
import org.openconcerto.sql.model.Where;
156 ilm 45
import org.openconcerto.sql.preferences.SQLPreferences;
142 ilm 46
import org.openconcerto.sql.request.UpdateBuilder;
47
import org.openconcerto.sql.utils.AlterTable;
149 ilm 48
import org.openconcerto.sql.utils.ChangeTable;
142 ilm 49
import org.openconcerto.sql.utils.SQLCreateTable;
144 ilm 50
import org.openconcerto.sql.utils.UniqueConstraintCreatorHelper;
151 ilm 51
import org.openconcerto.utils.CollectionUtils;
156 ilm 52
import org.openconcerto.utils.ExceptionHandler;
142 ilm 53
 
54
import java.math.BigDecimal;
156 ilm 55
import java.sql.Clob;
142 ilm 56
import java.sql.SQLException;
57
import java.sql.Types;
156 ilm 58
import java.util.ArrayList;
142 ilm 59
import java.util.Arrays;
156 ilm 60
import java.util.Collections;
142 ilm 61
import java.util.EnumSet;
156 ilm 62
import java.util.HashMap;
142 ilm 63
import java.util.List;
156 ilm 64
import java.util.Map;
174 ilm 65
import java.util.Set;
156 ilm 66
import java.util.prefs.BackingStoreException;
142 ilm 67
 
156 ilm 68
import org.apache.commons.dbutils.handlers.ArrayListHandler;
69
 
142 ilm 70
public class Updater_1_5 {
71
    private static final String COMPTE_CLIENT_TRANSACTION = "COMPTE_CLIENT_TRANSACTION";
72
 
73
    public static void update(final DBRoot root) throws SQLException {
74
        // Champ obsolete sur compte
75
        SQLTable tableCompte = root.getTable("COMPTE_PCE");
76
        if (!tableCompte.contains("OBSOLETE")) {
77
            final AlterTable alter = new AlterTable(tableCompte);
78
            alter.addBooleanColumn("OBSOLETE", Boolean.FALSE, false);
79
            tableCompte.getBase().getDataSource().execute(alter.asString());
80
            tableCompte.getSchema().updateVersion();
81
            tableCompte.fetchFields();
82
        }
83
 
180 ilm 84
        // Champ obsolete sur compte
85
        SQLTable tableTitre = root.getTable("TITRE_PERSONNEL");
86
        if (!tableTitre.contains("OBSOLETE")) {
87
            final AlterTable alter = new AlterTable(tableTitre);
88
            alter.addBooleanColumn("OBSOLETE", Boolean.FALSE, false);
89
            tableTitre.getBase().getDataSource().execute(alter.asString());
90
            tableTitre.getSchema().updateVersion();
91
            tableTitre.fetchFields();
92
            final UpdateBuilder updBuilder = new UpdateBuilder(tableTitre).setObject(tableTitre.getField("OBSOLETE"), Boolean.TRUE);
93
            updBuilder.setWhere(new Where(tableTitre.getField("CODE"), "=", "Mlle"));
94
            tableTitre.getBase().getDataSource().execute(updBuilder.asString());
95
            final UpdateBuilder updBuilder2 = new UpdateBuilder(tableTitre).setObject(tableTitre.getField("CODE"), "M.");
96
            updBuilder2.setWhere(new Where(tableTitre.getField("CODE"), "=", "Mr"));
97
 
98
            tableTitre.getBase().getDataSource().execute(updBuilder2.asString());
99
 
100
        }
101
 
142 ilm 102
        // Transaction du solde
103
        if (!root.contains(COMPTE_CLIENT_TRANSACTION)) {
104
            final SQLCreateTable createTable = new SQLCreateTable(root, COMPTE_CLIENT_TRANSACTION);
105
            createTable.addForeignColumn("CLIENT");
106
            createTable.addDateAndTimeColumn("DATE");
107
            createTable.addDecimalColumn("MONTANT", 16, 6, BigDecimal.valueOf(0), false);
108
            createTable.addForeignColumn("MODE_REGLEMENT");
109
            createTable.addForeignColumn("MOUVEMENT");
110
            try {
111
                root.getBase().getDataSource().execute(createTable.asString());
112
                InstallationPanel.insertUndef(createTable);
113
                root.refetchTable(COMPTE_CLIENT_TRANSACTION);
114
                root.getSchema().updateVersion();
115
            } catch (SQLException ex) {
116
                throw new IllegalStateException("Erreur lors de la création de la table " + COMPTE_CLIENT_TRANSACTION, ex);
117
            }
118
        }
119
        // Solde
120
        final SQLTable tClient = root.getTable("CLIENT");
121
        if (!tClient.contains("SOLDE_COMPTE")) {
122
            final AlterTable alterClient = new AlterTable(tClient);
123
            alterClient.addDecimalColumn("SOLDE_COMPTE", 16, 6, BigDecimal.valueOf(0), false);
124
            tClient.getBase().getDataSource().execute(alterClient.asString());
125
            tClient.getSchema().updateVersion();
126
            tClient.fetchFields();
127
        }
151 ilm 128
        if (!tClient.contains("DATE")) {
129
            final AlterTable alterClient = new AlterTable(tClient);
130
            alterClient.addColumn("DATE", "date");
131
            tClient.getBase().getDataSource().execute(alterClient.asString());
132
            tClient.getSchema().updateVersion();
133
            tClient.fetchFields();
134
        }
144 ilm 135
        if (!tClient.contains("COMMENTAIRES")) {
136
            final AlterTable alterClient = new AlterTable(tClient);
137
            alterClient.addVarCharColumn("COMMENTAIRES", 2048);
138
            tClient.getBase().getDataSource().execute(alterClient.asString());
139
            tClient.getSchema().updateVersion();
140
            tClient.fetchFields();
141
        }
142 ilm 142
 
143
        final SQLTable tCompteClient = root.getTable("COMPTE_CLIENT_TRANSACTION");
144
        if (!tCompteClient.contains("ID_MOUVEMENT")) {
145
            final AlterTable alterClient = new AlterTable(tCompteClient);
146
            alterClient.addForeignColumn("ID_MOUVEMENT", root.getTable("MOUVEMENT"));
147
            tClient.getBase().getDataSource().execute(alterClient.asString());
148
            tClient.getSchema().updateVersion();
149
            tClient.fetchFields();
150
        }
151
 
152
        final SQLTable tCmdClient = root.getTable("COMMANDE_CLIENT");
153
        if (!tCmdClient.contains("DATE_LIVRAISON_PREV")) {
154
            final AlterTable alterCmdClient = new AlterTable(tCmdClient);
155
            alterCmdClient.addColumn("DATE_LIVRAISON_PREV", "date");
156
            tCmdClient.getBase().getDataSource().execute(alterCmdClient.asString());
157
            tCmdClient.getSchema().updateVersion();
158
            tCmdClient.fetchFields();
159
        }
160
 
149 ilm 161
        if (!tClient.contains("CONDITIONS_LIVRAISON")) {
162
            final AlterTable alterClient = new AlterTable(tClient);
163
            alterClient.addVarCharColumn("CONDITIONS_LIVRAISON", 512);
164
            tClient.getBase().getDataSource().execute(alterClient.asString());
165
            tClient.getSchema().updateVersion();
166
            tClient.fetchFields();
167
        }
168
 
142 ilm 169
        {
170
            // Ajout du champ SANS_VALEUR_ENCAISSEMENT pour gérer les anciens cheques sans le compte
171
            // 511
172
            List<String> tablesCheque = Arrays.asList("CHEQUE_A_ENCAISSER", "CHEQUE_FOURNISSEUR");
173
 
174
            for (String string : tablesCheque) {
175
 
176
                final SQLTable table = root.getTable(string);
177
                if (!table.contains("SANS_VALEUR_ENCAISSEMENT")) {
178
                    AlterTable alterElt = new AlterTable(table);
179
                    alterElt.addBooleanColumn("SANS_VALEUR_ENCAISSEMENT", Boolean.FALSE, false);
180
                    table.getBase().getDataSource().execute(alterElt.asString());
181
                    root.refetchTable(string);
182
                    root.getSchema().updateVersion();
183
                    UpdateBuilder upBuilder = new UpdateBuilder(table);
184
                    upBuilder.setObject("SANS_VALEUR_ENCAISSEMENT", Boolean.TRUE);
185
                    table.getBase().getDataSource().execute(upBuilder.asString());
186
                }
187
            }
188
 
189
            SQLTable tableEncElt = root.getTable("ENCAISSER_MONTANT_ELEMENT");
190
            if (tableEncElt.getField("DATE").getType().getType() == Types.TIMESTAMP) {
191
                AlterTable t = new AlterTable(tableEncElt);
192
                t.alterColumn("DATE", EnumSet.allOf(Properties.class), "date", null, Boolean.TRUE);
193
                tableEncElt.getBase().getDataSource().execute(t.asString());
194
                root.refetchTable(tableEncElt.getName());
195
                root.getSchema().updateVersion();
196
            }
197
        }
198
 
199
        // TVA Intra
200
        final SQLTable tTva = root.getTable("TAXE");
201
        if (!tTva.contains("ID_COMPTE_PCE_COLLECTE_INTRA")) {
202
            final AlterTable alterTaxe = new AlterTable(tTva);
203
            alterTaxe.addForeignColumn("ID_COMPTE_PCE_COLLECTE_INTRA", root.getTable("COMPTE_PCE"));
204
            alterTaxe.addForeignColumn("ID_COMPTE_PCE_DED_INTRA", root.getTable("COMPTE_PCE"));
205
            tTva.getBase().getDataSource().execute(alterTaxe.asString());
206
            tTva.getSchema().updateVersion();
207
            tTva.fetchFields();
208
        }
209
 
210
        if (!root.contains("TAXE_COMPLEMENTAIRE")) {
211
            final SQLCreateTable createTable = new SQLCreateTable(root, "TAXE_COMPLEMENTAIRE");
212
            createTable.addForeignColumn("ID_COMPTE_PCE_PRODUITS", root.getTable("COMPTE_PCE"));
213
            createTable.addForeignColumn("ID_COMPTE_PCE", root.getTable("COMPTE_PCE"));
214
            createTable.addDecimalColumn("POURCENT", 16, 6, BigDecimal.valueOf(0), false);
215
            createTable.addVarCharColumn("CODE", 25);
216
            createTable.addVarCharColumn("NOM", 256);
217
 
218
            try {
219
                root.getBase().getDataSource().execute(createTable.asString());
220
                InstallationPanel.insertUndef(createTable);
221
                root.refetchTable("TAXE_COMPLEMENTAIRE");
222
                root.getSchema().updateVersion();
223
            } catch (SQLException ex) {
224
                throw new IllegalStateException("Erreur lors de la création de la table " + "TAXE_COMPLEMENTAIRE", ex);
225
            }
226
 
227
            SQLTable tableArt = root.getTable("ARTICLE");
228
            final AlterTable alterArt = new AlterTable(tableArt);
229
            alterArt.addForeignColumn("ID_TAXE_COMPLEMENTAIRE", root.getTable("TAXE_COMPLEMENTAIRE"));
230
            tableArt.getBase().getDataSource().execute(alterArt.asString());
231
            tableArt.getSchema().updateVersion();
232
            tableArt.fetchFields();
233
        }
234
 
235
        // GED
236
        if (!root.contains("ATTACHMENT")) {
237
            final SQLCreateTable createTable = new SQLCreateTable(root, "ATTACHMENT");
238
            createTable.addVarCharColumn("SOURCE_TABLE", 128);
239
            createTable.addIntegerColumn("SOURCE_ID", 0);
240
            createTable.addVarCharColumn("NAME", 256);
241
            createTable.addVarCharColumn("MIMETYPE", 256);
242
            createTable.addVarCharColumn("FILENAME", 256);
243
            createTable.addLongColumn("FILESIZE", 0L, false);
244
            createTable.addVarCharColumn("STORAGE_PATH", 256);
245
            createTable.addVarCharColumn("STORAGE_FILENAME", 256);
246
            createTable.addVarCharColumn("DIRECTORY", 256);
247
            createTable.addVarCharColumn("THUMBNAIL", 256);
248
            createTable.addIntegerColumn("THUMBNAIL_WIDTH", 32);
249
            createTable.addIntegerColumn("THUMBNAIL_HEIGHT", 32);
250
            createTable.addVarCharColumn("TAG", 128);
251
            createTable.addIntegerColumn("VERSION", 0);
252
            createTable.addVarCharColumn("HASH", 32);
180 ilm 253
            createTable.addBooleanColumn("ENCRYPTED", Boolean.FALSE, false);
142 ilm 254
            try {
255
                root.getBase().getDataSource().execute(createTable.asString());
256
                InstallationPanel.insertUndef(createTable);
257
                root.refetchTable("ATTACHMENT");
258
                root.getSchema().updateVersion();
259
            } catch (SQLException ex) {
260
                throw new IllegalStateException("Erreur lors de la création de la table " + "ATTACHMENT", ex);
261
            }
262
        }
263
        SQLTable tableAttachment = root.getTable("ATTACHMENT");
264
        if (!tableAttachment.contains("DIRECTORY")) {
265
            final AlterTable alter = new AlterTable(tableAttachment);
266
            alter.addVarCharColumn("STORAGE_FILENAME", 256);
267
            alter.addVarCharColumn("DIRECTORY", 256);
268
            tableAttachment.getBase().getDataSource().execute(alter.asString());
269
            tableAttachment.getSchema().updateVersion();
270
            tableAttachment.fetchFields();
271
        }
149 ilm 272
        if (!tableAttachment.contains("ID_PARENT")) {
273
            final AlterTable alter = new AlterTable(tableAttachment);
274
            alter.addForeignColumn("ID_PARENT", tableAttachment);
275
            tableAttachment.getBase().getDataSource().execute(alter.asString());
276
            tableAttachment.getSchema().updateVersion();
277
            tableAttachment.fetchFields();
278
        }
142 ilm 279
 
280
        if (!tableAttachment.contains("VERSION")) {
281
            final AlterTable alter = new AlterTable(tableAttachment);
282
            alter.addIntegerColumn("VERSION", 0);
283
            alter.addVarCharColumn("HASH", 32);
284
            alter.addVarCharColumn("INFOS", 8000);
285
            tableAttachment.getBase().getDataSource().execute(alter.asString());
286
            tableAttachment.getSchema().updateVersion();
287
            tableAttachment.fetchFields();
288
        }
180 ilm 289
        if (!tableAttachment.contains("ENCRYPTED")) {
290
            final AlterTable alter = new AlterTable(tableAttachment);
291
            alter.addBooleanColumn("ENCRYPTED", Boolean.FALSE, false);
292
            tableAttachment.getBase().getDataSource().execute(alter.asString());
293
            tableAttachment.getSchema().updateVersion();
294
            tableAttachment.fetchFields();
295
        }
151 ilm 296
        List<String> gedTable = Arrays.asList("CLIENT", "MOUVEMENT", "FOURNISSEUR", "ARTICLE", "FACTURE_FOURNISSEUR", "SAISIE_VENTE_FACTURE", "SALARIE");
142 ilm 297
        for (String string : gedTable) {
298
            SQLTable tableGED = root.getTable(string);
299
            if (!tableGED.contains("ATTACHMENTS")) {
300
                final AlterTable alter = new AlterTable(tableGED);
301
                alter.addIntegerColumn("ATTACHMENTS", 0);
302
                tableGED.getBase().getDataSource().execute(alter.asString());
303
                tableGED.getSchema().updateVersion();
304
                tableGED.fetchFields();
305
            }
306
        }
307
 
308
        // gestion articles en attente
309
        {
310
 
311
            // Vente
312
            SQLTable tableBLElt = root.getTable("BON_DE_LIVRAISON_ELEMENT");
313
            if (!tableBLElt.contains("ID_COMMANDE_CLIENT_ELEMENT")) {
314
                AlterTable t = new AlterTable(tableBLElt);
315
                t.addForeignColumn("ID_COMMANDE_CLIENT_ELEMENT", root.getTable("COMMANDE_CLIENT_ELEMENT"));
316
                tableBLElt.getBase().getDataSource().execute(t.asString());
317
                root.refetchTable(tableBLElt.getName());
318
                root.getSchema().updateVersion();
319
            }
320
 
321
            SQLTable tableVFElt = root.getTable("SAISIE_VENTE_FACTURE_ELEMENT");
322
            if (!tableVFElt.contains("ID_COMMANDE_CLIENT_ELEMENT")) {
323
                AlterTable t = new AlterTable(tableVFElt);
324
                t.addForeignColumn("ID_COMMANDE_CLIENT_ELEMENT", root.getTable("COMMANDE_CLIENT_ELEMENT"));
325
                tableVFElt.getBase().getDataSource().execute(t.asString());
326
                root.refetchTable(tableVFElt.getName());
327
                root.getSchema().updateVersion();
328
            }
329
 
330
            SQLTable tableCmdElt = root.getTable("COMMANDE_CLIENT_ELEMENT");
331
            if (!tableCmdElt.contains("LIVRE")) {
332
                AlterTable t = new AlterTable(tableCmdElt);
156 ilm 333
                // FIXME mis par défaut à true avant --> à fixer
142 ilm 334
                t.addBooleanColumn("LIVRE_FORCED", Boolean.FALSE, false);
151 ilm 335
                t.addBooleanColumn("LIVRE", Boolean.FALSE, false);
142 ilm 336
                t.addDecimalColumn("QTE_LIVREE", 16, 6, BigDecimal.ZERO, true);
337
                tableCmdElt.getBase().getDataSource().execute(t.asString());
338
                root.refetchTable(tableCmdElt.getName());
339
                root.getSchema().updateVersion();
340
 
341
                // String up = "UPDATE " + new SQLName(root.getName(),
342
                // tableCmdElt.getName()).quote()
343
                // + " SET \"QTE_LIVREE\"=\"QTE\"*\"QTE_UNITAIRE\", \"LIVRE_FORCED\"=true WHERE
344
                // \"ID_COMMANDE_CLIENT\" IN []";
156 ilm 345
                {
346
                    List<String> tablesWorkFlow = Arrays.asList("SAISIE_VENTE_FACTURE", "BON_DE_LIVRAISON");
347
                    for (String tableWorkFlow : tablesWorkFlow) {
348
 
349
                        SQLTable tableCmdCli = root.getTable("COMMANDE_CLIENT");
350
                        SQLTable tableTR = root.getTable("TR_COMMANDE_CLIENT");
351
                        SQLTable tableBL = root.getTable(tableWorkFlow);
352
                        // SQLTable tableFactC = root.getTable("SAISIE_VENTE_FACTURE");
353
                        String sel = "SELECT t.\"ID_COMMANDE_CLIENT\" FROM " + new SQLName(root.getName(), tableTR.getName()).quote() + " t ,"
354
                                + new SQLName(root.getName(), tableCmdCli.getName()).quote() + " c ," + new SQLName(root.getName(), tableBL.getName()).quote()
355
                                + " b WHERE c.\"ID\"=t.\"ID_COMMANDE_CLIENT\"" + " AND b.\"ID\"=t.\"ID_" + tableWorkFlow
356
                                + "\" AND b.\"ARCHIVE\" = 0 AND t.\"ID\" > 1 AND c.\"ID\" > 1 AND  c.\"ARCHIVE\" = 0 GROUP BY t.\"ID_COMMANDE_CLIENT\" HAVING (SUM(b.\""
357
                                + (tableWorkFlow.startsWith("BON") ? "TOTAL_HT" : "T_HT") + "\")>=SUM(c.\"T_HT\") ) ";
358
                        List<Integer> cmd = tableTR.getDBSystemRoot().getDataSource().executeCol(sel);
359
                        UpdateBuilder build = new UpdateBuilder(tableCmdElt);
360
                        build.set("QTE_LIVREE", "\"QTE\"*\"QTE_UNITAIRE\"");
361
                        build.setObject("LIVRE_FORCED", Boolean.TRUE);
362
                        final Where where = new Where(tableCmdElt.getField("ID_COMMANDE_CLIENT"), cmd);
363
                        build.setWhere(where);
364
 
365
                        tableTR.getDBSystemRoot().getDataSource().execute(build.asString());
366
                    }
367
                }
142 ilm 368
            }
151 ilm 369
            // Fix bad default value
370
            if (tableCmdElt.contains("LIVRE")) {
371
                AlterTable t = new AlterTable(tableCmdElt);
372
                t.alterColumnDefault("LIVRE", "false");
373
                tableCmdElt.getBase().getDataSource().execute(t.asString());
374
                root.refetchTable(tableCmdElt.getName());
375
                root.getSchema().updateVersion();
156 ilm 376
                UpdateBuilder upLivre = new UpdateBuilder(tableCmdElt);
377
                upLivre.setObject("LIVRE", Boolean.FALSE);
378
                upLivre.setWhere(new Where(tableCmdElt.getKey(), "=", 1));
379
 
380
                tableCmdElt.getBase().getDataSource().execute(upLivre.asString());
151 ilm 381
            }
382
 
142 ilm 383
            // Achat
384
 
385
            SQLTable tableBRElt = root.getTable("BON_RECEPTION_ELEMENT");
386
            if (!tableBRElt.contains("ID_COMMANDE_ELEMENT")) {
387
                AlterTable t = new AlterTable(tableBRElt);
388
                t.addForeignColumn("ID_COMMANDE_ELEMENT", root.getTable("COMMANDE_ELEMENT"));
389
                tableBRElt.getBase().getDataSource().execute(t.asString());
390
                root.refetchTable(tableBRElt.getName());
391
                root.getSchema().updateVersion();
392
            }
393
 
394
            SQLTable tableCmdFElt = root.getTable("COMMANDE_ELEMENT");
395
            if (!tableCmdFElt.contains("RECU")) {
396
                AlterTable t = new AlterTable(tableCmdFElt);
397
                t.addBooleanColumn("RECU_FORCED", Boolean.FALSE, false);
177 ilm 398
                t.addBooleanColumn("RECU", Boolean.FALSE, false);
142 ilm 399
                t.addDecimalColumn("QTE_RECUE", 16, 6, BigDecimal.ZERO, true);
400
                tableCmdFElt.getBase().getDataSource().execute(t.asString());
401
                root.refetchTable(tableCmdFElt.getName());
402
                root.getSchema().updateVersion();
403
 
404
                // String up = "UPDATE " + new SQLName(root.getName(),
405
                // tableCmdElt.getName()).quote()
406
                // + " SET \"QTE_LIVREE\"=\"QTE\"*\"QTE_UNITAIRE\", \"LIVRE_FORCED\"=true WHERE
407
                // \"ID_COMMANDE_CLIENT\" IN []";
156 ilm 408
                {
409
                    SQLTable tableCmdCli = root.getTable("COMMANDE");
410
                    SQLTable tableTR = root.getTable("TR_COMMANDE");
411
                    SQLTable tableBR = root.getTable("BON_RECEPTION");
412
                    String sel = "SELECT t.\"ID_COMMANDE\" FROM " + new SQLName(root.getName(), tableTR.getName()).quote() + " t ," + new SQLName(root.getName(), tableCmdCli.getName()).quote()
413
                            + " c ," + new SQLName(root.getName(), tableBR.getName()).quote() + " b WHERE c.\"ID\"=t.\"ID_COMMANDE\""
414
                            + " AND b.\"ID\"=t.\"ID_BON_RECEPTION\" AND b.\"ARCHIVE\" = 0 AND t.\"ID\" > 1 AND c.\"ID\" > 1 AND c.\"ARCHIVE\" = 0 GROUP BY t.\"ID_COMMANDE\" HAVING (SUM(b.\"TOTAL_HT\")>=SUM(c.\"T_HT\")) ";
415
 
416
                    System.err.println(sel);
417
                    List<Object> cmd = tableTR.getDBSystemRoot().getDataSource().executeCol(sel);
418
                    UpdateBuilder build = new UpdateBuilder(tableCmdFElt);
419
                    build.set("QTE_RECUE", "\"QTE\"*\"QTE_UNITAIRE\"");
420
                    build.setObject("RECU_FORCED", Boolean.TRUE);
421
                    final Where where = new Where(tableCmdFElt.getField("ID_COMMANDE"), cmd);
422
                    build.setWhere(where);
423
                    System.err.println(build.asString());
424
                    tableTR.getDBSystemRoot().getDataSource().execute(build.asString());
425
                }
177 ilm 426
            } else {
427
                // Fix bad default value
428
                String defaultValue = tableCmdFElt.getField("RECU").getDefaultValue();
429
                if (defaultValue != null && defaultValue.equals("true")) {
430
                    AlterTable t = new AlterTable(tableCmdFElt);
431
                    t.alterColumn("RECU", EnumSet.allOf(Properties.class), "boolean", "false", false);
432
                    tableCmdFElt.getBase().getDataSource().execute(t.asString());
433
                    root.refetchTable(tableCmdFElt.getName());
434
                    root.getSchema().updateVersion();
435
                    UpdateBuilder build = new UpdateBuilder(tableCmdFElt);
436
                    build.setObject("RECU", Boolean.FALSE);
437
 
438
                    Where w = Where.createRaw(tableCmdFElt.getField("QTE_RECUE").getQuotedName() + " < (" + tableCmdFElt.getField("QTE").getQuotedName() + "*"
439
                            + tableCmdFElt.getField("QTE_UNITAIRE").getQuotedName() + ")", tableCmdFElt.getField("QTE_UNITAIRE"), tableCmdFElt.getField("QTE"), tableCmdFElt.getField("QTE_RECUE"));
440
                    build.setWhere(w.or(new Where(tableCmdFElt.getKey(), "=", 1)));
441
                    System.err.println(build.asString());
442
                    tableCmdFElt.getDBSystemRoot().getDataSource().execute(build.asString());
443
                }
444
 
142 ilm 445
            }
446
 
156 ilm 447
            // Champ matière
448
            SQLTable tableArt = root.getTable("ARTICLE");
449
            if (!tableArt.contains("MATIERE")) {
450
                final AlterTable alter = new AlterTable(tableArt);
451
                alter.addVarCharColumn("MATIERE", 128);
452
                tableArt.getBase().getDataSource().execute(alter.asString());
453
                tableArt.getSchema().updateVersion();
454
                tableArt.fetchFields();
455
            }
456
 
142 ilm 457
        }
144 ilm 458
        // ----------------------- 1.5.1
142 ilm 459
 
144 ilm 460
        if (!root.contains("FABRICANT")) {
461
            final SQLCreateTable createTableEtat = new SQLCreateTable(root, "FABRICANT");
462
            createTableEtat.addVarCharColumn("NOM", 256);
463
            root.getBase().getDataSource().execute(createTableEtat.asString());
464
            InstallationPanel.insertUndef(createTableEtat);
465
            root.refetchTable("FABRICANT");
466
            root.getSchema().updateVersion();
467
 
468
            final AlterTable alterA = new AlterTable(root.getTable("ARTICLE"));
469
            alterA.addForeignColumn("ID_FABRICANT", root.findTable("FABRICANT"));
470
            root.getBase().getDataSource().execute(alterA.asString());
471
            root.refetchTable("ARTICLE");
472
            root.getSchema().updateVersion();
473
        }
474
 
475
        // CIM
476
        {
477
            if (!root.contains("ETAT_DEMANDE_ACHAT_ELEMENT")) {
478
                final SQLCreateTable createTableEtat = new SQLCreateTable(root, "ETAT_DEMANDE_ACHAT_ELEMENT");
479
                createTableEtat.addVarCharColumn("NOM", 256);
480
                createTableEtat.addIntegerColumn("COLOR", null, true);
481
                root.getBase().getDataSource().execute(createTableEtat.asString());
482
                InstallationPanel.insertUndef(createTableEtat);
483
                root.refetchTable("ETAT_DEMANDE_ACHAT_ELEMENT");
484
                root.getSchema().updateVersion();
485
 
486
                SQLRowValues rowVals = new SQLRowValues(root.getTable("ETAT_DEMANDE_ACHAT_ELEMENT"));
487
                rowVals.put("NOM", "En attente");
488
                rowVals.insert();
489
                rowVals.put("NOM", "Demande de prix");
490
                rowVals.insert();
491
                rowVals.put("NOM", "En commande");
492
                rowVals.insert();
493
                rowVals.put("NOM", "Réceptionnée");
494
                rowVals.insert();
495
                rowVals.put("NOM", "Réception partielle");
496
                rowVals.insert();
497
                rowVals.put("NOM", "A relancer");
498
                rowVals.insert();
499
            }
500
 
501
            if (!root.contains("DEMANDE_ACHAT_ELEMENT")) {
502
                final SQLCreateTable createTableDmd = new SQLCreateTable(root, "DEMANDE_ACHAT_ELEMENT");
503
                createTableDmd.addVarCharColumn("CODE", 256);
504
                createTableDmd.addVarCharColumn("NOM", 256);
505
                createTableDmd.addIntegerColumn("QTE", 1);
506
                createTableDmd.addDecimalColumn("QTE_UNITAIRE", 16, 6, BigDecimal.ONE, false);
507
 
508
                createTableDmd.addVarCharColumn("REPERE", 256);
509
                createTableDmd.addVarCharColumn("REFERENCE", 256);
510
                createTableDmd.addForeignColumn("FABRICANT");
511
                createTableDmd.addColumn("DATE", "date");
512
                createTableDmd.addForeignColumn("ETAT_DEMANDE_ACHAT_ELEMENT");
513
                createTableDmd.addForeignColumn("UNITE_VENTE");
514
                createTableDmd.addForeignColumn("ARTICLE");
515
                createTableDmd.addForeignColumn("FAMILLE_ARTICLE");
516
                createTableDmd.addForeignColumn("FOURNISSEUR");
517
                createTableDmd.addBooleanColumn("EN_STOCK", Boolean.FALSE, false);
518
 
519
                root.getBase().getDataSource().execute(createTableDmd.asString());
520
                InstallationPanel.insertUndef(createTableDmd);
521
                root.refetchTable("DEMANDE_ACHAT_ELEMENT");
522
                root.getSchema().updateVersion();
523
            }
524
 
525
            if (!root.contains("ETAT_DEMANDE_PRIX")) {
526
                final SQLCreateTable createTableEtat = new SQLCreateTable(root, "ETAT_DEMANDE_PRIX");
527
                createTableEtat.addVarCharColumn("NOM", 256);
528
                root.getBase().getDataSource().execute(createTableEtat.asString());
529
                InstallationPanel.insertUndef(createTableEtat);
530
                root.refetchTable("ETAT_DEMANDE_PRIX");
531
                root.getSchema().updateVersion();
532
 
533
                SQLRowValues rowVals = new SQLRowValues(root.getTable("ETAT_DEMANDE_PRIX"));
534
                rowVals.put("NOM", "En attente");
535
                rowVals.insert();
536
                rowVals.put("NOM", "Refusée");
537
                rowVals.insert();
538
                rowVals.put("NOM", "Acceptée");
539
                rowVals.insert();
540
                rowVals.put("NOM", "En cours");
541
                rowVals.insert();
542
            }
543
 
544
            if (!root.contains("DEMANDE_PRIX")) {
545
                final SQLCreateTable createTableDmd = new SQLCreateTable(root, "DEMANDE_PRIX");
546
                createTableDmd.addVarCharColumn("NUMERO", 256);
547
                createTableDmd.addVarCharColumn("OBJET", 1024);
548
                createTableDmd.addForeignColumn("FOURNISSEUR");
549
                // createTableDemande.addForeignColumn("ID_AFFAIRE",
550
                // ctxt.getRoot().findTable("AFFAIRE"));
551
                createTableDmd.addColumn("DATE", "date");
552
                // createTableDemandeM.addColumn("DATE_DISPOSITION", "date");
553
                createTableDmd.addColumn("DATE_BUTOIRE", "date");
554
                createTableDmd.addColumn("T_HT", "bigint DEFAULT 0");
555
                createTableDmd.addColumn("T_TVA", "bigint DEFAULT 0");
556
                createTableDmd.addColumn("T_TTC", "bigint DEFAULT 0");
557
                // createTableDemandeM.addForeignColumn("ID_TAXE", findTableTaxe);
558
 
559
                createTableDmd.addForeignColumn("COMMERCIAL");
560
                createTableDmd.addForeignColumn("ETAT_DEMANDE_PRIX");
561
                // createTableDemandeM.addForeignColumn("ID_ADRESSE",
562
                // ctxt.getRoot().findTable("ADRESSE"));
563
                createTableDmd.addVarCharColumn("INFOS", 1024);
564
 
565
                root.getBase().getDataSource().execute(createTableDmd.asString());
566
                InstallationPanel.insertUndef(createTableDmd);
567
                root.refetchTable("DEMANDE_PRIX");
568
                root.getSchema().updateVersion();
569
 
570
                final SQLCreateTable createTableDemandeMElt = new SQLCreateTable(root, "DEMANDE_PRIX_ELEMENT");
571
                createTableDemandeMElt.addVarCharColumn("NOM", 512);
572
                createTableDemandeMElt.addForeignColumn("DEMANDE_PRIX");
573
                createTableDemandeMElt.addForeignColumn("ID_TAXE", root.getTable("TAXE").getSQLName(), root.getTable("TAXE").getKey().getName(), "2");
574
 
575
                createTableDemandeMElt.addIntegerColumn("QTE", 0);
576
                createTableDemandeMElt.addColumn("VALEUR_METRIQUE_3", "real DEFAULT 0");
577
                createTableDemandeMElt.addColumn("PRIX_METRIQUE_VT_3", "bigint DEFAULT 0");
578
 
579
                createTableDemandeMElt.addDecimalColumn("PA_HT", 16, 6, BigDecimal.ZERO, false);
580
                createTableDemandeMElt.addDecimalColumn("PRIX_METRIQUE_HA_2", 16, 6, BigDecimal.ZERO, false);
581
 
582
                createTableDemandeMElt.addDecimalColumn("PRIX_METRIQUE_HA_1", 16, 6, BigDecimal.ZERO, false);
583
                createTableDemandeMElt.addDecimalColumn("T_PA_TTC", 16, 2, BigDecimal.ZERO, false);
584
                createTableDemandeMElt.addColumn("VALEUR_METRIQUE_1", "real DEFAULT 0");
585
                createTableDemandeMElt.addDecimalColumn("T_PA_HT", 16, 6, BigDecimal.ZERO, false);
586
                createTableDemandeMElt.addColumn("T_POIDS", "real DEFAULT 0");
587
                createTableDemandeMElt.addColumn("VALEUR_METRIQUE_2", "real DEFAULT 0");
588
                createTableDemandeMElt.addDecimalColumn("PRIX_METRIQUE_HA_3", 16, 6, BigDecimal.ZERO, false);
589
 
590
                createTableDemandeMElt.addDecimalColumn("PRIX_METRIQUE_VT_2", 16, 6, BigDecimal.ZERO, false);
591
                createTableDemandeMElt.addDecimalColumn("PRIX_METRIQUE_VT_1", 16, 6, BigDecimal.ZERO, false);
592
                createTableDemandeMElt.addDecimalColumn("T_PV_TTC", 16, 2, BigDecimal.ZERO, false);
593
                createTableDemandeMElt.addForeignColumn("ID_METRIQUE_1", root.findTable("METRIQUE"));
594
                SQLTable findTable = root.findTable("MODE_VENTE_ARTICLE");
595
                createTableDemandeMElt.addForeignColumn("ID_MODE_VENTE_ARTICLE", findTable.getSQLName(), findTable.getKey().getName(), String.valueOf(ReferenceArticleSQLElement.A_LA_PIECE));
596
                createTableDemandeMElt.addForeignColumn("ID_METRIQUE_3", root.findTable("METRIQUE"));
597
                createTableDemandeMElt.addForeignColumn("ID_STYLE", root.findTable("STYLE"));
598
                createTableDemandeMElt.addForeignColumn("ID_METRIQUE_2", root.findTable("METRIQUE"));
599
                createTableDemandeMElt.addIntegerColumn("QTE_ACHAT", 1);
600
                createTableDemandeMElt.addForeignColumn("ID_DEVISE", root.findTable("DEVISE"));
601
                createTableDemandeMElt.addForeignColumn("ID_FAMILLE_ARTICLE", root.findTable("FAMILLE_ARTICLE"));
602
 
603
                createTableDemandeMElt.addVarCharColumn("CODE", 256);
604
                createTableDemandeMElt.addColumn("SERVICE", "boolean DEFAULT false");
605
 
606
                createTableDemandeMElt.addDecimalColumn("T_PV_HT", 16, 6, BigDecimal.ZERO, false);
607
                createTableDemandeMElt.addDecimalColumn("PV_HT", 16, 6, BigDecimal.ZERO, false);
608
                createTableDemandeMElt.addColumn("POIDS", "real DEFAULT 0");
609
                createTableDemandeMElt.addDecimalColumn("PA_DEVISE", 16, 6, BigDecimal.ZERO, false);
610
                createTableDemandeMElt.addDecimalColumn("PA_DEVISE_T", 16, 6, BigDecimal.ZERO, false);
611
 
612
                createTableDemandeMElt.addForeignColumn("ID_ARTICLE", root.findTable("ARTICLE"));
613
                createTableDemandeMElt.addDecimalColumn("QTE_UNITAIRE", 16, 6, BigDecimal.ONE, false);
614
                createTableDemandeMElt.addForeignColumn("ID_UNITE_VENTE", root.findTable("UNITE_VENTE"));
615
                createTableDemandeMElt.addVarCharColumn("DESCRIPTIF", 2048);
616
                createTableDemandeMElt.addIntegerColumn("NIVEAU", 1);
156 ilm 617
                if (root.contains("DEPOT_STOCK")) {
618
                    createTableDemandeMElt.addForeignColumn("DEPOT_STOCK");
619
                }
144 ilm 620
                createTableDemandeMElt.addForeignColumn("ID_ECO_CONTRIBUTION", root.findTable("ECO_CONTRIBUTION"));
621
                createTableDemandeMElt.addDecimalColumn("ECO_CONTRIBUTION", 16, 2, BigDecimal.ZERO, false);
622
                createTableDemandeMElt.addDecimalColumn("T_ECO_CONTRIBUTION", 16, 2, BigDecimal.ZERO, false);
623
                createTableDemandeMElt.addForeignColumn("ID_DEMANDE_ACHAT_ELEMENT", root.findTable("DEMANDE_ACHAT_ELEMENT"));
624
 
625
                root.getBase().getDataSource().execute(createTableDemandeMElt.asString());
626
                InstallationPanel.insertUndef(createTableDemandeMElt);
627
                root.refetchTable("DEMANDE_PRIX_ELEMENT");
628
                root.getSchema().updateVersion();
629
 
630
                final AlterTable alterNumero = new AlterTable(root.getTable("NUMEROTATION_AUTO"));
631
                alterNumero.addVarCharColumn("DMD_PRIX_FORMAT", 128);
632
                alterNumero.addIntegerColumn("DMD_PRIX_START", 1);
633
                root.getBase().getDataSource().execute(alterNumero.asString());
634
                root.refetchTable("NUMEROTATION_AUTO");
635
                root.getSchema().updateVersion();
636
 
637
                final AlterTable alterCmd = new AlterTable(root.getTable("COMMANDE_ELEMENT"));
638
                alterCmd.addForeignColumn("ID_DEMANDE_ACHAT_ELEMENT", root.findTable("DEMANDE_ACHAT_ELEMENT"));
639
                root.getBase().getDataSource().execute(alterCmd.asString());
640
                root.refetchTable("COMMANDE_ELEMENT");
641
                root.getSchema().updateVersion();
642
 
643
                final AlterTable alterF = new AlterTable(root.getTable("FACTURE_FOURNISSEUR_ELEMENT"));
644
                alterF.addForeignColumn("ID_DEMANDE_ACHAT_ELEMENT", root.findTable("DEMANDE_ACHAT_ELEMENT"));
645
                root.getBase().getDataSource().execute(alterF.asString());
646
                root.refetchTable("FACTURE_FOURNISSEUR_ELEMENT");
647
                root.getSchema().updateVersion();
648
 
649
                final AlterTable alterB = new AlterTable(root.getTable("BON_RECEPTION_ELEMENT"));
650
                alterB.addForeignColumn("ID_DEMANDE_ACHAT_ELEMENT", root.findTable("DEMANDE_ACHAT_ELEMENT"));
651
                root.getBase().getDataSource().execute(alterB.asString());
652
                root.refetchTable("BON_RECEPTION_ELEMENT");
653
                root.getSchema().updateVersion();
654
            }
655
        }
656
 
657
        SQLTable tableDmdAChat = root.findTable("DEMANDE_ACHAT_ELEMENT");
658
        if (!tableDmdAChat.contains("ID_FAMILLE_ARTICLE")) {
659
            final AlterTable alterB = new AlterTable(tableDmdAChat);
660
            alterB.addForeignColumn("ID_FAMILLE_ARTICLE", root.findTable("FAMILLE_ARTICLE"));
661
            root.getBase().getDataSource().execute(alterB.asString());
662
            root.refetchTable("DEMANDE_ACHAT_ELEMENT");
663
            root.getSchema().updateVersion();
664
        }
665
        if (!tableDmdAChat.contains("ID_COMMANDE")) {
666
            final AlterTable alterB = new AlterTable(tableDmdAChat);
667
            alterB.addForeignColumn("ID_COMMANDE", root.findTable("COMMANDE"));
668
            root.getBase().getDataSource().execute(alterB.asString());
669
            root.refetchTable("DEMANDE_ACHAT_ELEMENT");
670
            root.getSchema().updateVersion();
671
        }
672
 
673
        if (!tableDmdAChat.contains("ID_BON_RECEPTION")) {
674
            final AlterTable alterB = new AlterTable(tableDmdAChat);
675
            alterB.addForeignColumn("ID_BON_RECEPTION", root.findTable("BON_RECEPTION"));
676
            root.getBase().getDataSource().execute(alterB.asString());
677
            root.refetchTable("DEMANDE_ACHAT_ELEMENT");
678
            root.getSchema().updateVersion();
679
        }
149 ilm 680
        if (!tableDmdAChat.contains("REPRISE")) {
681
            final AlterTable alterB = new AlterTable(tableDmdAChat);
682
            alterB.addBooleanColumn("REPRISE", Boolean.FALSE, false);
683
            root.getBase().getDataSource().execute(alterB.asString());
684
            root.refetchTable("DEMANDE_ACHAT_ELEMENT");
685
            root.getSchema().updateVersion();
686
        }
687
 
156 ilm 688
        if (!tableDmdAChat.contains("ID_COMMANDE")) {
689
            final AlterTable alterB = new AlterTable(tableDmdAChat);
690
            alterB.addForeignColumn("ID_COMMANDE", root.findTable("COMMANDE"));
691
            root.getBase().getDataSource().execute(alterB.asString());
692
            root.refetchTable("DEMANDE_ACHAT_ELEMENT");
693
            root.getSchema().updateVersion();
694
        }
695
 
696
        if (!tableDmdAChat.contains("REPRISE")) {
697
            final AlterTable alterB = new AlterTable(tableDmdAChat);
698
            alterB.addBooleanColumn("REPRISE", Boolean.FALSE, false);
699
            root.getBase().getDataSource().execute(alterB.asString());
700
            root.refetchTable("DEMANDE_ACHAT_ELEMENT");
701
            root.getSchema().updateVersion();
702
        }
703
 
149 ilm 704
        if (!tableDmdAChat.contains("IMPORT")) {
705
            final AlterTable alterB = new AlterTable(tableDmdAChat);
706
            alterB.addBooleanColumn("IMPORT", Boolean.FALSE, false);
707
            root.getBase().getDataSource().execute(alterB.asString());
708
            root.refetchTable("DEMANDE_ACHAT_ELEMENT");
709
            root.getSchema().updateVersion();
710
        }
711
 
156 ilm 712
        if (!tableDmdAChat.contains("ID_BON_RECEPTION")) {
713
            final AlterTable alterB = new AlterTable(tableDmdAChat);
714
            alterB.addForeignColumn("ID_BON_RECEPTION", root.findTable("BON_RECEPTION"));
715
            root.getBase().getDataSource().execute(alterB.asString());
716
            root.refetchTable("DEMANDE_ACHAT_ELEMENT");
717
            root.getSchema().updateVersion();
718
        }
144 ilm 719
        if (!tableDmdAChat.contains("QTE_RECUE")) {
720
            final AlterTable alterB = new AlterTable(tableDmdAChat);
721
            alterB.addIntegerColumn("QTE_RECUE", 0);
722
            root.getBase().getDataSource().execute(alterB.asString());
723
            root.refetchTable("DEMANDE_ACHAT_ELEMENT");
724
            root.getSchema().updateVersion();
725
        }
156 ilm 726
 
144 ilm 727
        if (!tableDmdAChat.contains("REPERE_SOURCE")) {
728
            final AlterTable alterB = new AlterTable(tableDmdAChat);
729
            alterB.addVarCharColumn("REPERE_SOURCE", 128);
730
            alterB.addVarCharColumn("CODE_SOURCE", 128);
731
            alterB.addVarCharColumn("NOM_SOURCE", 128);
732
            root.getBase().getDataSource().execute(alterB.asString());
733
            root.refetchTable("DEMANDE_ACHAT_ELEMENT");
734
            root.getSchema().updateVersion();
735
        }
736
        SQLTable tableEtatDmdAChat = root.findTable("ETAT_DEMANDE_ACHAT_ELEMENT");
737
        if (!tableEtatDmdAChat.contains("COLOR")) {
738
            final AlterTable alterB = new AlterTable(tableEtatDmdAChat);
739
            alterB.addIntegerColumn("COLOR", null, true);
740
            root.getBase().getDataSource().execute(alterB.asString());
741
            root.refetchTable("ETAT_DEMANDE_ACHAT_ELEMENT");
742
            root.getSchema().updateVersion();
743
        }
744
 
745
        if (!tableDmdAChat.contains("T_HT")) {
746
            final AlterTable alterB = new AlterTable(tableDmdAChat);
747
            alterB.addVarCharColumn("NUMERO_SE", 128);
748
            alterB.addVarCharColumn("DESCRIPTIF_SE", 512);
749
            alterB.addVarCharColumn("REVISION", 48);
750
            alterB.addDecimalColumn("P_HT", 16, 6, BigDecimal.ZERO, false);
751
            alterB.addDecimalColumn("T_HT", 16, 6, BigDecimal.ZERO, false);
752
            root.getBase().getDataSource().execute(alterB.asString());
753
            root.refetchTable("DEMANDE_ACHAT_ELEMENT");
754
            root.getSchema().updateVersion();
755
        }
756
 
757
        if (!root.contains("FACTURATION_COMMANDE_CLIENT")) {
758
            final SQLCreateTable createTableEtat = new SQLCreateTable(root, "FACTURATION_COMMANDE_CLIENT");
759
            createTableEtat.addVarCharColumn("NOM", 256);
760
            createTableEtat.addIntegerColumn("TYPE_FACTURE", TypeFactureCommandeClient.GLOBALE.getId());
761
            createTableEtat.addBooleanColumn("CHOICE", Boolean.FALSE, false);
762
            createTableEtat.addForeignColumn("TYPE_REGLEMENT");
763
            createTableEtat.addForeignColumn("COMMANDE_CLIENT");
764
            createTableEtat.addForeignColumn("SAISIE_VENTE_FACTURE");
765
            createTableEtat.addForeignColumn("MODELE");
766
            createTableEtat.addIntegerColumn("AJOURS", 0);
767
            createTableEtat.addDecimalColumn("MONTANT", 16, 8, BigDecimal.ZERO, false);
768
            createTableEtat.addDecimalColumn("POURCENT", 16, 8, BigDecimal.ZERO, false);
769
            createTableEtat.addBooleanColumn("COMPTANT", Boolean.FALSE, false);
770
            createTableEtat.addColumn("DATE_PREVISIONNELLE", "date");
771
            root.getBase().getDataSource().execute(createTableEtat.asString());
772
            InstallationPanel.insertUndef(createTableEtat);
773
            root.refetchTable("FACTURATION_COMMANDE_CLIENT");
774
            root.getSchema().updateVersion();
775
        }
776
        SQLTable tableFacturationCmd = root.getTable("FACTURATION_COMMANDE_CLIENT");
777
        if (!tableFacturationCmd.contains("FIN_MOIS")) {
778
            AlterTable alt = new AlterTable(tableFacturationCmd);
779
            alt.addBooleanColumn("FIN_MOIS", Boolean.FALSE, false);
780
            root.getBase().getDataSource().execute(alt.asString());
781
            root.refetchTable("FACTURATION_COMMANDE_CLIENT");
782
            root.getSchema().updateVersion();
783
        }
784
        if (!tableFacturationCmd.contains("LENJOUR")) {
785
            AlterTable alt = new AlterTable(tableFacturationCmd);
786
            alt.addIntegerColumn("LENJOUR", 0);
787
            root.getBase().getDataSource().execute(alt.asString());
788
            root.refetchTable("FACTURATION_COMMANDE_CLIENT");
789
            root.getSchema().updateVersion();
790
        }
156 ilm 791
 
144 ilm 792
        if (!root.contains("CHIFFRAGE_COMMANDE_CLIENT")) {
793
            final SQLCreateTable createTableEtat = new SQLCreateTable(root, "CHIFFRAGE_COMMANDE_CLIENT");
794
            createTableEtat.addVarCharColumn("NOM", 256);
795
            createTableEtat.addForeignColumn("COMMANDE_CLIENT");
796
            createTableEtat.addForeignColumn("UNITE_VENTE");
797
            createTableEtat.addForeignColumn("FAMILLE_ARTICLE");
798
            createTableEtat.addDecimalColumn("QTE", 16, 8, BigDecimal.ZERO, false);
799
            createTableEtat.addDecimalColumn("PA_HT", 16, 8, BigDecimal.ZERO, false);
800
            createTableEtat.addDecimalColumn("PV_HT", 16, 8, BigDecimal.ZERO, false);
801
            createTableEtat.addDecimalColumn("T_PV_HT", 16, 8, BigDecimal.ZERO, false);
802
            createTableEtat.addDecimalColumn("T_PA_HT", 16, 8, BigDecimal.ZERO, false);
803
            createTableEtat.addDecimalColumn("MARGE", 16, 8, BigDecimal.ZERO, false);
804
            root.getBase().getDataSource().execute(createTableEtat.asString());
805
            InstallationPanel.insertUndef(createTableEtat);
806
            root.refetchTable("CHIFFRAGE_COMMANDE_CLIENT");
807
            root.getSchema().updateVersion();
808
        }
809
 
810
        // VARIABLE_SALARIE
811
        SQLTable tableVarSal = root.findTable("VARIABLE_SALARIE");
812
        if (!tableVarSal.contains("HEURE_ABS_DEFAULT_VAL")) {
813
 
814
            final AlterTable alterB = new AlterTable(root.getTable("VARIABLE_SALARIE"));
815
 
816
            for (org.openconcerto.sql.model.SQLField sqlField : tableVarSal.getContentFields()) {
817
 
818
                String field = sqlField.getName();
819
                if (!field.equalsIgnoreCase("ID_USER_COMMON_CREATE") && !field.equalsIgnoreCase("ID_USER_COMMON_MODIFY") && !field.equalsIgnoreCase("MODIFICATION_DATE")
820
                        && !field.equalsIgnoreCase("CREATION_DATE") && !field.equalsIgnoreCase("HEURE_TRAV")) {
821
                    alterB.addColumn(field + "_DEFAULT_VAL", "real DEFAULT 0");
822
                }
823
            }
824
 
825
            root.getBase().getDataSource().execute(alterB.asString());
826
            root.refetchTable("VARIABLE_SALARIE");
827
            root.getSchema().updateVersion();
828
        }
829
 
830
        SQLTable tableFpaye = root.findTable("FICHE_PAYE");
831
        if (!tableFpaye.contains("TAXE_CM_SAL")) {
832
            final AlterTable alterB = new AlterTable(tableFpaye);
833
            alterB.addDecimalColumn("SAL_BASE_BRUT", 16, 2, BigDecimal.ZERO, false);
834
            alterB.addDecimalColumn("TAXE_CM_SAL", 16, 2, BigDecimal.ZERO, false);
835
            alterB.addDecimalColumn("TAXE_CM_PAT", 16, 2, BigDecimal.ZERO, false);
836
            alterB.addDecimalColumn("SAL_BRUT_COTISABLE", 16, 2, BigDecimal.ZERO, false);
837
            alterB.addDecimalColumn("SAL_BRUT_TAXABLE", 16, 2, BigDecimal.ZERO, false);
838
            root.getBase().getDataSource().execute(alterB.asString());
839
            root.refetchTable("FICHE_PAYE");
840
            root.getSchema().updateVersion();
841
 
842
            SQLTable tableElementPaye = root.getTable("FICHE_PAYE_ELEMENT");
843
 
844
            AlterTable tEltPaye = new AlterTable(tableElementPaye);
845
            tEltPaye.alterColumn("NOM", EnumSet.allOf(Properties.class), "varchar(512)", "''", false);
846
            tableElementPaye.getBase().getDataSource().execute(tEltPaye.asString());
847
            tableElementPaye.getSchema().updateVersion();
848
            tableElementPaye.fetchFields();
849
 
850
        }
851
 
177 ilm 852
        if (!tableFpaye.contains("CSG_REDUITE")) {
853
            final AlterTable alterB = new AlterTable(tableFpaye);
854
            alterB.addDecimalColumn("CSG_REDUITE", 16, 2, BigDecimal.ZERO, false);
855
            alterB.addDecimalColumn("SAL_BRUT_CSG", 16, 2, BigDecimal.ZERO, false);
856
            alterB.addDecimalColumn("SAL_BRUT_CSG_REDUITE", 16, 2, BigDecimal.ZERO, false);
857
            root.getBase().getDataSource().execute(alterB.asString());
858
            root.refetchTable("FICHE_PAYE");
859
            root.getSchema().updateVersion();
860
        }
861
 
144 ilm 862
        SQLTable tableCpaye = root.findTable("CUMULS_PAYE");
863
        if (!tableCpaye.contains("TAXE_CM_SAL_C")) {
864
            final AlterTable alterB = new AlterTable(tableCpaye);
865
            alterB.addDecimalColumn("TAXE_CM_SAL_C", 16, 2, BigDecimal.ZERO, false);
866
            alterB.addDecimalColumn("TAXE_CM_PAT_C", 16, 2, BigDecimal.ZERO, false);
867
            alterB.addDecimalColumn("SAL_BRUT_COTISABLE_C", 16, 2, BigDecimal.ZERO, false);
868
            alterB.addDecimalColumn("SAL_BRUT_TAXABLE_C", 16, 2, BigDecimal.ZERO, false);
869
            alterB.addDecimalColumn("SAL_BASE_BRUT_C", 16, 2, BigDecimal.ZERO, false);
870
            root.getBase().getDataSource().execute(alterB.asString());
871
            root.refetchTable("CUMULS_PAYE");
872
            root.getSchema().updateVersion();
873
        }
874
 
875
        SQLTable tableEtatCivil = root.findTable("ETAT_CIVIL");
876
        if (!tableEtatCivil.contains("NATIONNALITE")) {
877
            final AlterTable alterB = new AlterTable(tableEtatCivil);
878
            alterB.addVarCharColumn("NATIONNALITE", 256);
879
            alterB.addVarCharColumn("CNPS", 256);
880
            alterB.addVarCharColumn("TYPE_PIECE_IDENTITE", 256);
881
            alterB.addVarCharColumn("NUMERO_PIECE", 256);
882
            alterB.addVarCharColumn("NOM_PERE", 256);
883
            alterB.addVarCharColumn("NOM_MERE", 256);
884
            alterB.addVarCharColumn("CONJOINT_NOM", 256);
885
            alterB.addColumn("CONJOINT_DATE_NAISSANCE", "date");
886
            alterB.addVarCharColumn("CONJOINT_LIEU_NAISSANCE", 256);
887
            alterB.addVarCharColumn("CONJOINT_PROFESSION", 256);
888
            alterB.addColumn("DATE_DEBUT_PIECE", "date");
889
            alterB.addColumn("DATE_FIN_PIECE", "date");
890
            root.getBase().getDataSource().execute(alterB.asString());
891
            root.refetchTable("ETAT_CIVIL");
892
            root.getSchema().updateVersion();
893
        }
894
 
895
        // ARTICLE CLIENT
896
        if (!root.contains("ARTICLE_CODE_CLIENT")) {
897
            final SQLCreateTable createTableEtat = new SQLCreateTable(root, "ARTICLE_CODE_CLIENT");
898
            createTableEtat.addVarCharColumn("NOM", 512);
899
            createTableEtat.addVarCharColumn("CODE", 512);
900
            createTableEtat.addForeignColumn("CLIENT");
901
            createTableEtat.addForeignColumn("ARTICLE");
902
            root.getBase().getDataSource().execute(createTableEtat.asString());
903
            InstallationPanel.insertUndef(createTableEtat);
904
            root.refetchTable("ARTICLE_CODE_CLIENT");
905
            root.getSchema().updateVersion();
906
        }
907
        SQLTable tableClient = root.getTable("CLIENT");
908
        if (!tableClient.contains("CODE_FOURNISSEUR")) {
909
            final AlterTable alter = new AlterTable(tableClient);
910
            alter.addVarCharColumn("CODE_FOURNISSEUR", 256);
911
            tableCompte.getBase().getDataSource().execute(alter.asString());
912
            tableCompte.getSchema().updateVersion();
913
            tableCompte.fetchFields();
914
        }
915
        SQLTable tableArticle = root.getTable("ARTICLE");
916
        if (!tableArticle.contains("TARE")) {
917
            final AlterTable alterArticle = new AlterTable(tableArticle);
918
            alterArticle.addDecimalColumn("TARE", 16, 8, BigDecimal.ZERO, true);
919
            tableArticle.getBase().getDataSource().execute(alterArticle.asString());
920
            tableArticle.getSchema().updateVersion();
921
            tableArticle.fetchFields();
922
        }
923
        if (!tableArticle.contains("IFCO")) {
924
            final AlterTable alterArticle = new AlterTable(tableArticle);
925
            alterArticle.addIntegerColumn("IFCO", 0);
926
            tableArticle.getBase().getDataSource().execute(alterArticle.asString());
927
            tableArticle.getSchema().updateVersion();
928
            tableArticle.fetchFields();
929
        }
177 ilm 930
        if (!tableArticle.contains("DLC")) {
931
            final AlterTable alterArticle = new AlterTable(tableArticle);
932
            alterArticle.addColumn("DLC", "date");
933
            tableArticle.getBase().getDataSource().execute(alterArticle.asString());
934
            tableArticle.getSchema().updateVersion();
935
            tableArticle.fetchFields();
936
        }
144 ilm 937
        SQLTable tableFournisseur = root.getTable("FOURNISSEUR");
938
        if (!tableFournisseur.contains("NUMERO_TVA")) {
939
            final AlterTable alter = new AlterTable(tableFournisseur);
940
            alter.addVarCharColumn("NUMERO_TVA", 128);
941
            alter.addVarCharColumn("SIRET", 128);
942
            tableFournisseur.getBase().getDataSource().execute(alter.asString());
943
            tableFournisseur.getSchema().updateVersion();
944
            tableFournisseur.fetchFields();
945
        }
946
 
947
        // POS
149 ilm 948
        final SQLTable caisseT = root.getTable("CAISSE");
949
        final String registerLogTableName = "CAISSE_JOURNAL";
950
        if (!root.contains(registerLogTableName)) {
144 ilm 951
            final SQLTable receiptT = root.getTable("TICKET_CAISSE");
952
            final SQLTable userT = root.findTable("USER_COMMON", true);
953
 
149 ilm 954
            final SQLCreateTable createLogT = new SQLCreateTable(caisseT.getDBRoot(), registerLogTableName);
144 ilm 955
            createLogT.setCreateOrder(false);
956
            AddMDFields.addFields(createLogT, userT);
957
            createLogT.addForeignColumn("ID_CAISSE", caisseT);
958
            createLogT.addDateAndTimeColumn("DATE");
959
            createLogT.addForeignColumn("ID_USER", userT);
960
            createLogT.addVarCharColumn("EVT", 128);
961
            createLogT.addVarCharColumn("CREATOR", 128);
962
            createLogT.addVarCharColumn("CREATOR_VERSION", 128);
963
            createLogT.addUniqueConstraint("logSequence", Arrays.asList("ID_CAISSE", "DATE"));
964
 
965
            final SQLCreateTable createClotureT = new SQLCreateTable(caisseT.getDBRoot(), "CAISSE_CLOTURE");
966
            createClotureT.setCreateOrder(false);
967
            AddMDFields.addFields(createClotureT, userT);
968
            createClotureT.addForeignColumn("ID_ENTREE_JOURNAL", createLogT);
969
            createClotureT.addVarCharColumn("PERIODE", 32);
970
            final String dateType = createClotureT.getSyntax().getTypeNames(java.sql.Date.class).iterator().next();
971
            createClotureT.addColumn("DEBUT", dateType, null, false);
972
            createClotureT.addColumn("FIN", dateType, null, false);
149 ilm 973
            createClotureT.addDecimalColumn("TOTAL_TTC", 16, 6, BigDecimal.ZERO, false);
144 ilm 974
            // don't use foreign keys, we actually want redundant info so that we can check
975
            // coherence
976
            // nullable to allow for days without any sales
977
            createClotureT.addVarCharColumn("PREMIER_TICKET", 64, false, "null", true);
978
            createClotureT.addVarCharColumn("PREMIER_TICKET_HASH", 128, false, "null", true);
979
            createClotureT.addVarCharColumn("DERNIER_TICKET", 64, false, "null", true);
980
            createClotureT.addVarCharColumn("DERNIER_TICKET_HASH", 128, false, "null", true);
981
            createClotureT.addUniqueConstraint("uniqueness", Arrays.asList("ID_ENTREE_JOURNAL"));
982
 
983
            final AlterTable alterRegister = new AlterTable(caisseT);
984
            alterRegister.addForeignColumn("ID_DERNIERE_ENTREE_JOURNAL", createLogT);
985
            alterRegister.addForeignColumn("ID_DERNIERE_CLOTURE", createClotureT);
986
 
987
            final AlterTable alterReceipt = new AlterTable(receiptT);
988
            alterReceipt.addVarCharColumn("FILE_HASH", 128, false, "null", true);
989
            alterReceipt.addVarCharColumn("FILE_HASH_PREVIOUS", 128, false, "null", true);
990
            alterReceipt.addUniqueConstraint("uniqueNumber", new UniqueConstraintCreatorHelper(Arrays.asList("NUMERO"), TicketCaisseSQLElement.UNARCHIVED_WHERE) {
991
                @Override
992
                public Object getObject(SQLSyntax s) {
993
                    if (s.getSystem() == SQLSystem.H2) {
994
                        return TicketCaisseSQLElement.UniqueNumber_PartialUniqueTrigger.class;
995
                    } else {
996
                        return super.getObject(s);
997
                    }
998
                }
999
            });
1000
            alterReceipt.addUniqueConstraint("uniqueDate", new UniqueConstraintCreatorHelper(Arrays.asList("ID_CAISSE", "DATE"), TicketCaisseSQLElement.DATE_WHERE) {
1001
                @Override
1002
                public Object getObject(SQLSyntax s) {
1003
                    if (s.getSystem() == SQLSystem.H2) {
1004
                        return TicketCaisseSQLElement.UniqueDate_PartialUniqueTrigger.class;
1005
                    } else {
1006
                        return super.getObject(s);
1007
                    }
1008
                }
1009
            });
1010
 
1011
            root.createTables(createLogT, createClotureT);
1012
            root.getDBSystemRoot().getDataSource().execute(alterRegister.asString());
1013
            root.getDBSystemRoot().getDataSource().execute(alterReceipt.asString());
1014
            caisseT.getSchema().updateVersion();
1015
            alterRegister.getTable().fetchFields();
1016
            alterReceipt.getTable().fetchFields();
1017
        }
1018
 
149 ilm 1019
        SQLTable tableArt = root.getTable("ARTICLE");
1020
        if (!tableArt.contains("MASQUE_CAISSE")) {
1021
            final AlterTable alterArt = new AlterTable(tableArt);
1022
            alterArt.addBooleanColumn("MASQUE_CAISSE", Boolean.FALSE, false);
1023
            tableArt.getBase().getDataSource().execute(alterArt.asString());
1024
            tableArt.getSchema().updateVersion();
1025
            tableArt.fetchFields();
1026
        }
1027
 
151 ilm 1028
        if (!tableArt.contains("ADDITIONAL_TICKET_COPY")) {
1029
            final AlterTable alterArt = new AlterTable(tableArt);
1030
            alterArt.addBooleanColumn("ADDITIONAL_TICKET_COPY", Boolean.FALSE, false);
1031
            tableArt.getBase().getDataSource().execute(alterArt.asString());
1032
            tableArt.getSchema().updateVersion();
1033
            tableArt.fetchFields();
1034
        }
1035
 
149 ilm 1036
        SQLTable tableDevisAcompte = root.getTable("DEVIS");
1037
 
1038
        if (!tableDevisAcompte.contains("T_ACOMPTE")) {
1039
            final AlterTable alterB = new AlterTable(tableDevisAcompte);
1040
            alterB.addLongColumn("T_ACOMPTE", 0L, false);
1041
            root.getBase().getDataSource().execute(alterB.asString());
1042
            root.refetchTable("DEVIS");
1043
            root.getSchema().updateVersion();
1044
 
1045
            SQLTable tableEncaisse = root.getTable("ENCAISSER_MONTANT");
1046
            final AlterTable alterC = new AlterTable(tableEncaisse);
1047
            alterC.addBooleanColumn("ACOMPTE", Boolean.FALSE, false);
1048
            alterC.addForeignColumn("ID_DEVIS", tableDevisAcompte);
1049
            root.getBase().getDataSource().execute(alterC.asString());
1050
            root.refetchTable("ENCAISSER_MONTANT");
1051
            root.getSchema().updateVersion();
1052
 
1053
            SQLTable tableVF = root.getTable("SAISIE_VENTE_FACTURE");
1054
            final AlterTable alterF = new AlterTable(tableVF);
1055
            alterF.addLongColumn("T_ACOMPTE", 0L, false);
1056
            root.getBase().getDataSource().execute(alterF.asString());
1057
            root.refetchTable("SAISIE_VENTE_FACTURE");
1058
            root.getSchema().updateVersion();
1059
        }
1060
 
144 ilm 1061
        // Paye Simplifiée
1062
        if (!tableFpaye.contains("ALLEGEMENT_COTISATION")) {
1063
            final AlterTable alterB = new AlterTable(tableFpaye);
1064
            alterB.addDecimalColumn("ALLEGEMENT_COTISATION", 16, 2, BigDecimal.ZERO, false);
1065
            alterB.addDecimalColumn("CICE", 16, 2, BigDecimal.ZERO, false);
1066
            alterB.addDecimalColumn("AVANTAGE_NATURE", 16, 2, BigDecimal.ZERO, false);
1067
            alterB.addDecimalColumn("HEURE_TRAV", 16, 2, BigDecimal.ZERO, false);
1068
            root.getBase().getDataSource().execute(alterB.asString());
1069
            root.refetchTable("FICHE_PAYE");
1070
            root.getSchema().updateVersion();
1071
 
1072
            final AlterTable alterC = new AlterTable(tableCpaye);
1073
            alterC.addDecimalColumn("ALLEGEMENT_COTISATION_C", 16, 2, BigDecimal.ZERO, false);
1074
            alterC.addDecimalColumn("CICE_C", 16, 2, BigDecimal.ZERO, false);
1075
            alterC.addDecimalColumn("AVANTAGE_NATURE_C", 16, 2, BigDecimal.ZERO, false);
1076
            root.getBase().getDataSource().execute(alterC.asString());
1077
            root.refetchTable("CUMULS_PAYE");
1078
            root.getSchema().updateVersion();
1079
 
1080
        }
156 ilm 1081
 
144 ilm 1082
        if (!tableFpaye.contains("DETAILS_CONGES")) {
1083
            final AlterTable alterB = new AlterTable(tableFpaye);
1084
            alterB.addVarCharColumn("DETAILS_CONGES", 512);
1085
            root.getBase().getDataSource().execute(alterB.asString());
1086
            root.refetchTable("FICHE_PAYE");
1087
            root.getSchema().updateVersion();
1088
        }
1089
 
1090
        SQLTable tableFPayeElt = root.getTable("FICHE_PAYE_ELEMENT");
1091
        if (!tableFPayeElt.contains("ID_STYLE")) {
1092
            final AlterTable alterB = new AlterTable(tableFPayeElt);
1093
            alterB.addForeignColumn("ID_STYLE", root.getTable("STYLE"));
1094
            root.getBase().getDataSource().execute(alterB.asString());
1095
            root.refetchTable("FICHE_PAYE_ELEMENT");
1096
            root.getSchema().updateVersion();
1097
        }
1098
 
177 ilm 1099
        boolean upVar = false;
1100
        List<String> f = Arrays.asList("IJSS_BRUT", "IJSS_NET", "FRAIS_PRO", "RBT_TRANSPORT", "HEURE_CHOM", "TAUX_CHOM", "HEURE_INDEM", "ECRETEMENT_CSG", "IJSS_BRUT_SECU_PAS");
1101
        final AlterTable alterVarSal = new AlterTable(tableVarSal);
1102
        for (String field : f) {
1103
            if (!tableVarSal.contains(field)) {
1104
                upVar = true;
1105
                alterVarSal.addColumn(field, "real DEFAULT 0");
1106
                alterVarSal.addColumn(field + "_DEFAULT_VAL", "real DEFAULT 0");
1107
                alterVarSal.addColumn(field + "_CUMUL_VAL", "real DEFAULT 0");
144 ilm 1108
            }
177 ilm 1109
        }
1110
        if (upVar) {
1111
            root.getBase().getDataSource().execute(alterVarSal.asString());
144 ilm 1112
            root.refetchTable(tableVarSal.getName());
1113
            root.getSchema().updateVersion();
1114
        }
1115
 
1116
        SQLTable tableDevis = root.getTable("DEVIS");
1117
        if (!tableDevis.contains("ID_TAXE_PORT")) {
1118
            final AlterTable alterB = new AlterTable(tableDevis);
1119
            alterB.addForeignColumn("ID_TAXE_PORT", root.getTable("TAXE"));
1120
            root.getBase().getDataSource().execute(alterB.asString());
1121
            root.refetchTable(tableDevis.getName());
1122
            root.getSchema().updateVersion();
1123
        }
149 ilm 1124
 
1125
        // 1.5.3
1126
        List<String> tableRemiseF = Arrays.asList("COMMANDE", "BON_RECEPTION", "FACTURE_FOURNISSEUR");
1127
        for (String t : tableRemiseF) {
1128
 
1129
            final SQLTable tableCommande = root.getTable(t);
1130
            AlterTable tCommande = new AlterTable(tableCommande);
1131
            boolean updateCmd = false;
1132
            if (!tableCommande.contains("ID_TAXE_PORT")) {
1133
                updateCmd = true;
1134
                tCommande.addForeignColumn("ID_TAXE_PORT", root.getTable("TAXE"));
1135
            }
1136
 
1137
            if (!tableCommande.contains("PORT_HT")) {
1138
                updateCmd = true;
1139
                tCommande.addLongColumn("PORT_HT", Long.valueOf(0), false);
1140
            }
1141
 
1142
            if (!tableCommande.contains("REMISE_HT")) {
1143
                updateCmd = true;
1144
                tCommande.addLongColumn("REMISE_HT", Long.valueOf(0), false);
1145
            }
1146
 
1147
            if (updateCmd) {
1148
                tableCommande.getBase().getDataSource().execute(tCommande.asString());
1149
                tableCommande.getSchema().updateVersion();
1150
                tableCommande.fetchFields();
1151
            }
1152
        }
1153
 
1154
        List<String> tableElement = Arrays.asList("FACTURE_FOURNISSEUR_ELEMENT", "COMMANDE_ELEMENT", "BON_RECEPTION_ELEMENT", "DEMANDE_PRIX_ELEMENT");
1155
        for (String tableName : tableElement) {
1156
            final SQLTable table = root.getTable(tableName);
1157
            if (!table.contains("ID_FAMILLE_ARTICLE")) {
1158
                AlterTable t = new AlterTable(table);
1159
                t.addForeignColumn("ID_FAMILLE_ARTICLE", root.getTable("FAMILLE_ARTICLE"));
1160
                table.getBase().getDataSource().execute(t.asString());
1161
                root.refetchTable(table.getName());
1162
                root.getSchema().updateVersion();
1163
            }
1164
        }
1165
 
1166
        final SQLTable tableBlElt = root.getTable("BON_RECEPTION_ELEMENT");
1167
        if (!tableBlElt.contains("QTE_ORIGINE")) {
1168
            AlterTable t = new AlterTable(tableBlElt);
1169
            t.addIntegerColumn("QTE_ORIGINE", null, true);
1170
            tableBlElt.getBase().getDataSource().execute(t.asString());
1171
            root.refetchTable(tableBlElt.getName());
1172
            root.getSchema().updateVersion();
1173
        }
1174
        // Caisse 1.5.3
1175
        if (!root.getTable(registerLogTableName).contains("HOST_NAME")) {
1176
            final AlterTable alterRegisterLog = new AlterTable(root.getTable(registerLogTableName));
1177
            // to check for shared register installations
1178
            alterRegisterLog.addVarCharColumn("HOST_NAME", 128, true, null, true);
1179
            alterRegisterLog.addVarCharColumn("HOST_USER", 128, true, null, true);
1180
 
1181
            // no longer needed since we need more than the last entry, just remove the duplicate
1182
            // data and select from CAISSE_JOURNAL
1183
            final AlterTable alterRegister = new AlterTable(caisseT);
1184
            alterRegister.dropColumn("ID_DERNIERE_ENTREE_JOURNAL");
1185
 
1186
            for (final String sql : ChangeTable.cat(Arrays.asList(alterRegisterLog, alterRegister))) {
1187
                root.getDBSystemRoot().getDataSource().execute(sql);
1188
            }
1189
            caisseT.getSchema().updateVersion();
1190
            alterRegisterLog.getTable().fetchFields();
1191
            alterRegister.getTable().fetchFields();
1192
        }
1193
        // Prefs compte AN
1194
        SQLTable table = root.findTable("PREFS_COMPTE");
1195
 
1196
        if (!table.getFieldsName().contains("ID_JOURNAL_AN")) {
151 ilm 1197
            AlterTable t = new AlterTable(table);
149 ilm 1198
            t.addForeignColumn("ID_JOURNAL_AN", root.getTable("JOURNAL"));
1199
            t.addBooleanColumn("CREATE_NUL_SOLDE_ECR", Boolean.TRUE, false);
1200
            table.getBase().getDataSource().execute(t.asString());
1201
            table.getSchema().updateVersion();
1202
            table.fetchFields();
1203
        }
1204
 
177 ilm 1205
        if (!table.getFieldsName().contains("ID_JOURNAL_CB_ATTENTE")) {
1206
            AlterTable t = new AlterTable(table);
1207
            t.addForeignColumn("ID_JOURNAL_CB_ATTENTE", root.getTable("JOURNAL"));
1208
            table.getBase().getDataSource().execute(t.asString());
1209
            table.getSchema().updateVersion();
1210
            table.fetchFields();
1211
        }
1212
 
1213
        if (!table.getFieldsName().contains("ID_COMPTE_PCE_CB_ATTENTE")) {
1214
            AlterTable t = new AlterTable(table);
1215
            t.addForeignColumn("ID_COMPTE_PCE_CB_ATTENTE", root.getTable("COMPTE_PCE"));
1216
            table.getBase().getDataSource().execute(t.asString());
1217
            table.getSchema().updateVersion();
1218
            table.fetchFields();
1219
        }
1220
 
151 ilm 1221
        if (!table.getFieldsName().contains("AUTO_LETTRAGE")) {
1222
            AlterTable t = new AlterTable(table);
1223
            t.addBooleanColumn("AUTO_LETTRAGE", Boolean.FALSE, false);
1224
            table.getBase().getDataSource().execute(t.asString());
1225
            table.getSchema().updateVersion();
1226
            table.fetchFields();
1227
        }
156 ilm 1228
        // Etat stock
1229
        if (!root.contains("ETAT_STOCK")) {
151 ilm 1230
 
156 ilm 1231
            try {
1232
                final SQLCreateTable createTable = new SQLCreateTable(root, "ETAT_STOCK");
1233
                createTable.addColumn("DATE", "date");
1234
                createTable.addDecimalColumn("MONTANT_HA", 16, 8, BigDecimal.ZERO, true);
1235
                createTable.addDecimalColumn("MONTANT_VT", 16, 8, BigDecimal.ZERO, true);
1236
                createTable.addBooleanColumn("INVENTAIRE", Boolean.FALSE, false);
1237
                root.getBase().getDataSource().execute(createTable.asString());
1238
                InstallationPanel.insertUndef(createTable);
1239
                root.refetchTable("ETAT_STOCK");
1240
                root.getSchema().updateVersion();
1241
 
1242
                final SQLCreateTable createTableElt = new SQLCreateTable(root, "ETAT_STOCK_ELEMENT");
1243
                createTableElt.addForeignColumn("ARTICLE");
1244
                createTableElt.addForeignColumn("ETAT_STOCK");
1245
                createTableElt.addDecimalColumn("PA", 16, 8, BigDecimal.ZERO, true);
1246
                createTableElt.addDecimalColumn("PV", 16, 8, BigDecimal.ZERO, true);
1247
                createTableElt.addDecimalColumn("T_PA", 16, 8, BigDecimal.ZERO, true);
1248
                createTableElt.addDecimalColumn("T_PV", 16, 8, BigDecimal.ZERO, true);
1249
                createTableElt.addDecimalColumn("QTE", 16, 8, BigDecimal.ZERO, true);
1250
                createTableElt.addDecimalColumn("ECART", 16, 8, BigDecimal.ZERO, true);
1251
                createTableElt.addVarCharColumn("CODE", 256);
1252
                createTableElt.addVarCharColumn("NOM", 2048);
1253
 
1254
                root.getBase().getDataSource().execute(createTableElt.asString());
1255
                InstallationPanel.insertUndef(createTableElt);
1256
                root.refetchTable("ETAT_STOCK_ELEMENT");
1257
                root.getSchema().updateVersion();
1258
 
1259
            } catch (SQLException ex) {
1260
                throw new IllegalStateException("Erreur lors de la création de la table ETAT_STOCK_ELEMENT", ex);
1261
            }
1262
        } else {
1263
            final SQLTable tableEtatStock = root.getTable("ETAT_STOCK_ELEMENT");
1264
            SQLField fieldNom = tableEtatStock.getField("NOM");
1265
            int size = fieldNom.getType().getSize();
1266
            if (size == 512) {
1267
                AlterTable alter = new AlterTable(tableEtatStock);
1268
                alter.alterColumn("NOM", EnumSet.allOf(Properties.class), "varchar(" + 2048 + ")", "''", false);
1269
                tableEtatStock.getBase().getDataSource().execute(alter.asString());
1270
                tableEtatStock.getSchema().updateVersion();
1271
                tableEtatStock.fetchFields();
1272
            }
1273
 
1274
        }
1275
        SQLTable tableMvtStock = root.getTable("MOUVEMENT_STOCK");
1276
        if (!tableMvtStock.contains("ID_ETAT_STOCK")) {
1277
            final AlterTable alter = new AlterTable(tableMvtStock);
1278
            alter.addBooleanColumn("CLOTURE", Boolean.FALSE, false);
1279
            alter.addBooleanColumn("OUVERTURE", Boolean.FALSE, false);
1280
            alter.addForeignColumn("ID_ETAT_STOCK", root.getTable("ETAT_STOCK"));
1281
            tableMvtStock.getBase().getDataSource().execute(alter.asString());
1282
            tableMvtStock.getSchema().updateVersion();
1283
            tableMvtStock.fetchFields();
1284
        }
149 ilm 1285
        SQLTable tableEcr = root.getTable("ECRITURE");
1286
        if (!tableEcr.contains("CLOTURE")) {
1287
            final AlterTable alter = new AlterTable(tableEcr);
1288
            alter.addBooleanColumn("CLOTURE", Boolean.FALSE, false);
1289
            alter.addBooleanColumn("RAN", Boolean.FALSE, false);
1290
            tableEcr.getBase().getDataSource().execute(alter.asString());
1291
            tableEcr.getSchema().updateVersion();
1292
            tableEcr.fetchFields();
1293
        }
1294
 
1295
        // Jour Km Element
1296
        final SQLTable tkmElt = root.getTable("SAISIE_KM_ELEMENT");
1297
        if (!tkmElt.contains("JOUR")) {
1298
            final AlterTable alterKmElt = new AlterTable(tkmElt);
1299
            alterKmElt.addIntegerColumn("JOUR", 1);
1300
            tkmElt.getBase().getDataSource().execute(alterKmElt.asString());
1301
            tkmElt.getSchema().updateVersion();
1302
            tkmElt.fetchFields();
1303
        }
151 ilm 1304
        // Ref bancaires fournisseurs et clients
1305
        for (final SQLTable bankT : Arrays.asList(tableFournisseur, tClient)) {
1306
            if (!bankT.contains("IBAN")) {
1307
                final AlterTable alter = new AlterTable(bankT);
1308
                alter.addVarCharColumn("IBAN", 128);
1309
                alter.addVarCharColumn("BIC", 128);
1310
                bankT.getDBSystemRoot().getDataSource().execute(alter.asString());
1311
                bankT.getSchema().updateVersion();
1312
                bankT.fetchFields();
1313
            }
149 ilm 1314
        }
151 ilm 1315
        final SQLTable typeReglT = root.getTable("TYPE_REGLEMENT");
1316
        if (typeReglT.getRow(TypeReglementSQLElement.PRELEVEMENT) == null) {
1317
            final SQLRowValues directDebitVals = new SQLRowValues(typeReglT).put("NOM", "Prélèvement");
1318
            directDebitVals.put("COMPTANT", Boolean.FALSE).put("ECHEANCE", Boolean.FALSE);
1319
            directDebitVals.setID(TypeReglementSQLElement.PRELEVEMENT).insertVerbatim();
1320
        }
1321
        if (!tableClient.contains("ACCEPTE_EMAIL")) {
1322
            final AlterTable alter = new AlterTable(tableClient);
1323
            alter.addBooleanColumn("ACCEPTE_EMAIL", Boolean.FALSE, false);
1324
            alter.addBooleanColumn("ACCEPTE_COURRIER", Boolean.FALSE, false);
1325
            alter.addBooleanColumn("ACCEPTE_SMS", Boolean.FALSE, false);
1326
            alter.addBooleanColumn("ACCEPTE_TEL", Boolean.FALSE, false);
1327
            exec(alter);
1328
        }
1329
        final SQLTable contactT = root.getTable("CONTACT");
156 ilm 1330
        if (contactT != null && !contactT.contains("DATE_NAISSANCE")) {
151 ilm 1331
            final AlterTable alter = new AlterTable(contactT);
1332
            alter.addColumn("DATE_NAISSANCE", "date");
1333
            exec(alter);
1334
        }
156 ilm 1335
 
151 ilm 1336
        final SQLCreateTable createSDDMsgTable = SDDMessageSQLElement.getCreateTable(root);
1337
        if (createSDDMsgTable != null) {
1338
            final SQLCreateTable createMandate = SEPAMandateSQLElement.getCreateTable(root);
1339
            root.createTables(createSDDMsgTable, createMandate);
1340
            final SQLTable msgT = root.getTable(createSDDMsgTable.getName());
1341
            final SQLTable mandateT = root.getTable(createMandate.getName());
1342
 
1343
            final AlterTable alterFact = new AlterTable(root.getTable(SaisieVenteFactureSQLElement.TABLENAME));
1344
            alterFact.addForeignColumn(SaisieVenteFactureSQLElement.MESSAGE_FIELD_NAME, msgT);
1345
            alterFact.addVarCharColumn(SaisieVenteFactureSQLElement.END2END_FIELD_NAME, 35);
1346
 
1347
            final AlterTable alterModeRegl = new AlterTable(root.getTable("MODE_REGLEMENT"));
1348
            alterModeRegl.addForeignColumn(null, mandateT);
1349
 
1350
            for (final String sql : ChangeTable.cat(Arrays.asList(alterFact, alterModeRegl))) {
1351
                root.getDBSystemRoot().getDataSource().execute(sql);
1352
            }
1353
            root.getSchema().updateVersion();
1354
            root.refetch(CollectionUtils.createSet(alterFact.getName(), alterModeRegl.getName()));
1355
            root.setMetadata(SDDMessageSQLElement.SERIAL_MD, "0");
156 ilm 1356
        } else {
1357
            // Before r24495 InstallationPanel.fixUnboundedVarchar() would change this field to
1358
            // varchar
1359
            final SQLField xmlField = root.getTable(SDDMessageSQLElement.TABLE_NAME).getField("XML");
1360
            if (xmlField.getTypeDecl().contains("char")) {
1361
                final AlterTable alterTable = new AlterTable(xmlField.getTable());
1362
                alterTable.alterColumn(xmlField.getName(), EnumSet.of(Properties.TYPE), alterTable.getSyntax().getTypeNames(Clob.class).iterator().next(), null, null);
1363
                root.getDBSystemRoot().getDataSource().execute(alterTable.asString());
1364
                root.getSchema().updateVersion();
1365
                xmlField.getTable().fetchFields();
1366
            }
151 ilm 1367
        }
1368
 
1369
        final SQLTable vcT = root.getTable("SAISIE_VENTE_COMPTOIR");
1370
        if (!vcT.contains("ID_COMPTE_PCE_PRODUIT")) {
1371
            final AlterTable alter = new AlterTable(vcT);
1372
            alter.addForeignColumn("ID_COMPTE_PCE_PRODUIT", root.getTable("COMPTE_PCE"));
1373
            alter.addForeignColumn("ID_COMPTE_PCE_SERVICE", root.getTable("COMPTE_PCE"));
1374
            exec(alter);
1375
        }
1376
 
156 ilm 1377
        // 1.6
1378
        // gestion depots des cheques
1379
        String chequeDepotTable = "DEPOT_CHEQUE";
1380
        if (!root.contains(chequeDepotTable)) {
1381
 
1382
            // Table depot cheque
1383
            final SQLCreateTable createTableDepotCheque = new SQLCreateTable(root, chequeDepotTable);
1384
            createTableDepotCheque.addVarCharColumn("NOM", 512);
1385
            createTableDepotCheque.addLongColumn("MONTANT", 0L, false);
1386
            createTableDepotCheque.addColumn("DATE", "date");
1387
            if (root.contains("BANQUE")) {
1388
                createTableDepotCheque.addForeignColumn("ID_BANQUE", root.getTable("BANQUE"));
1389
            } else if (root.contains("BANQUE_POLE_PRODUIT")) {
1390
                createTableDepotCheque.addForeignColumn("ID_BANQUE", root.getTable("BANQUE_POLE_PRODUIT"));
1391
            }
1392
            createTableDepotCheque.addForeignColumn("ID_ECRITURE", root.getTable("ECRITURE"));
1393
            createTableDepotCheque.addForeignColumn("ID_MOUVEMENT", root.getTable("MOUVEMENT"));
1394
            root.getBase().getDataSource().execute(createTableDepotCheque.asString());
1395
            InstallationPanel.insertUndef(createTableDepotCheque);
1396
            root.refetchTable(chequeDepotTable);
1397
            root.getSchema().updateVersion();
1398
 
1399
            // Table depot cheque element
1400
            final SQLCreateTable createTableDepotElt = new SQLCreateTable(root, chequeDepotTable + "_ELEMENT");
1401
            createTableDepotElt.addVarCharColumn("NUMERO", 128);
1402
            createTableDepotElt.addVarCharColumn("BANQUE", 128);
1403
            createTableDepotElt.addVarCharColumn("PIECE", 512);
1404
            createTableDepotElt.addLongColumn("MONTANT", 0L, false);
1405
            createTableDepotElt.addColumn("DATE", "date");
1406
            createTableDepotElt.addForeignColumn("ID_CHEQUE_A_ENCAISSER", root.getTable("CHEQUE_A_ENCAISSER"));
1407
            createTableDepotElt.addForeignColumn("ID_ECRITURE", root.getTable("ECRITURE"));
1408
            createTableDepotElt.addForeignColumn("ID_CLIENT", root.getTable("CLIENT"));
1409
            createTableDepotElt.addForeignColumn("ID_DEPOT_CHEQUE", root.getTable(chequeDepotTable));
1410
            root.getBase().getDataSource().execute(createTableDepotElt.asString());
1411
            InstallationPanel.insertUndef(createTableDepotElt);
1412
            root.refetchTable(chequeDepotTable + "_ELEMENT");
1413
            root.getSchema().updateVersion();
1414
        }
1415
 
1416
        final SQLTable articleT = root.getTable("ARTICLE");
1417
        if (!articleT.contains("QTE_UNITAIRE")) {
1418
            final AlterTable alter = new AlterTable(articleT);
1419
            alter.addDecimalColumn("QTE_UNITAIRE", 16, 6, BigDecimal.ONE, false);
1420
            exec(alter);
1421
        }
1422
 
1423
        final SQLTable articleFT = root.getTable("ARTICLE_FOURNISSEUR");
1424
        if (!articleFT.contains("QTE_UNITAIRE")) {
1425
            final AlterTable alter = new AlterTable(articleFT);
1426
            alter.addDecimalColumn("QTE_UNITAIRE", 16, 6, BigDecimal.ONE, false);
1427
            exec(alter);
1428
        }
1429
 
1430
        // Cout de revient
1431
        if (!root.contains("COUT_REVIENT")) {
1432
            final SQLCreateTable createTableCR = new SQLCreateTable(root, "COUT_REVIENT");
1433
            createTableCR.addVarCharColumn("CODE", 48);
1434
            createTableCR.addVarCharColumn("NOM", 256);
1435
            createTableCR.addDecimalColumn("POURCENT", 16, 6, BigDecimal.ZERO, false);
1436
            root.getBase().getDataSource().execute(createTableCR.asString());
1437
            InstallationPanel.insertUndef(createTableCR);
1438
            root.refetchTable("COUT_REVIENT");
1439
            root.getSchema().updateVersion();
1440
 
1441
            final AlterTable alterArticle = new AlterTable(root.getTable("ARTICLE"));
1442
            alterArticle.addForeignColumn("ID_COUT_REVIENT", root.findTable("COUT_REVIENT"));
1443
            exec(alterArticle);
1444
            root.refetchTable("ARTICLE");
1445
            root.getSchema().updateVersion();
1446
        }
1447
 
1448
        if (!root.getTable("ARTICLE").contains("MARGE_WITH_COUT_REVIENT")) {
1449
            final AlterTable alterArticle = new AlterTable(root.getTable("ARTICLE"));
1450
            alterArticle.addBooleanColumn("MARGE_WITH_COUT_REVIENT", Boolean.FALSE, false);
1451
            exec(alterArticle);
1452
            root.refetchTable("ARTICLE");
1453
            root.getSchema().updateVersion();
1454
        }
1455
 
1456
        if (!root.getTable("FACTURE_FOURNISSEUR").contains("NET_A_PAYER")) {
1457
            final AlterTable alterFactF = new AlterTable(root.getTable("FACTURE_FOURNISSEUR"));
1458
            alterFactF.addLongColumn("NET_A_PAYER", 0L, false);
1459
            alterFactF.addLongColumn("AVOIR_TTC", 0L, false);
1460
            exec(alterFactF);
1461
            root.refetchTable("FACTURE_FOURNISSEUR");
1462
            root.getSchema().updateVersion();
1463
        }
1464
 
1465
        // Frais Document
1466
        if (!root.contains("FRAIS_DOCUMENT")) {
1467
            final SQLCreateTable createTableCR = new SQLCreateTable(root, "FRAIS_DOCUMENT");
1468
            createTableCR.addVarCharColumn("CODE", 48);
1469
            createTableCR.addVarCharColumn("NOM", 256);
1470
            createTableCR.addLongColumn("MONTANT_HT", 0L, false);
1471
            createTableCR.addForeignColumn("ID_TAXE", root.getTable("TAXE"));
1472
            createTableCR.addLongColumn("MONTANT_TTC", 0L, false);
1473
            root.getBase().getDataSource().execute(createTableCR.asString());
1474
            InstallationPanel.insertUndef(createTableCR);
1475
            root.refetchTable("FRAIS_DOCUMENT");
1476
 
1477
            final AlterTable alterClient = new AlterTable(root.getTable("CLIENT"));
1478
            alterClient.addForeignColumn("ID_FRAIS_DOCUMENT", root.findTable("FRAIS_DOCUMENT"));
1479
            exec(alterClient);
1480
            root.refetchTable("CLIENT");
1481
 
1482
            List<String> tables = Arrays.asList("DEVIS", "COMMANDE_CLIENT", "BON_DE_LIVRAISON", "SAISIE_VENTE_FACTURE");
1483
            for (String tableToUp : tables) {
1484
                final AlterTable alter = new AlterTable(root.getTable(tableToUp));
1485
                alter.addLongColumn("FRAIS_DOCUMENT_HT", 0L, false);
1486
                alter.addForeignColumn("ID_TAXE_FRAIS_DOCUMENT", root.getTable("TAXE"));
1487
                exec(alter);
1488
                root.refetchTable(tableToUp);
1489
            }
1490
            root.getSchema().updateVersion();
1491
        }
1492
 
1493
        final SQLTable chequeAvoirT = root.getTable("CHEQUE_AVOIR_CLIENT");
1494
 
1495
        String tBanque = root.contains("BANQUE") ? "BANQUE" : "BANQUE_POLE_PRODUIT";
1496
        if (!chequeAvoirT.contains("ID_" + tBanque)) {
1497
 
1498
            final AlterTable alterChqAvoir = new AlterTable(chequeAvoirT);
1499
            alterChqAvoir.addForeignColumn("ID_" + tBanque, root.findTable(tBanque));
1500
            exec(alterChqAvoir);
1501
        }
1502
 
1503
        // Prelevement à la source
1504
        final SQLTable fichePayeT = root.getTable("FICHE_PAYE");
1505
        if (!fichePayeT.contains("ID_PAS")) {
1506
            final SQLCreateTable createTablePas = new SQLCreateTable(root, "PAS");
1507
            createTablePas.addForeignColumn("ID_TYPE_TAUX_PAS", root.findTable("TYPE_TAUX_PAS"));
1508
            createTablePas.addVarCharColumn("CODE_PAS", 256);
1509
            createTablePas.addDecimalColumn("TAUX_PAS", 16, 2, BigDecimal.ZERO, false);
1510
            root.getBase().getDataSource().execute(createTablePas.asString());
1511
            InstallationPanel.insertUndef(createTablePas);
1512
            root.refetchTable("PAS");
1513
            root.getSchema().updateVersion();
1514
 
1515
            final AlterTable alterFichePaye = new AlterTable(fichePayeT);
1516
            alterFichePaye.addForeignColumn("ID_PAS", root.findTable("PAS"));
1517
            alterFichePaye.addDecimalColumn("TOTAL_PAS", 16, 2, BigDecimal.ZERO, false);
1518
            alterFichePaye.addDecimalColumn("NET_AVANT_PAS", 16, 2, BigDecimal.ZERO, false);
1519
            exec(alterFichePaye);
1520
 
1521
            final SQLTable tableSal = root.getTable("SALARIE");
1522
            final AlterTable alterSalarie = new AlterTable(tableSal);
1523
            alterSalarie.addForeignColumn("ID_PAS", root.findTable("PAS"));
1524
            exec(alterSalarie);
1525
 
1526
            final AlterTable alterEtatCivil = new AlterTable(tableEtatCivil);
1527
            alterEtatCivil.addVarCharColumn("NTT", 50);
1528
            exec(alterEtatCivil);
1529
 
1530
            Map<String, SQLRow> typeTauxPasMap = new HashMap<>();
1531
            SQLSelect selTypeTaux = new SQLSelect();
1532
            selTypeTaux.addSelect(root.findTable("TYPE_TAUX_PAS").getKey());
1533
            selTypeTaux.addSelect(root.findTable("TYPE_TAUX_PAS").getField("CODE"));
1534
            List<SQLRow> resultTypeTaux = SQLRowListRSH.execute(selTypeTaux);
1535
            for (SQLRow sqlRow : resultTypeTaux) {
1536
                typeTauxPasMap.put(sqlRow.getString("CODE"), sqlRow);
1537
            }
1538
 
1539
            SQLSelect selSal = new SQLSelect();
1540
            selSal.addSelect(tableSal.getKey());
1541
            List<SQLRow> rowSal = SQLRowListRSH.execute(selSal);
1542
            for (SQLRow sqlRow : rowSal) {
1543
                SQLRowValues rowValsSal = sqlRow.createEmptyUpdateRow();
1544
                final SQLRowValues putRowValues = rowValsSal.putRowValues("ID_PAS");
1545
                putRowValues.put("ID_TYPE_TAUX_PAS", typeTauxPasMap.get("13").getID());
1546
                putRowValues.put("TAUX_PAS", BigDecimal.ZERO);
1547
                putRowValues.commit();
1548
            }
1549
        }
1550
 
1551
        if (!root.contains("ARTICLE_TARIF_FOURNISSEUR")) {
1552
            final SQLCreateTable createTable = new SQLCreateTable(root, "ARTICLE_TARIF_FOURNISSEUR");
1553
 
1554
            createTable.addForeignColumn("ARTICLE");
1555
            createTable.addForeignColumn("FOURNISSEUR");
1556
            createTable.addVarCharColumn("REF_FOURNISSEUR", 500);
1557
            createTable.addVarCharColumn("TYPE_REAPPRO", 256);
1558
            createTable.addVarCharColumn("CONDITIONS", 1024);
1559
            createTable.addVarCharColumn("ACHETEUR", 256);
1560
            createTable.addVarCharColumn("CODE_PAYS_ORIGINE", 256);
1561
            createTable.addDecimalColumn("PRIX_ACHAT_DEVISE_F", 16, 6, BigDecimal.valueOf(0), false);
1562
            createTable.addDecimalColumn("PRIX_ACHAT", 16, 6, BigDecimal.valueOf(0), false);
1563
            createTable.addIntegerColumn("QTE", 1);
1564
            createTable.addDateAndTimeColumn("DATE_PRIX");
1565
            // createTable.addDecimalColumn("COEF_TRANSPORT_PORT", 16, 6, BigDecimal.valueOf(0),
1566
            // false);
1567
            // createTable.addDecimalColumn("COEF_TAXE_D", 16, 6, BigDecimal.valueOf(0), false);
1568
            // createTable.addDecimalColumn("COEF_TRANSPORT_SIEGE", 16, 6, BigDecimal.valueOf(0),
1569
            // false);
1570
            // createTable.addDecimalColumn("COEF_FRAIS_MOULE", 16, 6, BigDecimal.valueOf(0),
1571
            // false);
1572
            // createTable.addDecimalColumn("COEF_FRAIS_INDIRECTS", 16, 6, BigDecimal.valueOf(0),
1573
            // false);
1574
            createTable.addIntegerColumn("DELAI_REAPPRO", 0);
1575
            createTable.addIntegerColumn("DELAI_TRANSPORT", 0);
1576
            createTable.addIntegerColumn("PRIORITE", 0);
1577
            createTable.addDecimalColumn("COEF_PRIX_MINI", 16, 2, BigDecimal.valueOf(0), false);
1578
            root.getBase().getDataSource().execute(createTable.asString());
1579
            InstallationPanel.insertUndef(createTable);
1580
            root.refetchTable("ARTICLE_TARIF_FOURNISSEUR");
1581
            root.getSchema().updateVersion();
1582
        }
1583
 
1584
        // Gestion des catègories comptables
1585
        if (!root.contains("CATEGORIE_COMPTABLE")) {
1586
            final SQLCreateTable createTable = new SQLCreateTable(root, "CATEGORIE_COMPTABLE");
1587
            createTable.addVarCharColumn("NOM", 128);
1588
            createTable.addForeignColumn("ID_TAXE_VENTE", root.getTable("TAXE"));
1589
            createTable.addForeignColumn("ID_TAXE_ACHAT", root.getTable("TAXE"));
1590
            createTable.addForeignColumn("ID_COMPTE_PCE_VENTE", root.getTable("COMPTE_PCE"));
1591
            createTable.addForeignColumn("ID_COMPTE_PCE_ACHAT", root.getTable("COMPTE_PCE"));
1592
 
1593
            try {
1594
                root.getBase().getDataSource().execute(createTable.asString());
1595
                InstallationPanel.insertUndef(createTable);
1596
                root.refetchTable("CATEGORIE_COMPTABLE");
1597
                root.getSchema().updateVersion();
1598
            } catch (SQLException ex) {
1599
                throw new IllegalStateException("Erreur lors de la création de la table " + "CATEGORIE_COMPTABLE", ex);
1600
            }
1601
 
1602
            AlterTable tableClientCat = new AlterTable(root.getTable("CLIENT"));
1603
            tableClientCat.addForeignColumn("ID_CATEGORIE_COMPTABLE", root.getTable("CATEGORIE_COMPTABLE"));
1604
            root.getBase().getDataSource().execute(tableClientCat.asString());
1605
            root.refetchTable("CLIENT");
1606
            root.getSchema().updateVersion();
1607
 
1608
            AlterTable tableF = new AlterTable(root.getTable("FOURNISSEUR"));
1609
            tableF.addForeignColumn("ID_CATEGORIE_COMPTABLE", root.getTable("CATEGORIE_COMPTABLE"));
1610
            root.getBase().getDataSource().execute(tableF.asString());
1611
            root.refetchTable("FOURNISSEUR");
1612
            root.getSchema().updateVersion();
1613
 
1614
            final SQLCreateTable createTableArtCat = new SQLCreateTable(root, "ARTICLE_CATEGORIE_COMPTABLE");
1615
            createTableArtCat.addForeignColumn("ID_CATEGORIE_COMPTABLE", root.getTable("CATEGORIE_COMPTABLE"));
1616
            createTableArtCat.addForeignColumn("ID_ARTICLE", root.getTable("ARTICLE"));
1617
            createTableArtCat.addForeignColumn("ID_TAXE_ACHAT", root.getTable("TAXE"));
1618
            createTableArtCat.addForeignColumn("ID_TAXE_VENTE", root.getTable("TAXE"));
1619
            createTableArtCat.addForeignColumn("ID_COMPTE_PCE_VENTE", root.getTable("COMPTE_PCE"));
1620
            createTableArtCat.addForeignColumn("ID_COMPTE_PCE_ACHAT", root.getTable("COMPTE_PCE"));
1621
 
1622
            try {
1623
                root.getBase().getDataSource().execute(createTableArtCat.asString());
1624
                InstallationPanel.insertUndef(createTableArtCat);
1625
                root.refetchTable("CATEGORIE_COMPTABLE");
1626
                root.getSchema().updateVersion();
1627
            } catch (SQLException ex) {
1628
                throw new IllegalStateException("Erreur lors de la création de la table " + "ARTICLE_CATEGORIE_COMPTABLE", ex);
1629
            }
1630
 
1631
        }
1632
 
1633
        // Article fournisseur secondaire
1634
        if (!root.contains("ARTICLE_FOURNISSEUR_SECONDAIRE")) {
1635
            final SQLCreateTable createTable = new SQLCreateTable(root, "ARTICLE_FOURNISSEUR_SECONDAIRE");
1636
            createTable.addForeignColumn("ARTICLE");
1637
            createTable.addForeignColumn("FOURNISSEUR");
1638
            try {
1639
                root.getBase().getDataSource().execute(createTable.asString());
1640
                InstallationPanel.insertUndef(createTable);
1641
                root.refetchTable("ARTICLE_FOURNISSEUR_SECONDAIRE");
1642
                root.getSchema().updateVersion();
1643
            } catch (SQLException ex) {
1644
                throw new IllegalStateException("Erreur lors de la création de la table ARTICLE_FOURNISSEUR_SECONDAIRE", ex);
1645
            }
1646
        }
1647
 
1648
        if (!root.getTable("ARTICLE").contains("AUTO_PRIX_ACHAT_NOMENCLATURE")) {
1649
            AlterTable t = new AlterTable(root.getTable("ARTICLE"));
1650
            t.addBooleanColumn("AUTO_PRIX_ACHAT_NOMENCLATURE", false, false);
1651
            root.getTable("ARTICLE").getBase().getDataSource().execute(t.asString());
1652
            root.refetchTable(root.getTable("ARTICLE").getName());
1653
            root.getSchema().updateVersion();
1654
        }
1655
 
1656
        // Gestion multidepot
1657
        {
1658
            // Ajout table depot
1659
            if (!root.contains("DEPOT_STOCK")) {
1660
                final SQLCreateTable createTable = new SQLCreateTable(root, "DEPOT_STOCK");
1661
                createTable.addVarCharColumn("CODE", 25);
1662
                createTable.addVarCharColumn("NOM", 256);
1663
                createTable.addVarCharColumn("UI_LOCK", 256);
1664
                createTable.addForeignColumn("ID_USER_UI_LOCK", root.findTable("USER_COMMON"));
1665
                // sqlRowValues.put("UI_LOCK", "ro");
1666
                // sqlRowValues.put("ID_USER_UI_LOCK", 2);
1667
 
1668
                try {
1669
                    root.getBase().getDataSource().execute(createTable.asString());
1670
                    InstallationPanel.insertUndef(createTable);
1671
 
1672
                    root.refetchTable("DEPOT_STOCK");
1673
                    root.getSchema().updateVersion();
1674
 
1675
                    SQLRowValues rowValsDefStock = new SQLRowValues(root.getTable("DEPOT_STOCK"));
1676
                    rowValsDefStock.put("NOM", "Principal");
1677
                    rowValsDefStock.put("UI_LOCK", "ro");
1678
                    rowValsDefStock.commit();
1679
                } catch (SQLException ex) {
1680
                    throw new IllegalStateException("Erreur lors de la création de la table " + "DEPOT_STOCK", ex);
1681
                }
1682
            }
1683
 
1684
            List<String> tableElementDepot = Arrays.asList("FACTURE_FOURNISSEUR_ELEMENT", "DEVIS_ELEMENT", "COMMANDE_ELEMENT", "BON_RECEPTION_ELEMENT", "COMMANDE_CLIENT_ELEMENT",
1685
                    "BON_DE_LIVRAISON_ELEMENT", "SAISIE_VENTE_FACTURE_ELEMENT", "AVOIR_CLIENT_ELEMENT", "DEMANDE_PRIX_ELEMENT");
1686
            for (String tableName : tableElementDepot) {
1687
                final SQLTable tableToAddDepot = root.getTable(tableName);
1688
                if (!tableToAddDepot.contains("ID_DEPOT_STOCK")) {
1689
                    AlterTable t = new AlterTable(tableToAddDepot);
1690
                    t.addForeignColumn("ID_DEPOT_STOCK", root.getTable("DEPOT_STOCK"));
1691
                    tableToAddDepot.getBase().getDataSource().execute(t.asString());
1692
                    root.refetchTable(tableToAddDepot.getName());
1693
                    root.getSchema().updateVersion();
1694
                }
1695
            }
1696
 
1697
            // Depot defaut
1698
            if (!root.getTable("ARTICLE").contains("ID_DEPOT_STOCK")) {
1699
                AlterTable t = new AlterTable(root.getTable("ARTICLE"));
1700
                t.addForeignColumn("ID_DEPOT_STOCK", root.getTable("DEPOT_STOCK"));
1701
                root.getTable("ARTICLE").getBase().getDataSource().execute(t.asString());
1702
                root.refetchTable(root.getTable("ARTICLE").getName());
1703
                root.getSchema().updateVersion();
1704
            }
1705
 
1706
            // Liason depot stock
1707
            if (!root.getTable("STOCK").contains("ID_DEPOT_STOCK")) {
1708
                AlterTable t = new AlterTable(root.getTable("STOCK"));
1709
                t.addForeignColumn("ID_DEPOT_STOCK", root.getTable("DEPOT_STOCK"));
1710
                t.addForeignColumn("ID_ARTICLE", root.getTable("ARTICLE"));
1711
                t.addColumn("QTE_MIN", "real DEFAULT 0");
1712
                root.getTable("STOCK").getBase().getDataSource().execute(t.asString());
1713
                root.refetchTable(root.getTable("STOCK").getName());
1714
                root.getSchema().updateVersion();
1715
            }
1716
 
1717
            // Ajout depot sur mvt
1718
            if (!root.getTable("MOUVEMENT_STOCK").contains("ID_STOCK")) {
1719
                AlterTable t = new AlterTable(root.getTable("MOUVEMENT_STOCK"));
1720
                t.addForeignColumn("ID_STOCK", root.getTable("STOCK"));
1721
                t.addIntegerColumn("SOURCE_ELEMENTID", 1);
1722
                root.getTable("MOUVEMENT_STOCK").getBase().getDataSource().execute(t.asString());
1723
                root.refetchTable(root.getTable("MOUVEMENT_STOCK").getName());
1724
                root.getSchema().updateVersion();
1725
 
1726
                // REQUETE update STOCK.ID_ARTICLE et QTE_MIN
1727
                SQLTable tableStock = root.getTable("STOCK");
1728
                UpdateBuilder buildStockArt = new UpdateBuilder(tableStock);
1729
                buildStockArt.addBackwardVirtualJoin(tableArticle, "ID_STOCK");
1730
                buildStockArt.setFromVirtualJoinField("ID_ARTICLE", tableArticle.getAlias(), "ID");
1731
                buildStockArt.setFromVirtualJoinField("QTE_MIN", tableArticle.getAlias(), "QTE_MIN");
1732
                buildStockArt.setWhere(new Where(tableStock.getKey(), "!=", 1));
1733
 
1734
                // REQUETE UPDATE MVT ID_STOCK
1735
                UpdateBuilder buildMvtStock = new UpdateBuilder(tableMvtStock);
1736
                buildMvtStock.addForwardVirtualJoin(tableArticle, "ID_ARTICLE");
1737
                buildMvtStock.setFromVirtualJoinField("ID_STOCK", tableArticle.getAlias(), "ID_STOCK");
1738
 
1739
                // REQUETE UPDATE STOCK ID_DEPOT_STOCK
1740
                UpdateBuilder buildStock = new UpdateBuilder(tableStock);
1741
                buildStock.setObject("ID_DEPOT_STOCK", DepotStockSQLElement.DEFAULT_ID);
1742
 
1743
                UpdateBuilder buildArticleDepot = new UpdateBuilder(tableArticle);
1744
                buildArticleDepot.setObject("ID_DEPOT_STOCK", DepotStockSQLElement.DEFAULT_ID);
1745
 
1746
                // REQUETE UPDATE ARTICLE_DEPOT_STOCK
1747
                // String req = "INSERT INTO " + root.getTable("ARTICLE_STOCK").getSQLName().quote()
1748
                // + " (" + root.getTable("ARTICLE_STOCK").getField("ID_ARTICLE").getQuotedName() +
1749
                // ",";
1750
                // req += root.getTable("ARTICLE_STOCK").getField("ID_STOCK").getQuotedName() + ", "
1751
                // + root.getTable("ARTICLE_STOCK").getField("QTE_MIN").getQuotedName() + ") SELECT
1752
                // "
1753
                // + root.getTable("ARTICLE").getKey().getQuotedName() + "," +
1754
                // root.getTable("ARTICLE").getField("ID_STOCK").getQuotedName() + ", "
1755
                // + root.getTable("ARTICLE").getField("QTE_MIN").getQuotedName();
1756
                // req += " FROM " + root.getTable("ARTICLE").getSQLName().quote();
1757
                // req += " WHERE " + root.getTable("ARTICLE").getKey().getQuotedName() + " > 1 AND
1758
                // " + root.getTable("ARTICLE").getField("ID_STOCK").getQuotedName() + " >1 ";
1759
                // req += " AND " + root.getTable("ARTICLE").getArchiveField().getQuotedName() + " =
1760
                // 0";
1761
 
1762
                tableStock.getDBSystemRoot().getDataSource().execute(buildArticleDepot.asString());
1763
                tableStock.getDBSystemRoot().getDataSource().execute(buildMvtStock.asString());
1764
                tableStock.getDBSystemRoot().getDataSource().execute(buildStock.asString());
1765
                tableStock.getDBSystemRoot().getDataSource().execute(buildStockArt.asString());
1766
            }
1767
 
1768
        }
1769
 
1770
        // Gestion Livraison depuis devis
1771
        SQLTable tableTrDevis = root.getTable("TR_DEVIS");
1772
        if (!tableTrDevis.contains("ID_BON_DE_LIVRAISON")) {
1773
            AlterTable t = new AlterTable(tableTrDevis);
1774
            t.addForeignColumn("ID_BON_DE_LIVRAISON", root.getTable("BON_DE_LIVRAISON"));
1775
            tableTrDevis.getBase().getDataSource().execute(t.asString());
1776
            root.refetchTable(tableTrDevis.getName());
1777
            root.getSchema().updateVersion();
1778
        }
1779
        SQLTable tableBLElt = root.getTable("BON_DE_LIVRAISON_ELEMENT");
1780
        if (!tableBLElt.contains("ID_DEVIS_ELEMENT")) {
1781
            AlterTable t = new AlterTable(tableBLElt);
1782
            t.addForeignColumn("ID_DEVIS_ELEMENT", root.getTable("DEVIS_ELEMENT"));
1783
            tableBLElt.getBase().getDataSource().execute(t.asString());
1784
            root.refetchTable(tableBLElt.getName());
1785
            root.getSchema().updateVersion();
1786
        }
1787
        SQLTable tableDevisElt = root.getTable("DEVIS_ELEMENT");
1788
        if (!tableDevisElt.contains("QTE_LIVREE")) {
1789
            AlterTable t = new AlterTable(tableDevisElt);
1790
            t.addBooleanColumn("LIVRE_FORCED", Boolean.FALSE, false);
1791
            t.addBooleanColumn("LIVRE", Boolean.FALSE, false);
1792
            t.addDecimalColumn("QTE_LIVREE", 16, 6, BigDecimal.ZERO, true);
1793
            tableDevisElt.getBase().getDataSource().execute(t.asString());
1794
            root.refetchTable(tableDevisElt.getName());
1795
            root.getSchema().updateVersion();
1796
 
1797
            // Move show devise pref in global
1798
            SQLSelect sel = new SQLSelect();
1799
            sel.addSelect(root.getTable("DEVISE_HISTORIQUE").getKey(), "COUNT");
1800
            Number n = (Number) root.getDBSystemRoot().getDataSource().executeScalar(sel.asString());
1801
            if (n.intValue() > 0) {
1802
                SQLPreferences prefs = new SQLPreferences(root);
1803
                prefs.putBoolean(AbstractVenteArticleItemTable.ARTICLE_SHOW_DEVISE, true);
1804
                try {
1805
                    prefs.sync();
1806
                } catch (BackingStoreException e) {
1807
                    // TODO Auto-generated catch block
1808
                    e.printStackTrace();
1809
                }
1810
            }
1811
        }
1812
 
1813
        SQLTable tablePosteAn = root.getTable("POSTE_ANALYTIQUE");
1814
        if (!tablePosteAn.contains("OBSOLETE")) {
1815
            AlterTable t = new AlterTable(tablePosteAn);
1816
            t.addBooleanColumn("OBSOLETE", Boolean.FALSE, false);
1817
            tablePosteAn.getBase().getDataSource().execute(t.asString());
1818
            root.refetchTable(tablePosteAn.getName());
1819
            root.getSchema().updateVersion();
1820
        }
1821
 
1822
        SQLTable tableCheque = root.getTable("CHEQUE_A_ENCAISSER");
1823
        if (!tableCheque.contains("ID_COMPTE_PCE_TIERS")) {
1824
            AlterTable t = new AlterTable(tableCheque);
1825
            t.addVarCharColumn("TIERS", 256);
1826
            t.addForeignColumn("ID_COMPTE_PCE_TIERS", root.getTable("COMPTE_PCE"));
1827
            tableCheque.getBase().getDataSource().execute(t.asString());
1828
            root.refetchTable(tableCheque.getName());
1829
 
1830
            SQLTable tableEnc = root.getTable("ENCAISSER_MONTANT");
1831
            AlterTable tEnc = new AlterTable(tableEnc);
1832
            tEnc.addVarCharColumn("TIERS", 256);
1833
            tEnc.addForeignColumn("ID_COMPTE_PCE_TIERS", root.getTable("COMPTE_PCE"));
1834
            tableEnc.getBase().getDataSource().execute(tEnc.asString());
1835
            root.refetchTable(tableEnc.getName());
1836
            root.getSchema().updateVersion();
1837
 
1838
            SQLTable tableEch = root.getTable("ECHEANCE_CLIENT");
1839
            AlterTable tEch = new AlterTable(tableEch);
1840
            tEch.addVarCharColumn("TIERS", 256);
1841
            tEch.addForeignColumn("ID_COMPTE_PCE_TIERS", root.getTable("COMPTE_PCE"));
1842
            tableEch.getBase().getDataSource().execute(tEch.asString());
1843
            root.refetchTable(tableEch.getName());
1844
            root.getSchema().updateVersion();
1845
 
1846
            SQLTable tableChq = root.getTable("DEPOT_CHEQUE_ELEMENT");
1847
            AlterTable tChq = new AlterTable(tableChq);
1848
            tChq.addVarCharColumn("TIERS", 256);
1849
            tableChq.getBase().getDataSource().execute(tChq.asString());
1850
            root.refetchTable(tableChq.getName());
1851
            root.getSchema().updateVersion();
1852
        }
1853
 
1854
        if (!tableEcr.contains("DATE_ECHEANCE")) {
1855
            AlterTable t = new AlterTable(tableEcr);
1856
            t.addColumn("DATE_ECHEANCE", "date");
1857
            tableEcr.getBase().getDataSource().execute(t.asString());
1858
            root.refetchTable(tableEcr.getName());
1859
            root.getSchema().updateVersion();
1860
        }
1861
 
1862
        if (!tableVarSal.contains("HEURE_ABS_CUMUL_VAL")) {
1863
 
1864
            final AlterTable alterB = new AlterTable(root.getTable("VARIABLE_SALARIE"));
1865
 
1866
            for (org.openconcerto.sql.model.SQLField sqlField : tableVarSal.getContentFields()) {
1867
 
1868
                String field = sqlField.getName();
1869
                if (!field.equalsIgnoreCase("ID_USER_COMMON_CREATE") && !field.equalsIgnoreCase("ID_USER_COMMON_MODIFY") && !field.equalsIgnoreCase("MODIFICATION_DATE")
1870
                        && !field.equalsIgnoreCase("CREATION_DATE") && !field.endsWith("_DEFAULT_VAL")) {
1871
                    alterB.addColumn(field + "_CUMUL_VAL", "real DEFAULT 0");
1872
                }
1873
            }
1874
 
1875
            root.getBase().getDataSource().execute(alterB.asString());
1876
            root.refetchTable("VARIABLE_SALARIE");
1877
            root.getSchema().updateVersion();
1878
        }
174 ilm 1879
        SQLTable tableBL = root.getTable("BON_DE_LIVRAISON");
1880
        if (!tableBL.contains("ID_COMMERCIAL")) {
1881
            final AlterTable alterB = new AlterTable(tableBL);
1882
            alterB.addForeignColumn("ID_COMMERCIAL", root.getTable("COMMERCIAL"));
1883
            root.getBase().getDataSource().execute(alterB.asString());
1884
            root.refetchTable("BON_DE_LIVRAISON");
1885
            root.getSchema().updateVersion();
1886
        }
156 ilm 1887
 
1888
        // fix stock
1889
        {
1890
            SQLTable tableStock = root.getTable("STOCK");
1891
 
1892
            // Doublon depot stock
1893
            SQLSelect sel = new SQLSelect();
1894
            sel.addSelectFunctionStar("COUNT");
1895
            sel.addSelect(tableStock.getField("ID_DEPOT_STOCK"));
1896
            sel.addSelect(tableStock.getField("ID_ARTICLE"));
1897
            sel.addGroupBy(tableStock.getField("ID_DEPOT_STOCK"));
1898
            sel.addGroupBy(tableStock.getField("ID_ARTICLE"));
1899
            sel.setHaving(Where.createRaw("COUNT(*)>1", Collections.emptyList()));
1900
            System.err.println(sel.asString());
1901
 
1902
            List<Object[]> resultStockDoublon = (List<Object[]>) tableStock.getDBSystemRoot().getDataSource().execute(sel.asString(), new ArrayListHandler());
1903
            for (Object[] objects : resultStockDoublon) {
1904
 
1905
                if (((Number) objects[2]).intValue() == 1) {
1906
 
1907
                    UpdateBuilder buildStockArt = new UpdateBuilder(tableStock);
1908
                    buildStockArt.addBackwardVirtualJoin(tableArticle, "ID_STOCK");
1909
                    buildStockArt.setFromVirtualJoinField("ID_ARTICLE", tableArticle.getAlias(), "ID");
1910
                    buildStockArt.setWhere(new Where(tableStock.getKey(), "!=", 1));
1911
                    tableStock.getDBSystemRoot().getDataSource().execute(buildStockArt.asString());
1912
                } else {
1913
                    SQLSelect selD = new SQLSelect();
1914
                    selD.addSelectStar(tableStock);
1915
                    Where w = new Where(tableStock.getField("ID_ARTICLE"), "=", objects[2]);
1916
                    w = w.and(new Where(tableStock.getField("ID_DEPOT_STOCK"), "=", objects[1]));
1917
                    selD.setWhere(w);
1918
                    List<SQLRow> badStock = SQLRowListRSH.execute(selD);
1919
                    if (badStock.size() > 1) {
1920
                        SQLRowValues stock = badStock.get(0).createUpdateRow();
1921
                        List<String> fieldsToMerge = Arrays.asList("QTE_REEL", "QTE_TH", "QTE_RECEPT_ATTENTE", "QTE_LIV_ATTENTE");
1922
                        List<Integer> listBadIds = new ArrayList<>();
1923
                        for (int i = 1; i < badStock.size(); i++) {
1924
                            SQLRow rowBad = badStock.get(i);
1925
                            listBadIds.add(rowBad.getID());
1926
                            for (String field : fieldsToMerge) {
1927
                                stock.put(field, stock.getFloat(field) + rowBad.getFloat(field));
1928
                            }
1929
                            rowBad.createEmptyUpdateRow().put("ARCHIVE", 1).commit();
1930
                        }
1931
                        List<SQLTable> tablesToMErge = Arrays.asList(root.getTable("MOUVEMENT_STOCK"), root.getTable("ARTICLE"));
1932
                        for (SQLTable mergeTable : tablesToMErge) {
1933
                            UpdateBuilder up = new UpdateBuilder(mergeTable);
1934
                            up.setObject("ID_STOCK", stock.getID());
1935
                            up.setWhere(new Where(mergeTable.getField("ID_STOCK"), listBadIds));
1936
                            mergeTable.getDBSystemRoot().getDataSource().execute(up.asString());
1937
                        }
1938
                        stock.commit();
1939
 
1940
                    }
1941
                }
1942
            }
1943
 
1944
            SQLSelect selArt = new SQLSelect();
1945
            selArt.addSelectStar(tableArt);
1946
            Where w = new Where(tableArt.getField("ID_STOCK"), "=", 1);
1947
            selArt.setWhere(w);
1948
            List<SQLRow> badStock = SQLRowListRSH.execute(selArt);
1949
            for (SQLRow sqlRow : badStock) {
1950
                initStock(sqlRow);
1951
            }
1952
        }
1953
 
174 ilm 1954
        // Tarification client par quantite
1955
        if (root.getTable("TARIF_ARTICLE_CLIENT") == null) {
1956
            final SQLCreateTable createTableQtyTarif = new SQLCreateTable(root, "TARIF_ARTICLE_CLIENT");
1957
            createTableQtyTarif.addForeignColumn("ID_ARTICLE", root.getTable("ARTICLE"));
1958
            createTableQtyTarif.addForeignColumn("ID_CLIENT", root.getTable("CLIENT"));
1959
            createTableQtyTarif.addDecimalColumn("QUANTITE", 16, 3, BigDecimal.ONE, false);
1960
            createTableQtyTarif.addDecimalColumn("POURCENT_REMISE", 16, 3, null, true);
1961
            // createTableQtyTarif.addDecimalColumn("PRIX_METRIQUE_VT_1", 16, 6, null, true);
1962
            try {
1963
                root.getBase().getDataSource().execute(createTableQtyTarif.asString());
1964
                InstallationPanel.insertUndef(createTableQtyTarif);
1965
                root.refetchTable("TARIF_ARTICLE_CLIENT");
1966
                root.getSchema().updateVersion();
1967
            } catch (SQLException ex) {
1968
                throw new IllegalStateException("Erreur lors de la création de la table " + "TARIF_QUANTITE", ex);
1969
            }
1970
        }
156 ilm 1971
 
174 ilm 1972
        SQLTable tableTrCmd = root.getTable("TR_COMMANDE");
1973
        if (!tableTrCmd.contains("ID_FACTURE_FOURNISSEUR")) {
1974
            AlterTable t = new AlterTable(tableTrCmd);
1975
            t.addForeignColumn("ID_FACTURE_FOURNISSEUR", root.getTable("FACTURE_FOURNISSEUR"));
1976
            tableTrCmd.getBase().getDataSource().execute(t.asString());
1977
            root.refetchTable(tableTrCmd.getName());
1978
            root.getSchema().updateVersion();
1979
        }
1980
 
1981
        SQLTable tableContrat = root.getTable("CONTRAT_SALARIE");
1982
        if (!tableContrat.contains("COMPLEMENT_PCS")) {
1983
            AlterTable t = new AlterTable(tableContrat);
1984
            t.addVarCharColumn("COMPLEMENT_PCS", 54);
1985
            tableContrat.getBase().getDataSource().execute(t.asString());
1986
            root.refetchTable(tableContrat.getName());
1987
            root.getSchema().updateVersion();
1988
        }
177 ilm 1989
        if (!tableContrat.contains("SPECTACLE_OBJET")) {
1990
            AlterTable t = new AlterTable(tableContrat);
1991
            t.addVarCharColumn("SPECTACLE_OBJET", 54);
1992
            tableContrat.getBase().getDataSource().execute(t.asString());
1993
            root.refetchTable(tableContrat.getName());
1994
            root.getSchema().updateVersion();
1995
        }
174 ilm 1996
 
177 ilm 1997
        if (!tableContrat.contains("SPECTACLE_OBJET")) {
1998
            AlterTable t = new AlterTable(tableContrat);
1999
            t.addVarCharColumn("SPECTACLE_OBJET", 54);
2000
            tableContrat.getBase().getDataSource().execute(t.asString());
2001
            root.refetchTable(tableContrat.getName());
2002
            root.getSchema().updateVersion();
2003
        }
2004
        if (!tableContrat.contains("SPECTACLE_JOUR_CONTRAT")) {
2005
            AlterTable t = new AlterTable(tableContrat);
2006
            t.addDecimalColumn("SPECTACLE_JOUR_CONTRAT", 16, 2, null, true);
2007
            tableContrat.getBase().getDataSource().execute(t.asString());
2008
            root.refetchTable(tableContrat.getName());
2009
            root.getSchema().updateVersion();
2010
        }
2011
 
174 ilm 2012
        List<String> tablesCatComptable = Arrays.asList("DEVIS", "COMMANDE_CLIENT", "BON_DE_LIVRAISON", "SAISIE_VENTE_FACTURE", "AVOIR_CLIENT");
2013
        for (String tableToUp : tablesCatComptable) {
2014
            final SQLTable tableCatComptToAdd = root.getTable(tableToUp);
2015
            if (!tableCatComptToAdd.contains("ID_CATEGORIE_COMPTABLE")) {
2016
                final AlterTable alter = new AlterTable(tableCatComptToAdd);
2017
                alter.addForeignColumn("ID_CATEGORIE_COMPTABLE", root.getTable("CATEGORIE_COMPTABLE"));
2018
                exec(alter);
2019
                root.refetchTable(tableToUp);
2020
                root.getSchema().updateVersion();
2021
            }
2022
        }
2023
 
2024
 
2025
        // Modèles pour les emails
2026
        if (!root.contains(EmailTemplateSQLElement.TABLE_NAME)) {
2027
            final SQLCreateTable createTable = new SQLCreateTable(root, EmailTemplateSQLElement.TABLE_NAME);
2028
            createTable.addVarCharColumn("NOM", 80);
2029
            createTable.addVarCharColumn("TITRE", 80);
2030
            createTable.addVarCharColumn("TEXTE", 4096);
2031
            createTable.addVarCharColumn("FORMAT_DATE", 20);
2032
            createTable.addBooleanColumn("PAR_DEFAUT", Boolean.FALSE, false);
2033
            try {
2034
                root.getBase().getDataSource().execute(createTable.asString());
2035
                InstallationPanel.insertUndef(createTable);
2036
                root.refetchTable(EmailTemplateSQLElement.TABLE_NAME);
2037
                root.getSchema().updateVersion();
2038
            } catch (SQLException ex) {
2039
                throw new IllegalStateException("Erreur lors de la création de la table " + EmailTemplateSQLElement.TABLE_NAME, ex);
2040
            }
2041
        }
2042
        // Force undefined policy to inDb
2043
        root.setMetadata(SQLTable.UNDEFINED_ID_POLICY, "inDB");
177 ilm 2044
        final Map<String, Number> mapTableNameUndefined = SQLTable.getUndefinedIDs(root.getSchema());
174 ilm 2045
        final Set<String> tables = root.getSchema().getTableNames();
2046
        for (String tName : tables) {
2047
            if (!mapTableNameUndefined.containsKey(tName)) {
2048
                System.err.println("Updater_1_5.update() adding undefined in db for " + tName);
2049
                SQLTable.setUndefID(root.getSchema(), tName, null);
2050
            }
2051
        }
2052
 
2053
        // Création de la table Modéle
2054
        if (!root.contains("CONTACT_SALARIE")) {
2055
 
2056
            SQLCreateTable createModele = new SQLCreateTable(root, "CONTACT_SALARIE");
2057
            createModele.addVarCharColumn("NOM", 256);
2058
            createModele.addVarCharColumn("PRENOM", 256);
2059
            createModele.addVarCharColumn("TEL_DIRECT", 256);
2060
            createModele.addVarCharColumn("TEL_MOBILE", 256);
2061
            createModele.addVarCharColumn("EMAIL", 256);
2062
            createModele.addVarCharColumn("FAX", 256);
2063
            createModele.addVarCharColumn("FONCTION", 256);
2064
            createModele.addVarCharColumn("TEL_PERSONEL", 256);
2065
            createModele.addVarCharColumn("TEL_STANDARD", 256);
2066
            createModele.addForeignColumn("ID_TITRE_PERSONNEL", root.findTable("TITRE_PERSONNEL"));
2067
            createModele.addForeignColumn("ID_SALARIE", root.findTable("SALARIE"));
2068
 
2069
            try {
2070
                root.getBase().getDataSource().execute(createModele.asString());
2071
                root.refetchTable("CONTACT_SALARIE");
2072
                SQLRowValues rowVals = new SQLRowValues(root.getTable("CONTACT_SALARIE"));
2073
                SQLRow rowInserted = rowVals.commit();
2074
                SQLTable.setUndefID(root.getSchema(), "CONTACT_SALARIE", rowInserted.getID());
2075
                tableDevis.getSchema().updateVersion();
2076
            } catch (SQLException ex) {
2077
                throw new IllegalStateException("Erreur lors de la création de la table MODELE", ex);
2078
            }
2079
        }
2080
 
2081
        final SQLTable tableRgltPaye = root.getTable("REGLEMENT_PAYE");
2082
        if (!tableRgltPaye.contains("IBAN")) {
2083
            final AlterTable alter = new AlterTable(tableRgltPaye);
2084
            alter.addVarCharColumn("IBAN", 128);
2085
            exec(alter);
2086
            root.refetchTable(tableRgltPaye.getName());
2087
            root.getSchema().updateVersion();
2088
        }
2089
        if (!tableRgltPaye.contains("BIC")) {
2090
            final AlterTable alter = new AlterTable(tableRgltPaye);
2091
            alter.addVarCharColumn("BIC", 40);
2092
            exec(alter);
2093
            root.refetchTable(tableRgltPaye.getName());
2094
            root.getSchema().updateVersion();
2095
        }
2096
        final SQLTable tableSalarie = root.getTable("SALARIE");
2097
        if (!tableSalarie.contains("ID_USER_COMMON")) {
2098
            final AlterTable alter = new AlterTable(tableSalarie);
2099
            alter.addForeignColumn("ID_USER_COMMON", root.findTable("USER_COMMON"));
2100
            exec(alter);
2101
            root.refetchTable(tableSalarie.getName());
2102
            root.getSchema().updateVersion();
2103
        }
2104
 
2105
        final SQLTable tableInfosSalarie = root.getTable("INFOS_SALARIE_PAYE");
2106
        if (!tableInfosSalarie.contains("DUREE_FORFAIT")) {
2107
            final AlterTable alter = new AlterTable(tableInfosSalarie);
2108
            alter.addColumn("DUREE_FORFAIT", "real");
2109
            exec(alter);
2110
            root.refetchTable(tableSalarie.getName());
2111
            root.getSchema().updateVersion();
2112
        }
177 ilm 2113
        boolean upDimensionArt = false;
174 ilm 2114
 
177 ilm 2115
        final AlterTable alterDimensionArt = new AlterTable(tableArt);
2116
        if (!tableArt.contains("LONGUEUR")) {
2117
            alterDimensionArt.addDecimalColumn("LONGUEUR", 16, 8, null, true);
2118
            upDimensionArt = true;
2119
        }
2120
        if (!tableArt.contains("LARGEUR")) {
2121
            alterDimensionArt.addDecimalColumn("LARGEUR", 16, 8, null, true);
2122
            upDimensionArt = true;
2123
        }
2124
        if (!tableArt.contains("HAUTEUR")) {
2125
            alterDimensionArt.addDecimalColumn("HAUTEUR", 16, 8, null, true);
2126
            upDimensionArt = true;
2127
        }
2128
        if (upDimensionArt) {
2129
            tableArt.getBase().getDataSource().execute(alterDimensionArt.asString());
2130
            tableArt.getSchema().updateVersion();
2131
            tableArt.fetchFields();
2132
        }
2133
 
2134
        List<String> tableElementWithTable = Arrays.asList("FACTURE_FOURNISSEUR_ELEMENT", "DEVIS_ELEMENT", "COMMANDE_ELEMENT", "BON_RECEPTION_ELEMENT", "COMMANDE_CLIENT_ELEMENT",
2135
                "BON_DE_LIVRAISON_ELEMENT", "SAISIE_VENTE_FACTURE_ELEMENT", "AVOIR_CLIENT_ELEMENT", "DEMANDE_PRIX_ELEMENT");
2136
        for (String tableName : tableElementWithTable) {
2137
            final SQLTable tableToAddDimension = root.getTable(tableName);
2138
            boolean upDimensionArtItem = false;
2139
 
2140
            final AlterTable alterDimensionArtItem = new AlterTable(tableToAddDimension);
2141
            if (!tableToAddDimension.contains("LONGUEUR")) {
2142
                alterDimensionArtItem.addDecimalColumn("LONGUEUR", 16, 8, null, true);
2143
                upDimensionArtItem = true;
2144
            }
2145
            if (!tableToAddDimension.contains("LARGEUR")) {
2146
                alterDimensionArtItem.addDecimalColumn("LARGEUR", 16, 8, null, true);
2147
                upDimensionArtItem = true;
2148
            }
2149
            if (!tableToAddDimension.contains("HAUTEUR")) {
2150
                alterDimensionArtItem.addDecimalColumn("HAUTEUR", 16, 8, null, true);
2151
                upDimensionArtItem = true;
2152
            }
2153
            if (upDimensionArtItem) {
2154
                tableToAddDimension.getBase().getDataSource().execute(alterDimensionArtItem.asString());
2155
                tableToAddDimension.getSchema().updateVersion();
2156
                tableToAddDimension.fetchFields();
2157
            }
2158
        }
2159
 
2160
        if (!tTva.contains("DEFAULT_ACHAT")) {
2161
            final AlterTable alterTaxe = new AlterTable(tTva);
2162
            alterTaxe.addBooleanColumn("DEFAULT_ACHAT", Boolean.FALSE, false);
2163
            tTva.getBase().getDataSource().execute(alterTaxe.asString());
2164
            tTva.getSchema().updateVersion();
2165
            tTva.fetchFields();
2166
        }
2167
        SQLTable tableTypeRglt = root.getTable("TYPE_REGLEMENT");
2168
        if (!tableTypeRglt.contains("SEPA")) {
2169
            final AlterTable alterTaxe = new AlterTable(tableTypeRglt);
2170
            alterTaxe.addBooleanColumn("SEPA", Boolean.FALSE, false);
2171
            tableTypeRglt.getBase().getDataSource().execute(alterTaxe.asString());
2172
            tableTypeRglt.getSchema().updateVersion();
2173
            tableTypeRglt.fetchFields();
2174
            UpdateBuilder upSEPA = new UpdateBuilder(tableTypeRglt);
2175
            upSEPA.setObject("SEPA", Boolean.TRUE);
2176
            upSEPA.setObject("ECHEANCE", Boolean.TRUE);
2177
            upSEPA.setWhere(new Where(tableTypeRglt.getField("NOM"), "=", "Prélèvement"));
2178
            tTva.getBase().getDataSource().execute(upSEPA.asString());
2179
        }
2180
 
2181
        SQLTable tableEch = root.getTable("ECHEANCE_CLIENT");
2182
        if (!tableEch.contains("ID_SEPA_MANDATE")) {
2183
 
2184
            final AlterTable alterEch = new AlterTable(tableEch);
2185
            alterEch.addForeignColumn("ID_SEPA_MANDATE", root.getTable("SEPA_MANDATE"));
2186
            alterEch.addForeignColumn("ID_SDD_MESSAGE", root.getTable(SDDMessageSQLElement.TABLE_NAME));
2187
            alterEch.addVarCharColumn("SDD_EndToEndId", 35);
2188
            alterEch.addBooleanColumn("FICHIER_CREE", Boolean.FALSE, false);
2189
            alterEch.addBooleanColumn("REJETER", Boolean.FALSE, false);
2190
            alterEch.addVarCharColumn("ETS", 256);
2191
            alterEch.addForeignColumn("ID_" + BanqueSQLElement.TABLENAME, root.getTable(BanqueSQLElement.TABLENAME));
2192
            tableEch.getBase().getDataSource().execute(alterEch.asString());
2193
            tableEch.getSchema().updateVersion();
2194
            tableEch.fetchFields();
2195
 
2196
        }
2197
        if (!tClient.contains("ID_SEPA_MANDATE_DEFAULT")) {
2198
            final AlterTable alterClient = new AlterTable(tClient);
2199
            alterClient.addForeignColumn("ID_SEPA_MANDATE_DEFAULT", root.getTable("SEPA_MANDATE"));
2200
            tClient.getBase().getDataSource().execute(alterClient.asString());
2201
            tClient.getSchema().updateVersion();
2202
            tClient.fetchFields();
2203
        }
2204
 
2205
        if (!tableArt.contains("AUTO_PRIX_MIN_VENTE_NOMENCLATURE")) {
2206
            final AlterTable alter = new AlterTable(tableArt);
2207
            alter.addBooleanColumn("AUTO_PRIX_MIN_VENTE_NOMENCLATURE", Boolean.FALSE, false);
2208
            tableArt.getBase().getDataSource().execute(alter.asString());
2209
            tableArt.getSchema().updateVersion();
2210
            tableArt.fetchFields();
2211
        }
2212
        if (!tableArt.contains("DERNIER_DATE_ACHAT")) {
2213
            final AlterTable alter = new AlterTable(tableArt);
2214
            alter.addColumn("DERNIER_DATE_ACHAT", "date");
2215
            tableArt.getBase().getDataSource().execute(alter.asString());
2216
            tableArt.getSchema().updateVersion();
2217
            tableArt.fetchFields();
2218
            ReferenceArticleSQLElement.updateDateAchat(tableArt, null);
2219
        }
2220
 
2221
        SQLTable tableFactF = root.getTable("FACTURE_FOURNISSEUR");
2222
        if (!tableFactF.contains("DATE_REGLEMENT")) {
2223
            final AlterTable alter = new AlterTable(tableFactF);
2224
            alter.addColumn("DATE_REGLEMENT", "date");
2225
            tableFactF.getBase().getDataSource().execute(alter.asString());
2226
            tableFactF.getSchema().updateVersion();
2227
            tableFactF.fetchFields();
2228
        }
2229
 
2230
        SQLTable tableEchF = root.getTable("ECHEANCE_FOURNISSEUR");
2231
        if (!tableEchF.contains("ID_FACTURE_FOURNISSEUR")) {
2232
            final AlterTable alter = new AlterTable(tableEchF);
2233
            alter.addForeignColumn("ID_FACTURE_FOURNISSEUR", tableFactF);
2234
            tableEchF.getBase().getDataSource().execute(alter.asString());
2235
            tableEchF.getSchema().updateVersion();
2236
            tableEchF.fetchFields();
2237
        }
2238
 
2239
        List<String> achatItems = Arrays.asList("DEMANDE_PRIX_ELEMENT", "COMMANDE_ELEMENT", "BON_RECEPTION_ELEMENT", "FACTURE_FOURNISSEUR_ELEMENT");
2240
        for (String string : achatItems) {
2241
            boolean alter = false;
2242
            SQLTable tableItems = root.getTable(string);
2243
            final AlterTable t = new AlterTable(tableItems);
2244
 
2245
            if (!tableItems.getFieldsName().contains("POIDS_COLIS_NET")) {
2246
                t.addColumn("POIDS_COLIS_NET", "numeric (16,8) DEFAULT 1");
2247
                alter = true;
2248
            }
2249
 
2250
            if (!tableItems.getFieldsName().contains("T_POIDS_COLIS_NET")) {
2251
                t.addColumn("T_POIDS_COLIS_NET", "numeric (16,8) DEFAULT 1");
2252
                alter = true;
2253
            }
2254
 
2255
            if (!tableItems.getFieldsName().contains("NB_COLIS")) {
2256
                t.addColumn("NB_COLIS", "integer DEFAULT 0");
2257
                alter = true;
2258
            }
2259
            if (alter) {
2260
                tableItems.getBase().getDataSource().execute(t.asString());
2261
                tableItems.getSchema().updateVersion();
2262
                tableItems.fetchFields();
2263
            }
2264
        }
2265
 
2266
        for (String tableName : tableElementWithTable) {
2267
            final SQLTable tableToAddTare = root.getTable(tableName);
2268
            boolean upTareArtItem = false;
2269
 
2270
            final AlterTable alterTareArtItem = new AlterTable(tableToAddTare);
2271
            if (!tableToAddTare.contains("TARE")) {
2272
                alterTareArtItem.addDecimalColumn("TARE", 16, 8, null, true);
2273
                alterTareArtItem.alterColumn("POIDS_COLIS_NET", EnumSet.allOf(Properties.class), "numeric(16,8)", "0", true);
2274
                alterTareArtItem.alterColumn("T_POIDS_COLIS_NET", EnumSet.allOf(Properties.class), "numeric(16,8)", "0", true);
2275
 
2276
                alterTareArtItem.addDecimalColumn("T_POIDS_BRUT", 16, 8, BigDecimal.ZERO, true);
2277
 
2278
                upTareArtItem = true;
2279
            }
2280
 
2281
            if (upTareArtItem) {
2282
                tableToAddTare.getBase().getDataSource().execute(alterTareArtItem.asString());
2283
                tableToAddTare.getSchema().updateVersion();
2284
                tableToAddTare.fetchFields();
2285
 
2286
                int id = tableToAddTare.getUndefinedID();
2287
                if (id != SQLRow.NONEXISTANT_ID) {
2288
                    UpdateBuilder build = new UpdateBuilder(tableToAddTare);
2289
                    build.setObject("POIDS_COLIS_NET", BigDecimal.ZERO);
2290
                    build.setWhere(new Where(tableToAddTare.getKey(), "=", id));
2291
                    tableToAddTare.getDBSystemRoot().getDataSource().execute(build.asString());
2292
                }
2293
            }
2294
        }
2295
 
2296
        // Fix nb char MOUVEMENT_STOCK.NOM
2297
        if (tableMvtStock.getField("NOM").getType().getSize() == 45) {
2298
            AlterTable alterMvt = new AlterTable(tableMvtStock);
2299
            alterMvt.alterColumn("NOM", EnumSet.allOf(Properties.class), "varchar(512)", "''", false);
2300
            tableMvtStock.getDBSystemRoot().getDataSource().execute(alterMvt.asString());
2301
            tableMvtStock.getSchema().updateVersion();
2302
        }
2303
 
2304
        SQLTable tableEtatStock = root.getTable("ETAT_STOCK");
2305
        SQLTable tableDepotStock = root.getTable("DEPOT_STOCK");
2306
        if (!tableEtatStock.contains("ID_DEPOT_STOCK")) {
2307
            final AlterTable alter = new AlterTable(tableEtatStock);
2308
            alter.addForeignColumn("ID_DEPOT_STOCK", tableDepotStock);
2309
            tableEtatStock.getBase().getDataSource().execute(alter.asString());
2310
            tableEtatStock.getSchema().updateVersion();
2311
            tableEtatStock.fetchFields();
2312
        }
180 ilm 2313
 
2314
        // Création de la table Agence
2315
        if (!root.contains("AGENCE")) {
2316
            SQLCreateTable createAgence = new SQLCreateTable(root, "AGENCE");
2317
            createAgence.addVarCharColumn("DESIGNATION", 256);
2318
            createAgence.addVarCharColumn("TEL_1", 256);
2319
            createAgence.addVarCharColumn("EMAIL", 256);
2320
            createAgence.addVarCharColumn("FAX", 256);
2321
            createAgence.addForeignColumn("ID_CLIENT", root.findTable("CLIENT"));
2322
            createAgence.addForeignColumn("ID_ADRESSE", root.findTable("ADRESSE"));
2323
 
2324
            try {
2325
                // test
2326
                root.getBase().getDataSource().execute(createAgence.asString());
2327
                root.refetchTable("AGENCE");
2328
                SQLRowValues rowVals = new SQLRowValues(root.getTable("AGENCE"));
2329
                SQLRow rowInserted = rowVals.commit();
2330
                SQLTable.setUndefID(root.getSchema(), "AGENCE", rowInserted.getID());
2331
                tableDevis.getSchema().updateVersion();
2332
            } catch (SQLException ex) {
2333
                throw new IllegalStateException("Erreur lors de la création de la table AGENCE", ex);
2334
            }
2335
        }
2336
        // Tarification client par famille article
2337
        if (root.getTable("TARIF_FAMILLE_ARTICLE_CLIENT") == null) {
2338
            final SQLCreateTable createTableQtyTarif = new SQLCreateTable(root, "TARIF_FAMILLE_ARTICLE_CLIENT");
2339
            createTableQtyTarif.addForeignColumn("ID_FAMILLE_ARTICLE", root.getTable("FAMILLE_ARTICLE"));
2340
            createTableQtyTarif.addForeignColumn("ID_CLIENT", root.getTable("CLIENT"));
2341
            createTableQtyTarif.addDecimalColumn("QUANTITE", 16, 3, BigDecimal.ONE, false);
2342
            createTableQtyTarif.addDecimalColumn("POURCENT_REMISE", 16, 3, null, true);
2343
            // createTableQtyTarif.addDecimalColumn("PRIX_METRIQUE_VT_1", 16, 6, null, true);
2344
            try {
2345
                root.getBase().getDataSource().execute(createTableQtyTarif.asString());
2346
                InstallationPanel.insertUndef(createTableQtyTarif);
2347
                root.refetchTable("TARIF_FAMILLE_ARTICLE_CLIENT");
2348
                root.getSchema().updateVersion();
2349
            } catch (SQLException ex) {
2350
                throw new IllegalStateException("Erreur lors de la création de la table " + "TARIF_FAMILLE_ARTICLE_CLIENT", ex);
2351
            }
2352
        }
2353
 
2354
        // Tarification client par famille article
2355
        if (root.getTable("TARIF_FAMILLE_ARTICLE_CLIENT") == null) {
2356
            final SQLCreateTable createTableQtyTarif = new SQLCreateTable(root, "TARIF_FAMILLE_ARTICLE_CLIENT");
2357
            createTableQtyTarif.addForeignColumn("ID_FAMILLE_ARTICLE", root.getTable("FAMILLE_ARTICLE"));
2358
            createTableQtyTarif.addForeignColumn("ID_CLIENT", root.getTable("CLIENT"));
2359
            createTableQtyTarif.addDecimalColumn("QUANTITE", 16, 3, BigDecimal.ONE, false);
2360
            createTableQtyTarif.addDecimalColumn("POURCENT_REMISE", 16, 3, null, true);
2361
            // createTableQtyTarif.addDecimalColumn("PRIX_METRIQUE_VT_1", 16, 6, null, true);
2362
            try {
2363
                root.getBase().getDataSource().execute(createTableQtyTarif.asString());
2364
                InstallationPanel.insertUndef(createTableQtyTarif);
2365
                root.refetchTable("TARIF_FAMILLE_ARTICLE_CLIENT");
2366
                root.getSchema().updateVersion();
2367
            } catch (SQLException ex) {
2368
                throw new IllegalStateException("Erreur lors de la création de la table " + "TARIF_FAMILLE_ARTICLE_CLIENT", ex);
2369
            }
2370
        }
2371
 
2372
        SQLTable tableAvoirC = root.getTable("AVOIR_CLIENT");
2373
        if (!tableAvoirC.contains("ID_TAXE_PORT")) {
2374
            final AlterTable alterB = new AlterTable(tableAvoirC);
2375
            alterB.addForeignColumn("ID_TAXE_PORT", root.getTable("TAXE"));
2376
            root.getBase().getDataSource().execute(alterB.asString());
2377
            root.refetchTable(tableAvoirC.getName());
2378
            root.getSchema().updateVersion();
2379
        }
2380
 
2381
        SQLTable tableVF = root.getTable("SAISIE_VENTE_FACTURE");
2382
        if (!tableVF.contains("ID_FACTURATION_COMMANDE_CLIENT")) {
2383
            final AlterTable alter = new AlterTable(tableVF);
2384
            final SQLTable tableFacturationCommandeClient = tableVF.getDBRoot().getTable("FACTURATION_COMMANDE_CLIENT");
2385
            alter.addForeignColumn("ID_FACTURATION_COMMANDE_CLIENT", tableFacturationCommandeClient);
2386
            tableVF.getBase().getDataSource().execute(alter.asString());
2387
            tableVF.getSchema().updateVersion();
2388
            tableVF.fetchFields();
2389
 
2390
            // update ID_FACTURATION_COMMANDE
2391
            UpdateBuilder builder = new UpdateBuilder(tableVF);
2392
            AliasedTable ref = new AliasedTable(tableFacturationCommandeClient, "tf");
2393
            builder.addBackwardVirtualJoin(ref, "ID_SAISIE_VENTE_FACTURE");
2394
            builder.setFromVirtualJoinField("ID_FACTURATION_COMMANDE_CLIENT", "tf", tableFacturationCommandeClient.getKey().getName());
2395
            tableVF.getBase().getDataSource().execute(builder.asString());
2396
        }
2397
        if (!root.getTable("NUMEROTATION_AUTO").contains("CODE_LETTRAGE_PARTIEL")) {
2398
            final AlterTable alterNumero = new AlterTable(root.getTable("NUMEROTATION_AUTO"));
2399
            alterNumero.addVarCharColumn("CODE_LETTRAGE_PARTIEL", 256);
2400
            root.getBase().getDataSource().execute(alterNumero.asString());
2401
            root.refetchTable("NUMEROTATION_AUTO");
2402
            root.getSchema().updateVersion();
2403
        }
2404
 
2405
        if (!root.getTable("ECRITURE").contains("LETTRAGE_PARTIEL")) {
2406
            final AlterTable alterEcriture = new AlterTable(root.getTable("ECRITURE"));
2407
            alterEcriture.addVarCharColumn("LETTRAGE_PARTIEL", 256);
2408
            root.getBase().getDataSource().execute(alterEcriture.asString());
2409
            root.refetchTable("ECRITURE");
2410
            root.getSchema().updateVersion();
2411
        }
2412
 
2413
        SQLTable tableFactureF = root.getTable("FACTURE_FOURNISSEUR");
2414
        if (!tableFactureF.contains("ID_AVOIR_FOURNISSEUR_2")) {
2415
            final AlterTable alter = new AlterTable(tableFactureF);
2416
            final SQLTable tableAvoirF = tableVF.getDBRoot().getTable("AVOIR_FOURNISSEUR");
2417
            alter.addForeignColumn("ID_AVOIR_FOURNISSEUR_2", tableAvoirF);
2418
            tableFactureF.getBase().getDataSource().execute(alter.asString());
2419
            tableFactureF.getSchema().updateVersion();
2420
            tableVF.fetchFields();
2421
        }
2422
 
142 ilm 2423
    }
2424
 
174 ilm 2425
    public static void initStock(SQLRow rowArticle, int idDepot) {
156 ilm 2426
 
2427
        SQLSelect selStock = new SQLSelect();
174 ilm 2428
        selStock.addSelectStar(rowArticle.getTable().getTable("STOCK"));
2429
        selStock.setWhere(new Where(rowArticle.getTable().getTable("STOCK").getField("ID_ARTICLE"), "=", rowArticle.getID()));
156 ilm 2430
        List<SQLRow> rowsStock = SQLRowListRSH.execute(selStock);
2431
        Map<Integer, SQLRow> initedDepot = new HashMap<>();
2432
        for (SQLRow sqlRow : rowsStock) {
2433
            initedDepot.put(sqlRow.getForeignID("ID_DEPOT_STOCK"), sqlRow);
2434
        }
2435
 
2436
        List<StockItem> stockItems = new ArrayList<StockItem>();
174 ilm 2437
        try {
2438
            if (!initedDepot.keySet().contains(idDepot)) {
2439
                SQLRowValues rowVals = new SQLRowValues(rowArticle.getTable().getTable("STOCK"));
2440
                rowVals.put("ID_ARTICLE", rowArticle.getID());
2441
                rowVals.put("ID_DEPOT_STOCK", idDepot);
156 ilm 2442
 
174 ilm 2443
                SQLRow rowStock = rowVals.commit();
2444
                if ((rowArticle.getObject("ID_DEPOT_STOCK") == null || rowArticle.isForeignEmpty("ID_DEPOT_STOCK")) && idDepot == DepotStockSQLElement.DEFAULT_ID) {
2445
                    rowArticle.createEmptyUpdateRow().put("ID_STOCK", rowStock.getID()).put("ID_DEPOT_STOCK", DepotStockSQLElement.DEFAULT_ID).commit();
2446
                } else if (idDepot == rowArticle.getForeignID("ID_DEPOT_STOCK")) {
2447
                    rowArticle.createEmptyUpdateRow().put("ID_STOCK", rowStock.getID()).commit();
2448
                }
2449
                stockItems.add(new StockItem(rowArticle, rowStock));
156 ilm 2450
 
174 ilm 2451
            } else {
2452
                SQLRow rowExisting = initedDepot.get(idDepot);
2453
                if ((rowArticle.getObject("ID_DEPOT_STOCK") == null || rowArticle.isForeignEmpty("ID_DEPOT_STOCK")) && idDepot == DepotStockSQLElement.DEFAULT_ID) {
2454
                    rowArticle.createEmptyUpdateRow().put("ID_STOCK", rowExisting.getID()).put("ID_DEPOT_STOCK", DepotStockSQLElement.DEFAULT_ID).commit();
2455
                } else if (idDepot == rowArticle.getForeignID("ID_DEPOT_STOCK")) {
2456
                    rowArticle.createEmptyUpdateRow().put("ID_STOCK", rowExisting.getID()).commit();
156 ilm 2457
                }
174 ilm 2458
                stockItems.add(new StockItem(rowArticle, rowExisting));
2459
            }
2460
        } catch (SQLException e) {
2461
            ExceptionHandler.handle("Erreur lors de l'initialisation du stock de l'article", e);
2462
        }
2463
 
2464
        if (rowArticle.getReferentRows(rowArticle.getTable().getTable("ARTICLE_ELEMENT").getField("ID_ARTICLE_PARENT")).size() > 0) {
2465
            ComposedItemStockUpdater up = new ComposedItemStockUpdater(rowArticle.getTable().getDBRoot(), stockItems);
2466
            try {
2467
                up.updateNomenclature(stockItems);
156 ilm 2468
            } catch (SQLException e) {
174 ilm 2469
                ExceptionHandler.handle("Erreur lors de l'actualisation du stock!", e);
156 ilm 2470
            }
2471
        }
2472
    }
2473
 
174 ilm 2474
    public static void initStock(SQLRow row) {
2475
        int foreignID = DepotStockSQLElement.DEFAULT_ID;
2476
        if (row.getObject("ID_DEPOT_STOCK") != null && !row.isForeignEmpty("ID_DEPOT_STOCK")) {
2477
            foreignID = row.getForeignID("ID_DEPOT_STOCK");
2478
        }
2479
        initStock(row, foreignID);
2480
    }
2481
 
151 ilm 2482
    public static void exec(final AlterTable alter) throws SQLException {
2483
        alter.getTable().getDBSystemRoot().getDataSource().execute(alter.asString());
2484
        alter.getTable().getSchema().updateVersion();
2485
        alter.getTable().fetchFields();
2486
    }
142 ilm 2487
}