$value) { $transaction[$field] = sql_escape_string($value, 0); } $unique = sql_fetch_one_cell("select id from expense where unique_id = {$transaction['unique_id']}"); if($unique !== false) { $expense = &new Expense($unique); return $expense; } $enteredBy = sql_fetch_one_cell("select id from person where name like {$transaction['entered_by']}"); if($enteredBy === false) { $sql = "insert into person (name) values({$transaction['entered_by']})"; $enteredBy = sql_insert($sql, 'person_id_seq'); } $sql = "insert into expense (amount, store, entered_by, date, unique_id) values({$transaction['amount']}, {$transaction['store']}, $enteredBy, {$transaction['date']}, {$transaction['unique_id']})"; $id = sql_insert($sql, 'expense_id_seq'); $expense = &new Expense($id); return $expense; } //static function insert($info) { foreach($info as $field => $data) { $fields[] = $field; $values[] = ticks($data); } $fields = implode(', ', $fields); $values = implode(', ', $values); $sql = "insert into expense ($fields) values($values)"; $id = sql_insert($sql, 'expense_id_seq'); return $id; } //static function &getExpenseList($month, $year) { $endday = Category::getEndDay($month, $year); $sql = "select e.id, c.id as c_id, c.name as category, c.color as color, (e.amount / span_months) as amount, e.deleted as deleted, e.store, p.name as entered_by, e.date from expense e inner join person p on e.entered_by = p.id left outer join category c on e.category_id = c.id where date + (span_months - 1 || ' months')::interval >= '$year-$month-01' and date < '$year-$month-$endday' and e.deleted = 0 order by date"; $answer = sql_fetch_map($sql, "id"); return $answer; } //static function &getExpenseListByCatId($month, $year, $catId) { $endday = Category::getEndDay($month, $year); $sql = "select e.id, c.id as c_id, c.name as category, c.color as color, (e.amount / span_months) as amount, e.deleted as deleted, e.store, p.name as entered_by, e.date from expense e inner join person p on e.entered_by = p.id left outer join category c on e.category_id = c.id where date + (span_months - 1 || ' months')::interval >= '$year-$month-01' and date < '$year-$month-$endday' and e.deleted = 0 and c.id = $catId order by date"; $answer = sql_fetch_map($sql, "id"); return $answer; } //static function getBlankInfo() { return array('date' => null, 'amount' => null, 'comment' => null, 'store' => null, 'entered_by' => null, 'category_id' => null, 'span_months' => null); } function Expense($id) { $this->id = $id; } function setBasicItem($field, $value) { $value = sql_escape_string($value); sql_query("update expense set $field = $value where id = $this->id"); } function getInfo() { $sql = "select * from expense where id = {$this->id}"; $info = sql_fetch_one($sql); return $info; } function delete() { $this->setBasicItem('deleted', 1); } }