OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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