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
 
16
import org.openconcerto.erp.core.finance.payment.element.TypeReglementSQLElement;
61 ilm 17
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
132 ilm 18
import org.openconcerto.erp.core.sales.pos.POSConfiguration;
18 ilm 19
import org.openconcerto.erp.core.sales.pos.model.Article;
20
import org.openconcerto.erp.core.sales.pos.model.Categorie;
21
import org.openconcerto.erp.core.sales.pos.model.Paiement;
144 ilm 22
import org.openconcerto.erp.core.sales.pos.model.ReceiptCode;
18 ilm 23
import org.openconcerto.erp.core.sales.pos.model.Ticket;
24
import org.openconcerto.sql.Configuration;
25
import org.openconcerto.sql.element.SQLElement;
174 ilm 26
import org.openconcerto.sql.element.SQLElementDirectory;
18 ilm 27
import org.openconcerto.sql.model.SQLRow;
174 ilm 28
import org.openconcerto.sql.model.SQLRowListRSH;
29
import org.openconcerto.sql.model.SQLSelect;
18 ilm 30
import org.openconcerto.ui.DefaultGridBagConstraints;
90 ilm 31
import org.openconcerto.utils.DecimalUtils;
18 ilm 32
 
33
import java.awt.GridBagConstraints;
34
import java.awt.GridBagLayout;
35
import java.awt.event.ActionEvent;
36
import java.awt.event.ActionListener;
67 ilm 37
import java.math.BigDecimal;
144 ilm 38
import java.text.ParseException;
174 ilm 39
import java.util.HashMap;
18 ilm 40
import java.util.List;
174 ilm 41
import java.util.Map;
18 ilm 42
 
43
import javax.swing.JButton;
44
import javax.swing.JPanel;
45
import javax.swing.JSeparator;
46
 
47
public class TextAreaTicketPanel extends JPanel {
48
 
156 ilm 49
    public TextAreaTicketPanel(final POSConfiguration conf, SQLRow row) {
18 ilm 50
        super(new GridBagLayout());
51
        GridBagConstraints c = new DefaultGridBagConstraints();
52
        c.fill = GridBagConstraints.BOTH;
53
        c.weightx = 0;
54
        c.weighty = 0;
55
 
56
        final Ticket ticket = createTicket(row);
57
 
58
        JButton button = new JButton("Imprimer");
59
        button.addActionListener(new ActionListener() {
60
            @Override
61
            public void actionPerformed(ActionEvent e) {
156 ilm 62
                conf.print(ticket);
18 ilm 63
            }
64
        });
65
 
66
        c.fill = GridBagConstraints.NONE;
67
        c.anchor = GridBagConstraints.CENTER;
68
        this.add(button, c);
69
 
70
        c.anchor = GridBagConstraints.WEST;
71
        c.fill = GridBagConstraints.HORIZONTAL;
72
        JSeparator sep = new JSeparator();
73
        c.gridy++;
74
        c.weightx = 1;
75
        this.add(sep, c);
76
        final TextAreaTicketPrinter comp = new TextAreaTicketPrinter();
77
        c.gridy++;
78
        c.weighty = 1;
79
        this.add(comp, c);
80
 
156 ilm 81
        ticket.print(comp, conf.getTicketPrinterConfiguration1().getTicketWidth());
18 ilm 82
    }
83
 
84
    private Ticket createTicket(SQLRow row) {
174 ilm 85
        // TODO merger ce code avec CaissepPanel.loadArticles si possible
144 ilm 86
        final Ticket t;
87
        try {
88
            t = new Ticket(new ReceiptCode(row.getString("NUMERO")), row.getDate("DATE"), row.getString("FILE_HASH_PREVIOUS"));
89
        } catch (ParseException e) {
90
            throw new IllegalStateException("Couldn't parse " + row, e);
91
        }
18 ilm 92
 
174 ilm 93
        final SQLElementDirectory directory = Configuration.getInstance().getDirectory();
94
        SQLElement eltEncaisser = directory.getElement("ENCAISSER_MONTANT");
18 ilm 95
        List<SQLRow> l = row.getReferentRows(eltEncaisser.getTable());
96
        for (SQLRow row2 : l) {
97
            long montant = row2.getLong("MONTANT");
98
            SQLRow rowMode = row2.getForeign("ID_MODE_REGLEMENT");
99
            int type = Paiement.CB;
100
            if (rowMode.getInt("ID_TYPE_REGLEMENT") == TypeReglementSQLElement.CB) {
101
                type = Paiement.CB;
102
            } else {
103
                if (rowMode.getInt("ID_TYPE_REGLEMENT") == TypeReglementSQLElement.CHEQUE) {
104
                    type = Paiement.CHEQUE;
105
                } else {
106
                    if (rowMode.getInt("ID_TYPE_REGLEMENT") == TypeReglementSQLElement.ESPECE) {
107
                        type = Paiement.ESPECES;
108
                    }
109
                }
110
            }
111
            Paiement p = new Paiement(type);
112
            p.setMontantInCents((int) montant);
113
            t.addPaiement(p);
114
        }
115
 
174 ilm 116
        SQLElement eltArticle = directory.getElement("SAISIE_VENTE_FACTURE_ELEMENT");
117
 
118
        final SQLSelect selUniteVente = new SQLSelect();
119
        selUniteVente.addSelectStar(directory.getElement("UNITE_VENTE").getTable());
120
        final Map<Integer, String> mapUniteVenteName = new HashMap<>();
121
        for (SQLRow rowUniteVente : SQLRowListRSH.execute(selUniteVente)) {
122
            mapUniteVenteName.put(rowUniteVente.getID(), rowUniteVente.getString("CODE"));
123
        }
124
 
18 ilm 125
        List<SQLRow> l2 = row.getReferentRows(eltArticle.getTable());
126
        Categorie c = new Categorie("");
127
        for (SQLRow row2 : l2) {
61 ilm 128
            Article a = new Article(c, row2.getString("NOM"), row2.getInt("ID_ARTICLE"));
174 ilm 129
            if (row2.getInt("ID_UNITE_VENTE") != 2) {
130
                a.setSalesUnit(mapUniteVenteName.get(row2.getInt("ID_UNITE_VENTE")));
131
            }
67 ilm 132
            BigDecimal ht = (BigDecimal) row2.getObject("PV_HT");
132 ilm 133
            a.setPriceWithoutTax(ht);
67 ilm 134
            int idTaxe = row2.getInt("ID_TAXE");
135
            float tva = TaxeCache.getCache().getTauxFromId(idTaxe);
174 ilm 136
            a.setPriceWithTax(ht.multiply(BigDecimal.valueOf(1.0 + (tva / 100.0D)), DecimalUtils.HIGH_PRECISION));
67 ilm 137
            a.setIdTaxe(idTaxe);
18 ilm 138
            t.addArticle(a);
174 ilm 139
            if (a.getSalesUnit() == null) {
140
                t.setArticleCount(a, new BigDecimal(row2.getInt("QTE")));
141
            } else {
142
                t.setArticleCount(a, row2.getBigDecimal("QTE_UNITAIRE"));
143
            }
18 ilm 144
        }
145
 
146
        return t;
147
    }
148
}