@@ -97,6 +97,9 @@ def _post(self, soft=True):
97
97
continue
98
98
99
99
invoice .check_declarations_amounts (declarations )
100
+ declarations_used_amounts = invoice .get_declarations_used_amounts (
101
+ declarations
102
+ )
100
103
101
104
# Assign account move lines to declarations for each invoice
102
105
# Get only lines with taxes
@@ -105,11 +108,11 @@ def _post(self, soft=True):
105
108
continue
106
109
# Group lines by tax
107
110
grouped_lines = self .get_move_lines_by_declaration (lines )
108
- invoice .update_declarations (declarations , grouped_lines )
111
+ invoice .update_declarations (declarations_used_amounts , grouped_lines )
109
112
110
113
return posted
111
114
112
- def update_declarations (self , declarations , grouped_lines ):
115
+ def update_declarations (self , declarations_used_amounts , grouped_lines ):
113
116
"""
114
117
Update the declarations adding a new line representing this invoice.
115
118
@@ -125,19 +128,30 @@ def update_declarations(self, declarations, grouped_lines):
125
128
amount *= - 1
126
129
# Select right declaration(s)
127
130
if force_declaration :
128
- declarations = [ force_declaration ]
131
+ declaration_id_to_amount_dict = { force_declaration . id : amount }
129
132
else :
130
- declarations = declarations
133
+ declaration_id_to_amount_dict = declarations_used_amounts
131
134
132
- for declaration in declarations :
135
+ for declaration_id in declaration_id_to_amount_dict :
136
+ declaration = self .env [
137
+ "l10n_it_declaration_of_intent.declaration"
138
+ ].browse (declaration_id )
133
139
if tax not in declaration .taxes_ids :
134
140
continue
135
141
# avoid creating line with same invoice_id
136
142
declaration .line_ids .filtered (
137
143
lambda line : line .invoice_id == self
138
144
).unlink ()
139
145
declaration .line_ids = [
140
- (0 , 0 , self ._prepare_declaration_line (amount , lines , tax )),
146
+ (
147
+ 0 ,
148
+ 0 ,
149
+ self ._prepare_declaration_line (
150
+ declaration_id_to_amount_dict [declaration_id ],
151
+ lines ,
152
+ tax ,
153
+ ),
154
+ ),
141
155
]
142
156
# Link declaration to invoice
143
157
self .declaration_of_intent_ids = [(4 , declaration .id )]
@@ -216,6 +230,21 @@ def get_declarations(self):
216
230
)
217
231
return declarations
218
232
233
+ def get_declarations_used_amounts (self , declarations ):
234
+ """Get used amount by declarations for this invoice."""
235
+ self .ensure_one ()
236
+ declarations_used_amounts = {}
237
+ sign = 1 if self .move_type in ["out_invoice" , "in_invoice" ] else - 1
238
+ for tax_line in self .line_ids .filtered ("tax_ids" ):
239
+ amount = sign * tax_line .price_subtotal
240
+ for declaration in declarations :
241
+ if declaration .id not in declarations_used_amounts :
242
+ declarations_used_amounts [declaration .id ] = 0
243
+ if any (tax in declaration .taxes_ids for tax in tax_line .tax_ids ):
244
+ declarations_used_amounts [declaration .id ] += amount
245
+ amount = 0.0
246
+ return declarations_used_amounts
247
+
219
248
def check_declarations_amounts (self , declarations ):
220
249
"""
221
250
Compare this invoice's tax amounts and `declarations` plafond.
0 commit comments