OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.erp.generationEcritures;

import org.openconcerto.sql.model.DBRoot;
import org.openconcerto.sql.model.SQLInsert;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.users.User;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class Mouvement {
    private String numero;
    private List<Ecriture> ecritures = new ArrayList<>();
    private Number id;
    private BigDecimal debit = BigDecimal.ZERO;
    private BigDecimal credit = BigDecimal.ZERO;
    private Piece piece;
    // Source
    private Number idSource;
    private String source;
    private Mouvement pere;
    private MouvementPostInsertionAction postInsertionAction;

    public Mouvement() {
    }

    public void setSource(Number idSource, String source) {
        this.idSource = idSource;
        this.source = source;
    }

    public void add(Ecriture e) {
        this.ecritures.add(e);
        this.debit = this.debit.add(e.getDebit());
        this.credit = this.credit.add(e.getCredit());
        e.setMouvement(this);
    }

    List<Ecriture> getEcritures() {
        return this.ecritures;
    }

    void setId(Number id) {
        this.id = id;
    }

    public Number getId() {
        return this.id;
    }

    SQLInsert createInsert(DBRoot root, User user) {
        final SQLInsert insert = new SQLInsert();
        final SQLTable table = root.getTable("MOUVEMENT");
        // FIXME le numero doit être généré en auto
        // Thread.dumpStack();
        // NUMERO = SELECT (MAX) ?
        // insert.add(table.getField("NUMERO"), this.numero);
        // select for update max(numero)
        if (this.idSource != null) {
            insert.add(table.getField("IDSOURCE"), this.idSource);
            insert.add(table.getField("SOURCE"), this.source);
        }
        insert.add(table.getField("ID_PIECE"), this.piece.getId().intValue());
        if (this.pere != null) {
            insert.add(table.getField("ID_MOUVEMENT_PERE"), this.pere.getId().intValue());
        }
        insert.addCreationTrackedField(user, table);
        return insert;
    }

    public boolean isBalanced() {
        BigDecimal d = BigDecimal.ZERO;
        BigDecimal c = BigDecimal.ZERO;
        for (Ecriture e : this.ecritures) {
            d = d.add(e.getDebit());
            c = c.add(e.getCredit());
        }
        return d.compareTo(c) == 0;
    }

    public Piece getPiece() {
        return this.piece;
    }

    public void setPiece(Piece piece) {
        this.piece = piece;

    }

    public boolean isEmpty() {
        return this.ecritures.isEmpty();
    }

    public MouvementPostInsertionAction getPostInsertionAction() {
        return this.postInsertionAction;
    }

    public void setAfterInsert(MouvementPostInsertionAction insertionAction) {
        this.postInsertionAction = insertionAction;
    }
}