OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.erp.core.sales.invoice.report;

import org.openconcerto.erp.config.ComptaPropsConfiguration;
import org.openconcerto.erp.core.customerrelationship.mail.EmailTemplate;
import org.openconcerto.erp.core.finance.payment.element.ModeDeReglementSQLElement;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.users.UserManager;
import org.openconcerto.utils.GestionDevise;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MailRelanceCreator {

    private final EmailTemplate template;
    private final SQLRow rowEcheance;
    private final Map<String, String> map;

    public MailRelanceCreator(EmailTemplate template, SQLRow row) {
        if (template == null) {
            template = new EmailTemplate("defautl", getDefaultObject(), getDefaultValue(), true, "dd/MM/yyyy");
        }
        this.template = template;
        this.rowEcheance = row;

        this.map = getMapValues();
    }

    public String getDefaultObject() {
        return "Relance {FactureNumero}";
    }

    public String getDefaultValue() {
        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."
                + "\nCe montant correspond à la facture {FactureNumero} datée du {FactureDate} qui a pour échéance le {FactureDateEcheance}."
                + "\nNous présumons qu'il s'agit d'un simple oubli de votre part.\n\n"
                + "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"
                + "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.";

        return value;
    }

    private Map<String, String> getMapValues() {
        String datePattern = this.template.getDateFormat();
        if (datePattern == null || datePattern.trim().isEmpty()) {
            datePattern = "dd/MM/yyyy";
        }

        final Map<String, String> map = new HashMap<>();

        final SQLRow rowSoc = ((ComptaPropsConfiguration) Configuration.getInstance()).getRowSociete();
        final SQLRow rowSocAdresse = rowSoc.getForeignRow("ID_ADRESSE_COMMON");
        SQLRow rowUser = rowSoc.getTable().getDBRoot().findTable("USER_COMMON").getRow(UserManager.getUser().getId());
        map.put("UserName", rowUser.getString("NOM"));
        map.put("UserFirstName", rowUser.getString("PRENOM"));
        if (rowUser.getTable().contains("MAIL")) {
            map.put("UserMail", rowUser.getString("MAIL"));
        }
        if (rowUser.getTable().contains("TEL")) {
            map.put("UserTel", rowUser.getString("TEL"));
        }
        // Infos societe
        map.put("SocieteType", rowSoc.getString("TYPE"));
        map.put("SocieteNom", rowSoc.getString("NOM"));
        map.put("SocieteAdresse", rowSocAdresse.getString("RUE"));
        map.put("SocieteCodePostal", rowSocAdresse.getString("CODE_POSTAL"));

        String ville = rowSocAdresse.getString("VILLE");
        final Object cedex = rowSocAdresse.getObject("CEDEX");
        final boolean hasCedex = rowSocAdresse.getBoolean("HAS_CEDEX");

        if (hasCedex) {
            ville += " CEDEX";
            if (cedex != null && cedex.toString().trim().length() > 0) {
                ville += " " + cedex.toString().trim();
            }
        }

        map.put("SocieteVille", ville);

        SQLRow rowClient;
        final SQLRow clientRowNX = this.rowEcheance.getForeignRow("ID_CLIENT");
            rowClient = clientRowNX;
        SQLRow rowAdresse = rowClient.getForeignRow("ID_ADRESSE");
        if (!clientRowNX.isForeignEmpty("ID_ADRESSE_F")) {
            rowAdresse = clientRowNX.getForeign("ID_ADRESSE_F");
        }
        // Client compte
        SQLRow rowCompteClient = clientRowNX.getForeignRow("ID_COMPTE_PCE");
        String numero = rowCompteClient.getString("NUMERO");
        map.put("ClientNumeroCompte", numero);

        // Infos Client
        map.put("ClientType", rowClient.getString("FORME_JURIDIQUE"));
        map.put("ClientNom", rowClient.getString("NOM"));
        map.put("ClientAdresse", rowAdresse.getString("RUE"));
        map.put("ClientCodePostal", rowAdresse.getString("CODE_POSTAL"));
        String villeCli = rowAdresse.getString("VILLE");
        final Object cedexCli = rowAdresse.getObject("CEDEX");
        final boolean hasCedexCli = rowAdresse.getBoolean("HAS_CEDEX");

        if (hasCedexCli) {
            villeCli += " CEDEX";
            if (cedexCli != null && cedexCli.toString().trim().length() > 0) {
                villeCli += " " + cedexCli.toString().trim();
            }
        }

        map.put("ClientVille", villeCli);

        // Date relance
        Date d = new Date();
        DateFormat dateFormat = new SimpleDateFormat(datePattern);
        map.put("RelanceDate", dateFormat.format(d));

        SQLRow rowFacture = this.rowEcheance.getForeignRow("ID_SAISIE_VENTE_FACTURE");


        // Infos facture
        Long lTotal = (Long) rowFacture.getObject("T_TTC");
        Long lRestant = (Long) this.rowEcheance.getObject("MONTANT");
        Long lVerse = Long.valueOf(lTotal.longValue() - lRestant.longValue());
        map.put("FactureNumero", rowFacture.getString("NUMERO"));
        map.put("FactureReference", rowFacture.getString("NOM"));
        map.put("FactureTotal", GestionDevise.currencyToString(lTotal.longValue(), true));
        map.put("FactureRestant", GestionDevise.currencyToString(lRestant.longValue(), true));
        map.put("FactureVerse", GestionDevise.currencyToString(lVerse.longValue(), true));
        map.put("FactureDate", dateFormat.format((Date) rowFacture.getObject("DATE")));

        Date dFacture = (Date) rowFacture.getObject("DATE");
        SQLRow modeRegRow = rowFacture.getForeignRow("ID_MODE_REGLEMENT");
        Date dateEch = ModeDeReglementSQLElement.calculDate(modeRegRow.getInt("AJOURS"), modeRegRow.getInt("LENJOUR"), dFacture);
        map.put("FactureDateEcheance", dateFormat.format(dateEch));
        map.put("message", "");
        return map;
    }

    public String getObject() {
        return fill(this.template.getTitle());
    }

    public String getValue() {
        return fill(this.template.getText());
    }

    private String fill(String string) {
        String result = string;
        for (String key : this.map.keySet()) {
            result = result.replace("{" + key + "}", this.map.get(key));
        }
        return result;
    }

}