OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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