217 lines
5.8 KiB
PHP
217 lines
5.8 KiB
PHP
<?php
|
|
require_once('Slim/Slim.php');
|
|
\Slim\Slim::registerAutoloader();
|
|
|
|
$app=new \Slim\Slim();
|
|
|
|
$app->get('/entries/:account_id/:year/:month', 'getEntries');
|
|
$app->get('/accounts', 'getAccounts');
|
|
$app->get('/accounts/:account_id/months', 'getMonths');
|
|
$app->delete('/entries/:id', 'removeEntry');
|
|
$app->post('/entries/add', 'addEntry');
|
|
$app->put('/entries/save/:id', 'saveEntry');
|
|
$app->post('/accounts/add','addAccount');
|
|
$app->put('/accounts/save/:id','saveAccount');
|
|
$app->delete('/accounts/:id', 'removeAccount');
|
|
|
|
$app->run();
|
|
|
|
function getConnection() {
|
|
$db=new PDO("pgsql:host=localhost;dbname=accountant", "accountant", "accountant");
|
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
return $db;
|
|
}
|
|
|
|
// Return the entries
|
|
function getEntries($account_id, $year, $month) {
|
|
|
|
$day=$year."-".$month."-01";
|
|
|
|
$connection=getConnection();
|
|
|
|
$sql = <<<EOF
|
|
select
|
|
id,
|
|
value_date,
|
|
operation_date,
|
|
label,
|
|
value,
|
|
account_id,
|
|
sold,
|
|
pointedsold,
|
|
category
|
|
from (
|
|
select
|
|
*,
|
|
sum(value) over(order by value_date, operation_date, label desc, value desc) as sold,
|
|
sum(value) over(partition by operation_date is not null order by value_date, operation_date, label desc, value desc) as pointedSold
|
|
from entry
|
|
where account_id=:account_id
|
|
order by value_date desc, operation_date desc, label, value) as e
|
|
where
|
|
date_trunc('month', e.value_date) = :day
|
|
EOF;
|
|
|
|
$statement=$connection->prepare($sql);
|
|
$statement->bindParam("day", $day);
|
|
$statement->bindParam("account_id", $account_id);
|
|
|
|
$return=$statement->execute();
|
|
|
|
echo(json_encode($statement->fetchAll(PDO::FETCH_ASSOC)));
|
|
}
|
|
|
|
// Add an entry
|
|
function addEntry() {
|
|
$request = \Slim\Slim::getInstance()->request();
|
|
$entry = json_decode($request->getBody(), true);
|
|
|
|
$connection=getConnection();
|
|
|
|
$statement=$connection->prepare("insert into entry (value_date, operation_date, label, value, account_id, category) values (:value_date, :operation_date, :label, :value, :account_id, :category)");
|
|
|
|
$statement->bindParam("value_date", $entry['value_date']);
|
|
$statement->bindParam("operation_date", $entry['operation_date']);
|
|
$statement->bindParam("label", $entry['label']);
|
|
$statement->bindParam("value", $entry['value']);
|
|
$statement->bindParam("account_id", $entry['account_id']);
|
|
$statement->bindParam("category", $entry["category"]);
|
|
|
|
$return=$statement->execute();
|
|
|
|
echo("Entry saved.");
|
|
}
|
|
|
|
// Saves an entry
|
|
function saveEntry($id) {
|
|
$request = \Slim\Slim::getInstance()->request();
|
|
$entry = json_decode($request->getBody(), true);
|
|
|
|
$connection=getConnection();
|
|
|
|
$statement=$connection->prepare("update entry set value_date=:value_date, operation_date=:operation_date, label=:label, value=:value, account_id=:account_id, category=:category where id=:id");
|
|
|
|
$statement->bindParam("value_date", $entry['value_date']);
|
|
$statement->bindParam("operation_date", array_key_exists("operation_date", $entry) ? $entry['operation_date'] : null);
|
|
$statement->bindParam("label", $entry['label']);
|
|
$statement->bindParam("value", $entry['value']);
|
|
$statement->bindParam("account_id", $entry['account_id']);
|
|
$statement->bindParam("id", $entry['id']);
|
|
$statement->bindParam("category", array_key_exists("category", $entry) ? $entry["category"] : null);
|
|
|
|
$return=$statement->execute();
|
|
|
|
echo($entry['id'] . " saved.");
|
|
}
|
|
|
|
// Remove an entry
|
|
function removeEntry($id) {
|
|
$connection=getConnection();
|
|
|
|
$statement=$connection->prepare("delete from entry where id=:id");
|
|
$statement->bindParam("id", $id);
|
|
|
|
$return=$statement->execute();
|
|
|
|
echo("Entry #" . $id . " removed.");
|
|
}
|
|
|
|
// Return the accounts with their solds.
|
|
function getAccounts() {
|
|
$connection=getConnection();
|
|
|
|
$sql = <<<EOF
|
|
select
|
|
account.id,
|
|
account.name,
|
|
sum(entry.value) as future,
|
|
sum(case when entry.operation_date is not null then entry.value else cast(0 as numeric) end) as pointed,
|
|
sum(case when entry.value_date <= now() then entry.value else cast(0 as numeric) end) as current
|
|
from
|
|
account
|
|
left outer join entry on (account.id = entry.account_id)
|
|
group by
|
|
account.id
|
|
order by
|
|
account.name
|
|
EOF;
|
|
|
|
$statement=$connection->prepare($sql);
|
|
|
|
$return=$statement->execute();
|
|
|
|
echo(json_encode($statement->fetchAll(PDO::FETCH_ASSOC)));
|
|
}
|
|
|
|
// Returns the months for an account.
|
|
function getMonths($account_id) {
|
|
$connection=getConnection();
|
|
|
|
$sql = <<<EOF
|
|
select
|
|
distinct extract(year from value_date) as year,
|
|
extract(month from value_date) as month
|
|
from
|
|
entry
|
|
where
|
|
account_id = :account_id
|
|
order by
|
|
year,
|
|
month
|
|
EOF;
|
|
|
|
$statement=$connection->prepare($sql);
|
|
$statement->bindParam("account_id", $account_id);
|
|
|
|
$return=$statement->execute();
|
|
|
|
echo(json_encode($statement->fetchAll(PDO::FETCH_ASSOC)));
|
|
}
|
|
|
|
function addAccount() {
|
|
$request = \Slim\Slim::getInstance()->request();
|
|
$account = json_decode($request->getBody(), true);
|
|
|
|
$connection=getConnection();
|
|
|
|
$statement=$connection->prepare("insert into account (name) values (:name)");
|
|
|
|
$statement->bindParam("name", $account['name']);
|
|
|
|
$return=$statement->execute();
|
|
|
|
echo("Account saved.");
|
|
}
|
|
|
|
function saveAccount($id) {
|
|
$request = \Slim\Slim::getInstance()->request();
|
|
$account = json_decode($request->getBody(), true);
|
|
|
|
$connection=getConnection();
|
|
|
|
$statement=$connection->prepare("update account set name=:name where id=:id");
|
|
|
|
$statement->bindParam("name", $account['name']);
|
|
$statement->bindParam("id", $id);
|
|
|
|
$return=$statement->execute();
|
|
|
|
echo("Account #$id saved.");
|
|
}
|
|
|
|
// Remove an account
|
|
function removeAccount($id) {
|
|
$connection=getConnection();
|
|
|
|
$statement=$connection->prepare("delete from account where id=:id");
|
|
$statement->bindParam("id", $id);
|
|
|
|
$return=$statement->execute();
|
|
|
|
echo("Account #$id removed.");
|
|
}
|
|
|
|
?>
|
|
|