OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 174 Rev 182
Line 1... Line 1...
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
Line 13... Line 13...
13
 
13
 
14
 package org.openconcerto.erp.generationDoc;
14
 package org.openconcerto.erp.generationDoc;
15
 
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.common.ui.PreviewFrame;
17
import org.openconcerto.erp.core.common.ui.PreviewFrame;
-
 
18
import org.openconcerto.erp.core.customerrelationship.mail.EmailTemplate;
-
 
19
import org.openconcerto.erp.core.customerrelationship.mail.ValueListener;
18
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureXmlSheet;
20
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureXmlSheet;
19
import org.openconcerto.erp.core.sales.quote.report.PaypalStamper;
21
import org.openconcerto.erp.core.sales.quote.report.PaypalStamper;
20
import org.openconcerto.erp.generationDoc.element.TypeModeleSQLElement;
22
import org.openconcerto.erp.generationDoc.element.TypeModeleSQLElement;
-
 
23
import org.openconcerto.erp.model.MouseSheetXmlListeListener;
21
import org.openconcerto.erp.preferences.PayPalPreferencePanel;
24
import org.openconcerto.erp.preferences.PayPalPreferencePanel;
22
import org.openconcerto.erp.storage.CloudStorageEngine;
25
import org.openconcerto.erp.storage.CloudStorageEngine;
23
import org.openconcerto.erp.storage.StorageEngine;
26
import org.openconcerto.erp.storage.StorageEngine;
24
import org.openconcerto.erp.storage.StorageEngines;
27
import org.openconcerto.erp.storage.StorageEngines;
25
import org.openconcerto.openoffice.OOUtils;
28
import org.openconcerto.openoffice.OOUtils;
26
import org.jopendocument.link.Component;
29
import org.jopendocument.link.Component;
27
import org.openconcerto.sql.Configuration;
30
import org.openconcerto.sql.Configuration;
-
 
31
import org.openconcerto.sql.element.SQLComponent;
28
import org.openconcerto.sql.element.SQLElement;
32
import org.openconcerto.sql.element.SQLElement;
29
import org.openconcerto.sql.model.SQLBase;
33
import org.openconcerto.sql.model.SQLBase;
30
import org.openconcerto.sql.model.SQLRow;
34
import org.openconcerto.sql.model.SQLRow;
31
import org.openconcerto.sql.model.SQLRowAccessor;
35
import org.openconcerto.sql.model.SQLRowAccessor;
-
 
36
import org.openconcerto.sql.model.SQLTable;
32
import org.openconcerto.sql.preferences.SQLPreferences;
37
import org.openconcerto.sql.preferences.SQLPreferences;
-
 
38
import org.openconcerto.sql.view.EditFrame;
-
 
39
import org.openconcerto.sql.view.EditPanel.EditMode;
-
 
40
import org.openconcerto.ui.FrameUtil;
-
 
41
import org.openconcerto.utils.Action;
33
import org.openconcerto.utils.ExceptionHandler;
42
import org.openconcerto.utils.ExceptionHandler;
34
import org.openconcerto.utils.cc.ITransformer;
43
import org.openconcerto.utils.cc.ITransformer;
-
 
44
import org.openconcerto.utils.i18n.Grammar;
-
 
45
import org.openconcerto.utils.i18n.TranslationManager;
35
 
46
 
36
import java.awt.GraphicsEnvironment;
47
import java.awt.GraphicsEnvironment;
37
import java.awt.print.PrinterJob;
48
import java.awt.print.PrinterJob;
38
import java.io.BufferedInputStream;
49
import java.io.BufferedInputStream;
39
import java.io.File;
50
import java.io.File;
Line 47... Line 58...
47
import java.net.HttpURLConnection;
58
import java.net.HttpURLConnection;
48
import java.net.URL;
59
import java.net.URL;
49
import java.net.URLConnection;
60
import java.net.URLConnection;
50
import java.net.URLEncoder;
61
import java.net.URLEncoder;
51
import java.nio.charset.StandardCharsets;
62
import java.nio.charset.StandardCharsets;
-
 
63
import java.util.ArrayList;
52
import java.util.Arrays;
64
import java.util.Arrays;
-
 
