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 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.erp.core.sales.pos.ui;

import org.openconcerto.erp.core.sales.pos.POSConfiguration;
import org.openconcerto.erp.core.sales.pos.io.Printable;
import org.openconcerto.erp.core.sales.pos.io.TicketPrinter;
import org.openconcerto.erp.core.sales.pos.model.RegisterLog;
import org.openconcerto.task.config.ComptaBasePropsConfiguration;
import org.openconcerto.utils.ExceptionHandler;
import org.openconcerto.utils.FileUtils;
import org.openconcerto.utils.JImage;
import org.openconcerto.utils.StringUtils;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.swing.JPanel;

public class CaisseMenuPanel extends JPanel {

    private CaisseFrame frame;

    CaisseMenuPanel(final CaisseFrame caisseFrame) {
        this.frame = caisseFrame;
        this.setBackground(Color.WHITE);
        this.setOpaque(true);
        this.setLayout(new GridBagLayout());
        final GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.CENTER;
        c.weightx = 0;
        c.weighty = 0;
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(20, 20, 30, 20);
        final JImage image = new JImage(ComptaBasePropsConfiguration.class.getResource("logo.png"));
        this.add(image, c);
        c.gridx++;
        File testTicketDir = new File("test");
        if (testTicketDir.exists() && testTicketDir.isDirectory()) {
            File[] files = testTicketDir.listFiles();
            for (int i = 0; i < files.length; i++) {
                final File f = files[i];
                if (f.getName().endsWith(".txt")) {
                    final POSButton b = new POSButton(f.getName().substring(0, f.getName().length() - 4));
                    this.add(b, c);
                    c.gridy++;
                    b.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                            System.err.println("CaisseMenuPanel.CaisseMenuPanel(...).new ActionListener() {...}.actionPerformed()");
                            POSConfiguration.getInstance().printOnceOnFirstPrinter(new Printable() {

                                @Override
                                public void print(TicketPrinter prt, int ticketWidth) {
                                    prt.clearBuffer(f.getName());
                                    System.err.println("CaisseMenuPanel.CaisseMenuPanel(...)print()");
                                    String content = f.getName();
                                    try {
                                        content = FileUtils.read(f);
                                        List<String> lines = StringUtils.splitIntoLines(content);
                                        for (String line : lines) {
                                            line = line.trim();
                                            System.err.println("CaisseMenuPanel.CaisseMenuPanel(...)print()" + line);
                                            if (line.startsWith("[bl]")) {
                                                prt.addToBuffer(line.substring(3), TicketPrinter.BOLD_LARGE);
                                            } else if (line.startsWith("[b]")) {
                                                prt.addToBuffer(line.substring(3), TicketPrinter.BOLD);
                                            } else {
                                                prt.addToBuffer(line);
                                            }
                                        }
                                    } catch (IOException e1) {
                                        prt.addToBuffer(content);
                                        prt.addToBuffer(e1.getMessage());
                                        e1.printStackTrace();
                                    }
                                    try {
                                        prt.printBuffer();
                                    } catch (final Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            });

                        }
                    });
                }

            }
        }

        final POSButton bTickets = new POSButton("Liste des tickets");
        this.add(bTickets, c);
        c.gridy++;
        final POSButton bCloture = new POSButton("Clôturer");
        this.add(bCloture, c);
        c.gridy++;
        c.insets = new Insets(20, 20, 20, 20);
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.EAST;
        final POSButton bQuit = new POSButton("Quitter");
        bQuit.setBackground(Color.decode("#AD1457"));
        this.add(bQuit, c);
        // Listeners
        bTickets.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    frame.showTickets(null);
                } catch (Exception ex) {
                    ExceptionHandler.handle("Erreur", ex);
                }
            }
        });

        bCloture.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                final JPanel p = new JPanel();
                p.setOpaque(true);
                p.setLayout(new GridBagLayout());
                final GridBagConstraints c = new GridBagConstraints();
                c.fill = GridBagConstraints.HORIZONTAL;
                c.anchor = GridBagConstraints.WEST;
                c.weightx = 0;
                c.weighty = 0;
                c.gridx = 0;
                c.gridy = 0;
                c.insets = new Insets(20, 20, 30, 20);
                c.gridwidth = 2;
                p.add(new POSLabel("Voulez vous clôturer la caisse ?"), c);
                c.gridy++;
                c.gridwidth = 1;
                POSButton bOkCloture = new POSButton("Clôturer");
                bOkCloture.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        try {
                            final boolean quit = caisseFrame.getDB().fetchRegisterState().checkIfMoved();
                            if (!quit) {
                                frame.getControler().setLCD("Cloture", "En cours...", 0);
                                final int userID = POSConfiguration.getInstance().getUserID();
                                final RegisterLog newLog = caisseFrame.getFiles().close(userID);
                                caisseFrame.getDB().close(userID, newLog);
                                frame.getControler().setLCD("Cloture", "Terminee", 0);
                                // TODO lock down the UI until open again
                            }
                            quit();
                        } catch (Exception ex) {
                            ExceptionHandler.handle("Erreur", ex);
                        }
                        System.err.println("CLOTURE");
                        frame.showCaisse();
                    }
                });
                p.add(bOkCloture, c);
                c.gridx++;
                POSButton bCancelCloture = new POSButton("Annuler");
                bCancelCloture.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        p.setVisible(false);
                        System.err.println("CANCEL CLOTURE");
                        frame.showCaisse();
                    }
                });
                p.add(bCancelCloture, c);

                frame.showPanel(p);
            }
        });

        bQuit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    quit();
                } catch (Exception ex) {
                    ExceptionHandler.handle("Erreur", ex);
                }
            }
        });

    }

    private void quit() {
        // Fermeture
        frame.dispose();
    }

}