|
| 1 | +from odoo import _, api, models |
| 2 | +from odoo.exceptions import UserError |
| 3 | + |
| 4 | + |
| 5 | +class AccountJournal(models.Model): |
| 6 | + _inherit = "account.journal" |
| 7 | + |
| 8 | + def _get_l10n_ar_afip_ws(self): |
| 9 | + """Return the list of values of the selection field.""" |
| 10 | + res = super()._get_l10n_ar_afip_ws() |
| 11 | + return [ |
| 12 | + ("wsmtxca", _("Codificación de producto - RG2904 (WSMTXCA)")), |
| 13 | + ] + res |
| 14 | + |
| 15 | + def _get_l10n_ar_afip_pos_types_selection(self): |
| 16 | + """Add more options to the selection field AFIP POS System, re order options by common use""" |
| 17 | + res = super()._get_l10n_ar_afip_pos_types_selection() |
| 18 | + res.insert(0, ("WSMTXCAWS", _("Codificación de producto - Web Service"))) |
| 19 | + return res |
| 20 | + |
| 21 | + @api.depends("l10n_ar_afip_pos_system") |
| 22 | + def _compute_l10n_ar_afip_ws(self): |
| 23 | + """Depending on AFIP POS System selected set the proper AFIP WS""" |
| 24 | + super()._compute_l10n_ar_afip_ws() |
| 25 | + type_mapping = {"WSMTXCAWS": "wsmtxca"} |
| 26 | + for rec in self: |
| 27 | + rec.l10n_ar_afip_ws = type_mapping.get(rec.l10n_ar_afip_pos_system, False) |
| 28 | + |
| 29 | + def l10n_ar_check_afip_pos_number(self): |
| 30 | + """Return information about the AFIP POS numbers related to the given AFIP WS""" |
| 31 | + if self.l10n_ar_afip_ws == "wsmtxca": |
| 32 | + self.ensure_one() |
| 33 | + connection = self.company_id._l10n_ar_get_connection(self.l10n_ar_afip_ws) |
| 34 | + client, auth = connection._get_client() |
| 35 | + if self.company_id._get_environment_type() == "testing": |
| 36 | + raise UserError( |
| 37 | + _( |
| 38 | + '"Check Available AFIP PoS" is not implemented in testing mode for webservice %s', |
| 39 | + self.l10n_ar_afip_ws, |
| 40 | + ) |
| 41 | + ) |
| 42 | + auth = self._wsmtxca_convert_auth(auth) |
| 43 | + response = client.service.consultarPuntosVentaCAE(auth) |
| 44 | + raise UserError(response) |
| 45 | + return super().l10n_ar_check_afip_pos_number() |
| 46 | + |
| 47 | + def _l10n_ar_get_afip_last_invoice_number(self, document_type): |
| 48 | + """Consult via webservice the number of the last invoice register""" |
| 49 | + afip_ws = self.l10n_ar_afip_ws |
| 50 | + if afip_ws == "wsmtxca": |
| 51 | + self.ensure_one() |
| 52 | + if self.env.registry.in_test_mode(): |
| 53 | + return 0 |
| 54 | + |
| 55 | + pos_number = self.l10n_ar_afip_pos_number |
| 56 | + connection = self.company_id._l10n_ar_get_connection(afip_ws) |
| 57 | + client, auth = connection._get_client() |
| 58 | + last = errors = False |
| 59 | + |
| 60 | + data = self._wsmtxca_convert_auth(auth) |
| 61 | + response = client.service.consultarUltimoComprobanteAutorizado( |
| 62 | + data, |
| 63 | + consultaUltimoComprobanteAutorizadoRequest={ |
| 64 | + "codigoTipoComprobante": document_type.code, |
| 65 | + "numeroPuntoVenta": pos_number, |
| 66 | + }, |
| 67 | + ) |
| 68 | + if response.numeroComprobante: |
| 69 | + last = response.numeroComprobante |
| 70 | + if response.arrayErrores: |
| 71 | + errors = "".join( |
| 72 | + [ |
| 73 | + "\n* Code %s: %s" % (err.codigo, err.descripcion) |
| 74 | + for err in response.arrayErrores.codigoDescripcion |
| 75 | + ] |
| 76 | + ) |
| 77 | + error_first_invoice = [1502] |
| 78 | + if error_first_invoice == [err.codigo for err in response.arrayErrores.codigoDescripcion]: |
| 79 | + errors = False # No invoices found for this document type |
| 80 | + last = 0 |
| 81 | + |
| 82 | + if errors: |
| 83 | + raise UserError( |
| 84 | + _("We receive this error trying to consult the last invoice number to AFIP:\n%s", errors) |
| 85 | + ) |
| 86 | + return last |
| 87 | + return super()._l10n_ar_get_afip_last_invoice_number(document_type) |
| 88 | + |
| 89 | + @api.model |
| 90 | + def _get_codes_per_journal_type(self, afip_pos_system): |
| 91 | + res = super()._get_codes_per_journal_type(afip_pos_system) |
| 92 | + if afip_pos_system != "WSMTXCAWS": |
| 93 | + return res |
| 94 | + |
| 95 | + usual_codes = ["1", "2", "3", "6", "7", "8"] |
| 96 | + invoice_m_code = ["51", "52", "53"] |
| 97 | + mipyme_codes = ["201", "202", "203", "206", "207", "208"] |
| 98 | + codes = usual_codes + invoice_m_code + mipyme_codes |
| 99 | + |
| 100 | + res[0] = ("code", "in", codes) |
| 101 | + return res |
| 102 | + |
| 103 | + @api.model |
| 104 | + def _wsmtxca_convert_auth(self, auth): |
| 105 | + res = {"sign": auth.get("Sign"), "token": auth.get("Token"), "cuitRepresentada": auth.get("Cuit")} |
| 106 | + return res |
0 commit comments