1
- # -*- coding: utf-8 -*-
2
1
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
3
- # License LGPL-3.0 or later (https://www.gnu.org/licenses/agpl .html).
2
+ # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl .html).
4
3
5
- from odoo import api , models , _
6
- from odoo .exceptions import UserError
7
- from odoo .tools import float_is_zero
4
+ from odoo import api , models
8
5
9
6
10
7
class SaleOrder (models .Model ):
@@ -17,117 +14,3 @@ def _get_invoice_group_key(self, order):
17
14
@api .model
18
15
def _get_draft_invoices (self , invoices , references ):
19
16
return invoices , references
20
-
21
- @api .multi
22
- def action_invoice_create (self , grouped = False , final = False ):
23
- """
24
- Create the invoice associated to the SO.
25
- :param grouped: if True, invoices are grouped by SO id. If False,
26
- invoices are grouped by (partner_invoice_id, currency)
27
- :param final: if True, refunds will be generated if necessary
28
- :returns: list of created invoices
29
- """
30
-
31
- invoices = {}
32
- references = {}
33
-
34
- # START HOOK
35
- # Take into account draft invoices when creating new ones
36
- self ._get_draft_invoices (invoices , references )
37
- # END HOOK
38
-
39
- inv_obj = self .env ['account.invoice' ]
40
- precision = self .env ['decimal.precision' ].\
41
- precision_get ('Product Unit of Measure' )
42
-
43
- # START HOOK
44
- # As now from the beginning there can be invoices related to that
45
- # order, instead of new invoices, new lines are taking into account in
46
- # order to know whether there are invoice lines or not
47
- new_lines = False
48
- # END HOOK
49
- for order in self :
50
- # START HOOK
51
- # Add more flexibility in grouping key fields
52
- # WAS: group_key = order.id if grouped
53
- # else (order.partner_invoice_id.id, order.currency_id.id)
54
- group_key = order .id if grouped else \
55
- self ._get_invoice_group_key (order )
56
- # 'invoice' must be always instantiated respecting the old logic
57
- if group_key in invoices :
58
- invoice = invoices [group_key ]
59
- # END HOOK
60
- for line in order .order_line .sorted (
61
- key = lambda l : l .qty_to_invoice < 0 ):
62
- if float_is_zero (line .qty_to_invoice ,
63
- precision_digits = precision ):
64
- continue
65
- if group_key not in invoices :
66
- inv_data = order ._prepare_invoice ()
67
- invoice = inv_obj .create (inv_data )
68
- references [invoice ] = order
69
- invoices [group_key ] = invoice
70
- elif group_key in invoices :
71
- # START HOOK
72
- # This line below is added in order to cover cases where an
73
- # invoice is not created and instead a draft one is picked
74
- invoice = invoices [group_key ]
75
- # END HOOK
76
- vals = {}
77
- if order .name not in invoices [group_key ].\
78
- origin .split (', ' ):
79
- vals ['origin' ] = invoices [group_key ].origin + ', ' + \
80
- order .name
81
- if order .client_order_ref and order .client_order_ref not \
82
- in invoices [group_key ].name .split (', ' ) and \
83
- order .client_order_ref != invoices [group_key ].name :
84
- vals ['name' ] = invoices [group_key ].name + ', ' + \
85
- order .client_order_ref
86
- invoices [group_key ].write (vals )
87
- if line .qty_to_invoice > 0 :
88
- line .invoice_line_create (invoices [group_key ].id ,
89
- line .qty_to_invoice )
90
- # START HOOK
91
- # Change to true if new lines are added
92
- new_lines = True
93
- # END HOOK
94
- elif line .qty_to_invoice < 0 and final :
95
- line .invoice_line_create (invoices [group_key ].id ,
96
- line .qty_to_invoice )
97
- # START HOOK
98
- # Change to true if new lines are added
99
- new_lines = True
100
- # END HOOOK
101
-
102
- if references .get (invoices .get (group_key )):
103
- if order not in references [invoices [group_key ]]:
104
- references [invoice ] = references [invoice ] | order
105
-
106
- # START HOOK
107
- # WAS: if not invoices:
108
- # Check if new lines have been added in order to determine whether
109
- # there are invoice lines or not
110
- if not new_lines :
111
- raise UserError (_ ('There is no invoicable line.' ))
112
- # END HOOK
113
-
114
- for invoice in invoices .values ():
115
- if not invoice .invoice_line_ids :
116
- raise UserError (_ ('There is no invoicable line.' ))
117
- # If invoice is negative, do a refund invoice instead
118
- if invoice .amount_untaxed < 0 :
119
- invoice .type = 'out_refund'
120
- for line in invoice .invoice_line_ids :
121
- line .quantity = - line .quantity
122
- # Use additional field helper function (for account extensions)
123
- for line in invoice .invoice_line_ids :
124
- line ._set_additional_fields (invoice )
125
- # Necessary to force computation of taxes. In account_invoice,
126
- # they are triggered by onchanges, which are not triggered when
127
- # doing a create.
128
- invoice .compute_taxes ()
129
- invoice .message_post_with_view (
130
- 'mail.message_origin_link' ,
131
- values = {'self' : invoice , 'origin' : references [invoice ]},
132
- subtype_id = self .env .ref ('mail.mt_note' ).id )
133
- return [inv .id for inv in invoices .values ()]
0 commit comments