| 1 |
|
| 2 |
|
| 3 |
from odoo import api, fields, models |
| 4 |
from odoo.tools import html_escape |
| 5 |
|
| 6 |
|
| 7 |
class AccountEdiFormat(models.Model): |
| 8 |
_name = 'account.edi.format' |
| 9 |
_description = 'EDI format' |
| 10 |
|
| 11 |
name = fields.Char() |
| 12 |
code = fields.Char(required=True) |
| 13 |
|
| 14 |
_unique_code = models.Constraint( |
| 15 |
'unique (code)', |
| 16 |
'This code already exists', |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
@api.model_create_multi |
| 24 |
def create(self, vals_list): |
| 25 |
edi_formats = super().create(vals_list) |
| 26 |
|
| 27 |
if not edi_formats: |
| 28 |
return edi_formats |
| 29 |
|
| 30 |
|
| 31 |
if not self.pool.loaded: |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
self.pool._delay_compute_edi_format_ids = True |
| 36 |
else: |
| 37 |
journals = self.env['account.journal'].search([]) |
| 38 |
journals._compute_edi_format_ids() |
| 39 |
|
| 40 |
|
| 41 |
if any(edi_format._needs_web_services() for edi_format in edi_formats): |
| 42 |
self.env.ref('account_edi.ir_cron_edi_network').active = True |
| 43 |
|
| 44 |
return edi_formats |
| 45 |
|
| 46 |
def _register_hook(self): |
| 47 |
if hasattr(self.pool, "_delay_compute_edi_format_ids"): |
| 48 |
del self.pool._delay_compute_edi_format_ids |
| 49 |
journals = self.env['account.journal'].search([]) |
| 50 |
journals._compute_edi_format_ids() |
| 51 |
|
| 52 |
return super()._register_hook() |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
def _get_move_applicability(self, move): |
| 59 |
""" Core function for the EDI processing: it first checks whether the EDI format is applicable on a given |
| 60 |
move, if so, it then returns a dictionary containing the functions to call for this move. |
| 61 |
|
| 62 |
:return: dict mapping str to function (callable) |
| 63 |
* post: function called for edi.documents with state 'to_send' (post flow) |
| 64 |
* cancel: function called for edi.documents with state 'to_cancel' (cancel flow) |
| 65 |
* post_batching: function returning the batching key for the post flow |
| 66 |
* cancel_batching: function returning the batching key for the cancel flow |
| 67 |
* edi_content: function called when computing the edi_content for an edi.document |
| 68 |
""" |
| 69 |
self.ensure_one() |
| 70 |
|
| 71 |
def _needs_web_services(self): |
| 72 |
""" Indicate if the EDI must be generated asynchronously through to some web services. |
| 73 |
|
| 74 |
:return: True if such a web service is available, False otherwise. |
| 75 |
""" |
| 76 |
self.ensure_one() |
| 77 |
return False |
| 78 |
|
| 79 |
def _is_compatible_with_journal(self, journal): |
| 80 |
""" Indicate if the EDI format should appear on the journal passed as parameter to be selected by the user. |
| 81 |
If True, this EDI format will appear on the journal. |
| 82 |
|
| 83 |
:param journal: The journal. |
| 84 |
:returns: True if this format can appear on the journal, False otherwise. |
| 85 |
""" |
| 86 |
|
| 87 |
self.ensure_one() |
| 88 |
return journal.type == 'sale' |
| 89 |
|
| 90 |
def _is_enabled_by_default_on_journal(self, journal): |
| 91 |
""" Indicate if the EDI format should be selected by default on the journal passed as parameter. |
| 92 |
If True, this EDI format will be selected by default on the journal. |
| 93 |
|
| 94 |
:param journal: The journal. |
| 95 |
:returns: True if this format should be enabled by default on the journal, False otherwise. |
| 96 |
""" |
| 97 |
return True |
| 98 |
|
| 99 |
def _check_move_configuration(self, move): |
| 100 |
""" Checks the move and relevant records for potential error (missing data, etc). |
| 101 |
|
| 102 |
:param move: The move to check. |
| 103 |
:returns: A list of error messages. |
| 104 |
""" |
| 105 |
|
| 106 |
return [] |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
def _prepare_invoice_report(self, pdf_writer, edi_document): |
| 113 |
""" |
| 114 |
Prepare invoice report to be printed. |
| 115 |
:param pdf_writer: The pdf writer with the invoice pdf content loaded. |
| 116 |
:param edi_document: The edi document to be added to the pdf file. |
| 117 |
""" |
| 118 |
|
| 119 |
self.ensure_one() |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
@api.model |
| 126 |
def _format_error_message(self, error_title, errors): |
| 127 |
bullet_list_msg = ''.join('<li>%s</li>' % html_escape(msg) for msg in errors) |
| 128 |
return '%s<ul>%s</ul>' % (error_title, bullet_list_msg) |