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