book: start-saldo + sperre bei lines>0, saldo, rec_name + tests
This commit is contained in:
parent
8fd6e0d339
commit
ae5303658e
9 changed files with 218 additions and 40 deletions
75
book.py
75
book.py
|
@ -9,6 +9,10 @@ from trytond.exceptions import UserError
|
|||
from trytond.i18n import gettext
|
||||
from trytond.transaction import Transaction
|
||||
from trytond.pool import Pool
|
||||
from trytond.report import Report
|
||||
from decimal import Decimal
|
||||
from sql.aggregate import Sum
|
||||
from sql.conditionals import Case
|
||||
|
||||
|
||||
STATES = {
|
||||
|
@ -48,6 +52,15 @@ class Book(Workflow, ModelSQL, ModelView):
|
|||
account = fields.Many2One(string='Account', select=True,
|
||||
model_name='account.account', ondelete='RESTRICT',
|
||||
states=STATES, depends=DEPENDS)
|
||||
start_balance = fields.Numeric(string='Initial Amount', required=True,
|
||||
states={
|
||||
'readonly': Or(
|
||||
STATES['readonly'],
|
||||
Bool(Eval('lines')),
|
||||
),
|
||||
}, depends=DEPENDS+['lines'])
|
||||
balance = fields.Function(fields.Numeric(string='Balance', readonly=True),
|
||||
'on_change_with_balance')
|
||||
currency = fields.Many2One(string='Currency', required=True,
|
||||
model_name='currency.currency',
|
||||
states={
|
||||
|
@ -64,6 +77,7 @@ class Book(Workflow, ModelSQL, ModelView):
|
|||
def __setup__(cls):
|
||||
super(Book, cls).__setup__()
|
||||
cls._order.insert(0, ('name', 'ASC'))
|
||||
cls._order.insert(0, ('state', 'ASC'))
|
||||
t = cls.__table__()
|
||||
cls._sql_constraints.extend([
|
||||
('state_val',
|
||||
|
@ -90,6 +104,12 @@ class Book(Workflow, ModelSQL, ModelView):
|
|||
},
|
||||
})
|
||||
|
||||
@classmethod
|
||||
def default_start_balance(cls):
|
||||
""" zero
|
||||
"""
|
||||
return Decimal('0.0')
|
||||
|
||||
@classmethod
|
||||
def default_currency(cls):
|
||||
""" currency of company
|
||||
|
@ -116,6 +136,55 @@ class Book(Workflow, ModelSQL, ModelView):
|
|||
"""
|
||||
return Transaction().user
|
||||
|
||||
@staticmethod
|
||||
def order_state(tables):
|
||||
""" edit = 0, check/done = 1
|
||||
"""
|
||||
Book2 = Pool().get('cashbook.book')
|
||||
tab_book = Book2.__table__()
|
||||
table, _ = tables[None]
|
||||
|
||||
query = tab_book.select(
|
||||
Case(
|
||||
(tab_book.state == 'open', 0),
|
||||
else_ = 1),
|
||||
where=tab_book.id==table.id
|
||||
)
|
||||
return [query]
|
||||
|
||||
def get_rec_name(self, name):
|
||||
""" name, balance, state
|
||||
"""
|
||||
return '%(name)s | %(balance)s %(symbol)s | %(state)s' % {
|
||||
'name': self.name or '-',
|
||||
'balance': Report.format_number(self.balance or 0.0, None),
|
||||
'symbol': getattr(self.currency, 'symbol', '-'),
|
||||
'state': self.state_string,
|
||||
}
|
||||
|
||||
@fields.depends('id', 'start_balance')
|
||||
def on_change_with_balance(self, name=None):
|
||||
""" compute balance
|
||||
"""
|
||||
Line = Pool().get('cashbook.line')
|
||||
tab_line = Line.__table__()
|
||||
cursor = Transaction().connection.cursor()
|
||||
|
||||
query = tab_line.select(
|
||||
Sum(tab_line.credit - tab_line.debit).as_('balance'),
|
||||
group_by=[tab_line.cashbook],
|
||||
where=tab_line.cashbook == self.id
|
||||
)
|
||||
|
||||
if self.id:
|
||||
if self.start_balance is not None:
|
||||
balance = self.start_balance
|
||||
cursor.execute(*query)
|
||||
result = cursor.fetchone()
|
||||
if result:
|
||||
balance += result[0]
|
||||
return balance
|
||||
|
||||
@classmethod
|
||||
@ModelView.button
|
||||
@Workflow.transition('open')
|
||||
|
@ -147,6 +216,12 @@ class Book(Workflow, ModelSQL, ModelView):
|
|||
actions = iter(args)
|
||||
for books, values in zip(actions, actions):
|
||||
for book in books:
|
||||
if 'start_balance' in values.keys():
|
||||
if len(book.lines) > 0:
|
||||
raise UserError(gettext(
|
||||
'cashbook.msg_book_err_startamount_with_lines',
|
||||
bookname = book.rec_name,
|
||||
))
|
||||
if book.state != 'open':
|
||||
# allow state-update, if its the only action
|
||||
if not (('state' in values.keys()) and (len(values.keys()) == 1)):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue