*************************************************************
*  Mise  jour des interventions du planning			*	
*  Vanessa									*
*  septembre 2003								*
*										*
*************************************************************

* Ouvertures des fichiers

EXECUTE "SET-THOUS ."
EXECUTE "SET-DEC ,"
EXECUTE "DATE.FORMAT"
OPEN "", "TPSDETAILSINTERV" TO F.TPSDETAILSINTERV ELSE PRINT "OUVERTURE TPSDETAILSINTERV" ;* en lecture criture
OPEN "", "TPSEXCEPTIONS" TO F.TPSEXCEPTIONS ELSE PRINT "OUVERTURE TPSEXCEPTIONS" ;* en lecture criture
OPEN "", "TPSMOI" TO F.TPSMOI ELSE PRINT "OUVERTURE TPSMOI" ;* en lecture criture
OPEN "", "ABSENCESAIDANT" TO F.ABSENCESAIDANT ELSE RETURN
OPEN "", "TPSTEMPO" TO F.TPSTEMPO ELSE STOP
OPEN "", "CIVILAIDE" TO F.CIVILAIDE ELSE STOP
OPEN "", "CIVILAIDANT" TO F.CIVILAIDANT ELSE STOP
OPEN "", "TPSPARAM" TO F.TPSPARAM ELSE STOP
*********************************************************
* Recuperation des arguments

PROCREAD ARGUMENTS ELSE STOP
W_CodeAide=FIELD(ARGUMENTS,"|",2)
W_DateFinInterv=FIELD(ARGUMENTS,"|",3)
W_CodeAct=FIELD(ARGUMENTS,"|",4)
W_CodeAidant=FIELD(ARGUMENTS,"|",5)
W_TypeAct=FIELD(ARGUMENTS,"|",6)
W_AncCodeAbs=FIELD(ARGUMENTS,"|",7)
W_NewCodeAbs=FIELD(ARGUMENTS,"|",8)
W_Trait=FIELD(ARGUMENTS,"|",9)
W_CodeContrat =FIELD(ARGUMENTS,"|",10)
W_AbsenceAide = FIELD(ARGUMENTS,"|",11)
W_ListeInterv = FIELD(ARGUMENTS,"|",12)
Origine = FIELD(ARGUMENTS,"|",13)
User = FIELD(ARGUMENTS,"|",14)
*********************************************************
* PROGRAMME PRINCIPAL

NomRequeteRempl = ""
W_ParamRempl = ""
W_SortieRempl = ""
W_FinOrigineRempl = ""
W_DebParamRempl = ""
W_FinParamRempl = ""
W_CleRempl = ""

W_Requete=""
W_RequeteExcep=""
W_RequeteMoi=""
W_Temp=""

W_ListeActSemaine = ""
EXECUTE 'SELECT ACTIVITES AVEC 18 = "1"'
W_fin = "F"
LOOP
READNEXT W_Cle ELSE W_fin = "V"
WHILE W_fin = "F"
	W_ListeActSemaine = W_ListeActSemaine:W_Cle:"|"
REPEAT