65
import java.util.Collections;
53
import java.util.HashMap;
66
import java.util.HashMap;
54
import java.util.List;
67
import java.util.List;
55
import java.util.Map;
68
import java.util.Map;
56
import java.util.StringJoiner;
69
import java.util.StringJoiner;
57
import java.util.concurrent.Callable;
70
import java.util.concurrent.Callable;
Line 138... Line 151...
138
 
151
 
139
    public final SQLElement getElement() {
152
    public final SQLElement getElement() {
140
        return this.elt;
153
        return this.elt;
141
    }
154
    }
142
 
155
 
-
 
156
    public Future<SheetXml> showPrintAndExportAsynchronous(final boolean showDocument, final boolean printDocument, final boolean exportToPDF) {
-
 
157
        return showPrintAndExportAsynchronous(showDocument, printDocument, exportToPDF, Collections.emptyList());
-
 
158
    }
-
 
159
 
143
    /**
160
    /**
144
     * Show, print and export the document to PDF. This method is asynchronous, but is executed in a
161
     * Show, print and export the document to PDF. This method is asynchronous, but is executed in a
145
     * single threaded queue shared with createDocument
162
     * single threaded queue shared with createDocument
146
     */
163
     */
147
    public Future<SheetXml> showPrintAndExportAsynchronous(final boolean showDocument, final boolean printDocument, final boolean exportToPDF) {
164
    public Future<SheetXml> showPrintAndExportAsynchronous(final boolean showDocument, final boolean printDocument, final boolean exportToPDF, List<Action> actions) {
148
        final Callable<SheetXml> c = new Callable<SheetXml>() {
165
        final Callable<SheetXml> c = new Callable<SheetXml>() {
149
            @Override
166
            @Override
150
            public SheetXml call() throws Exception {
167
            public SheetXml call() throws Exception {
-
 
168
                showPrintAndExport(showDocument, printDocument, exportToPDF, actions);
-
 
169
                return SheetXml.this;
-
 
170
            }
-
 
171
        };
-
 
172
        return runnableQueue.submit(c);
-
 
173
 
-
 
174
    }
-
 
175
 
-
 
176
    public Future<SheetXml> showPrintAndExportAsynchronous(final boolean showDocument, final boolean printDocument, final boolean exportToPDF, final SQLElement element, SQLRow row) {
-
 
177
        final Callable<SheetXml> c = new Callable<SheetXml>() {
-
 
178
 
-
 
179
            @Override
-
 
180
            public SheetXml call() throws Exception {
-
 
181
                final List<Action> actions = new ArrayList<>();
-
 
182
                if (SheetXml.this instanceof AbstractSheetXml) {
-
 
183
                    final SQLTable table = element.getTable();
-
 
184
                    final Action emailAction = new Action(TranslationManager.getInstance().getTranslationForAction("document.pdf.send.email")) {
-
 
185
 
-
 
186
                        @Override
-
 
187
                        public void run(Object source) {
-
 
188
                            EmailTemplate.askTemplate(null, table.getDBRoot(), new ValueListener() {
-
 
189
 
-
 
190
                                @Override
-
 
191
                                public void valueSelected(Object value) {
-
 
192
                                    final MouseSheetXmlListeListener l = new MouseSheetXmlListeListener(element, ((AbstractSheetXml) SheetXml.this).getClass());
-
 
193
                                    l.sendMail((EmailTemplate) value, (AbstractSheetXml) SheetXml.this, true);
-
 
194
                                }
-
 
195
                            });
-
 
196
 
-
 
197
                        }
-
 
198
                    };
-
 
199
                    actions.add(emailAction);
-
 
200
                }
-
 
201
                final Action modifyAction = new Action(TranslationManager.getInstance().getTranslationForAction("modify") + " " + element.getName().getVariant(Grammar.DEFINITE_ARTICLE_SINGULAR)) {
-
 
202
 
-
 
203
                    @Override
-
 
204
                    public void run(Object source) {
-
 
205
                        if (source instanceof PreviewFrame) {
-
 
206
                            ((PreviewFrame) source).dispose();
-
 
207
                        }
-
 
208
 
-
 
209
                        final SQLComponent component = element.createDefaultComponent();
-
 
210
                        final EditFrame f = new EditFrame(component, EditMode.MODIFICATION);
-
 
211
                        f.selectionId(row.getID());
-
 
212
                        FrameUtil.show(f);
-
 
213
 
-
 
214
                    }
-
 
215
                };
-
 
216
                actions.add(modifyAction);
151
                showPrintAndExport(showDocument, printDocument, exportToPDF);
217
                showPrintAndExport(showDocument, printDocument, exportToPDF, actions);
152
                return SheetXml.this;
218
                return SheetXml.this;
153
            }
219
            }
154
        };
220
        };
155
        return runnableQueue.submit(c);
221
        return runnableQueue.submit(c);
156
 
222
 
Line 160... Line 226...
160
 
226
 
161
        return runnableQueue.submit(r);
227
        return runnableQueue.submit(r);
162
    }
228
    }
