OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Rev 182 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
174 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.generationEcritures;
15
 
16
import org.openconcerto.sql.model.DBRoot;
17
import org.openconcerto.sql.model.SQLInsert;
18
import org.openconcerto.sql.model.SQLTable;
177 ilm 19
import org.openconcerto.sql.users.User;
174 ilm 20
 
21
import java.math.BigDecimal;
22
import java.util.ArrayList;
23
import java.util.List;
24
 
25
public class Mouvement {
26
    private String numero;
27
    private List<Ecriture> ecritures = new ArrayList<>();
28
    private Number id;
29
    private BigDecimal debit = BigDecimal.ZERO;
30
    private BigDecimal credit = BigDecimal.ZERO;
31
    private Piece piece;
32
    // Source
33
    private Number idSource;
34
    private String source;
35
    private Mouvement pere;
36
    private MouvementPostInsertionAction postInsertionAction;
37
 
38
    public Mouvement() {
39
    }
40
 
41
    public void setSource(Number idSource, String source) {
42
        this.idSource = idSource;
43
        this.source = source;
44
    }
45
 
46
    public void add(Ecriture e) {
47
        this.ecritures.add(e);
48
        this.debit = this.debit.add(e.getDebit());
49
        this.credit = this.credit.add(e.getCredit());
50
        e.setMouvement(this);
51
    }
52
 
53
    List<Ecriture> getEcritures() {
54
        return this.ecritures;
55
    }
56
 
57
    void setId(Number id) {
58
        this.id = id;
59
    }
60
 
61
    public Number getId() {
62
        return this.id;
63
    }
64
 
177 ilm 65
    SQLInsert createInsert(DBRoot root, User user) {
174 ilm 66
        final SQLInsert insert = new SQLInsert();
67
        final SQLTable table = root.getTable("MOUVEMENT");
68
        // FIXME le numero doit être généré en auto
69
        // Thread.dumpStack();
70
        // NUMERO = SELECT (MAX) ?
71
        // insert.add(table.getField("NUMERO"), this.numero);
72
        // select for update max(numero)
73
        if (this.idSource != null) {
74
            insert.add(table.getField("IDSOURCE"), this.idSource);
75
            insert.add(table.getField("SOURCE"), this.source);
76
        }
77
        insert.add(table.getField("ID_PIECE"), this.piece.getId().intValue());
78
        if (this.pere != null) {
79
            insert.add(table.getField("ID_MOUVEMENT_PERE"), this.pere.getId().intValue());
80
        }
177 ilm 81
        insert.addCreationTrackedField(user, table);
174 ilm 82
        return insert;
83
    }
84
 
85
    public boolean isBalanced() {
86
        BigDecimal d = BigDecimal.ZERO;
87
        BigDecimal c = BigDecimal.ZERO;
88
        for (Ecriture e : this.ecritures) {
89
            d = d.add(e.getDebit());
90
            c = c.add(e.getCredit());
91
        }
92
        return d.compareTo(c) == 0;
93
    }
94
 
95
    public Piece getPiece() {
96
        return this.piece;
97
    }
98
 
99
    public void setPiece(Piece piece) {
100
        this.piece = piece;
101
 
102
    }
103
 
104
    public boolean isEmpty() {
105
        return this.ecritures.isEmpty();
106
    }
107
 
108
    public MouvementPostInsertionAction getPostInsertionAction() {
109
        return this.postInsertionAction;
110
    }
111
 
112
    public void setAfterInsert(MouvementPostInsertionAction insertionAction) {
113
        this.postInsertionAction = insertionAction;
114
    }
115
}