OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
18 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.core.sales.pos.ui;
15
 
144 ilm 16
import org.openconcerto.erp.core.sales.pos.POSConfiguration;
17
import org.openconcerto.erp.core.sales.pos.TicketPrinterConfiguration;
149 ilm 18
import org.openconcerto.erp.core.sales.pos.io.Printable;
144 ilm 19
import org.openconcerto.erp.core.sales.pos.io.TicketPrinter;
151 ilm 20
import org.openconcerto.erp.core.sales.pos.model.Article;
149 ilm 21
import org.openconcerto.erp.core.sales.pos.model.RegisterFiles;
22
import org.openconcerto.erp.core.sales.pos.model.RegisterLog;
144 ilm 23
import org.openconcerto.erp.core.sales.pos.model.Ticket;
24
import org.openconcerto.ui.DefaultListModel;
25
import org.openconcerto.ui.touch.ScrollableList;
26
import org.openconcerto.utils.ExceptionHandler;
151 ilm 27
import org.openconcerto.utils.Pair;
144 ilm 28
 
18 ilm 29
import java.awt.Color;
30
import java.awt.Component;
31
import java.awt.Dimension;
142 ilm 32
import java.awt.FlowLayout;
18 ilm 33
import java.awt.Font;
34
import java.awt.Graphics;
35
import java.awt.Graphics2D;
36
import java.awt.GridBagConstraints;
37
import java.awt.GridBagLayout;
38
import java.awt.Insets;
39
import java.awt.RenderingHints;
142 ilm 40
import java.awt.event.ActionEvent;
41
import java.awt.event.ActionListener;
149 ilm 42
import java.io.IOException;
43
import java.text.ParseException;
44
import java.util.Calendar;
45
import java.util.Date;
46
import java.util.List;
18 ilm 47
 
48
import javax.swing.JLabel;
49
import javax.swing.JList;
50
import javax.swing.JPanel;
51
import javax.swing.JScrollPane;
52
import javax.swing.ListCellRenderer;
53
import javax.swing.ListSelectionModel;
54
import javax.swing.event.ListSelectionEvent;
55
import javax.swing.event.ListSelectionListener;
56
 
149 ilm 57
import org.jdom2.JDOMException;
58
 
