OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011-2019 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.io.ConcertProtocol;
import org.openconcerto.erp.core.sales.pos.io.ConcertProtocol.ConcertStateListener;
import org.openconcerto.erp.core.sales.pos.model.Paiement;
import org.openconcerto.ui.DefaultGridBagConstraints;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;

import com.lowagie.text.Font;

public class CBPanel extends JPanel implements ConcertStateListener {

    private final JLabel connectionLabel = new JLabel("\u226B");
    private final JLabel sendLabel = new JLabel("\u226B");
    private final JLabel recvLabel = new JLabel("\u226B");
    private final JLabel finalDescLabel = new JLabel("", JLabel.CENTER);
    private final POSButton bForce = new POSButton("Forçage manuel");
    private final POSButton bDrawer = new POSButton("Ouverture tiroir caisse");
    private final POSButton bCancel = new POSButton("Annuler la demande de réglement");

    public CBPanel(CaisseFrame frame, CaisseControler controller, Paiement p) {
        this.setBackground(Color.WHITE);
        this.setOpaque(true);
        this.setLayout(new GridBagLayout());

        JPanel innerPanel = new JPanel();
        innerPanel.setBackground(Color.WHITE);
        innerPanel.setOpaque(true);
        JPanel actionPanel = new JPanel();
        actionPanel.setBackground(Color.WHITE);
        actionPanel.setOpaque(true);

        POSLabel title = new POSLabel("Terminal de paiement");
        title.setHorizontalAlignment(JLabel.CENTER);

        GridBagConstraints c = new DefaultGridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 0;
        c.weighty = 0;
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.insets = new Insets(10, 10, 10, 10);
        this.add(title, c);

        c.insets = new Insets(2, 10, 10, 10);
        c.gridy++;
        this.add(new JSeparator(JSeparator.HORIZONTAL), c);

        c.insets = new Insets(10, 10, 10, 10);
        c.gridy++;
        c.weightx = 1;
        c.weighty = 1;
        add(innerPanel, c);

        c.gridy++;
        add(actionPanel, c);

        connectionLabel.setFont(connectionLabel.getFont().deriveFont(Font.BOLD, 18.f));
        connectionLabel.setForeground(Color.BLUE);
        sendLabel.setFont(sendLabel.getFont().deriveFont(Font.BOLD, 18.f));
        sendLabel.setForeground(Color.BLUE);
        recvLabel.setFont(recvLabel.getFont().deriveFont(Font.BOLD, 18.f));
        recvLabel.setForeground(Color.BLUE);
        finalDescLabel.setFont(getFont().deriveFont(24f));

        GridBagConstraints cg = new GridBagConstraints();
        cg.fill = GridBagConstraints.BOTH;
        cg.insets = new Insets(10, 10, 10, 10);
        cg.weightx = 0;
        cg.weighty = 0;
        cg.gridx = 0;
        cg.gridy = 0;
        cg.gridwidth = 3;

        JPanel jp2 = new JPanel();
        jp2.setOpaque(false);
        jp2.setBorder(BorderFactory.createCompoundBorder(new EtchedBorder(), new EmptyBorder(20, 20, 20, 20)));
        jp2.setLayout(new GridBagLayout());
        jp2.setPreferredSize(new Dimension(500, 180));
        innerPanel.add(jp2, cg);

        JLabel amountLabel = new JLabel("Montant : " + new BigDecimal(p.getMontantInCents()).movePointLeft(2).setScale(2));
        amountLabel.setHorizontalAlignment(JLabel.CENTER);
        amountLabel.setFont(connectionLabel.getFont().deriveFont(Font.BOLD, 18.f));
        amountLabel.setForeground(Color.BLUE);

        c = new DefaultGridBagConstraints();
        Insets inset = c.insets;
        c.insets = new Insets(10, 10, 10, 10);
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 0;
        c.weighty = 0;
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        c.weightx = 1;
        jp2.add(amountLabel, c);

        c.gridy++;
        c.gridwidth = 1;
        c.weightx = 0;
        c.insets = inset;
        jp2.add(connectionLabel, c);

        c.gridx++;
        c.weightx = 1;
        jp2.add(new JLabel("Connexion au terminal de paiement"), c);

        c.gridx = 0;
        c.gridy++;
        c.weightx = 0;
        jp2.add(sendLabel, c);

        c.gridx++;
        c.weightx = 1;
        jp2.add(new JLabel("Envoi des informations"), c);

        c.gridx = 0;
        c.gridy++;
        c.weightx = 0;
        jp2.add(recvLabel, c);

        c.gridx++;
        c.weightx = 1;
        jp2.add(new JLabel("Attente de réception des informations de paiement"), c);

        Insets insets = c.insets;
        c.insets = new Insets(20, 10, 10, 10);
        c.gridx = 0;
        c.gridy++;
        c.weightx = 0;

        c.gridx++;
        c.weightx = 1;
        jp2.add(finalDescLabel, c);

        c.insets = insets;

        GridBagConstraints ca = new GridBagConstraints();
        ca.fill = GridBagConstraints.BOTH;
        ca.insets = new Insets(0, 4, 4, 4);
        ca.weightx = 1;
        ca.weighty = 0;
        ca.gridx = 3;
        ca.gridy = 0;

        actionPanel.add(bForce, ca);
        bForce.setVisible(false);

        ca.gridx++;
        actionPanel.add(bDrawer, ca);
        bDrawer.setVisible(false);

        ca.gridx++;
        actionPanel.add(bCancel, ca);

        bDrawer.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                controller.openDrawer();
                frame.showCaisse();
            }
        });

        bForce.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                controller.openDrawer();
                frame.showCaisse();
            }
        });

        bCancel.setBackground(new Color(233, 56, 63));
        bCancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "Veuillez annuler la demande de réglement sur le TPE");
                frame.showCaisse();
            }
        });

        if (p.getMontantInCents() < 0) {
            stateChanged(ConcertStateListener.States.NOK);
            return;
        }

        final String creditCardPort = controller.getPOSConf().getCreditCardPort();
        if (creditCardPort != null && creditCardPort.trim().length() > 2) {
            final Thread t = new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        ConcertProtocol cp = new ConcertProtocol(creditCardPort);
                        cp.addStateListener(CBPanel.this);
                        boolean ok = cp.sendCardPayment(p.getMontantInCents(), ConcertProtocol.CURRENCY_EUR);

                        if (ok) {
                            controller.addPaiement(p);
                        }
                    } catch (Throwable ex) {
                        stateChanged(ConcertStateListener.States.ERROR);
                    }
                }
            });
            t.setDaemon(true);
            t.start();
        } else {
            stateChanged(ConcertStateListener.States.ERROR);
        }
    }

    @Override
    public void stateChanged(States state) {
        System.err.println("CBPanel.stateChanged(" + state + ")");
        switch (state) {
        case ERROR:
            connectionLabel.setText("\u0445");
            connectionLabel.setForeground(Color.RED.darker());
            sendLabel.setText("\u0445");
            sendLabel.setForeground(Color.RED.darker());
            recvLabel.setText("\u0445");
            recvLabel.setForeground(Color.RED.darker());
            finalDescLabel.setText("Erreur de communication");
            bForce.setVisible(true);
            break;
        case CONNECTED:
            connectionLabel.setText("\u221A");
            connectionLabel.setForeground(Color.GREEN.darker());
            bForce.setVisible(false);
            break;
        case WAITING:
            sendLabel.setText("\u221A");
            sendLabel.setForeground(Color.GREEN.darker());
            break;
        case NOK:
            recvLabel.setText("\u221A");
            recvLabel.setForeground(Color.GREEN.darker());
            finalDescLabel.setForeground(Color.RED.darker());
            finalDescLabel.setText("Paiement refusé");
            bForce.setVisible(false);
            bDrawer.setVisible(false);
            break;
        case OK:
            recvLabel.setText("\u221A");
            recvLabel.setForeground(Color.GREEN.darker());
            finalDescLabel.setForeground(Color.GREEN.darker());
            finalDescLabel.setText("Paiement accepté");
            bDrawer.setVisible(true);
            bCancel.setVisible(false);
            break;
        default:
            break;
        }
    }
}