OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 182 | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
174 ilm 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<>();
185 ilm 28
    private List<MouvementGED> gedURLs;
174 ilm 29
    private Number id;
30
    private BigDecimal debit = BigDecimal.ZERO;
31
    private BigDecimal credit = BigDecimal.ZERO;
32
    private Piece piece;
33
    // Source
34
    private Number idSource;
35
    private String source;
36
    private Mouvement pere;
37
    private MouvementPostInsertionAction postInsertionAction;
38
 
39
    public Mouvement() {
40
    }
41
 
42
    public void setSource(Number idSource, String source) {
43
        this.idSource = idSource;
44
        this.source = source;
45
    }
46
 
47
    public void add(Ecriture e) {
48
        this.ecritures.add(e);
49
        this.debit = this.debit.add(e.getDebit());
50
        this.credit = this.credit.add(e.getCredit());
51
        e.setMouvement(this);
52
    }
53
 
54
    List<Ecriture> getEcritures() {
55
        return this.ecritures;
56
    }
57
 
58
    void setId(Number id) {
59
        this.id = id;
60
    }
61
 
62
    public Number getId() {
63
        return this.id;
64
    }
65
 
185 ilm 66
    public List<MouvementGED> getURLs() {
67
        return this.gedURLs;
68
    }
69
 
70
    public void addURL(MouvementGED e) {
71
        if (this.gedURLs == null) {
72
            this.gedURLs = new ArrayList<>();
73
        }
74
        e.setEcriture(this);
75
        this.gedURLs.add(e);
76
    }
77
 
78
    public boolean hasURLs() {
79
        return this.gedURLs != null;
80
    }
81
 
177 ilm 82
    SQLInsert createInsert(DBRoot root, User user) {
174 ilm 83
        final SQLInsert insert = new SQLInsert();
84
        final SQLTable table = root.getTable("MOUVEMENT");
85
        // FIXME le numero doit être généré en auto
86
        // Thread.dumpStack();
87
        // NUMERO = SELECT (MAX) ?
88
        // insert.add(table.getField("NUMERO"), this.numero);
89
        // select for update max(numero)
90
        if (this.idSource != null) {
91
            insert.add(table.getField("IDSOURCE"), this.idSource);
92
            insert.add(table.getField("SOURCE"), this.source);
93
        }
94
        insert.add(table.getField("ID_PIECE"), this.piece.getId().intValue());
95
        if (this.pere != null) {
96
            insert.add(table.getField("ID_MOUVEMENT_PERE"), this.pere.getId().intValue());
97
        }
177 ilm 98
        insert.addCreationTrackedField(user, table);
174 ilm 99
        return insert;
100
    }
101
 
102
    public boolean isBalanced() {
103
        BigDecimal d = BigDecimal.ZERO;
104
        BigDecimal c = BigDecimal.ZERO;
105
        for (Ecriture e : this.ecritures) {
106
            d = d.add(e.getDebit());
107
            c = c.add(e.getCredit());
108
        }
109
        return d.compareTo(c) == 0;
110
    }
111
 
112
    public Piece getPiece() {
113
        return this.piece;
114
    }
115
 
116
    public void setPiece(Piece piece) {
117
        this.piece = piece;
118
 
119
    }
120
 
182 ilm 121
    public String asString() {
122
        StringBuilder s = new StringBuilder();
123
        BigDecimal d = BigDecimal.ZERO;
124
        BigDecimal c = BigDecimal.ZERO;
125
        s.append("Mouvement N°");
126
        s.append(this.numero);
127
        s.append("\n");
128
        for (Ecriture e : this.ecritures) {
129
 
130
            s.append("D : " + e.getDebit());
131
            s.append("\tC : " + e.getCredit());
132
 
133
            d = d.add(e.getDebit());
134
            c = c.add(e.getCredit());
135
            s.append("\tS : " + (d.subtract(c)) + "\n");
136
        }
137
        return s.toString();
138
    }
139
 
174 ilm 140
    public boolean isEmpty() {
141
        return this.ecritures.isEmpty();
142
    }
143
 
144
    public MouvementPostInsertionAction getPostInsertionAction() {
145
        return this.postInsertionAction;
146
    }
147
 
148
    public void setAfterInsert(MouvementPostInsertionAction insertionAction) {
149
        this.postInsertionAction = insertionAction;
150
    }
151
}