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
18 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.
18 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.sales.shipment.action;
15
 
156 ilm 16
import org.openconcerto.erp.action.CreateListFrameAbstractAction;
17
import org.openconcerto.erp.config.ComptaPropsConfiguration;
73 ilm 18
import org.openconcerto.erp.core.common.component.TransfertBaseSQLComponent;
19
import org.openconcerto.erp.core.common.ui.IListFilterDatePanel;
20
import org.openconcerto.erp.core.common.ui.IListTotalPanel;
156 ilm 21
import org.openconcerto.erp.core.sales.shipment.element.BonDeLivraisonSQLElement;
93 ilm 22
import org.openconcerto.erp.core.sales.shipment.report.BonLivraisonXmlSheet;
18 ilm 23
import org.openconcerto.erp.model.MouseSheetXmlListeListener;
94 ilm 24
import org.openconcerto.sql.model.FieldPath;
73 ilm 25
import org.openconcerto.sql.model.SQLField;
26
import org.openconcerto.sql.model.SQLInjector;
94 ilm 27
import org.openconcerto.sql.model.SQLRowAccessor;
73 ilm 28
import org.openconcerto.sql.model.SQLRowValues;
94 ilm 29
import org.openconcerto.sql.model.graph.Path;
30
import org.openconcerto.sql.model.graph.PathBuilder;
18 ilm 31
import org.openconcerto.sql.view.ListeAddPanel;
94 ilm 32
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
25 ilm 33
import org.openconcerto.sql.view.list.IListe;
34
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
35
import org.openconcerto.sql.view.list.RowAction;
36
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
73 ilm 37
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
18 ilm 38
import org.openconcerto.ui.DefaultGridBagConstraints;
94 ilm 39
import org.openconcerto.ui.table.PercentTableCellRenderer;
40
import org.openconcerto.utils.CollectionUtils;
41
import org.openconcerto.utils.DecimalUtils;
18 ilm 42
 
43
import java.awt.GridBagConstraints;
73 ilm 44
import java.awt.GridBagLayout;
18 ilm 45
import java.awt.event.ActionEvent;
94 ilm 46
import java.math.BigDecimal;
47
import java.math.RoundingMode;
18 ilm 48
import java.util.ArrayList;
94 ilm 49
import java.util.Collection;
18 ilm 50
import java.util.List;
94 ilm 51
import java.util.Set;
18 ilm 52
 
53
import javax.swing.AbstractAction;
54
import javax.swing.JFrame;
55
import javax.swing.JPanel;
73 ilm 56
import javax.swing.JTabbedPane;
18 ilm 57
 
