140 |
ilm |
1 |
package org.openconcerto.modules.label;
|
|
|
2 |
|
|
|
3 |
import java.awt.Color;
|
|
|
4 |
import java.awt.Graphics;
|
|
|
5 |
import java.awt.event.ActionEvent;
|
|
|
6 |
import java.awt.geom.Rectangle2D;
|
181 |
ilm |
7 |
import java.io.File;
|
140 |
ilm |
8 |
import java.io.IOException;
|
181 |
ilm |
9 |
import java.nio.charset.StandardCharsets;
|
|
|
10 |
import java.util.ArrayList;
|
183 |
ilm |
11 |
import java.util.HashMap;
|
181 |
ilm |
12 |
import java.util.LinkedHashMap;
|
140 |
ilm |
13 |
import java.util.List;
|
183 |
ilm |
14 |
import java.util.Map;
|
181 |
ilm |
15 |
import java.util.Map.Entry;
|
|
|
16 |
import java.util.TreeMap;
|
140 |
ilm |
17 |
|
|
|
18 |
import javax.swing.AbstractAction;
|
181 |
ilm |
19 |
import javax.swing.JFrame;
|
140 |
ilm |
20 |
import javax.swing.SwingWorker;
|
|
|
21 |
|
|
|
22 |
import org.openconcerto.erp.generationDoc.provider.AdresseFullClientValueProvider;
|
|
|
23 |
import org.openconcerto.erp.modules.AbstractModule;
|
|
|
24 |
import org.openconcerto.erp.modules.ComponentsContext;
|
|
|
25 |
import org.openconcerto.erp.modules.ModuleFactory;
|
183 |
ilm |
26 |
import org.openconcerto.sql.model.SQLField;
|
181 |
ilm |
27 |
import org.openconcerto.sql.model.SQLRow;
|
140 |
ilm |
28 |
import org.openconcerto.sql.model.SQLRowAccessor;
|
|
|
29 |
import org.openconcerto.sql.model.SQLRowValues;
|
|
|
30 |
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
|
|
|
31 |
import org.openconcerto.sql.model.SQLTable;
|
|
|
32 |
import org.openconcerto.sql.model.Where;
|
|
|
33 |
import org.openconcerto.sql.view.list.IListe;
|
|
|
34 |
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
|
|
|
35 |
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
|
|
|
36 |
import org.openconcerto.utils.ExceptionHandler;
|
181 |
ilm |
37 |
import org.openconcerto.utils.FileUtils;
|
140 |
ilm |
38 |
import org.openconcerto.utils.GestionDevise;
|
|
|
39 |
import org.openconcerto.utils.StringUtils;
|
|
|
40 |
|
|
|
41 |
public final class ModuleLabel extends AbstractModule {
|
181 |
ilm |
42 |
final LinkedHashMap<String, String> zplTemplates = new LinkedHashMap<String, String>();
|
183 |
ilm |
43 |
final LinkedHashMap<String, String> gplTemplates = new LinkedHashMap<String, String>();
|
|
|
44 |
private Map<String, File> dirMap = new HashMap<>();
|
140 |
ilm |
45 |
|
|
|
46 |
public ModuleLabel(ModuleFactory f) throws IOException {
|
|
|
47 |
super(f);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
@Override
|
|
|
51 |
protected void setupComponents(ComponentsContext ctxt) {
|
181 |
ilm |
52 |
readTemplates(new File("Template/Labels"));
|
|
|
53 |
readTemplates(new File("Configuration/Template/Labels"));
|
|
|
54 |
|
140 |
ilm |
55 |
final String actionName = "Imprimer les étiquettes";
|
|
|
56 |
final PredicateRowAction aArticle = new PredicateRowAction(new AbstractAction(actionName) {
|
|
|
57 |
|
|
|
58 |
@Override
|
|
|
59 |
public void actionPerformed(ActionEvent arg0) {
|
|
|
60 |
final IListe list = IListe.get(arg0);
|
|
|
61 |
final List<Integer> selectedIDs = list.getSelection().getSelectedIDs();
|
183 |
ilm |
62 |
final SQLTable tArticle = list.getSelectedRowAccessors().get(0).getTable();
|
181 |
ilm |
63 |
final SwingWorker<List<RowValuesLabel>, String> wworker = new SwingWorker<List<RowValuesLabel>, String>() {
|
140 |
ilm |
64 |
|
|
|
65 |
@Override
|
181 |
ilm |
66 |
protected List<RowValuesLabel> doInBackground() throws Exception {
|
140 |
ilm |
67 |
final SQLRowValues graph = new SQLRowValues(tArticle);
|
|
|
68 |
graph.putNulls("NOM", "PV_TTC");
|
|
|
69 |
final SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(graph);
|
181 |
ilm |
70 |
final List<SQLRowValues> rows = fetcher.fetch(new Where(tArticle.getKey(), selectedIDs));
|
|
|
71 |
final List<RowValuesLabel> list = new ArrayList<>(rows.size());
|
|
|
72 |
for (SQLRowValues row : rows) {
|
|
|
73 |
list.add(new RowValuesLabel(row));
|
|
|
74 |
}
|
|
|
75 |
return list;
|
140 |
ilm |
76 |
}
|
|
|
77 |
|
181 |
ilm |
78 |
@Override
|
140 |
ilm |
79 |
protected void done() {
|
|
|
80 |
try {
|
181 |
ilm |
81 |
final List<RowValuesLabel> values = get();
|
140 |
ilm |
82 |
|
|
|
83 |
final LabelFrame f = new LabelFrame(values, new LabelRenderer() {
|
|
|
84 |
|
|
|
85 |
@Override
|
181 |
ilm |
86 |
public void paintLabel(Graphics g, Label label, int x, int y, int gridWith, int gridHeight, float fontSize) {
|
140 |
ilm |
87 |
g.setColor(Color.BLACK);
|
|
|
88 |
g.setFont(g.getFont().deriveFont(fontSize));
|
|
|
89 |
// Labels borders
|
|
|
90 |
final int hBorder = 12;
|
|
|
91 |
final int vBorder = 8;
|
|
|
92 |
// Product name
|
181 |
ilm |
93 |
SQLRowValues row = ((RowValuesLabel) label).getSQLRowValues();
|
140 |
ilm |
94 |
final String text = row.getString("NOM");
|
|
|
95 |
final List<String> l = StringUtils.wrap(text, g.getFontMetrics(), gridWith - 2 * hBorder);
|
|
|
96 |
final int lineHeight = g.getFontMetrics().getHeight();
|
|
|
97 |
int lineY = y;
|
|
|
98 |
final int margin = gridHeight - l.size() * lineHeight;
|
|
|
99 |
if (margin > 0) {
|
181 |
ilm |
100 |
lineY += margin / 2;
|
140 |
ilm |
101 |
}
|
|
|
102 |
for (String line : l) {
|
|
|
103 |
g.drawString(line, x + hBorder, lineY);
|
|
|
104 |
lineY += lineHeight;
|
|
|
105 |
}
|
|
|
106 |
// Price
|
|
|
107 |
g.setFont(g.getFont().deriveFont(fontSize + 2));
|
|
|
108 |
final String price = GestionDevise.currencyToString(row.getBigDecimal("PV_TTC")) + " € TTC";
|
|
|
109 |
final Rectangle2D r2 = g.getFont().getStringBounds(price, g.getFontMetrics().getFontRenderContext());
|
|
|
110 |
g.drawString(price, x + (int) (gridWith - hBorder - r2.getWidth()), y + gridHeight - vBorder);
|
|
|
111 |
|
|
|
112 |
}
|
|
|
113 |
});
|
|
|
114 |
f.setTitle(actionName);
|
|
|
115 |
f.setLocationRelativeTo(null);
|
|
|
116 |
f.pack();
|
|
|
117 |
f.setResizable(false);
|
|
|
118 |
f.setVisible(true);
|
|
|
119 |
} catch (Exception e) {
|
|
|
120 |
ExceptionHandler.handle("Erreur d'impression", e);
|
|
|
121 |
}
|
181 |
ilm |
122 |
}
|
140 |
ilm |
123 |
};
|
|
|
124 |
wworker.execute();
|
|
|
125 |
|
|
|
126 |
}
|
|
|
127 |
}, true, false);
|
|
|
128 |
final PredicateRowAction aClient = new PredicateRowAction(new AbstractAction(actionName) {
|
|
|
129 |
|
|
|
130 |
@Override
|
|
|
131 |
public void actionPerformed(ActionEvent arg0) {
|
|
|
132 |
final IListe list = IListe.get(arg0);
|
|
|
133 |
final List<Integer> selectedIDs = list.getSelection().getSelectedIDs();
|
181 |
ilm |
134 |
final SQLTable tClient = list.getSelectedRows().get(0).getTable();
|
|
|
135 |
final SwingWorker<List<RowValuesLabel>, String> wworker = new SwingWorker<List<RowValuesLabel>, String>() {
|
140 |
ilm |
136 |
|
|
|
137 |
@Override
|
181 |
ilm |
138 |
protected List<RowValuesLabel> doInBackground() throws Exception {
|
140 |
ilm |
139 |
final SQLRowValues graph = new SQLRowValues(tClient);
|
|
|
140 |
graph.putNulls("NOM");
|
|
|
141 |
final SQLRowValues a1 = graph.putRowValues("ID_ADRESSE");
|
|
|
142 |
a1.putNulls(a1.getTable().getFieldsName());
|
|
|
143 |
final SQLRowValues a2 = graph.putRowValues("ID_ADRESSE_L");
|
|
|
144 |
a2.putNulls(a2.getTable().getFieldsName());
|
|
|
145 |
final SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(graph);
|
181 |
ilm |
146 |
final List<SQLRowValues> rows = fetcher.fetch(new Where(tClient.getKey(), selectedIDs));
|
|
|
147 |
|
|
|
148 |
final List<RowValuesLabel> list = new ArrayList<>(rows.size());
|
|
|
149 |
for (SQLRowValues row : rows) {
|
|
|
150 |
list.add(new RowValuesLabel(row));
|
|
|
151 |
}
|
|
|
152 |
return list;
|
140 |
ilm |
153 |
}
|
|
|
154 |
|
181 |
ilm |
155 |
@Override
|
140 |
ilm |
156 |
protected void done() {
|
|
|
157 |
try {
|
181 |
ilm |
158 |
final List<RowValuesLabel> values = get();
|
140 |
ilm |
159 |
final LabelFrame f = new LabelFrame(values, new LabelRenderer() {
|
|
|
160 |
|
|
|
161 |
@Override
|
181 |
ilm |
162 |
public void paintLabel(Graphics g, Label label, int x, int y, int gridWith, int gridHeight, float fontSize) {
|
|
|
163 |
SQLRowValues row = ((RowValuesLabel) label).getSQLRowValues();
|
140 |
ilm |
164 |
SQLRowAccessor rAddr = row.getForeign("ID_ADRESSE_L");
|
|
|
165 |
if (rAddr == null || rAddr.isUndefined()) {
|
|
|
166 |
rAddr = row.getForeign("ID_ADRESSE");
|
|
|
167 |
}
|
|
|
168 |
if (rAddr == null || rAddr.isUndefined()) {
|
|
|
169 |
return;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
String text = AdresseFullClientValueProvider.getFormattedAddress(rAddr, row.getString("NOM") + "\n");
|
|
|
173 |
|
|
|
174 |
// Default font at 10pt black
|
|
|
175 |
g.setColor(Color.BLACK);
|
|
|
176 |
g.setFont(g.getFont().deriveFont(fontSize));
|
|
|
177 |
// Labels borders
|
|
|
178 |
final int hBorder = 12;
|
|
|
179 |
// Product name
|
|
|
180 |
final List<String> l = StringUtils.wrap(text, g.getFontMetrics(), gridWith - 2 * hBorder);
|
|
|
181 |
final int lineHeight = g.getFontMetrics().getHeight();
|
|
|
182 |
int lineY = y + lineHeight + 4;
|
|
|
183 |
for (String line : l) {
|
|
|
184 |
g.drawString(line, x + hBorder, lineY);
|
|
|
185 |
lineY += lineHeight;
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
}
|
|
|
189 |
});
|
|
|
190 |
f.setTitle(actionName);
|
|
|
191 |
f.setLocationRelativeTo(null);
|
|
|
192 |
f.pack();
|
|
|
193 |
f.setResizable(false);
|
|
|
194 |
f.setVisible(true);
|
|
|
195 |
|
|
|
196 |
} catch (Exception e) {
|
|
|
197 |
ExceptionHandler.handle("Erreur d'impression", e);
|
|
|
198 |
}
|
181 |
ilm |
199 |
}
|
140 |
ilm |
200 |
};
|
|
|
201 |
wworker.execute();
|
|
|
202 |
|
|
|
203 |
}
|
|
|
204 |
}, true, false);
|
|
|
205 |
|
|
|
206 |
aArticle.setPredicate(IListeEvent.createSelectionCountPredicate(1, Integer.MAX_VALUE));
|
|
|
207 |
aClient.setPredicate(IListeEvent.createSelectionCountPredicate(1, Integer.MAX_VALUE));
|
|
|
208 |
ctxt.getElement("ARTICLE").getRowActions().add(aArticle);
|
|
|
209 |
ctxt.getElement("CLIENT").getRowActions().add(aClient);
|
181 |
ilm |
210 |
|
|
|
211 |
if (!this.zplTemplates.isEmpty()) {
|
|
|
212 |
for (final Entry<String, String> entry : this.zplTemplates.entrySet()) {
|
|
|
213 |
final String zpl = entry.getValue();
|
|
|
214 |
final PredicateRowAction action = new PredicateRowAction(new AbstractAction("Imprimer l'étiquette " + entry.getKey()) {
|
|
|
215 |
|
|
|
216 |
@Override
|
|
|
217 |
public void actionPerformed(ActionEvent arg0) {
|
|
|
218 |
final ZPLPrinterPanel p = new ZPLPrinterPanel(zpl);
|
|
|
219 |
final JFrame f = new JFrame();
|
|
|
220 |
final IListe list = IListe.get(arg0);
|
|
|
221 |
final int idProduct = list.getSelection().getSelectedID();
|
183 |
ilm |
222 |
final SQLTable tArticle = list.getSelectedRowAccessors().get(0).getTable();
|
181 |
ilm |
223 |
|
|
|
224 |
final SwingWorker<SQLRowValues, String> wworker = new SwingWorker<SQLRowValues, String>() {
|
|
|
225 |
|
|
|
226 |
@Override
|
|
|
227 |
protected SQLRowValues doInBackground() throws Exception {
|
183 |
ilm |
228 |
SQLRowValues rArticle = new SQLRowValues(tArticle);
|
|
|
229 |
rArticle.putNulls(tArticle.getFieldsName());
|
|
|
230 |
for (SQLField f : tArticle.getFields()) {
|
|
|
231 |
if (f.getName().startsWith("ID_ARTICLE_DECLINAISON_")) {
|
|
|
232 |
SQLRowValues rowValsDecl = rArticle.putRowValues(f.getName());
|
|
|
233 |
rowValsDecl.putNulls(f.getForeignTable().getFieldsName());
|
|
|
234 |
}
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
final List<SQLRowValues> fetchRow = SQLRowValuesListFetcher.create(rArticle).fetch(new Where(tArticle.getKey(), "=", idProduct));
|
|
|
238 |
return fetchRow.get(0);
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
@Override
|
|
|
242 |
protected void done() {
|
|
|
243 |
try {
|
|
|
244 |
final SQLRowValues values = get();
|
|
|
245 |
p.initUI(values);
|
|
|
246 |
f.setTitle(entry.getKey());
|
|
|
247 |
f.setContentPane(p);
|
|
|
248 |
f.pack();
|
|
|
249 |
f.setLocationRelativeTo(null);
|
|
|
250 |
f.setVisible(true);
|
|
|
251 |
|
|
|
252 |
} catch (Exception e) {
|
|
|
253 |
ExceptionHandler.handle("Erreur d'impression", e);
|
|
|
254 |
}
|
|
|
255 |
}
|
|
|
256 |
};
|
|
|
257 |
wworker.execute();
|
|
|
258 |
|
|
|
259 |
}
|
|
|
260 |
}, true, false);
|
|
|
261 |
|
|
|
262 |
action.setPredicate(IListeEvent.createSelectionCountPredicate(1, 1));
|
|
|
263 |
ctxt.getElement("ARTICLE").getRowActions().add(action);
|
|
|
264 |
}
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
if (!gplTemplates.isEmpty()) {
|
|
|
268 |
for (final Entry<String, String> entry : gplTemplates.entrySet()) {
|
|
|
269 |
final String gpl = entry.getValue();
|
|
|
270 |
final PredicateRowAction action = new PredicateRowAction(new AbstractAction("Imprimer l'étiquette " + entry.getKey()) {
|
|
|
271 |
|
|
|
272 |
@Override
|
|
|
273 |
public void actionPerformed(ActionEvent arg0) {
|
|
|
274 |
final GPLPrinterPanel p = new GPLPrinterPanel(gpl, getDir(entry.getKey()));
|
|
|
275 |
final JFrame f = new JFrame();
|
|
|
276 |
final IListe list = IListe.get(arg0);
|
|
|
277 |
final int idProduct = list.getSelection().getSelectedID();
|
|
|
278 |
final SQLTable tArticle = list.getSelectedRowAccessors().get(0).getTable();
|
|
|
279 |
|
|
|
280 |
final SwingWorker<SQLRowValues, String> wworker = new SwingWorker<SQLRowValues, String>() {
|
|
|
281 |
|
|
|
282 |
@Override
|
|
|
283 |
protected SQLRowValues doInBackground() throws Exception {
|
181 |
ilm |
284 |
final SQLRow row = tArticle.getRow(idProduct);
|
|
|
285 |
row.fetchValues();
|
|
|
286 |
return row.asRowValues();
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
@Override
|
|
|
290 |
protected void done() {
|
|
|
291 |
try {
|
|
|
292 |
final SQLRowValues values = get();
|
|
|
293 |
p.initUI(values);
|
|
|
294 |
f.setTitle(entry.getKey());
|
|
|
295 |
f.setContentPane(p);
|
|
|
296 |
f.pack();
|
|
|
297 |
f.setLocationRelativeTo(null);
|
|
|
298 |
f.setVisible(true);
|
|
|
299 |
|
|
|
300 |
} catch (Exception e) {
|
|
|
301 |
ExceptionHandler.handle("Erreur d'impression", e);
|
|
|
302 |
}
|
|
|
303 |
}
|
|
|
304 |
};
|
|
|
305 |
wworker.execute();
|
|
|
306 |
|
|
|
307 |
}
|
|
|
308 |
}, true, false);
|
|
|
309 |
|
|
|
310 |
action.setPredicate(IListeEvent.createSelectionCountPredicate(1, 1));
|
|
|
311 |
ctxt.getElement("ARTICLE").getRowActions().add(action);
|
|
|
312 |
}
|
|
|
313 |
}
|
|
|
314 |
|
140 |
ilm |
315 |
}
|
|
|
316 |
|
183 |
ilm |
317 |
protected File getDir(String value) {
|
|
|
318 |
return dirMap.get(value);
|
|
|
319 |
}
|
|
|
320 |
|
140 |
ilm |
321 |
@Override
|
|
|
322 |
protected void start() {
|
181 |
ilm |
323 |
|
140 |
ilm |
324 |
}
|
|
|
325 |
|
181 |
ilm |
326 |
private void readTemplates(File templatesDir) {
|
|
|
327 |
System.out.println("ModuleLabel.readTemplates() " + templatesDir.getAbsolutePath());
|
|
|
328 |
if (templatesDir.exists() && templatesDir.isDirectory()) {
|
|
|
329 |
System.err.println("ModuleLabel.readTemplates() " + templatesDir.getAbsolutePath());
|
|
|
330 |
File[] files = templatesDir.listFiles();
|
|
|
331 |
if (files != null) {
|
183 |
ilm |
332 |
LinkedHashMap<String, String> zmap = new LinkedHashMap<>();
|
|
|
333 |
LinkedHashMap<String, String> gmap = new LinkedHashMap<>();
|
181 |
ilm |
334 |
for (File f : files) {
|
|
|
335 |
if (f.getName().endsWith(".zpl")) {
|
|
|
336 |
try {
|
|
|
337 |
String zpl = FileUtils.read(f, StandardCharsets.UTF_8);
|
|
|
338 |
String name = f.getName().substring(0, f.getName().length() - 4).trim();
|
183 |
ilm |
339 |
|
|
|
340 |
zmap.put(name, zpl);
|
|
|
341 |
dirMap.put(name, templatesDir);
|
|
|
342 |
|
181 |
ilm |
343 |
System.err.println("ModuleLabel.readTemplates() add " + name);
|
183 |
ilm |
344 |
|
181 |
ilm |
345 |
} catch (Exception e) {
|
|
|
346 |
System.err.println(this.getClass().getCanonicalName() + "start() cannot read zpl template : " + f.getAbsolutePath() + " : " + e.getMessage());
|
|
|
347 |
}
|
183 |
ilm |
348 |
} else if (f.getName().endsWith(".graphicspl")) {
|
|
|
349 |
try {
|
|
|
350 |
String zpl = FileUtils.read(f, StandardCharsets.UTF_8);
|
|
|
351 |
String name = f.getName().substring(0, f.getName().length() - ".graphicspl".length()).trim();
|
|
|
352 |
gmap.put(name, zpl);
|
|
|
353 |
dirMap.put(name, templatesDir);
|
|
|
354 |
|
|
|
355 |
} catch (Exception e) {
|
|
|
356 |
System.err.println(this.getClass().getCanonicalName() + "start() cannot read graphicspl template : " + f.getAbsolutePath() + " : " + e.getMessage());
|
|
|
357 |
}
|
181 |
ilm |
358 |
}
|
|
|
359 |
}
|
|
|
360 |
// Tri de la map par clef
|
183 |
ilm |
361 |
final TreeMap<String, String> copy1 = new TreeMap<>(zmap);
|
|
|
362 |
|
|
|
363 |
zplTemplates.clear();
|
|
|
364 |
zplTemplates.putAll(copy1);
|
|
|
365 |
|
|
|
366 |
final TreeMap<String, String> copy2 = new TreeMap<>(gmap);
|
|
|
367 |
gplTemplates.clear();
|
|
|
368 |
gplTemplates.putAll(copy2);
|
|
|
369 |
|
181 |
ilm |
370 |
}
|
|
|
371 |
|
|
|
372 |
} else {
|
|
|
373 |
System.err.println("ModuleLabel.readTemplates() " + templatesDir.getAbsolutePath() + " missing");
|
|
|
374 |
}
|
|
|
375 |
}
|
|
|
376 |
|
140 |
ilm |
377 |
@Override
|
|
|
378 |
protected void stop() {
|
181 |
ilm |
379 |
// nothing
|
140 |
ilm |
380 |
}
|
|
|
381 |
}
|