76 |
ilm |
1 |
/*
|
|
|
2 |
* Créé le 30 mai 2012
|
|
|
3 |
*/
|
|
|
4 |
package org.openconcerto.modules.subscription;
|
|
|
5 |
|
|
|
6 |
import java.util.Calendar;
|
|
|
7 |
import java.util.Date;
|
|
|
8 |
import java.util.HashMap;
|
|
|
9 |
import java.util.List;
|
|
|
10 |
import java.util.Map;
|
|
|
11 |
|
|
|
12 |
import org.openconcerto.sql.Configuration;
|
96 |
ilm |
13 |
import org.openconcerto.sql.model.AliasedTable;
|
76 |
ilm |
14 |
import org.openconcerto.sql.model.SQLRow;
|
|
|
15 |
import org.openconcerto.sql.model.SQLRowListRSH;
|
|
|
16 |
import org.openconcerto.sql.model.SQLRowValues;
|
|
|
17 |
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
|
|
|
18 |
import org.openconcerto.sql.model.SQLSelect;
|
|
|
19 |
import org.openconcerto.sql.model.SQLSelectJoin;
|
|
|
20 |
import org.openconcerto.sql.model.SQLTable;
|
|
|
21 |
import org.openconcerto.sql.model.TableRef;
|
|
|
22 |
import org.openconcerto.sql.model.Where;
|
|
|
23 |
import org.openconcerto.utils.cc.ITransformer;
|
|
|
24 |
|
|
|
25 |
public class SubscriptionChecker {
|
|
|
26 |
|
|
|
27 |
private final String type;
|
|
|
28 |
private final SQLTable table;
|
|
|
29 |
|
|
|
30 |
public SubscriptionChecker(SQLTable table) {
|
|
|
31 |
this.table = table;
|
|
|
32 |
|
|
|
33 |
if (this.table.getName().equalsIgnoreCase("SAISIE_VENTE_FACTURE")) {
|
|
|
34 |
this.type = "FACTURE";
|
|
|
35 |
} else if (this.table.getName().equalsIgnoreCase("DEVIS")) {
|
|
|
36 |
this.type = "DEVIS";
|
|
|
37 |
} else {
|
|
|
38 |
this.type = "COMMANDE";
|
|
|
39 |
}
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
public Map<SQLRow, Calendar> check() {
|
|
|
43 |
|
|
|
44 |
// Fectcher pour récupérer la derniere facture ou devis de chaque abonnement
|
|
|
45 |
final SQLRowValues vals = new SQLRowValues(this.table);
|
|
|
46 |
vals.put(this.table.getKey().getName(), null);
|
|
|
47 |
vals.put("NUMERO", null);
|
|
|
48 |
vals.put("DATE", null);
|
|
|
49 |
vals.put("ID_ABONNEMENT", null);
|
169 |
ilm |
50 |
final boolean containsPrev = this.table.contains("PREVISIONNELLE");
|
|
|
51 |
if (containsPrev) {
|
|
|
52 |
vals.put("PREVISIONNELLE", null);
|
|
|
53 |
}
|
152 |
ilm |
54 |
vals.put("CREATION_AUTO_VALIDER", null);
|
76 |
ilm |
55 |
|
|
|
56 |
final SQLRowValuesListFetcher fetcher = new SQLRowValuesListFetcher(vals);
|
|
|
57 |
fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
|
|
|
58 |
@Override
|
|
|
59 |
public SQLSelect transformChecked(SQLSelect sel) {
|
|
|
60 |
|
|
|
61 |
TableRef tableFAlias = sel.getAlias(SubscriptionChecker.this.table);
|
96 |
ilm |
62 |
SQLSelectJoin join = sel.addJoin("RIGHT", new AliasedTable(SubscriptionChecker.this.table, "f2"), new Where(tableFAlias.getField("ARCHIVE"), "=", 0));
|
76 |
ilm |
63 |
Where w = new Where(join.getJoinedTable().getField("DATE"), "<=", tableFAlias.getField("DATE"));
|
|
|
64 |
w = w.and(new Where(join.getJoinedTable().getField("ID_ABONNEMENT"), "=", tableFAlias.getField("ID_ABONNEMENT")));
|
|
|
65 |
join.setWhere(w);
|
|
|
66 |
sel.setWhere(new Where(tableFAlias.getField("ID_ABONNEMENT"), "IS NOT", (Object) null));
|
|
|
67 |
|
|
|
68 |
System.err.println(sel.asString());
|
|
|
69 |
return sel;
|
|
|
70 |
}
|
|
|
71 |
});
|
|
|
72 |
|
|
|
73 |
List<SQLRowValues> list = fetcher.fetch();
|
|
|
74 |
|
|
|
75 |
Map<Number, SQLRowValues> map = new HashMap<Number, SQLRowValues>();
|
|
|
76 |
for (SQLRowValues sqlRow : list) {
|
|
|
77 |
map.put(sqlRow.getInt("ID_ABONNEMENT"), sqlRow);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
// Verification du renouvellement des abonnements
|
169 |
ilm |
81 |
SQLSelect selAb = new SQLSelect();
|
76 |
ilm |
82 |
SQLTable tableAb = Configuration.getInstance().getRoot().findTable("ABONNEMENT");
|
|
|
83 |
selAb.addSelectStar(tableAb);
|
|
|
84 |
List<SQLRow> rows = SQLRowListRSH.execute(selAb);
|
|
|
85 |
|
|
|
86 |
// Liste des abonnements associés à la prochaine date de renouvellement
|
|
|
87 |
Map<SQLRow, Calendar> aboFactureRenouvel = new HashMap<SQLRow, Calendar>();
|
|
|
88 |
|
|
|
89 |
Date toDay = new Date();
|
|
|
90 |
|
|
|
91 |
for (SQLRow sqlRow : rows) {
|
|
|
92 |
if (sqlRow.getBoolean("CREATE_" + this.type)) {
|
|
|
93 |
int nbMois = sqlRow.getInt("NB_MOIS_" + this.type);
|
|
|
94 |
|
|
|
95 |
// On recupere la derniere date de l'abonnement soit la date de debut soit la date
|
|
|
96 |
// de la derniere facture
|
|
|
97 |
Calendar calStartFact = sqlRow.getDate("DATE_DEBUT_" + this.type);
|
|
|
98 |
SQLRowValues rowFact = map.get(sqlRow.getID());
|
|
|
99 |
if (rowFact != null) {
|
169 |
ilm |
100 |
if (!containsPrev || (rowFact.getObject("PREVISIONNELLE") != null && !rowFact.getBoolean("PREVISIONNELLE"))) {
|
76 |
ilm |
101 |
calStartFact = rowFact.getDate("DATE");
|
|
|
102 |
} else {
|
|
|
103 |
// Si le dernier element de l'abonnement n'a pas été validé on ne crée pas
|
|
|
104 |
// le prochain
|
|
|
105 |
continue;
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
calStartFact.add(Calendar.MONTH, nbMois);
|
|
|
110 |
if (toDay.after(calStartFact.getTime())) {
|
|
|
111 |
|
|
|
112 |
aboFactureRenouvel.put(sqlRow, calStartFact);
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
return aboFactureRenouvel;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
}
|