get_db_connection(); if(array_key_exists("month", $this->_request) && array_key_exists("year", $this->_request)) { $day=$this->_request['year']."-".$this->_request['month']."-01"; } else { $day=date("Y-m-01"); } $account=$this->_request['account']; $statement=$connection->prepare("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 order by value_date desc, operation_date desc, label, value) as e where date_trunc('month', e.value_date) = :day "); $statement->bindParam("day", $day); $statement->bindParam("account", $account); $return=$statement->execute(); if($return) { $this->response($statement->fetchAll(PDO::FETCH_ASSOC)); } else { $this->response($statement->errorInfo()[2], 500); } } protected function save_entry() { $entry=$this->_request['entry']; $connection=$this->get_db_connection(); if($entry['id'] != null) { $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"); } else { $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'] == '' ? null : $entry['operation_date']); $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", $entry['category']); $return=$statement->execute(); if($return) { $this->response(array("status"=>1, "message" => $entry['id'] . " saved.")); } else { $this->response($statement->errorInfo()[2], 500); } } protected function remove_entry() { $entry=$this->_request['entry']; $connection=$this->get_db_connection(); $statement=$connection->prepare("delete from entry where id=:id"); $statement->bindParam("id", $entry['id']); $return=$statement->execute(); if($return) { $this->response(array("status"=>1, "message"=>"Entry #" . $entry['id'] . " removed.")); } else { $this->response($statement->errorInfo()[2], 500); } } protected function get_accounts() { $connection=$this->get_db_connection(); $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(); if($return) { $result=$statement->fetchAll(PDO::FETCH_ASSOC); $this->response($result); } else { $this->response($statement->errorInfo()[2], 500); } } protected function get_months() { $account=$this->_request['account']; $connection=$this->get_db_connection(); $statement=$connection->prepare("select distinct extract(year from value_date) as year, extract(month from value_date) as month from entry where account_id = :account order by year, month"); $statement->bindParam("account", $account); $return=$statement->execute(); if($return) { $this->response($statement->fetchAll(PDO::FETCH_ASSOC)); } else { $this->response($statement->errorInfo()[2], 500); } } } $entryApi = new EntryAPI(); $entryApi->process_api(); ?>