account_cfdi_xml.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-
  2. from odoo import _, api, fields, models
  3. from odoo.exceptions import UserError, ValidationError
  4. import logging
  5. _logger = logging.getLogger(__name__)
  6. class AccountCfdiXml(models.TransientModel):
  7. _name = 'account.cfdi.xml'
  8. _description = 'Importación por XML'
  9. company_id = fields.Many2one(comodel_name='res.company', string='Compañía', default=lambda self: self.env.company, readonly=True)
  10. filedata_file = fields.Many2many(comodel_name='ir.attachment', string='Archivos XML')
  11. filedata_name = fields.Char(string="Nombre de archivo")
  12. def import_file(self):
  13. if len(self.filedata_file) > 0:
  14. attachment_list = []
  15. for content in self.filedata_file:
  16. try:
  17. attachment_data = {
  18. 'name': content.name,
  19. 'type': 'binary',
  20. 'company_id': self.company_id.id,
  21. 'datas': content.datas,
  22. 'store_fname': content.name,
  23. 'mimetype': 'application/xml'
  24. }
  25. data_uuid = {
  26. "xml": attachment_data,
  27. }
  28. attachment_list.append(data_uuid)
  29. except Exception as e:
  30. _logger.info(e)
  31. if attachment_list:
  32. cfdi_ids = self.env['account.cfdi'].create_cfdis(attachment_list)
  33. if cfdi_ids:
  34. return {
  35. "name": _("CFDIs importados"),
  36. "view_mode": "list,form",
  37. "res_model": "account.cfdi",
  38. "type": "ir.actions.act_window",
  39. "target": "current",
  40. "domain": [("id", "=", cfdi_ids.ids)]
  41. }
  42. else:
  43. return {
  44. 'type': 'ir.actions.client',
  45. 'tag': 'display_notification',
  46. 'params': {
  47. 'title': _("No se cargaron nuevos CFDIs al sistema ya que estos ya existen o no pertenecen a la empresa, favor de validar."),
  48. 'type': 'warning',
  49. 'sticky': True,
  50. },
  51. }
  52. else:
  53. raise ValidationError(_('No ha subido ningún archivo XML'))