OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
149 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.sales.pos.POSConfiguration;
17
import org.openconcerto.erp.core.sales.pos.io.DefaultTicketPrinter;
18
import org.openconcerto.erp.core.sales.pos.io.Printable;
19
import org.openconcerto.erp.core.sales.pos.io.TicketPrinter;
20
import org.openconcerto.erp.core.sales.pos.model.Paiement;
21
import org.openconcerto.erp.core.sales.pos.model.RegisterLog;
22
import org.openconcerto.erp.core.sales.pos.model.RegisterState;
151 ilm 23
import org.openconcerto.erp.core.sales.pos.model.RegisterState.Status;
149 ilm 24
import org.openconcerto.erp.core.sales.pos.model.Ticket;
25
 
174 ilm 26
import java.io.IOException;
149 ilm 27
import java.math.BigDecimal;
28
import java.text.DateFormat;
29
import java.text.ParseException;
30
import java.text.SimpleDateFormat;
31
import java.util.Date;
32
import java.util.LinkedHashMap;
33
import java.util.List;
34
import java.util.Map;
151 ilm 35
import java.util.Map.Entry;
149 ilm 36
import java.util.TreeMap;
37
import java.util.logging.Level;
38
 
39
abstract class RegisterSummary implements Printable {
40
 
41
    private final String name;
42
    private final RegisterLog log;
43
 
44
    protected RegisterSummary(final String name, final RegisterLog log) {
45
        super();
46
        this.name = name;
47
        this.log = log;
48
    }
49
 
50
    public boolean isPrintingTicketsList() {
51
        return true;
52
    }
53
 
54
    public final RegisterLog getLog() {
55
        return this.log;
56
    }
57
 
58
    public abstract String getLabel();
59
 
174 ilm 60
    protected abstract List<Ticket> getReceipts() throws ParseException, IOException;
149 ilm 61
 
62
    @Override
63
    public void print(TicketPrinter prt, int ticketWidth) {
64
        final DateFormat dateFormat = new SimpleDateFormat("dd/MM/YYYY à HH:mm:ss");
65
        final String title = this.name + " de la caisse " + getLog().getRegisterID();
66
        prt.clearBuffer(title);
67
        try {
68
            final List<Ticket> receipts = this.getReceipts();
69
 
70
            final Map<Ticket, Map<String, BigDecimal>> typesByReceipt = new LinkedHashMap<>();
71
            final Map<Ticket, BigDecimal> paidByReceipt = new LinkedHashMap<>();
72
            final Map<Ticket, BigDecimal> valueByReceipt = new LinkedHashMap<>();
73
 
74
            final Map<String, BigDecimal> totalByType = new TreeMap<>();
75
            BigDecimal total = BigDecimal.ZERO;
76
            BigDecimal totalPaid = BigDecimal.ZERO;
77
 
78
            for (final Ticket t : receipts) {
79
                final int receiptInCents = t.getTotalInCents();
80
                final BigDecimal receiptValue = BigDecimal.valueOf(receiptInCents).movePointLeft(2);
81
 
82
                BigDecimal receiptPaid = BigDecimal.ZERO;
83
                final Map<String, BigDecimal> byType = new TreeMap<>();
84
                for (final Paiement p : t.getPaiements()) {
85
                    final BigDecimal paid = BigDecimal.valueOf(p.getMontantInCents()).movePointLeft(2);
86
                    receiptPaid = receiptPaid.add(paid);
87
                    addInMap(byType, p.getTypeAsString(), paid);
88
                    addInMap(totalByType, p.getTypeAsString(), paid);
89
                }
90
                typesByReceipt.put(t, byType);
91
                valueByReceipt.put(t, receiptValue);
92
                paidByReceipt.put(t, receiptPaid);
93
                total = total.add(receiptValue);
94
                totalPaid = totalPaid.add(receiptPaid);
95
            }
151 ilm 96
            final BigDecimal rendu = totalPaid.subtract(total);
149 ilm 97
 
98
            prt.addToBuffer(title, TicketPrinter.BOLD_LARGE);
99
            prt.addToBuffer("");
100
            prt.addToBuffer("Edité le " + dateFormat.format(new Date()));
101
            final RegisterState registerState = getLog().getRegisterState();
102
            prt.addToBuffer((registerState.getStatus() == Status.OPEN ? "Ouvert" : "Fermé") + " le " + registerState.formatDate(dateFormat));
103
            prt.addToBuffer("");
104
            prt.addToBuffer("");
105
            prt.addToBuffer("Tickets de caisse", TicketPrinter.BOLD_LARGE);
106
            prt.addToBuffer("");
107
 
108
            Paiement espece = new Paiement(Paiement.ESPECES);
151 ilm 109
            // TODO same name as "Total TTC"
110
            prt.addToBuffer(DefaultTicketPrinter.formatSides(ticketWidth, "Total des ventes", total.toPlainString()), TicketPrinter.BOLD);
149 ilm 111
            for (final Entry<String, BigDecimal> e2 : totalByType.entrySet()) {
112
                final String typePayment = e2.getKey();
113
                if (typePayment.equals(espece.getTypeAsString())) {
114
                    BigDecimal value = e2.getValue();
115
                    value = value.subtract(rendu);
116
                    prt.addToBuffer(DefaultTicketPrinter.formatSides(ticketWidth, "      " + typePayment, value.toPlainString()));
117
                } else {
118
                    prt.addToBuffer(DefaultTicketPrinter.formatSides(ticketWidth, "      " + typePayment, e2.getValue().toPlainString()));
119
                }
120
            }
121
 
122
            prt.addToBuffer(DefaultTicketPrinter.formatSides(ticketWidth, "Total des paiements", totalPaid.toPlainString()), TicketPrinter.BOLD);
123
            for (final Entry<String, BigDecimal> e2 : totalByType.entrySet()) {
124
                prt.addToBuffer(DefaultTicketPrinter.formatSides(ticketWidth, "      " + e2.getKey(), e2.getValue().toPlainString()));
125
            }
126
            prt.addToBuffer("");
151 ilm 127
            prt.addToBuffer(DefaultTicketPrinter.formatSides(ticketWidth, "Total rendu", rendu.toPlainString()), TicketPrinter.BOLD);
149 ilm 128
            prt.addToBuffer("");
129
            prt.addToBuffer(DefaultTicketPrinter.formatSides(ticketWidth, "Total TTC (Euros)", total.toPlainString()), TicketPrinter.BOLD);
130
            prt.addToBuffer(DefaultTicketPrinter.formatSides(ticketWidth, "Nombre de tickets", "" + receipts.size()));
131
            final long avg = receipts.isEmpty() ? 0 : total.movePointRight(2).longValueExact() / receipts.size();
132
            prt.addToBuffer(DefaultTicketPrinter.formatSides(ticketWidth, "Panier moyen (Euros)", BigDecimal.valueOf(avg).movePointLeft(2).toPlainString()));
133
            prt.addToBuffer("");
134
            if (isPrintingTicketsList()) {
135
                prt.addToBuffer("");
136
                prt.addToBuffer("Liste des tickets", TicketPrinter.BOLD_LARGE);
137
                prt.addToBuffer("");
138
                for (final Entry<Ticket, Map<String, BigDecimal>> e : typesByReceipt.entrySet()) {
139
                    final Ticket t = e.getKey();
140
                    final BigDecimal receiptValue = valueByReceipt.get(t);
141
                    prt.addToBuffer(DefaultTicketPrinter.formatSides(ticketWidth, t.getCode(), receiptValue.toPlainString()), TicketPrinter.BOLD);
142
                    for (final Entry<String, BigDecimal> e2 : e.getValue().entrySet()) {
143
                        prt.addToBuffer(DefaultTicketPrinter.formatSides(ticketWidth, " " + e2.getKey(), e2.getValue().toPlainString()));
144
                    }
145
                    // rendu
146
                    final BigDecimal receiptChange = paidByReceipt.get(t).subtract(receiptValue);
147
                    if (receiptChange.signum() != 0)
148
                        prt.addToBuffer(DefaultTicketPrinter.formatSides(ticketWidth, " rendu", receiptChange.toPlainString()));
149
 
150
                    prt.addToBuffer("");
151
                }
152
            }
153
        } catch (Exception e) {
154
            POSConfiguration.getLogger().log(Level.WARNING, "Couldn't fill " + this.name, e);
155
            prt.addToBuffer("Erreur");
156
        }
157
        try {
158
            prt.printBuffer();
159
        } catch (Exception e) {
160
            POSConfiguration.getLogger().log(Level.WARNING, "Couldn't print " + this.name, e);
161
        }
162
    }
163
 
164
    static private final <K> void addInMap(final Map<K, BigDecimal> m, final K key, final BigDecimal val) {
165
        final BigDecimal prev = m.get(key);
166
        m.put(key, prev == null ? val : prev.add(val));
167
    }
168
}