Add completion rule to complete from SO payment ref

This commit is contained in:
Florian da Costa
2021-05-28 17:02:51 +02:00
committed by clementmbr
parent 64c9ecf750
commit b0db768161
3 changed files with 75 additions and 3 deletions

View File

@@ -7,4 +7,10 @@
<field name="function_to_call">get_from_name_and_so</field>
</record>
<record id="bank_statement_completion_rule_2" model="account.move.completion.rule">
<field name="name">Match from line name (based on SO payment reference)</field>
<field name="sequence">55</field>
<field name="function_to_call">get_from_name_and_so_payment_ref</field>
</record>
</odoo>

View File

@@ -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

View File

@@ -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,
)