Commit 64b3a31b authored by Javier Vega's avatar Javier Vega 🌮
Browse files

[IMP] SAT status shown in the document inspector

parent 7fb618f7
Pipeline #90005 failed with stages
in 22 minutes and 31 seconds
......@@ -848,3 +848,43 @@ msgstr ""
#, python-format
msgid "or Click to upload the files"
msgstr ""
#. module: invoice_xunnel
#: model:ir.model.fields,field_description:invoice_xunnel.field_documents_document__sat_status
msgid "Sat Status"
msgstr "Estado del SAT"
#. module: invoice_xunnel
#: model_terms:ir.ui.view,arch_db:invoice_xunnel.documents_view_kanban
msgid "Sat staus: $"
msgstr "Estado del SAT: $"
#. module: invoice_xunnel
#: model:ir.model.fields.selection,name:invoice_xunnel.selection__documents_document__sat_status__not_found
msgid "Not Found"
msgstr "No encontrado"
#. module: invoice_xunnel
#: model:ir.model.fields.selection,name:invoice_xunnel.selection__documents_document__sat_status__undefined
msgid "Not Synced Yet"
msgstr "Aún sin sincronizar"
#. module: invoice_xunnel
#: model:ir.model.fields.selection,name:invoice_xunnel.selection__documents_document__sat_status__cancelled
msgid "Cancelled"
msgstr "Cancelado"
#. module: invoice_xunnel
#: model:ir.model.fields,help:invoice_xunnel.field_documents_document__sat_status
msgid "Refers to the status of the invoice inside the SAT system."
msgstr "Refiere al estado del documento dentro del systema del SAT"
#. module: invoice_xunnel
#: model:ir.model.fields.selection,name:invoice_xunnel.selection__documents_document__sat_status__none
msgid "State not defined"
msgstr "Estado no definido"
#. module: invoice_xunnel
#: model:ir.model.fields.selection,name:invoice_xunnel.selection__documents_document__sat_status__valid
msgid "Valid"
msgstr "Válido"
......@@ -2,9 +2,13 @@ from datetime import datetime
import base64
import json
import logging
import requests
from lxml import objectify
from odoo import models, fields, api
from odoo import models, fields, api, tools
from odoo.addons.l10n_mx_edi.models.account_invoice import CFDI_SAT_QR_STATE
from odoo.exceptions import ValidationError
from odoo.tools.float_utils import float_repr
_logger = logging.getLogger(__name__)
......@@ -12,6 +16,17 @@ _logger = logging.getLogger(__name__)
class Document(models.Model):
_inherit = 'documents.document'
sat_status = fields.Selection(
selection=[
('none', 'State not defined'),
('undefined', 'Not Synced Yet'),
('not_found', 'Not Found'),
('cancelled', 'Cancelled'),
('valid', 'Valid'),
],
compute='_compute_sat_status',
default='undefined',
help='Refers to the status of the invoice inside the SAT system.')
emitter_partner_id = fields.Many2one(
'res.partner',
compute="_compute_emitter_partner_id",
......@@ -81,6 +96,63 @@ class Document(models.Model):
rec.invoice_total_amount = xml.get('Total')
rec.stamp_date = datetime.strptime(stamp_date, "%Y-%m-%dT%H:%M:%S")
@api.depends('datas')
def _compute_sat_status(self):
for rec in self.filtered('xunnel_document'):
xml = rec.get_xml_object(rec.datas)
if not xml:
return
rec.sat_status = self.l10n_mx_edi_update_sat_status_xml(xml)
def l10n_mx_edi_update_sat_status_xml(self, xml):
"""Check SAT WS to make sure the invoice is valid.
inv: dict containing values to check SAT WS correctly.
"""
template = """<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns0="http://tempuri.org/"
xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<ns1:Body>
<ns0:Consulta>
<ns0:expresionImpresa>${data}</ns0:expresionImpresa>
</ns0:Consulta>
</ns1:Body>
</SOAP-ENV:Envelope>"""
supplier_rfc = xml.Emisor.get('Rfc', '').upper()
customer_rfc = xml.Receptor.get('Rfc', '').upper()
amount = float(xml.get('Total', 0.0))
uuid = xml.get('UUID', '')
currency = self.env['res.currency'].search([
('name', '=', xml.get('Moneda', 'MXN'))
])
precision = currency.decimal_places if currency else 0
tfd = self.env['account.move'].l10n_mx_edi_get_tfd_etree(xml)
uuid = tfd.get('UUID', '')
total = float_repr(amount, precision_digits=precision)
params = '?re=%s&amp;rr=%s&amp;tt=%s&amp;id=%s' % (
tools.html_escape(tools.html_escape(supplier_rfc or '')),
tools.html_escape(tools.html_escape(customer_rfc or '')),
total or 0.0, uuid or '')
soap_env = template.format(data=params)
try:
soap_xml = requests.post(
'https://consultaqr.facturaelectronica.sat.gob.mx/ConsultaCFDIService.svc?wsdl',
data=soap_env,
timeout=20,
headers={
'SOAPAction': 'http://tempuri.org/IConsultaCFDIService/Consulta',
'Content-Type': 'text/xml; charset=utf-8'
})
response = objectify.fromstring(soap_xml.text)
status = response.xpath('//a:Estado', namespaces={
'a': 'http://schemas.datacontract.org/2004/07/Sat.Cfdi.Negocio.ConsultaCfdi.Servicio'
})
except Exception as e:
raise ValidationError(str(e))
return CFDI_SAT_QR_STATE.get(status[0] if status else '', 'none')
@api.depends('datas')
def _compute_product_list(self):
documents = self.filtered(
......
......@@ -7,6 +7,7 @@ odoo.define('invoice_xunnel.documents_dashboard', (require) => {
this._super.apply(this, arguments);
_.defaults(this.fieldsInfo[this.viewType], _.pick(this.fields, [
'emitter_partner_id',
'sat_status',
'xunnel_document',
'invoice_total_amount',
'product_list',
......
......@@ -14,6 +14,10 @@ odoo.define('invoice_xunnel.DocumentsInspector', function( require ) {
label: 'Emitter',
icon: 'fa fa-user-circle o_documents_folder_color'
});
this._renderField('sat_status', {
label: 'SAT status',
icon: 'fa fa-bullseye'
});
this._renderField('invoice_total_amount', {
label: 'Total',
icon: 'fa fa-usd o_documents_tag_color'
......
......@@ -8,6 +8,7 @@
<field name="arch" type="xml">
<xpath expr="//search/group" position="before">
<field name="emitter_partner_id" string="Emitter"/>
<field name="sat_status"/>
<field name="invoice_total_amount" string="Total Amount"/>
</xpath>
</field>
......@@ -34,6 +35,9 @@
<p t-if="record.emitter_partner_id.raw_value" class="mb-0">
<field name="name" class="o_text_overflow"/>
</p>
<p t-if="record.sat_status.raw_value" class="mb-0">
Sat staus: $ <field name="sat_status"/>
</p>
<p t-if="record.invoice_total_amount.raw_value" class="mb-0">
Total amount: $ <field name="invoice_total_amount"/>
</p>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment