| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from odoo import models, api, fields, _ |
| 5 |
from odoo.tools.float_utils import float_compare |
| 6 |
|
| 7 |
|
| 8 |
class AccountMove(models.Model): |
| 9 |
_inherit = 'account.move' |
| 10 |
|
| 11 |
l10n_it_ddt_ids = fields.Many2many('stock.picking', compute="_compute_ddt_ids") |
| 12 |
l10n_it_ddt_count = fields.Integer(compute="_compute_ddt_ids") |
| 13 |
|
| 14 |
def _l10n_it_edi_document_type_mapping(self): |
| 15 |
""" Deferred invoices (not direct) require TD24 FatturaPA Document Type. """ |
| 16 |
res = super()._l10n_it_edi_document_type_mapping() |
| 17 |
for document_type, infos in res.items(): |
| 18 |
if document_type == 'TD07': |
| 19 |
continue |
| 20 |
infos['direct_invoice'] = True |
| 21 |
res['TD24'] = {'move_types': ['out_invoice'], 'import_type': 'in_invoice', 'direct_invoice': False} |
| 22 |
return res |
| 23 |
|
| 24 |
def _l10n_it_edi_invoice_is_direct(self): |
| 25 |
""" An invoice is only direct if the Transport Documents are all done the same day as the invoice. """ |
| 26 |
for ddt in self.l10n_it_ddt_ids: |
| 27 |
if not ddt.date_done or ddt.date_done.date() != self.invoice_date: |
| 28 |
return False |
| 29 |
return True |
| 30 |
|
| 31 |
def _l10n_it_edi_features_for_document_type_selection(self): |
| 32 |
res = super()._l10n_it_edi_features_for_document_type_selection() |
| 33 |
res['direct_invoice'] = self._l10n_it_edi_invoice_is_direct() |
| 34 |
return res |
| 35 |
|
| 36 |
def _get_ddt_values(self): |
| 37 |
""" |
| 38 |
We calculate the link between the invoice lines and the deliveries related to the invoice through the |
| 39 |
links with the sale order(s). We assume that the first picking was invoiced first. (FIFO) |
| 40 |
:return: a dictionary with as key the picking and value the invoice line numbers (by counting) |
| 41 |
""" |
| 42 |
self.ensure_one() |
| 43 |
|
| 44 |
if self.move_type != "out_invoice" or self.state != 'posted': |
| 45 |
return {} |
| 46 |
line_count = 0 |
| 47 |
invoice_line_pickings = {} |
| 48 |
for line in self.invoice_line_ids.filtered(lambda l: l.display_type not in ('line_section', 'line_subsection', 'line_note')): |
| 49 |
line_count += 1 |
| 50 |
done_moves_related = line.sale_line_ids.mapped('move_ids').filtered( |
| 51 |
lambda m: m.state == 'done' and m.location_dest_id.usage == 'customer' and m.picking_type_id.code == 'outgoing') |
| 52 |
if len(done_moves_related) <= 1: |
| 53 |
if done_moves_related and line_count not in invoice_line_pickings.get(done_moves_related.picking_id, []): |
| 54 |
invoice_line_pickings.setdefault(done_moves_related.picking_id, []).append(line_count) |
| 55 |
else: |
| 56 |
total_invoices = done_moves_related.mapped('sale_line_id.invoice_lines').filtered( |
| 57 |
lambda l: l.move_id.state == 'posted' and l.move_id.move_type == 'out_invoice').sorted(lambda l: (l.move_id.invoice_date, l.move_id.id)) |
| 58 |
total_invs = [(i.product_uom_id._compute_quantity(i.quantity, i.product_id.uom_id), i) for i in total_invoices] |
| 59 |
inv = total_invs.pop(0) |
| 60 |
|
| 61 |
for move in done_moves_related.sorted(lambda m: (m.date, m.id)): |
| 62 |
rounding = move.product_uom.rounding |
| 63 |
move_qty = move.product_qty |
| 64 |
while (float_compare(move_qty, 0, precision_rounding=rounding) > 0): |
| 65 |
if float_compare(inv[0], move_qty, precision_rounding=rounding) > 0: |
| 66 |
inv = (inv[0] - move_qty, inv[1]) |
| 67 |
invoice_line = inv[1] |
| 68 |
move_qty = 0 |
| 69 |
if float_compare(inv[0], move_qty, precision_rounding=rounding) <= 0: |
| 70 |
move_qty -= inv[0] |
| 71 |
invoice_line = inv[1] |
| 72 |
if total_invs: |
| 73 |
inv = total_invs.pop(0) |
| 74 |
else: |
| 75 |
move_qty = 0 |
| 76 |
|
| 77 |
if invoice_line == line and line_count not in invoice_line_pickings.get(move.picking_id, []): |
| 78 |
invoice_line_pickings.setdefault(move.picking_id, []).append(line_count) |
| 79 |
return invoice_line_pickings |
| 80 |
|
| 81 |
@api.depends('invoice_line_ids', 'invoice_line_ids.sale_line_ids') |
| 82 |
def _compute_ddt_ids(self): |
| 83 |
it_out_invoices = self.filtered(lambda i: i.move_type == 'out_invoice' and i.company_id.account_fiscal_country_id.code == 'IT') |
| 84 |
for invoice in it_out_invoices: |
| 85 |
invoice_line_pickings = invoice._get_ddt_values() |
| 86 |
pickings = self.env['stock.picking'] |
| 87 |
for picking in invoice_line_pickings: |
| 88 |
pickings |= picking |
| 89 |
invoice.l10n_it_ddt_ids = pickings |
| 90 |
invoice.l10n_it_ddt_count = len(pickings) |
| 91 |
for invoice in self - it_out_invoices: |
| 92 |
invoice.l10n_it_ddt_ids = self.env['stock.picking'] |
| 93 |
invoice.l10n_it_ddt_count = 0 |
| 94 |
|
| 95 |
def get_linked_ddts(self): |
| 96 |
self.ensure_one() |
| 97 |
return { |
| 98 |
'type': 'ir.actions.act_window', |
| 99 |
'view_mode': 'list,form', |
| 100 |
'name': _("Linked deliveries"), |
| 101 |
'res_model': 'stock.picking', |
| 102 |
'domain': [('id', 'in', self.l10n_it_ddt_ids.ids)], |
| 103 |
} |
| 104 |
|
| 105 |
def _l10n_it_edi_get_values(self, pdf_values=None): |
| 106 |
template_values = super()._l10n_it_edi_get_values(pdf_values) |
| 107 |
template_values['ddt_dict'] = self._get_ddt_values() |
| 108 |
return template_values |