18 ilm 59
public class ListeDesTicketsPanel extends JPanel implements ListSelectionListener {
60
 
144 ilm 61
    private static final String ARIAL_FONT = "Arial";
149 ilm 62
 
63
    static private final class X extends RegisterSummary {
64
 
65
        private final List<Ticket> receipts;
66
 
67
        protected X(final RegisterLog log, final List<Ticket> receipts) {
68
            super("X", log);
69
            this.receipts = receipts;
70
        }
71
 
72
        @Override
73
        protected final List<Ticket> getReceipts() {
74
            return this.receipts;
75
        }
76
 
77
        @Override
78
        public String getLabel() {
79
            return "Synthèse actuelle (X)";
80
        }
81
    }
82
 
83
    static private final class XLite extends RegisterSummary {
84
 
85
        private final List<Ticket> receipts;
86
 
87
        protected XLite(final RegisterLog log, final List<Ticket> receipts) {
88
            super("X", log);
89
            this.receipts = receipts;
90
        }
91
 
92
        @Override
93
        public boolean isPrintingTicketsList() {
94
            return false;
95
        }
96
 
97
        @Override
98
        protected final List<Ticket> getReceipts() {
99
            return this.receipts;
100
        }
101
 
102
        @Override
103
        public String getLabel() {
104
            return "Synthèse actuelle simplifiée (résumé du X)";
105
        }
106
    }
107
 
108
    static private final class Z extends RegisterSummary {
109
 
110
        protected Z(final RegisterFiles files, final Calendar day) throws IOException, JDOMException {
111
            super("Z", new RegisterLog(files.getLogFile(day)).parse());
112
        }
113
 
114
        @Override
115
        protected final List<Ticket> getReceipts() throws ParseException {
116
            return this.getLog().parseReceipts();
117
        }
118
 
119
        @Override
120
        public String getLabel() {
121
            return "Synthèse précédente (Z)";
122
        }
123
    }
124
 
18 ilm 125
    private JList l;
126
    private CaisseFrame frame;
142 ilm 127
 
18 ilm 128
    private TextAreaTicketPrinter ticketP;
129
    private ScrollableList ticketList;
130
    private DefaultListModel ticketLlistModel;
131
 
132
    ListeDesTicketsPanel(CaisseFrame caisseFrame) {
133
        this.frame = caisseFrame;
134
        this.setBackground(Color.WHITE);
135
        this.setOpaque(true);
136
        this.setLayout(new GridBagLayout());
137
        GridBagConstraints c = new GridBagConstraints();
138
        c.fill = GridBagConstraints.BOTH;
139
        c.anchor = GridBagConstraints.CENTER;
140
        c.weightx = 1;
141
        c.weighty = 0;
142
        c.gridx = 0;
143
        c.gridy = 0;
144
        c.gridwidth = 2;
145
 
146
        // Place pour le menu
142 ilm 147
        StatusBar p = new StatusBar();
148
        p.setTitle("Liste des tickets");
149
        p.setLayout(new FlowLayout(FlowLayout.RIGHT));
150
        POSButton bBack = new POSButton("Fermer");
151
        p.add(bBack);
152
 
18 ilm 153
        this.add(p, c);
154
 
155
        // Liste des tickets
156
        c.gridy++;
157
        c.gridwidth = 1;
158
        c.weighty = 1;
159
        c.gridheight = 2;
160
 
144 ilm 161
        try {
149 ilm 162
            final RegisterLog lastLog = this.frame.getFiles().getLastLog();
163
            final List<Ticket> receipts = lastLog.parseReceipts();
164
            ticketLlistModel = new DefaultListModel();
165
            ticketLlistModel.addElement(new X(lastLog, receipts));
166
            ticketLlistModel.addElement(new XLite(lastLog, receipts));
167
            final Date previousDate = lastLog.getFirstRegisterEvent().getPreviousDate();
168
            if (previousDate != null) {
169
                final Calendar cal = Calendar.getInstance();
170
                cal.setTime(previousDate);
171
                ticketLlistModel.addElement(new Z(this.frame.getFiles(), cal));
172
            }
173
            ticketLlistModel.addAll(receipts);
174
 
144 ilm 175
        } catch (Exception exn) {
176
            ExceptionHandler.handle(this.frame, "Impossible de charger les tickets", exn);
177
        }
178
        final Font f = new Font(ARIAL_FONT, Font.PLAIN, 24);
18 ilm 179
        ticketList = new ScrollableList(ticketLlistModel) {
180
            @Override
181
            public void paintCell(Graphics g, Object object, int index, boolean isSelected, int posY) {
182
                g.setFont(f);
183
 
184
                if (isSelected) {
185
                    g.setColor(new Color(232, 242, 254));
186
                } else {
187
                    g.setColor(Color.WHITE);
188
                }
189
                g.fillRect(0, posY, getWidth(), getCellHeight());
190
 
191
                //
192
                g.setColor(Color.GRAY);
193
                g.drawLine(0, posY + this.getCellHeight() - 1, this.getWidth(), posY + this.getCellHeight() - 1);
194
 
195
                if (isSelected) {
196
                    g.setColor(Color.BLACK);
197
                } else {
198
                    g.setColor(Color.GRAY);
199
                }
200
                ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
149 ilm 201
                if (object instanceof Ticket) {
202
                    Ticket article = (Ticket) object;
203
                    String label = "Ticket " + article.getCode();
18 ilm 204
 
149 ilm 205
                    String euro = TicketCellRenderer.centsToString(article.getTotalInCents()) + "€";
18 ilm 206
 
149 ilm 207
                    int wEuro = (int) g.getFontMetrics().getStringBounds(euro, g).getWidth();
208
                    g.drawString(label, 10, posY + 39);
209
                    g.drawString(euro, getWidth() - 5 - wEuro, posY + 39);
210
                } else {
211
                    g.drawString(((RegisterSummary) object).getLabel(), 10, posY + 39);
212
                }
18 ilm 213
            }
214
        };
215
        this.add(ticketList, c);
216
        // Ticket
217
        c.fill = GridBagConstraints.VERTICAL;
218
        c.gridx++;
219
        c.gridheight = 1;
220
        c.insets = new Insets(10, 10, 10, 10);
221
        ticketP = new TextAreaTicketPrinter();
222
 
223
        JScrollPane scrollPane = new JScrollPane(ticketP);
224
        scrollPane.setPreferredSize(new Dimension(400, 200));
225
        scrollPane.setMinimumSize(new Dimension(400, 200));
226
        this.add(scrollPane, c);
227
 
228
        ticketList.addListSelectionListener(new ListSelectionListener() {
229
 
230
            @Override
231
            public void valueChanged(ListSelectionEvent e) {
232
                Object selectedValue = ticketList.getSelectedValue();
233
                setSelectedTicket(selectedValue);
234
            }
235
        });
236
 
237
        // Menu
238
 
239
        c.gridy++;
240
        c.weighty = 0;
241
        c.fill = GridBagConstraints.NONE;
144 ilm 242
        final Font font = new Font(ARIAL_FONT, Font.PLAIN, 46);
151 ilm 243
        l = new JList(new String[] { "Imprimer", "Annuler" });
18 ilm 244
        l.setCellRenderer(new ListCellRenderer() {
245
 
246
            @Override
247
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
248
                JLabel l = new JLabel(value.toString()) {
249
                    @Override
250
                    public void paint(Graphics g) {
251
 
252
                        super.paint(g);
253
 
254
                        g.setColor(Color.LIGHT_GRAY);
255
                        g.drawLine(0, 0, this.getWidth(), 0);
256
                    }
257
                };
258
                l.setFont(font);
259
                return l;
260
            }
261
 
262
        });
263
        l.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
264
        l.getSelectionModel().addListSelectionListener(this);
265
 
151 ilm 266
        l.setFixedCellHeight(160);
18 ilm 267
        this.add(l, c);
142 ilm 268
 
144 ilm 269
        setFont(new Font(ARIAL_FONT, Font.BOLD, 24));
142 ilm 270
 
271
        // Listeners
272
        bBack.addActionListener(new ActionListener() {
273
 
274
            @Override
275
            public void actionPerformed(ActionEvent e) {
276
                frame.showCaisse();
277
 
278
            }
279
        });
280
 
18 ilm 281
    }
