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 | 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.humanresources.payroll.report;

import org.openconcerto.erp.core.common.element.StyleSQLElement;
import org.openconcerto.erp.generationDoc.AbstractSheetXMLWithDate;
import org.openconcerto.erp.preferences.PayeGlobalPreferencePanel;
import org.openconcerto.erp.preferences.PrinterNXProps;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowListRSH;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.preferences.SQLPreferences;
import org.openconcerto.utils.cc.ITransformer;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class FichePayeSheetXML extends AbstractSheetXMLWithDate {

    public static final String TEMPLATE_ID = "FichePayeSimplifiee";
    public static final String TEMPLATE_PROPERTY_NAME = "LocationFichePaye";
    final Map<Integer, String> cotisationSimplifieeLink = new HashMap<>();

    public FichePayeSheetXML(final SQLRow row) {
        this(row, !new SQLPreferences(row.getTable().getDBRoot()).getBoolean(PayeGlobalPreferencePanel.NOT_PAYE_SIMPL, Boolean.FALSE));
    }

    public FichePayeSheetXML(final SQLRow row, boolean simplifie) {
        super(row);
        this.printer = PrinterNXProps.getInstance().getStringProperty("FichePayePrinter");
        this.elt = Configuration.getInstance().getDirectory().getElement("FICHE_PAYE");

        if (simplifie) {

            SQLSelect sel = new SQLSelect();
            sel.addSelect(row.getTable().getDBRoot().findTable("RUBRIQUE_COTISATION").getField("LIGNE_PAYE_SIMPLIFIEE"));
            sel.addSelect(row.getTable().getDBRoot().findTable("RUBRIQUE_COTISATION").getKey());
            List<SQLRow> rubCotis = SQLRowListRSH.execute(sel);

            for (SQLRow sqlRow : rubCotis) {
                cotisationSimplifieeLink.put(sqlRow.getID(), sqlRow.getString("LIGNE_PAYE_SIMPLIFIEE"));
            }

            final String codeStatu = row.getForeign("ID_SALARIE").getForeign("ID_INFOS_SALARIE_PAYE").getForeign("ID_CONTRAT_SALARIE").getForeign("ID_CODE_STATUT_CATEGORIEL").getString("CODE");
            final boolean cadre = codeStatu.equals("01") || codeStatu.equals("02");

            final Set<Integer> ignoredRubriqueComm = new HashSet<Integer>();
            // Total nat
            ignoredRubriqueComm.add(2);
            // Total brut
            // ignoredRubriqueComm.add(3);
            // Ligne vide
            ignoredRubriqueComm.add(7);
            // Cotisations sal
            ignoredRubriqueComm.add(4);
            // Cotisation pat
            ignoredRubriqueComm.add(5);

            this.setPostProcess(new ITransformer<List<SQLRowAccessor>, List<SQLRowAccessor>>() {

                @Override
                public List<SQLRowAccessor> transformChecked(List<SQLRowAccessor> input) {

                    List<SQLRowAccessor> result = new ArrayList<SQLRowAccessor>(input.size());
                    List<SQLRowAccessor> resultCom = new ArrayList<SQLRowAccessor>(input.size());
                    List<SQLRowAccessor> resultNet = new ArrayList<SQLRowAccessor>(input.size());

                    List<SQLRowAccessor> resultCotis = new ArrayList<SQLRowAccessor>(input.size());
                    Map<String, SQLRowValues> mapLigneSimplifiee = initCotisations(cadre, resultCotis);
                    for (SQLRowAccessor sqlRowAccessor : input) {
                        if (sqlRowAccessor.getBoolean("IN_PERIODE")) {
                            if (sqlRowAccessor.getString("SOURCE").equals("RUBRIQUE_BRUT")) {
                                if (result.size() == 0) {
                                    SQLRowValues rowValsTitle = new SQLRowValues(sqlRowAccessor.getTable());
                                    rowValsTitle.put("ID_STYLE", eltStyle.getAllStyleByName().get("Titre 1"));
                                    rowValsTitle.put("NOM", "Rubrique de brut");
                                    rowValsTitle.put("IMPRESSION", Boolean.TRUE);
                                    rowValsTitle.put("IN_PERIODE", Boolean.TRUE);
                                    result.add(rowValsTitle);
                                }
                                SQLRowValues rowValsToAdd = sqlRowAccessor.asRowValues();
                                rowValsToAdd.put("ID_STYLE", 2);
                                result.add(rowValsToAdd);
                            }
                            if (sqlRowAccessor.getString("SOURCE").equals("RUBRIQUE_NET") && sqlRowAccessor.getBoolean("IMPRESSION") && sqlRowAccessor.getBoolean("IN_PERIODE")) {
                                if (resultNet.size() == 0) {
                                    SQLRowValues rowValsTitle = new SQLRowValues(sqlRowAccessor.getTable());
                                    rowValsTitle.put("ID_STYLE", eltStyle.getAllStyleByName().get("Titre 1"));
                                    rowValsTitle.put("NOM", "Rubrique de net");
                                    rowValsTitle.put("IMPRESSION", Boolean.TRUE);
                                    rowValsTitle.put("IN_PERIODE", Boolean.TRUE);
                                    resultNet.add(rowValsTitle);
                                }
                                SQLRowValues rowValsToAdd = sqlRowAccessor.asRowValues();
                                rowValsToAdd.put("ID_STYLE", 2);
                                resultNet.add(rowValsToAdd);
                            }

                            if (sqlRowAccessor.getString("SOURCE").equals("RUBRIQUE_COMM") && !ignoredRubriqueComm.contains(sqlRowAccessor.getInt("IDSOURCE"))) {
                                if (resultNet.size() == 0) {
                                    result.add(sqlRowAccessor);
                                } else {
                                    resultNet.add(sqlRowAccessor);
                                }

                            }
                            if (sqlRowAccessor.getString("SOURCE").equals("RUBRIQUE_COTISATION")) {
                                cumuls(sqlRowAccessor, mapLigneSimplifiee, cadre);
                            }
                        }
                    }
                    result.addAll(resultNet);
                    result.addAll(resultCom);
                    result.addAll(resultCotis);
                    return result;
                }
            });
        }
    }

    private void cumuls(final SQLRowAccessor sqlRowAccessor, Map<String, SQLRowValues> mapLigneSimplifiee, final boolean cadre) {
        // "TAUX_PAT" numeric(16,6) DEFAULT 0,
        // "TAUX_SAL" numeric(16,6) DEFAULT 0,
        // "NB_BASE" numeric(16,6) DEFAULT 0,
        // "MONTANT_SAL_AJ" numeric(16,2) DEFAULT 0,
        // "MONTANT_PAT" numeric(16,2) DEFAULT 0,
        // "MONTANT_SAL_DED" numeric(16,2) DEFAULT 0,
        String idLigneSimplifiee = this.cotisationSimplifieeLink.get(sqlRowAccessor.getInt("IDSOURCE"));
        if (idLigneSimplifiee == null || idLigneSimplifiee.trim().length() == 0) {
            final SQLRow rowRub = sqlRowAccessor.getTable().getDBRoot().findTable("RUBRIQUE_COTISATION").getRow(sqlRowAccessor.getInt("IDSOURCE"));

            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    JOptionPane.showMessageDialog(null, "Aucune liaison définie vers une ligne de bulletin simplifié pour la cotisation " + rowRub.getString("CODE") + " - " + rowRub.getString("NOM")
                            + ".\n La liaison sera faite sur autres contributions dues par l'employeur!");
                }
            });
            idLigneSimplifiee = LignePayeSimplifiee.AUTRES_CONTRIBUTIONS.getId();
        }

        if (!idLigneSimplifiee.equals(LignePayeSimplifiee.IGNORE.getId())) {
            SQLRowValues ligneSimplifiee = mapLigneSimplifiee.get(idLigneSimplifiee);
            if (ligneSimplifiee == null) {

                final LignePayeSimplifiee fromID = LignePayeSimplifiee.fromID(idLigneSimplifiee);
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        JOptionPane.showMessageDialog(null,
                                "Aucune ligne simplifiée " + fromID + " dans le bulletin " + (cadre ? "cadre!" : "non cadre!") + "\n La ligne " + sqlRowAccessor.getString("NOM") + " a été ignoré!");
                    }
                });

            } else {
                BigDecimal montantPatBulletinSimpl = BigDecimal.ZERO;
                BigDecimal montantSalBulletinSimpl = BigDecimal.ZERO;
                if (ligneSimplifiee.getBigDecimal("MONTANT_SAL_DED") != null) {
                    montantSalBulletinSimpl = ligneSimplifiee.getBigDecimal("MONTANT_SAL_DED");
                }
                if (ligneSimplifiee.getBigDecimal("MONTANT_PAT") != null) {
                    montantPatBulletinSimpl = ligneSimplifiee.getBigDecimal("MONTANT_PAT");
                }

                BigDecimal montantPatLigne = BigDecimal.ZERO;
                BigDecimal montantSalLigne = BigDecimal.ZERO;
                if (sqlRowAccessor.getBigDecimal("MONTANT_SAL_DED") != null) {
                    montantSalLigne = sqlRowAccessor.getBigDecimal("MONTANT_SAL_DED");
                }
                if (sqlRowAccessor.getBigDecimal("MONTANT_PAT") != null) {
                    montantPatLigne = sqlRowAccessor.getBigDecimal("MONTANT_PAT");
                }
                if (montantPatLigne.add(montantSalLigne).signum() != 0) {
                    ligneSimplifiee.put("NB_BASE", sqlRowAccessor.getBigDecimal("NB_BASE"));
                    ligneSimplifiee.put("MONTANT_SAL_DED", montantSalBulletinSimpl.add(montantSalLigne));
                    ligneSimplifiee.put("MONTANT_PAT", montantPatBulletinSimpl.add(montantPatLigne));
                }

                BigDecimal tauxPatBulletinSimpl = BigDecimal.ZERO;
                BigDecimal tauxSalBulletinSimpl = BigDecimal.ZERO;
                if (ligneSimplifiee.getBigDecimal("TAUX_SAL") != null) {
                    tauxSalBulletinSimpl = ligneSimplifiee.getBigDecimal("TAUX_SAL");
                }
                if (ligneSimplifiee.getBigDecimal("TAUX_PAT") != null) {
                    tauxPatBulletinSimpl = ligneSimplifiee.getBigDecimal("TAUX_PAT");
                }

                BigDecimal tauxPatLigne = BigDecimal.ZERO;
                BigDecimal tauxSalLigne = BigDecimal.ZERO;
                if (sqlRowAccessor.getBigDecimal("TAUX_SAL") != null) {
                    tauxSalLigne = sqlRowAccessor.getBigDecimal("TAUX_SAL");
                }
                if (sqlRowAccessor.getBigDecimal("TAUX_PAT") != null) {
                    tauxPatLigne = sqlRowAccessor.getBigDecimal("TAUX_PAT");
                }

                if (tauxSalLigne.signum() > 0) {
                    ligneSimplifiee.put("TAUX_SAL", tauxSalBulletinSimpl.add(tauxSalLigne));
                }

                if (tauxPatLigne.signum() > 0) {
                    ligneSimplifiee.put("TAUX_PAT", tauxPatBulletinSimpl.add(tauxPatLigne));
                }
            }
        }
    }

    private StyleSQLElement eltStyle = Configuration.getInstance().getDirectory().getElement(StyleSQLElement.class);

    private Map<String, SQLRowValues> initCotisations(boolean cadre, List<SQLRowAccessor> resultCotis) {

        SQLTable tableFichePayeElt = this.row.getTable().getDBRoot().findTable("FICHE_PAYE_ELEMENT");
        SQLRowValues rowValsCotis = new SQLRowValues(tableFichePayeElt);

        rowValsCotis.put("ID_STYLE", eltStyle.getAllStyleByName().get("Titre 2"));
        rowValsCotis.put("NOM", "Cotisations et contributions sociales");
        // rowValsCotis.put("NB_BASE", BigDecimal.ZERO);
        // rowValsCotis.put("TAUX_SAL", BigDecimal.ZERO);
        // rowValsCotis.put("MONTANT_SAL_AJ", BigDecimal.ZERO);
        // rowValsCotis.put("MONTANT_SAL_DED", BigDecimal.ZERO);
        // rowValsCotis.put("MONTANT_PAT", BigDecimal.ZERO);
        rowValsCotis.put("IMPRESSION", Boolean.TRUE);
        rowValsCotis.put("IN_PERIODE", Boolean.TRUE);
        resultCotis.add(rowValsCotis);

        Map<String, SQLRowValues> mapPayeSimplifiee = new HashMap<String, SQLRowValues>();
        List<GroupePayeSimplifiee> listGroupe = cadre ? StructurePayeSimplifiee.getStructureCadre() : StructurePayeSimplifiee.getStructureNonCadre();
        for (GroupePayeSimplifiee groupe : listGroupe) {
            SQLRowValues rowValsFichePaye = new SQLRowValues(tableFichePayeElt);
            rowValsFichePaye.put("NOM", groupe.getTranslation());
            rowValsFichePaye.put("IMPRESSION", Boolean.TRUE);
            rowValsFichePaye.put("IN_PERIODE", Boolean.TRUE);
            rowValsFichePaye.put("ID_STYLE", eltStyle.getAllStyleByName().get("Normal"));
            // rowValsFichePaye.put("NB_BASE", BigDecimal.ZERO);
            // rowValsFichePaye.put("TAUX_SAL", BigDecimal.ZERO);
            // rowValsFichePaye.put("MONTANT_SAL_AJ", BigDecimal.ZERO);
            // rowValsFichePaye.put("MONTANT_SAL_DED", BigDecimal.ZERO);
            // rowValsFichePaye.put("MONTANT_PAT", BigDecimal.ZERO);
            resultCotis.add(rowValsFichePaye);
            mapPayeSimplifiee.put(groupe.getId(), rowValsFichePaye);
            for (LignePayeSimplifiee lignePayeSimplifiee : groupe.getChildren()) {

                if (groupe.isShowChildren()) {
                    SQLRowValues rowValsLPaye = new SQLRowValues(tableFichePayeElt);
                    rowValsLPaye.put("NOM", lignePayeSimplifiee.getTranslation());
                    rowValsLPaye.put("IMPRESSION", Boolean.TRUE);
                    rowValsLPaye.put("IN_PERIODE", Boolean.TRUE);
                    rowValsLPaye.put("ID_STYLE", eltStyle.getAllStyleByName().get("Normal"));
                    // rowValsLPaye.put("NB_BASE", BigDecimal.ZERO);
                    // rowValsLPaye.put("TAUX_SAL", BigDecimal.ZERO);
                    // rowValsLPaye.put("MONTANT_SAL_AJ", BigDecimal.ZERO);
                    // rowValsLPaye.put("MONTANT_SAL_DED", BigDecimal.ZERO);
                    // rowValsLPaye.put("MONTANT_PAT", BigDecimal.ZERO);
                    mapPayeSimplifiee.put(lignePayeSimplifiee.getId(), rowValsLPaye);
                    resultCotis.add(rowValsLPaye);
                } else {
                    mapPayeSimplifiee.put(lignePayeSimplifiee.getId(), rowValsFichePaye);
                }
            }
        }
        return mapPayeSimplifiee;
    }

    @Override
    public String getDefaultTemplateId() {
        return TEMPLATE_ID;
    }

    @Override
    public String getName() {
        SQLPreferences prefs = new SQLPreferences(elt.getTable().getDBRoot());
        boolean prefNomSal = prefs.getBoolean(PayeGlobalPreferencePanel.SALARIE_NOM_FICHIER, Boolean.FALSE);
        SQLRow rowSal = row.getForeign("ID_SALARIE");
        SQLRow rowMois = row.getForeign("ID_MOIS");
        Calendar du = row.getDate("DU");
        String suffix = "";
        if (prefNomSal) {
            suffix = "_" + rowSal.getString("NOM");
        }
        if (du != null && du.get(Calendar.DAY_OF_MONTH) != 1) {
            suffix = "_" + du.get(Calendar.DAY_OF_MONTH);
        }
        return ("FichePaye_" + rowSal.getString("CODE") + suffix + "_" + rowMois.getString("NOM") + "_" + row.getString("ANNEE"));
    }

}