OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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