we don't need diogenes session anymore
[platal.git] / modules / trezo.php
CommitLineData
f7d602db 1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22class TrezoModule extends PLModule
23{
24 function handlers()
25 {
26 return array(
27 'trezo' => $this->make_hook('default', AUTH_COOKIE),
28 'trezo/ops' => $this->make_hook('operation', AUTH_COOKIE),
29 );
30 }
31
32 function handler_default(&$page)
33 {
34 global $globals;
35
36 $page->changeTpl('trezo/index.tpl');
37
38 require_once 'money/trezo.inc.php';
39
40 //table operations :
41 //+--------+---------------+------+-----+------------+----------------+
42 //| field | type | null | key | default | extra |
43 //+--------+---------------+------+-----+------------+----------------+
44 //| id | int(11) | | pri | null | auto_increment |
45 //| date | date | | | 0000-00-00 | |
46 //| label | varchar(80) | | | | |
47 //| credit | decimal(10,2) | | | 0.00 | |
48 //| debit | decimal(10,2) | | | 0.00 | |
49 //+--------+---------------+------+-----+------------+----------------+
50
51 $annee_sel = Env::get('annee', date('y'));
52 $mois_sel = Env::get('mois', sprintf('%02u', date('m') - (intval(date('m') - 1) % 3)));
53
54 $mois_sel_fin = sprintf('%02u',$mois_sel + 2);
55 $from_date = "$annee_sel-$mois_sel-01";
56 $to_date = "$annee_sel-$mois_sel_fin-31";
57 $mon_sel = $trim_fr[$mois_sel]." $annee_sel";
58
59 $page->assign('from_solde', solde_until($from_date));
60 $page->assign('to_solde', solde_until($to_date));
61 $page->assign('annee_sel', $annee_sel);
62 $page->assign('mois_sel', $mois_sel);
63 $page->assign('mon_sel', $mon_sel);
64 $page->assign_by_ref('month_arr', $trim_fr);
65
66 $page->assign('ops', $globals->xdb->iterator(
67 "SELECT date, label, credit, debit
68 FROM money_trezo WHERE date >= {?} and date <= {?} order by date",
69 $from_date, $to_date));
f7d602db 70 }
71
f3cb77fc 72 function handler_operation(&$page)
f7d602db 73 {
74 global $globals;
75
76 $page->changeTpl('trezo/gere_operations.tpl');
77
78 require_once 'money/trezo.inc.php';
79
80 $page->assign('xorg_title','Polytechnique.org - Administration - Trezo : gestion');
81
82 $action = clean_request('action');
83 $op_id = clean_request('op_id');
84 $op_date = clean_request('op_date');
85 $op_label = clean_request('op_label');
86 $op_credit = clean_request('op_credit');
87 $op_debit = clean_request('op_debit');
88
89 $annee_sel = isset($_REQUEST['annee']) ? $_REQUEST['annee'] : date("Y");
90 $mois_sel = isset($_REQUEST['mois']) ? $_REQUEST['mois'] : sprintf("%02u", date('m'));
91
92 $from_date = "$annee_sel-$mois_sel-01";
93 $to_date = "$annee_sel-$mois_sel-31";
94 $mon_sel = $trim_fr[$mois_sel]." $annee_sel";
95
96 switch($action) {
97 case "edit":
98 if ($op_id) {
99 $res = $globals->xdb->query("SELECT date,label,credit,debit FROM money_trezo WHERE id={?}", $op_id);
100 list($op_date,$op_label,$op_credit,$op_debit) = $res->fetchOneRow();
101 }
102 break;
103
104 case "update":
105 if (isDate($op_date)){
106 $mydatefr = explode("/",$op_date);
107 $mydate = $mydatefr[2]."-".$mydatefr[1]."-".$mydatefr[0];
108 } else {
109 $mydate = date("Y-m-d");
110 }
111
112 $sql = "replace into money_trezo set date='$mydate',label='".addslashes($op_label)."'";
113
114 if ($op_credit) { $sql .= ',credit='.$op_credit; }
115 if ($op_debit) { $sql .= ',debit='.$op_debit; }
116 if ($op_id) { $sql .= ",id='$op_id'"; }
117
118 $globals->xdb->execute($sql);
119 break;
120
121 case "del":
122 if ($op_id) {
123 $globals->xdb->execute("DELETE FROM money_trezo WHERE id={?}", $op_id);
124 }
125 break;
126 }
127
128 $page->assign('op_id', $op_id);
129 $page->assign('annee_sel', $annee_sel);
130 $page->assign('mois_sel', $mois_sel);
131 $page->assign('op_date', $op_date);
132 $page->assign('op_label', $op_label);
133 $page->assign('op_debit', $op_debit);
134 $page->assign('op_credit', $op_credit);
135 $page->assign('mon_sel', $mon_sel);
136
137 $page->assign('from_solde', solde_until($from_date));
138 $page->assign('to_solde', solde_until($to_date));
139 $page->assign('month_arr', $mois_fr);
140 $page->assign('ops', $globals->xdb->iterator(
141 "SELECT id, date, label, credit, debit FROM money_trezo
142 WHERE date >= {?} and date <= {?} ORDER BY date",
143 $from_date, $to_date));
f7d602db 144 }
145}
146
147?>