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

Rev Author Line No. Line
142 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.model;
15
 
16
import org.openconcerto.erp.core.finance.payment.element.TypeReglementSQLElement;
17
import org.openconcerto.erp.core.sales.pos.io.DefaultTicketPrinter;
18
import org.openconcerto.erp.core.sales.pos.io.TicketPrinter;
19
import org.openconcerto.erp.core.sales.pos.ui.TicketCellRenderer;
20
import org.openconcerto.erp.generationEcritures.GenerationEcritures;
21
import org.openconcerto.erp.generationEcritures.GenerationReglementVenteNG;
22
import org.openconcerto.erp.model.PrixTTC;
23
import org.openconcerto.sql.Configuration;
24
import org.openconcerto.sql.model.SQLDataSource;
25
import org.openconcerto.sql.model.SQLRow;
144 ilm 26
import org.openconcerto.sql.model.SQLRowAccessor;
142 ilm 27
import org.openconcerto.sql.model.SQLRowValues;
28
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
29
import org.openconcerto.sql.model.SQLSelect;
30
import org.openconcerto.sql.model.SQLTable;
31
import org.openconcerto.sql.utils.SQLUtils;
32
import org.openconcerto.utils.ExceptionHandler;
33
import org.openconcerto.utils.cc.ITransformer;
34
 
35
import java.math.BigDecimal;
36
import java.math.RoundingMode;
37
import java.sql.SQLException;
38
import java.text.SimpleDateFormat;
39
import java.util.ArrayList;
40
import java.util.Collections;
41
import java.util.Comparator;
42
import java.util.Date;
43
import java.util.List;
44
import java.util.Locale;
45
 
46
public class Client {
47
 
48
    public static final Client NONE = new Client(-1, "Client inconnu", BigDecimal.ZERO);
49
    private int id;
50
    private String fullName;
51
    private BigDecimal solde;
52
    private String addr;
53
 
54
    public Client(int id, String fullName, BigDecimal solde) {
55
        this.id = id;
56
        this.fullName = fullName;
57
        if (solde == null) {
58
            solde = BigDecimal.ZERO;
59
        }
60
        this.solde = solde;
61
    }
62
 
63
    public int getId() {
64
        return this.id;
65
    }
66
 
67
    public String getFullName() {
68
        return fullName;
69
    }
70
 
71
    public BigDecimal getSolde() {
72
        return solde;
73
    }
74
 
75
    public String getAddr() {
76
        return addr;
77
    }
78
 
79
    public void setAdresse(String string) {
80
        this.addr = string.trim();
81
    }
82
 
83
    public void credit(final BigDecimal amount, final int paymentType) throws Exception {
84
        if (amount == null || amount.longValue() <= 0) {
85
            return;
86
        }
87
 
88
        // inserer la transaction
89
        final SQLDataSource ds = Configuration.getInstance().getSystemRoot().getDataSource();
90
        SQLUtils.executeAtomic(ds, new SQLUtils.SQLFactory<Object>() {
91
            @Override
92
            public Object create() throws SQLException {
93
 
94
                final SQLTable table = Configuration.getInstance().getRoot().findTable("COMPTE_CLIENT_TRANSACTION");
95
                SQLRowValues rowVals = new SQLRowValues(table);
96
                rowVals.put("ID_CLIENT", getId());
97
                final Date today = new Date();
98
                rowVals.put("DATE", today);
99
                rowVals.put("MONTANT", amount);
100
                SQLRowValues rowValsEltMode = rowVals.putRowValues("ID_MODE_REGLEMENT");
101
                if (paymentType == Paiement.CB) {
102
                    rowValsEltMode.put("ID_TYPE_REGLEMENT", TypeReglementSQLElement.CB);
103
                } else if (paymentType == Paiement.CHEQUE) {
104
                    rowValsEltMode.put("ID_TYPE_REGLEMENT", TypeReglementSQLElement.CHEQUE);
105
                } else if (paymentType == Paiement.ESPECES) {
106
                    rowValsEltMode.put("ID_TYPE_REGLEMENT", TypeReglementSQLElement.ESPECE);
107
                }
108
                SQLRow rowTransact = rowVals.commit();
109
                GenerationEcritures ecr = new GenerationEcritures();
110
                int idMvt = ecr.getNewMouvement(table.getName(), rowTransact.getID(), 1, "Transact. " + getFullName());
111
                rowTransact = rowTransact.createEmptyUpdateRow().put("ID_MOUVEMENT", idMvt).commit();
112
 
113
                // mise à jour du solde
114
                SQLTable tableClient = table.getForeignTable("ID_CLIENT");
115
                SQLRow row = tableClient.getRow(getId());
116
                BigDecimal solde = row.getBigDecimal("SOLDE_COMPTE");
117
                final BigDecimal nouveauSolde = solde.add(amount);
118
                row.createEmptyUpdateRow().put("SOLDE_COMPTE", nouveauSolde).commit();
119
 
120
                final long centAmountValue = amount.movePointRight(2).setScale(0, RoundingMode.HALF_UP).longValue();
121
                // Créeation des réglements et écritures
122
                try {
123
                    new GenerationReglementVenteNG("Crédit du solde " + Client.this.fullName, row, new PrixTTC(centAmountValue), today, rowTransact.getForeign("ID_MODE_REGLEMENT"), rowTransact,
124
                            rowTransact.getForeign("ID_MOUVEMENT"), true, true);
125
                } catch (Exception e) {
126
                    e.printStackTrace();
127
                    throw new SQLException(e);
128
                }
129
 
130
                // TODO VALIDER les écritures ici et en fermeture de caisse
131
 
132
                Client.this.solde = nouveauSolde;
133
                return null;
134
            }
135
        });
136
    }
137
 
156 ilm 138
    public void printCredit(TicketPrinter prt, List<TicketLine> headers, int ticketWidth, BigDecimal amount, int paymentType, BigDecimal nouveauSolde) {
149 ilm 139
        prt.clearBuffer(this.getFullName() + " crédit");
142 ilm 140
        for (TicketLine line : headers) {
141
            prt.addToBuffer(line);
142
        }
143
 
144
        // Date
145
        prt.addToBuffer("");
146
        SimpleDateFormat df = new SimpleDateFormat("EEEE d MMMM yyyy à HH:mm", Locale.FRENCH);
147
        prt.addToBuffer(DefaultTicketPrinter.formatRight(ticketWidth, "Le " + df.format(new Date())));
148
        prt.addToBuffer("");
149
        prt.addToBuffer("RECU", TicketPrinter.BOLD);
150
        prt.addToBuffer("");
151
 
152
        String amountStr = TicketCellRenderer.centsToString(amount.movePointRight(2).setScale(0, RoundingMode.HALF_UP).intValue());
153
        prt.addToBuffer("Paiement de " + amountStr + " euros");
154
        prt.addToBuffer("En" + new Paiement(paymentType).getTypeAsString());
155
        prt.addToBuffer("");
156
        int ntotal = amount.movePointRight(2).setScale(0, RoundingMode.HALF_UP).intValue();
157
        prt.addToBuffer("Nouveau solde : " + TicketCellRenderer.centsToString(ntotal) + " euros");
158
        prt.addToBuffer("");
159
        try {
160
            prt.printBuffer();
161
        } catch (Exception e) {
162
            e.printStackTrace();
163
        }
164
    }
165
 
166
    public List<Transaction> getTransactions() {
167
        final SQLTable table = Configuration.getInstance().getRoot().findTable("COMPTE_CLIENT_TRANSACTION");
168
        final SQLRowValues graph = new SQLRowValues(table);
169
        graph.putNulls("DATE", "MONTANT");
144 ilm 170
        graph.putRowValues("ID_MODE_REGLEMENT").putNulls("ID_TYPE_REGLEMENT");
142 ilm 171
        graph.putRowValues("ID_CLIENT");
172
        final SQLRowValuesListFetcher fetcher = new SQLRowValuesListFetcher(graph);
173
        fetcher.appendSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
174
 
175
            @Override
176
            public SQLSelect transformChecked(SQLSelect input) {
177
                return input.setWhere(table.getField("ID_CLIENT"), "=", getId());
178
            }
179
        });
