forked from OCA/sale-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.py
136 lines (125 loc) · 6.2 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from odoo import _
from odoo.exceptions import UserError
from odoo.tools import float_is_zero
from odoo.addons.sale.models.sale import SaleOrder
def post_load_hook():
def new_action_invoice_create(self, grouped=False, final=False):
"""
Create the invoice associated to the SO.
:param grouped: if True, invoices are grouped by SO id. If False,
invoices are grouped by (partner_invoice_id, currency)
:param final: if True, refunds will be generated if necessary
:returns: list of created invoices
"""
if not hasattr(self, '_get_invoice_group_key'):
return self.action_invoice_create_original(grouped=grouped,
final=final)
invoices = {}
references = {}
# START HOOK
# Take into account draft invoices when creating new ones
self._get_draft_invoices(invoices, references)
# END HOOK
inv_obj = self.env['account.invoice']
precision = self.env['decimal.precision'].\
precision_get('Product Unit of Measure')
# START HOOK
# As now from the beginning there can be invoices related to that
# order, instead of new invoices, new lines are taking into account in
# order to know whether there are invoice lines or not
new_lines = False
# END HOOK
for order in self:
for line in order.order_line.sorted(
key=lambda l: l.qty_to_invoice < 0):
if float_is_zero(line.qty_to_invoice,
precision_digits=precision):
continue
# START HOOK
# Allow to check if a line should not be invoiced
if line._do_not_invoice():
continue
# END HOOK
# START HOOK
# Add more flexibility in grouping key fields
# WAS: group_key = order.id if grouped
# else (order.partner_invoice_id.id, order.currency_id.id)
group_key = order.id if grouped else \
self._get_invoice_group_line_key(line)
# 'invoice' must be always instantiated
# respecting the old logic
if group_key in invoices:
invoice = invoices[group_key]
# END HOOK
if group_key not in invoices:
inv_data = line._prepare_invoice()
invoice = inv_obj.create(inv_data)
references[invoice] = order
invoices[group_key] = invoice
elif group_key in invoices:
# START HOOK
# This line below is added in order to cover cases where an
# invoice is not created and instead a draft one is picked
invoice = invoices[group_key]
# END HOOK
vals = {}
if order.name not in invoices[group_key].\
origin.split(', '):
vals['origin'] = invoices[group_key].origin + ', ' + \
order.name
if order.client_order_ref and order.client_order_ref not \
in invoices[group_key].name.split(', ') and \
order.client_order_ref != invoices[group_key].name:
vals['name'] = invoices[group_key].name + ', ' +\
order.client_order_ref
invoices[group_key].write(vals)
if line.qty_to_invoice > 0:
line.invoice_line_create(invoices[group_key].id,
line.qty_to_invoice)
# START HOOK
# Change to true if new lines are added
new_lines = True
# END HOOK
elif line.qty_to_invoice < 0 and final:
line.invoice_line_create(invoices[group_key].id,
line.qty_to_invoice)
# START HOOK
# Change to true if new lines are added
new_lines = True
# END HOOOK
if references.get(invoices.get(group_key)):
if order not in references[invoices[group_key]]:
references[invoice] = references[invoice] | order
# START HOOK
# WAS: if not invoices:
# Check if new lines have been added in order to determine whether
# there are invoice lines or not
if not new_lines and not self.env.context.get('no_check_lines', False):
raise UserError(_('There is no invoicable line.'))
# END HOOK
for invoice in invoices.values():
if not invoice.invoice_line_ids:
raise UserError(_('There is no invoicable line.'))
# If invoice is negative, do a refund invoice instead
if invoice.amount_untaxed < 0:
invoice.type = 'out_refund'
for line in invoice.invoice_line_ids:
line.quantity = -line.quantity
# Use additional field helper function (for account extensions)
for line in invoice.invoice_line_ids:
line._set_additional_fields(invoice)
# Necessary to force computation of taxes. In account_invoice,
# they are triggered by onchanges, which are not triggered when
# doing a create.
invoice.compute_taxes()
invoice.message_post_with_view(
'mail.message_origin_link',
values={'self': invoice, 'origin': references[invoice]},
subtype_id=self.env.ref('mail.mt_note').id)
return [inv.id for inv in invoices.values()]
if not hasattr(SaleOrder, 'action_invoice_create_original'):
SaleOrder.action_invoice_create_original = \
SaleOrder.action_invoice_create
SaleOrder.action_invoice_create = new_action_invoice_create