<?php

class RHP_Component
{
    private static function standard_prefixe($GROUPBY, $DEFAULT = "")
    {
        if (substr($GROUPBY, 3, 3) == 'CLS') {
            $GROUPBY_section = substr($GROUPBY, 6);
            $GROUPBY = substr($GROUPBY, 0, 6);
        }
        $field = '';
        $field_id = '';
        $field_code = '';
        $field_texte = '';
        switch ($GROUPBY) {
            case '*TAB':
                $field = "*TAB";
                break;
            case '-1':
                $field = "";
                break;
            case 'MOIACT': //             @deprecated
                $field = "mois_activite";
                break;
            case 'ANNACT': //             @deprecated
                $field = "annee_activite";
                break;
            case 'MOIPAI': //             @deprecated
                $field = "mois_paie";
                break;
            case 'ANNPAI': //             @deprecated
                $field = "annee_paie";
                break;
            case 'MOIACTPAI': //             @deprecated
                $field = "mois_activite_paie";
                break;
            case 'ANNACTPAI': //             @deprecated
                $field = "annee_activite_paie";
                break;
            case 'AMACT':
                $field = "v_rh_view_1.annee_mois_activite";
                break;
            case 'ATRIMACT':
                $field = "v_rh_view_1.annee_trimestre_activite";
                break;
            case 'AACT':
                $field = "v_rh_view_1.annee_activite";
                break;
            case 'MACT':
                $field = "v_rh_view_1.mois_only_activite";
                break;
            case 'TRIMACT':
                $field = "v_rh_view_1.trimestre_activite";
                break;
            case 'SEMACT':
                $field = "semaine_activite";
                break;
            case 'JOURACT':
                $field = "jour_activite";
                break;

            case 'AMCAL':
                $field = "annee_mois_paie";
                break;
            case 'ATRIMCAL':
                $field = "annee_trimestre_paie";
                break;
            case 'ACAL':
                $field = "annee_paie";
                break;
            case 'MCAL':
                $field = "mois_only_paie";
                break;
            case 'TRIMCAL':
                $field = "trimestre_paie";
                break;
            case 'AGE':
                $field = "age";
                break;
            case 'AGECLS':
                $field = "age_section";
                break;
            case 'ANC':
                $field = "anciennete_annee";
                break;
            case 'ANCCLS':
                $field = "anciennete_annee_section";
                break;
			case 'BUD':
                $field = "budget_cle";
                break;
            case 'BUDCLS':
                $field = "budget_clesection";
                break;
            case 'CNT':
                $field = "v_rh_view_1.contrat";
                break;
            case 'ENT':
                $field = "entreprise";
                break;
            case 'ENTCLS':
                $field = "entreprise_section";
                break;
            case 'ETS':
                $field = "etablissement";
                break;
            case 'VENCAPETS':
                $field = "ventilation_capsante_etablissement";
                break;
            case 'ETSCLS':
                $field = "etablissement_section";
                break;
            case 'CPO':
                $field = "code_postal";
                break;
            case 'CPOCLS':
                $field = "code_postal_section";
                break;
            case 'CPD':
                $field = "departement";
                break;
            case 'GRI':
                $field = "grille";
                break;
            case 'GRICLS':
                $field = "grille_section";
                break;
            case 'GRG':
                $field = "grille_groupe";
                break;
            case 'GRGCLS':
                $field = "grille_groupe_section";
                break;
            case 'QUA':
                $field = "qualification";
                break;
            case 'QUACLS':
                $field = "qualification_section";
                break;
            case 'POL':
                $field = "pole";
                break;
            case 'POLCLS':
                $field = "pole_section";
                break;
            case 'POP':
                $field = "population";
                break;
            case 'POPCLS':
                $field = "population_section";
                break;
            case 'SER':
                $field = "service";
                break;
            case 'SERCLS':
                $field = "service_section";
                break;
            case 'TTT':
                $field = "type_temps_travail";
                break;
            case 'TTTCLS':
                $field = "type_temps_travail_section";
                break;
            case 'TCN':
                $field = "type_contrat";
                break;
            case 'TCNCLS':
                $field = "type_contrat_section";
                break;
            case 'THO':
                $field = "type_horaire";
                break;
            case 'THOCLS':
                $field = "type_horaire_section";
                break;
            case 'SPE':
                $field = "specialite";
                break;
            case 'SPECLS':
                $field = "specialite_section";
                break;
            case 'STA':
                $field = "statut";
                break;
            case 'STACLS':
                $field = "statut_section";
                break;
            case 'CEM':
                $field = "code_emploi";
                break;
            case 'CEMCLS':
                $field = "code_emploi_section";
                break;
            case 'GCT':
                $field = "groupe_cotisant";
                break;
            case 'GCTCLS':
                $field = "groupe_cotisant_section";
                break;
            case 'CSC':
                $field = "categorie_socio_professionnelle";
                break;
            case 'CSCCLS':
                $field = "categorie_socio_professionnelle_section";
                break;
            case 'MTD':
                $field = "motif_debut";
                break;
            case 'MTDCLS':
                $field = "motif_debut_section";
                break;
            case 'MTF':
                $field = "motif_fin";
                break;
            case 'MTFCLS':
                $field = "motif_fin_section";
                break;
            case 'SAP':
                $field = "section_analytique_paie";
                break;
            case 'SAPCLS':
                $field = "section_analytique_paie_section";
                break;
            case 'SAC':
                $field = "section_analytique";
                break;
            case 'SACCLS':
                $field = "section_analytique_section";
                break;
            case 'SIN':
                $field = "societe_interim";
                break;
            case 'SINCLS':
                $field = "societe_interim_section";
                break;
            case 'SEX':
                $field = "v_rh_view_1.sexe";
                break;
            case 'SFA':
                $field = "situation_famille";
                break;
            case 'SFACLS':
                $field = "situation_famille_section";
                break;
            case 'NAT':
                $field = "nationalite";
                break;
            case 'NIR':
                $field = "nir";
                break;
            case 'NATCLS':
                $field = "nationalite_section";
                break;
            case 'SAL':
                $field = "v_rh_view_1.salarie";
                break;
            case 'SALCLS':
                $field = "salarie_section";
                break;
            case 'PER':
                $field = "personne";
                break;
            case 'SRP':
                $field = "salarie_remplace";
                break;
            case 'SRPCLS':
                $field = "salarie_remplace_section";
                break;
            case 'CNT':
                $field = "contrat";
                break;
            case 'SALFIN':
                $field = "salarie";
                break;
            case 'CNTFIN':
                $field = "contrat";
                break;
            case 'RUP':
                $field = "rubrique";
                break;
            case 'RUPCLS':
                $field = "rubrique_section";
                break;
            case 'CPT':
                $field = "compte";
                break;
            case 'CPTCLS':
                $field = "compte_section";
                break;
            case 'ORC':
                $field = "organisme_cotisation";
                break;
            case 'ORCCLS':
                $field = "organisme_cotisation_section";
                break;
            case 'MOA':
                $field = "motif_arret";
                break;
            case 'MOACLS':
                $field = "motif_arret_section";
                break;
            case 'MPA':
                $field = "precision_motif_arret";
                break;
            case 'MPACLS':
                $field = "precision_motif_arret_section";
                break;
            case 'ARR':
                $field = "arret_travail";
                break;
            case 'MVM':
                $field = "motif_visite";
                break;
            case 'FOR':
                $field = "liste_formation";
                break;
            case 'FORM':
                $field = "formation";
                break;
            case 'VIS':
                $field = "visite";
                break;
            case 'ACC':
                $field = "accident_travail";
                break;
            case 'ATC':
                $field = "accident_circonstance";
                break;
            case 'ATL':
                $field = "accident_lieu";
                break;
            case 'ATN':
                $field = "accident_nature";
                break;
            case 'ATS':
                $field = "accident_siege";
                break;
            case 'UNF':
                $field = "unite_fonctionnelle";
                break;
            case 'UNFCLS':
                $field = "unite_fonctionnelle_section";
                break;
            case 'LEB':
                $field = "lettre_budgetaire";
                break;
            case 'UNFCLS':
                $field = "unite_fonctionnelle_section";
                break;
            case 'GCP':
                $field = "commission_paritaire";
                break;
            case 'GCPCLS':
                $field = "commission_paritaire_section";
                break;
            case 'COI':
                $field = "compte_salarie";
                break;
            case 'COICLS':
                $field = "compte_salarie_section";
                break;
            case 'CAC':
                $field = "categorie_conge";
                break;
            case 'CACCLS':
                $field = "categorie_conge_section";
                break;
            case 'CAE':
                $field = "cadre_emploi";
                break;
            case 'CAECLS':
                $field = "cadre_emploi_section";
                break;
            case 'FIL':
                $field = "filiere";
                break;
            case 'FILCLS':
                $field = "filiere_section";
                break;
            case 'CAS':
                $field = "categorie_statutaire";
                break;
            case 'CASCLS':
                $field = "categorie_statutaire_section";
                break;
            case 'PEP':
                $field = "planning_etablissement";
                break;
            case 'PEPCLS':
                $field = "planning_etablissement_section";
                break;
            case 'PSP':
                $field = "planning_service";
                break;
            case 'PSPCLS':
                $field = "planning_service_section";
                break;
            case 'PSE':
                $field = "planning_service_source";
                break;
            case 'PSECLS':
                $field = "planning_service_source_section";
                break;
            case 'PQP':
                $field = "planning_qualification";
                break;
            case 'PQPCLS':
                $field = "planning_qualification_section";
                break;
            case 'PQU':
                $field = "planning_qualification_source";
                break;
            case 'PQUCLS':
                $field = "planning_qualification_source_section";
                break;
            case 'PNI':
                $field = "planning_niveau";
                break;
            case 'PNICLS':
                $field = "planning_niveau_section";
                break;
            case 'PCH':
                $field = "planning_code_horaire";
                break;
            case 'PCHCLS':
                $field = "planning_code_horaire_section";
                break;
            case 'PAB':
                $field = "planning_type_absence";
                break;
            case 'PABCLS':
                $field = "planning_type_absence_section";
                break;
            case 'AN1':
                $field = "ng_section_analytique_1";
                break;
            case 'AN1CLS':
                $field = "ng_section_analytique_1_section";
                break;
            case 'AN2':
                $field = "ng_section_analytique_2";
                break;
            case 'AN2CLS':
                $field = "ng_section_analytique_2_section";
                break;
            case 'AN3':
                $field = "ng_section_analytique_3";
                break;
            case 'AN3CLS':
                $field = "ng_section_analytique_3_section";
                break;
            case 'VIS':
                $field = "salarie";
                break;
            case 'EXC':
                $field = "expert_controle";
                break;
            default:
                $field = $DEFAULT;
        }
        return $field;
    }

    private static function standard_view($GROUPBY, $DEFAULT = "v_contrats_1")
    {
        if (substr($GROUPBY, 3, 3) == 'CLS') {
            $GROUPBY_section = substr($GROUPBY, 6);
            $GROUPBY = substr($GROUPBY, 0, 6);
        }
        $field = '';
        $field_id = '';
        $field_code = '';
        $field_texte = '';

        switch ($GROUPBY) {
            case '*TAB':
                $field = "*TAB";
                break;
            default:
                $field = "";
        }

        return $field;
    }

    public static function standard_field_id($GROUPBY, $DEFAULT = "")
    {
        $prefixe = self::standard_prefixe($GROUPBY, $DEFAULT = "");
        $view = self::standard_view($GROUPBY);

        if (substr($GROUPBY, 3, 3) == 'CLS') {
            $GROUPBY_section = substr($GROUPBY, 6);
            $field = $view . "$prefixe" . "_id[$GROUPBY_section]";
        } else {
            switch ($prefixe) {
                case "":
                    $field = "0";
                    break;
                case "*TAB":
                    $field = "-9999999";
                    break;
                default:
                    $field = $view . "$prefixe" . "_id";
            }
        }
        return $field;
    }

    public static function standard_field_code($GROUPBY, $DEFAULT = "")
    {
        $prefixe = self::standard_prefixe($GROUPBY, $DEFAULT = "");
        $view = self::standard_view($GROUPBY);

        if (substr($GROUPBY, 3, 3) == 'CLS') {
            $GROUPBY_section = substr($GROUPBY, 6);
            $field = $view . "$prefixe" . "_code[$GROUPBY_section]";
        } else {
            switch ($prefixe) {
                case "":
                    $field = "''";
                    break;
                case "*TAB":
                    $field = "' Résumé'";
                    break;
                default:
                    $field = $view . "$prefixe" . "_code";
            }
        }
        return $field;
    }

