1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| public ArrayList<ArrayList<Object>> select_date(String date_debut, String date_fin, int id_operations, int id_filtres, int id_sous_filtres, int id_fournisseur, String mode_date, boolean bool_saison, Datetime datetime_saison) {
ArrayList<ArrayList<Object>> requete = new ArrayList<ArrayList<Object>>();
String champ_date="";
String champ_fournisseur=(id_fournisseur!=0)?"AND `id_fournisseur`="+id_fournisseur:"";
String champ_operation=(id_operations!=0)?"AND `id_type_paiement`="+id_operations:"";
String champ_filtre=(id_filtres!=0)?"AND (CASE WHEN aa.id_filtre IS NULL THEN od.id_filtre ELSE aa.id_filtre END)="+id_filtres:"";
String champ_sous_filtre=(id_sous_filtres!=0)?"AND (CASE WHEN aa.id_sous_filtre IS NULL THEN od.id_sous_filtre ELSE aa.id_sous_filtre END)="+id_sous_filtres:"";
String saison="", group_by="", frais_fixe="", frais_non_fixe="", nom_filtre="", jointure_od="", jointure_aa="";
if(bool_saison){
champ_date="od.date,";
saison=",CASE WHEN MONTH(od.date) > "+(datetime_saison.getMois())+" THEN CONCAT(YEAR(od.date), '-', YEAR(od.date) + 1) " +
"WHEN MONTH(od.date) = "+(datetime_saison.getMois())+" AND DAY(od.date)>= "+(datetime_saison.getJour_du_mois())+" THEN CONCAT(YEAR(od.date), '-', YEAR(od.date) + 1) " +
"ELSE CONCAT(YEAR(od.date) - 1, '-', YEAR(od.date)) END AS Saison";
group_by= "GROUP BY Saison";
}
else if(mode_date.equals("sansgroupby")){
group_by="";
jointure_od="LEFT JOIN filtres od_ft ON od_ft.id=od.id_filtre ";
jointure_aa="LEFT JOIN filtres aa_ft ON aa_ft.id=aa.id_filtre ";
if(id_filtres!=0 || id_sous_filtres!=0){
frais_fixe=",SUM(CASE WHEN od.id_achat_articles<>0 THEN (CASE WHEN aa_ft.frais_fixe=1 THEN aa.montant ELSE 0 END) ELSE (CASE WHEN od_ft.frais_fixe=1 THEN od.debit ELSE 0 END) END) AS frais_fixe,";
frais_non_fixe="SUM(CASE WHEN od.id_achat_articles<>0 THEN (CASE WHEN aa_ft.frais_fixe=0 THEN aa.montant ELSE 0 END) ELSE (CASE WHEN od_ft.frais_fixe=0 THEN od.debit ELSE 0 END) END) AS frais_non_fixe";
}
else{
frais_fixe=",SUM(CASE WHEN od.id_achat_articles<>0 THEN od.montant_total ELSE (CASE WHEN od_ft.frais_fixe=1 THEN od.montant_total ELSE 0 END) END) AS frais_fixe,";
frais_non_fixe="SUM(CASE WHEN od_ft.frais_fixe=0 THEN od.montant_total ELSE 0 END) AS frais_non_fixe";
}
}
else if(mode_date.equals("groupbyFiltre")){
champ_date="od.date,";
jointure_od="LEFT JOIN filtres od_ft ON od_ft.id=od.id_filtre ";
jointure_aa="LEFT JOIN filtres aa_ft ON aa_ft.id=aa.id_filtre ";
frais_fixe=",SUM(CASE WHEN od.id_achat_articles<>0 THEN (CASE WHEN aa_ft.frais_fixe=1 THEN aa.montant ELSE 0 END) ELSE (CASE WHEN od_ft.frais_fixe=1 THEN od.debit ELSE 0 END) END) AS frais_fixe,";
frais_non_fixe="SUM(CASE WHEN od.id_achat_articles<>0 THEN (CASE WHEN aa_ft.frais_fixe=0 THEN aa.montant ELSE 0 END) ELSE (CASE WHEN od_ft.frais_fixe=0 THEN od.debit ELSE 0 END) END) AS frais_non_fixe";
nom_filtre=",(CASE WHEN od.id_achat_articles<>0 THEN aa_ft.nom ELSE od_ft.nom END) AS nom_filtre";
group_by="GROUP BY nom_filtre";// Groupemement par Filtre
}
else if(mode_date.equals("groupbySousFiltre")){
champ_date="od.date,";
jointure_od="LEFT JOIN filtres od_ft ON od_ft.id=od.id_filtre LEFT JOIN filtres_sous_filtres od_sft ON od_sft.id=od.id_sous_filtre ";
jointure_aa="LEFT JOIN filtres aa_ft ON aa_ft.id=aa.id_filtre LEFT JOIN filtres_sous_filtres aa_sft ON aa_sft.id=aa.id_sous_filtre ";
frais_fixe=",SUM(CASE WHEN od.id_achat_articles<>0 THEN (CASE WHEN aa_ft.frais_fixe=1 THEN aa.montant ELSE 0 END) ELSE (CASE WHEN od_ft.frais_fixe=1 THEN od.debit ELSE 0 END) END) AS frais_fixe,";
frais_non_fixe="SUM(CASE WHEN od.id_achat_articles<>0 THEN (CASE WHEN aa_ft.frais_fixe=0 THEN aa.montant ELSE 0 END) ELSE (CASE WHEN od_ft.frais_fixe=0 THEN od.debit ELSE 0 END) END) AS frais_non_fixe";
nom_filtre=",(CASE WHEN (CASE WHEN od.id_achat_articles<>0 THEN aa_sft.nom ELSE od_sft.nom END) IS NULL THEN (CASE WHEN od.id_achat_articles<>0 THEN aa_ft.nom ELSE od_ft.nom END) ELSE (CASE WHEN od.id_achat_articles<>0 THEN aa_sft.nom ELSE od_sft.nom END) END) AS nom_sousfiltre";
group_by="GROUP BY nom_sousfiltre";// Groupemement par Sous-Filtre
}
else if(mode_date.equals("groupbyFournisseurs")){
champ_date="od.date,";
jointure_od="LEFT JOIN filtres od_ft ON od_ft.id=od.id_filtre ";
jointure_aa="LEFT JOIN filtres aa_ft ON aa_ft.id=aa.id_filtre LEFT JOIN fournisseurs fs ON fs.id=od.id_fournisseur ";
frais_fixe=",SUM(CASE WHEN od.id_achat_articles<>0 THEN (CASE WHEN aa_ft.frais_fixe=1 THEN aa.montant ELSE 0 END) ELSE (CASE WHEN od_ft.frais_fixe=1 THEN od.debit ELSE 0 END) END) AS frais_fixe,";
frais_non_fixe="SUM(CASE WHEN od.id_achat_articles<>0 THEN (CASE WHEN aa_ft.frais_fixe=0 THEN aa.montant ELSE 0 END) ELSE (CASE WHEN od_ft.frais_fixe=0 THEN od.debit ELSE 0 END) END) AS frais_non_fixe";
nom_filtre=",fs.nom AS nom_fournisseurs";
group_by="GROUP BY nom_fournisseurs";// Groupemement par Fournisseurs
}
else{
champ_date="od.date,";
group_by="GROUP BY "+mode_date;
}
String requete_sql=(id_filtres!=0 || id_sous_filtres!=0 || mode_date.equals("groupbyFiltre") || mode_date.equals("groupbySousFiltre") || mode_date.equals("groupbyFournisseurs"))?
//avec filtre
"SELECT "+champ_date+" COUNT(od.id) AS nombre, " +
"SUM(CASE WHEN od.id_achat_articles<>0 THEN aa.montant ELSE od.debit END) AS depense, " +
"SUM(CASE WHEN od.id_type_paiement = 1 THEN (CASE WHEN od.id_achat_articles<>0 THEN aa.montant ELSE od.debit END) ELSE 0 END) AS depense_liquide "+saison+" "+frais_fixe+" "+frais_non_fixe+" "+nom_filtre+" " +
"FROM operation_depenses od " +
"LEFT JOIN achat_articles aa ON aa.id_achat=od.id_achat_articles " +
" "+jointure_od+" "+jointure_aa+" " +
"WHERE (od.date BETWEEN '"+date_debut+"' AND '"+date_fin+"') "+champ_fournisseur+" "+champ_operation+" "+champ_filtre+" "+champ_sous_filtre+" " +
""+group_by+"":
//aucun filtre
"SELECT "+champ_date+" COUNT(od.id) AS nombre, " +
"SUM(od.montant_total) AS depense, " +
"SUM(CASE WHEN od.id_type_paiement = 1 THEN od.montant_total ELSE 0 END) AS depense_liquide "+saison+" "+frais_fixe+" "+frais_non_fixe+" " +
"FROM operation_depenses od " +
" "+jointure_od+" " +
"WHERE (od.date BETWEEN '"+date_debut+"' AND '"+date_fin+"') "+champ_fournisseur+" "+champ_operation+" " +
""+group_by+"";
connect= JdbcConnector.getInstance();
try {
try {
prepare = this.connect.prepareStatement(requete_sql);
System.out.println("//////////////REQUETE/////////////");
System.out.println(requete_sql);
System.out.println("//////////////FIN REQUETE/////////////");
//On exécute la requête
resultat = prepare.executeQuery();
int i=0;
while(resultat.next()){
requete.add(i, new ArrayList<Object>());
if(mode_date.equals("sansgroupby")){
requete.get(i).add(0, "");
}
else{
requete.get(i).add(0, resultat.getString("od.date"));
}
requete.get(i).add(1, resultat.getInt("nombre"));
requete.get(i).add(2, resultat.getFloat("depense"));
requete.get(i).add(3, resultat.getFloat("depense_liquide"));
if(bool_saison){
requete.get(i).add(4, resultat.getString("Saison"));
}
else if(mode_date.equals("sansgroupby") || mode_date.equals("groupbyFiltre") || mode_date.equals("groupbySousFiltre") || mode_date.equals("groupbyFournisseurs")){
requete.get(i).add(4, resultat.getFloat("frais_fixe"));
requete.get(i).add(5, resultat.getFloat("frais_non_fixe"));
if(mode_date.equals("groupbyFiltre")){
requete.get(i).add(6, resultat.getString("nom_filtre"));
}
if(mode_date.equals("groupbySousFiltre")){
requete.get(i).add(6, resultat.getString("nom_sousfiltre"));
}
if(mode_date.equals("groupbyFournisseurs")){
requete.get(i).add(6, resultat.getString("nom_fournisseurs"));
}
}
i++;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (resultat != null) {
resultat.close();
}
if (prepare != null) {
prepare.close();
}
if (connect != null) {
connect.close();
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return requete;
} |
Partager