Skip to content

Commit

Permalink
Merge pull request #117 from openoakland/feature/api-funds
Browse files Browse the repository at this point in the history
Add General Fund amounts to API endpoints
  • Loading branch information
macfarlandian authored Aug 31, 2017
2 parents c3a4094 + dfb3015 commit b7032bd
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 45 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,13 @@ The API we have built is completely independent of the Open Budget Oakland site,
### Using the plugin to generate the API

The WordPress plugin (OBO Custom Routes) that generates our API can be installed and used on any WordPress site, providing a database table with the expected column names is present. Currently, the plugin is hard-coded to expect a table called ```oakland_budget_items```. Obviously, that would be something you'd want to change if you were to use the plugin for another project. Additionally, database queries can easily be altered to fit a different table structure and to create different kinds of endpoints with a bit of PHP skill.

### Developing locally

To develop new features for the API, you may want to run Wordpress locally.
This repo includes a configuration file for doing so with [Docker Compose](https://docs.docker.com/compose/).
With Docker Compose installed, simply run `docker-compose up` in `wordpress plugin for custom API endpoints/`
to activate linked containers for Wordpress, MySQL, and PhpMyAdmin. The Wordpress container will
mount that directory as though it were Wordpress' `plugins/` directory, allowing your edits to
the plugin files in `obo_custom_routes/` to be reflected in your Wordpress instance. (Additional plugins that
are not part of this repository will appear in that directory; they should be ignored by git.)
4 changes: 4 additions & 0 deletions wordpress plugin for custom API endpoints/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*
!.gitignore
!obo_custom_routes/
!docker-compose.yml
34 changes: 34 additions & 0 deletions wordpress plugin for custom API endpoints/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
version: "3"

services:
web:
image: wordpress
links:
- mysql
environment:
- WORDPRESS_DB_PASSWORD=password
ports:
- "8080:80"
working_dir: /var/www/html
volumes:
- ./:/var/www/html/wp-content/plugins/
mysql:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=wordpress
volumes:
- obo_datavolume:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
restart: always
links:
- mysql:db
ports:
- 8888:80
volumes:
- /sessions

volumes:
obo_datavolume:
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,19 @@ public function get_year_totals( $request ) {
$data = array();

$query_e = $wpdb->prepare("
SELECT budget_type, fiscal_year_range, SUM(amount_num) AS total
FROM $table
WHERE account_type = %s
GROUP BY budget_type, fiscal_year_range
", 'Expense' );
SELECT t1.budget_type, t1.fiscal_year_range, total, general_fund
FROM (
SELECT budget_type, fiscal_year_range, SUM(amount_num) AS total
FROM $table
WHERE account_type = %s
GROUP BY budget_type, fiscal_year_range) t1
LEFT JOIN (
SELECT budget_type, fiscal_year_range, SUM(amount_num) AS general_fund
FROM $table
WHERE account_type = %s AND fund_code = %d
GROUP BY budget_type, fiscal_year_range) t2
USING (budget_type, fiscal_year_range)
", 'Expense', 'Expense', 1010);
$expense_items = $wpdb->get_results( $query_e );
// Check to see that there is something to send
if ( empty($expense_items) ) {
Expand All @@ -136,11 +144,19 @@ public function get_year_dept_totals( $request ) {
$data = array();

$query_e = $wpdb->prepare("
SELECT budget_type, fiscal_year_range, department, SUM(amount_num) AS total
FROM $table
WHERE account_type = %s AND fiscal_year_range = %s
GROUP BY budget_type, department
", 'Expense', $fiscal_year_range );
SELECT t1.budget_type, t1.fiscal_year_range, department, total, general_fund
FROM (
SELECT budget_type, fiscal_year_range, department, SUM(amount_num) AS total
FROM $table
WHERE account_type = %s AND fiscal_year_range = %s
GROUP BY budget_type, department) t1
LEFT JOIN (
SELECT budget_type, fiscal_year_range, department, SUM(amount_num) AS general_fund
FROM $table
WHERE account_type = %s AND fiscal_year_range = %s AND fund_code = %d
GROUP BY budget_type, department) t2
USING (budget_type, department)
", 'Expense', $fiscal_year_range, 'Expense', $fiscal_year_range, 1010 );
$expense_items = $wpdb->get_results( $query_e );
// Check to see that there is something to send
if ( empty($expense_items) ) {
Expand All @@ -167,11 +183,20 @@ public function get_year_account_totals( $request ) {
$data = array();

$query_e = $wpdb->prepare("
SELECT budget_type, fiscal_year_range, account_description, SUM(amount_num) AS total
FROM $table
WHERE account_type = %s AND fiscal_year_range = %s
GROUP BY budget_type, account_description
", 'Expense', $fiscal_year_range );
SELECT t1.budget_type, t1.fiscal_year_range, account_description, total, general_fund
FROM (
SELECT budget_type, fiscal_year_range, account_description, SUM(amount_num) AS total
FROM $table
WHERE account_type = %s AND fiscal_year_range = %s
GROUP BY budget_type, account_description) t1
LEFT JOIN (
SELECT budget_type, fiscal_year_range, account_description, SUM(amount_num) AS general_fund
FROM $table
WHERE account_type = %s AND fiscal_year_range = %s AND fund_code = %d
GROUP BY budget_type, account_description) t2
USING (budget_type, account_description)
", 'Expense', $fiscal_year_range, 'Expense',
$fiscal_year_range, 1010);
$expense_items = $wpdb->get_results( $query_e );
// Check to see that there is something to send
if ( empty($expense_items) ) {
Expand All @@ -198,11 +223,19 @@ public function get_year_accountcat_totals( $request ) {
$data = array();

$query_e = $wpdb->prepare("
SELECT budget_type, fiscal_year_range, account_category, SUM(amount_num) AS total
FROM $table
WHERE account_type = %s AND fiscal_year_range = %s
GROUP BY budget_type, account_category
", 'Expense', $fiscal_year_range );
SELECT t1.budget_type, t1.fiscal_year_range, account_category, total, general_fund
FROM (
SELECT budget_type, fiscal_year_range, account_category, SUM(amount_num) AS total
FROM $table
WHERE account_type = %s AND fiscal_year_range = %s
GROUP BY budget_type, account_category) t1
LEFT JOIN (
SELECT budget_type, fiscal_year_range, account_category, SUM(amount_num) AS general_fund
FROM $table
WHERE account_type = %s AND fiscal_year_range = %s AND fund_code = %d
GROUP BY budget_type, account_category) t2
USING (budget_type, account_category)
", 'Expense', $fiscal_year_range, 'Expense', $fiscal_year_range, 1010 );
$expense_items = $wpdb->get_results( $query_e );
// Check to see that there is something to send
if ( empty($expense_items) ) {
Expand Down Expand Up @@ -288,6 +321,7 @@ public function prepare_year_totals_for_response( $row, $request ) {
$data['budget_type'] = ( ! empty( $schema['properties']['budget_type'] ) )? $row->budget_type : '' ;
$data['fiscal_year_range'] = ( ! empty( $schema['properties']['fiscal_year_range'] ) )? $row->fiscal_year_range : '' ;
$data['total'] = ( ! empty( $schema['properties']['total'] ) )? $row->total : '' ;
$data['general_fund'] = ( ! empty( $schema['properties']['general_fund'] ) ) ? $row->general_fund : '';
//Set context
$data = $this->add_additional_fields_to_object( $data, $request );
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
Expand Down Expand Up @@ -317,6 +351,7 @@ public function prepare_year_dept_totals_for_response( $row, $request ) {
$data['fiscal_year_range'] = ( ! empty( $schema['properties']['fiscal_year_range'] ) )? $row->fiscal_year_range : '' ;
$data['department'] = ( ! empty( $schema['properties']['department'] ) )? $row->department : '' ;
$data['total'] = ( ! empty( $schema['properties']['total'] ) )? $row->total : '' ;
$data['general_fund'] = ( ! empty( $schema['properties']['general_fund'] ) ) ? $row->general_fund : '';
//Set context
$data = $this->add_additional_fields_to_object( $data, $request );
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
Expand Down Expand Up @@ -346,6 +381,7 @@ public function prepare_year_account_totals_for_response( $row, $request ) {
$data['fiscal_year_range'] = ( ! empty( $schema['properties']['fiscal_year_range'] ) )? $row->fiscal_year_range : '' ;
$data['account_description'] = ( ! empty( $schema['properties']['account_description'] ) )? $row->account_description : '' ;
$data['total'] = ( ! empty( $schema['properties']['total'] ) )? $row->total : '' ;
$data['general_fund'] = ( ! empty( $schema['properties']['general_fund'] ) ) ? $row->general_fund : '';
//Set context
$data = $this->add_additional_fields_to_object( $data, $request );
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
Expand Down Expand Up @@ -375,6 +411,7 @@ public function prepare_year_accountcat_totals_for_response( $row, $request ) {
$data['fiscal_year_range'] = ( ! empty( $schema['properties']['fiscal_year_range'] ) )? $row->fiscal_year_range : '' ;
$data['account_category'] = ( ! empty( $schema['properties']['account_category'] ) )? $row->account_category : '' ;
$data['total'] = ( ! empty( $schema['properties']['total'] ) )? $row->total : '' ;
$data['general_fund'] = ( ! empty( $schema['properties']['general_fund'] ) ) ? $row->general_fund : '';
//Set context
$data = $this->add_additional_fields_to_object( $data, $request );
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
Expand Down Expand Up @@ -464,7 +501,13 @@ public function get_item_schema() {
'readonly' => true
),
'total' => array(
'description' => __("Sum of all the expense line items in the budget"),
'description' => __("Sum of all the expense line items in the budget."),
'type' => 'integer',
'context' => array('view', 'embed'),
'readonly' => true
),
'general_fund' => array(
'description' => __("Sum of all the expense line items in the General Fund only."),
'type' => 'integer',
'context' => array('view', 'embed'),
'readonly' => true
Expand Down
Loading

0 comments on commit b7032bd

Please sign in to comment.