180
        final List<SQLRowValues> values = fetcher.fetch();
181
        final List<Transaction> l = new ArrayList<Transaction>();
182
        for (SQLRowValues sqlRowValues : values) {
183
            final Date date = sqlRowValues.getDate("DATE").getTime();
184
            final BigDecimal amount = sqlRowValues.getBigDecimal("MONTANT");
144 ilm 185
            final SQLRowAccessor modeRegl = sqlRowValues.getNonEmptyForeign("ID_MODE_REGLEMENT");
186
            final int idReglement = modeRegl == null ? TypeReglementSQLElement.INVALID : modeRegl.getForeignID("ID_TYPE_REGLEMENT");
142 ilm 187
            int payment = -1;
188
            if (idReglement == TypeReglementSQLElement.CB) {
189
                payment = Paiement.CB;
190
            } else if (idReglement == TypeReglementSQLElement.ESPECE) {
191
                payment = Paiement.ESPECES;
192
            } else if (idReglement == TypeReglementSQLElement.CHEQUE) {
193
                payment = Paiement.CHEQUE;
194
            } else if (amount.signum() == -1) {
195
                payment = Paiement.SOLDE;
196
            } else {
197
 
198
                ExceptionHandler.handle("unknown ID_MODE_REGLEMENT " + idReglement);
199
            }
200
 
201
            final Transaction t = new Transaction(date, amount, payment);
202
            l.add(t);
203
        }
204
        Collections.sort(l, new Comparator<Transaction>() {
205
 
206
            @Override
207
            public int compare(Transaction o1, Transaction o2) {
208
                // Du plus récent au plus ancien
209
                return o2.getDate().compareTo(o1.getDate());
210
            }
211
        });
212
        return l;
213
    }
214
}