diff --git a/account_move_line_rma_order_line/__init__.py b/account_move_line_rma_order_line/__init__.py index b49b39ef..0ab02f92 100644 --- a/account_move_line_rma_order_line/__init__.py +++ b/account_move_line_rma_order_line/__init__.py @@ -7,4 +7,35 @@ _logger = logging.getLogger(__name__) def post_init_hook(cr, registry): - api.Environment(cr, SUPERUSER_ID, {}) + env = api.Environment(cr, SUPERUSER_ID, {}) + aml_model = env["account.move.line"] + sm_model = env["stock.move"] + svl_model = env["stock.valuation.layer"] + aml_moves = aml_model.search([("rma_line_id", "!=", False)]) + sm_moves = sm_model.search([("rma_line_id", "!=", False)]) + for account_move in aml_moves.mapped("move_id"): + for aml_w_rma in account_move.invoice_line_ids.filtered( + lambda x: x.product_id + and x.account_id.id + != x.product_id.categ_id.property_stock_valuation_account_id.id + and x.rma_line_id + ): + invoice_lines_without_rma = account_move.invoice_line_ids.filtered( + lambda x: x.product_id.id == aml_w_rma.product_id.id + and not x.rma_line_id + and aml_w_rma.name in x.name + ) + if invoice_lines_without_rma: + invoice_lines_without_rma.write( + { + "rma_line_id": aml_w_rma.rma_line_id.id, + } + ) + for move in sm_moves: + current_layers = svl_model.search([("stock_move_id", "=", move.id)]) + if current_layers: + for aml in current_layers.mapped("account_move_id.line_ids").filtered( + lambda x: x.account_id.id + != move.product_id.categ_id.property_stock_valuation_account_id.id + ): + aml.rma_line_id = move.rma_line_id.id diff --git a/account_move_line_rma_order_line/__manifest__.py b/account_move_line_rma_order_line/__manifest__.py index 081ddf35..b8556c82 100644 --- a/account_move_line_rma_order_line/__manifest__.py +++ b/account_move_line_rma_order_line/__manifest__.py @@ -16,4 +16,5 @@ "installable": True, "maintainers": ["ChisOForgeFlow"], "development_status": "Beta", + "post_init_hook": "post_init_hook", } diff --git a/account_move_line_rma_order_line/tests/test_account_move_line_rma_order_line.py b/account_move_line_rma_order_line/tests/test_account_move_line_rma_order_line.py index 3b73b44c..936c75e8 100644 --- a/account_move_line_rma_order_line/tests/test_account_move_line_rma_order_line.py +++ b/account_move_line_rma_order_line/tests/test_account_move_line_rma_order_line.py @@ -228,6 +228,9 @@ class TestAccountMoveLineRmaOrderLine(common.SavepointCase): ] rma = self._create_rma(products2move, self.partner1) rma_line = rma.rma_line_ids + for rma in rma_line: + if rma.price_unit == 0: + rma.price_unit = 1.0 rma_line.action_rma_approve() wizard = self.rma_make_picking.with_context( { @@ -269,6 +272,12 @@ class TestAccountMoveLineRmaOrderLine(common.SavepointCase): "description": "Test refund", } ) + for item in make_refund.item_ids: + item.write( + { + "qty_to_refund": 1.0, + } + ) make_refund.invoice_refund() rma_line.refund_line_ids.move_id.filtered( lambda x: x.state != "posted"