Simplify workflow engine

review256001
This commit is contained in:
Cédric Krier committed 2012-03-25 00:17:31 +01:00
1 parent c62bae0a8f
commit 78892320b2
4 files changed
+118 -109

No files matched your search

+1 -1
View File
@@ -12,7 +12,7 @@ minor_version = int(minor_version)
requires = []
for dep in info.get('depends', []):
if not re.match(r'(ir|res|workflow|webdav)(\W|$)', dep):
if not re.match(r'(ir|res|webdav)(\W|$)', dep):
requires.append('trytond_%s >= %s.%s, < %s.%s' %
(dep, major_version, minor_version, major_version,
minor_version + 1))
+61 -44
View File
@@ -1,7 +1,7 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
from decimal import Decimal
from trytond.model import ModelWorkflow, ModelView, ModelSQL, fields
from trytond.model import Workflow, ModelView, ModelSQL, fields
from trytond.pyson import Eval, If
from trytond.transaction import Transaction
from trytond.backend import TableHandler
@@ -11,7 +11,7 @@ _STATES = {'readonly': Eval('state') != 'draft'}
_DEPENDS = ['state']
class Statement(ModelWorkflow, ModelSQL, ModelView):
class Statement(Workflow, ModelSQL, ModelView):
'Account Statement'
_name = 'account.statement'
_description = __doc__
@@ -63,13 +63,30 @@ class Statement(ModelWorkflow, ModelSQL, ModelView):
def __init__(self):
super(Statement, self).__init__()
self._rpc.update({
'draft_workflow': True,
})
self._order[0] = ('id', 'DESC')
self._error_messages.update({
'wrong_end_balance': 'End Balance must be %s!',
})
self._transitions |= set((
('draft', 'validated'),
('validated', 'posted'),
('validated', 'cancel'),
('cancel', 'draft'),
))
self._buttons.update({
'draft': {
'invisible': Eval('state') != 'cancel',
},
'validate': {
'invisible': Eval('state') != 'draft',
},
'post': {
'invisible': Eval('state') != 'validated',
},
'cancel': {
'invisible': Eval('state') != 'validated',
},
})
def init(self, module_name):
cursor = Transaction().cursor
@@ -256,51 +273,51 @@ class Statement(ModelWorkflow, ModelSQL, ModelView):
amount_to_pay - abs(line['amount'])
return res
def wkf_validated(self, statement):
@ModelView.button
@Workflow.transition('draft')
def draft(self, ids):
pass
@ModelView.button
@Workflow.transition('validated')
def validate(self, ids):
statement_line_obj = Pool().get('account.statement.line')
lang_obj = Pool().get('ir.lang')
computed_end_balance = statement.start_balance
for line in statement.lines:
computed_end_balance += line.amount
if computed_end_balance != statement.end_balance:
lang_id, = lang_obj.search([
('code', '=', Transaction().language),
])
lang = lang_obj.browse(lang_id)
for statement in self.browse(ids):
computed_end_balance = statement.start_balance
for line in statement.lines:
computed_end_balance += line.amount
if computed_end_balance != statement.end_balance:
lang_id, = lang_obj.search([
('code', '=', Transaction().language),
])
lang = lang_obj.browse(lang_id)
amount = lang_obj.format(lang,
'%.' + str(statement.journal.currency.digits) + 'f',
computed_end_balance, True)
self.raise_user_error('wrong_end_balance', error_args=(amount,))
for line in statement.lines:
statement_line_obj.create_move(line)
self.write(statement.id, {
'state':'validated',
})
amount = lang_obj.format(lang,
'%.' + str(statement.journal.currency.digits) + 'f',
computed_end_balance, True)
self.raise_user_error('wrong_end_balance', error_args=(amount,))
for line in statement.lines:
statement_line_obj.create_move(line)
def wkf_posted(self, statement):
@ModelView.button
@Workflow.transition('posted')
def post(self, ids):
statement_line_obj = Pool().get('account.statement.line')
statement_line_obj.post_move(statement.lines)
self.write(statement.id, {
'state':'posted',
})
statements = self.browse(ids)
lines = [l for s in statements for l in s.lines]
statement_line_obj.post_move(lines)
def wkf_cancel(self, statement):
@ModelView.button
@Workflow.transition('cancel')
def cancel(self, ids):
statement_line_obj = Pool().get('account.statement.line')
statement_line_obj.delete_move(statement.lines)
self.write(statement.id, {
'state':'cancel',
})
def draft_workflow(self, ids):
self.workflow_trigger_create(ids)
self.write(ids, {
'state': 'draft',
})
return True
statements = self.browse(ids)
lines = [l for s in statements for l in s.lines]
statement_line_obj.delete_move(lines)
Statement()
@@ -426,10 +443,7 @@ class Line(ModelSQL, ModelView):
def create_move(self, line):
'''
Create move for the statement line
:param line: a BrowseRecord of the line
:return: the move id
Create move for the statement line and return move id if created.
'''
pool = Pool()
move_obj = pool.get('account.move')
@@ -439,6 +453,9 @@ class Line(ModelSQL, ModelView):
move_line_obj = pool.get('account.move.line')
lang_obj = pool.get('ir.lang')
if line.move:
return
period_id = period_obj.find(line.statement.company.id,
date=line.date)
+51 -55
View File
@@ -43,15 +43,14 @@ this repository contains the full copyright notices and license terms. -->
<label name="state"/>
<field name="state"/>
<group col="4" colspan="2" id="buttons">
<button string="Validate" name="validated"
states="{'invisible': Not(Equal(Eval('state'), 'draft'))}"/>
<button string="Validate" name="validate"
type="object"/>
<button string="Cancel" name="cancel"
states="{'invisible': Not(Equal(Eval('state'), 'validated'))}"/>
<button string="Post" name="posted"
states="{'invisible': Not(Equal(Eval('state'), 'validated'))}"/>
<button string="Reset to Draft" name="draft_workflow"
type="object"
states="{'invisible': Not(Equal(Eval('state'), 'cancel'))}"/>
type="object"/>
<button string="Post" name="post"
type="object"/>
<button string="Reset to Draft" name="draft"
type="object"/>
</group>
</group>
</page>
@@ -226,53 +225,6 @@ this repository contains the full copyright notices and license terms. -->
</field>
</record>
<!-- Statement workflow -->
<record model="workflow" id="wkf_statement">
<field name="name">Statement</field>
<field name="model">account.statement</field>
<field name="on_create" eval="True"/>
</record>
<record model="workflow.activity" id="statement_act_draft">
<field name="workflow" ref="wkf_statement"/>
<field name="flow_start" eval="True"/>
<field name="name">Draft</field>
</record>
<record model="workflow.activity" id="statement_act_validated">
<field name="workflow" ref="wkf_statement"/>
<field name="method">wkf_validated</field>
<field name="name">Validated</field>
</record>
<record model="workflow.activity" id="statement_act_posted">
<field name="workflow" ref="wkf_statement"/>
<field name="method">wkf_posted</field>
<field name="name">Posted</field>
<field name="flow_stop" eval="True"/>
</record>
<record model="workflow.activity" id="statement_act_cancel">
<field name="workflow" ref="wkf_statement"/>
<field name="method">wkf_cancel</field>
<field name="name">Canceled</field>
<field name="flow_stop" eval="True"/>
</record>
<record model="workflow.transition" id="statement_trans_draft_validated">
<field name="act_from" ref="statement_act_draft"/>
<field name="act_to" ref="statement_act_validated"/>
<field name="group" ref="account.group_account"/>
<field name="signal">validated</field>
</record>
<record model="workflow.transition" id="statement_trans_validated_posted">
<field name="act_from" ref="statement_act_validated"/>
<field name="act_to" ref="statement_act_posted"/>
<field name="group" ref="account.group_account"/>
<field name="signal">posted</field>
</record>
<record model="workflow.transition" id="statement_trans_validated_cancel">
<field name="act_from" ref="statement_act_validated"/>
<field name="act_to" ref="statement_act_cancel"/>
<field name="group" ref="account.group_account_admin"/>
<field name="signal">cancel</field>
</record>
<record model="ir.model.access" id="access_statement_account_admin">
<field name="model" search="[('model', '=', 'account.statement')]"/>
<field name="group" ref="account.group_account_admin"/>
@@ -323,5 +275,49 @@ this repository contains the full copyright notices and license terms. -->
<field name="perm_delete" eval="True"/>
</record>
<record model="ir.model.button" id="statement_draft_button">
<field name="name">draft</field>
<field name="model"
search="[('model', '=', 'account.statement')]"/>
</record>
<record model="ir.model.button-res.group"
id="statement_draft_button_group_account">
<field name="button" ref="statement_draft_button"/>
<field name="group" ref="account.group_account"/>
</record>
<record model="ir.model.button" id="statement_validate_button">
<field name="name">validate</field>
<field name="model"
search="[('model', '=', 'account.statement')]"/>
</record>
<record model="ir.model.button-res.group"
id="statement_validate_button_group_account">
<field name="button" ref="statement_validate_button"/>
<field name="group" ref="account.group_account"/>
</record>
<record model="ir.model.button" id="statement_post_button">
<field name="name">post</field>
<field name="model"
search="[('model', '=', 'account.statement')]"/>
</record>
<record model="ir.model.button-res.group"
id="statement_post_button_group_account">
<field name="button" ref="statement_post_button"/>
<field name="group" ref="account.group_account"/>
</record>
<record model="ir.model.button" id="statement_cancel_button">
<field name="name">cancel</field>
<field name="model"
search="[('model', '=', 'account.statement')]"/>
</record>
<record model="ir.model.button-res.group"
id="statement_cancel_button_group_account">
<field name="button" ref="statement_cancel_button"/>
<field name="group" ref="account.group_account_admin"/>
</record>
</data>
</tryton>
+5 -9
View File
@@ -25,7 +25,7 @@ Install account_statement and account_invoice::
>>> modules = Module.find([
... ('name', 'in', ('account_statement', 'account_invoice')),
... ])
>>> Module.button_install([x.id for x in modules], config.context)
>>> Module.install([x.id for x in modules], config.context)
>>> Wizard('ir.module.module.install_upgrade').execute('upgrade')
Create company::
@@ -148,8 +148,7 @@ Create 2 customer invoices::
>>> invoice_line.account = revenue
>>> invoice_line.description = 'Test'
>>> customer_invoice1.save()
>>> Invoice.workflow_trigger_validate(customer_invoice1.id, 'open',
... config.context)
>>> Invoice.open([customer_invoice1.id], config.context)
>>> customer_invoice1.state
u'open'
@@ -163,8 +162,7 @@ Create 2 customer invoices::
>>> invoice_line.account = revenue
>>> invoice_line.description = 'Test'
>>> customer_invoice2.save()
>>> Invoice.workflow_trigger_validate(customer_invoice2.id, 'open',
... config.context)
>>> Invoice.open([customer_invoice2.id], config.context)
>>> customer_invoice2.state
u'open'
@@ -180,8 +178,7 @@ Create 1 supplier invoices::
>>> invoice_line.account = expense
>>> invoice_line.description = 'Test'
>>> supplier_invoice.save()
>>> Invoice.workflow_trigger_validate(supplier_invoice.id, 'open',
... config.context)
>>> Invoice.open([supplier_invoice.id], config.context)
>>> supplier_invoice.state
u'open'
@@ -252,8 +249,7 @@ Paid 50 to supplier::
Validate statement::
>>> Statement.workflow_trigger_validate(statement.id, 'validated',
... config.context)
>>> Statement.validate([statement.id], config.context)
>>> statement.state
u'validated'