    public static function standard_field_texte($GROUPBY, $DEFAULT = "")
    {
        $prefixe = self::standard_prefixe($GROUPBY, $DEFAULT = "");
        $view = self::standard_view($GROUPBY);

        if (substr($GROUPBY, 3, 3) == 'CLS') {
            $GROUPBY_section = substr($GROUPBY, 6);
            $field = $view . "$prefixe" . "_texte[$GROUPBY_section]";
        } else {
            switch ($prefixe) {
                case "":
                    $field = "''";
                    break;
                case "*TAB":
                    $field = "'Résumé'";
                    break;
                default:
                    $field = $view . "$prefixe" . "_texte";
            }
        }
        return $field;
    }

    public static function standard_field_select($field_prefixe, $table_name, $selected_value, $field2_prefixe = "")
    {
        $select = "";
        $module = "rh";
        if ($table_name == 't_finess') {
            $module = "base";
        }

        if ($table_name == 't_ages') {
            $tmpArr = explode('|', $selected_value);
            $selected_value = $tmpArr[0];
            $tmpAge1 = (isset($tmpArr[1]) ? $tmpArr[1] : '');
            $tmpAge2 = (isset($tmpArr[2]) ? $tmpArr[2] : '');
        }

        if ($table_name == 't_section_analytique') {
            $tmpArr = explode('|', $selected_value);
            if ($tmpArr[0] == '-1') {
                $selected_value = $tmpArr[0];
            } else {
                $selected_value = $tmpArr[1];
            }
        }

        if ($selected_value != '-1') {
            $section = 1;
            $mode = substr($selected_value, 0, 2);
            $oid = $selected_value;
            if ($mode == 'LS') {
                $section = substr($selected_value, 2, 2);
                $oid = substr($selected_value, 4);
            }
            if ($mode != 'LI' && $mode != 'SL' && $mode != 'LS' && $mode != 'C*' && $mode != 'NV' && $mode != 'IN') {
                if ($mode != 'D2') {
                    $mode = 'DT';
                }
                $oid = substr($selected_value, 2);
            }
            if ($field_prefixe == "v_rh_view_1.entreprise" && $mode == 'LI') {
                $field_prefixe = $field2_prefixe;
            }
            if ($field_prefixe == "v_rh_view_1.entreprise" && $mode == 'SL') {
                $field_prefixe = $field2_prefixe;
            }
            if ($mode == 'LI' || $mode == 'SL') {
                $oid = substr($selected_value, 2);
            }
            if ($mode == 'C*') {
                if ($table_name != "t_ages") {
                    $where_codes = sql_codes_list_a(substr($selected_value, 2), 'code');
                } else {
                    $where_codes = sql_codes_list_n(substr($selected_value, 2), "to_number(code,'000')");
                }
            }

            // Spécial intervalle âges.
            $where = '';
            if ($mode == 'IN') {
                if ($tmpAge1 != '') {
                    $where = ' >= ' . $tmpAge1;
                }
                if ($tmpAge2 != '') {
                    $where = ' <= ' . $tmpAge2;
                }
                if ($tmpAge1 != '' && $tmpAge2 != '') {
                    $where = ' BETWEEN ' . $tmpAge1 . ' AND ' . $tmpAge2;
                }
            }

            $subselect_select = '';

            $field_id = $field_prefixe . "_id";
            $field2_id = $field2_prefixe . "_id";
            $field_section_id = $field_prefixe . "_section_id[" . $section . "]";

            if ($mode == 'LI') {
                $subselect_select = " $field_id = ANY (array(SELECT to_id FROM $module.t_listes_contenu WHERE liste_id = $oid)::bigint[])";
            }
            if ($mode == 'SL') {
                $subselect_select = " $field_id != ALL (array(SELECT to_id FROM $module.t_listes_contenu WHERE liste_id = $oid)::bigint[])";
            }
            if ($mode == 'LS') {
                $subselect_select = " $field_section_id = $oid";
            }
            if ($mode == 'NV') {
                $subselect_select = " $field_id IS DISTINCT FROM 0";
            }
            if ($mode == 'DT') {
                $subselect_select = " $field_id = $oid";
            }
            if ($mode == 'D2') {
                $subselect_select = " $field2_id = $oid";
            }
            if ($mode == 'C*') {
                $subselect_select = " $field_id = ANY (array(SELECT oid FROM $module.$table_name WHERE $where_codes)::bigint[])";
            }
            if ($mode == 'IN') {
                $subselect_select = " age_id $where";
            }

            if ($subselect_select != '') {
                $select = " AND $subselect_select ";
            }
        }

        return $select;
    }

