| 1 |
|
| 2 |
|
| 3 |
from odoo import _, api, fields, models |
| 4 |
from odoo.exceptions import UserError |
| 5 |
|
| 6 |
|
| 7 |
class AccountMove(models.Model): |
| 8 |
_inherit = 'account.move' |
| 9 |
|
| 10 |
l10n_it_edi_doi_date = fields.Date( |
| 11 |
string="Date on which Declaration of Intent is applied", |
| 12 |
compute='_compute_l10n_it_edi_doi_date', |
| 13 |
) |
| 14 |
|
| 15 |
l10n_it_edi_doi_use = fields.Boolean( |
| 16 |
string="Use Declaration of Intent", |
| 17 |
compute='_compute_l10n_it_edi_doi_use', |
| 18 |
) |
| 19 |
|
| 20 |
l10n_it_edi_doi_id = fields.Many2one( |
| 21 |
string="Declaration of Intent", |
| 22 |
compute='_compute_l10n_it_edi_doi_id', |
| 23 |
store=True, |
| 24 |
readonly=False, |
| 25 |
precompute=True, |
| 26 |
comodel_name='l10n_it_edi_doi.declaration_of_intent', |
| 27 |
) |
| 28 |
|
| 29 |
l10n_it_edi_doi_amount = fields.Monetary( |
| 30 |
string='Declaration of Intent Amount', |
| 31 |
compute='_compute_l10n_it_edi_doi_amount', |
| 32 |
store=True, |
| 33 |
readonly=True, |
| 34 |
help="Total amount of sales under the Declaration of Intent of this document", |
| 35 |
) |
| 36 |
|
| 37 |
l10n_it_edi_doi_warning = fields.Text( |
| 38 |
string="Declaration of Intent Threshold Warning", |
| 39 |
compute='_compute_l10n_it_edi_doi_warning', |
| 40 |
) |
| 41 |
|
| 42 |
@api.depends('invoice_date') |
| 43 |
def _compute_l10n_it_edi_doi_date(self): |
| 44 |
for move in self: |
| 45 |
move.l10n_it_edi_doi_date = move.invoice_date or fields.Date.context_today(self) |
| 46 |
|
| 47 |
@api.depends('l10n_it_edi_doi_id', 'country_code', 'move_type') |
| 48 |
def _compute_l10n_it_edi_doi_use(self): |
| 49 |
sale_types = self.env['account.move'].get_sale_types() |
| 50 |
for move in self: |
| 51 |
move.l10n_it_edi_doi_use = ( |
| 52 |
move.l10n_it_edi_doi_id |
| 53 |
or (move.country_code == "IT" and move.move_type in sale_types) |
| 54 |
) |
| 55 |
|
| 56 |
@api.depends('company_id', 'partner_id.commercial_partner_id', 'l10n_it_edi_doi_date', 'currency_id') |
| 57 |
def _compute_l10n_it_edi_doi_id(self): |
| 58 |
for move in self: |
| 59 |
if not move.l10n_it_edi_doi_use or move.state != 'draft' and not move.l10n_it_edi_doi_id: |
| 60 |
move.l10n_it_edi_doi_id = False |
| 61 |
continue |
| 62 |
partner = move.partner_id.commercial_partner_id |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
validity_warnings = move.l10n_it_edi_doi_id._get_validity_warnings( |
| 67 |
move.company_id, partner, move.currency_id, move.l10n_it_edi_doi_date |
| 68 |
) |
| 69 |
if move.l10n_it_edi_doi_id and not validity_warnings: |
| 70 |
continue |
| 71 |
|
| 72 |
declaration = self.env['l10n_it_edi_doi.declaration_of_intent']\ |
| 73 |
._fetch_valid_declaration_of_intent(move.company_id, partner, move.currency_id, move.l10n_it_edi_doi_date) |
| 74 |
move.l10n_it_edi_doi_id = declaration |
| 75 |
|
| 76 |
@api.depends('l10n_it_edi_doi_id', 'tax_totals', 'move_type') |
| 77 |
def _compute_l10n_it_edi_doi_amount(self): |
| 78 |
""" |
| 79 |
Consider all the lines in self that belong to declaration of intent `declaration` |
| 80 |
and have the special declaration of intent tax applied. |
| 81 |
This function computes the signed sum of the price_total of all those lines |
| 82 |
(the tax amount of the lines is always 0). |
| 83 |
The direction_sign determines the sign: 1 (-1) for inbound (outbound) types. |
| 84 |
""" |
| 85 |
for move in self: |
| 86 |
tax = move.company_id.l10n_it_edi_doi_tax_id |
| 87 |
if not tax or not move.l10n_it_edi_doi_id: |
| 88 |
move.l10n_it_edi_doi_amount = 0 |
| 89 |
continue |
| 90 |
declaration_lines = move.invoice_line_ids.filtered( |
| 91 |
|
| 92 |
|
| 93 |
lambda line: line.tax_ids.ids == tax.ids |
| 94 |
) |
| 95 |
move.l10n_it_edi_doi_amount = sum(declaration_lines.mapped('price_total')) * -move.direction_sign |
| 96 |
|
| 97 |
@api.depends('l10n_it_edi_doi_id', 'l10n_it_edi_doi_amount', 'state') |
| 98 |
def _compute_l10n_it_edi_doi_warning(self): |
| 99 |
for move in self: |
| 100 |
move.l10n_it_edi_doi_warning = '' |
| 101 |
declaration = move.l10n_it_edi_doi_id |
| 102 |
|
| 103 |
show_warning = ( |
| 104 |
declaration |
| 105 |
and move.is_sale_document(include_receipts=False) |
| 106 |
and move.state != 'cancel' |
| 107 |
) |
| 108 |
if not show_warning: |
| 109 |
continue |
| 110 |
|
| 111 |
declaration_invoiced = declaration.invoiced |
| 112 |
declaration_not_yet_invoiced = declaration.not_yet_invoiced |
| 113 |
if move.state != 'posted': |
| 114 |
|
| 115 |
|
| 116 |
declaration_invoiced += move.l10n_it_edi_doi_amount |
| 117 |
additional_invoiced_qty = {} |
| 118 |
linked_orders = self.env['sale.order'] |
| 119 |
for invoice_line in move.invoice_line_ids: |
| 120 |
for sale_line in invoice_line.sale_line_ids: |
| 121 |
order = sale_line.order_id |
| 122 |
if order.l10n_it_edi_doi_id == declaration: |
| 123 |
linked_orders |= order |
| 124 |
qty_invoiced = invoice_line.product_uom_id._compute_quantity(invoice_line.quantity, sale_line.product_uom_id) * -move.direction_sign |
| 125 |
sale_line_id = sale_line.ids[0] |
| 126 |
additional_invoiced_qty[sale_line_id] = additional_invoiced_qty.get(sale_line_id, 0) + qty_invoiced |
| 127 |
for order in linked_orders: |
| 128 |
not_yet_invoiced = order.l10n_it_edi_doi_not_yet_invoiced |
| 129 |
not_yet_invoiced_after_posting = order._l10n_it_edi_doi_get_amount_not_yet_invoiced( |
| 130 |
declaration, |
| 131 |
additional_invoiced_qty=additional_invoiced_qty, |
| 132 |
) |
| 133 |
declaration_not_yet_invoiced -= not_yet_invoiced - not_yet_invoiced_after_posting |
| 134 |
|
| 135 |
validity_warnings = declaration._get_validity_warnings( |
| 136 |
move.company_id, move.commercial_partner_id, move.currency_id, move.l10n_it_edi_doi_date, |
| 137 |
invoiced_amount=declaration_invoiced, |
| 138 |
) |
| 139 |
|
| 140 |
threshold_warning = declaration._build_threshold_warning_message(declaration_invoiced, declaration_not_yet_invoiced) |
| 141 |
|
| 142 |
move.l10n_it_edi_doi_warning = '{}\n\n{}'.format('\n'.join(validity_warnings), threshold_warning).strip() |
| 143 |
|
| 144 |
@api.depends('l10n_it_edi_doi_id') |
| 145 |
def _compute_fiscal_position_id(self): |
| 146 |
super()._compute_fiscal_position_id() |
| 147 |
for move in self: |
| 148 |
declaration_fiscal_position = move.company_id.l10n_it_edi_doi_fiscal_position_id |
| 149 |
if declaration_fiscal_position and move.l10n_it_edi_doi_id: |
| 150 |
move.fiscal_position_id = declaration_fiscal_position |
| 151 |
|
| 152 |
def copy_data(self, default=None): |
| 153 |
data_list = super().copy_data(default) |
| 154 |
for move, data in zip(self, data_list): |
| 155 |
date = fields.Date.context_today(self) |
| 156 |
validity_warnings = move.l10n_it_edi_doi_id._get_validity_warnings( |
| 157 |
move.company_id, move.commercial_partner_id, move.currency_id, date, |
| 158 |
only_blocking=True, |
| 159 |
) |
| 160 |
if validity_warnings: |
| 161 |
del data['l10n_it_edi_doi_id'] |
| 162 |
del data['fiscal_position_id'] |
| 163 |
return data_list |
| 164 |
|
| 165 |
@api.constrains('l10n_it_edi_doi_id') |
| 166 |
def _check_l10n_it_edi_doi_id(self): |
| 167 |
for move in self: |
| 168 |
validity_errors = move.l10n_it_edi_doi_id._get_validity_errors( |
| 169 |
move.company_id, move.partner_id.commercial_partner_id, move.currency_id |
| 170 |
) |
| 171 |
if validity_errors: |
| 172 |
raise UserError('\n'.join(validity_errors)) |
| 173 |
|
| 174 |
def _post(self, soft=True): |
| 175 |
errors = [] |
| 176 |
for move in self: |
| 177 |
declaration = move.l10n_it_edi_doi_id |
| 178 |
if declaration: |
| 179 |
validity_warnings = declaration._get_validity_warnings( |
| 180 |
move.company_id, move.commercial_partner_id, move.currency_id, move.l10n_it_edi_doi_date, |
| 181 |
invoiced_amount=move.l10n_it_edi_doi_amount, |
| 182 |
only_blocking=True |
| 183 |
) |
| 184 |
errors.extend(validity_warnings) |
| 185 |
|
| 186 |
declaration_of_intent_tax = move.company_id.l10n_it_edi_doi_tax_id |
| 187 |
if not declaration_of_intent_tax: |
| 188 |
continue |
| 189 |
|
| 190 |
declaration_lines = move.invoice_line_ids.filtered( |
| 191 |
lambda line: declaration_of_intent_tax in line.tax_ids |
| 192 |
) |
| 193 |
if declaration_lines and not declaration: |
| 194 |
errors.append(_('Given the tax %s is applied, there should be a Declaration of Intent selected.', |
| 195 |
declaration_of_intent_tax.name)) |
| 196 |
if any(line.tax_ids != declaration_of_intent_tax for line in declaration_lines): |
| 197 |
errors.append(_('A line using tax %s should not contain any other taxes', |
| 198 |
declaration_of_intent_tax.name)) |
| 199 |
if errors: |
| 200 |
raise UserError('\n'.join(errors)) |
| 201 |
|
| 202 |
return super()._post(soft) |
| 203 |
|
| 204 |
def action_open_declaration_of_intent(self): |
| 205 |
self.ensure_one() |
| 206 |
return { |
| 207 |
'name': _("Declaration of Intent for %s", self.display_name), |
| 208 |
'type': 'ir.actions.act_window', |
| 209 |
'view_mode': 'form', |
| 210 |
'res_model': 'l10n_it_edi_doi.declaration_of_intent', |
| 211 |
'res_id': self.l10n_it_edi_doi_id.id, |
| 212 |
} |