return: SETOF activite.p_gmt_valorises
lang: plpgsql
src: |
  DECLARE
      _nb_jours_presence int;
      _zone_sejour int;
      _montant_valorise int;
      _montant_fj int;
      _input_record RECORD;
      _output_record activite.p_gmt_valorises%ROWTYPE;
  BEGIN
      FOR _input_record IN
          SELECT
              p_sejours_groupes_smr.no_sejour,
              p_sejours_groupes_smr.nb_jours_presence,
              p_sejours_groupes_smr.gme_code,
              p_sejours_groupes_smr.gmt_code,
              p_sejours_groupes_smr.date_debut_gmt,
              p_sejours_groupes_smr.date_fin_gmt,
              COALESCE(v1.texte, v2.texte, v3.texte) AS texte,
              COALESCE(v1.dzf, v2.dzf, v3.dzf) AS dzf,
              COALESCE(v1.fzf, v2.fzf, v3.fzf) AS fzf,
              COALESCE(v1.tzb, v2.tzb, v3.tzb) AS tzb,
              COALESCE(v1.tzf1, v2.tzf1, v3.tzf1) AS tzf1,
              COALESCE(v1.szb, v2.szb, v3.szb) AS szb,
              COALESCE(v1.szh, v2.szh, v3.szh) AS szh
              FROM activite.p_sejours_groupes_smr
                  LEFT JOIN base.v_tarifs_prive_gmt_en_7 v1 ON p_sejours_groupes_smr.gmt_code = v1.code
                      AND p_sejours_groupes_smr.date_fin_gmt BETWEEN v1.date_debut AND v1.date_fin 
                      AND p_sejours_groupes_smr.gme_code = v1.gme_code
                  LEFT JOIN base.v_tarifs_prive_gmt_en_8 v2 ON p_sejours_groupes_smr.gmt_code = v2.code
                      AND p_sejours_groupes_smr.date_fin_gmt BETWEEN v2.date_debut AND v2.date_fin 
                      AND p_sejours_groupes_smr.gme_code = v2.gme_code
                  LEFT JOIN base.v_tarifs_prive_gmt_dzf_90 v3 ON p_sejours_groupes_smr.gmt_code = v3.code
                      AND p_sejours_groupes_smr.date_fin_gmt BETWEEN v3.date_debut AND v3.date_fin 
                      AND p_sejours_groupes_smr.gme_code = v3.gme_code
              WHERE p_sejours_groupes_smr.types_gmt_code = 'hc_sans_zf'
          LOOP
              IF _input_record.gmt_code LIKE '7%' 
                  THEN 
                      _nb_jours_presence := _input_record.nb_jours_presence;
                  ELSE 
                      _nb_jours_presence := LEAST(_input_record.nb_jours_presence, 90);
              END IF;
              SELECT COALESCE(activite.get_montant_fj_valorise_par_sejour(_input_record.no_sejour), 0) INTO _montant_fj;
        
              IF _nb_jours_presence >= _input_record.dzf
                  THEN 
                      _zone_sejour := 2;
                  ELSE
                      _zone_sejour := 1;
              END IF;
        
              CASE _zone_sejour
                  WHEN 1 THEN _montant_valorise := _input_record.tzb - (_input_record.dzf - _nb_jours_presence) * _input_record.szb ;
                  WHEN 2 THEN _montant_valorise := _input_record.tzf1;
              END CASE
              ;
              
              _output_record.no_sejour := _input_record.no_sejour;
              _output_record.gme_code := _input_record.gme_code;
              _output_record.gmt_code := _input_record.gmt_code;
              _output_record.gmt_libelle := _input_record.texte;
              _output_record.date_debut_gmt := _input_record.date_debut_gmt;
              _output_record.date_fin_gmt := _input_record.date_fin_gmt;
  
              IF _input_record.gmt_code LIKE '7%' 
                  THEN 
                      _output_record.montant_valorise := (_montant_valorise * _nb_jours_presence/7) - _montant_fj;
                      _output_record.montant_valorise_par_jour := ((_montant_valorise * _nb_jours_presence/7) - _montant_fj) / _nb_jours_presence;
                  ELSE
                      _output_record.montant_valorise := _montant_valorise - _montant_fj;
                      _output_record.montant_valorise_par_jour := (_montant_valorise - _montant_fj) / _nb_jours_presence;
              END IF;
              
              RETURN NEXT _output_record;
          END LOOP;
        
        RETURN;
    END;