OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | 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.generationDoc.provider;

import org.openconcerto.erp.generationDoc.SpreadSheetCellValueContext;
import org.openconcerto.erp.generationDoc.SpreadSheetCellValueProviderManager;
import org.openconcerto.sql.model.SQLRowAccessor;

public class AdresseFullClientValueProvider extends AdresseClientProvider {

    private final int type;
    private final boolean withName;
    private final boolean noCountry;

    public AdresseFullClientValueProvider(int type, boolean withName, boolean noCountry) {
        this.type = type;
        this.withName = withName;
        this.noCountry = noCountry;
    }

    @Override
    public Object getValue(SpreadSheetCellValueContext context) {
        final SQLRowAccessor r = getAdresse(context.getRow(), this.type);
        String result = "";
        if (this.withName) {
            result = context.getRow().getForeign("ID_CLIENT").getString("NOM") + "\n\n";
        }
        result = getFormattedAddress(r, result, !this.noCountry);

        return result;
    }

    public static String getFormattedAddress(final SQLRowAccessor rAddress, String prefix) {
        return getFormattedAddress(rAddress, prefix, true);
    }

    public static String getFormattedAddress(final SQLRowAccessor rAddress, String prefix, boolean withCountry) {
        String result = prefix;
        boolean notEmptyLib = getStringTrimmed(rAddress, "LIBELLE").length() > 0;
        if (notEmptyLib) {
            result = getStringTrimmed(rAddress, "LIBELLE") + "\n";
        }
        boolean notEmptyDest = getStringTrimmed(rAddress, "DEST").length() > 0;
        if (notEmptyDest) {
            result = getStringTrimmed(rAddress, "DEST") + "\n";
        }
        boolean notEmptyRue = getStringTrimmed(rAddress, "RUE").length() > 0;
        if (notEmptyRue) {
            result += getStringTrimmed(rAddress, "RUE") + "\n";
        }
        result += notEmptyDest && notEmptyLib && notEmptyLib ? ("\n" + getStringTrimmed(rAddress, "CODE_POSTAL")) : getStringTrimmed(rAddress, "CODE_POSTAL");
        result += " ";
        if (rAddress.getTable().contains("DISTRICT")) {
            result += getStringTrimmed(rAddress, "DISTRICT") + " ";
        }
        result += getStringTrimmed(rAddress, "VILLE");
        if (rAddress.getBoolean("HAS_CEDEX")) {
            result += " Cedex";
            String cedex = getStringTrimmed(rAddress, "CEDEX");
            if (cedex.length() > 0) {
                result += " " + cedex;
            }
        }
        if (rAddress.getTable().contains("PROVINCE")) {
            String province = getStringTrimmed(rAddress, "PROVINCE");
            boolean department = rAddress.getTable().contains("DEPARTEMENT");
            if (province.length() > 0 || (department && getStringTrimmed(rAddress, "DEPARTEMENT").length() > 0)) {
                result += "\n";
            }
            if (province.length() > 0) {

                result += getStringTrimmed(rAddress, ("PROVINCE")) + " ";
            }

            if (department && getStringTrimmed(rAddress, "DEPARTEMENT").length() > 0) {

                result += getStringTrimmed(rAddress, "DEPARTEMENT");
            }
        }

        if (withCountry && getStringTrimmed(rAddress, "PAYS").length() > 0) {
            result += "\n" + getStringTrimmed(rAddress, "PAYS");
        }
        return result;
    }

    public static String getStringTrimmed(SQLRowAccessor r, String field) {
        String result = r.getString(field);
        if (result == null)
            return "";
        return result.trim();
    }

    public static void register() {
        SpreadSheetCellValueProviderManager.put("address.customer.full", new AdresseFullClientValueProvider(ADRESSE_PRINCIPALE, false, false));
        SpreadSheetCellValueProviderManager.put("address.customer.invoice.full", new AdresseFullClientValueProvider(ADRESSE_FACTURATION, false, false));
        SpreadSheetCellValueProviderManager.put("address.customer.shipment.full", new AdresseFullClientValueProvider(ADRESSE_LIVRAISON, false, false));
        SpreadSheetCellValueProviderManager.put("address.customer.full.nocountry", new AdresseFullClientValueProvider(ADRESSE_PRINCIPALE, false, true));
        SpreadSheetCellValueProviderManager.put("address.customer.invoice.full.nocountry", new AdresseFullClientValueProvider(ADRESSE_FACTURATION, false, true));
        SpreadSheetCellValueProviderManager.put("address.customer.shipment.full.nocountry", new AdresseFullClientValueProvider(ADRESSE_LIVRAISON, false, true));
        SpreadSheetCellValueProviderManager.put("address.customer.full.withname", new AdresseFullClientValueProvider(ADRESSE_PRINCIPALE, true, false));
        SpreadSheetCellValueProviderManager.put("address.customer.invoice.full.withname", new AdresseFullClientValueProvider(ADRESSE_FACTURATION, true, false));
        SpreadSheetCellValueProviderManager.put("address.customer.shipment.full.withname", new AdresseFullClientValueProvider(ADRESSE_LIVRAISON, true, false));
        SpreadSheetCellValueProviderManager.put("address.customer.full.withname.nocountry", new AdresseFullClientValueProvider(ADRESSE_PRINCIPALE, true, true));
        SpreadSheetCellValueProviderManager.put("address.customer.invoice.full.withname.nocountry", new AdresseFullClientValueProvider(ADRESSE_FACTURATION, true, true));
        SpreadSheetCellValueProviderManager.put("address.customer.shipment.full.withname.nocountry", new AdresseFullClientValueProvider(ADRESSE_LIVRAISON, true, true));
    }
}