forked from OCA/l10n-brazil
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaccount_move.py
763 lines (670 loc) · 29.5 KB
/
account_move.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
# Copyright (C) 2009 - TODAY Renato Lima - Akretion
# Copyright (C) 2019 - TODAY Raphaël Valyi - Akretion
# Copyright (C) 2020 - TODAY Luis Felipe Mileo - KMEE
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
from dateutil.relativedelta import relativedelta
from odoo import _, api, fields, models
from odoo.exceptions import UserError
from odoo.addons.l10n_br_fiscal.constants.fiscal import (
DOCUMENT_ISSUER_COMPANY,
DOCUMENT_ISSUER_PARTNER,
FISCAL_IN_OUT_ALL,
FISCAL_OUT,
MODELO_FISCAL_NFE,
SITUACAO_EDOC_AUTORIZADA,
SITUACAO_EDOC_CANCELADA,
SITUACAO_EDOC_EM_DIGITACAO,
)
MOVE_TO_OPERATION = {
"out_invoice": "out",
"in_invoice": "in",
"out_refund": "in",
"in_refund": "out",
"out_receipt": "out",
"in_receipt": "in",
}
REFUND_TO_OPERATION = {
"out_invoice": "in",
"in_invoice": "out",
"out_refund": "out",
"in_refund": "in",
}
FISCAL_TYPE_REFUND = {
"out": ["purchase_refund", "in_return"],
"in": ["sale_refund", "out_return"],
}
MOVE_TAX_USER_TYPE = {
"out_invoice": "sale",
"in_invoice": "purchase",
"out_refund": "sale",
"in_refund": "purchase",
}
SHADOWED_FIELDS = [
"partner_id",
"company_id",
"currency_id",
"partner_shipping_id",
"user_id",
]
class AccountMove(models.Model):
_name = "account.move"
_inherit = [
_name,
"l10n_br_fiscal.document.mixin.methods",
"l10n_br_fiscal.document.invoice.mixin",
]
_inherits = {"l10n_br_fiscal.document": "fiscal_document_id"}
_order = "date DESC, name DESC"
# some account.move records _inherits from an fiscal.document that is
# disabled with active=False (dummy record) in the l10n_br_fiscal_document table.
# To make the invoices still visible, we set active=True
# in the account_move table.
active = fields.Boolean(
default=True,
)
cnpj_cpf = fields.Char(
string="CNPJ/CPF",
related="partner_id.cnpj_cpf",
)
legal_name = fields.Char(
string="Adapted Legal Name",
related="partner_id.legal_name",
)
ie = fields.Char(
string="Adapted State Tax Number",
related="partner_id.inscr_est",
)
document_electronic = fields.Boolean(
related="document_type_id.electronic",
string="Electronic?",
)
fiscal_document_id = fields.Many2one(
comodel_name="l10n_br_fiscal.document",
string="Fiscal Document",
required=True,
copy=False,
ondelete="cascade",
)
fiscal_operation_type = fields.Selection(
selection=FISCAL_IN_OUT_ALL,
related=None,
compute="_compute_fiscal_operation_type",
)
# override the incoterm inherited by the fiscal document
# to have the same value as the native incoterm of the invoice.
incoterm_id = fields.Many2one(related="invoice_incoterm_id")
def _compute_fiscal_operation_type(self):
for inv in self:
if inv.move_type == "entry":
# if it is a Journal Entry there is nothing to do.
inv.fiscal_operation_type = False
continue
if inv.fiscal_operation_id:
inv.fiscal_operation_type = (
inv.fiscal_operation_id.fiscal_operation_type
)
else:
inv.fiscal_operation_type = MOVE_TO_OPERATION[inv.move_type]
def _get_amount_lines(self):
"""Get object lines instaces used to compute fields"""
return self.mapped("invoice_line_ids")
@api.model
def _shadowed_fields(self):
"""Returns the list of shadowed fields that are synced
from the parent."""
return SHADOWED_FIELDS
@api.model
def _inject_shadowed_fields(self, vals_list):
for vals in vals_list:
for field in self._shadowed_fields():
if vals.get(field):
vals["fiscal_%s" % (field,)] = vals[field]
@api.model
def fields_view_get(
self, view_id=None, view_type="form", toolbar=False, submenu=False
):
invoice_view = super().fields_view_get(view_id, view_type, toolbar, submenu)
if view_type == "form":
view = self.env["ir.ui.view"]
if view_id == self.env.ref("l10n_br_account.fiscal_invoice_form").id:
invoice_line_form_id = self.env.ref(
"l10n_br_account.fiscal_invoice_line_form"
).id
sub_form_view = self.env["account.move.line"].fields_view_get(
view_id=invoice_line_form_id, view_type="form"
)["arch"]
sub_form_node = self.env["account.move.line"].inject_fiscal_fields(
sub_form_view
)
sub_arch, sub_fields = view.postprocess_and_fields(
sub_form_node, "account.move.line", False
)
line_field_name = "invoice_line_ids"
invoice_view["fields"][line_field_name]["views"]["form"] = {
"fields": sub_fields,
"arch": sub_arch,
}
else:
if invoice_view["fields"].get("invoice_line_ids"):
invoice_line_form_id = self.env.ref(
"l10n_br_account.invoice_form"
).id
sub_form_view = invoice_view["fields"]["invoice_line_ids"]["views"][
"form"
]["arch"]
sub_form_node = self.env["account.move.line"].inject_fiscal_fields(
sub_form_view
)
sub_arch, sub_fields = view.postprocess_and_fields(
sub_form_node, "account.move.line", False
)
line_field_name = "invoice_line_ids"
invoice_view["fields"][line_field_name]["views"]["form"] = {
"fields": sub_fields,
"arch": sub_arch,
}
if invoice_view["fields"].get("line_ids"):
invoice_line_form_id = self.env.ref(
"l10n_br_account.invoice_form"
).id
sub_form_view = invoice_view["fields"]["line_ids"]["views"]["tree"][
"arch"
]
sub_form_node = self.env["account.move.line"].inject_fiscal_fields(
sub_form_view
)
sub_arch, sub_fields = view.postprocess_and_fields(
sub_form_node, "account.move.line", False
)
line_field_name = "line_ids"
invoice_view["fields"][line_field_name]["views"]["tree"] = {
"fields": sub_fields,
"arch": sub_arch,
}
return invoice_view
@api.depends(
"line_ids.matched_debit_ids.debit_move_id.move_id.payment_id.is_matched",
"line_ids.matched_debit_ids.debit_move_id.move_id.line_ids.amount_residual",
"line_ids.matched_debit_ids.debit_move_id.move_id.line_ids.amount_residual_currency",
"line_ids.matched_credit_ids.credit_move_id.move_id.payment_id.is_matched",
"line_ids.matched_credit_ids.credit_move_id.move_id.line_ids.amount_residual",
"line_ids.matched_credit_ids.credit_move_id.move_id.line_ids.amount_residual_currency",
"line_ids.debit",
"line_ids.credit",
"line_ids.currency_id",
"line_ids.amount_currency",
"line_ids.amount_residual",
"line_ids.amount_residual_currency",
"line_ids.payment_id.state",
"line_ids.full_reconcile_id",
"ind_final",
)
def _compute_amount(self):
if self.company_id.country_id.code != "BR":
return super()._compute_amount()
for move in self:
for line in move.line_ids:
if (
move.is_invoice(include_receipts=True)
and not line.exclude_from_invoice_tab
):
line._update_taxes()
result = super()._compute_amount()
for move in self:
if move.move_type == "entry" or move.is_outbound():
sign = -1
else:
sign = 1
inv_line_ids = move.line_ids.filtered(
lambda l: not l.exclude_from_invoice_tab
)
move.amount_untaxed = sum(inv_line_ids.mapped("amount_untaxed"))
move.amount_tax = sum(inv_line_ids.mapped("amount_tax"))
move.amount_untaxed_signed = sign * sum(
inv_line_ids.mapped("amount_untaxed")
)
move.amount_tax_signed = sign * sum(inv_line_ids.mapped("amount_tax"))
return result
@api.onchange("ind_final")
def _onchange_ind_final(self):
"""Trigger the recompute of the taxes when the ind_final is changed"""
for line in self.invoice_line_ids:
line._onchange_fiscal_operation_id()
return self._recompute_dynamic_lines(recompute_all_taxes=True)
@api.model
def default_get(self, fields_list):
defaults = super().default_get(fields_list)
move_type = self.env.context.get("default_move_type", "out_invoice")
if not move_type == "entry":
defaults["fiscal_operation_type"] = MOVE_TO_OPERATION[move_type]
if defaults["fiscal_operation_type"] == FISCAL_OUT:
defaults["issuer"] = DOCUMENT_ISSUER_COMPANY
else:
defaults["issuer"] = DOCUMENT_ISSUER_PARTNER
return defaults
@api.model
def _move_autocomplete_invoice_lines_create(self, vals_list):
new_vals_list = super(
AccountMove, self.with_context(lines_compute_amounts=True)
)._move_autocomplete_invoice_lines_create(vals_list)
for vals in new_vals_list:
if not vals.get("document_type_id"):
vals["fiscal_document_id"] = self.env.company.fiscal_dummy_id.id
return new_vals_list
def _move_autocomplete_invoice_lines_values(self):
self.ensure_one()
if self._context.get("lines_compute_amounts"):
self.line_ids._compute_amounts()
return super()._move_autocomplete_invoice_lines_values()
@api.model_create_multi
def create(self, vals_list):
self._inject_shadowed_fields(vals_list)
invoice = super().create(vals_list)
return invoice
def write(self, values):
self._inject_shadowed_fields([values])
result = super().write(values)
return result
def unlink(self):
"""Allows delete a draft or cancelled invoices"""
unlink_moves = self.env["account.move"]
unlink_documents = self.env["l10n_br_fiscal.document"]
for move in self:
if not move.exists():
continue
if (
move.fiscal_document_id
and move.fiscal_document_id.id != self.env.company.fiscal_dummy_id.id
):
unlink_documents |= move.fiscal_document_id
unlink_moves |= move
result = super(AccountMove, unlink_moves).unlink()
unlink_documents.unlink()
self.clear_caches()
return result
@api.returns("self", lambda value: value.id)
def copy(self, default=None):
default = default or {}
if self.document_type_id:
default["fiscal_line_ids"] = False
else:
default["fiscal_line_ids"] = self.line_ids[0]
return super().copy(default)
@api.model
def _serialize_tax_grouping_key(self, grouping_dict):
return "-".join(str(v) for v in grouping_dict.values())
@api.model
def _compute_taxes_mapped(self, base_line):
move = base_line.move_id
if move.is_invoice(include_receipts=True):
handle_price_include = True
sign = -1 if move.is_inbound() else 1
quantity = base_line.quantity
is_refund = move.move_type in ("out_refund", "in_refund")
price_unit_wo_discount = (
sign * base_line.price_unit * (1 - (base_line.discount / 100.0))
)
else:
handle_price_include = False
quantity = 1.0
tax_type = base_line.tax_ids[0].type_tax_use if base_line.tax_ids else None
is_refund = (tax_type == "sale" and base_line.debit) or (
tax_type == "purchase" and base_line.credit
)
price_unit_wo_discount = base_line.amount_currency
balance_taxes_res = base_line.tax_ids._origin.with_context(
force_sign=move._get_tax_force_sign()
).compute_all(
price_unit_wo_discount,
currency=base_line.currency_id,
quantity=quantity,
product=base_line.product_id,
partner=base_line.partner_id,
is_refund=is_refund,
handle_price_include=handle_price_include,
fiscal_taxes=base_line.fiscal_tax_ids,
operation_line=base_line.fiscal_operation_line_id,
ncm=base_line.ncm_id,
nbs=base_line.nbs_id,
nbm=base_line.nbm_id,
cest=base_line.cest_id,
discount_value=base_line.discount_value,
insurance_value=base_line.insurance_value,
other_value=base_line.other_value,
freight_value=base_line.freight_value,
fiscal_price=base_line.fiscal_price,
fiscal_quantity=base_line.fiscal_quantity,
uot_id=base_line.uot_id,
icmssn_range=base_line.icmssn_range_id,
icms_origin=base_line.icms_origin,
ind_final=base_line.ind_final,
inss_reduction_manual=base_line.inss_reduction_manual,
)
return balance_taxes_res
def _preprocess_taxes_map(self, taxes_map):
"""Useful in case we want to pre-process taxes_map"""
taxes_mapped = super()._preprocess_taxes_map(taxes_map=taxes_map)
for line in self.line_ids.filtered(
lambda line: not line.tax_repartition_line_id
):
if not line.tax_ids or not line.fiscal_tax_ids:
continue
compute_all_vals = self._compute_taxes_mapped(line)
for tax_vals in compute_all_vals["taxes"]:
grouping_dict = self._get_tax_grouping_key_from_base_line(
line, tax_vals
)
grouping_key = self._serialize_tax_grouping_key(grouping_dict)
tax_repartition_line = self.env["account.tax.repartition.line"].browse(
tax_vals["tax_repartition_line_id"]
)
if taxes_mapped[grouping_key]:
taxes_mapped[grouping_key]["amount"] += tax_vals["amount"]
taxes_mapped[grouping_key][
"tax_base_amount"
] += self._get_base_amount_to_display(
tax_vals["base"], tax_repartition_line, tax_vals["group"]
)
return taxes_mapped
def _recompute_payment_terms_lines(self):
"""Compute the dynamic payment term lines of the journal entry.
overwritten this method to change aml's field name.
"""
# TODO - esse método é executado em um onchange, na emissão de um novo
# documento fiscal o numero do documento pode estar em branco
# atualizar esse dado ao validar a fatura, ou atribuir o número da NFe
# antes de salva-la.
result = super()._recompute_payment_terms_lines()
if self.document_number:
terms_lines = self.line_ids.filtered(
lambda l: l.account_id.user_type_id.type in ("receivable", "payable")
and l.move_id.document_type_id
)
terms_lines.sorted(lambda line: line.date_maturity)
for idx, terms_line in enumerate(terms_lines):
# TODO TODO pegar o método do self.fiscal_document_id.with_context(
# fiscal_document_no_company=True
# )._compute_document_name()
terms_line.name = "{}/{}-{}".format(
self.document_number, idx + 1, len(terms_lines)
)
return result
# @api.model
# def invoice_line_move_line_get(self):
# # TODO FIXME migrate. No such method in Odoo 13+
# move_lines_dict = super().invoice_line_move_line_get()
# new_mv_lines_dict = []
# for line in move_lines_dict:
# invoice_line = self.line_ids.filtered(lambda l: l.id == line.get("invl_id"))
#
# if invoice_line.fiscal_operation_id:
# if invoice_line.fiscal_operation_id.deductible_taxes:
# line["price"] = invoice_line.price_total
# else:
# line["price"] = invoice_line.price_total - (
# invoice_line.amount_tax_withholding
# + invoice_line.amount_tax_included
# )
#
# if invoice_line.cfop_id:
# if invoice_line.cfop_id.finance_move:
# new_mv_lines_dict.append(line)
# else:
# new_mv_lines_dict.append(line)
#
# return new_mv_lines_dict
#
@api.onchange("fiscal_operation_id")
def _onchange_fiscal_operation_id(self):
result = super()._onchange_fiscal_operation_id()
if self.fiscal_operation_id and self.fiscal_operation_id.journal_id:
self.journal_id = self.fiscal_operation_id.journal_id
return result
def open_fiscal_document(self):
if self.env.context.get("move_type", "") == "out_invoice":
xmlid = "l10n_br_account.fiscal_invoice_out_action"
elif self.env.context.get("move_type", "") == "in_invoice":
xmlid = "l10n_br_account.fiscal_invoice_in_action"
else:
xmlid = "l10n_br_account.fiscal_invoice_all_action"
action = self.env["ir.actions.act_window"]._for_xml_id(xmlid)
form_view = [(self.env.ref("l10n_br_account.fiscal_invoice_form").id, "form")]
if "views" in action:
action["views"] = form_view + [
(state, view) for state, view in action["views"] if view != "form"
]
else:
action["views"] = form_view
action["res_id"] = self.id
return action
def action_date_assign(self):
"""Usamos esse método para definir a data de emissão do documento
fiscal e numeração do documento fiscal para ser usado nas linhas
dos lançamentos contábeis."""
# TODO FIXME migrate. No such method in Odoo 13+
result = super().action_date_assign()
for invoice in self:
if invoice.document_type_id:
if invoice.issuer == DOCUMENT_ISSUER_COMPANY:
invoice.fiscal_document_id._document_date()
invoice.fiscal_document_id._document_number()
return result
def button_draft(self):
for i in self.filtered(lambda d: d.document_type_id):
if i.state_edoc == SITUACAO_EDOC_CANCELADA:
if i.issuer == DOCUMENT_ISSUER_COMPANY:
raise UserError(
_(
"You can't set this document number: {} to draft "
"because this document is cancelled in SEFAZ"
).format(i.document_number)
)
if i.state_edoc != SITUACAO_EDOC_EM_DIGITACAO:
i.fiscal_document_id.action_document_back2draft()
return super().button_draft()
def action_document_send(self):
invoices = self.filtered(lambda d: d.document_type_id)
if invoices:
invoices.mapped("fiscal_document_id").action_document_send()
# FIXME: na migração para a v14 foi permitido o post antes do envio
# para destravar a migração, mas poderia ser cogitado de obrigar a
# transmissão antes do post novamente como na v12.
# for invoice in invoices:
# invoice.move_id.post(invoice=invoice)
def action_document_cancel(self):
for i in self.filtered(lambda d: d.document_type_id):
return i.fiscal_document_id.action_document_cancel()
def action_document_correction(self):
for i in self.filtered(lambda d: d.document_type_id):
return i.fiscal_document_id.action_document_correction()
def action_document_invalidate(self):
for i in self.filtered(lambda d: d.document_type_id):
return i.fiscal_document_id.action_document_invalidate()
def action_document_back2draft(self):
"""Sets fiscal document to draft state and cancel and set to draft
the related invoice for both documents remain equivalent state."""
for i in self.filtered(lambda d: d.document_type_id):
i.button_cancel()
i.button_draft()
def action_post(self):
result = super().action_post()
self.mapped("fiscal_document_id").filtered(
lambda d: d.document_type_id
).action_document_confirm()
# TODO FIXME
# Deixar a migração das funcionalidades do refund por último.
# Verificar se ainda haverá necessidade desse código.
# for record in self.filtered(lambda i: i.refund_move_id):
# if record.state == "open":
# # Ao confirmar uma fatura/documento fiscal se é uma devolução
# # é feito conciliado com o documento de origem para abater
# # o valor devolvido pelo documento de refund
# to_reconcile_lines = self.env["account.move.line"]
# for line in record.move_id.line_ids:
# if line.account_id.id == record.account_id.id:
# to_reconcile_lines += line
# if line.reconciled:
# line.remove_move_reconcile()
# for line in record.refund_move_id.move_id.line_ids:
# if line.account_id.id == record.refund_move_id.account_id.id:
# to_reconcile_lines += line
# to_reconcile_lines.filtered(lambda l: l.reconciled).reconcile()
return result
def view_xml(self):
self.ensure_one()
return self.fiscal_document_id.view_xml()
def view_pdf(self):
self.ensure_one()
return self.fiscal_document_id.view_pdf()
def action_send_email(self):
self.ensure_one()
return self.fiscal_document_id.action_send_email()
@api.onchange("document_type_id")
def _onchange_document_type_id(self):
# We need to ensure that invoices without a fiscal document have the
# document_number blank, as all invoices without a fiscal document share this
# same field, they are linked to the same dummy fiscal document.
# Otherwise, in the tree view, this field will be displayed with the same value
# for all these invoices.
if not self.document_type_id:
self.document_number = ""
def _reverse_moves(self, default_values_list=None, cancel=False):
new_moves = super()._reverse_moves(
default_values_list=default_values_list, cancel=cancel
)
force_fiscal_operation_id = False
if self.env.context.get("force_fiscal_operation_id"):
force_fiscal_operation_id = self.env["l10n_br_fiscal.operation"].browse(
self.env.context.get("force_fiscal_operation_id")
)
for record in new_moves.filtered(lambda i: i.document_type_id):
if (
not force_fiscal_operation_id
and not record.fiscal_operation_id.return_fiscal_operation_id
):
raise UserError(
_("""Document without Return Fiscal Operation! \n Force one!""")
)
record.fiscal_operation_id = (
force_fiscal_operation_id
or record.fiscal_operation_id.return_fiscal_operation_id
)
record._onchange_fiscal_operation_id()
for line in record.invoice_line_ids:
if (
not force_fiscal_operation_id
and not line.fiscal_operation_id.return_fiscal_operation_id
):
raise UserError(
_(
"""Line without Return Fiscal Operation! \n
Please force one! \n{}""".format(
line.name
)
)
)
line.fiscal_operation_id = (
force_fiscal_operation_id
or line.fiscal_operation_id.return_fiscal_operation_id
)
line._onchange_fiscal_operation_id()
# Adds the related document to the NF-e.
# this is required for correct xml validation
if record.document_type_id and record.document_type_id.code in (
MODELO_FISCAL_NFE
):
record.fiscal_document_id._document_reference(
record.reversed_entry_id.fiscal_document_id
)
return new_moves
def _prepare_wh_invoice(self, move_line, fiscal_group):
wh_date_invoice = move_line.move_id.date
wh_due_invoice = wh_date_invoice.replace(day=fiscal_group.wh_due_day)
values = {
"partner_id": fiscal_group.partner_id.id,
"date": wh_date_invoice,
"date_due": wh_due_invoice + relativedelta(months=1),
"type": "in_invoice",
"account_id": fiscal_group.partner_id.property_account_payable_id.id,
"journal_id": move_line.journal_id.id,
"origin": move_line.move_id.name,
}
return values
def _prepare_wh_invoice_line(self, invoice, move_line):
values = {
"name": move_line.name,
"quantity": move_line.quantity,
"uom_id": move_line.product_uom_id,
"price_unit": abs(move_line.balance),
"move_id": invoice.id,
"account_id": move_line.account_id.id,
"wh_move_line_id": move_line.id,
"account_analytic_id": move_line.analytic_account_id.id,
}
return values
def _finalize_invoices(self, invoices):
for invoice in invoices:
invoice.compute_taxes()
for line in invoice.line_ids:
# Use additional field helper function (for account extensions)
line._set_additional_fields(invoice)
invoice._onchange_cash_rounding()
def create_wh_invoices(self):
for move in self:
for line in move.line_ids.filtered(lambda l: l.tax_line_id):
# Create Wh Invoice only for supplier invoice
if line.move_id and line.move_id.type != "in_invoice":
continue
account_tax_group = line.tax_line_id.tax_group_id
if account_tax_group and account_tax_group.fiscal_tax_group_id:
fiscal_group = account_tax_group.fiscal_tax_group_id
if fiscal_group.tax_withholding:
invoice = self.env["account.move"].create(
self._prepare_wh_invoice(line, fiscal_group)
)
self.env["account.move.line"].create(
self._prepare_wh_invoice_line(invoice, line)
)
self._finalize_invoices(invoice)
invoice.action_post()
def _withholding_validate(self):
for m in self:
invoices = (
self.env["account.move.line"]
.search([("wh_move_line_id", "in", m.mapped("line_ids").ids)])
.mapped("move_id")
)
invoices.filtered(lambda i: i.state == "open").button_cancel()
invoices.filtered(lambda i: i.state == "cancel").button_draft()
invoices.invalidate_cache()
invoices.filtered(lambda i: i.state == "draft").unlink()
def post(self, invoice=False):
# TODO FIXME migrate: no more invoice keyword
result = super().post()
if invoice:
if (
invoice.document_type_id
and invoice.document_electronic
and invoice.issuer == DOCUMENT_ISSUER_COMPANY
and invoice.state_edoc != SITUACAO_EDOC_AUTORIZADA
):
self.button_cancel()
return result
def button_cancel(self):
for doc in self.filtered(lambda d: d.document_type_id):
doc.fiscal_document_id.action_document_cancel()
# Esse método é responsavel por verificar se há alguma fatura de impostos
# retidos associada a essa fatura e cancela-las também.
self._withholding_validate()
return super().button_cancel()
# TODO: Por ora esta solução contorna o problema
# AttributeError: 'Boolean' object has no attribute 'depends_context'
# Este erro está relacionado com o campo active implementado via localização
# nos modelos account.move.line e l10n_br_fiscal.document.line
# Este problema começou após este commit:
# https://github.com/oca/ocb/commit/1dcd071b27779e7d6d8f536c7dce7002d27212ba
def _get_integrity_hash_fields_and_subfields(self):
return self._get_integrity_hash_fields() + [
f"line_ids.{subfield}"
for subfield in self.env["account.move.line"]._get_integrity_hash_fields()
]