282
 
283
    @Override
284
    public void valueChanged(ListSelectionEvent e) {
285
        if (e.getValueIsAdjusting()) {
286
            return;
287
        }
288
        Object selectedValue = ticketList.getSelectedValue();
289
        int selectedIndex = l.getSelectedIndex();
144 ilm 290
        if (selectedIndex == 0 && selectedValue != null) {
149 ilm 291
            POSConfiguration.getInstance().printOnceOnFirstPrinter(((Printable) selectedValue));
151 ilm 292
        } else if (selectedIndex == 1 && selectedValue != null) {
293
            // Annulation du ticket
294
            Ticket t = (Ticket) selectedValue;
295
            for (Pair<Article, Integer> a : t.getArticles()) {
296
                frame.getControler().addArticle(a.getFirst());
297
                frame.getControler().setArticleCount(a.getFirst(), -a.getSecond());
298
                frame.getControler().setArticleHT(a.getFirst(), a.getFirst().getPriceWithoutTax());
299
            }
300
            frame.showCaisse();
18 ilm 301
        }
65 ilm 302
        l.clearSelection();
18 ilm 303
    }
304
 
305
    public void setSelectedTicket(Object selectedValue) {
306
        ticketP.clear();
307
        if (selectedValue != null) {
149 ilm 308
            POSConfiguration.getInstance().print(((Printable) selectedValue), new TicketPrinterConfiguration() {
142 ilm 309
                @Override
310
                public TicketPrinter createTicketPrinter() {
311
                    return ticketP;
312
                }
313
 
314
                @Override
315
                public int getCopyCount() {
316
                    return 1;
317
                }
318
 
319
                @Override
320
                public boolean isValid() {
321
                    return true;
322
                }
323
            });
18 ilm 324
            try {
325
                ticketP.printBuffer();
326
            } catch (Exception e1) {
327
                e1.printStackTrace();
328
            }
329
        }
330
        ticketList.setSelectedValue(selectedValue, true);
331
    }
332
}