| 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 | $page->changeTpl('trezo/index.tpl'); |
| 35 | |
| 36 | require_once 'money/trezo.inc.php'; |
| 37 | |
| 38 | //table operations : |
| 39 | //+--------+---------------+------+-----+------------+----------------+ |
| 40 | //| field | type | null | key | default | extra | |
| 41 | //+--------+---------------+------+-----+------------+----------------+ |
| 42 | //| id | int(11) | | pri | null | auto_increment | |
| 43 | //| date | date | | | 0000-00-00 | | |
| 44 | //| label | varchar(80) | | | | | |
| 45 | //| credit | decimal(10,2) | | | 0.00 | | |
| 46 | //| debit | decimal(10,2) | | | 0.00 | | |
| 47 | //+--------+---------------+------+-----+------------+----------------+ |
| 48 | |
| 49 | $annee_sel = Env::get('annee', date('y')); |
| 50 | $mois_sel = Env::get('mois', sprintf('%02u', date('m') - (intval(date('m') - 1) % 3))); |
| 51 | |
| 52 | $mois_sel_fin = sprintf('%02u',$mois_sel + 2); |
| 53 | $from_date = "$annee_sel-$mois_sel-01"; |
| 54 | $to_date = "$annee_sel-$mois_sel_fin-31"; |
| 55 | $mon_sel = $trim_fr[$mois_sel]." $annee_sel"; |
| 56 | |
| 57 | $page->assign('from_solde', solde_until($from_date)); |
| 58 | $page->assign('to_solde', solde_until($to_date)); |
| 59 | $page->assign('annee_sel', $annee_sel); |
| 60 | $page->assign('mois_sel', $mois_sel); |
| 61 | $page->assign('mon_sel', $mon_sel); |
| 62 | $page->assign_by_ref('month_arr', $trim_fr); |
| 63 | |
| 64 | $page->assign('ops', XDB::iterator( |
| 65 | "SELECT date, label, credit, debit |
| 66 | FROM money_trezo WHERE date >= {?} and date <= {?} order by date", |
| 67 | $from_date, $to_date)); |
| 68 | } |
| 69 | |
| 70 | function handler_operation(&$page) |
| 71 | { |
| 72 | $page->changeTpl('trezo/gere_operations.tpl'); |
| 73 | |
| 74 | require_once 'money/trezo.inc.php'; |
| 75 | |
| 76 | $page->assign('xorg_title','Polytechnique.org - Administration - Trezo : gestion'); |
| 77 | |
| 78 | $action = clean_request('action'); |
| 79 | $op_id = clean_request('op_id'); |
| 80 | $op_date = clean_request('op_date'); |
| 81 | $op_label = clean_request('op_label'); |
| 82 | $op_credit = clean_request('op_credit'); |
| 83 | $op_debit = clean_request('op_debit'); |
| 84 | |
| 85 | $annee_sel = isset($_REQUEST['annee']) ? $_REQUEST['annee'] : date("Y"); |
| 86 | $mois_sel = isset($_REQUEST['mois']) ? $_REQUEST['mois'] : sprintf("%02u", date('m')); |
| 87 | |
| 88 | $from_date = "$annee_sel-$mois_sel-01"; |
| 89 | $to_date = "$annee_sel-$mois_sel-31"; |
| 90 | $mon_sel = $trim_fr[$mois_sel]." $annee_sel"; |
| 91 | |
| 92 | switch($action) { |
| 93 | case "edit": |
| 94 | if ($op_id) { |
| 95 | $res = XDB::query("SELECT date,label,credit,debit FROM money_trezo WHERE id={?}", $op_id); |
| 96 | list($op_date,$op_label,$op_credit,$op_debit) = $res->fetchOneRow(); |
| 97 | } |
| 98 | break; |
| 99 | |
| 100 | case "update": |
| 101 | if (isDate($op_date)){ |
| 102 | $mydatefr = explode("/",$op_date); |
| 103 | $mydate = $mydatefr[2]."-".$mydatefr[1]."-".$mydatefr[0]; |
| 104 | } else { |
| 105 | $mydate = date("Y-m-d"); |
| 106 | } |
| 107 | |
| 108 | $sql = "replace into money_trezo set date='$mydate',label='".addslashes($op_label)."'"; |
| 109 | |
| 110 | if ($op_credit) { $sql .= ',credit='.$op_credit; } |
| 111 | if ($op_debit) { $sql .= ',debit='.$op_debit; } |
| 112 | if ($op_id) { $sql .= ",id='$op_id'"; } |
| 113 | |
| 114 | XDB::execute($sql); |
| 115 | break; |
| 116 | |
| 117 | case "del": |
| 118 | if ($op_id) { |
| 119 | XDB::execute("DELETE FROM money_trezo WHERE id={?}", $op_id); |
| 120 | } |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | $page->assign('op_id', $op_id); |
| 125 | $page->assign('annee_sel', $annee_sel); |
| 126 | $page->assign('mois_sel', $mois_sel); |
| 127 | $page->assign('op_date', $op_date); |
| 128 | $page->assign('op_label', $op_label); |
| 129 | $page->assign('op_debit', $op_debit); |
| 130 | $page->assign('op_credit', $op_credit); |
| 131 | $page->assign('mon_sel', $mon_sel); |
| 132 | |
| 133 | $page->assign('from_solde', solde_until($from_date)); |
| 134 | $page->assign('to_solde', solde_until($to_date)); |
| 135 | $page->assign('month_arr', $mois_fr); |
| 136 | $page->assign('ops', XDB::iterator( |
| 137 | "SELECT id, date, label, credit, debit FROM money_trezo |
| 138 | WHERE date >= {?} and date <= {?} ORDER BY date", |
| 139 | $from_date, $to_date)); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | ?> |