| 1 |
from io import BytesIO |
| 2 |
import logging |
| 3 |
import re |
| 4 |
|
| 5 |
from odoo import _, api, models |
| 6 |
|
| 7 |
_logger = logging.getLogger(__name__) |
| 8 |
|
| 9 |
|
| 10 |
class AccountMoveSend(models.AbstractModel): |
| 11 |
_inherit = 'account.move.send' |
| 12 |
|
| 13 |
@api.model |
| 14 |
def _is_tr_nilvera_applicable(self, move): |
| 15 |
return move.l10n_tr_nilvera_send_status == 'not_sent' and move.is_invoice(include_receipts=True) and move.country_code == 'TR' |
| 16 |
|
| 17 |
def _get_all_extra_edis(self) -> dict: |
| 18 |
# EXTENDS 'account' |
| 19 |
res = super()._get_all_extra_edis() |
| 20 |
res.update({'tr_nilvera': {'label': _("by Nilvera"), 'is_applicable': self._is_tr_nilvera_applicable}}) |
| 21 |
return res |
| 22 |
|
| 23 |
# ------------------------------------------------------------------------- |
| 24 |
# ATTACHMENTS |
| 25 |
# ------------------------------------------------------------------------- |
| 26 |
|
| 27 |
def _get_invoice_extra_attachments(self, move): |
| 28 |
# EXTENDS 'account' |
| 29 |
# Add the Nilvera PDF to the mail attachments. |
| 30 |
attachments = super()._get_invoice_extra_attachments(move) |
| 31 |
if move.l10n_tr_nilvera_send_status == 'succeed' and move.message_main_attachment_id.id != move.invoice_pdf_report_id.id: |
| 32 |
attachments += move.message_main_attachment_id |
| 33 |
return attachments |
| 34 |
|
| 35 |
# ------------------------------------------------------------------------- |
| 36 |
# ALERTS |
| 37 |
# ------------------------------------------------------------------------- |
| 38 |
|
| 39 |
def _get_alerts(self, moves, moves_data): |
| 40 |
def _is_valid_nilvera_name(move): |
| 41 |
_, parts = move._get_sequence_format_param(move.name) |
| 42 |
|
| 43 |
return ( |
| 44 |
parts['year'] != 0 |
| 45 |
and parts['year_length'] == 4 |
| 46 |
and parts['seq'] != 0 |
| 47 |
and re.match(r'^[A-Za-z0-9]{3}[^A-Za-z0-9]?$', parts['prefix1']) |
| 48 |
) |
| 49 |
|
| 50 |
alerts = super()._get_alerts(moves, moves_data) |
| 51 |
|
| 52 |
# Filter for moves that have 'tr_nilvera' in their EDI data |
| 53 |
tr_nilvera_moves = moves.filtered(lambda m: 'tr_nilvera' in moves_data[m]['extra_edis']) |
| 54 |
|
| 55 |
# Show alert if the current company is in Türkiye and test mode is enabled for Nilvera |
| 56 |
if self.env.company.account_fiscal_country_id.code == 'TR' and self.env.company.l10n_tr_nilvera_use_test_env: |
| 57 |
alerts['l10n_tr_nilvera_einvoice_test_mode'] = { |
| 58 |
'level': 'info', |
| 59 |
'message': _("Testing mode is enabled."), |
| 60 |
} |
| 61 |
|
| 62 |
if tr_companies_missing_required_codes := tr_nilvera_moves.company_id.filtered(lambda c: c.country_code == 'TR' and not (c.partner_id.category_id.parent_id and self.env["res.partner.category"]._get_l10n_tr_official_mandatory_categories())): |
| 63 |
alerts["tr_companies_missing_required_codes"] = { |
| 64 |
"message": _("Please ensure that your company contact has either the 'MERSISNO' or 'TICARETSICILNO' tag with a value assigned."), |
| 65 |
"action_text": _("View Company(s)"), |
| 66 |
"action": tr_companies_missing_required_codes.partner_id._get_records_action(name=_("Check tags on company(s)")), |
| 67 |
"level": "danger", |
| 68 |
} |
| 69 |
|
| 70 |
# Alert if company is missing required data (country = TR, and tax ID, city, state, street) |
| 71 |
if tr_companies_missing_required_fields := tr_nilvera_moves.filtered( |
| 72 |
lambda m: ( |
| 73 |
not m.company_id.vat |
| 74 |
or not m.company_id.street |
| 75 |
or not m.company_id.city |
| 76 |
or not m.company_id.state_id |
| 77 |
or m.company_id.country_code != 'TR' |
| 78 |
) |
| 79 |
).company_id: |
| 80 |
alerts["tr_companies_missing_required_fields"] = { |
| 81 |
'level': 'danger', |
| 82 |
"message": _( |
| 83 |
"The following company(s) either do not have their country set as Türkiye " |
| 84 |
"or are missing at least one of these fields: Tax ID, Street, City, or State" |
| 85 |
), |
| 86 |
"action_text": _("View Company(s)"), |
| 87 |
"action": tr_companies_missing_required_fields._get_records_action(name=_( |
| 88 |
"Check Tax ID, City, Street, State, and Country or Company(s)" |
| 89 |
)), |
| 90 |
} |
| 91 |
|
| 92 |
# Alert if partner is missing required data (tax ID, street, city, state, country) |
| 93 |
if tr_partners_missing_required_fields := self._get_l10n_tr_tax_partner_address_alert(tr_nilvera_moves): |
| 94 |
alerts["tr_partners_missing_required_fields"] = tr_partners_missing_required_fields |
| 95 |
|
| 96 |
# Alert if partner is missing required tax office |
| 97 |
if tr_partners_missing_tax_office := self._get_l10n_tr_tax_partner_tax_office_alert(tr_nilvera_moves): |
| 98 |
alerts["tr_partners_missing_tax_office"] = tr_partners_missing_tax_office |
| 99 |
|
| 100 |
# Alert if TR company is missing required tax office |
| 101 |
if tr_companies_missing_tax_office := self._get_l10n_tr_tax_company_tax_office_alert(tr_nilvera_moves): |
| 102 |
alerts["tr_companies_missing_tax_office"] = tr_companies_missing_tax_office |
| 103 |
|
| 104 |
# Alert if partner does not use UBL TR e-invoice format or has not checked Nilvera status |
| 105 |
if tr_partners_invalid_edi_or_status := tr_nilvera_moves.filtered( |
| 106 |
lambda m: ( |
| 107 |
m.partner_id.invoice_edi_format != 'ubl_tr' |
| 108 |
or m.partner_id.l10n_tr_nilvera_customer_status == 'not_checked' |
| 109 |
) |
| 110 |
).partner_id: |
| 111 |
alerts["tr_partners_invalid_edi_or_status"] = { |
| 112 |
'level': 'danger', |
| 113 |
"message": _( |
| 114 |
"The following partner(s) either do not have the e-invoice format UBL TR 1.2 " |
| 115 |
"or have not checked their Nilvera Status" |
| 116 |
), |
| 117 |
"action_text": _("View Partner(s)"), |
| 118 |
"action": tr_partners_invalid_edi_or_status._get_records_action( |
| 119 |
name=_("Check e-Invoice Format or Nilvera Status on Partner(s)" |
| 120 |
)), |
| 121 |
} |
| 122 |
|
| 123 |
if tr_invalid_subscription_dates := moves.filtered( |
| 124 |
lambda move: move._l10n_tr_nilvera_einvoice_check_invalid_subscription_dates() |
| 125 |
): |
| 126 |
alerts["tr_invalid_subscription_dates"] = { |
| 127 |
'level': 'danger', |
| 128 |
"message": _( |
| 129 |
"The following invoice(s) need to have the same Start Date and End Date " |
| 130 |
"on all their respective Invoice Lines." |
| 131 |
), |
| 132 |
"action_text": _("View Invoice(s)"), |
| 133 |
"action": tr_invalid_subscription_dates._get_records_action( |
| 134 |
name=_("Check data on Invoice(s)"), |
| 135 |
), |
| 136 |
} |
| 137 |
|
| 138 |
if invalid_negative_lines := tr_nilvera_moves.filtered( |
| 139 |
lambda move: move._l10n_tr_nilvera_einvoice_check_negative_lines(), |
| 140 |
): |
| 141 |
alerts["critical_invalid_negative_lines"] = { |
| 142 |
"level": "danger", |
| 143 |
"message": _("Nilvera portal cannot process negative quantity nor negative price on invoice lines"), |
| 144 |
"action_text": _("View Invoice(s)"), |
| 145 |
"action": invalid_negative_lines._get_records_action(name=_("Check data on Invoice(s)")), |
| 146 |
} |
| 147 |
|
| 148 |
if moves_with_invalid_name := tr_nilvera_moves.filtered(lambda move: not _is_valid_nilvera_name(move)): |
| 149 |
alerts['tr_moves_with_invalid_name'] = { |
| 150 |
'level': 'danger', |
| 151 |
'message': _( |
| 152 |
"The invoice name must follow the format when sending to Nilvera: 3 alphanumeric characters, " |
| 153 |
"followed by the year, and then a sequential number. Example: INV/2025/000001", |
| 154 |
), |
| 155 |
'action_text': _("View Invoice(s)"), |
| 156 |
'action': moves_with_invalid_name._get_records_action(name=_("Check name on Invoice(s)")), |
| 157 |
} |
| 158 |
|
| 159 |
return alerts |
| 160 |
|
| 161 |
def _get_l10n_tr_tax_partner_address_alert(self, moves): |
| 162 |
# Extended in l10n_tr_nilvera_einvoice_extended to remove error based on l10n_tr_is_export_invoice |
| 163 |
if tr_partners_missing_required_fields := moves.filtered( |
| 164 |
lambda m: ( |
| 165 |
not m.partner_id.vat |
| 166 |
or not m.partner_id.street |
| 167 |
or not m.partner_id.city |
| 168 |
or not m.partner_id.state_id |
| 169 |
or not m.partner_id.country_id |
| 170 |
), |
| 171 |
).partner_id: |
| 172 |
return { |
| 173 |
"message": _("The following partner(s) are missing at least one of these fields: Tax ID, Street, City, State or Country"), |
| 174 |
"action_text": _("View Partner(s)"), |
| 175 |
"action": tr_partners_missing_required_fields._get_records_action(name=_("Check Tax ID, City, Street, State, and Country or Partner(s)")), |
| 176 |
"level": "danger", |
| 177 |
} |
| 178 |
return {} |
| 179 |
|
| 180 |
def _get_l10n_tr_tax_partner_tax_office_alert(self, moves): |
| 181 |
# Overriden in l10n_tr_nilvera_einvoice_extended to give error based on l10n_tax_office field |
| 182 |
if tr_einvoice_partners_missing_ref := moves.partner_id.filtered(lambda p: p.l10n_tr_nilvera_customer_status == "einvoice" and not p.ref and p.country_code == "TR"): |
| 183 |
return { |
| 184 |
"message": _("The following E-Invoice partner(s) must have the reference field set to the tax office name."), |
| 185 |
"action_text": _("View Partner(s)"), |
| 186 |
"action": tr_einvoice_partners_missing_ref._get_records_action(name=_("Check reference on Partner(s)")), |
| 187 |
"level": "danger", |
| 188 |
} |
| 189 |
|
| 190 |
return {} |
| 191 |
|
| 192 |
def _get_l10n_tr_tax_company_tax_office_alert(self, moves): |
| 193 |
# Overriden in l10n_tr_nilvera_einvoice_extended to give error based on l10n_tax_office field |
| 194 |
if tr_companies_missing_tax_office := moves.company_id.partner_id.filtered(lambda p: not p.reference and p.country_code == "TR"): |
| 195 |
return { |
| 196 |
"message": _("The following TR Company(s) must have the reference field set to the tax office name."), |
| 197 |
"action_text": _("View Company(s)"), |
| 198 |
"action": tr_companies_missing_tax_office._get_records_action(name=_("TR Company(s)")), |
| 199 |
"level": "danger", |
| 200 |
} |
| 201 |
return {} |
| 202 |
|
| 203 |
# ------------------------------------------------------------------------- |
| 204 |
# BUSINESS ACTIONS |
| 205 |
# ------------------------------------------------------------------------- |
| 206 |
def _link_invoice_documents(self, invoices_data): |
| 207 |
# EXTENDS 'account' |
| 208 |
super()._link_invoice_documents(invoices_data) |
| 209 |
# The move needs to be put as sent only if sent by Nilvera |
| 210 |
for invoice, invoice_data in invoices_data.items(): |
| 211 |
if invoice.company_id.country_code == 'TR': |
| 212 |
invoice.is_move_sent = invoice.l10n_tr_nilvera_send_status == 'sent' |
| 213 |
|
| 214 |
|
| 215 |
@api.model |
| 216 |
def _call_web_service_before_invoice_pdf_render(self, invoices_data): |
| 217 |
# EXTENDS 'account' |
| 218 |
super()._call_web_service_before_invoice_pdf_render(invoices_data) |
| 219 |
|
| 220 |
for invoice, invoice_data in invoices_data.items(): |
| 221 |
if 'tr_nilvera' in invoice_data['extra_edis']: |
| 222 |
if attachment_values := invoice_data.get('ubl_cii_xml_attachment_values'): |
| 223 |
xml_file = BytesIO(attachment_values.get('raw')) |
| 224 |
xml_file.name = attachment_values['name'] |
| 225 |
else: |
| 226 |
xml_file = BytesIO(invoice.ubl_cii_xml_id.raw or b'') |
| 227 |
xml_file.name = invoice.ubl_cii_xml_id.name or '' |
| 228 |
|
| 229 |
if not invoice.partner_id.l10n_tr_nilvera_customer_alias_id: |
| 230 |
# If no alias is saved, the user is either an E-Archive user or we haven't checked before. Check again |
| 231 |
# just in case. |
| 232 |
invoice.partner_id._check_nilvera_customer() |
| 233 |
customer_alias = invoice._get_partner_l10n_tr_nilvera_customer_alias_name() |
| 234 |
if customer_alias: # E-Invoice |
| 235 |
invoice._l10n_tr_nilvera_submit_einvoice(xml_file, customer_alias) |
| 236 |
else: # E-Archive |
| 237 |
invoice._l10n_tr_nilvera_submit_earchive(xml_file) |
| 238 |
|
| 239 |
@api.model |
| 240 |
def _postprocess_invoice_ubl_xml(self, invoice, invoice_data): |
| 241 |
# EXTENDS account_edi_ubl_cii |
| 242 |
# Nilvera rejects XMLs with the PDF attachment. |
| 243 |
|
| 244 |
if invoice_data['invoice_edi_format'] == 'ubl_tr': |
| 245 |
return |
| 246 |
|
| 247 |
return super()._postprocess_invoice_ubl_xml(invoice, invoice_data) |