Encapsulates different methods to be able to perform a better inheritance in other modules
This commit is contained in:
parent
90f87328c8
commit
7f2128a357
59
invoice.py
59
invoice.py
|
@ -108,6 +108,9 @@ FACe_REQUIRED_FIELDS = ['facturae_person_type', 'facturae_residence_type']
|
||||||
_slugify_strip_re = re.compile(r'[^\w\s-]')
|
_slugify_strip_re = re.compile(r'[^\w\s-]')
|
||||||
_slugify_hyphenate_re = re.compile(r'[-\s]+')
|
_slugify_hyphenate_re = re.compile(r'[-\s]+')
|
||||||
|
|
||||||
|
DEFAULT_FACTURAE_TEMPLATE = 'template_facturae_3.2.1.xml'
|
||||||
|
DEFAULT_FACTURAE_SCHEMA = 'Facturaev3_2_1-offline.xsd'
|
||||||
|
|
||||||
|
|
||||||
def slugify(value):
|
def slugify(value):
|
||||||
if not isinstance(value, str):
|
if not isinstance(value, str):
|
||||||
|
@ -258,21 +261,37 @@ class Invoice(metaclass=PoolMeta):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def generate_facturae_default(cls, invoices, certificate_password):
|
def generate_facturae_default(cls, invoices, certificate_password):
|
||||||
to_save = []
|
to_write = ([],)
|
||||||
for invoice in invoices:
|
for invoice in invoices:
|
||||||
if invoice.invoice_facturae:
|
if invoice.invoice_facturae:
|
||||||
continue
|
continue
|
||||||
facturae_content = invoice.get_facturae()
|
facturae_content = invoice.get_facturae()
|
||||||
invoice._validate_facturae(facturae_content)
|
invoice._validate_facturae(facturae_content)
|
||||||
if backend.name() != 'sqlite':
|
if backend.name() != 'sqlite':
|
||||||
invoice.invoice_facturae = invoice._sign_facturae(
|
invoice_facturae = invoice._sign_facturae(
|
||||||
facturae_content, certificate_password)
|
facturae_content, certificate_password)
|
||||||
to_save.append(invoice)
|
else:
|
||||||
if to_save:
|
invoice_facturae = facturae_content
|
||||||
cls.save(to_save)
|
to_write[0].append(invoice)
|
||||||
|
to_write += ({'invoice_facturae': invoice_facturae},)
|
||||||
|
if to_write:
|
||||||
|
cls.write(*to_write)
|
||||||
|
|
||||||
def get_facturae(self):
|
def get_facturae(self):
|
||||||
"""Return the content of factura-e XML file"""
|
jinja_env = Environment(
|
||||||
|
loader=FileSystemLoader(module_path()),
|
||||||
|
trim_blocks=True,
|
||||||
|
lstrip_blocks=True,
|
||||||
|
)
|
||||||
|
template = DEFAULT_FACTURAE_TEMPLATE
|
||||||
|
return self._get_jinja_template(jinja_env, template).render(
|
||||||
|
self._get_content_to_render(), ).encode('utf-8')
|
||||||
|
|
||||||
|
def _get_jinja_template(self, jinja_env, template):
|
||||||
|
return jinja_env.get_template(template)
|
||||||
|
|
||||||
|
def _get_content_to_render(self):
|
||||||
|
"""Return the content to render in factura-e XML file"""
|
||||||
pool = Pool()
|
pool = Pool()
|
||||||
Currency = pool.get('currency.currency')
|
Currency = pool.get('currency.currency')
|
||||||
Date = pool.get('ir.date')
|
Date = pool.get('ir.date')
|
||||||
|
@ -400,14 +419,7 @@ class Invoice(metaclass=PoolMeta):
|
||||||
'invoice': self.rec_name,
|
'invoice': self.rec_name,
|
||||||
})
|
})
|
||||||
|
|
||||||
jinja_env = Environment(
|
return {
|
||||||
loader=FileSystemLoader(module_path()),
|
|
||||||
trim_blocks=True,
|
|
||||||
lstrip_blocks=True,
|
|
||||||
)
|
|
||||||
jinja_template = jinja_env.get_template('template_facturae_3.2.1.xml')
|
|
||||||
|
|
||||||
return jinja_template.render({
|
|
||||||
'invoice': self,
|
'invoice': self,
|
||||||
'Decimal': Decimal,
|
'Decimal': Decimal,
|
||||||
'Currency': Currency,
|
'Currency': Currency,
|
||||||
|
@ -415,29 +427,32 @@ class Invoice(metaclass=PoolMeta):
|
||||||
'exchange_rate': exchange_rate,
|
'exchange_rate': exchange_rate,
|
||||||
'exchange_rate_date': exchange_rate_date,
|
'exchange_rate_date': exchange_rate_date,
|
||||||
'UOM_CODE2TYPE': UOM_CODE2TYPE,
|
'UOM_CODE2TYPE': UOM_CODE2TYPE,
|
||||||
}, ).encode('utf-8')
|
}
|
||||||
|
|
||||||
def _validate_facturae(self, xml_string):
|
de _validate_facturae(self, xffml_string, schema_file_path=None):
|
||||||
"""
|
"""
|
||||||
Inspired by https://github.com/pedrobaeza/l10n-spain/blob/d01d049934db55130471e284012be7c860d987eb/l10n_es_facturae/wizard/create_facturae.py
|
Inspired by https://github.com/pedrobaeza/l10n-spain/blob/d01d049934db55130471e284012be7c860d987eb/l10n_es_facturae/wizard/create_facturae.py
|
||||||
"""
|
"""
|
||||||
logger = logging.getLogger('account_invoice_facturae')
|
logger = logging.getLogger('account_invoice_facturae')
|
||||||
|
|
||||||
schema_file_path = os.path.join(
|
if not schema_file_path:
|
||||||
module_path(),
|
schema_file_path = os.path.join(
|
||||||
'Facturaev3_2_1-offline.xsd')
|
module_path(),
|
||||||
with open(schema_file_path, encoding='utf-8') as schema_file:
|
DEFAULT_FACTURAE_SCHEMA)
|
||||||
|
with open(schema_file_path) as schema_file:
|
||||||
facturae_schema = etree.XMLSchema(file=schema_file)
|
facturae_schema = etree.XMLSchema(file=schema_file)
|
||||||
logger.debug("Schema Facturaev3_2_1-offline.xsd loaded")
|
logger.debug("%s loaded" % schema_file_path)
|
||||||
|
logger.debug("%s loaded" % schema_file_path)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
facturae_schema.assertValid(etree.fromstring(xml_string))
|
facturae_schema.assertValid(etree.fromstring(xml_string))
|
||||||
logger.debug("Factura-e XML of invoice %s validated",
|
logger.debug("Factura-e XML of invoice %s validated",
|
||||||
self.rec_name)
|
self.rec_name)
|
||||||
except Exception as e:
|
except Exception, e:
|
||||||
logger.warning("Error validating generated Factura-e file",
|
logger.warning("Error validating generated Factura-e file",
|
||||||
exc_info=True)
|
exc_info=True)
|
||||||
logger.debug(xml_string)
|
logger.debug(xml_string)
|
||||||
|
logger.debug("Factura-e XML of invoice %s validated",
|
||||||
self.raise_user_error('invalid_factura_xml_file', (self.rec_name, e))
|
self.raise_user_error('invalid_factura_xml_file', (self.rec_name, e))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue