OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev 156 Rev 180
Line 13... Line 13...
13
 
13
 
14
 package org.openconcerto.erp.core.customerrelationship.customer.element;
14
 package org.openconcerto.erp.core.customerrelationship.customer.element;
15
 
15
 
16
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
16
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
17
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
17
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
-
 
18
import org.openconcerto.erp.config.Log;
-
 
19
 
18
import org.openconcerto.erp.core.common.ui.DeviseField;
20
import org.openconcerto.erp.core.common.ui.DeviseField;
-
 
21
import org.openconcerto.erp.core.sales.invoice.element.SaisieVenteFactureSQLElement;
-
 
22
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureXmlSheet;
-
 
23
import org.openconcerto.erp.generationDoc.A4;
-
 
24
import org.openconcerto.erp.generationDoc.ProgressPrintingFrame;
-
 
25
import org.openconcerto.erp.generationDoc.gestcomm.FicheRelanceSheet;
19
import org.openconcerto.erp.generationDoc.gestcomm.RelanceSheet;
26
import org.openconcerto.erp.generationDoc.gestcomm.RelanceSheet;
20
import org.openconcerto.erp.preferences.PrinterNXProps;
27
import org.openconcerto.erp.preferences.PrinterNXProps;
21
import org.openconcerto.sql.element.BaseSQLComponent;
28
import org.openconcerto.sql.element.BaseSQLComponent;
22
import org.openconcerto.sql.element.SQLComponent;
29
import org.openconcerto.sql.element.SQLComponent;
23
import org.openconcerto.sql.element.TreesOfSQLRows;
30
import org.openconcerto.sql.element.TreesOfSQLRows;
Line 27... Line 34...
27
import org.openconcerto.sql.model.SQLTable;
34
import org.openconcerto.sql.model.SQLTable;
28
import org.openconcerto.sql.sqlobject.ElementComboBox;
35
import org.openconcerto.sql.sqlobject.ElementComboBox;
29
import org.openconcerto.sql.sqlobject.JUniqueTextField;
36
import org.openconcerto.sql.sqlobject.JUniqueTextField;
30
import org.openconcerto.sql.view.EditFrame;
37
import org.openconcerto.sql.view.EditFrame;
31
import org.openconcerto.ui.DefaultGridBagConstraints;
38
import org.openconcerto.ui.DefaultGridBagConstraints;
-
 
39
import org.openconcerto.sql.view.list.IListe;
-
 
40
import org.openconcerto.sql.view.list.RowAction;
32
import org.openconcerto.ui.JDate;
41
import org.openconcerto.ui.JDate;
33
import org.openconcerto.ui.component.ITextArea;
42
import org.openconcerto.ui.component.ITextArea;
34
import org.openconcerto.ui.component.InteractionMode;
43
import org.openconcerto.ui.component.InteractionMode;
35
import org.openconcerto.utils.ExceptionHandler;
44
import org.openconcerto.utils.ExceptionHandler;
36
 
45
 
37
import java.awt.GridBagConstraints;
46
import java.awt.GridBagConstraints;
38
import java.awt.GridBagLayout;
47
import java.awt.GridBagLayout;
-
 
48
 
-
 
49
import java.awt.Window;
-
 
50
import java.awt.event.ActionEvent;
-
 
51
import java.awt.print.Paper;
-
 
52
import java.awt.print.PrinterException;
-
 
53
import java.awt.print.PrinterJob;
39
import java.sql.SQLException;
54
import java.sql.SQLException;
40
import java.util.ArrayList;
55
import java.util.ArrayList;
41
import java.util.Calendar;
56
import java.util.Calendar;
42
import java.util.List;
57
import java.util.List;
-
 
58
import java.util.logging.Level;
43
 
59
 
-
 
60
import javax.print.PrintService;
-
 
61
import javax.print.attribute.Attribute;
-
 
62
import javax.print.attribute.HashPrintRequestAttributeSet;
-
 
63
import javax.print.attribute.Size2DSyntax;
-
 
64
import javax.print.attribute.standard.Copies;
-
 
65
import javax.print.attribute.standard.MediaPrintableArea;
-
 
66
import javax.print.attribute.standard.MediaSizeName;
-
 