IF Origine # "AbsAide" THEN
    
   IF W_AncCodeAbs = "" THEN
      W_Temp='SSELECT TPSDETAILSINTERV AVEC 1 > "':W_DateFinInterv:'" OR = ""'
      
      IF W_CodeAide # "" THEN
         W_Temp=W_Temp:' AND AVEC CodeAide = "':W_CodeAide:'"'
      END
      IF W_CodeAct # "" THEN
         W_Temp=W_Temp:' AND AVEC CodeActInterv = "':W_CodeAct:'"'
      END
      IF W_CodeAidant # "" THEN
         W_Temp=W_Temp:' AND AVEC CodeAidant = "':W_CodeAidant:'"'
      END
      IF W_TypeAct # "" THEN
         W_Temp=W_Temp:' AND AVEC TypeActDetailInterv = "':W_TypeAct:'"'
      END
   END ELSE
	READ ENR_ABSENCESAIDANT FROM F.ABSENCESAIDANT, W_AncCodeAbs ELSE
		* ne dois pas arriver (l'absence n'est supprime qu'aprs traitement). Prvu au cas ou....
		ENR_ABSENCESAIDANT<2> = W_DateFinInterv
	END 
      W_Temp='SSELECT TPSDETAILSINTERV AVEC 6 = "':W_AncCodeAbs:'"'

   END
   
   EXECUTE W_Temp
   W_NomRequete = "MajDateFinInterv":W_Trait:TIME()
   EXECUTE 'SAUVE-LISTE ':W_NomRequete
   EXECUTE "LISTE ":W_NomRequete RETURNING MSGCODE

   IF MSGCODE<1>#209 THEN
      SELECT F.TPSDETAILSINTERV TO W_Requete

      W_End = "FAUX"
      LOOP
         READNEXT W_Cle FROM W_Requete ELSE W_End = "VRAI"
      WHILE W_End = "FAUX" DO
         READ ENR_TPSDETAILSINTERV FROM F.TPSDETAILSINTERV,W_Cle ELSE ENR_TPSDETAILSINTERV=""
	 W_CleRempl = W_Cle
	 W_FinOrigineRempl = ENR_TPSDETAILSINTERV<1> 
         IF W_Trait="M" THEN
            IF W_DateFinInterv # "" THEN
		   * nb : pas de traitement spcifique activits semaine, car
		   * les tests ci dessus liminerons les interventions  liminer et ne toucherons pas aux autres.
		   IF W_AncCodeAbs = "" THEN
			ENR_TPSDETAILSINTERV<1>=W_DateFinInterv
		   END ELSE
		      IF ENR_ABSENCESAIDANT<2> > W_DateFinInterv THEN
			   * On a rduit l'absence
			   IF ENR_TPSDETAILSINTERV<1> > W_DateFinInterv THEN
	                   ENR_TPSDETAILSINTERV<1>=W_DateFinInterv
			   END
		      END ELSE
			   * On a prolong l'absence
			   IF ENR_TPSDETAILSINTERV<1> = ENR_ABSENCESAIDANT<2> THEN
				* nb : on ne prolonge que si date fin intev = ancienne date fin arret,
				* et on a dans W_ListeInterv la liste des interventions  ne pas prolonger...
			      IF INDEX(W_ListeActSemaine,W_Cle[16,6]:"|",1) = 0 AND INDEX(W_ListeInterv,W_Cle:";",1) = 0 THEN
			        * Activites   la  semaine : on a date de dbut = date de fin.
	                    ENR_TPSDETAILSINTERV<1>=W_DateFinInterv
			      END
			   END

		      END
		   END
		   
               IF ENR_TPSDETAILSINTERV<1> < W_Cle[11,5] THEN
		  W_DebParamRempl = W_Cle[11,5]
		  W_FinParamRempl = W_FinOrigineRempl
		  GOSUB 501
		  W_SortieRempl = W_SortieRempl :" Intervention supprime."
		  GOSUB 500
                  DELETE F.TPSDETAILSINTERV,W_Cle
               END ELSE
		    IF W_FinOrigineRempl = "" OR W_FinOrigineRempl > ENR_TPSDETAILSINTERV<1> THEN
		    * je veux tre sur qu'on appelle le PR que si on a rduit l'interv, car il n'est fait que pour ca.
			W_DebParamRempl = ENR_TPSDETAILSINTERV<1> +1
			W_FinParamRempl = W_FinOrigineRempl
			GOSUB 501
		    W_SortieRempl = W_SortieRempl :" Nouvelle date de fin au ":OCONV(ENR_TPSDETAILSINTERV<1>,"D4/"):"."
		   	GOSUB 500
		    END
                WRITE ENR_TPSDETAILSINTERV ON F.TPSDETAILSINTERV,W_Cle
               END

            END ELSE
              IF W_NewCodeAbs # "" THEN
               ENR_TPSDETAILSINTERV<6>=W_NewCodeAbs
		   IF W_AncCodeAbs[8,5] >= DATE() THEN
			DELETE F.TPSDETAILSINTERV, W_Cle
			IF W_NewCodeAbs[8,5] <= ENR_TPSDETAILSINTERV<1> THEN
				WRITE ENR_TPSDETAILSINTERV ON F.TPSDETAILSINTERV, W_Cle[1,10]:W_NewCodeAbs[8,5]:W_Cle[16,11]
			END ELSE
				* Rien
			END
		   END ELSE
               	WRITE ENR_TPSDETAILSINTERV ON F.TPSDETAILSINTERV, W_Cle
		   END
		  END
            END

         END ELSE
		IF W_Trait = "S" THEN
			W_DebParamRempl = W_Cle[11,5]
			W_FinParamRempl = W_FinOrigineRempl
			GOSUB 501
			W_SortieRempl = W_SortieRempl :" Intervention supprime."
			GOSUB 500
			
            	DELETE F.TPSDETAILSINTERV,W_Cle
		END ELSE
			* impossible car on n'est pas dans le traitement "Absences de l'aid !"	
		END

         END
      REPEAT

   END

   EXECUTE 'EFFACER-LISTE ':W_NomRequete

   IF W_AncCodeAbs = "" THEN

   	W_Temp='SSELECT TPSEXCEPTIONS AVEC CleDetailsIntervExcep = ""'

	W_Temp=W_Temp:' OR AVEC DateDebExcep > "':W_DateFinInterv:'"'
	IF W_CodeAide # "" THEN
      	W_Temp=W_Temp:' AND AVEC CodeAideExcep = "':W_CodeAide:'"'
   	END
   	IF W_CodeAidant # "" THEN
      	W_Temp=W_Temp:' AND AVEC CodeAidantExcep = "':W_CodeAidant:'"'
	END
   	IF W_TypeAct # "" THEN
      	W_Temp=W_Temp:' AND AVEC TypeActExcep = "':W_TypeAct:'"'
   	END
   	GOSUB 100
   END ELSE
	IF W_DateFinInterv # "" THEN
	* On ne touche qu'aux ventuelles exceptions postrieures a la date de fin d'absence.
		
	    W_Temp='SSELECT TPSEXCEPTIONS AVEC AbsExcept = "':W_AncCodeAbs:'"'

	    W_Temp=W_Temp:' AND AVEC DateDebExcep > "':W_DateFinInterv:'" OR AVEC CleDetailsIntervExcep = ""'
   	    GOSUB 100
	

	END ELSE
		IF W_Trait = "S" THEN
		* on a dj supprim les interventions correspondantes
		* Donc d'ventuelles exceptions sur ces interventions sont "orphelines"
			   W_Temp='SSELECT TPSEXCEPTIONS AVEC CleDetailsIntervExcep = ""'
			   GOSUB 100	
		END ELSE
		* on a prolong les dates de fin des interventions, pas d'incidences sur les exceptions.
		* ou bien on a modif la date de dbut d'absence = pas d'incidences sur les dates d'interventions ni donc sur les dates d'exceptions
		END
	END
   END

   IF W_CodeContrat # "" THEN

   	W_Temp='SSELECT TPSMOI AVEC DateMoi > "':W_DateFinInterv:'"'
   	W_Temp=W_Temp:' AND AVEC 4 = "':W_CodeContrat:'"'

   	EXECUTE W_Temp
	EXECUTE 'SAUVE-LISTE W_RequeteMoi'
  	EXECUTE "LISTE W_RequeteMoi" RETURNING MSGCODE

   	IF MSGCODE<1>#209 THEN

      	SELECT F.TPSMOI TO W_RequeteMoi

	      W_End = "FAUX"
      	LOOP
	         READNEXT W_Cle FROM W_RequeteMoi ELSE W_End = "VRAI"
      	WHILE W_End = "FAUX" DO
	         DELETE F.TPSMOI,W_Cle
      	REPEAT

	END 
   END

END ELSE
	* Traitement des absences de l'aid !
* les cls d'interventions  traiter sont en MV dans W_CodeAide => Super !
   CONVERT CHAR(252) TO CHAR(254) IN W_CodeAide
   i = 0
   LOOP
	i = i + 1
   WHILE W_CodeAide<i> # ""
	W_Cle = W_CodeAide<i>
	READ ENR_TPSDETAILSINTERV FROM F.TPSDETAILSINTERV,W_Cle THEN
        W_CleRempl = W_Cle
	W_FinOrigineRempl = ENR_TPSDETAILSINTERV<1>
         IF W_Trait="M" THEN
            	
               ENR_TPSDETAILSINTERV<1>=W_DateFinInterv

               IF ENR_TPSDETAILSINTERV<1> < W_Cle[11,5] THEN
		  W_DebParamRempl = W_Cle[11,5]
			  W_FinParamRempl = W_FinOrigineRempl
			  GOSUB 500
			  GOSUB 501
			W_SortieRempl = W_SortieRempl :" Intervention supprime."
                  DELETE F.TPSDETAILSINTERV,W_Cle
               END ELSE
		  IF W_FinOrigineRempl = "" OR W_FinOrigineRempl > ENR_TPSDETAILSINTERV<1> THEN
		    * je veux tre sur qu'on appelle le PR que si on a rduit l'interv, car il n'est fait que pour ca.
			W_DebParamRempl = ENR_TPSDETAILSINTERV<1> +1
			W_FinParamRempl = W_FinOrigineRempl
		   	GOSUB 500
			GOSUB 501
			W_SortieRempl = W_SortieRempl :" Nouvelle date de fin au ":W_DateFinInterv:"."
		   END
                  WRITE ENR_TPSDETAILSINTERV ON F.TPSDETAILSINTERV,W_Cle
               END

         END ELSE
		IF W_Trait = "S" THEN
			W_DebParamRempl = W_Cle[11,5]
			  W_FinParamRempl = W_FinOrigineRempl
			  GOSUB 500
			  GOSUB 501
			W_SortieRempl = W_SortieRempl :" Intervention supprime."
	            	DELETE F.TPSDETAILSINTERV,W_Cle
		END ELSE
			* Nb : dans W_DateFinInterv, on a la nouvelle date de DEBUT !!!
			IF W_DateFinInterv <= ENR_TPSDETAILSINTERV<1> OR ENR_TPSDETAILSINTERV<1> = "" THEN 
				WRITE ENR_TPSDETAILSINTERV ON F.TPSDETAILSINTERV, W_Cle[1,10]:W_DateFinInterv:W_Cle[16,11]
			END
			DELETE F.TPSDETAILSINTERV,W_Cle
		END

         END
	END
	
   REPEAT
   W_Temp='SSELECT TPSEXCEPTIONS '
   IF W_Trait = "S" THEN
	W_Temp=W_Temp:'AVEC CleDetailsIntervExcep = ""' 	
   END ELSE
	IF W_Trait = "D" THEN
		W_Temp=W_Temp:'AVEC CleDetailsIntervExcep = "" OR AVEC 1 = "':W_CodeAide<1>:']"'
		i = 1
	END ELSE
		W_Temp=W_Temp:'AVEC DateDebExcep > "':W_DateFinInterv:'" AND AVEC CleDetailsIntervExcep = ""'
		i = 0
	END
	
	LOOP
	i = i + 1
	WHILE W_CodeAide<i> # ""
		IF W_Trait # "S" THEN 
			W_Temp = W_Temp:' OR = "':W_CodeAide<i>:']"'
		END
	REPEAT
   END
   GOSUB 100

END

WRITE W_SortieRempl ON F.TPSTEMPO, "MAJDATEFININTERV":User

STOP

100
   EXECUTE W_Temp
	
   W_End = "FAUX"
   LOOP
         READNEXT W_Cle ELSE W_End = "VRAI"
   WHILE W_End = "FAUX" DO
	   IF W_Trait # "D" THEN
         	DELETE F.TPSEXCEPTIONS,W_Cle
	   END ELSE
		IF W_Cle[11,5] < W_DateFinInterv THEN
			DELETE F.TPSEXCEPTIONS,W_Cle
		END ELSE
			READ ENR_TPSEXCEPTIONS FROM F.TPSEXCEPTIONS, W_Cle THEN
				ENR_TPSEXCEPTIONS<1> =  ENR_TPSEXCEPTIONS<1>[1,10]:W_DateFinInterv:ENR_TPSEXCEPTIONS<1>[16,11]
				WRITE ENR_TPSEXCEPTIONS ON F.TPSEXCEPTIONS, W_Cle
			END
		END
	   END
   REPEAT
RETURN

500 
*PRINT "Va faire CALL :"
IF Origine = "ACTIV" THEN
	  IF NomRequeteRempl = "" THEN
		NomRequeteRempl = "REMPLRECUR":User:TIME()
		EXECUTE 'SELECT TPSREMPLACEAIDANT AVEC CodeAide = "':W_CodeAide:']" AND AVEC FinAbsAidant >= "':W_DateFinInterv:'" PAR DebAbsAidant' 
		EXECUTE 'SAUVE-LISTE ':NomRequeteRempl
	  END
END
W_ParamRempl<1> = W_CleRempl
W_ParamRempl<2> = W_DebParamRempl
W_ParamRempl<3> = W_FinParamRempl
W_ParamRempl<4> = NomRequeteRempl
W_ParamRempl<5> = User
*PRINT W_ParamRempl
CALL REMPLRECURSIF(RETURNVAL, W_ParamRempl,W_SortieRempl)
*PRINT W_SortieRempl
RETURN

501
W_SortieRempl = W_SortieRempl:CHAR(13):CHAR(10)
READ ENR_CIVILAIDANT FROM F.CIVILAIDANT, W_CleRempl[1,5] THEN
W_SortieRempl = W_SortieRempl:W_CleRempl[1,5]:" ":ENR_CIVILAIDANT<2>:" ":ENR_CIVILAIDANT<3>:" chez "
READ ENR_TPSPARAM FROM F.TPSPARAM, "1" ELSE
 ENR_TPSPARAM = ""
 ENR_TPSPARAM<20> = "2"
END
W_SortieRempl = W_SortieRempl :W_CleRempl[6,5]
READ ENR_CIVILAIDE FROM F.CIVILAIDE, W_CleRempl[6,5] THEN
	W_SortieRempl = W_SortieRempl :" ":ENR_CIVILAIDE<2>:" ":ENR_CIVILAIDE<3>:" "
END
BEGIN CASE
	CASE W_CleRempl[22,1] = "0"
		W_SortieRempl = W_SortieRempl :"le lundi "
	CASE W_CleRempl[22,1] = "1"
		W_SortieRempl = W_SortieRempl :"le mardi "
	CASE W_CleRempl[22,1] = "2"
		W_SortieRempl = W_SortieRempl :"le mercredi "
	CASE W_CleRempl[22,1] = "3"
		W_SortieRempl = W_SortieRempl :"le jeudi "
	CASE W_CleRempl[22,1] = "4"
		W_SortieRempl = W_SortieRempl :"le vendredi "
	CASE W_CleRempl[22,1] = "5"
		W_SortieRempl = W_SortieRempl :"le samedi "
	CASE W_CleRempl[22,1] = "6"
		W_SortieRempl = W_SortieRempl :"le dimanche "
END CASE
	W_HreDebARenvoyer = W_CleRempl[23,4]
	W_Decimales = W_HreDebARenvoyer[3,2]
	IF ENR_TPSPARAM<20> = "2" THEN
		W_Decimales = INT(W_Decimales * 0.6 + 1/2)"R%2"
	END
	W_SortieRempl = W_SortieRempl :"de ":W_HreDebARenvoyer[1,2]:"h":W_Decimales:" "
	W_HreFinARenvoyer = ENR_TPSDETAILSINTERV<2>
	IF LEN(W_HreFinARenvoyer) = 4 THEN
		
	END ELSE
		W_HreFinARenvoyer = "0":W_HreFinARenvoyer
	END
	W_Decimales = W_HreFinARenvoyer[3,2]
	IF ENR_TPSPARAM<20> = "2" THEN
		W_Decimales = INT(W_Decimales * 0.6 + 1/2)"R%2"
	END
	W_SortieRempl = W_SortieRempl :" ":W_HreFinARenvoyer[1,2]*1:"h":W_Decimales:" "
	IF W_FinOrigineRempl = "" THEN
		W_SortieRempl = W_SortieRempl :"depuis le ":OCONV(W_CleRempl[11,5],"D4/")
	END ELSE
		IF W_FinOrigineRempl = W_CleRempl[11,5] THEN
			W_SortieRempl = W_SortieRempl :"le ":OCONV(W_CleRempl[11,5],"D4/")
		END ELSE
			W_SortieRempl = W_SortieRempl :"du ":OCONV(W_CleRempl[11,5],"D4/"):" au ":OCONV(W_FinOrigineRempl,"D4/")
		END
	END


* Ci dessou : END du READ ENR_CIVILAIDANT
END
RETURN
