OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
83 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.
83 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.generationDoc.provider;
15
 
16
import org.openconcerto.erp.generationDoc.SpreadSheetCellValueContext;
17
import org.openconcerto.erp.generationDoc.SpreadSheetCellValueProvider;
18
import org.openconcerto.erp.generationDoc.SpreadSheetCellValueProviderManager;
19
import org.openconcerto.sql.model.SQLRowAccessor;
20
 
21
import java.math.BigDecimal;
142 ilm 22
import java.math.RoundingMode;
83 ilm 23
import java.util.Collection;
24
 
25
public class PaiementRemainedProvider implements SpreadSheetCellValueProvider {
26
 
93 ilm 27
    private enum Type {
28
        DONE, REMAINED
29
    };
30
 
31
    private final Type t;
32
 
33
    private PaiementRemainedProvider(Type t) {
34
        this.t = t;
35
    }
36
 
83 ilm 37
    public Object getValue(SpreadSheetCellValueContext context) {
38
        SQLRowAccessor row = context.getRow();
39
        return getRestant(row);
40
    }
41
 
42
    public static void register() {
94 ilm 43
        SpreadSheetCellValueProviderManager.put("invoice.payment.remained", new PaiementRemainedProvider(Type.REMAINED));
44
        SpreadSheetCellValueProviderManager.put("invoice.payment.done", new PaiementRemainedProvider(Type.DONE));
93 ilm 45
        SpreadSheetCellValueProviderManager.put("invoice.paiement.remained", new PaiementRemainedProvider(Type.REMAINED));
46
        SpreadSheetCellValueProviderManager.put("invoice.paiement.done", new PaiementRemainedProvider(Type.DONE));
83 ilm 47
    }
48
 
49
    private BigDecimal getRestant(SQLRowAccessor r) {
50
        Collection<? extends SQLRowAccessor> rows = r.getReferentRows(r.getTable().getTable("ECHEANCE_CLIENT"));
51
        long totalEch = 0;
52
 
53
        for (SQLRowAccessor row : rows) {
54
            if (!row.getBoolean("REGLE") && !row.getBoolean("REG_COMPTA")) {
55
                totalEch += row.getLong("MONTANT");
56
            }
57
        }
142 ilm 58
        if (r.getTable().contains("ACOMPTE_COMMANDE") && r.getBigDecimal("ACOMPTE_COMMANDE") != null) {
59
            long totalAcompte = r.getBigDecimal("ACOMPTE_COMMANDE").movePointRight(2).setScale(0, RoundingMode.HALF_UP).longValue();
144 ilm 60
            if (totalEch >= totalAcompte) {
142 ilm 61
                totalEch -= totalAcompte;
62
            }
63
        }
83 ilm 64
 
93 ilm 65
        long total = totalEch;
66
        if (t == Type.DONE) {
67
            total = r.getLong("T_TTC") - total;
182 ilm 68
            //TODO deduire T avoir
93 ilm 69
        }
70
        return new BigDecimal(total).movePointLeft(2);
83 ilm 71
    }
72
 
73
}