    public static function specific_field_select($field, $value, $dateDebut, $dateFin)
    {
        $ret = '';

        switch ($field) {
            case 'PRESENCESAL':
                switch ($value) {
                    case '1':
                        $ret .= "
                        AND v_rh_view_1.date_debut_activite = '$dateDebut'
                        ";
                        break;
                    case '2':
                        $ret .= "
                        AND v_rh_view_1.date_fin_activite = '$dateFin'
                        ";
                        break;
                    case '3':
                        $ret .= "
                        AND v_rh_view_1.date_debut_activite = '$dateDebut'
                        AND v_rh_view_1.date_fin_activite = '$dateFin'
                        ";
                        break;
                }
                break;
            case 'PRESENCECNT':
                switch ($value) {
                    case '1':
                        $ret .= "
                        AND v_rh_view_1.date_debut_contrat <= '$dateDebut'
                        ";
                        break;
                    case '2':
                        $ret .= "
                        AND v_rh_view_1.date_fin_contrat >= '$dateFin'
                        ";
                        break;
                    case '3':
                        $ret .= "
                        AND v_rh_view_1.date_debut_contrat <= '$dateDebut'
                        AND v_rh_view_1.date_fin_contrat >= '$dateFin'
                        ";
                        break;
                    case '4':
                        $ret .= "
                        AND v_rh_view_1.present_debut_mois = 1
                        ";
                        break;
                    case '4a':
                        $ret .= "
                        AND (FALSE
                            OR v_rh_view_1.present_debut_mois||v_rh_view_1.mois_only_activite_code = '101'
                            OR v_rh_view_1.present_debut_mois||v_rh_view_1.mois_only_activite_code = '104'
                            OR v_rh_view_1.present_debut_mois||v_rh_view_1.mois_only_activite_code = '107'
                            OR v_rh_view_1.present_debut_mois||v_rh_view_1.mois_only_activite_code = '110')
                        ";
                        break;
                    case '4b':
                        $ret .= "
                        AND v_rh_view_1.present_debut_mois||v_rh_view_1.mois_only_activite_code = '101'
                        ";
                        break;
                    case '5':
                        $ret .= "
                        AND v_rh_view_1.present_fin_mois = 1
                        ";
                        break;
                    case '5a':
                        $ret .= "
                        AND (FALSE
                            OR v_rh_view_1.present_fin_mois||v_rh_view_1.mois_only_activite_code = '103'
                            OR v_rh_view_1.present_fin_mois||v_rh_view_1.mois_only_activite_code = '106'
                            OR v_rh_view_1.present_fin_mois||v_rh_view_1.mois_only_activite_code = '109'
                            OR v_rh_view_1.present_fin_mois||v_rh_view_1.mois_only_activite_code = '112')
                        ";
                        break;
                    case '5b':
                        $ret .= "
                        AND v_rh_view_1.present_fin_mois||v_rh_view_1.mois_only_activite_code = '112'
                        ";
                        break;
                    case '6':
                        $ret .= "
                        AND v_rh_view_1.present_debut_mois = 1
                        AND v_rh_view_1.present_fin_mois = 1
                        ";
                        break;
                    case '6a':
                        $ret .= "
                        AND (FALSE
                            OR v_rh_view_1.present_debut_mois||v_rh_view_1.mois_only_activite_code = '101'
                            OR v_rh_view_1.present_debut_mois||v_rh_view_1.mois_only_activite_code = '104'
                            OR v_rh_view_1.present_debut_mois||v_rh_view_1.mois_only_activite_code = '107'
                            OR v_rh_view_1.present_debut_mois||v_rh_view_1.mois_only_activite_code = '110')
                        AND (FALSE
                            OR v_rh_view_1.present_fin_mois||v_rh_view_1.mois_only_activite_code = '103'
                            OR v_rh_view_1.present_fin_mois||v_rh_view_1.mois_only_activite_code = '106'
                            OR v_rh_view_1.present_fin_mois||v_rh_view_1.mois_only_activite_code = '109'
                            OR v_rh_view_1.present_fin_mois||v_rh_view_1.mois_only_activite_code = '112')
                        ";
                        break;
                    case '6b':
                        $ret .= "
                        AND v_rh_view_1.present_debut_mois||v_rh_view_1.mois_only_activite_code = '101'
                        AND v_rh_view_1.present_fin_mois||v_rh_view_1.mois_only_activite_code = '112'
                        ";
                        break;
                    case '7':
                        $ret .= "
                        AND v_rh_view_1.mois_activite_code = to_char('$dateDebut'::date, 'YYYYMM'::text)
                        AND v_rh_view_1.present_debut_mois = 1
                        ";
                        break;
                    case '8':
                        $ret .= "
                        AND v_rh_view_1.mois_activite_code = to_char('$dateFin'::date, 'YYYYMM'::text)
                        AND v_rh_view_1.present_fin_mois = 1
                        ";
                        break;
                    case '9':
                        $ret .= "
                        AND v_rh_view_1.mois_activite_code = to_char('$dateDebut'::date, 'YYYYMM'::text)
                        AND v_rh_view_1.present_debut_mois = 1
                        AND v_rh_view_1.mois_activite_code = to_char('$dateFin'::date, 'YYYYMM'::text)
                        AND v_rh_view_1.present_fin_mois = 1
                        ";
                        break;
                }
                break;
        }

        return $ret;
    }

    public static function specific_day_field_select($days)
    {
        $ret = '';

        // On effectue le traitement uniquement si une case a été décochée.
        if (implode('', $days) != '1111111') {
            for ($i = 0; $i < 7; $i++) {
                if ($days[$i] == '0') {
                    $ret .= "\nAND to_char(v_rh_view_1.date_fin_activite, 'ID') != " . ($i + 1);
                }
            }
        }

        return $ret;
    }
}
