From d361e827d462ad0721c52c9af56d2b3c47a68466 Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Wed, 9 Jan 2013 23:39:26 +0100 Subject: [PATCH] Improved UI, improved API, improved other things... --- src/html/api/entry.php | 20 ++---------------- src/html/index.html | 35 ++++++++++++++++++++------------ src/html/js/entries.js | 46 +++++++++++++++++++++--------------------- 3 files changed, 47 insertions(+), 54 deletions(-) diff --git a/src/html/api/entry.php b/src/html/api/entry.php index de509b9..ff13390 100644 --- a/src/html/api/entry.php +++ b/src/html/api/entry.php @@ -17,30 +17,14 @@ class EntryAPI extends RestAPI { $account=$this->_request['account']; - $statement=$connection->prepare("select sum(value) as futuresold, sum(case when operation_date is not null then value else cast(0 as numeric) end) as pointedsold, sum(case when value_date <= now() then value else cast(0 as numeric) end) as currentsold from entry"); - - $return=$statement->execute(); - - if(!$return) { - $this->response($statement->errorInfo()[2], 500); - } - - $pageInfos=$statement->fetch(PDO::FETCH_ASSOC); - $statement=$connection->prepare("select id, value_date, operation_date, label, value, account_id, sold, pointedSold from (select *, sum(value) over(order by value_date, operation_date, label desc, id desc) as sold, sum(value) over(partition by operation_date is not null order by value_date, operation_date, label desc, id desc) as pointedSold from entry where account_id=:account order by value_date desc, operation_date desc, label, id) as e where date_trunc('month', e.value_date) = :day "); $statement->bindParam("day", $day); $statement->bindParam("account", $account); $return=$statement->execute(); - $data=array( - "pointedSold"=>$pageInfos['pointedsold'], - "futureSold"=>$pageInfos['futuresold'], - "currentSold"=>$pageInfos['currentsold'], - "entries"=>$statement->fetchAll(PDO::FETCH_ASSOC)); - if($return) { - $this->response($data); + $this->response($statement->fetchAll(PDO::FETCH_ASSOC)); } else { $this->response($statement->errorInfo()[2], 500); } @@ -93,7 +77,7 @@ class EntryAPI extends RestAPI { protected function get_accounts() { $connection=$this->get_db_connection(); - $statement=$connection->prepare("select id, name from account order by name"); + $statement=$connection->prepare("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 join entry on (account.id = entry.account_id) group by account.id order by account.name"); $return=$statement->execute(); diff --git a/src/html/index.html b/src/html/index.html index 4862ccd..0d315c3 100644 --- a/src/html/index.html +++ b/src/html/index.html @@ -11,23 +11,32 @@
- -

Entries

+ -
+ -

+
-

Current sold:
- Future sold:
- Pointed sold:

+
-

Add entry

- -
- +  Add entry diff --git a/src/html/js/entries.js b/src/html/js/entries.js index a7038f6..f5a01fd 100644 --- a/src/html/js/entries.js +++ b/src/html/js/entries.js @@ -47,7 +47,7 @@ var ListViewModel = function() { self.entries.removeAll(); self.selectedItem(null); - $.each(data['entries'], function(index, element) { + $.each(data, function(index, element) { self.entries.push(new entry({ id: element.id, value_date: new Date(element.value_date), @@ -59,10 +59,24 @@ var ListViewModel = function() { pointedSold: element.operation_date ? element.pointedsold : '' })); }); + }); + }; - self.currentSold(data['currentSold']); - self.pointedSold(data['pointedSold']); - self.futureSold(data['futureSold']); + self.loadAccounts = function() { + $.post("api/entry.php", {action: "get_accounts"}).success(function (result) { + self.accounts(result); + + if(!self.account()){ + self.account(result[0]); + } + }); + }; + + self.loadMonths = function(account){ + $.post("api/entry.php", {action: "get_months", account: account.id}).success(function (result) { + self.months(result); + + self.month(result[result.length - 1]); }); }; @@ -155,7 +169,7 @@ var ListViewModel = function() { if (confirm('Are you sure you wish to delete this item?')) { $.post("api/entry.php", {action: "remove_entry", entry:item}).success(function (result) { self.selectedItem(null); - self.loadEntries(); + self.loadAccounts(); }); } } else { @@ -163,33 +177,19 @@ var ListViewModel = function() { } }; - self.changeMonth = function(month) { + self.selectMonth = function(month) { self.month(month); }; - self.loadAccounts = function() { - $.post("api/entry.php", {action: "get_accounts"}).success(function (result) { - self.accounts(result); - - if(! self.account()) { - self.account(result[0]); - } - }); - }; - - self.loadMonths = function(account){ - $.post("api/entry.php", {action: "get_months", account: account.id}).success(function (result) { - self.months(result); - - self.month(result[result.length - 1]); - }); + self.selectAccount = function(account) { + self.account(account); }; self.account.subscribe(self.loadMonths); self.month.subscribe(self.loadEntries); - self.loadAccounts(); + $(document).ready(self.loadAccounts); }; function dateToString(date) {