Skip to content

Commit

Permalink
[ADD] mejoras y controles en los tipos de comprobantes y reconociendo…
Browse files Browse the repository at this point in the history
… por el RNC el nombre del contacto
  • Loading branch information
alexcape14 committed Apr 26, 2024
1 parent 73cb1bb commit 421b5eb
Show file tree
Hide file tree
Showing 8 changed files with 311 additions and 132 deletions.
44 changes: 37 additions & 7 deletions l10n_do_pos/controller/main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import base64
import datetime
import json
import os
import logging

import odoo

from odoo import http, models, fields, _
from odoo.http import request

Expand All @@ -23,3 +16,40 @@ def get_ncf_order(self, order):
if pos_order_id:
if pos_order_id.account_move:
pos_order_id.write({'l10n_latam_document_number': pos_order_id.account_move.l10n_latam_document_number})

class DGIIController(http.Controller):
@http.route('/dgii/get_contribuyentes', type='json', auth='user', methods=['POST'])
def get_contribuyentes(self, vat):
if not vat:
return {'error': 'VAT is missing'}

result = request.env['res.partner'].sudo().get_contribuyentes(vat)
return result

class PosOrderController(http.Controller):
@http.route('/pos/my_orders', type='json', auth='user')
def get_pos_orders(self):
# Busca todos los pedidos POS para la sesión POS activa y en estado 'draft'
pos_session = request.env['pos.session'].search([('state', '=', 'opened'), ('user_id', '=', request.uid)],
limit=1)
if pos_session:
orders = request.env['pos.order'].search([
('session_id', '=', pos_session.id),
('state', '=', 'draft')
])
orders_data = [{
'id': order.id,
'name': order.name,
'total': order.amount_total
} for order in orders]
return {'orders': orders_data}
else:
return {'error': 'No active POS session found'}

@http.route('/check_invoices', type='json', auth='user')
def check_invoices(self, partner_id=None, **kwargs):
if partner_id:
invoices = request.env['account.move'].sudo().search(
[('partner_id', '=', partner_id), ('state', '=', 'posted')])
return {'has_invoices': bool(invoices)}
return {'has_invoices': False}
1 change: 1 addition & 0 deletions l10n_do_pos/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from . import pos_order
from . import ir_sequence
from . import pos_session
from . import res_partner
8 changes: 3 additions & 5 deletions l10n_do_pos/models/pos_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ def _order_fields(self, ui_order):
if ui_order["to_invoice"]:
res.update(
{
# "account_move": ui_order['account_move'],
# "l10n_latam_sequence_id": ui_order["l10n_latam_sequence_id"],
"l10n_latam_document_number": ui_order[
"l10n_latam_document_number"
],
Expand All @@ -105,7 +103,7 @@ def _order_fields(self, ui_order):
"l10n_latam_use_documents": True,
"l10n_do_origin_ncf": ui_order["l10n_do_origin_ncf"],
"l10n_do_return_status": ui_order["l10n_do_return_status"],
# "l10n_do_is_return_order": ui_order["l10n_do_is_return_order"],
"l10n_do_is_return_order": ui_order["l10n_do_is_return_order"],
"l10n_do_return_order_id": ui_order["l10n_do_return_order_id"],
# "l10n_do_ncf_expiration_date": ui_order[
# "l10n_do_ncf_expiration_date"
Expand Down Expand Up @@ -194,7 +192,7 @@ def _prepare_invoice_vals(self):
# invoice_vals["is_l10n_do_internal_sequence"] = True

if self.l10n_do_is_return_order:
invoice_vals["type"] = "out_refund"
invoice_vals["move_type"] = "out_refund"

return invoice_vals

Expand Down Expand Up @@ -225,7 +223,7 @@ def _process_order(self, order, draft, existing_order):

@api.model
def order_search_from_ui(self, day_limit=0, config_id=0, session_id=0):
invoice_domain = [("type", "=", "out_invoice")]
invoice_domain = [("move_type", "=", "out_invoice")]
pos_order_domain = []

if day_limit:
Expand Down
66 changes: 66 additions & 0 deletions l10n_do_pos/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from odoo import models, fields, api, _
import requests
import xml.etree.ElementTree as ET
import json


class Partner(models.Model):
_inherit = 'res.partner'

@api.model
def get_contribuyentes(self, vat):
rnc = vat # Use the passed parameter directly
if not rnc or len(rnc) not in (9, 11):
warning_message = _(
'No es una secuencia valida de Cedula o RNC, puede continuar si no estas validando este dato de lo '
'contrario verificar %s') % rnc
return {'warning': {'title': _('Warning'), 'message': warning_message}}

# SOAP Request Setup
soap_request = f"""<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetContribuyentes xmlns="http://dgii.gov.do/">
<value>{rnc}</value>
<patronBusqueda>0</patronBusqueda>
<inicioFilas>0</inicioFilas>
<filaFilas>1</filaFilas>
<IMEI></IMEI>
</GetContribuyentes>
</soap12:Body>
</soap12:Envelope>"""
url = "https://dgii.gov.do/wsMovilDGII/WSMovilDGII.asmx?WSDL"
headers = {"Content-Type": "application/soap+xml; charset=utf-8"}

# Sending the SOAP request
response = requests.post(url, data=soap_request, headers=headers)
if response.status_code == 200:
try:
root = ET.fromstring(response.content)
result_element = root.find('.//{http://dgii.gov.do/}GetContribuyentesResult')
if result_element is None:
raise ValueError("The desired element was not found in the XML response")

data_dict = json.loads(result_element.text)
estatus = data_dict.get('ESTATUS')
if estatus == "0":
return self._generate_warning_message(rnc, data_dict, 'SUSPENDIDO')
elif estatus == "2":
return {'name': data_dict['RGE_NOMBRE']}
elif estatus == "3":
return self._generate_warning_message(rnc, data_dict, 'DADO DE BAJA')

except Exception as e:
error_message = _('Error processing response from DGII: %s') % str(e)
return {'warning': {'title': _('Error'), 'message': error_message}}
else:
error_message = _('Request failed with status code: %s') % response.status_code
return {'warning': {'title': _('Error'), 'message': error_message}}

def _generate_warning_message(self, rnc, data_dict, status):
warning_message = _(
'Este contribuyente se encuentra inactivo. \n\nCédula/RNC: %s\nNombre/Razón Social: %s\nEstado: %s') % (
rnc, data_dict["RGE_NOMBRE"], status)

Check failure on line 65 in l10n_do_pos/models/res_partner.py

View workflow job for this annotation

GitHub Actions / flake8

continuation line over-indented for hanging indent
return {'warning': {'title': _('Warning'), 'message': warning_message}}
Loading

0 comments on commit 421b5eb

Please sign in to comment.