163
 
229
 
164
    public void showPrintAndExport(final boolean showDocument, final boolean printDocument, boolean exportToPDF) {
230
    public void showPrintAndExport(final boolean showDocument, final boolean printDocument, boolean exportToPDF) {
-
 
231
        showPrintAndExport(showDocument, printDocument, exportToPDF, Boolean.getBoolean("org.openconcerto.oo.useODSViewer"), false, Collections.emptyList());
-
 
232
    }
-
 
233
 
-
 
234
    public void showPrintAndExport(final boolean showDocument, final boolean printDocument, boolean exportToPDF, List<Action> actions) {
165
        showPrintAndExport(showDocument, printDocument, exportToPDF, Boolean.getBoolean("org.openconcerto.oo.useODSViewer"), false);
235
        showPrintAndExport(showDocument, printDocument, exportToPDF, Boolean.getBoolean("org.openconcerto.oo.useODSViewer"), false, actions);
-
 
236
    }
-
 
237
 
-
 
238
    public void showPrintAndExport(final boolean showDocument, final boolean printDocument, boolean exportToPDF, boolean useODSViewer, boolean exportPDFSynch) {
-
 
239
        showPrintAndExport(showDocument, printDocument, exportToPDF, useODSViewer, exportPDFSynch, Collections.emptyList());
166
    }
240
    }
167
 
241
 
168
    /**
242
    /**
169
     * Show, print and export the document to PDF. This method is synchronous
243
     * Show, print and export the document to PDF. This method is synchronous
170
     */
244
     */