67
import javax.swing.AbstractAction;
44
import javax.swing.JCheckBox;
68
import javax.swing.JCheckBox;
45
import javax.swing.JLabel;
69
import javax.swing.JLabel;
46
import javax.swing.JPanel;
70
import javax.swing.JPanel;
47
import javax.swing.JSeparator;
71
import javax.swing.JSeparator;
48
import javax.swing.SwingUtilities;
72
import javax.swing.SwingUtilities;
-
 
73
import javax.swing.JOptionPane;
-
 
74
import javax.swing.SwingUtilities;
49
 
75
 
50
public class RelanceSQLElement extends ComptaSQLConfElement {
76
public class RelanceSQLElement extends ComptaSQLConfElement {
-
 
77
    public static final String ITEM_TYPE = "type.id";
-
 
78
    public static final String ITEM_DATE = "date";
-
 
79
    public static final String ITEM_NUMBER = "number";
-
 
80
    public static final String ITEM_CUSTOMER = "customer.id";
-
 
81
    public static final String ITEM_SALES_INVOICE_INPUT = "sales.invoice.input.id";
-
 
82
    public static final String ITEM_AMOUNT = "amount";
-
 
83
    public static final String ITEM_INFORMATIONS = "informations";
-
 
84
    public static final String ITEM_VISUALIZATION = "visualization";
-
 
85
    public static final String ITEM_PRINT = "impression";
-
 
86
    private static final double POINTS_PER_INCH = 72.0;
51
 
87
 
52
    public RelanceSQLElement() {
88
    public RelanceSQLElement() {
53
        super("RELANCE", "une relance client", "relances clients");
89
        super("RELANCE", "une relance client", "relances clients");
-
 
90
 
-
 
91
        RowAction actionShowDoc = new RowAction(new AbstractAction("Voir le document") {
-
 
92
            public void actionPerformed(ActionEvent e) {
-
 
93
                final RelanceSheet s = new RelanceSheet(IListe.get(e).getSelectedRow().asRow().fetchNew(false));
-
 
94
                s.generate(false, false, "");
-
 
95
                s.showDocument();
-
 
96
            }
-
 
97
        }, false) {
-
 
98
            @Override
-
 
99
            public boolean enabledFor(List<SQLRowValues> selection) {
-
 
100
                if (selection.size() == 1) {
-
 
101
                    SQLRowValues rowRelance = selection.get(0);
-
 
102
                    boolean isNotMail = !(rowRelance.getForeign("ID_TYPE_LETTRE_RELANCE") == null || rowRelance.isForeignEmpty("ID_TYPE_LETTRE_RELANCE"));
-
 
103
                    return isNotMail;
-
 
104
                }
-
 
105
                return false;
-
 
106
            }
-
 
107
        };
-
 
108
 
-
 
109
        getRowActions().add(actionShowDoc);
-
 
110
 
-
 
111
        RowAction actionPrintDoc = new RowAction(new AbstractAction("Imprimer") {
-
 
112
            public void actionPerformed(ActionEvent e) {
-
 
113
                print(e, false);
-
 
114
            }
-
 
115
        }, false) {
-
 
116
            @Override
-
 
117
            public boolean enabledFor(List<SQLRowValues> selection) {
-
 
118
                if (selection.size() >= 1) {
-
 
119
                    boolean isNotMail = true;
-
 
120
                    for (SQLRowValues rowRelance : selection) {
-
 
121
 
-
 
122
                        isNotMail &= !(rowRelance.getForeign("ID_TYPE_LETTRE_RELANCE") == null || rowRelance.isForeignEmpty("ID_TYPE_LETTRE_RELANCE"));
-
 
123
                    }
-
 
124
                    return isNotMail;
-
 
125
                }
-
 
126
                return false;
-
 
127
            }
-
 
128
        };
-
 
129
 
-
 
130
        getRowActions().add(actionPrintDoc);
-
 
131
 
-
 
132
        // Impression
-
 
133
 
-
 
134
        RowAction actionPrintDocFact = new RowAction(new AbstractAction("Imprimer avec la facture") {
-
 
135
            public void actionPerformed(ActionEvent e) {
-
 
136
                print(e, true);
-
 
137
            }
-
 
138
        }, false) {
-
 
139
            @Override
-
 
140
            public boolean enabledFor(List<SQLRowValues> selection) {
-
 
141
                if (selection.size() >= 1) {
-
 
142
                    boolean isNotMail = true;
-
 
143
                    for (SQLRowValues rowRelance : selection) {
-
 
144
 
-
 
145
                        isNotMail &= !(rowRelance.getForeign("ID_TYPE_LETTRE_RELANCE") == null || rowRelance.isForeignEmpty("ID_TYPE_LETTRE_RELANCE"));
-
 
146
                    }
-
 
147
                    return isNotMail;
-
 
148
                }
-
 
149
                return false;
-
 
150
            }
-
 
151
        };
-
 
152
 
-
 
153
        getRowActions().add(actionPrintDocFact);
-
 
154
 
-
 
155
        // Générer
-
 
156
 
-
 
157
        RowAction actionGen = new RowAction(new AbstractAction("Générer le document") {
-
 
158
            public void actionPerformed(ActionEvent e) {
-
 
159
 
-
 
160
                for (SQLRowValues rowVals : IListe.get(e).getSelectedRows()) {
-
 
161
                    final RelanceSheet s = new RelanceSheet(rowVals.asRow().fetchNew(false));
-
 
162
                    String printer = PrinterNXProps.getInstance().getStringProperty("RelancePrinter");
-
 
163
                    s.generate(false, true, printer, true);
-
 
164
                    s.showDocument();
-
 
165
                }
-
 
166
            }
-
 
167
        }, false) {
-
 
168
            @Override
-
 
169
            public boolean enabledFor(List<SQLRowValues> selection) {
-
 
170
                if (selection.size() >= 1) {
-
 
171
                    SQLRowValues rowRelance = selection.get(0);
-
 
172
                    boolean isNotMail = !(rowRelance.getForeign("ID_TYPE_LETTRE_RELANCE") == null || rowRelance.isForeignEmpty("ID_TYPE_LETTRE_RELANCE"));
-
 
173
                    return isNotMail;
-
 
174
                }
-
 
175
                return false;
-
 
176
            }
-
 
177
        };
-
 
178
 
-
 
179
        getRowActions().add(actionGen);
-
 
180
 
-
 
181
        RowAction actionFiche = new RowAction(new AbstractAction("Créer la fiche de relance") {
-
 
182
            public void actionPerformed(ActionEvent e) {
-
 
183
                try {
-
 
184
                    FicheRelanceSheet sheet = new FicheRelanceSheet(IListe.get(e).getSelectedRow().asRow().fetchNew(false));
-
 
185
                    sheet.createDocumentAsynchronous();
-
 
186
                    sheet.showPrintAndExportAsynchronous(true, false, true);
-
 
187
                } catch (Exception ex) {
-
 
188
                    ExceptionHandler.handle("Impression impossible", ex);
-
 
189
                }
-
 
190
            }
-
 
191
        }, false) {
-
 
192
            @Override
-
 
193
            public boolean enabledFor(List<SQLRowValues> selection) {
-
 
194
                return (selection.size() == 1);
-
 
195
            }
-
 
196
        };
-
 
197
 
-
 
198
        getRowActions().add(actionFiche);
-
 
199
 
54
    }
200
    }
55
 
201
 
56
    protected List<String> getListFields() {
202
    protected List<String> getListFields() {
57
        final List<String> l = new ArrayList<String>();
203
        final List<String> l = new ArrayList<String>();
58
        l.add("NUMERO");
204
        l.add("NUMERO");
Line 300... Line 446...
300
 
446
 
301
    @Override
447
    @Override
302
    protected String createCode() {
448
    protected String createCode() {
303
        return this.createCodeOfPackage() + ".chaseletter";
449
        return this.createCodeOfPackage() + ".chaseletter";
304
    }
450
    }
-
 
451
 
-
 
452
    public void print(final ActionEvent ev, boolean withInvoice) {
-
 
453
        //
-
 
454
        final IListe ilist = IListe.get(ev);
-
 
455
        String printerName = PrinterNXProps.getInstance().getStringProperty("RelancePrinter");
-
 
456
        // Printer configuration
-
 
457
        final PrinterJob printJob = PrinterJob.getPrinterJob();
-
 
458
 
-
 
459
        // Set the printer
-
 
460
        PrintService myService = null;
-
 
461
        if (printerName != null && printerName.trim().length() > 0) {
-
 
462
            final PrintService[] services = PrinterJob.lookupPrintServices();
-
 
463
            for (int i = 0; i < services.length; i++) {
-
 
464
                if (services[i].getName().equals(printerName)) {
-
 
465
                    myService = services[i];
-
 
466
                    break;
-
 
467
                }
-
 
468
            }
-
 
469
            if (myService != null) {
-
 
470
                try {
-
 
471
                    printJob.setPrintService(myService);
-
 
472
                } catch (PrinterException e) {
-
 
473
                    Log.get().log(Level.SEVERE, "cannot print", e);
-
 
474
                    JOptionPane.showMessageDialog(null, "Imprimante non compatible");
-
 
475
                    return;
-
 
476
                }
-
 
477
            }
-
 
478
        }
-
 
479
 
-
 
480
        final HashPrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
-
 
481
        // L'impression est forcée en A4, sur OpenSuse le format est en
-
 
482
        // Letter par défaut alors que l'imprimante est en A4 dans le système
-
 
483
        final MediaSizeName media = MediaSizeName.ISO_A4;
-
 
484
        attributes.add(media);
-
 
485
        Paper paper = new A4(0, 0);
-
 
486
 
-
 
487
        final MediaPrintableArea printableArea = new MediaPrintableArea((float) (paper.getImageableX() / POINTS_PER_INCH), (float) (paper.getImageableY() / POINTS_PER_INCH),
-
 
488
                (float) (paper.getImageableWidth() / POINTS_PER_INCH), (float) (paper.getImageableHeight() / POINTS_PER_INCH), Size2DSyntax.INCH);
-
 
489
        attributes.add(printableArea);
-
 
490
        attributes.add(new Copies(1));
-
 
491
 
-
 
492
        boolean okToPrint = printJob.printDialog(attributes);
-
 
493
        final Attribute attribute = attributes.get(Copies.class);
-
 
494
        if (attribute != null) {
-
 
495
            final Copies attributeCopies = (Copies) attribute;
-
 
496
            final int value = attributeCopies.getValue();
-
 
497
            printJob.setCopies(value);
-
 
498
        } else {
-
 
499
            printJob.setCopies(1);
-
 
500
        }
-
 
501
 
-
 
502
        if (okToPrint) {
-
 
503
            Window w = SwingUtilities.getWindowAncestor(ilist);
-
 
504
            final ProgressPrintingFrame pFrame = new ProgressPrintingFrame(w, printJob, "Impression", "Impression en cours", 300);
-
 
505
            // Génération + impression
-
 
506
            final List<SQLRowValues> rows = IListe.get(ev).getSelectedRows();
-
 
507
            final Thread thread = new Thread() {
-
 
508
                @Override
-
 
509
                public void run() {
-
 
510
                    final int size = rows.size();
-
 
511
                    for (int i = 0; i < size; i++) {
-
 
512
                        final int index = i;
-
 
513
                        SwingUtilities.invokeLater(new Runnable() {
-
 
514
 
-
 
515
                            @Override
-
 
516
                            public void run() {
-
 
517
                                pFrame.setMessage("Document " + (index + 1) + "/" + size);
-
 
518
                                pFrame.setProgress((100 * (index + 1)) / size);
-
 
519
                            }
-
 
520
                        });
-
 
521
                        if (!pFrame.isCancelled()) {
-
 
522
                            SQLRowValues r = rows.get(i);
-
 
523
                            SQLRow rowRelance = r.asRow().fetchNew(false);
-
 
524
                            RelanceSheet s = new RelanceSheet(rowRelance);
-
 
525
                            s.printDocument(printJob);
-
 
526
                            if (withInvoice) {
-
 
527
                                SaisieVenteFactureSQLElement element = getDirectory().getElement(SaisieVenteFactureSQLElement.class);
-
 
528
                                final VenteFactureXmlSheet sheet = new VenteFactureXmlSheet(rowRelance.getForeignRow("ID_SAISIE_VENTE_FACTURE"));
-
 
529
                                try {
-
 
530
                                    sheet.getOrCreateDocumentFile();
-
 
531
                                    sheet.printDocument(printJob);
-
 
532
                                } catch (Exception e) {
-
 
533
                                    ExceptionHandler.handle("Erreur lors de la création de la facture", e);
-
 
534
                                }
-
 
535
                            }
-
 
536
 
-
 
537
                        }
-
 
538
                    }
-
 
539
                }
-
 
540
            };
-
 
541
            thread.setPriority(Thread.MIN_PRIORITY);
-
 
542
            thread.setDaemon(true);
-
 
543
            pFrame.setLocationRelativeTo(ilist);
-
 
544
            pFrame.setVisible(true);
-
 
545
            thread.start();
-
 
546
 
-
 
547
        }
-
 
548
 
-
 
549
    }
305
}
550
}