1 | <?php |
---|
2 | |
---|
3 | /* * *************************************************************************\ |
---|
4 | * Associaspip, extension de SPIP pour gestion d'associations * |
---|
5 | * * |
---|
6 | * Copyright (c) 2007 Bernard Blazin & François de Montlivault (V1) * |
---|
7 | * Copyright (c) 2010-2011 Emmanuel Saint-James & Jeannot Lapin (V2) * |
---|
8 | * Ecrit par Marcel BOLLA en 01/2012 * |
---|
9 | * * |
---|
10 | * Ce programme est un logiciel libre distribue sous licence GNU/GPL. * |
---|
11 | * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. * |
---|
12 | \************************************************************************** */ |
---|
13 | |
---|
14 | if (!defined('_ECRIRE_INC_VERSION')) |
---|
15 | return; |
---|
16 | |
---|
17 | // Export du Compte de Resultat en .tsv ou .tab |
---|
18 | function exec_export_compteresultats_tsv() { |
---|
19 | if (!autoriser('associer', 'export_compteresultats')) { |
---|
20 | include_spip('inc/minipres'); |
---|
21 | echo minipres(); |
---|
22 | } else { |
---|
23 | include_spip('inc/charsets'); |
---|
24 | include_spip('inc/association_plan_comptable'); |
---|
25 | $tsv = new TSV(_request('var')); |
---|
26 | $tsv->EnTete(); |
---|
27 | foreach (array('charges', 'produits', 'contributions_volontaires') as $key) { |
---|
28 | $tsv->LesEcritures($key); |
---|
29 | } |
---|
30 | $tsv->Enregistre(); |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | /** |
---|
35 | * Utilisation d'une classe tres tres tres simple !!! |
---|
36 | */ |
---|
37 | class TSV { |
---|
38 | |
---|
39 | var $exercice; |
---|
40 | var $join; |
---|
41 | var $sel; |
---|
42 | var $where; |
---|
43 | var $having; |
---|
44 | var $order; |
---|
45 | var $out; |
---|
46 | |
---|
47 | function __construct($var) { |
---|
48 | $tableau = unserialize(rawurldecode($var)); |
---|
49 | $this->exercice = $tableau[0]; |
---|
50 | $this->join = $tableau[1]; |
---|
51 | $this->sel = $tableau[2]; |
---|
52 | $this->where = $tableau[3]; |
---|
53 | $this->having = $tableau[4]; |
---|
54 | $this->order = $tableau[5]; |
---|
55 | $this->out = ''; |
---|
56 | } |
---|
57 | |
---|
58 | function EnTete() { |
---|
59 | $this->out .= str_replace(array("\t","\n"), array('\t','\n'), utf8_decode(html_entity_decode(_T('asso:entete_code')))) ."\t"; |
---|
60 | $this->out .= str_replace(array("\t","\n"), array('\t','\n'), utf8_decode(html_entity_decode(_T('asso:entete_intitule')))) ."\t"; |
---|
61 | $this->out .= str_replace(array("\t","\n"), array('\t','\n'), utf8_decode(html_entity_decode(_T('asso:entete_montant')))) ."\n"; |
---|
62 | } |
---|
63 | |
---|
64 | function LesEcritures($key) { |
---|
65 | switch ($key) { |
---|
66 | case 'charges' : |
---|
67 | $quoi = "SUM(depense) AS valeurs"; |
---|
68 | break; |
---|
69 | case 'produits' : |
---|
70 | $quoi = "SUM(recette) AS valeurs"; |
---|
71 | break; |
---|
72 | case 'contributions_volontaires' : |
---|
73 | $quoi = "SUM(depense) AS charge_evaluee, SUM(recette) AS produit_evalue"; |
---|
74 | break; |
---|
75 | } |
---|
76 | $query = sql_select( |
---|
77 | "imputation, $quoi, DATE_FORMAT(date, '%Y') AS annee".$this->sel, // select |
---|
78 | 'spip_asso_comptes '.$this->join, // from |
---|
79 | $this->where, // where |
---|
80 | $this->order, // group by |
---|
81 | $this->order, // order by |
---|
82 | '', // limit |
---|
83 | $this->having .$GLOBALS['association_metas']['classe_'.$key] // having |
---|
84 | ); |
---|
85 | $chapitre = ''; |
---|
86 | $i = 0; |
---|
87 | while ($data = sql_fetch($query)) { |
---|
88 | if ($key==='contributions_volontaires') { |
---|
89 | if ($data['charge_evaluee']>0) { |
---|
90 | $valeurs = $data['charge_evaluee']; |
---|
91 | } else { |
---|
92 | $valeurs = $data['produit_evalue']; |
---|
93 | } |
---|
94 | } else { |
---|
95 | $valeurs = $data['valeurs']; |
---|
96 | } |
---|
97 | $new_chapitre = substr($data['code'], 0, 2); |
---|
98 | if ($chapitre!=$new_chapitre) { |
---|
99 | $this->out .= str_replace(array("\t","\n"), array('\t','\n'), $new_chapitre) .'",'; |
---|
100 | $this->out .= str_replace(array("\t","\n"), array('\t','\n'), ($GLOBALS['association_metas']['plan_comptable_prerenseigne']?association_plan_comptable_complet($new_chapitre):sql_getfetsel('intitule','spip_asso_plan',"code='$new_chapitre'"))) ."\t\n"; |
---|
101 | $chapitre = $new_chapitre; |
---|
102 | } |
---|
103 | $this->out .= str_replace(array("\t","\n"), array('\t','\n'), $data['code']) ."\t"; |
---|
104 | $this->out .= str_replace(array("\t","\n"), array('\t','\n'), $data['intitule']) ."\t"; |
---|
105 | $this->out .= $valeurs."\n"; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | function Enregistre() { |
---|
110 | $fichier = _DIR_RACINE.'/'._NOM_TEMPORAIRES_ACCESSIBLES.'compte_resultats_'.$this->exercice.'.tab'; |
---|
111 | $f = fopen($fichier, 'w'); |
---|
112 | fputs($f, $this->out); |
---|
113 | fclose($f); |
---|
114 | header('Content-type: application/tsv'); |
---|
115 | header('Content-Disposition: attachment; filename="'.$fichier.'"'); |
---|
116 | readfile($fichier); |
---|
117 | } |
---|
118 | |
---|
119 | } |
---|
120 | |
---|
121 | ?> |
---|