OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | 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
 *
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.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)));
73 ilm 67
        PredicateRowAction toInvoiceAction = new PredicateRowAction(new AbstractAction() {
68
            public void actionPerformed(ActionEvent e) {
93 ilm 69
                transfertFactureClient(IListe.get(e).getSelectedRows());
18 ilm 70
            }
73 ilm 71
        }, false, "sales.shipment.create.invoice");
72
        toInvoiceAction.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
18 ilm 73
 
73 ilm 74
        // Tabs
75
        final JTabbedPane tabs = new JTabbedPane();
177 ilm 76
        tabs.addTab(getConf().getERP_TM().translate("sales.shipment.allShipments"), createAllDeliveryPanel(toInvoiceAction));
77
        tabs.addTab(getConf().getERP_TM().translate("sales.shipment.nonInvoicedShipments"), createDeliveryWithoutInvoicePanel(toInvoiceAction));
78
        tabs.addTab(getConf().getERP_TM().translate("sales.shipment.invoicedShipments"), createDeliveryWithInvoicePanel());
73 ilm 79
        frame.setContentPane(tabs);
18 ilm 80
 
73 ilm 81
        return frame;
82
    }
18 ilm 83
 
156 ilm 84
    private ListeAddPanel getPanel(final BonDeLivraisonSQLElement eltCmd, final SQLTableModelSourceOnline tableSource, final List<RowAction> allowedActions) {
73 ilm 85
        final ListeAddPanel panel = new ListeAddPanel(eltCmd, new IListe(tableSource));
18 ilm 86
 
73 ilm 87
        final List<SQLField> fields = new ArrayList<SQLField>(2);
88
        fields.add(eltCmd.getTable().getField("TOTAL_HT"));
177 ilm 89
        final IListTotalPanel totalPanel = new IListTotalPanel(panel.getListe(), fields, getConf().getERP_TM().translate("sales.shipment.listTotal"));
73 ilm 90
 
91
        final GridBagConstraints c = new DefaultGridBagConstraints();
92
        c.gridwidth = GridBagConstraints.REMAINDER;
93
        c.fill = GridBagConstraints.BOTH;
94
        c.anchor = GridBagConstraints.EAST;
95
        c.weightx = 1;
96
        c.gridy = 4;
97
 
98
        // Date panel
99
        final IListFilterDatePanel datePanel = new IListFilterDatePanel(panel.getListe(), eltCmd.getTable().getField("DATE"), IListFilterDatePanel.getDefaultMap());
100
 
93 ilm 101
        panel.getListe().addIListeActions(new MouseSheetXmlListeListener(BonLivraisonXmlSheet.class) {
18 ilm 102
            @Override
25 ilm 103
            public List<RowAction> addToMenu() {
73 ilm 104
                return allowedActions;
18 ilm 105
            }
25 ilm 106
        }.getRowActions());
18 ilm 107
 
156 ilm 108
        panel.getListe().addIListeAction(eltCmd.getCloneAction());
109
 
73 ilm 110
        datePanel.setFilterOnDefault();
111
 
112
        final JPanel bottomPanel = new JPanel();
113
        bottomPanel.setLayout(new GridBagLayout());
114
        bottomPanel.setOpaque(false);
115
        final GridBagConstraints c2 = new DefaultGridBagConstraints();
116
        c2.fill = GridBagConstraints.NONE;
117
        c2.weightx = 1;
118
        bottomPanel.add(datePanel, c2);
119
 
120
        c2.gridx++;
121
        c2.weightx = 0;
122
        c2.anchor = GridBagConstraints.EAST;
123
        bottomPanel.add(totalPanel, c2);
124
 
125
        panel.add(bottomPanel, c);
126
        return panel;
18 ilm 127
    }
128
 
