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 | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
132 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.invoice.report;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.finance.payment.element.ModeDeReglementSQLElement;
18
import org.openconcerto.erp.preferences.MailRelancePreferencePanel;
19
import org.openconcerto.sql.Configuration;
20
import org.openconcerto.sql.model.SQLRow;
21
import org.openconcerto.sql.preferences.SQLPreferences;
22
import org.openconcerto.sql.users.UserManager;
23
import org.openconcerto.utils.GestionDevise;
24
 
25
import java.text.DateFormat;
26
import java.text.SimpleDateFormat;
27
import java.util.Date;
28
import java.util.HashMap;
29
import java.util.Map;
30
 
31
public class MailRelanceCreator {
32
 
33
    SQLRow rowEcheance;
34
 
35
    public MailRelanceCreator() {
36
 
37
    }
38
 
39
    public String getDefaultObject() {
40
        return "Relance {FactureNumero}";
41
    }
42
 
43
    public String getDefaultValue() {
44
        String value = "Bonjour,\n\nSauf erreur de notre part, votre compte laisse apparaître dans nos livres un montant de {FactureRestant}€ non réglé à ce jour."
45
                + "\nCe montant correspond à la facture {FactureNumero} datée du {FactureDate} qui a pour échéance le {FactureDateEcheance}."
46
                + "\nNous présumons qu'il s'agit d'un simple oubli de votre part.\n\n"
47
                + "Toutefois, si le paiement avait été effectué, nous vous serions très obligés de nous en communiquer la date et le mode de règlement.\n\n"
48
                + "Dans l'attente d’un prompt règlement,\n\n" + "Nous vous prions d\'agréer, Madame, Monsieur, l\'expression de nos sentiments distingués.";
49
 
50
        return value;
51
    }
52
 
53
    public Map<String, String> getMapValues(SQLRow rowEch, String datePattern) {
54
        final Map<String, String> map = new HashMap<String, String>();
55
 
56
        final SQLRow rowSoc = ((ComptaPropsConfiguration) Configuration.getInstance()).getRowSociete();
57
        final SQLRow rowSocAdresse = rowSoc.getForeignRow("ID_ADRESSE_COMMON");
58
        SQLRow rowUser = rowSoc.getTable().getDBRoot().findTable("USER_COMMON").getRow(UserManager.getUser().getId());
59
        map.put("UserName", rowUser.getString("NOM"));
60
        map.put("UserFirstName", rowUser.getString("PRENOM"));
61
        if (rowUser.getTable().contains("MAIL")) {
62
            map.put("UserMail", rowUser.getString("MAIL"));
63
        }
64
        if (rowUser.getTable().contains("TEL")) {
65
            map.put("UserTel", rowUser.getString("TEL"));
66
        }
67
        // Infos societe
68
        map.put("SocieteType", rowSoc.getString("TYPE"));
69
        map.put("SocieteNom", rowSoc.getString("NOM"));
70
        map.put("SocieteAdresse", rowSocAdresse.getString("RUE"));
71
        map.put("SocieteCodePostal", rowSocAdresse.getString("CODE_POSTAL"));
72
 
73
        String ville = rowSocAdresse.getString("VILLE");
74
        final Object cedex = rowSocAdresse.getObject("CEDEX");
75
        final boolean hasCedex = rowSocAdresse.getBoolean("HAS_CEDEX");
76
 
77
        if (hasCedex) {
78
            ville += " CEDEX";
79
            if (cedex != null && cedex.toString().trim().length() > 0) {
80
                ville += " " + cedex.toString().trim();
81
            }
82
        }
83
 
84
        map.put("SocieteVille", ville);
85
 
86
        SQLRow rowClient;
87
        final SQLRow clientRowNX = rowEch.getForeignRow("ID_CLIENT");
88
            rowClient = clientRowNX;
89
        SQLRow rowAdresse = rowClient.getForeignRow("ID_ADRESSE");
90
        if (!clientRowNX.isForeignEmpty("ID_ADRESSE_F")) {
91
            rowAdresse = clientRowNX.getForeign("ID_ADRESSE_F");
92
        }
93
        // Client compte
94
        SQLRow rowCompteClient = clientRowNX.getForeignRow("ID_COMPTE_PCE");
95
        String numero = rowCompteClient.getString("NUMERO");
96
        map.put("ClientNumeroCompte", numero);
97
 
98
        // Infos Client
99
        map.put("ClientType", rowClient.getString("FORME_JURIDIQUE"));
100
        map.put("ClientNom", rowClient.getString("NOM"));
101
        map.put("ClientAdresse", rowAdresse.getString("RUE"));
102
        map.put("ClientCodePostal", rowAdresse.getString("CODE_POSTAL"));
103
        String villeCli = rowAdresse.getString("VILLE");
104
        final Object cedexCli = rowAdresse.getObject("CEDEX");
105
        final boolean hasCedexCli = rowAdresse.getBoolean("HAS_CEDEX");
106
 
107
        if (hasCedexCli) {
108
            villeCli += " CEDEX";
109
            if (cedexCli != null && cedexCli.toString().trim().length() > 0) {
110
                villeCli += " " + cedexCli.toString().trim();
111
            }
112
        }
113
 
114
        map.put("ClientVille", villeCli);
115
 
116
        // Date relance
117
        Date d = new Date();
118
        DateFormat dateFormat = new SimpleDateFormat(datePattern);
119
        map.put("RelanceDate", dateFormat.format(d));
120
 
121
        SQLRow rowFacture = rowEch.getForeignRow("ID_SAISIE_VENTE_FACTURE");
122
 
123
 
124
        // Infos facture
125
        Long lTotal = (Long) rowFacture.getObject("T_TTC");
126
        Long lRestant = (Long) rowEch.getObject("MONTANT");
127
        Long lVerse = new Long(lTotal.longValue() - lRestant.longValue());
128
        map.put("FactureNumero", rowFacture.getString("NUMERO"));
129
        map.put("FactureReference", rowFacture.getString("NOM"));
130
        map.put("FactureTotal", GestionDevise.currencyToString(lTotal.longValue(), true));
131
        map.put("FactureRestant", GestionDevise.currencyToString(lRestant.longValue(), true));
132
        map.put("FactureVerse", GestionDevise.currencyToString(lVerse.longValue(), true));
133
        map.put("FactureDate", dateFormat.format((Date) rowFacture.getObject("DATE")));
134
 
135
        Date dFacture = (Date) rowFacture.getObject("DATE");
136
        SQLRow modeRegRow = rowFacture.getForeignRow("ID_MODE_REGLEMENT");
137
        Date dateEch = ModeDeReglementSQLElement.calculDate(modeRegRow.getInt("AJOURS"), modeRegRow.getInt("LENJOUR"), dFacture);
138
        map.put("FactureDateEcheance", dateFormat.format(dateEch));
139
 
140
        return map;
141
    }
142
 
143
    public String getObject(SQLRow rowEch) {
144
        SQLPreferences prefs = new SQLPreferences(rowEch.getTable().getDBRoot());
145
        String object = prefs.get(MailRelancePreferencePanel.MAIL_RELANCE_OBJET, getDefaultObject());
146
        String date = prefs.get(MailRelancePreferencePanel.MAIL_RELANCE_DATE_PATTERN, "dd/MM/yyyy");
147
        return fill(rowEch, date, object);
148
    }
149
 
150
    public String getValue(SQLRow rowEch) {
151
        SQLPreferences prefs = new SQLPreferences(rowEch.getTable().getDBRoot());
152
        String value = prefs.get(MailRelancePreferencePanel.MAIL_RELANCE, getDefaultValue());
153
        String date = prefs.get(MailRelancePreferencePanel.MAIL_RELANCE_DATE_PATTERN, "dd/MM/yyyy");
154
        return fill(rowEch, date, value);
155
    }
156
 
157
    private String fill(SQLRow rowEch, String datePattern, String string) {
158
 
159
        Map<String, String> map = getMapValues(rowEch, datePattern);
160
        String result = string;
161
        for (String key : map.keySet()) {
162
            result = result.replace("{" + key + "}", map.get(key));
163
        }
164
        return result;
165
    }
166
 
167
}