OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Rev 182 | 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
 *
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.customerrelationship.customer.element;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
67 ilm 18
import org.openconcerto.erp.core.customerrelationship.customer.report.FicheClientXmlSheet;
142 ilm 19
import org.openconcerto.erp.preferences.GestionClientPreferencePanel;
67 ilm 20
import org.openconcerto.erp.preferences.PrinterNXProps;
21
import org.openconcerto.ql.LabelCreator;
22
import org.openconcerto.ql.QLPrinter;
18 ilm 23
import org.openconcerto.sql.Configuration;
24
import org.openconcerto.sql.element.SQLComponent;
174 ilm 25
import org.openconcerto.sql.model.FieldPath;
67 ilm 26
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 27
import org.openconcerto.sql.model.SQLRowValues;
174 ilm 28
import org.openconcerto.sql.model.graph.Path;
142 ilm 29
import org.openconcerto.sql.preferences.SQLPreferences;
18 ilm 30
import org.openconcerto.sql.request.ListSQLRequest;
174 ilm 31
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
67 ilm 32
import org.openconcerto.sql.view.list.IListe;
33
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
34
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
174 ilm 35
import org.openconcerto.sql.view.list.SQLTableModelSource;
67 ilm 36
import org.openconcerto.ui.EmailComposer;
174 ilm 37
import org.openconcerto.utils.CollectionUtils;
67 ilm 38
import org.openconcerto.utils.ExceptionHandler;
18 ilm 39
 
67 ilm 40
import java.awt.Font;
41
import java.awt.event.ActionEvent;
18 ilm 42
import java.util.ArrayList;
132 ilm 43
import java.util.Arrays;
18 ilm 44
import java.util.List;
174 ilm 45
import java.util.Set;
18 ilm 46
 
67 ilm 47
import javax.swing.AbstractAction;
48
 
