OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
185 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011-2019 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.core.supplychain.stock.element;
15
 
16
import org.openconcerto.sql.model.FieldPath;
17
import org.openconcerto.sql.model.SQLRowAccessor;
18
import org.openconcerto.sql.model.SQLRowValues;
19
import org.openconcerto.ui.table.AbstractTreeTableModel;
20
import org.openconcerto.ui.table.TreeTableModel;
21
 
22
import java.text.SimpleDateFormat;
23
import java.util.Calendar;
24
import java.util.Date;
25
import java.util.List;
26
 
27
public class StockTreeModel extends AbstractTreeTableModel {
28
    static final SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
29
 
30
    public StockTreeModel(Object root) {
31
        super(root);
32
 
33
    }
34
 
35
    @Override
36
    public Object getChild(Object parent, int index) {
37
        if (parent instanceof StockRootNode) {
38
            return ((StockRootNode) parent).getChild(index);
39
        }
40
        if (parent instanceof StockNode) {
41
            return ((StockNode) parent).getLot(index);
42
        }
43
        return null;
44
    }
45
 
46
    @Override
47
    public int getChildCount(Object parent) {
48
        if (parent instanceof StockRootNode) {
49
            final int size = ((StockRootNode) parent).getSize();
50
            return size;
51
        }
52
        if (parent instanceof StockNode) {
53
            final int size = ((StockNode) parent).getLotSize();
54
            return size;
55
        }
56
        return 0;
57
    }
58
 
59
    @Override
60
    public boolean isLeaf(Object node) {
61
        if (node instanceof StockRootNode) {
62
            return false;
63
        }
64
        if (node instanceof StockNode) {
65
            return ((StockNode) node).getLotSize() == 0;
66
        }
67
        return true;
68
    }
69
 
70
    @Override
71
    public int getColumnCount() {
72
        if (((StockRootNode) getRoot()).hasBatch()) {
73
            return 6;
74
        }
75
        return 5;
76
    }
77
 
78
    @Override
79
    public String getColumnName(int column) {
80
        if (column == 0) {
81
            return "Dépôt";
82
        }
83
        if (column >= 1 && column < 5) {
84
            return ((StockRootNode) getRoot()).getLabelFor(column);
85
        }
86
        if (column == 5) {
87
            return "DLC/DLUO";
88
        }
89
        return "";
90
    }
91
 
92
    @SuppressWarnings("rawtypes")
93
    @Override
94
    public Class getColumnClass(int column) {
95
        if (column == 0) {
96
            return TreeTableModel.class;
97
        }
98
        if (column > 0 && column < 5) {
99
            return Float.class;
100
        }
101
        if (column == 5) {
102
            return Date.class;
103
        }
104
        return String.class;
105
    }
106
 
107
    @Override
108
    public Object getValueAt(Object node, int column) {
109
        if (node instanceof StockRootNode) {
110
            return null;
111
        }
112
        if (node instanceof StockNode) {
113
            final StockNode n = ((StockNode) node);
114
            if (column >= 5) {
115
                return null;
116
            }
117
            final StockRootNode root = ((StockRootNode) getRoot());
118
            final FieldPath fieldPath = root.getFieldPath(column);
119
            return n.getValueAt(fieldPath, column);
120
        }
121
        if (node instanceof BatchNode) {
122
            final BatchNode rLot = (BatchNode) node;
123
            if (column == 4) {
124
                return rLot.getValues().getBigDecimal("QUANTITE").floatValue();
125
            }
126
            if (column == 5) {
127
                final Calendar c1 = rLot.getValues().getDate("DLC");
128
                final Calendar c2 = rLot.getValues().getDate("DUO");
129
                if (c1 == null && c2 != null) {
130
                    return c2.getTime();
131
                }
132
                if (c2 == null && c1 != null) {
133
                    return c1.getTime();
134
                }
135
                if (c1 != null && c2 != null) {
136
                    if (c1.before(c2)) {
137
                        return c1.getTime();
138
                    } else {
139
                        return c2.getTime();
140
                    }
141
                } else {
142
                    return null;
143
                }
144
 
145
            }
146
 
147
        }
148
 
149
        return null;
150
    }
151
 
152
    public void load(SQLRowAccessor article) {
153
        ((StockRootNode) getRoot()).load(article);
154
        fireTreeStructureChanged(this, new Object[] { getRoot() }, new int[] { 0 }, new Object[] {});
155
    }
156
 
157
}
158
 
159
class BatchNode {
160
    private final SQLRowValues sqlRowValues;
161
    private String str;
162
 
163
    public BatchNode(SQLRowValues sqlRowValues) {
164
        this.sqlRowValues = sqlRowValues;
165
        final StringBuilder b = new StringBuilder();
166
        if (!sqlRowValues.getString("NUMERO_LOT").isEmpty()) {
167
            b.append(sqlRowValues.getString("NUMERO_LOT"));
168
 
169
        }
170
        if (!sqlRowValues.getString("NUMERO_SERIE").isEmpty()) {
171
            b.append(sqlRowValues.getString("NUMERO_SERIE"));
172
            b.append(" ");
173
        }
174
 
175
        if (sqlRowValues.getDate("DLC") != null) {
176
            b.append(" DLC: ");
177
            b.append(StockTreeModel.df.format(sqlRowValues.getDate("DLC").getTime()));
178
            b.append(" ");
179
        }
180
        if (sqlRowValues.getDate("DLUO") != null) {
181
            b.append(" DLUO: ");
182
            b.append(StockTreeModel.df.format(sqlRowValues.getDate("DLUO").getTime()));
183
            b.append(" ");
184
        }
185
        this.str = b.toString().trim();
186
    }
187
 
188
    @Override
189
    public String toString() {
190
        return this.str;
191
    }
192
 
193
    public SQLRowValues getValues() {
194
        return this.sqlRowValues;
195
    }
196
}
197
 
198
class StockNode {
199
    private final SQLRowValues sqlRowValues;
200
    private final List<BatchNode> lots;
201
 
202
    public StockNode(SQLRowValues sqlRowValues, List<BatchNode> lots) {
203
        this.sqlRowValues = sqlRowValues;
204
        this.lots = lots;
205
    }
206
 
207
    public int getLotSize() {
208
        if (this.lots == null)
209
            return 0;
210
        else
211
            return this.lots.size();
212
    }
213
 
214
    public Object getValueAt(FieldPath fieldPath, int columnIndex) {
215
        return this.sqlRowValues.followPath(fieldPath.getPath()).getObject(fieldPath.getFieldName());
216
    }
217
 
218
    public BatchNode getLot(int index) {
219
        if (this.lots == null)
220
            return null;
221
        return this.lots.get(index);
222
    }
223
 
224
    @Override
225
    public String toString() {
226
        return this.sqlRowValues.getForeign("ID_DEPOT_STOCK").getString("NOM");
227
    }
228
}