73 ilm 129
    JPanel createAllDeliveryPanel(final PredicateRowAction toInvoiceAction) {
156 ilm 130
        final BonDeLivraisonSQLElement eltCmd = getElem();
73 ilm 131
        final SQLTableModelSourceOnline tableSource = eltCmd.getTableSource(true);
132
        final List<RowAction> allowedActions = new ArrayList<RowAction>();
133
        allowedActions.add(toInvoiceAction);
156 ilm 134
        BaseSQLTableModelColumn colAvancement = new BaseSQLTableModelColumn(eltCmd.getDirectory().getTranslator().getDescFor(eltCmd.getTable(), "invoiceProgress").getTitleLabel(), BigDecimal.class) {
94 ilm 135
 
136
            @Override
137
            protected Object show_(SQLRowAccessor r) {
138
 
139
                return getAvancement(r);
140
            }
141
 
142
            @Override
143
            public Set<FieldPath> getPaths() {
144
                final Path p = new PathBuilder(eltCmd.getTable()).addTable("TR_BON_DE_LIVRAISON").addTable("SAISIE_VENTE_FACTURE").build();
145
                return CollectionUtils.createSet(new FieldPath(p, "T_HT"));
146
            }
147
        };
148
        tableSource.getColumns().add(colAvancement);
149
        colAvancement.setRenderer(new PercentTableCellRenderer());
73 ilm 150
        final ListeAddPanel panel = getPanel(eltCmd, tableSource, allowedActions);
151
        return panel;
152
    }
153
 
94 ilm 154
    private BigDecimal getAvancement(SQLRowAccessor r) {
155
        Collection<? extends SQLRowAccessor> rows = r.getReferentRows(r.getTable().getTable("TR_BON_DE_LIVRAISON"));
156
        long totalFact = 0;
157
        long total = r.getLong("TOTAL_HT");
158
        for (SQLRowAccessor row : rows) {
159
            if (!row.isForeignEmpty("ID_SAISIE_VENTE_FACTURE")) {
160
                SQLRowAccessor rowFact = row.getForeign("ID_SAISIE_VENTE_FACTURE");
161
                Long l = rowFact.getLong("T_HT");
162
                totalFact += l;
163
            }
164
        }
165
        if (total > 0) {
166
            return new BigDecimal(totalFact).divide(new BigDecimal(total), DecimalUtils.HIGH_PRECISION).movePointRight(2).setScale(2, RoundingMode.HALF_UP);
167
        } else {
168
            return BigDecimal.ONE.movePointRight(2);
169
        }
170
    }
171
 
73 ilm 172
    JPanel createDeliveryWithInvoicePanel() {
156 ilm 173
        final BonDeLivraisonSQLElement eltCmd = getElem();
73 ilm 174
        final SQLTableModelSourceOnline tableSource = eltCmd.getTableSource(true);
175
        final List<RowAction> allowedActions = new ArrayList<RowAction>();
176
 
177
        // Filter on transfered
178
        final SQLInjector injector = SQLInjector.getInjector(eltCmd.getTable(), eltCmd.getTable().getTable("SAISIE_VENTE_FACTURE"));
179
        injector.setOnlyTransfered(tableSource);
180
 
181
        final ListeAddPanel panel = getPanel(eltCmd, tableSource, allowedActions);
182
        return panel;
183
    }
184
 
185
    JPanel createDeliveryWithoutInvoicePanel(PredicateRowAction toInvoiceAction) {
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
        allowedActions.add(toInvoiceAction);
190
 
191
        // Filter on not transfered
192
        final SQLInjector injector = SQLInjector.getInjector(eltCmd.getTable(), eltCmd.getTable().getTable("SAISIE_VENTE_FACTURE"));
193
        injector.setOnlyNotTransfered(tableSource);
194
 
195
        final ListeAddPanel panel = getPanel(eltCmd, tableSource, allowedActions);
196
        return panel;
197
    }
198
 
18 ilm 199
    /**
200
     * Transfert en Facture
201
     *
202
     * @param row
203
     */
73 ilm 204
    private void transfertFactureClient(List<SQLRowValues> rows) {
205
        TransfertBaseSQLComponent.openTransfertFrame(rows, "SAISIE_VENTE_FACTURE");
18 ilm 206
    }
207
}