Cancel account move when stock move is cancelled

Closes #13597
This commit is contained in:
Cédric Krier
2024-10-14 17:54:18 +02:00
parent 745099113f
commit 9e9fdb7782
3 changed files with 132 additions and 1 deletions
@@ -1,3 +1,4 @@
* Cancel account move when stock move is cancelled
Version 7.2.0 - 2024-04-29
--------------------------
+27 -1
View File
@@ -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)
@@ -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')