OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
80 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.supplychain.order.element;
15
 
94 ilm 16
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
151 ilm 17
import org.openconcerto.erp.core.edm.AttachmentAction;
94 ilm 18
import org.openconcerto.erp.core.supplychain.order.component.FactureFournisseurSQLComponent;
19
import org.openconcerto.erp.generationDoc.gestcomm.FactureFournisseurXmlSheet;
20
import org.openconcerto.erp.model.MouseSheetXmlListeListener;
21
import org.openconcerto.sql.element.SQLComponent;
177 ilm 22
import org.openconcerto.sql.model.FieldPath;
174 ilm 23
import org.openconcerto.sql.model.SQLRowAccessor;
177 ilm 24
import org.openconcerto.sql.model.graph.Path;
174 ilm 25
import org.openconcerto.sql.view.EditFrame;
26
import org.openconcerto.sql.view.EditPanel;
177 ilm 27
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
174 ilm 28
import org.openconcerto.sql.view.list.IListe;
177 ilm 29
import org.openconcerto.sql.view.list.RowAction;
30
import org.openconcerto.sql.view.list.SQLTableModelSource;
151 ilm 31
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
32
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
177 ilm 33
import org.openconcerto.ui.table.PercentTableCellRenderer;
34
import org.openconcerto.utils.CollectionUtils;
35
import org.openconcerto.utils.DecimalUtils;
142 ilm 36
import org.openconcerto.utils.ListMap;
94 ilm 37
 
174 ilm 38
import java.awt.event.ActionEvent;
177 ilm 39
import java.math.BigDecimal;
40
import java.math.RoundingMode;
151 ilm 41
import java.util.ArrayList;
177 ilm 42
import java.util.Collection;
156 ilm 43
import java.util.HashSet;
151 ilm 44
import java.util.List;
156 ilm 45
import java.util.Set;
151 ilm 46
 
174 ilm 47
import javax.swing.AbstractAction;
48
 
