From 9e9fdb778241dd1c18eec3ca290a8a145c4dc440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 14 Oct 2024 17:54:18 +0200 Subject: [PATCH] Cancel account move when stock move is cancelled Closes #13597 --- modules/account_stock_continental/CHANGELOG | 1 + modules/account_stock_continental/stock.py | 28 ++++- ...count_stock_continental_move_cancelled.rst | 104 ++++++++++++++++++ 3 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 modules/account_stock_continental/tests/scenario_account_stock_continental_move_cancelled.rst diff --git a/modules/account_stock_continental/CHANGELOG b/modules/account_stock_continental/CHANGELOG index 7d48d91c1b..bf01580f23 100644 --- a/modules/account_stock_continental/CHANGELOG +++ b/modules/account_stock_continental/CHANGELOG @@ -1,3 +1,4 @@ +* Cancel account move when stock move is cancelled Version 7.2.0 - 2024-04-29 -------------------------- diff --git a/modules/account_stock_continental/stock.py b/modules/account_stock_continental/stock.py index d80dc1752e..c57f22e97d 100644 --- a/modules/account_stock_continental/stock.py +++ b/modules/account_stock_continental/stock.py @@ -2,7 +2,7 @@ # this repository contains the full copyright notices and license terms. from decimal import Decimal -from trytond.model import ModelView, Workflow +from trytond.model import ModelView, Workflow, fields from trytond.modules.account.exceptions import PeriodNotFoundError from trytond.pool import Pool, PoolMeta from trytond.transaction import Transaction @@ -11,6 +11,9 @@ from trytond.transaction import Transaction class Move(metaclass=PoolMeta): __name__ = 'stock.move' + account_moves = fields.One2Many( + 'account.move', 'origin', "Account Moves", readonly=True) + def _get_account_stock_move_lines(self, type_): ''' Return move lines for stock move @@ -176,3 +179,26 @@ class Move(metaclass=PoolMeta): account_moves.append(account_move) AccountMove.save(account_moves) AccountMove.post(account_moves) + + @classmethod + @ModelView.button + @Workflow.transition('cancelled') + def cancel(cls, moves): + pool = Pool() + AccountMove = pool.get('account.move') + super().cancel(moves) + + delete_moves, cancel_moves = [], [] + for move in moves: + for account_move in move.account_moves: + if account_move.state == 'draft': + delete_moves.append(account_move) + else: + cancel_move = account_move.cancel() + cancel_move.origin = move + cancel_moves.append(cancel_move) + if delete_moves: + AccountMove.delete(delete_moves) + if cancel_moves: + AccountMove.save(cancel_moves) + AccountMove.post(cancel_moves) diff --git a/modules/account_stock_continental/tests/scenario_account_stock_continental_move_cancelled.rst b/modules/account_stock_continental/tests/scenario_account_stock_continental_move_cancelled.rst new file mode 100644 index 0000000000..b8d82707c8 --- /dev/null +++ b/modules/account_stock_continental/tests/scenario_account_stock_continental_move_cancelled.rst @@ -0,0 +1,104 @@ +================================================= +Account Stock Continental Move Cancelled Scenario +================================================= + +Imports:: + + >>> from decimal import Decimal + + >>> from proteus import Model + >>> from trytond.modules.account.tests.tools import ( + ... create_chart, create_fiscalyear, get_accounts) + >>> from trytond.modules.account_stock_continental.tests.tools import ( + ... add_stock_accounts) + >>> from trytond.modules.company.tests.tools import create_company + >>> from trytond.modules.currency.tests.tools import get_currency + >>> from trytond.tests.tools import activate_modules, assertEqual + +Activate modules:: + + >>> config = activate_modules( + ... 'account_stock_continental', create_company, create_chart) + + >>> ProductCategory = Model.get('product.category') + >>> ProductTemplate = Model.get('product.template') + >>> ProductUom = Model.get('product.uom') + >>> StockMove = Model.get('stock.move') + >>> StockLocation = Model.get('stock.location') + +Create fiscal year:: + + >>> fiscalyear = create_fiscalyear() + >>> fiscalyear.account_stock_method = 'continental' + >>> fiscalyear.click('create_period') + +Get accounts:: + + >>> accounts = add_stock_accounts(get_accounts()) + >>> receivable = accounts['receivable'] + >>> payable = accounts['payable'] + >>> revenue = accounts['revenue'] + >>> expense = accounts['expense'] + >>> stock = accounts['stock'] + >>> stock_in = accounts['stock_expense'] + >>> stock_out, = stock_in.duplicate() + +Get stock locations:: + + >>> supplier_loc, = StockLocation.find([('code', '=', 'SUP')]) + >>> storage_loc, = StockLocation.find([('code', '=', 'STO')]) + +Create product category:: + + >>> account_category = ProductCategory(name="Account Category") + >>> account_category.accounting = True + >>> account_category.account_expense = expense + >>> account_category.account_revenue = revenue + >>> account_category.account_stock = stock + >>> account_category.account_stock_in = stock_in + >>> account_category.account_stock_out = stock_out + >>> account_category.save() + +Create product:: + + >>> template = ProductTemplate() + >>> template.name = "Product" + >>> template.default_uom, = ProductUom.find([('name', '=', "Unit")]) + >>> template.type = 'goods' + >>> template.cost_price_method = 'fixed' + >>> template.account_category = account_category + >>> template.save() + >>> product, = template.products + >>> product.cost_price = Decimal('10.0000') + >>> product.save() + +Receive product:: + + >>> stock_move = StockMove() + >>> stock_move.product = product + >>> stock_move.from_location = supplier_loc + >>> stock_move.to_location = storage_loc + >>> stock_move.quantity = 1 + >>> stock_move.unit_price = Decimal('10.0000') + >>> stock_move.currency = get_currency() + >>> stock_move.click('do') + >>> stock_move.state + 'done' + >>> account_move, = stock_move.account_moves + >>> account_move.state + 'posted' + >>> stock.reload() + >>> stock.balance + Decimal('10.00') + +Cancel reception:: + + >>> stock_move.click('cancel') + >>> stock_move.state + 'cancelled' + >>> len(stock_move.account_moves) + 2 + >>> assertEqual([m.state for m in stock_move.account_moves], ['posted', 'posted']) + >>> stock.reload() + >>> stock.balance + Decimal('0.00')