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 | |
22 | class 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)); |
70 | |
71 | return PL_OK; |
72 | } |
73 | |
f3cb77fc |
74 | function handler_operation(&$page) |
f7d602db |
75 | { |
76 | global $globals; |
77 | |
78 | $page->changeTpl('trezo/gere_operations.tpl'); |
79 | |
80 | require_once 'money/trezo.inc.php'; |
81 | |
82 | $page->assign('xorg_title','Polytechnique.org - Administration - Trezo : gestion'); |
83 | |
84 | $action = clean_request('action'); |
85 | $op_id = clean_request('op_id'); |
86 | $op_date = clean_request('op_date'); |
87 | $op_label = clean_request('op_label'); |
88 | $op_credit = clean_request('op_credit'); |
89 | $op_debit = clean_request('op_debit'); |
90 | |
91 | $annee_sel = isset($_REQUEST['annee']) ? $_REQUEST['annee'] : date("Y"); |
92 | $mois_sel = isset($_REQUEST['mois']) ? $_REQUEST['mois'] : sprintf("%02u", date('m')); |
93 | |
94 | $from_date = "$annee_sel-$mois_sel-01"; |
95 | $to_date = "$annee_sel-$mois_sel-31"; |
96 | $mon_sel = $trim_fr[$mois_sel]." $annee_sel"; |
97 | |
98 | switch($action) { |
99 | case "edit": |
100 | if ($op_id) { |
101 | $res = $globals->xdb->query("SELECT date,label,credit,debit FROM money_trezo WHERE id={?}", $op_id); |
102 | list($op_date,$op_label,$op_credit,$op_debit) = $res->fetchOneRow(); |
103 | } |
104 | break; |
105 | |
106 | case "update": |
107 | if (isDate($op_date)){ |
108 | $mydatefr = explode("/",$op_date); |
109 | $mydate = $mydatefr[2]."-".$mydatefr[1]."-".$mydatefr[0]; |
110 | } else { |
111 | $mydate = date("Y-m-d"); |
112 | } |
113 | |
114 | $sql = "replace into money_trezo set date='$mydate',label='".addslashes($op_label)."'"; |
115 | |
116 | if ($op_credit) { $sql .= ',credit='.$op_credit; } |
117 | if ($op_debit) { $sql .= ',debit='.$op_debit; } |
118 | if ($op_id) { $sql .= ",id='$op_id'"; } |
119 | |
120 | $globals->xdb->execute($sql); |
121 | break; |
122 | |
123 | case "del": |
124 | if ($op_id) { |
125 | $globals->xdb->execute("DELETE FROM money_trezo WHERE id={?}", $op_id); |
126 | } |
127 | break; |
128 | } |
129 | |
130 | $page->assign('op_id', $op_id); |
131 | $page->assign('annee_sel', $annee_sel); |
132 | $page->assign('mois_sel', $mois_sel); |
133 | $page->assign('op_date', $op_date); |
134 | $page->assign('op_label', $op_label); |
135 | $page->assign('op_debit', $op_debit); |
136 | $page->assign('op_credit', $op_credit); |
137 | $page->assign('mon_sel', $mon_sel); |
138 | |
139 | $page->assign('from_solde', solde_until($from_date)); |
140 | $page->assign('to_solde', solde_until($to_date)); |
141 | $page->assign('month_arr', $mois_fr); |
142 | $page->assign('ops', $globals->xdb->iterator( |
143 | "SELECT id, date, label, credit, debit FROM money_trezo |
144 | WHERE date >= {?} and date <= {?} ORDER BY date", |
145 | $from_date, $to_date)); |
146 | |
147 | return PL_OK; |
148 | } |
149 | } |
150 | |
151 | ?> |