OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev 156 Rev 174
Line 13... Line 13...
13
 
13
 
14
 package org.openconcerto.erp.core.sales.product.model;
14
 package org.openconcerto.erp.core.sales.product.model;
15
 
15
 
16
import org.openconcerto.erp.core.sales.product.model.ProductHelper.SupplierPriceField;
16
import org.openconcerto.erp.core.sales.product.model.ProductHelper.SupplierPriceField;
17
import org.openconcerto.erp.core.supplychain.stock.element.DepotStockSQLElement;
17
import org.openconcerto.erp.core.supplychain.stock.element.DepotStockSQLElement;
-
 
18
import org.openconcerto.sql.Configuration;
-
 
19
import org.openconcerto.sql.model.SQLField;
18
import org.openconcerto.sql.model.SQLRow;
20
import org.openconcerto.sql.model.SQLRow;
19
import org.openconcerto.sql.model.SQLRowAccessor;
21
import org.openconcerto.sql.model.SQLRowAccessor;
20
import org.openconcerto.sql.model.SQLRowValues;
22
import org.openconcerto.sql.model.SQLRowValues;
21
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
23
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
22
import org.openconcerto.sql.model.SQLTable;
24
import org.openconcerto.sql.model.SQLTable;
Line 165... Line 167...
165
        // return new ProductComponent(rowVals.getForeign("ID_ARTICLE"), qteFinal);
167
        // return new ProductComponent(rowVals.getForeign("ID_ARTICLE"), qteFinal);
166
    }
168
    }
167
 
169
 
168
    private static SQLRowAccessor getStock(SQLRowAccessor rowValsProduct, SQLRowAccessor rowValsElt, SQLRowAccessor rowValsSource) {
170
    private static SQLRowAccessor getStock(SQLRowAccessor rowValsProduct, SQLRowAccessor rowValsElt, SQLRowAccessor rowValsSource) {
169
        SQLRowAccessor rowStock = null;
171
        SQLRowAccessor rowStock = null;
170
        final int idDepot;
172
        int idDepot = -1;
-
 
173
 
-
 
174
        if (rowValsProduct.getTable().getDBRoot().contains("AFFAIRE")) {
-
 
175
            SQLTable tableAff = rowValsProduct.getTable().getTable("AFFAIRE");
-
 
176
            if (tableAff.contains("ID_DEPOT_STOCK")) {
-
 
177
                if (rowValsSource.getFields().contains("ID_DEPOT_STOCK") && !rowValsSource.isForeignEmpty("ID_DEPOT_STOCK")) {
-
 
178
                    idDepot = rowValsSource.getForeignID("ID_DEPOT_STOCK");
-
 
179
                } else if (rowValsSource.getFields().contains("ID_AFFAIRE") && !rowValsSource.isForeignEmpty("ID_AFFAIRE")) {
-
 
180
                    SQLRowAccessor rowAff = rowValsSource.getForeign("ID_AFFAIRE");
-
 
181
                    if (rowAff.getFields().contains("ID_DEPOT_STOCK") && !rowAff.isForeignEmpty("ID_DEPOT_STOCK")) {
-
 
182
                        idDepot = rowAff.getForeignID("ID_DEPOT_STOCK");
-
 
183
                    }
-
 
184
                } else {
-
 
185
                    SQLField fieldParent = Configuration.getInstance().getDirectory().getElement(rowValsSource.getTable()).getParentForeignField();
-
 
186
                    if (fieldParent != null) {
-
 
187
                        SQLRowAccessor parent = rowValsSource.getForeign(fieldParent.getName());
-
 
188
 
-
 
189
                        if (parent.getFields().contains("ID_AFFAIRE") && !parent.isForeignEmpty("ID_AFFAIRE")) {
-
 
190
                            SQLRowAccessor rowAff = parent.getForeign("ID_AFFAIRE");
-
 
191
                            if (rowAff.getFields().contains("ID_DEPOT_STOCK") && !rowAff.isForeignEmpty("ID_DEPOT_STOCK")) {
-
 
192
                                idDepot = rowAff.getForeignID("ID_DEPOT_STOCK");
-
 
193
                            }
-
 
194
                        }
-
 
195
                    }
-
 
196
                }
-
 
197
            }
-
 
198
        }
-
 
199
 
-
 
200
        if (idDepot == -1) {
-
 
201
 
171
        if (rowValsSource.getFields().contains("ID_DEPOT_STOCK") && !rowValsSource.isForeignEmpty("ID_DEPOT_STOCK")) {
202
            if (rowValsSource.getFields().contains("ID_DEPOT_STOCK") && !rowValsSource.isForeignEmpty("ID_DEPOT_STOCK")) {
172
            idDepot = rowValsSource.getForeignID("ID_DEPOT_STOCK");
203
                idDepot = rowValsSource.getForeignID("ID_DEPOT_STOCK");
173
        } else {
204
            } else {
174
            if (rowValsElt.getForeign("ID_DEPOT_STOCK") != null && !rowValsElt.isForeignEmpty("ID_DEPOT_STOCK")) {
205
                if (rowValsElt.getForeign("ID_DEPOT_STOCK") != null && !rowValsElt.isForeignEmpty("ID_DEPOT_STOCK")) {
175
                idDepot = rowValsElt.getForeignID("ID_DEPOT_STOCK");
206
                    idDepot = rowValsElt.getForeignID("ID_DEPOT_STOCK");
Line 181... Line 212...
181
                    ExceptionHandler.handle("Erreur lors de l'initialisation du stock!", e);
212
                        ExceptionHandler.handle("Erreur lors de l'initialisation du stock!", e);
182
                }
213
                    }
183
 
214
 
184
            }
215
                }
185
        }
216
            }
186
 
217
        }
