<?php
/**
 * Class represents records from table selfservice_balance
 * {autogenerated}
 * @property int $balance_id PK
 * @property int $user_id FK user
 * @property int $type_id FK selfservice_type
 * @property int $balance
 * {/autogenerated}
 */
class SelfserviceBalance extends Am_Record
{
}

/** @extends Am_Table<SelfserviceBalance> */
class SelfserviceBalanceTable extends Am_Table
{
    protected $_key = 'balance_id';
    protected $_table = '?_selfservice_balance';

    function balance(User $user, $type_id = null)
    {
        return $this->_db->selectCell("SELECT SUM(balance)
            FROM {$this->_table}
            WHERE user_id = ?
            {AND type_id = ?}",
            $user->pk(), is_null($type_id) ? DBSIMPLE_SKIP : $type_id);
    }
}