OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | 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
 
149 ilm 16
import org.openconcerto.erp.core.sales.pos.io.Printable;
17
import org.openconcerto.erp.core.sales.pos.io.TicketPrinter;
144 ilm 18
import org.openconcerto.erp.core.sales.pos.model.RegisterLog;
19
import org.openconcerto.task.config.ComptaBasePropsConfiguration;
20
import org.openconcerto.utils.ExceptionHandler;
149 ilm 21
import org.openconcerto.utils.FileUtils;
144 ilm 22
import org.openconcerto.utils.JImage;
149 ilm 23
import org.openconcerto.utils.StringUtils;
144 ilm 24
 
18 ilm 25
import java.awt.Color;
26
import java.awt.GridBagConstraints;
27
import java.awt.GridBagLayout;
142 ilm 28
import java.awt.Insets;
29
import java.awt.event.ActionEvent;
30
import java.awt.event.ActionListener;
149 ilm 31
import java.io.File;
32
import java.io.IOException;
33
import java.util.List;
18 ilm 34
 
35
import javax.swing.JPanel;
36
 
142 ilm 37
public class CaisseMenuPanel extends JPanel {
38
 
18 ilm 39
    private CaisseFrame frame;
40
 
144 ilm 41
    CaisseMenuPanel(final CaisseFrame caisseFrame) {
18 ilm 42
        this.frame = caisseFrame;
43
        this.setBackground(Color.WHITE);
44
        this.setOpaque(true);
45
        this.setLayout(new GridBagLayout());
142 ilm 46
        final GridBagConstraints c = new GridBagConstraints();
47
        c.fill = GridBagConstraints.HORIZONTAL;
18 ilm 48
        c.anchor = GridBagConstraints.CENTER;
142 ilm 49
        c.weightx = 0;
50
        c.weighty = 0;
18 ilm 51
        c.gridx = 0;
52
        c.gridy = 0;
142 ilm 53
        c.insets = new Insets(20, 20, 30, 20);
54
        final JImage image = new JImage(ComptaBasePropsConfiguration.class.getResource("logo.png"));
55
        this.add(image, c);
56
        c.gridx++;
149 ilm 57
        File testTicketDir = new File("test");
58
        if (testTicketDir.exists() && testTicketDir.isDirectory()) {
59
            File[] files = testTicketDir.listFiles();
60
            for (int i = 0; i < files.length; i++) {
61
                final File f = files[i];
62
                if (f.getName().endsWith(".txt")) {
63
                    final POSButton b = new POSButton(f.getName().substring(0, f.getName().length() - 4));
64
                    this.add(b, c);
65
                    c.gridy++;
66
                    b.addActionListener(new ActionListener() {
18 ilm 67
 
149 ilm 68
                        @Override
69
                        public void actionPerformed(ActionEvent e) {
70
                            System.err.println("CaisseMenuPanel.CaisseMenuPanel(...).new ActionListener() {...}.actionPerformed()");
156 ilm 71
                            caisseFrame.getPOSConf().printOnceOnFirstPrinter(new Printable() {
149 ilm 72
 
73
                                @Override
74
                                public void print(TicketPrinter prt, int ticketWidth) {
75
                                    prt.clearBuffer(f.getName());
76
                                    System.err.println("CaisseMenuPanel.CaisseMenuPanel(...)print()");
77
                                    String content = f.getName();
78
                                    try {
79
                                        content = FileUtils.read(f);
80
                                        List<String> lines = StringUtils.splitIntoLines(content);
81
                                        for (String line : lines) {
82
                                            line = line.trim();
83
                                            System.err.println("CaisseMenuPanel.CaisseMenuPanel(...)print()" + line);
84
                                            if (line.startsWith("[bl]")) {
85
                                                prt.addToBuffer(line.substring(3), TicketPrinter.BOLD_LARGE);
86
                                            } else if (line.startsWith("[b]")) {
87
                                                prt.addToBuffer(line.substring(3), TicketPrinter.BOLD);
88
                                            } else {
89
                                                prt.addToBuffer(line);
90
                                            }
91
                                        }
92
                                    } catch (IOException e1) {
93
                                        prt.addToBuffer(content);
94
                                        prt.addToBuffer(e1.getMessage());
95
                                        e1.printStackTrace();
96
                                    }
97
                                    try {
98
                                        prt.printBuffer();
99
                                    } catch (final Exception e) {
100
                                        e.printStackTrace();
101
                                    }
102
                                }
103
                            });
104
 
105
                        }
106
                    });
107
                }
108
 
109
            }
110
        }
111
 
142 ilm 112
        final POSButton bTickets = new POSButton("Liste des tickets");
113
        this.add(bTickets, c);
114
        c.gridy++;
115
        final POSButton bCloture = new POSButton("Clôturer");
116
        this.add(bCloture, c);
117
        c.gridy++;
118
        c.insets = new Insets(20, 20, 20, 20);
119
        c.fill = GridBagConstraints.NONE;
120
        c.anchor = GridBagConstraints.EAST;
121
        final POSButton bQuit = new POSButton("Quitter");
122
        bQuit.setBackground(Color.decode("#AD1457"));
123
        this.add(bQuit, c);
124
        // Listeners
125
        bTickets.addActionListener(new ActionListener() {
126
 
18 ilm 127
            @Override
142 ilm 128
            public void actionPerformed(ActionEvent e) {
129
                try {
130
                    frame.showTickets(null);
131
                } catch (Exception ex) {
132
                    ExceptionHandler.handle("Erreur", ex);
133
                }
134
            }
135
        });
149 ilm 136
 
142 ilm 137
        bCloture.addActionListener(new ActionListener() {
18 ilm 138
 
142 ilm 139
            @Override
140
            public void actionPerformed(ActionEvent e) {
151 ilm 141
                final JPanel p = new JPanel();
149 ilm 142
                p.setOpaque(true);
143
                p.setLayout(new GridBagLayout());
144
                final GridBagConstraints c = new GridBagConstraints();
145
                c.fill = GridBagConstraints.HORIZONTAL;
146
                c.anchor = GridBagConstraints.WEST;
147
                c.weightx = 0;
148
                c.weighty = 0;
149
                c.gridx = 0;
150
                c.gridy = 0;
151
                c.insets = new Insets(20, 20, 30, 20);
152
                c.gridwidth = 2;
153
                p.add(new POSLabel("Voulez vous clôturer la caisse ?"), c);
154
                c.gridy++;
155
                c.gridwidth = 1;
156
                POSButton bOkCloture = new POSButton("Clôturer");
157
                bOkCloture.addActionListener(new ActionListener() {
18 ilm 158
 
149 ilm 159
                    @Override
160
                    public void actionPerformed(ActionEvent e) {
161
                        try {
177 ilm 162
                            final boolean quit = caisseFrame.getDB().fetchRegisterState().checkIfMoved(caisseFrame.getConf().getERP_TM());
149 ilm 163
                            if (!quit) {
164
                                frame.getControler().setLCD("Cloture", "En cours...", 0);
156 ilm 165
                                final int userID = caisseFrame.getPOSConf().getUserID();
149 ilm 166
                                final RegisterLog newLog = caisseFrame.getFiles().close(userID);
156 ilm 167
                                caisseFrame.getDB().close(caisseFrame.getPOSConf(), newLog);
149 ilm 168
                                frame.getControler().setLCD("Cloture", "Terminee", 0);
169
                                // TODO lock down the UI until open again
170
                            }
171
                            quit();
172
                        } catch (Exception ex) {
173
                            ExceptionHandler.handle("Erreur", ex);
174
                        }
175
                        System.err.println("CLOTURE");
176
                        frame.showCaisse();
177
                    }
178
                });
179
                p.add(bOkCloture, c);
180
                c.gridx++;
181
                POSButton bCancelCloture = new POSButton("Annuler");
182
                bCancelCloture.addActionListener(new ActionListener() {
183
 
184
                    @Override
185
                    public void actionPerformed(ActionEvent e) {
186
                        p.setVisible(false);
187
                        System.err.println("CANCEL CLOTURE");
188
                        frame.showCaisse();
189
                    }
190
                });
191
                p.add(bCancelCloture, c);
192
 
193
                frame.showPanel(p);
18 ilm 194
            }
195
        });
149 ilm 196
 
142 ilm 197
        bQuit.addActionListener(new ActionListener() {
18 ilm 198
 
142 ilm 199
            @Override
200
            public void actionPerformed(ActionEvent e) {
201
                try {
144 ilm 202
                    quit();
142 ilm 203
                } catch (Exception ex) {
204
                    ExceptionHandler.handle("Erreur", ex);
65 ilm 205
                }
19 ilm 206
            }
142 ilm 207
        });
208
 
18 ilm 209
    }
210
 
144 ilm 211
    private void quit() {
212
        // Fermeture
213
        frame.dispose();
214
    }
215
 
18 ilm 216
}