diff --git a/account_move_so_import/data/completion_rule_data.xml b/account_move_so_import/data/completion_rule_data.xml index a6701d89..3065ffc9 100644 --- a/account_move_so_import/data/completion_rule_data.xml +++ b/account_move_so_import/data/completion_rule_data.xml @@ -7,4 +7,10 @@ get_from_name_and_so + + Match from line name (based on SO payment reference) + 55 + get_from_name_and_so_payment_ref + + diff --git a/account_move_so_import/models/account_move.py b/account_move_so_import/models/account_move.py index 6e3d707a..54a7aca7 100644 --- a/account_move_so_import/models/account_move.py +++ b/account_move_so_import/models/account_move.py @@ -11,7 +11,13 @@ class AccountMoveCompletionRule(models.Model): _inherit = "account.move.completion.rule" function_to_call = fields.Selection( - selection_add=[("get_from_name_and_so", "From line name (based on SO number)")] + selection_add=[ + ("get_from_name_and_so", "From line name (based on SO number)"), + ( + "get_from_name_and_so_payment_ref", + "From line name (based on SO payment ref)", + ), + ] ) # Should be private but data are initialized with no update XML @@ -47,3 +53,32 @@ class AccountMoveCompletionRule(models.Model): if len(orders) == 1: res["partner_id"] = orders[0].partner_id.id return res + + # Should be private but data are initialized with no update XML + def get_from_name_and_so_payment_ref(self, line): + """ + :param int/long st_line: read of the concerned + account.bank.statement.line + + :return: + A dict of value that can be passed directly to the write method of + the statement line or {} + {'partner_id': value, + 'account_id': value, + + ...} + """ + res = {} + so_obj = self.env["sale.order"] + orders = so_obj.search([("reference", "=", line.name)]) + if len(orders) > 1: + raise ErrorTooManyPartner( + _( + 'Line named "%s" was matched by more ' + "than one partner while looking on SO by ref." + ) + % line.name + ) + if len(orders) == 1: + res["partner_id"] = orders[0].partner_id.id + return res diff --git a/account_move_so_import/tests/test_completion_so.py b/account_move_so_import/tests/test_completion_so.py index eb189de5..e7dcfa10 100644 --- a/account_move_so_import/tests/test_completion_so.py +++ b/account_move_so_import/tests/test_completion_so.py @@ -16,6 +16,7 @@ class TestCompliteSO(SavepointCase): { "name": "Test order", "partner_id": cls.partner.id, + "reference": "test payment ref", "order_line": [ ( 0, @@ -48,6 +49,9 @@ class TestCompliteSO(SavepointCase): rule_ids += cls.env.ref( "account_move_so_import." "bank_statement_completion_rule_1" ) + rule_ids += cls.env.ref( + "account_move_so_import." "bank_statement_completion_rule_2" + ) # create journal with profile cls.journal = cls.env["account.journal"].create( { @@ -81,9 +85,9 @@ class TestCompliteSO(SavepointCase): ], limit=1, ) + cls.order.action_confirm() - def test_completion_so(self): - self.order.action_confirm() + def test_completion_so_name(self): self.env["account.move.line"].create( [ { @@ -109,3 +113,30 @@ class TestCompliteSO(SavepointCase): payable_aml.partner_id, self.partner, ) + + def test_completion_so_payment_ref(self): + self.env["account.move.line"].create( + [ + { + "name": self.order.reference, + "account_id": self.account_payable.id, + "move_id": self.move.id, + "credit": 1, + }, + { + "name": "counter part", + "account_id": self.account_bank.id, + "move_id": self.move.id, + "debit": 1, + }, + ] + ) + payable_aml = self.move.line_ids.filtered( + lambda line: line.account_id == self.account_payable + ) + self.assertFalse(self.move.partner_id) + self.move.button_auto_completion() + self.assertEqual( + payable_aml.partner_id, + self.partner, + )