JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
some clients I have set to auto-accept (very high tiny agreement) until they actually...
[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                 $total_finished_untested = 0;
73                 $total_finished_unpaid = 0;
74                 $total_queue = 0;
75                 $total_working = 0;
76                 foreach($arr as $client_id => $t) {
77                         $finished_untested = 0;
78                         $finished_unpaid = 0;
79                         $queue = 0;
80                         $working = 0;
81                         if(isset($t['finished_untested'])) {
82                                 $finished_untested = $t['finished_untested'];
83                         }
84                         if(isset($t['finished_unpaid'])) {
85                                 $finished_unpaid = $t['finished_unpaid'];
86                         }
87                         if(isset($t['queue'])) {
88                                 $queue = $t['queue'];
89                         }
90                         if(isset($t['needs_approval'])) { # administrator page re-uses this for TASK_WORKING
91                                 $working = $t['needs_approval'];
92                         }
93                         tem_set('tested_total', $finished_unpaid);
94                         tem_set('done_total', $finished_unpaid + $finished_untested);
95                         tem_set('not_tested_total', $finished_untested);
96                         tem_set('queued_total', $queue + $working);
97                         tem_set('client', db_get_value('people', 'username', 'where id=%i', $client_id));
98                         tem_show('unpaid_totals_row');
99                         $total_finished_untested += $finished_untested;
100                         $total_finished_unpaid += $finished_unpaid;
101                         $total_queue += $queue;
102                         $total_working += $working;
103                 }
104
105                 tem_set('tested_total', $total_finished_unpaid);
106                 tem_set('done_total', $total_finished_unpaid + $total_finished_untested);
107                 tem_set('not_tested_total', $total_finished_untested);
108                 tem_set('queued_total', $total_queue + $total_working);
109                 tem_set('client', 'all');
110                 tem_show('unpaid_totals_row');
111
112                 tem_show('unpaid_totals');
113         }
114 }
115
116 function _index_main() {
117         $client_id = logged_in();
118
119         # make sure they've filled out the tiny user agreement
120         $tiny_agreement = db_get_value('people', 'tiny_agreement', 'where id=%i', $client_id);
121         if($tiny_agreement < 30 || $tiny_agreement > 999) {
122                 return './tiny_agreement';
123         }
124
125         if(logged_in_as_contractor()) {
126                 tem_show('su_link');
127                 tem_show('needs_attention_header');
128
129                 $GLOBALS['unpaid_totals'] = array();
130                 # things with a view link:
131                 task_summary('needs_approval', 'where state=%i order by id desc', TASK_WORKING); # this affects queued_total
132
133                 # things with an edit link:
134                 task_summary('needs_fixing', 'where state=%i || state=%i order by id desc', TASK_NEEDS_QUOTE, TASK_BUG);
135                 task_summary('finished_untested', 'where state=%i order by client_id, finished_at desc', TASK_NEEDS_TESTING);
136                 task_summary('finished_unpaid', 'where state=%i && paid=0 order by finished_at desc', TASK_FINISHED);
137                 task_summary('queue', 'where state=%i order by client_id, ord desc', TASK_QUEUED);
138                 totals_summary($GLOBALS['unpaid_totals']);
139                 task_summary('finished_paid', 'where state=%i && paid = 1 order by finished_at desc limit 20', TASK_FINISHED);
140         } else {
141                 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)) {
142                         tem_show('needs_attention_header');
143                         task_summary('needs_approval', 'where client_id=%i && (state=%i || state=%i) order by id', $client_id, TASK_NEEDS_GO_AHEAD, TASK_NEEDS_TESTING);
144                         task_summary('needs_fixing', 'where client_id=%i && (state=%i || state=%i) order by id', $client_id, TASK_DRAFT, TASK_NEEDS_CLARIFICATION);
145                 }
146                 task_summary('queue', 'where client_id=%i && state=%i order by ord desc', $client_id, TASK_QUEUED);
147                 task_summary('jason', 'where client_id=%i && (state=%i || state=%i) order by id desc', $client_id, TASK_NEEDS_QUOTE, TASK_BUG);
148                 task_summary('jason_working', 'where client_id=%i && state=%i order by id desc', $client_id, TASK_WORKING);
149                 task_summary('finished_unpaid', 'where client_id=%i && state=%i && paid=0 order by finished_at desc', $client_id, TASK_FINISHED);
150                 task_summary('finished_paid', 'where client_id=%i && state=%i && paid=1 order by finished_at desc', $client_id, TASK_FINISHED);
151         }
152         return;
153 }