80 ilm 49
public class FactureFournisseurSQLElement extends ComptaSQLConfElement {
50
 
51
    public FactureFournisseurSQLElement() {
52
        super("FACTURE_FOURNISSEUR", "une facture fournisseur", "factures fournisseur");
94 ilm 53
        MouseSheetXmlListeListener mouseSheetXmlListeListener = new MouseSheetXmlListeListener(FactureFournisseurXmlSheet.class);
54
        mouseSheetXmlListeListener.setGenerateHeader(true);
55
        mouseSheetXmlListeListener.setShowHeader(true);
56
        getRowActions().addAll(mouseSheetXmlListeListener.getRowActions());
151 ilm 57
        if (getTable().contains("ATTACHMENTS")) {
58
            PredicateRowAction actionAttachment = new PredicateRowAction(new AttachmentAction().getAction(), true);
59
            actionAttachment.setPredicate(IListeEvent.getSingleSelectionPredicate());
60
            getRowActions().add(actionAttachment);
61
        }
174 ilm 62
        RowAction actionClone = new RowAction(new AbstractAction() {
63
            public void actionPerformed(ActionEvent e) {
64
 
65
                EditFrame editFrame = new EditFrame(FactureFournisseurSQLElement.this, EditPanel.CREATION);
66
 
67
                ((FactureFournisseurSQLComponent) editFrame.getSQLComponent()).loadFactureExistante(IListe.get(e).getSelectedId());
68
                editFrame.setVisible(true);
69
            }
70
        }, true, "supplychain.invoice.clone") {
71
            public boolean enabledFor(IListeEvent evt) {
72
                List<? extends SQLRowAccessor> l = evt.getSelectedRows();
73
                return (l != null && l.size() == 1);
74
            }
75
        };
76
        getRowActions().add(actionClone);
80 ilm 77
    }
78
 
156 ilm 79
    @Override
80
    public Set<String> getReadOnlyFields() {
81
        Set<String> s = new HashSet<>();
82
        s.add("NET_A_PAYER");
83
        return s;
84
    }
85
 
80 ilm 86
    protected List<String> getListFields() {
87
        final List<String> l = new ArrayList<String>();
88
        l.add("NUMERO");
89
        l.add("NOM");
90
        l.add("DATE");
91
        l.add("ID_FOURNISSEUR");
92
        l.add("T_HT");
93
        l.add("T_TTC");
94
        l.add("INFOS");
177 ilm 95
        l.add("DATE_REGLEMENT");
80 ilm 96
        return l;
97
    }
98
 
142 ilm 99
    @Override
100
    public ListMap<String, String> getShowAs() {
101
        ListMap<String, String> map = new ListMap<String, String>();
102
        map.putCollection(null, "NUMERO", "DATE");
103
        return map;
104
    }
105
 
80 ilm 106
    protected List<String> getComboFields() {
107
        final List<String> l = new ArrayList<String>();
108
        l.add("NUMERO");
109
        l.add("NOM");
110
        l.add("DATE");
111
        return l;
112
    }
113
 
177 ilm 114
    @Override
115
    protected void _initTableSource(SQLTableModelSource res) {
116
        super._initTableSource(res);
117
 
118
        final BaseSQLTableModelColumn colAvancement = new BaseSQLTableModelColumn("Avancement réglement", BigDecimal.class) {
119
 
120
            @Override
121
            protected Object show_(SQLRowAccessor r) {
122
 
123
                return getAvancement(r);
124
            }
125
 
126
            @Override
127
            public Set<FieldPath> getPaths() {
128
                Path p = new Path(getTable());
129
                p = p.add(getTable().getTable("ECHEANCE_FOURNISSEUR"));
130
 
131
                Path p2 = new Path(getTable());
132
                p2 = p2.add(getTable().getField("ID_AVOIR_FOURNISSEUR"));
133
 
134
                return CollectionUtils.createSet(new FieldPath(p, "MONTANT"), new FieldPath(p, "REG_COMPTA"), new FieldPath(p, "REGLE"), new FieldPath(p2, "MONTANT_TTC"));
135
            }
136
        };
137
        res.getColumns().add(colAvancement);
138
        colAvancement.setRenderer(new PercentTableCellRenderer());
139
 
140
    }
141
 
142
    private BigDecimal getAvancement(SQLRowAccessor r) {
143
        Collection<? extends SQLRowAccessor> rows = r.getReferentRows(r.getTable().getTable("ECHEANCE_FOURNISSEUR"));
144
        long totalEch = 0;
145
 
146
        for (SQLRowAccessor row : rows) {
147
            if (!row.getBoolean("REGLE") && !row.getBoolean("REG_COMPTA")) {
148
                totalEch += row.getLong("MONTANT");
149
            }
150
        }
151
 
152
        SQLRowAccessor avoir = r.getForeign("ID_AVOIR_FOURNISSEUR");
153
        BigDecimal avoirTTC = BigDecimal.ZERO;
154
        if (avoir != null && !avoir.isUndefined()) {
155
            avoirTTC = new BigDecimal(avoir.getLong("MONTANT_TTC"));
156
        }
157
 
158
        final BigDecimal totalAregler = new BigDecimal(r.getLong("T_TTC")).subtract(avoirTTC);
159
        if (totalAregler.signum() > 0 && totalEch > 0) {
160
            return totalAregler.subtract(new BigDecimal(totalEch)).divide(totalAregler, DecimalUtils.HIGH_PRECISION).movePointRight(2).setScale(2, RoundingMode.HALF_UP);
161
        } else {
162
            return BigDecimal.ONE.movePointRight(2);
163
        }
164
    }
165
 
80 ilm 166
    /*
167
     * (non-Javadoc)
168
     *
169
     * @see org.openconcerto.devis.SQLElement#getComponent()
170
     */
171
    public SQLComponent createComponent() {
172
        return new FactureFournisseurSQLComponent();
173
    }
174
 
175
    @Override
176
    protected String createCode() {
156 ilm 177
        return createCodeOfPackage() + ".invoice.purchase";
80 ilm 178
    }
179
}