changeset 0458d195ba93 in modules/account_stock_anglo_saxon:default
details: 
https://hg.tryton.org/modules/account_stock_anglo_saxon?cmd=changeset;node=0458d195ba93
description:
        Add ir.message and use custom exceptions

        issue3672
diffstat:

 exceptions.py |   8 ++++++++
 invoice.py    |  26 +++++++++++---------------
 message.xml   |  14 ++++++++++++++
 stock.py      |   4 ++--
 tryton.cfg    |   1 +
 5 files changed, 36 insertions(+), 17 deletions(-)

diffs (113 lines):

diff -r 3632f666d7ed -r 0458d195ba93 exceptions.py
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/exceptions.py     Sat Dec 29 14:20:29 2018 +0100
@@ -0,0 +1,8 @@
+# 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 UserWarning
+
+
+class COGSWarning(UserWarning):
+    pass
diff -r 3632f666d7ed -r 0458d195ba93 invoice.py
--- a/invoice.py        Mon Oct 01 13:23:04 2018 +0200
+++ b/invoice.py        Sat Dec 29 14:20:29 2018 +0100
@@ -2,25 +2,19 @@
 # this repository contains the full copyright notices and license terms.
 from decimal import Decimal
 import operator
+
+from trytond.i18n import gettext
 from trytond.pool import Pool, PoolMeta
 from trytond.transaction import Transaction
 
+from .exceptions import COGSWarning
+
 __all__ = ['InvoiceLine']
 
 
 class InvoiceLine(metaclass=PoolMeta):
     __name__ = 'account.invoice.line'
 
-    @classmethod
-    def __setup__(cls):
-        super(InvoiceLine, cls).__setup__()
-        cls._error_messages.update({
-                'stock_move_different_product': (
-                    "The invoice line '%(line)s' is linked to stock moves "
-                    "for other products than '%(product)s'.\n"
-                    "This may compute a wrong COGS."),
-                })
-
     def _get_anglo_saxon_move_lines(self, amount, type_):
         '''
         Return account move for anglo-saxon stock accounting
@@ -67,6 +61,7 @@
         pool = Pool()
         Move = pool.get('stock.move')
         Period = pool.get('account.period')
+        Warning = pool.get('res.user.warning')
 
         result = super(InvoiceLine, self).get_move_lines()
 
@@ -92,11 +87,12 @@
             # remove move for different product
             if move.product != self.product:
                 warning_name = '%s.stock.different_product' % self
-                self.raise_user_warning(
-                    warning_name, 'stock_move_different_product', {
-                        'line': self.rec_name,
-                        'product': self.product.rec_name,
-                        })
+                if Warning.check(warning_name):
+                    raise COGSWarning(warning_name,
+                        gettext('account_stock_anglo_saxon'
+                            '.msg_invoice_line_stock_move_different_product',
+                            line=self.rec_name,
+                            product=self.product.rec_name))
             else:
                 moves.append(move)
 
diff -r 3632f666d7ed -r 0458d195ba93 message.xml
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/message.xml       Sat Dec 29 14:20:29 2018 +0100
@@ -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 group="1">
+        <record model="ir.message" 
id="msg_invoice_line_stock_move_different_product">
+            <field name="text">The invoice line "%(line)s" is linked to stock 
moves of other products than "%(product)s".
+This may compute a wrong COGS.</field>
+        </record>
+        <record model="ir.message" id="msg_move_quantity_greater">
+            <field name="text">Anglo-Saxon quantity cannot be greater than 
quantity.</field>
+        </record>
+    </data>
+</tryton>
diff -r 3632f666d7ed -r 0458d195ba93 stock.py
--- a/stock.py  Mon Oct 01 13:23:04 2018 +0200
+++ b/stock.py  Sat Dec 29 14:20:29 2018 +0100
@@ -32,10 +32,10 @@
         cls._sql_constraints += [
             ('check_in_anglo_saxon_quantity',
                 Check(t, t.quantity >= t.in_anglo_saxon_quantity),
-                'Anglo-Saxon quantity can not be greater than quantity.'),
+                'account_stock_anglo_saxon.msg_move_quantity_greater'),
             ('check_out_anglo_saxon_quantity',
                 Check(t, t.quantity >= t.out_anglo_saxon_quantity),
-                'Anglo-Saxon quantity can not be greater than quantity.'),
+                'account_stock_anglo_saxon.msg_move_quantity_greater'),
             ]
 
     @staticmethod
diff -r 3632f666d7ed -r 0458d195ba93 tryton.cfg
--- a/tryton.cfg        Mon Oct 01 13:23:04 2018 +0200
+++ b/tryton.cfg        Sat Dec 29 14:20:29 2018 +0100
@@ -20,3 +20,4 @@
     minimal_chart_pt.xml
     minimal_chart_ru.xml
     minimal_chart_sl.xml
+    message.xml

Reply via email to