JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
88b09f6434198e7cb1b99e2899c11e712daee514
[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         $num_rows = 0;
38         if($rows) {
39                 $num_rows = count($rows);
40                 if($num_rows > 1) {
41                         tem_show($tem_prefix . '_plural');
42                 } else {
43                         tem_show($tem_prefix . '_singular');
44                 }
45                 $total = 0.0;
46                 foreach($rows as $row) {
47                         list($id, $price, $title, $state, $client_id, $paid) = $row;
48                         tem_set('task_id', $id);
49                         tem_set('task_title', $title);
50                         tem_set('task_price', $price);
51                         tem_set('task_state', task_state_pretty($state));
52                         if(logged_in_as_contractor()) {
53                                 tem_set('client', db_get_value('people', 'username', 'where id=%i', $client_id));
54                                 if($tem_prefix == 'queue') {
55                                         tem_show('queued_for');
56                                 }
57                         }
58                         if($num_rows > 1 && $tem_prefix == 'queue') {
59                                 tem_show('not_the_only_queued');
60                         }
61                         tem_show($tem_prefix . '_row');
62                         if(!$paid && isset($GLOBALS['unpaid_totals'])) {
63                                 if(!isset($GLOBALS['unpaid_totals'][$client_id])) {
64                                         $GLOBALS['unpaid_totals'][$client_id] = array();
65                                 }
66                                 if(!isset($GLOBALS['unpaid_totals'][$client_id][$tem_prefix])) {
67                                         $GLOBALS['unpaid_totals'][$client_id][$tem_prefix] = 0;
68                                 }
69                                 $GLOBALS['unpaid_totals'][$client_id][$tem_prefix] += $price;
70                         }
71                         $total += $price;
72                 }
73
74                 tem_set('task_total', $total);
75
76                 tem_show($tem_prefix);
77         }
78
79         return $num_rows;
80 }
81
82 # pass multi-dimensional hash arr[client_id][status].
83 # status strings: 'finished_untested', 'finished_unpaid', 'queue'
84 function totals_summary($arr) {
85         if($arr) {
86                 $total_finished_untested = 0;
87                 $total_finished_unpaid = 0;
88                 $total_queue = 0;
89                 $total_working = 0;
90                 foreach($arr as $client_id => $t) {
91                         $finished_untested = 0;
92                         $finished_unpaid = 0;
93                         $queue = 0;
94                         $working = 0;
95                         if(isset($t['finished_untested'])) {
96                                 $finished_untested = $t['finished_untested'];
97                         }
98                         if(isset($t['finished_unpaid'])) {
99                                 $finished_unpaid = $t['finished_unpaid'];
100                         }
101                         if(isset($t['queue'])) {
102                                 $queue = $t['queue'];
103                         }
104                         if(isset($t['do_finish'])) {
105                                 $working = $t['do_finish'];
106                         }
107                         tem_set('tested_total', $finished_unpaid);
108                         tem_set('done_total', $finished_unpaid + $finished_untested);
109                         tem_set('not_tested_total', $finished_untested);
110                         tem_set('queued_total', $queue + $working);
111                         tem_set('client', db_get_value('people', 'username', 'where id=%i', $client_id));
112                         tem_show('unpaid_totals_row');
113                         $total_finished_untested += $finished_untested;
114                         $total_finished_unpaid += $finished_unpaid;
115                         $total_queue += $queue;
116                         $total_working += $working;
117                 }
118
119                 tem_set('tested_total', $total_finished_unpaid);
120                 tem_set('done_total', $total_finished_unpaid + $total_finished_untested);
121                 tem_set('not_tested_total', $total_finished_untested);
122                 tem_set('queued_total', $total_queue + $total_working);
123                 tem_set('client', 'all');
124                 tem_show('unpaid_totals_row');
125
126                 tem_show('unpaid_totals');
127         }
128 }
129
130 function _index_main() {
131         $client_id = logged_in();
132         $ever_was_contractor = ever_was_contractor();
133
134         $contractor_full_name = db_get_value('people', 'name', 'where id=1');
135         if(!$contractor_full_name) {
136                 die('To finish installing this site, make yourself an account in the people table with id=1');
137         }
138         list($contractor_name,) = explode(' ', $contractor_full_name, 2);
139         tem_set('contractor_name', $contractor_name);
140         tem_set('contractor_full_name', $contractor_full_name);
141
142
143         # make sure the client (not the contractor) has filled out the tiny agreement
144         if(!$ever_was_contractor) {
145                 $tiny_agreement = db_get_value('people', 'tiny_agreement', 'where id=%i', $client_id);
146                 if($tiny_agreement < 30 || $tiny_agreement > 999) {
147                         return './tiny_agreement';
148                 }
149         }
150
151         if($ever_was_contractor) {
152                 tem_show('su_link');
153         }
154
155         if(logged_in_as_contractor()) {
156                 tem_show('contractor_links');
157                 tem_show('contractor_todo');
158
159                 $GLOBALS['unpaid_totals'] = array();
160                 task_summary('do_finish', 'where state=%i order by id desc', TASK_WORKING); # this affects queued_total
161                 task_summary('do_fix', 'where state=%i order by client_id desc', TASK_BUG);
162                 task_summary('do_price', 'where state=%i order by client_id desc', TASK_NEEDS_QUOTE);
163
164                 task_summary('finished_untested', 'where state=%i order by client_id, finished_at desc', TASK_NEEDS_TESTING);
165                 task_summary('finished_unpaid', 'where state=%i && paid=0 order by finished_at desc', TASK_FINISHED);
166                 task_summary('queue', 'where state=%i order by client_id, ord desc', TASK_QUEUED);
167                 totals_summary($GLOBALS['unpaid_totals']);
168                 task_summary('finished_paid', 'where state=%i && paid = 1 order by finished_at desc limit 20', TASK_FINISHED);
169         } else {
170                 tem_show('not_contractor_links');
171                 $num_rows = 0;
172                 $num_rows += task_summary('do_approve_price', 'where client_id=%i && state=%i order by id', $client_id, TASK_NEEDS_GO_AHEAD);
173                 $num_rows += task_summary('do_clarify', 'where client_id=%i && state=%i order by id', $client_id, TASK_NEEDS_CLARIFICATION);
174                 $num_rows += task_summary('do_test', 'where client_id=%i && state=%i order by id', $client_id, TASK_NEEDS_TESTING);
175                 $num_rows += task_summary('do_draft', 'where client_id=%i && state=%i order by id', $client_id, TASK_DRAFT);
176                 $num_rows += task_summary('do_on_hold', 'where client_id=%i && state=%i order by id', $client_id, TASK_ON_HOLD);
177
178                 if($num_rows == 0) {
179                         tem_show('nothing_needs_your_attention');
180                 } elseif($num_rows == 1) {
181                         tem_show('this_needs_your_attention');
182                 } else {
183                         tem_show('these_need_your_attention');
184                 }
185
186                 task_summary('queue', 'where client_id=%i && state=%i order by ord desc', $client_id, TASK_QUEUED);
187
188                 task_summary('contractor_will', 'where client_id=%i && (state=%i || state=%i || state=%i) order by id desc', $client_id, TASK_NEEDS_QUOTE, TASK_BUG, TASK_WORKING);
189
190                 task_summary('finished_unpaid', 'where client_id=%i && state=%i && paid=0 order by finished_at desc', $client_id, TASK_FINISHED);
191                 task_summary('finished_paid', 'where client_id=%i && state=%i && paid=1 order by finished_at desc', $client_id, TASK_FINISHED);
192         }
193         return;
194 }