JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added "unpaid totals" section to admin home. removed untested capability of dealing...
[contractor-progress.git] / index.php
1 <?php
2
3 #  Copyright (C) 2008  Jason Woofenden
4 #
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU Affero General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU Affero General Public License for more details.
14 #
15 #  You should have received a copy of the GNU Affero General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 require_once('code/tasks.php');
19
20 function index_main() {
21         if(!logged_in()) {
22                 return 'login';
23         }
24         $ret = _index_main();
25         if($ret) {
26                 return $ret;
27         }
28         tem_show('main_body');
29 }
30
31 # pass multiple argumens for where-clause and printf-args just like db_get_rows()
32 function task_summary($tem_prefix, $where_clause/*, ... */) {
33         $args = func_get_args();
34         $args = array_slice($args, 1);
35         array_unshift($args, 'tasks', 'id,price,title,state,client_id,paid');
36         $rows = call_user_func_array('db_get_rows', $args);
37         #$rows = db_get_rows('tasks', 'id,price,title,state,client_id', $where_clause);
38         if($rows) {
39                 $total = 0.0;
40                 foreach($rows as $row) {
41                         list($id, $price, $title, $state, $client_id, $paid) = $row;
42                         tem_set('task_id', $id);
43                         tem_set('task_title', $title);
44                         tem_set('task_price', $price);
45                         tem_set('task_state', task_state_pretty($state));
46                         if(logged_in_as_contractor()) {
47                                 tem_set('client', db_get_value('people', 'username', 'where id=%i', $client_id));
48                         }
49                         tem_show($tem_prefix . '_row');
50                         if(!$paid && isset($GLOBALS['unpaid_totals'])) {
51                                 if(!isset($GLOBALS['unpaid_totals'][$client_id])) {
52                                         $GLOBALS['unpaid_totals'][$client_id] = array();
53                                 }
54                                 if(!isset($GLOBALS['unpaid_totals'][$client_id][$tem_prefix])) {
55                                         $GLOBALS['unpaid_totals'][$client_id][$tem_prefix] = 0;
56                                 }
57                                 $GLOBALS['unpaid_totals'][$client_id][$tem_prefix] += $price;
58                         }
59                         $total += $price;
60                 }
61
62                 tem_set('task_total', $total);
63
64                 tem_show($tem_prefix);
65         }
66 }
67
68 # pass multi-dimensional hash arr[client_id][status].
69 # status strings: 'finished_untested', 'finished_unpaid', 'queue'
70 function totals_summary($arr) {
71         if($arr) {
72                 foreach($arr as $client_id => $t) {
73                         $finished_untested = 0;
74                         $finished_unpaid = 0;
75                         $queue = 0;
76                         $working = 0;
77                         if(isset($t['finished_untested'])) {
78                                 $finished_untested = $t['finished_untested'];
79                         }
80                         if(isset($t['finished_unpaid'])) {
81                                 $finished_unpaid = $t['finished_unpaid'];
82                         }
83                         if(isset($t['queue'])) {
84                                 $queue = $t['queue'];
85                         }
86                         if(isset($t['needs_approval'])) { # administrator page re-uses this for TASK_WORKING
87                                 $working = $t['needs_approval'];
88                         }
89                         tem_set('tested_total', $finished_unpaid);
90                         tem_set('done_total', $finished_unpaid + $finished_untested);
91                         tem_set('queued_total', $queue + $working);
92                         tem_set('client', db_get_value('people', 'username', 'where id=%i', $client_id));
93                         tem_show('unpaid_totals_row');
94                 }
95
96                 tem_show('unpaid_totals');
97         }
98 }
99
100 function _index_main() {
101         $client_id = logged_in();
102
103         # make sure they've filled out the tiny user agreement
104         $tiny_agreement = db_get_value('people', 'tiny_agreement', 'where id=%i', $client_id);
105         if($tiny_agreement < 30) {
106                 return './tiny_agreement';
107         }
108
109         if(logged_in_as_contractor()) {
110                 tem_show('su_link');
111                 tem_show('needs_attention_header');
112
113                 $GLOBALS['unpaid_totals'] = array();
114                 # things with a view link:
115                 task_summary('needs_approval', 'where state=%i order by id desc', TASK_WORKING); # this affects queued_total
116
117                 # things with an edit link:
118                 task_summary('needs_fixing', 'where state=%i || state=%i order by id desc', TASK_NEEDS_QUOTE, TASK_BUG);
119                 task_summary('finished_untested', 'where state=%i order by client_id, finished_at desc', TASK_NEEDS_TESTING);
120                 task_summary('finished_unpaid', 'where state=%i && paid=0 order by finished_at desc', TASK_FINISHED);
121                 task_summary('queue', 'where state=%i order by client_id, ord desc', TASK_QUEUED);
122                 totals_summary($GLOBALS['unpaid_totals']);
123                 task_summary('finished_paid', 'where state=%i && paid = 1 order by finished_at desc limit 20', TASK_FINISHED);
124         } else {
125                 if(db_count('tasks', 'where client_id=%i && (state=%i || state=%i || state=%i || state=%i)', $client_id, TASK_DRAFT, TASK_NEEDS_CLARIFICATION, TASK_NEEDS_GO_AHEAD, TASK_NEEDS_TESTING)) {
126                         tem_show('needs_attention_header');
127                         task_summary('needs_approval', 'where client_id=%i && (state=%i || state=%i) order by id', $client_id, TASK_NEEDS_GO_AHEAD, TASK_NEEDS_TESTING);
128                         task_summary('needs_fixing', 'where client_id=%i && (state=%i || state=%i) order by id', $client_id, TASK_DRAFT, TASK_NEEDS_CLARIFICATION);
129                 }
130                 task_summary('queue', 'where client_id=%i && state=%i order by ord desc', $client_id, TASK_QUEUED);
131                 task_summary('jason', 'where client_id=%i && (state=%i || state=%i) order by id desc', $client_id, TASK_NEEDS_QUOTE, TASK_BUG);
132                 task_summary('jason_working', 'where client_id=%i && state=%i order by id desc', $client_id, TASK_WORKING);
133                 task_summary('finished_unpaid', 'where client_id=%i && state=%i && paid=0 order by finished_at desc', $client_id, TASK_FINISHED);
134                 task_summary('finished_paid', 'where client_id=%i && state=%i && paid=1 order by finished_at desc', $client_id, TASK_FINISHED);
135         }
136         return;
137 }