// template routine Java
package routines;

import java.util.Date;
import java.util.Calendar;

/*
 * user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
 * it must be before the "{talendTypes}" key.
 * 
 * 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
 * long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
 * Short
 * 
 * 3. {Category} define a category for the Function. it is required. its value is user-defined .
 * 
 * 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
 * 
 * <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
 * Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
 * added. you can have many parameters for the Function.
 * 
 * 5. {example} gives a example for the Function. it is optional.
 */
public class calculDates {

    /**
     * DateDebut: retourne date de debut
     * 
     * 
     * {talendTypes} String
     * 
     * {Category} User Defined
     * 
     * {param} date(Datedebut)
     * 
     * {example} helloExemple("world") # hello world !.
     */
    public static Date Datedebut() {
    	String Today=TalendDate.getDate("dd/MM/yyyy");

    	String Annee = TalendDate.formatDate("yyyy",TalendDate.parseDate("dd/MM/yyyy",Today));
    	String Semaine = TalendDate.formatDate("ww",TalendDate.parseDate("dd/MM/yyyy",Today));

    	//aujourd'hui, je calcul les statistiques de la semaine dernière
    	Integer SemaineStat = Integer.parseInt(Semaine)-1;
    	Semaine = String.valueOf(SemaineStat);

    	String DebutAnnee = "01/01/"+Annee;
    	Date Lundi = TalendDate.parseDate("dd/MM/yyyy",DebutAnnee);
    	String JourDebutAnnee = TalendDate.formatDate("EEEE",TalendDate.parseDate("dd/MM/yyyy",DebutAnnee));


    	if (JourDebutAnnee.equals("mardi")) Lundi = TalendDate.addDate(Lundi,-1,"dd");
    	if (JourDebutAnnee.equals("mercredi")) Lundi = TalendDate.addDate(Lundi,-2,"dd");
    	if (JourDebutAnnee.equals("jeudi")) Lundi = TalendDate.addDate(Lundi,-3,"dd");
    	if (JourDebutAnnee.equals("vendredi")) Lundi = TalendDate.addDate(Lundi,3,"dd");
    	if (JourDebutAnnee.equals("samedi")) Lundi = TalendDate.addDate(Lundi,2,"dd");
    	if (JourDebutAnnee.equals("dimanche")) Lundi = TalendDate.addDate(Lundi,1,"dd");

    	Date DateDebut = TalendDate.addDate(Lundi,((SemaineStat-1)*7),"dd");
    	Date DateFin = TalendDate.addDate(Lundi,((SemaineStat-1)*7)+6,"dd");

    	// Les statistiques sont pris sur une intervale de 12 semaines avant semaine de la statistique (84 jours).

    	DateDebut = TalendDate.addDate(DateDebut,-84,"dd");
    	DateFin = TalendDate.addDate(DateFin,-7,"dd");

    	// j'utilise after et before, c'est pour cela que je déduit 1 de DateDebut et ajoute 1 à DateFin

    	DateDebut = TalendDate.addDate(DateDebut,-1,"dd");
    	DateFin = TalendDate.addDate(DateFin,1,"dd");


    	return DateDebut;

    }
    
    
    /**
     * DateFin: retourne date de fin
     * 
     * 
     * {talendTypes} String
     * 
     * {Category} User Defined
     * 
     * {param} date(Datefin)
     * 
     * 
     */
    public static Date Datefin() {
    	String Today=TalendDate.getDate("dd/MM/yyyy");

    	String Annee = TalendDate.formatDate("yyyy",TalendDate.parseDate("dd/MM/yyyy",Today));
    	String Semaine = TalendDate.formatDate("ww",TalendDate.parseDate("dd/MM/yyyy",Today));

    	//aujourd'hui, je calcul les statistiques de la semaine dernière
    	Integer SemaineStat = Integer.parseInt(Semaine)-1;
    	Semaine = String.valueOf(SemaineStat);

    	String DebutAnnee = "01/01/"+Annee;
    	Date Lundi = TalendDate.parseDate("dd/MM/yyyy",DebutAnnee);
    	String JourDebutAnnee = TalendDate.formatDate("EEEE",TalendDate.parseDate("dd/MM/yyyy",DebutAnnee));


    	if (JourDebutAnnee.equals("mardi")) Lundi = TalendDate.addDate(Lundi,-1,"dd");
    	if (JourDebutAnnee.equals("mercredi")) Lundi = TalendDate.addDate(Lundi,-2,"dd");
    	if (JourDebutAnnee.equals("jeudi")) Lundi = TalendDate.addDate(Lundi,-3,"dd");
    	if (JourDebutAnnee.equals("vendredi")) Lundi = TalendDate.addDate(Lundi,3,"dd");
    	if (JourDebutAnnee.equals("samedi")) Lundi = TalendDate.addDate(Lundi,2,"dd");
    	if (JourDebutAnnee.equals("dimanche")) Lundi = TalendDate.addDate(Lundi,1,"dd");

    	Date DateDebut = TalendDate.addDate(Lundi,((SemaineStat-1)*7),"dd");
    	Date DateFin = TalendDate.addDate(Lundi,((SemaineStat-1)*7)+6,"dd");

    	// Les statistiques sont pris sur une intervale de 12 semaines avant semaine de la statistique (84 jours).

    	DateDebut = TalendDate.addDate(DateDebut,-84,"dd");
    	DateFin = TalendDate.addDate(DateFin,-7,"dd");

    	// j'utilise after et before, c'est pour cela que je déduit 1 de DateDebut et ajoute 1 à DateFin

    	DateDebut = TalendDate.addDate(DateDebut,-1,"dd");
    	DateFin = TalendDate.addDate(DateFin,1,"dd");


    	return DateFin;

    }
    /**
     * HP: retourne HP en minutes
     * {talendTypes} Long
     * 
     * {Category} User Defined
     * 
     * {param} date(HeureDebut, HeureFin)
     *  
     * 
     */

   
	    public static long HP(Date HeureDebut, Date HeureFin) {
	    	
	    	long HeuresPrestees = HeureFin.getTime() - HeureDebut.getTime();			
	    	HeuresPrestees /= 1000;
	    	HeuresPrestees /= 60; 	
	    	
	    	//retourne les HP en minutes
	    	
	        return HeuresPrestees;
	    }
	    

 }

