Add error exceptions

This commit is contained in:
Oscar 2021-11-04 23:30:45 -05:00
parent f92ad63177
commit 53bf0b149d
7 changed files with 137 additions and 41 deletions

View File

@ -1,9 +1,2 @@
# 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 trytond.exceptions import UserError, UserWarning
from trytond.model.exceptions import ValidationError
class PlannedDateProductionError(ValidationError):
pass

View File

@ -4,7 +4,10 @@ this repository contains the full copyright notices and license terms. -->
<tryton>
<data grouped="1">
<record model="ir.message" id="msg_planned_date_required">
<field name="text">Planned date is required</field>
<field name="text">Planned date is required!</field>
</record>
<record model="ir.message" id="msg_category_account_stock">
<field name="text">Missing account category for product: "%(product)s"</field>
</record>
</data>
</tryton>

80
product.py Normal file
View File

@ -0,0 +1,80 @@
# 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 trytond.model import fields
from trytond.pyson import Eval
from trytond.pool import PoolMeta, Pool
from trytond.modules.account_product.product import account_used
account_names = ['account_stock', 'account_production']
class Category(metaclass=PoolMeta):
__name__ = 'product.category'
account_production = fields.MultiValue(fields.Many2One('account.account',
'Account Production', domain=[
('closed', '!=', True),
('type.stock', '=', True),
('company', '=', Eval('context', {}).get('company', -1)),
],
states={
'invisible': (~Eval('context', {}, ).get('company')
| Eval('account_parent')
| ~Eval('accounting', False)),
},
depends=['account_parent', 'accounting']))
@classmethod
def multivalue_model(cls, field):
pool = Pool()
if field in account_names:
return pool.get('product.category.account')
return super(Category, cls).multivalue_model(field)
@property
@account_used('account_production')
def account_production_used(self):
pass
@property
@account_used('account_stock')
def account_stock_used(self):
pass
@fields.depends('accounting', 'account_stock')
def on_change_accounting(self):
super().on_change_accounting()
if not self.accounting:
self.account_production = None
class CategoryAccount(metaclass=PoolMeta):
__name__ = 'product.category.account'
account_production = fields.Many2One(
'account.account', "Account Production",
domain=[
('closed', '!=', True),
('type.expense', '=', True),
('company', '=', Eval('company', -1)),
],
depends=['company'])
class Template(metaclass=PoolMeta):
__name__ = 'product.template'
@property
@account_used('account_production', 'account_category')
def account_production_used(self):
pass
class Product(metaclass=PoolMeta):
__name__ = 'product.product'
account_production_used = fields.Function(fields.Many2One(
'account.account', "Account COGS"), 'get_account_stock_category')
def get_account_stock_category(self, name):
if self.account_category:
if name == 'account_production_used':
return self.account_category.account_production.id

14
product.xml Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0"?>
<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. -->
<tryton>
<data>
<record model="ir.ui.view" id="category_view_form">
<field name="model">product.category</field>
<field name="inherit" ref="product.category_view_form"/>
<field name="name">category_form</field>
</record>
</data>
</tryton>

View File

@ -1,16 +1,16 @@
# 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.report import Report
from trytond.pool import PoolMeta, Pool
from trytond.pyson import Eval
from trytond.modules.company import CompanyReport
from trytond.model import fields, ModelView
from trytond.model import fields
from trytond.transaction import Transaction
from trytond.wizard import (
Wizard, StateView, StateAction, StateReport, StateTransition, Button)
from .exceptions import PlannedDateProductionError
from trytond.wizard import Wizard, StateTransition
from trytond.modules.company import CompanyReport
from trytond.i18n import gettext
from trytond.exceptions import UserError
from trytond.report import Report
def round_dec(number):
return Decimal(number.quantize(Decimal('.01')))
@ -31,9 +31,6 @@ class Production(metaclass=PoolMeta):
def draft(cls, productions):
super(Production, cls).draft(productions)
cursor = Transaction().connection.cursor()
pool = Pool()
Move = pool.get('stock.move')
AccountMove = pool.get('account.move')
moves = []
for p in productions:
if p.raw_production_move:
@ -67,7 +64,6 @@ class Production(metaclass=PoolMeta):
def create_account_move_stock(self, kind):
pool = Pool()
Move = pool.get('account.move')
Line = pool.get('account.move.line')
Period = pool.get('account.period')
Journal = pool.get('account.journal')
Period = pool.get('account.period')
@ -80,8 +76,9 @@ class Production(metaclass=PoolMeta):
journal = journals[0]
if not self.planned_date:
raise PlannedDateProductionError(
gettext('production_accounting.msg_planned_date_required'))
raise UserError(
gettext('production_accounting.msg_planned_date_required')
)
lines = []
balance = Decimal(0)
@ -89,23 +86,38 @@ class Production(metaclass=PoolMeta):
for _in in self.inputs:
if _in.product.cost_price == 0:
continue
account_id = _in.product.template.account_category.account_stock.id
category = _in.product.template.account_category
if not category or not category.account_stock:
raise UserError(gettext(
'production_accounting.msg_category_account_stock',
product=_in.product.rec_name
))
account_id = category.account_stock.id
credit = round_dec(_in.product.cost_price * Decimal(_in.quantity))
credit = round(credit, 0)
lines.append({
_line = {
'description': _in.product.template.name,
'account': account_id,
'debit': 0,
'credit': credit
})
}
if category.account_stock.party_required:
_line['party'] = self.company.party.id
lines.append(_line)
balance += credit
amount = 0
for _out in self.outputs:
credit = debit = Decimal("0.00")
account_production_id = _out.product.template.account_category.account_stock_production.id
category = _out.product.template.account_category
# if not category or not category.account_stock_production:
# raise UserError(gettext(
# 'production_accounting.msg_category_account_stock',
# product=_out.product.rec_name
# ))
# account_production_id = category.account_stock_production.id
account_production_id = category.account_stock.id
if kind == 'assigned':
debit = balance
if kind == 'done':
@ -113,13 +125,15 @@ class Production(metaclass=PoolMeta):
credit = round(credit, 0)
amount += credit
lines.append({
_line = {
'description': _out.product.template.name,
'account': account_production_id,
'debit': debit,
'credit': credit
})
}
if category.account_stock.party_required:
_line['party'] = self.company.party.id
lines.append(_line)
if kind == 'done':
account_stock_id = self.product.template.account_category.account_stock.id

View File

@ -1,10 +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.pool import PoolMeta, Pool
from trytond.pyson import Eval
from trytond.model import fields
from trytond.pool import PoolMeta
class Move(metaclass=PoolMeta):
@ -13,10 +10,3 @@ class Move(metaclass=PoolMeta):
@classmethod
def _get_origin(cls):
return super(Move, cls)._get_origin() + ['production']
# @fields.depends('end', 'production_input', 'production_output')
# def on_change_with_effective_date(self):
# if self.production_input:
#
# if self.production_output:
# return self.end

View File

@ -1,5 +1,5 @@
[tryton]
version=6.0.1
version=6.0.2
depends:
party
company
@ -8,4 +8,6 @@ depends:
production
account_stock_latin
xml:
message.xml
production.xml
product.xml