171
    public void showPrintAndExport(final boolean showDocument, final boolean printDocument, boolean exportToPDF, boolean useODSViewer, boolean exportPDFSynch) {
245
    public void showPrintAndExport(final boolean showDocument, final boolean printDocument, boolean exportToPDF, boolean useODSViewer, boolean exportPDFSynch, List<Action> actions) {
172
 
246
 
173
        final File generatedFile = getGeneratedFile();
247
        final File generatedFile = getGeneratedFile();
174
        final File pdfFile = getGeneratedPDFFile();
248
        final File pdfFile = getGeneratedPDFFile();
175
        if (generatedFile == null || !generatedFile.exists()) {
249
        if (generatedFile == null || !generatedFile.exists()) {
176
            JOptionPane.showMessageDialog(null, "Fichier généré manquant : " + generatedFile);
250
            JOptionPane.showMessageDialog(null, "Fichier généré manquant : " + generatedFile);
Line 198... Line 272...
198
                }
272
                }
199
            } else {
273
            } else {
200
                final OpenDocument doc = new OpenDocument(generatedFile);
274
                final OpenDocument doc = new OpenDocument(generatedFile);
201
 
275
 
202
                if (showDocument) {
276
                if (showDocument) {
203
                    showPreviewDocument();
277
                    showPreviewDocument(actions);
204
                }
278
                }
205
                if (printDocument) {
279
                if (printDocument) {
206
                    // Print !
280
                    // Print !
207
                    DefaultNXDocumentPrinter printer = new DefaultNXDocumentPrinter();
281
                    DefaultNXDocumentPrinter printer = new DefaultNXDocumentPrinter();
208
                    printer.print(Arrays.asList(doc));
282
                    printer.print(Arrays.asList(doc));
Line 260... Line 334...
260
    public void createPDF(final File generatedFile, final File pdfFile, final OpenDocument doc, String storagePath) {
334
    public void createPDF(final File generatedFile, final File pdfFile, final OpenDocument doc, String storagePath) {
261
        if (pdfFile == null) {
335
        if (pdfFile == null) {
262
            throw new IllegalArgumentException("null PDF file");
336
            throw new IllegalArgumentException("null PDF file");
263
        }
337
        }
264
        try {
338
        try {
265
            if (VenteFactureXmlSheet.TEMPLATE_ID.equals(getDefaultTemplateId())) {
-
 
266
                final SQLPreferences prefs = SQLPreferences.getMemCached(getElement().getTable().getDBRoot());
-
 
267
                if (prefs.getBoolean(PayPalPreferencePanel.PAYPAL_INVOICE, false)) {
-
 
268
                    try {
-
 
269
                        final File inFile = File.createTempFile("oc_", pdfFile.getName());
-
 
270
                        SheetUtils.convert2PDF(doc, inFile);
-
 
271
                        PaypalStamper s = new PaypalStamper();
-
 
272
                        int x = prefs.getInt(PayPalPreferencePanel.PAYPAL_INVOICE_X, 0);
-
 
273
                        int y = prefs.getInt(PayPalPreferencePanel.PAYPAL_INVOICE_Y, 0);
-
 
274
 
-
 
275
                        // Reference
-
 
276
                        String ref = getSQLRow().getString("NUMERO");
-
 
277
                        // Montant : ex : 10.55
-
 
278
                        long cents = getSQLRow().getLong("NET_A_PAYER");
-
 
279
                        String amount = cents / 100 + "." + cents % 100;
-
 
280
                        // Devise
-
 
281
                        // TODO : autres devises
-
 
282
                        String currency = "EUR";
-
 
283
                        // POST
-
 
284
                        final URL url = new URL("https://cloud.openconcerto.org/payment");
-
 
285
                        final URLConnection con = url.openConnection();
-
 
286
                        final HttpURLConnection http = (HttpURLConnection) con;
-
 
287
                        http.setRequestMethod("POST");
-
 
288
                        http.setDoOutput(true);
-
 
289
                        http.setDefaultUseCaches(false);
-
 
290
 
-
 
291
                        String hyperlink = null;
-
 
292
                        // x-www-form-urlencoded
-
 
293
                        final Map<String, String> arguments = new HashMap<>();
-
 
294
                        arguments.put("pI", prefs.get(PayPalPreferencePanel.PAYPAL_CLIENTID, ""));
-
 
295
                        arguments.put("pS", prefs.get(PayPalPreferencePanel.PAYPAL_SECRET, ""));
-
 
296
                        arguments.put("ref", ref);
-
 
297
                        arguments.put("amount", amount);
-
 
298
                        arguments.put("currency", currency);
-
 
299
                        arguments.put("type", "paypal");
-
 
300
                        final StringJoiner sj = new StringJoiner("&");
-
 
301
                        for (Map.Entry<String, String> entry : arguments.entrySet()) {
-
 
302
                            sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "=" + URLEncoder.encode(entry.getValue(), "UTF-8"));
-
 
303
                        }
-
 
304
                        final String postData = sj.toString();
-
 
305
                        System.err.println("SheetXml.createPDF() " + postData);
-
 
306
                        byte[] out = postData.getBytes(StandardCharsets.UTF_8);
-
 
307
                        int length = out.length;
-
 
308
                        http.setFixedLengthStreamingMode(length);
-
 
309
                        http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
-
 
310
                        http.connect();
-
 
311
                        try (OutputStream os = http.getOutputStream()) {
-
 
312
                            os.write(out);
-
 
313
                        }
-
 
314
                        if (http.getResponseCode() != 401) {
-
 
315
 
-
 
316
                            InputStream is = http.getInputStream();
-
 
317
                            InputStreamReader isr = new InputStreamReader(is);
-
 
318
 
-
 
319
                            int numCharsRead;
-
 
320
                            char[] charArray = new char[1024];
-
 
321
                            StringBuilder sb = new StringBuilder();
-
 
322
                            while ((numCharsRead = isr.read(charArray)) > 0) {
-
 
323
                                sb.append(charArray, 0, numCharsRead);
-
 
324
                            }
-
 
325
                            //
-
 
326
                            hyperlink = sb.toString();
-
 
327
                        }
-
 
328
                        s.addLink(inFile, pdfFile, x, y, hyperlink);
-
 
329
                    } catch (Exception e) {
-
 
330
                        e.printStackTrace();
-
 
331
                        SheetUtils.convert2PDF(doc, pdfFile);
-
 
332
                    }
-
 
333
 
-
 
334
                } else {
-
 
335
                    SheetUtils.convert2PDF(doc, pdfFile);
-
 
336
                }
-
 
337
            } else {
-
 
338
                SheetUtils.convert2PDF(doc, pdfFile);
339
            SheetUtils.convert2PDF(doc, pdfFile);
339
            }
-
 
340
 
-
 
341
        } catch (Throwable e) {
340
        } catch (Exception e) {
342
            ExceptionHandler.handle("Impossible de créer le PDF " + pdfFile.getAbsolutePath(), e);
341
            ExceptionHandler.handle("Impossible de créer le PDF " + pdfFile.getAbsolutePath(), e);
343
        }
342
        }
344
        if (!pdfFile.canRead()) {
343
        if (!pdfFile.canRead()) {
345
            ExceptionHandler.handle("Le fichier PDF " + pdfFile.getAbsolutePath() + " ne peut être lu.");
344
            ExceptionHandler.handle("Le fichier PDF " + pdfFile.getAbsolutePath() + " ne peut être lu.");
346
        }
345
        }
-
 
346
        storeWithStorageEngines(generatedFile, pdfFile, storagePath);
-
 
347
    }
-
 
348
 
-
 
349
    protected void storeWithStorageEngines(final File generatedFile, final File pdfFile, String storagePath) {
347
        List<StorageEngine> engines = StorageEngines.getInstance().getActiveEngines();
350
        List<StorageEngine> engines = StorageEngines.getInstance().getActiveEngines();
348
        for (StorageEngine storageEngine : engines) {
351
        for (StorageEngine storageEngine : engines) {
349
            if (storageEngine.isConfigured() && storageEngine.allowAutoStorage()) {
352
            if (storageEngine.isConfigured() && storageEngine.allowAutoStorage()) {
350
                final String path = storagePath;
353
                final String path = storagePath;
351
                try {
354
                try {
Line 488... Line 491...
488
 
491
 
489
    public File getOrCreatePDFDocumentFile(boolean createRecent, boolean useODSViewer) throws Exception {
492
    public File getOrCreatePDFDocumentFile(boolean createRecent, boolean useODSViewer) throws Exception {
490
        File f = getGeneratedPDFFile();
493
        File f = getGeneratedPDFFile();
491
        if (!f.exists()) {
494
        if (!f.exists()) {
492
            getOrCreateDocumentFile();
495
            getOrCreateDocumentFile();
493
            showPrintAndExport(false, false, true, useODSViewer, true);
496
            showPrintAndExport(false, false, true, useODSViewer, true, Collections.emptyList());
494
            return f;
497
            return f;
495
        } else {
498
        } else {
496
            File fODS = getOrCreateDocumentFile();
499
            File fODS = getOrCreateDocumentFile();
497
            if (fODS.lastModified() > f.lastModified()) {
500
            if (fODS.lastModified() > f.lastModified()) {
498
                showPrintAndExport(false, false, true, useODSViewer, true);
501
                showPrintAndExport(false, false, true, useODSViewer, true, Collections.emptyList());
499
            }
502
            }
500
            return f;
503
            return f;
501
        }
504
        }
502
    }
505
    }
503
 
506
 
Line 545... Line 548...
545
        }
548
        }
546
 
549
 
547
    }
550
    }
548
 
551
 
549
    public void showPreviewDocument() throws Exception {
552
    public void showPreviewDocument() throws Exception {
550
        showPreviewDocument(null, null);
553
        showPreviewDocument(Collections.emptyList());
551
    }
554
    }
552
 
555
 
553
    public void showPreviewDocument(String actionName, Runnable r) throws Exception {
556
    public void showPreviewDocument(List<Action> actions) throws Exception {
554
        File f = null;
557
        File f = null;
555
        f = getOrCreateDocumentFile();
558
        f = getOrCreateDocumentFile();
556
        PreviewFrame.show(f, actionName, r);
559
        PreviewFrame.show(f, actions);
557
    }
560
    }
558
 
561
 
559
    public void printDocument() {
562
    public void printDocument() {
560
        printDocument(null);
563
        printDocument(null);
561
    }
564
    }