18 ilm 49
public class ClientNormalSQLElement extends ComptaSQLConfElement {
50
 
51
    public ClientNormalSQLElement() {
52
        super("CLIENT", "un client", "clients");
144 ilm 53
        try {
54
            final String property = PrinterNXProps.getInstance().getProperty("QLPrinter");
55
                if (property != null && property.trim().length() > 0) {
56
                    PredicateRowAction actionPrintLabel = new PredicateRowAction(new AbstractAction() {
67 ilm 57
 
144 ilm 58
                        @Override
59
                        public void actionPerformed(ActionEvent e) {
60
                            final SQLRowAccessor row = IListe.get(e).fetchSelectedRow();
61
                            printLabel(row, property);
62
                        }
63
                    }, false, "customerrelationship.customer.label.print");
64
                    actionPrintLabel.setPredicate(IListeEvent.getSingleSelectionPredicate());
65
                    getRowActions().add(actionPrintLabel);
66
                }
67
 
68
                PredicateRowAction actionFicheClient = new PredicateRowAction(new AbstractAction() {
67 ilm 69
                    @Override
70
                    public void actionPerformed(ActionEvent e) {
80 ilm 71
                        final SQLRowAccessor row = IListe.get(e).fetchSelectedRow();
144 ilm 72
                        FicheClientXmlSheet sheet = new FicheClientXmlSheet(row.asRow());
73
                        sheet.createDocumentAsynchronous();
74
                        sheet.showPrintAndExportAsynchronous(true, false, true);
67 ilm 75
                    }
144 ilm 76
                }, false, "customerrelationship.customer.info.create");
77
                actionFicheClient.setPredicate(IListeEvent.getSingleSelectionPredicate());
78
                getRowActions().add(actionFicheClient);
67 ilm 79
 
144 ilm 80
 
81
            PredicateRowAction action = new PredicateRowAction(new AbstractAction() {
82
 
132 ilm 83
                @Override
84
                public void actionPerformed(ActionEvent e) {
144 ilm 85
                    sendMail(IListe.get(e).getSelectedRows());
86
 
132 ilm 87
                }
144 ilm 88
            }, true, "customerrelationship.customer.email.send");
89
            action.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
90
            getRowActions().add(action);
67 ilm 91
 
144 ilm 92
        } catch (Exception e) {
93
            e.printStackTrace();
94
        }
18 ilm 95
    }
96
 
142 ilm 97
    public void printLabel(SQLRowAccessor row, String qlPrinterProperty) {
98
        final LabelCreator c = new LabelCreator(720);
99
        c.setLeftMargin(10);
100
        c.setTopMargin(10);
101
        c.setDefaultFont(new Font("Verdana", Font.PLAIN, 50));
102
        c.addLineBold(row.getString("NOM"));
103
        final SQLRowAccessor foreignRow = row.asRow().getForeign("ID_ADRESSE");
104
        final String string = foreignRow.getString("RUE");
105
        String[] s = string.split("\n");
106
        for (String string2 : s) {
107
            c.addLineNormal(string2);
108
        }
109
        c.addLineNormal(foreignRow.getString("CODE_POSTAL") + " " + foreignRow.getString("VILLE"));
110
 
111
        final String pays = foreignRow.getString("PAYS");
112
        if (pays != null && pays.trim().length() > 0 && !pays.equalsIgnoreCase(ComptaPropsConfiguration.getInstanceCompta().getRowSociete().getForeignRow("ID_ADRESSE_COMMON").getString("PAYS"))) {
113
            c.addLineNormal(pays);
114
        }
115
 
116
        final QLPrinter prt = new QLPrinter(qlPrinterProperty);
117
        try {
118
            prt.print(c.getImage());
119
        } catch (Exception ex) {
120
            ex.printStackTrace();
121
        }
122
    }
123
 
93 ilm 124
    protected void sendMail(List<SQLRowValues> l) {
67 ilm 125
 
126
        String mail = "";
127
            for (SQLRowAccessor rowCli : l) {
128
                String string = rowCli.getString("MAIL");
93 ilm 129
 
130
                if (string == null) {
131
                    // Refetch au cas où la colonne n'est pas présente dans la liste
132
                    string = rowCli.asRow().getString("MAIL");
133
                }
67 ilm 134
                if (string != null && string.trim().length() > 0) {
135
                    mail += string + ";";
136
                }
137
            }
138
        try {
139
            EmailComposer.getInstance().compose(mail, "", "");
140
        } catch (Exception exn) {
141
            ExceptionHandler.handle(null, "Impossible de créer le courriel", exn);
142
        }
143
 
144
    }
145
 
18 ilm 146
    protected boolean showMdr = true;
147
 
148
    protected List<String> getListFields() {
149
        final List<String> l = new ArrayList<String>();
150
            l.add("CODE");
151
        l.add("NOM");
41 ilm 152
        l.add("RESPONSABLE");
18 ilm 153
        l.add("ID_ADRESSE");
154
        l.add("TEL");
80 ilm 155
        l.add("TEL_P");
18 ilm 156
        l.add("FAX");
157
        l.add("MAIL");
158
            l.add("NUMERO_TVA");
159
            l.add("SIRET");
160
            l.add("ID_COMPTE_PCE");
161
            l.add("ID_MODE_REGLEMENT");
162
        l.add("INFOS");
149 ilm 163
        l.add("ID_COMMERCIAL");
18 ilm 164
        return l;
165
    }
166
 
167
    @Override
132 ilm 168
    protected void _initListRequest(ListSQLRequest req) {
169
        super._initListRequest(req);
170
        req.addForeignToGraphToFetch("ID_MODE_REGLEMENT", Arrays.asList("AJOURS", "LENJOUR"));
18 ilm 171
    }
172
 
173
    protected List<String> getComboFields() {
174
        final List<String> l = new ArrayList<String>();
93 ilm 175
        l.add("GROUPE");
18 ilm 176
        l.add("NOM");
25 ilm 177
        if (getTable().getFieldsName().contains("LOCALISATION")) {
178
            l.add("LOCALISATION");
179
        } else {
180
            l.add("CODE");
181
        }
142 ilm 182
        SQLPreferences prefs = SQLPreferences.getMemCached(getTable().getDBRoot());
183
        if (prefs.getBoolean(GestionClientPreferencePanel.DISPLAY_CLIENT_PCE, false)) {
184
            l.add("ID_COMPTE_PCE");
185
        }
18 ilm 186
        return l;
187
    }
188
 
174 ilm 189
    @Override
190
    protected void _initTableSource(SQLTableModelSource res) {
191
        super._initTableSource(res);
192
 
193
        res.getColumns().add(new BaseSQLTableModelColumn(getDirectory().getTranslator().getLabelFor(getTable().getField("CATEGORIES")), String.class) {
194
 
195
            @Override
196
            protected Object show_(SQLRowAccessor r) {
197
 
198
                return r.getString("CATEGORIES");
199
            }
200
 
201
            @Override
202
            public Set<FieldPath> getPaths() {
203
                Path p = new Path(getTable());
204
                return CollectionUtils.createSet(new FieldPath(p, "CATEGORIES"));
205
 
206
            }
207
        });
208
 
209
    }
210
 
18 ilm 211
    /*
212
     * (non-Javadoc)
213
     *
214
     * @see org.openconcerto.devis.SQLElement#getComponent()
215
     */
216
    public SQLComponent createComponent() {
28 ilm 217
        return new ClientNormalSQLComponent(this);
18 ilm 218
    }
219
 
156 ilm 220
    @Override
221
    protected String createCode() {
222
        return "customerrelationship.customer";
223
    }
18 ilm 224
}