156 ilm 58
public class ListeDesBonsDeLivraisonAction extends CreateListFrameAbstractAction<BonDeLivraisonSQLElement, JFrame> {
18 ilm 59
 
156 ilm 60
    public ListeDesBonsDeLivraisonAction(final ComptaPropsConfiguration conf) {
61
        super(conf, BonDeLivraisonSQLElement.class);
18 ilm 62
    }
63
 
156 ilm 64
    @Override
65
    protected JFrame instantiateFrame() {
66
        final JFrame frame = new JFrame(String.valueOf(getValue(NAME)));
182 ilm 67
        RowAction toInvoiceAction = new RowAction(new AbstractAction() {
73 ilm 68
            public void actionPerformed(ActionEvent e) {
93 ilm 69
                transfertFactureClient(IListe.get(e).getSelectedRows());
18 ilm 70
            }
182 ilm 71
        }, false, "sales.shipment.create.invoice") {
72
            @Override
73
            public boolean enabledFor(List<SQLRowValues> selection) {
18 ilm 74
 
182 ilm 75
                if (!selection.isEmpty()) {
76
                    for (SQLRowValues sqlRowValues : selection) {
77
                        if (sqlRowValues.getBoolean("VERROU_FACTURATION")) {
78
                            return false;
79
                        }
80
                    }
81
                    return true;
82
                }
83
                return false;
84
 
85
            }
86
        };
73 ilm 87
        // Tabs
88
        final JTabbedPane tabs = new JTabbedPane();
177 ilm 89
        tabs.addTab(getConf().getERP_TM().translate("sales.shipment.allShipments"), createAllDeliveryPanel(toInvoiceAction));
90
        tabs.addTab(getConf().getERP_TM().translate("sales.shipment.nonInvoicedShipments"), createDeliveryWithoutInvoicePanel(toInvoiceAction));
91
        tabs.addTab(getConf().getERP_TM().translate("sales.shipment.invoicedShipments"), createDeliveryWithInvoicePanel());
73 ilm 92
        frame.setContentPane(tabs);
18 ilm 93
 
73 ilm 94
        return frame;
95
    }
18 ilm 96
 
156 ilm 97
    private ListeAddPanel getPanel(final BonDeLivraisonSQLElement eltCmd, final SQLTableModelSourceOnline tableSource, final List<RowAction> allowedActions) {
73 ilm 98
        final ListeAddPanel panel = new ListeAddPanel(eltCmd, new IListe(tableSource));
18 ilm 99
 
73 ilm 100
        final List<SQLField> fields = new ArrayList<SQLField>(2);
101
        fields.add(eltCmd.getTable().getField("TOTAL_HT"));
177 ilm 102
        final IListTotalPanel totalPanel = new IListTotalPanel(panel.getListe(), fields, getConf().getERP_TM().translate("sales.shipment.listTotal"));
73 ilm 103
 
104
        final GridBagConstraints c = new DefaultGridBagConstraints();
105
        c.gridwidth = GridBagConstraints.REMAINDER;
106
        c.fill = GridBagConstraints.BOTH;
107
        c.anchor = GridBagConstraints.EAST;
108
        c.weightx = 1;
109
        c.gridy = 4;
110
 
111
        // Date panel
112
        final IListFilterDatePanel datePanel = new IListFilterDatePanel(panel.getListe(), eltCmd.getTable().getField("DATE"), IListFilterDatePanel.getDefaultMap());
113
 
182 ilm 114
        panel.getListe().addIListeActions(new MouseSheetXmlListeListener(eltCmd, BonLivraisonXmlSheet.class) {
18 ilm 115
            @Override
25 ilm 116
            public List<RowAction> addToMenu() {
73 ilm 117
                return allowedActions;
18 ilm 118
            }
25 ilm 119
        }.getRowActions());
18 ilm 120
 
156 ilm 121
        panel.getListe().addIListeAction(eltCmd.getCloneAction());
122
 
73 ilm 123
        datePanel.setFilterOnDefault();
124
 
125
        final JPanel bottomPanel = new JPanel();
126
        bottomPanel.setLayout(new GridBagLayout());
127
        bottomPanel.setOpaque(false);
128
        final GridBagConstraints c2 = new DefaultGridBagConstraints();
129
        c2.fill = GridBagConstraints.NONE;
130
        c2.weightx = 1;
131
        bottomPanel.add(datePanel, c2);
132
 
133
        c2.gridx++;
134
        c2.weightx = 0;
135
        c2.anchor = GridBagConstraints.EAST;
136
        bottomPanel.add(totalPanel, c2);
137
 
138
        panel.add(bottomPanel, c);
139
        return panel;
18 ilm 140
    }
141
 
182 ilm 142
    JPanel createAllDeliveryPanel(final RowAction toInvoiceAction) {
156 ilm 143
        final BonDeLivraisonSQLElement eltCmd = getElem();
73 ilm 144
        final SQLTableModelSourceOnline tableSource = eltCmd.getTableSource(true);
145
        final List<RowAction> allowedActions = new ArrayList<RowAction>();
146
        allowedActions.add(toInvoiceAction);
156 ilm 147
        BaseSQLTableModelColumn colAvancement = new BaseSQLTableModelColumn(eltCmd.getDirectory().getTranslator().getDescFor(eltCmd.getTable(), "invoiceProgress").getTitleLabel(), BigDecimal.class) {
94 ilm 148
 
149
            @Override
150
            protected Object show_(SQLRowAccessor r) {
151
 
152
                return getAvancement(r);
153
            }
154
 
155
            @Override
156
            public Set<FieldPath> getPaths() {
157
                final Path p = new PathBuilder(eltCmd.getTable()).addTable("TR_BON_DE_LIVRAISON").addTable("SAISIE_VENTE_FACTURE").build();
158
                return CollectionUtils.createSet(new FieldPath(p, "T_HT"));
159
            }
160
        };
161
        tableSource.getColumns().add(colAvancement);
162
        colAvancement.setRenderer(new PercentTableCellRenderer());
73 ilm 163
        final ListeAddPanel panel = getPanel(eltCmd, tableSource, allowedActions);
164
        return panel;
165
    }
166
 
94 ilm 167
    private BigDecimal getAvancement(SQLRowAccessor r) {
168
        Collection<? extends SQLRowAccessor> rows = r.getReferentRows(r.getTable().getTable("TR_BON_DE_LIVRAISON"));
169
        long totalFact = 0;
170
        long total = r.getLong("TOTAL_HT");
171
        for (SQLRowAccessor row : rows) {
172
            if (!row.isForeignEmpty("ID_SAISIE_VENTE_FACTURE")) {
173
                SQLRowAccessor rowFact = row.getForeign("ID_SAISIE_VENTE_FACTURE");
174
                Long l = rowFact.getLong("T_HT");
175
                totalFact += l;
176
            }
177
        }
178
        if (total > 0) {
179
            return new BigDecimal(totalFact).divide(new BigDecimal(total), DecimalUtils.HIGH_PRECISION).movePointRight(2).setScale(2, RoundingMode.HALF_UP);
180
        } else {
181
            return BigDecimal.ONE.movePointRight(2);
182
        }
183
    }
184
 
73 ilm 185
    JPanel createDeliveryWithInvoicePanel() {
156 ilm 186
        final BonDeLivraisonSQLElement eltCmd = getElem();
73 ilm 187
        final SQLTableModelSourceOnline tableSource = eltCmd.getTableSource(true);
188
        final List<RowAction> allowedActions = new ArrayList<RowAction>();
189
 
190
        // Filter on transfered
191
        final SQLInjector injector = SQLInjector.getInjector(eltCmd.getTable(), eltCmd.getTable().getTable("SAISIE_VENTE_FACTURE"));
192
        injector.setOnlyTransfered(tableSource);
193
 
194
        final ListeAddPanel panel = getPanel(eltCmd, tableSource, allowedActions);
195
        return panel;
196
    }
197
 
182 ilm 198
    JPanel createDeliveryWithoutInvoicePanel(RowAction toInvoiceAction) {
156 ilm 199
        final BonDeLivraisonSQLElement eltCmd = getElem();
73 ilm 200
        final SQLTableModelSourceOnline tableSource = eltCmd.getTableSource(true);
201
        final List<RowAction> allowedActions = new ArrayList<RowAction>();
202
        allowedActions.add(toInvoiceAction);
203
 
204
        // Filter on not transfered
205
        final SQLInjector injector = SQLInjector.getInjector(eltCmd.getTable(), eltCmd.getTable().getTable("SAISIE_VENTE_FACTURE"));
206
        injector.setOnlyNotTransfered(tableSource);
207
 
208
        final ListeAddPanel panel = getPanel(eltCmd, tableSource, allowedActions);
209
        return panel;
210
    }
211
 
18 ilm 212
    /**
213
     * Transfert en Facture
214
     *
215
     * @param row
216
     */
73 ilm 217
    private void transfertFactureClient(List<SQLRowValues> rows) {
218
        TransfertBaseSQLComponent.openTransfertFrame(rows, "SAISIE_VENTE_FACTURE");
18 ilm 219
    }
220
}