JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
update wfpl and templates
[contractor-progress.git] / index.php
index 121f099..0724a48 100644 (file)
--- a/index.php
+++ b/index.php
@@ -1,33 +1,72 @@
 <?php
 
-require_once('code/tasks.php');
+#  Copyright (C) 2008  Jason Woofenden
+#
+#  This program is free software: you can redistribute it and/or modify
+#  it under the terms of the GNU Affero General Public License as published by
+#  the Free Software Foundation, either version 3 of the License, or
+#  (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU Affero General Public License for more details.
+#
+#  You should have received a copy of the GNU Affero General Public License
+#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-function enc_money($float) {
-       return format_money($float, $cents = true);
-}
+require_once('code/tasks.php');
 
 function index_main() {
        if(!logged_in()) {
-               return './login';
+               return 'login';
        }
        $ret = _index_main();
        if($ret) {
                return $ret;
        }
-       tem_show('main_body');
 }
 
-function task_summary($tem_prefix, $where_clause) {
-       $rows = db_get_rows('tasks', 'id,price,title,state', $where_clause);
+# pass multiple argumens for where-clause and printf-args just like db_get_rows()
+function task_summary($tem_prefix, $where_clause/*, ... */) {
+       $args = func_get_args();
+       $args = array_slice($args, 1);
+       array_unshift($args, 'tasks', 'id,price,title,state,client_id,paid');
+       $rows = call_user_func_array('db_get_rows', $args);
+       $num_rows = 0;
        if($rows) {
+               $num_rows = count($rows);
+               if($num_rows > 1) {
+                       tem_show($tem_prefix . '_plural');
+               } else {
+                       tem_show($tem_prefix . '_singular');
+               }
                $total = 0.0;
                foreach($rows as $row) {
-                       list($id, $price, $title, $state) = $row;
+                       list($id, $price, $title, $state, $client_id, $paid) = $row;
                        tem_set('task_id', $id);
                        tem_set('task_title', $title);
                        tem_set('task_price', $price);
                        tem_set('task_state', task_state_pretty($state));
+                       if(logged_in_as_contractor()) {
+                               tem_set('client', db_get_value('people', 'username', 'where id=%i', $client_id));
+                               if($tem_prefix == 'queue') {
+                                       tem_show('queued_for');
+                               }
+                       }
+                       if($num_rows > 1 && $tem_prefix == 'queue') {
+                               tem_show('not_the_only_queued');
+                       }
                        tem_show($tem_prefix . '_row');
+                       if(!$paid && isset($GLOBALS['unpaid_totals'])) {
+                               if(!isset($GLOBALS['unpaid_totals'][$client_id])) {
+                                       $GLOBALS['unpaid_totals'][$client_id] = array();
+                               }
+                               if(!isset($GLOBALS['unpaid_totals'][$client_id][$tem_prefix])) {
+                                       $GLOBALS['unpaid_totals'][$client_id][$tem_prefix] = 0;
+                               }
+                               $GLOBALS['unpaid_totals'][$client_id][$tem_prefix] += $price;
+                       }
                        $total += $price;
                }
 
@@ -35,15 +74,120 @@ function task_summary($tem_prefix, $where_clause) {
 
                tem_show($tem_prefix);
        }
+
+       return $num_rows;
+}
+
+# pass multi-dimensional hash arr[client_id][status].
+# status strings: 'finished_untested', 'finished_unpaid', 'queue'
+function totals_summary($arr) {
+       if($arr) {
+               $total_finished_untested = 0;
+               $total_finished_unpaid = 0;
+               $total_queue = 0;
+               $total_working = 0;
+               foreach($arr as $client_id => $t) {
+                       $finished_untested = 0;
+                       $finished_unpaid = 0;
+                       $queue = 0;
+                       $working = 0;
+                       if(isset($t['finished_untested'])) {
+                               $finished_untested = $t['finished_untested'];
+                       }
+                       if(isset($t['finished_unpaid'])) {
+                               $finished_unpaid = $t['finished_unpaid'];
+                       }
+                       if(isset($t['queue'])) {
+                               $queue = $t['queue'];
+                       }
+                       if(isset($t['do_finish'])) {
+                               $working = $t['do_finish'];
+                       }
+                       tem_set('tested_total', $finished_unpaid);
+                       tem_set('done_total', $finished_unpaid + $finished_untested);
+                       tem_set('not_tested_total', $finished_untested);
+                       tem_set('queued_total', $queue + $working);
+                       tem_set('client', db_get_value('people', 'username', 'where id=%i', $client_id));
+                       tem_show('unpaid_totals_row');
+                       $total_finished_untested += $finished_untested;
+                       $total_finished_unpaid += $finished_unpaid;
+                       $total_queue += $queue;
+                       $total_working += $working;
+               }
+
+               tem_set('tested_total', $total_finished_unpaid);
+               tem_set('done_total', $total_finished_unpaid + $total_finished_untested);
+               tem_set('not_tested_total', $total_finished_untested);
+               tem_set('queued_total', $total_queue + $total_working);
+               tem_set('client', 'all');
+               tem_show('unpaid_totals_row');
+
+               tem_show('unpaid_totals');
+       }
 }
 
 function _index_main() {
-       task_summary('needs_attention', 'where state=' . TASK_DRAFT . ' || state=' . TASK_NEEDS_CLARIFICATION . ' || state=' . TASK_NEEDS_GO_AHEAD . ' || state=' . TASK_NEEDS_TESTING . ' order by id');
-       task_summary('queue', 'where state=' . TASK_QUEUED . ' order by ord');
-       task_summary('jason', 'where state=' . TASK_NEEDS_QUOTE . ' || state=' . TASK_WORKING . ' || state=' . TASK_BUG . ' order by id desc');
-       #task_summary('jason_pricing', 'where state=' . TASK_NEEDS_QUOTE . ' order by id desc');
-       #task_summary('jason_working', 'where state=' . TASK_WORKING . ' order by id desc');
-       task_summary('finished_unpaid', 'where state=' . TASK_FINISHED . ' && paid = 0 order by id desc');
-       task_summary('finished_paid', 'where state=' . TASK_FINISHED . ' && paid = 1 order by id desc');
+       $client_id = logged_in();
+       $ever_was_contractor = ever_was_contractor();
+
+       $contractor_full_name = db_get_value('people', 'name', 'where id=1');
+       if(!$contractor_full_name) {
+               die('To finish installing this site, make yourself an account in the people table with id=1');
+       }
+       list($contractor_name,) = explode(' ', $contractor_full_name, 2);
+       tem_set('contractor_name', $contractor_name);
+       tem_set('contractor_full_name', $contractor_full_name);
+
+
+       # make sure the client (not the contractor) has filled out the tiny agreement
+       if(!$ever_was_contractor) {
+               $tiny_agreement = db_get_value('people', 'tiny_agreement', 'where id=%i', $client_id);
+               if($tiny_agreement < 30 || $tiny_agreement > 999) {
+                       return './tiny_agreement';
+               }
+       }
+
+       if($ever_was_contractor) {
+               tem_show('su_link');
+       }
+
+       if(logged_in_as_contractor()) {
+               tem_show('contractor_links');
+               tem_show('contractor_todo');
+
+               $GLOBALS['unpaid_totals'] = array();
+               task_summary('do_finish', 'where state=%i order by id desc', TASK_WORKING); # this affects queued_total
+               task_summary('do_fix', 'where state=%i order by client_id desc', TASK_BUG);
+               task_summary('do_price', 'where state=%i order by client_id desc', TASK_NEEDS_QUOTE);
+
+               task_summary('finished_untested', 'where state=%i order by client_id, finished_at desc', TASK_NEEDS_TESTING);
+               task_summary('finished_unpaid', 'where state=%i && paid=0 order by finished_at desc', TASK_FINISHED);
+               task_summary('queue', 'where state=%i order by client_id, ord desc', TASK_QUEUED);
+               totals_summary($GLOBALS['unpaid_totals']);
+               task_summary('finished_paid', 'where state=%i && paid = 1 order by finished_at desc limit 20', TASK_FINISHED);
+       } else {
+               tem_show('not_contractor_links');
+               $num_rows = 0;
+               $num_rows += task_summary('do_approve_price', 'where client_id=%i && state=%i order by id', $client_id, TASK_NEEDS_GO_AHEAD);
+               $num_rows += task_summary('do_clarify', 'where client_id=%i && state=%i order by id', $client_id, TASK_NEEDS_CLARIFICATION);
+               $num_rows += task_summary('do_test', 'where client_id=%i && state=%i order by id', $client_id, TASK_NEEDS_TESTING);
+               $num_rows += task_summary('do_draft', 'where client_id=%i && state=%i order by id', $client_id, TASK_DRAFT);
+               $num_rows += task_summary('do_on_hold', 'where client_id=%i && state=%i order by id', $client_id, TASK_ON_HOLD);
+
+               if($num_rows == 0) {
+                       tem_show('nothing_needs_your_attention');
+               } elseif($num_rows == 1) {
+                       tem_show('this_needs_your_attention');
+               } else {
+                       tem_show('these_need_your_attention');
+               }
+
+               task_summary('queue', 'where client_id=%i && state=%i order by ord desc', $client_id, TASK_QUEUED);
+
+               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);
+
+               task_summary('finished_unpaid', 'where client_id=%i && state=%i && paid=0 order by finished_at desc', $client_id, TASK_FINISHED);
+               task_summary('finished_paid', 'where client_id=%i && state=%i && paid=1 order by finished_at desc', $client_id, TASK_FINISHED);
+       }
        return;
 }