forked from OCA/account-invoicing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.py
38 lines (33 loc) · 1.31 KB
/
hooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Copyright 2016 Antonio Espinosa <[email protected]>
# Copyright 2017-2018 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import SUPERUSER_ID, api
def match_origin_lines(refund):
"""Try to match lines by product or by description."""
invoice = refund.reversed_entry_id
invoice_lines = invoice.invoice_line_ids
for refund_line in refund.invoice_line_ids:
for invoice_line in invoice_lines:
match = (
refund_line.product_id
and refund_line.product_id == invoice_line.product_id
or refund_line.name == invoice_line.name
)
if match:
invoice_lines -= invoice_line
refund_line.origin_line_id = invoice_line.id
break
if not invoice_lines:
break
def post_init_hook(cr, registry):
with api.Environment.manage():
env = api.Environment(cr, SUPERUSER_ID, {})
# Linking all refund invoices to its original invoices
refunds = env["account.move"].search(
[
("type", "in", ("out_refund", "in_refund")),
("reversed_entry_id", "!=", False),
]
)
for refund in refunds:
match_origin_lines(refund)