OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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