Add new transaction.

Add UserError when we have another line activated with the same crop and plantation.

Task #047246
This commit is contained in:
Juanjo Garcia 2022-03-28 17:06:05 +02:00
parent 5f2e65a417
commit db424f5d83
4 changed files with 47 additions and 3 deletions

View File

@ -1,9 +1,12 @@
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
from trytond.model import fields, Workflow, ModelView, ModelSQL
from trytond.pool import PoolMeta
from trytond.pyson import Eval
from decimal import Decimal
from trytond.pyson import If
from trytond.exceptions import UserError
from trytond.pool import Pool
from trytond.i18n import gettext
_STATES = {
'readonly': Eval('state') != 'draft',
@ -58,8 +61,15 @@ class AgronomicsContract(Workflow, ModelSQL, ModelView):
('draft', 'active'),
('active', 'cancelled'),
('active', 'done'),
('active', 'draft'),
('cancelled', 'draft'),
))
cls._buttons.update({
'draft': {
'invisible': ~Eval('state').in_(['cancelled', 'active']),
'icon': If(Eval('state') == 'cancelled', 'tryton-undo',
'tryton-back'),
},
'active': {
'invisible': Eval('state') != 'draft',
'icon': 'tryton-forward',
@ -96,10 +106,31 @@ class AgronomicsContract(Workflow, ModelSQL, ModelView):
return self.crop.end_date
return None
@classmethod
@ModelView.button
@Workflow.transition('draft')
def draft(cls, contracts):
pass
@classmethod
@ModelView.button
@Workflow.transition('active')
def active(cls, contracts):
pool = Pool()
ContractLine = pool.get('agronomics.contract.line')
for contract in contracts:
for line in contract.lines:
active_lines = ContractLine.search([
('contract.crop', '=', contract.crop),
('parcel', '=', line.parcel),
('contract.state', '=', 'active'),
])
if active_lines:
raise UserError(gettext(
'agronomics.msg_cant_active_contract',
contract=contract.rec_name,
parcel=line.parcel.rec_name))
pass
@classmethod

View File

@ -98,6 +98,16 @@ this repository contains the full copyright notices and license terms. -->
<field name="group" ref="group_agronomics"/>
</record>
<record model="ir.model.button" id="agronomics_contract_draft_button">
<field name="name">draft</field>
<field name="string">Draft</field>
<field name="model" search="[('model', '=', 'agronomics.contract')]"/>
</record>
<record model="ir.model.button-res.group" id="agronomics_contract_draft_button_group_agronomics">
<field name="button" ref="agronomics_contract_draft_button"/>
<field name="group" ref="group_agronomics"/>
</record>
<record model="ir.model.button" id="agronomics_contract_done_button">
<field name="name">done</field>
<field name="string">Done</field>

View File

@ -39,6 +39,8 @@ this repository contains the full copyright notices and license terms. -->
<record model="ir.message" id="msg_not_assigned_weight">
<field name="text">The weight in "%(weighing)s" is not distributed and its not being forced to analysis</field>
</record>
<record model="ir.message" id="msg_cant_active_contract">
<field name="text">The contract "%(contract)s" cant be activated because the parcel "%(parcel)s" is in another active contract.</field>
</record>
</data>
</tryton>

View File

@ -24,9 +24,10 @@ this repository contains the full copyright notices and license terms. -->
<field name="weighings" colspan="4"/>
</page>
</notebook>
<group id="buttons" col="5" colspan="4">
<group id="buttons" col="6" colspan="4">
<label name="state"/>
<field name="state"/>
<button name="draft"/>
<button name="active"/>
<button name="cancel"/>
<button name="done"/>