OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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