From 53bf0b149d888e792ffc1d0e8d4cdd2f74bf6dd1 Mon Sep 17 00:00:00 2001 From: Oscar Date: Thu, 4 Nov 2021 23:30:45 -0500 Subject: [PATCH] Add error exceptions --- exceptions.py | 7 ----- message.xml | 5 +++- product.py | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ product.xml | 14 +++++++++ production.py | 56 ++++++++++++++++++++++-------------- stock.py | 12 +------- tryton.cfg | 4 ++- 7 files changed, 137 insertions(+), 41 deletions(-) create mode 100644 product.py create mode 100644 product.xml diff --git a/exceptions.py b/exceptions.py index e46c3cb..4effdfa 100644 --- a/exceptions.py +++ b/exceptions.py @@ -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 diff --git a/message.xml b/message.xml index bf92387..6b9ee27 100644 --- a/message.xml +++ b/message.xml @@ -4,7 +4,10 @@ this repository contains the full copyright notices and license terms. --> - Planned date is required + Planned date is required! + + + Missing account category for product: "%(product)s" diff --git a/product.py b/product.py new file mode 100644 index 0000000..5f8689f --- /dev/null +++ b/product.py @@ -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 diff --git a/product.xml b/product.xml new file mode 100644 index 0000000..b4a3ea6 --- /dev/null +++ b/product.xml @@ -0,0 +1,14 @@ + + + + + + + product.category + + category_form + + + + diff --git a/production.py b/production.py index 0ac294a..9c1f712 100644 --- a/production.py +++ b/production.py @@ -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 diff --git a/stock.py b/stock.py index 4907f0d..84a46ea 100644 --- a/stock.py +++ b/stock.py @@ -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 diff --git a/tryton.cfg b/tryton.cfg index 81067e5..d20c33b 100644 --- a/tryton.cfg +++ b/tryton.cfg @@ -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