Added account management (add, edit, remove).

This commit is contained in:
Alexis Lahouze
2013-01-14 00:33:32 +01:00
parent afc2ccd0ca
commit 6b6c54a048
3 changed files with 210 additions and 35 deletions

View File

@ -4,16 +4,19 @@ require_once('Slim/Slim.php');
$app=new \Slim\Slim();
$app->get('/entries/:account_id/:year/:month', 'get_entries');
$app->get('/accounts', 'get_accounts');
$app->get('/accounts/:account_id/months', 'get_months');
$app->delete('/entries/:id', 'remove_entry');
$app->post('/entries/add', 'add_entry');
$app->put('/entries/save/:id', 'save_entry');
$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 get_connection() {
function getConnection() {
$db=new PDO("pgsql:host=localhost;dbname=accountant", "accountant", "accountant");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
@ -21,14 +24,14 @@ function get_connection() {
}
// Return the entries
function get_entries($account_id, $year, $month) {
function getEntries($account_id, $year, $month) {
$day=$year."-".$month."-01";
$connection=get_connection();
$connection=getConnection();
$sql = <<<EOF
select
select
id,
value_date,
operation_date,
@ -40,7 +43,7 @@ select
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
@ -60,11 +63,11 @@ EOF;
}
// Add an entry
function add_entry() {
function addEntry() {
$request = \Slim\Slim::getInstance()->request();
$entry = json_decode($request->getBody(), true);
$connection=get_connection();
$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)");
@ -77,15 +80,15 @@ function add_entry() {
$return=$statement->execute();
echo("Entry saved.");
echo("Entry saved.");
}
// Saves an entry
function save_entry($id) {
function saveEntry($id) {
$request = \Slim\Slim::getInstance()->request();
$entry = json_decode($request->getBody(), true);
$connection=get_connection();
$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");
@ -99,12 +102,12 @@ function save_entry($id) {
$return=$statement->execute();
echo($entry['id'] . " saved.");
echo($entry['id'] . " saved.");
}
// Remove an entry
function remove_entry($id) {
$connection=get_connection();
function removeEntry($id) {
$connection=getConnection();
$statement=$connection->prepare("delete from entry where id=:id");
$statement->bindParam("id", $id);
@ -115,8 +118,8 @@ function remove_entry($id) {
}
// Return the accounts with their solds.
function get_accounts() {
$connection=get_connection();
function getAccounts() {
$connection=getConnection();
$sql = <<<EOF
select
@ -127,7 +130,7 @@ select
sum(case when entry.value_date <= now() then entry.value else cast(0 as numeric) end) as current
from
account
join entry on (account.id = entry.account_id)
left outer join entry on (account.id = entry.account_id)
group by
account.id
order by
@ -142,8 +145,8 @@ EOF;
}
// Returns the months for an account.
function get_months($account_id) {
$connection=get_connection();
function getMonths($account_id) {
$connection=getConnection();
$sql = <<<EOF
select
@ -165,5 +168,49 @@ EOF;
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.");
}
?>