Added two new examples.
[ajatus.git] / plugins / ajatus / examples / export_expenses.php
blob122505ffac3f915543a53c4c1ec3bbab9ac6f659
1 <?php
2 require_once(realpath(dirname(__FILE__) . '/') . '/../ajatus.php');
4 $db = 'ajatus_dev_db';
5 $content_db = "{$db}_content";
7 try
9 $ajatus = new ajatus(array(
10 'host' => 'couchdb_server',
11 'db' => $db
12 ));
14 catch (ajatus_exception $e)
16 die( "Error initializing Ajatus! Reason:\n{$e}\n" );
19 $expense_reports = $ajatus->types->expense->all(array
21 'additional_map_values' => array
23 'description' => 'doc.value.description'
26 )->rows;
28 $report_count = count($expense_reports);
30 $status_for_everyone = array();
31 $totals_for_users = array();
33 foreach ($expense_reports as $expense)
35 $reporter = $expense->value->creator->val;
36 $amount = $expense->value->amount->val;
38 $description = $report->value->description->val;
40 $date_ts = ajatus_helpers_date::jsdatetime_to_unixtime($expense->value->date->val);
42 $identifier = (string) date("Y-m", $date_ts);
44 if (! isset($status_for_everyone[$identifier]))
46 $status_for_everyone[$identifier] = 0;
49 if (! isset($status_for_users[$identifier]))
51 $status_for_users[$identifier] = array();
54 if (! isset($status_for_users[$identifier][$reporter]))
56 $status_for_users[$identifier][$reporter] = 0;
59 $status_for_everyone[$identifier] += $amount;
60 $status_for_users[$identifier][$reporter] += $amount;
62 ksort($status_for_everyone);
64 foreach ($status_for_everyone as $identifier => $month_total)
66 echo "{$identifier}\n------------\n";
68 foreach ($status_for_users[$identifier] as $reporter => $total)
70 echo " {$reporter} => {$total}\n";
73 echo "Total: {$month_total}\n\n";