187
        SQLTable stockTable = rowValsElt.getTable().getTable("STOCK");
218
        SQLTable stockTable = rowValsElt.getTable().getTable("STOCK");
188
        SQLRowValues putRowValuesStock = new SQLRowValues(stockTable);
219
        SQLRowValues putRowValuesStock = new SQLRowValues(stockTable);
189
        putRowValuesStock.putNulls(stockTable.getTable().getFieldsName());
220
        putRowValuesStock.putNulls(stockTable.getTable().getFieldsName());
190
 
221
 
191
        SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(putRowValuesStock);
222
        SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(putRowValuesStock);
Line 213... Line 244...
213
        } else if (rowValsResult.size() > 1) {
244
        } else if (rowValsResult.size() > 1) {
214
            throw new IllegalStateException("2 lignes de stocks pour le même dépôt! Article " + rowValsProduct.getID() + " Depot " + rowValsElt.getForeignID("ID_DEPOT_STOCK"));
245
            throw new IllegalStateException("2 lignes de stocks pour le même dépôt! Article " + rowValsProduct.getID() + " Depot " + rowValsElt.getForeignID("ID_DEPOT_STOCK"));
215
        }
246
        }
216
        return rowStock;
247
        return rowStock;
217
    }
248
    }
-
 
249
 
-
 
250
    public static SQLRowAccessor findOrCreateStock(SQLRowAccessor article, SQLRowAccessor depot) {
-
 
251
 
-
 
252
        SQLRowAccessor rowStock = null;
-
 
253
        SQLTable stockTable = article.getTable().getTable("STOCK");
-
 
254
        SQLRowValues putRowValuesStock = new SQLRowValues(stockTable);
-
 
255
        putRowValuesStock.putNulls(stockTable.getTable().getFieldsName());
-
 
256
 
-
 
257
        SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(putRowValuesStock);
-
 
258
        Where w = new Where(putRowValuesStock.getTable().getField("ID_DEPOT_STOCK"), "=", depot.getID());
-
 
259
        Where w2 = new Where(putRowValuesStock.getTable().getField("ID_ARTICLE"), "=", article.getID());
-
 
260
        Collection<SQLRowValues> rowValsResult = fetch.fetch(w.and(w2));
-
 
261
        if (rowValsResult.size() == 0) {
-
 
262
            SQLRowValues rowValsStock = new SQLRowValues(stockTable);
-
 
263
            rowValsStock.put("ID_ARTICLE", article.getID());
-
 
264
            rowValsStock.put("ID_DEPOT_STOCK", depot.getID());
-
 
265
            rowValsStock.put("QTE_TH", 0F);
-
 
266
            rowValsStock.put("QTE_REEL", 0F);
-
 
267
            rowValsStock.put("QTE_RECEPT_ATTENTE", 0F);
-
 
268
            rowValsStock.put("QTE_LIV_ATTENTE", 0F);
-
 
269
            try {
-
 
270
                rowStock = rowValsStock.insert();
-
 
271
            } catch (SQLException e) {
-
 
272
                ExceptionHandler.handle("Erreur lors la création du stock!", e);
-
 
273
            }
-
 
274
        } else if (rowValsResult.size() == 1) {
-
 
275
            rowStock = rowValsResult.iterator().next();
-
 
276
        } else if (rowValsResult.size() > 1) {
-
 
277
            throw new IllegalStateException("2 lignes de stocks pour le même dépôt! Article " + article.getID() + " Depot " + depot.getID());
-
 
278
        }
-
 
279
        return rowStock;
-
 
280
 
-
 
281
    }
218
}
282
}