-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathabos_fonctions.php
332 lines (287 loc) · 9.66 KB
/
abos_fonctions.php
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/**
* Filtres, balises etc necessaires au calcul de la page
*
* @plugin Abonnements
* @copyright 2014
* @author cedric
* @licence GNU/GPL
* @package SPIP\Abos\Fonctions
*/
if (!defined('_ECRIRE_INC_VERSION')) {
return;
}
/**
* Afficher le rapport stats abos
* @return string
*/
function filtre_abos_reporting_decompte_dist() {
include_spip('abos/compter');
return abos_reporting_decompte(36); // sur 36 mois en affichage dans le site
}
function filtre_abos_reporting_parrainages() {
include_spip('abos/compter');
return abos_reporting_parrainages(6); // sur 12 mois en affichage dans le site
}
/**
* Compter les abonnes vus connectes sur le mois passe
* @return mixed
*/
function abos_compter_abonnes_recents_enligne() {
$from = date('Y-m-d H:i:s', strtotime('-1 month'));
//$now = date('Y-m-d H:i:s');
$n = sql_getfetsel(
'count(A.id_auteur) as n',
'spip_abonnements as A JOIN spip_auteurs as AU ON AU.id_auteur=A.id_auteur',
'A.id_auteur>0 AND AU.en_ligne>' . sql_quote($from) . ' AND A.statut=' . sql_quote('ok')
);
return $n;
}
/**
* Traduire le combo statut+date echeance en une chaine de langue
* @param string $statut
* @param string $date_echeance
* @return string
*/
function abos_statut_en_clair($statut, $date_echeance) {
static $now = null;
if (is_null($now)) {
$now = date('Y-m-d H:i:s');
}
if ($statut == 'resilie') {
return 'abonnement:info_statut_resilie';
} elseif ($statut == 'ok') {
if ($date_echeance < $now) {
return 'abonnement:info_statut_impaye';
}
return 'abonnement:info_statut_ok';
}
if ($statut == 'prepa') {
return 'abonnement:info_statut_prepa';
}
return 'abonnement:info_statut_erreur';
}
/**
* Traduit la duree d'abonnement en info lisible, sous forme de periode
* tous les x mois, tous les ans...
* @param $duree
* @return mixed|string
*/
function abos_periode_en_clair($periodicite) {
$nb = intval($periodicite);
$duree = trim(preg_replace(',^\d+\s+,', '', $periodicite));
$duree = ($nb == 1 ? _T('abooffre:periodicite_' . $duree) : _T('abooffre:tous_les_nb_' . $duree, ['nb' => $nb]));
return $duree;
}
/**
* Traduit la duree d'abonnement en info lisible, sous forme de durée
* 1 mois, 2 mois, 1 an, 2 ans...
* @param $duree
* @return mixed|string
*/
function abos_duree_en_clair($duree) {
$nb = intval($duree);
$duree = trim(preg_replace(',^\d+\s+,', '', $duree));
$duree = ($nb == 1 ? _T('abooffre:1_' . $duree) : _T('abooffre:nb_' . $duree, ['nb' => $nb]));
return $duree;
}
/**
* Affiche le nombre de mois depuis lequel un abonnement court
*
* @param string $date_debut
* @return string
*/
function abos_duree_abonnement($date_debut) {
if (!$date_debut) {
return;
}
$decal = date('U') - date('U', strtotime($date_debut));
if ($decal < 0) {
return '';
}
$mois = ceil($decal / (3600 * 24 * 365 / 12));
return $mois;
}
/**
* Lister les transactions liees a un abonnement
* @param int $id_abonnement
* @param int $id_commande
* @return array
*/
function abos_liste_transactions($id_abonnement, $id_commande = 0) {
$ids = sql_allfetsel('id_objet', 'spip_abonnements_liens', 'id_abonnement=' . intval($id_abonnement) . ' AND objet=' . sql_quote('transaction'));
$ids = array_column($ids, 'id_objet');
if ($id_commande) {
$id2s = sql_allfetsel('id_transaction', 'spip_transactions', 'id_commande=' . intval($id_commande));
$id2s = array_column($id2s, 'id_transaction');
$ids = array_merge($id2s, $ids);
}
if (!$ids) {
$ids = [0];
}
return $ids;
}
/**
* Traduit les credits d'un abonnement en info lisible
* Les credits sont definis par des mots cles (un mot cle = un type de credits)
* usage facultatif
* @param string|array $credits
* @return string
*/
function abos_credits_en_clair($credits) {
static $lib_mot = [];
if (!is_array($credits)) {
$credits = unserialize($credits);
if ($credits === false) {
return '';
}
}
$out = [];
foreach ($credits as $id_mot => $nb) {
if (!isset($lib_mot[$id_mot])) {
if ($row = sql_fetsel('titre', 'spip_mots', 'id_mot=' . intval($id_mot))) {
$lib_mot[$id_mot] = '<abbr title="' . attribut_html(typo($row['titre'])) . '">' . substr($row['titre'], 0, 1) . '</abbr>';
}
}
if ($nb) {
$out[] = $lib_mot[$id_mot] . ($nb === 'inf' ? '' : "($nb)");
}
}
return typo(implode(', ', $out));
}
function abos_couper_abbr($texte, $longueur = 20) {
$t = '<abbr title="' . attribut_html($texte) . '">' . couper($texte, $longueur) . '</abbr>';
return $t;
}
/**
* @param $idb
* @param $boucles
* @param $crit
*/
function critere_somme_echeance_dist($idb, &$boucles, $crit) {
$boucle = &$boucles[$idb];
$boucle->select[] = 'sum(prix_echeance) as somme_echeance';
}
/**
* @param $idb
* @param $boucles
* @param $crit
*/
function critere_abonement_en_cours_dist($idb, &$boucles, $crit) {
$boucle = &$boucles[$idb];
$t = $boucle->id_table;
$_date_ref = "date('Y-m-d H:i:00',\$_SERVER['REQUEST_TIME'])";
$boucle->where[] = "'$t.date_debut<'.sql_quote($_date_ref).' AND ($t.date_fin<$t.date_debut OR $t.date_fin>'.sql_quote($_date_ref).')'";
}
// Si on est hors d'une boucle {recherche}, ne pas "prendre" cette balise
// http://doc.spip.org/@balise_POINTS_dist
function balise_SOMME_ECHEANCE_dist($p) {
return rindex_pile($p, 'somme_echeance', 'somme_echeance');
}
/**
* <BOUCLE(ABONNEMENTS)>
* si il n'y a pas de critere statut, le boucle ABONNEMENTS filtre sur statut=ok
* ET date_fin valide (date<date_fin ou date_fin null ou date_fin<date_debut)
*
* @param $id_boucle
* @param $boucles
* @return string
*/
function boucle_ABONNEMENTS_dist($id_boucle, &$boucles) {
$boucle = &$boucles[$id_boucle];
$id_table = $boucle->id_table;
$boucle->from[$id_table] = 'spip_abonnements';
$mstatut = $id_table . '.statut';
// conditions de statut
instituer_boucle($boucle);
// Restreindre aux abonnements dont la date de fin n'est pas passee
if (!$boucle->modificateur['criteres']['statut']) {
$_date_ref = "date('Y-m-d H:i:00',\$_SERVER['REQUEST_TIME'])";
$boucle->where[] = ["'OR'", ["'>'", "'$id_table" . ".date_fin'", "sql_quote($_date_ref)"], ["'OR'", "'$id_table" . ".date_fin IS NULL'", ["'<'", "'$id_table" . ".date_fin'", "'$id_table" . ".date_debut'"]]];
}
return calculer_boucle($id_boucle, $boucles);
}
function critere_ABONNEMENTS_parrain_dist($idb, &$boucles, $crit) {
$boucle = &$boucles[$idb];
$t = $boucle->id_table;
$_parrain = !isset($crit->param[0][0]) ? "_request('parrain')" : calculer_liste([$crit->param[0][0]], [], $boucles, $boucles[$idb]->id_parent);
$where = "'$t.id_abonnement IN (SELECT DISTINCT LLLL.id_abonnement FROM spip_abonnements_liens AS LLLL JOIN spip_transactions AS TTTT ON (TTTT.id_transaction = LLLL.id_objet AND LLLL.objet = \'transaction\') WHERE '.sql_in('TTTT.parrain',is_array($_parrain)?$_parrain:array($_parrain)).')'";
if ($crit->cond) {
$where = "(is_array($_parrain)?count(array_filter($_parrain)):$_parrain)?$where:''";
}
$boucle->where[] = $where;
}
/**
* Trouver les auteurs qui ont payes une transaction mais n'ont aucun abonnement
* @return array
*/
function abos_auteur_sans_abonnement() {
include_spip('base/abstract_sql');
$hasabo = sql_allfetsel('id_auteur', 'spip_abonnements', "statut IN ('ok','resilie')");
$hasabo = array_column($hasabo, 'id_auteur');
$hastrans = sql_allfetsel('id_auteur', 'spip_transactions', "statut='ok' AND id_auteur>0 AND " . sql_in('id_auteur', $hasabo, 'NOT'));
$hastrans = array_column($hastrans, 'id_auteur');
if (!$hastrans) {
$hastrans = [0];
}
return $hastrans;
}
/**
* Trouver les auteurs qui ont plusieurs abonnements en cours
* @return array
*/
function abos_auteur_plusieurs_abonnements() {
include_spip('base/abstract_sql');
$hasabo = sql_allfetsel('id_auteur, count(id_auteur) AS N', 'spip_abonnements', "statut IN ('ok')", 'id_auteur', '', '', 'N>1');
$hasabo = array_column($hasabo, 'id_auteur');
if (!$hasabo) {
$hasabo = [0];
}
return $hasabo;
}
/**
* Calculer un historique du CA genere par cette offre d'abonnement
* @param $id_abo_offre
* @return string
*/
function abos_historique_encaissements($id_abo_offres) {
if (!is_array($id_abo_offres)) {
$id_abo_offres = [$id_abo_offres];
}
$id_commandes = sql_allfetsel('A.id_commande', 'spip_abonnements as A', sql_in('A.id_abo_offre', $id_abo_offres));
$id_commandes = array_column($id_commandes, 'id_commande');
$rows = sql_allfetsel(
'count(T.id_transaction) as nombre_mensuel, sum(T.montant_ht) as montant_mensuel_ht,sum(T.montant) as montant_mensuel,T.date_paiement',
'spip_transactions AS T',
"T.id_commande>0 AND T.statut='ok' AND " . sql_in('T.id_commande', $id_commandes),
"DATE_FORMAT(T.date_paiement,'%Y-%m')",
'T.date_paiement DESC'
);
$out = '';
foreach ($rows as $row) {
$mois = affdate_mois_annee($row['date_paiement']);
$montant = bank_affiche_montant($row['montant_mensuel']);
$montant_ht = bank_affiche_montant($row['montant_mensuel_ht']);
$nb = $row['nombre_mensuel'];
$out .= "<tr><td>$mois</td><td class='numeric'>$nb</td><td class='numeric'>$montant_ht</td><td class='numeric'>$montant</td></tr>\n";
}
if ($out) {
$out = "<table class='spip'>"
. "<thead><tr class='row_first'>"
. '<th>' . spip_ucfirst(_T('date_un_mois')) . '</th>'
. "<th class='numeric'>" . _T('abonnement:label_nombre') . '</th>'
. "<th class='numeric'>" . _T('abonnement:label_montant_ht') . '</th>'
. "<th class='numeric'>" . _T('abonnement:label_montant') . '</th>'
. '</tr></thead>'
. "<tbody>$out</tbody></table>";
}
return $out;
}
function abos_historique_encaissements_periode($duree) {
$id_abo_offres = sql_allfetsel('id_abo_offre', 'spip_abo_offres', 'duree=' . sql_quote($duree));
if (!$id_abo_offres) {
return '';
}
$id_abo_offres = array_column($id_abo_offres, 'id_abo_offre');
return abos_historique_encaissements($id_abo_offres);
}