1 | <?php |
---|
2 | /***************************************************************************\ |
---|
3 | * Associaspip, extension de SPIP pour gestion d'associations * |
---|
4 | * * |
---|
5 | * Copyright (c) 2007 Bernard Blazin & Fran�ois de Montlivault (V1) * |
---|
6 | * Copyright (c) 2010-2011 Emmanuel Saint-James & Jeannot Lapin (V2) * |
---|
7 | * * |
---|
8 | * Ce programme est un logiciel libre distribue sous licence GNU/GPL. * |
---|
9 | * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. * |
---|
10 | \***************************************************************************/ |
---|
11 | |
---|
12 | |
---|
13 | if (!defined('_ECRIRE_INC_VERSION')) |
---|
14 | return; |
---|
15 | |
---|
16 | include_spip ('inc/navigation_modules'); |
---|
17 | |
---|
18 | function exec_bilan() |
---|
19 | { |
---|
20 | if (!autoriser('associer', 'comptes')) { |
---|
21 | include_spip('inc/minipres'); |
---|
22 | echo minipres(); |
---|
23 | } else { |
---|
24 | $plan = sql_countsel('spip_asso_plan'); |
---|
25 | $exercice = intval(_request('exercice')); |
---|
26 | if(!$exercice){ |
---|
27 | /* on recupere l'id_exercice dont la date "fin" est "la plus grande" */ |
---|
28 | $exercice = sql_getfetsel('id_exercice','spip_asso_exercices','','','fin DESC'); |
---|
29 | if(!$exercice) |
---|
30 | $exercice=0; |
---|
31 | } |
---|
32 | $exercice_data = sql_asso1ligne('exercice', $exercice); |
---|
33 | // recupere l'id_destination de la ou des destinations dans POST ou cree une entree a 0 dans le tableau |
---|
34 | if (!($ids_destination_bilan = _request('destination'))) |
---|
35 | $ids_destination_bilan = array(0); |
---|
36 | onglets_association('titre_onglet_comptes'); |
---|
37 | // INTRO : rappel de l'exercicee affichee |
---|
38 | $infos['exercice_entete_debut'] = association_datefr($exercice_data['debut'], 'dtstart'); |
---|
39 | $infos['exercice_entete_fin'] = association_datefr($exercice_data['fin'], 'dtend'); |
---|
40 | echo totauxinfos_intro($exercice_data['intitule'], 'exercice', $exercice, $infos); |
---|
41 | if ($GLOBALS['association_metas']['destinations']=='on') { |
---|
42 | // cree un menu a choix multiple des destinations a inserer dans la boite info et recupere les intitule de toutes les destinations dans un tableau |
---|
43 | $select_destination = ''; |
---|
44 | $intitule_destinations = array(); |
---|
45 | $query = sql_select('id_destination, intitule', 'spip_asso_destination', '', '', 'intitule'); |
---|
46 | while ($data = sql_fetch($query)) { |
---|
47 | $select_destination .= '<option value="'.$data['id_destination'].'"'; |
---|
48 | if (!(array_search($data['id_destination'], $ids_destination_bilan)===FALSE)) |
---|
49 | $select_destination .= ' selected="selected"'; |
---|
50 | $select_destination .= '>'.$data['intitule'].'</option>'; |
---|
51 | $intitule_destinations[$data['id_destination']] = $data['intitule']; |
---|
52 | } |
---|
53 | echo '<form method="post" action="'.generer_url_ecrire('bilan', "exercice=$exercice").'"><div>'; |
---|
54 | echo '<select name ="destination[]" class="fondl" multiple>'; |
---|
55 | echo '<option value="0"'; |
---|
56 | if (!(array_search(0, $ids_destination_bilan)===FALSE)) |
---|
57 | echo ' selected="selected"'; |
---|
58 | echo '>'._T('asso:bilan_total').'</option><option disabled="disabled">--------</option>'.$select_destination; |
---|
59 | echo '</select>'; |
---|
60 | echo '<p class="boutons"><input type="submit" value="Bilan" /></p>'; |
---|
61 | echo '</div></form>'; |
---|
62 | } |
---|
63 | // datation et raccourcis |
---|
64 | icones_association(array('comptes', "exercice=$exercice"), array( |
---|
65 | 'cpte_resultat_titre_general' => array('finances-24.png', 'compte_resultat', "exercice=$exercice"), |
---|
66 | 'annexe_titre_general' => array('finances-24.png', 'annexe', "exercice=$exercice"), |
---|
67 | )); |
---|
68 | debut_cadre_association('finances-24.png', 'bilans_comptables', $exercice_data['intitule']); |
---|
69 | $clas_banque = $GLOBALS['association_metas']['classe_banques']; |
---|
70 | $clas_contrib_volontaire = $GLOBALS['association_metas']['classe_contributions_volontaires']; // une contribution benevole ne doit pas etre comptabilisee en charge/produit |
---|
71 | if ($plan) { |
---|
72 | $join = ' RIGHT JOIN spip_asso_plan ON imputation=code'; |
---|
73 | $sel = ', code, intitule, classe'; |
---|
74 | $where = " date>='$exercice_data[debut]' AND date<='$exercice_data[fin]' "; |
---|
75 | $having = "classe<>'". sql_quote($clas_banque). "' AND classe<>'" .sql_quote($clas_contrib_volontaire) .'\''; |
---|
76 | $order = 'code'; |
---|
77 | } else { |
---|
78 | $join = $sel = $where = $having = $order = ''; |
---|
79 | } |
---|
80 | // on boucle sur le tableau des destinations en refaisant le fetch a chaque iteration |
---|
81 | foreach ($ids_destination_bilan as $id_destination) { |
---|
82 | $total_recettes = $total_depenses = $total_soldes = 0; |
---|
83 | //TABLEAU EXPLOITATION |
---|
84 | if ($id_destination!=0) { |
---|
85 | $intitule_destination_bilan = $intitule_destinations[$id_destination]; |
---|
86 | } else { |
---|
87 | if ($GLOBALS['association_metas']['destinations']=='on') $intitule_destination_bilan = _T('asso:toutes_destination'); |
---|
88 | } |
---|
89 | echo "\n<fieldset>"; |
---|
90 | echo '<legend><strong>'. _T('asso:resultat_courant') . ' ' .$intitule_destination_bilan. '</strong></legend>'; |
---|
91 | echo "<table width='100%' class='asso_tablo' id='asso_tablo_bilan_resultat'>\n"; |
---|
92 | echo "<thead>\n<tr>"; |
---|
93 | echo '<th colspan="2"> </th>'; |
---|
94 | echo '<th width="50">'. _T('asso:bilan_recettes') .'</th>'; |
---|
95 | echo '<th width="50">'. _T('asso:bilan_depenses') .'</th>'; |
---|
96 | echo '<th width="50">'. _T('asso:bilan_solde').'</th>'; |
---|
97 | echo "</tr>\n</thead><tbody>"; |
---|
98 | // si on fait le bilan sur toutes les destinations (quand aucune de selectionnee ou que destination n'est pas on) |
---|
99 | if ($id_destination==0) { |
---|
100 | $query = sql_select( |
---|
101 | "imputation, SUM(recette) AS recettes, SUM(depense) AS depenses, DATE_FORMAt(date, '%Y') AS annee $sel", |
---|
102 | "spip_asso_comptes $join", |
---|
103 | $where, $order, '', '', $having); |
---|
104 | while ($data = sql_fetch($query)) { |
---|
105 | $recettes = $data['recettes']; |
---|
106 | $depenses = $data['depenses']; |
---|
107 | $solde = $recettes-$depenses; |
---|
108 | echo '<tr>'; |
---|
109 | echo '<td class="text">'. $data['code'] . '</td>'; |
---|
110 | echo '<td class="text">'. $data['intitule'] .'</td>'; |
---|
111 | echo '<td class="decimal">'.association_prixfr($recettes).'</td>'; |
---|
112 | echo '<td class="decimal">'.association_prixfr($depenses).'</td>'; |
---|
113 | echo '<td class="decimal">'.association_prixfr($solde).'</td>'; |
---|
114 | echo "</tr>\n"; |
---|
115 | $total_recettes += $recettes; |
---|
116 | $total_depenses += $depenses; |
---|
117 | $total_soldes += $solde; |
---|
118 | } |
---|
119 | } else { // on fait le bilan d'une seule destination |
---|
120 | $query = sql_select( |
---|
121 | "imputation, DATE_FORMAT(date, '%Y') AS annee, |
---|
122 | SUM(spip_asso_destination_op.recette) AS recettes, |
---|
123 | SUM(spip_asso_destination_op.depense) AS depenses, |
---|
124 | spip_asso_destination_op.id_destination $sel", |
---|
125 | "spip_asso_comptes LEFT JOIN spip_asso_destination_op ON spip_asso_destination_op.id_compte=spip_asso_comptes.id_compte $join", |
---|
126 | "spip_asso_destination_op.id_destination=$id_destination AND $where", |
---|
127 | $order, '', '', $having); |
---|
128 | while ($data = sql_fetch($query)) { |
---|
129 | $recettes = $data['recettes']; |
---|
130 | $depenses = $data['depenses']; |
---|
131 | $solde = $recettes-$depenses; |
---|
132 | echo '<tr>'; |
---|
133 | echo '<td class="text">'. $data['code'] .'</td>'; |
---|
134 | echo '<td class="text">'. $data['intitule'] .'</td>'; |
---|
135 | echo '<td class="decimal">'.association_prixfr($recettes).'</td>'; |
---|
136 | echo '<td class="decimal">'.association_prixfr($depenses).'</td>'; |
---|
137 | echo '<td class="decimal">'.association_prixfr($solde).'</td>'; |
---|
138 | echo "</tr>\n"; |
---|
139 | $total_recettes += $recettes; |
---|
140 | $total_depenses += $depenses; |
---|
141 | $total_soldes += $solde; |
---|
142 | } |
---|
143 | } |
---|
144 | echo "</tbody><tfoot>\n<tr>"; |
---|
145 | echo '<th colspan="2">'. _T('asso:resultat_courant') .'</th>'; |
---|
146 | echo '<th class="decimal">'. association_prixfr($total_recettes) .'</th>'; |
---|
147 | echo '<th class="decimal">'. association_prixfr($total_depenses) .'</th>'; |
---|
148 | echo '<th class="decimal">'. association_prixfr($total_soldes) .'</th>'; |
---|
149 | echo "</tr>\n</tfoot>\n</table>\n"; |
---|
150 | echo '</fieldset>'; |
---|
151 | } |
---|
152 | if ($plan) |
---|
153 | bilan_encaisse(); |
---|
154 | fin_page_association(); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | /* Dans la fonction suivante "bilan_encaisse($annee)" on dissocie la "lecture" et "l'affichage" |
---|
159 | * afin de pouvoir traiter et calculer des valeurs intermédiaires : |
---|
160 | * 1 - on ne comptabilise pour le terme "encaisse" que les sommes dont le journal est 53xx ou 51xx |
---|
161 | * 2 - si "imputation" vaut 58xx : c'est un virement interne et le solde du compte doit etre a zéro |
---|
162 | * sinon il y a une erreur !! |
---|
163 | * 3 - si "imputation" vaut 86xx ou 87xx : c'est une contribution volontaire ... il est preferable que |
---|
164 | * que les comptes 86 et 87 s'equilibrent. Faire apparaitre dans le "bilan" uniquement le cas ou il |
---|
165 | * y a desequilibre ! |
---|
166 | */ |
---|
167 | |
---|
168 | // TODO : le passage en exercice budgétaire - 1)date de fin de l'exercice 2)faire apparaitre l'avoir initial : qui est le solde à (debut -1) de l'exercice (report) |
---|
169 | |
---|
170 | function bilan_encaisse() |
---|
171 | { |
---|
172 | $lesEcritures = array(); |
---|
173 | $laDateDuJour = date('Y-m-d'); # pour ne pas comptabiliser les opérations au delà d'aujourd'hui !!!! |
---|
174 | $laClasseBanque = $GLOBALS['association_metas']['classe_banques']; |
---|
175 | $laClasseContributionVolontaire = $GLOBALS['association_metas']['classe_contributions_volontaires']; |
---|
176 | $query = sql_select('*', 'spip_asso_plan', '(classe='.sql_quote($laClasseBanque).' OR classe='.sql_quote($laClasseContributionVolontaire).') AND active=1', '', 'code' ); |
---|
177 | while ($val = sql_fetch($query)) { |
---|
178 | $code = $val['code']; |
---|
179 | /* on declare un tableau et on le rempli avec les donnees du compte */ |
---|
180 | $lesEcritures[$code] = array(); |
---|
181 | $lesEcritures[$code]['code'] = $val['code']; |
---|
182 | $lesEcritures[$code]['intitule'] = $val['intitule']; |
---|
183 | $lesEcritures[$code]['date_solde'] = $val['date_anterieure']; |
---|
184 | $lesEcritures[$code]['solde_anterieur'] = $val['solde_anterieur']; |
---|
185 | /* ne pas comptabiliser les opérations au delà d'aujourd'hui meme si il y a des echeances futures !!!! */ |
---|
186 | $compte = sql_fetsel('SUM(recette) AS recettes, SUM(depense) AS depenses, date, imputation', |
---|
187 | 'spip_asso_comptes', |
---|
188 | 'date >= '.sql_quote($lesEcritures[$code]['date_solde']).' AND date <= '.sql_quote($laDateDuJour).' AND (journal = '.sql_quote($code).' OR imputation = '.sql_quote($code).')', |
---|
189 | 'journal'); |
---|
190 | if ($compte) { |
---|
191 | if(substr($compte['imputation'],0,1)===$GLOBALS['association_metas']['classe_contributions_volontaires']) { |
---|
192 | /* c'est une contribution volontaire du type 8xxx : |
---|
193 | * c'est une dépense evaluee si 86xx ou recette évaluée si 87xx |
---|
194 | * qui doit apparaître dans le compte de resultat |
---|
195 | */ |
---|
196 | $lesEcritures['86xx']['solde'] = $compte['depenses']; |
---|
197 | $lesEcritures['87xx']['solde'] = $compte['recettes']; |
---|
198 | } elseif (substr($compte['imputation'],0,1)===$GLOBALS['association_metas']['classe_banques']) { |
---|
199 | /* c'est un virement interne avec le code 58xx : |
---|
200 | * le solde du compte doit être à zero sinon il y a erreur ! |
---|
201 | */ |
---|
202 | $lesEcritures['58xx']['solde'] = $compte['recettes']-$compte['depenses']; |
---|
203 | } else { |
---|
204 | /* c'est une recette ou une depense */ |
---|
205 | $lesEcritures[$code]['solde'] = $compte['recettes']-$compte['depenses']; |
---|
206 | } |
---|
207 | } |
---|
208 | } |
---|
209 | echo "\n<fieldset>"; |
---|
210 | echo '<legend><strong>' . _T('asso:encaisse') . '</strong></legend>'; |
---|
211 | echo "<table width='100%' class='asso_tablo' id='asso_tablo_bilan_encaisse'>\n"; |
---|
212 | echo "<thead>\n<tr>"; |
---|
213 | echo '<th colspan="2"> </th>'; |
---|
214 | echo '<th>'. _T('asso:avoir_initial') .'</th>'; |
---|
215 | echo '<th>'. _T('asso:avoir_actuel') .'</th>'; |
---|
216 | echo "</tr>\n</thead><tbody>"; |
---|
217 | $total_actuel = $total_initial = 0; |
---|
218 | foreach($lesEcritures as $compteFinancier) { |
---|
219 | /* c'est un compte banque 53xx ou un compte caisse 51xx */ |
---|
220 | if(substr($compteFinancier['code'],0,2)==='51' OR substr($compteFinancier['code'],0,2)==='53') { //!! Parametrer !!! Pas de valeurs en dures sinon ca ne sera valable que pour la France |
---|
221 | echo '<tr>'; |
---|
222 | echo '<td class="text">'. $compteFinancier['code'] .' : '. $compteFinancier['intitule'] .'</td>'; |
---|
223 | echo '<td class="date">'. association_datefr($compteFinancier['date_solde'],'dtstart') .'</td>'; |
---|
224 | echo '<td class="decimal">'. association_prixfr($compteFinancier['solde_anterieur']) .'</td>'; |
---|
225 | echo '<td class="decimal">'. association_prixfr($compteFinancier['solde_anterieur']+$compteFinancier['solde']) .'</td>'; |
---|
226 | $total_initial += $compteFinancier['solde_anterieur']; |
---|
227 | $total_actuel += $compteFinancier['solde_anterieur']+$compteFinancier['solde']; |
---|
228 | } else { |
---|
229 | if($compteFinancier['58xx']['solde']!=0) { |
---|
230 | $erreur58=TRUE; |
---|
231 | $messageErreur58 = _L("Attention : Virement interne non équilibré !"); |
---|
232 | } |
---|
233 | if($compteFinancier['86xx']['solde']!=$compteFinancier['87xx']['solde']) { |
---|
234 | $erreur8687=TRUE; |
---|
235 | $messageErreur8687 = _L("Attention : Comptes 86xx et 87xx ne sont pas équilibrés !"); |
---|
236 | } |
---|
237 | } |
---|
238 | } |
---|
239 | echo "</tr>\n</tbody><tfoot>\n<tr>"; |
---|
240 | echo '<th colspan="2" class="text">'. _T('asso:encaisse') .'</th>'; |
---|
241 | echo '<th class="decimal">'. association_prixfr($total_initial) .'</th>'; |
---|
242 | echo '<th class="decimal">'. association_prixfr($total_actuel) .'</th>'; |
---|
243 | echo "</tr>\n</tfoot>\n</table>\n"; |
---|
244 | if($erreur58){ |
---|
245 | echo $messageErreur58; |
---|
246 | } |
---|
247 | if($erreur8687){ |
---|
248 | echo $messageErreur8687; |
---|
249 | } |
---|
250 | echo '</fieldset>'; |
---|
251 | } |
---|
252 | |
---|
253 | ?> |
---|