Skip to content

Commit

Permalink
[fix] - Actualizo vistas de wizard y partner para mejorar la UX
Browse files Browse the repository at this point in the history
      - Agrego el campo last_update_census para guardar la fecha de actualizacion
      - creo la funcion parce_census_vals para permitir extender datos del padron de otros modulos
      - Elimino campos que aun no estan el la localizacion (por ejempo los del los padrones)
      - ahora es la interseccion de dos arrys evitando problemas con posible campos inexistentes en el modelo partner fields_names = list(set(partner_vals) & set(fields_names))
  • Loading branch information
filoquin authored and jjscarafia committed Sep 1, 2021
1 parent 1f6f4c1 commit a1b72f7
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 156 deletions.
131 changes: 64 additions & 67 deletions l10n_ar_afipws/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,108 +16,64 @@
class ResPartner(models.Model):
_inherit = 'res.partner'

def cuit_required(self):
self.ensure_one()
if not self.vat:
raise UserError(_('No CUIT configured for partner [%i] %s') % (
self.id, self.name))
return self.vat
last_update_census = fields.Date(
string='Last update census'
)

def get_data_from_padron_afip(self):
self.ensure_one()
cuit = self.cuit_required()

# GET COMPANY
# if there is certificate for user company, use that one, if not
# use the company for the first certificate found
company = self.env.user.company_id
env_type = company._get_environment_type()
try:
certificate = company.get_key_and_certificate(
company._get_environment_type())
except Exception:
certificate = self.env['afipws.certificate'].search([
('alias_id.type', '=', env_type),
('state', '=', 'confirmed'),
], limit=1)
if not certificate:
raise UserError(_(
'Not confirmed certificate found on database'))
company = certificate.alias_id.company_id

# consultamos a5 ya que extiende a4 y tiene validez de constancia
padron = company.get_connection('ws_sr_padron_a5').connect()
error_msg = _(
'No pudimos actualizar desde padron afip al partner %s (%s).\n'
'Recomendamos verificar manualmente en la página de AFIP.\n'
'Obtuvimos este error: %s')
try:
padron.Consultar(cuit)
except SoapFault as e:
raise UserError(error_msg % (self.name, cuit, e.faultstring))
except Exception as e:
raise UserError(error_msg % (self.name, cuit, e))

if not padron.denominacion or padron.denominacion == ', ':
raise UserError(error_msg % (
self.name, cuit, 'La afip no devolvió nombre'))
# Separo esto para poder heredar de otros
# modulos y extender los datos
def parce_census_vals(self, census):

# porque imp_iva activo puede ser S o AC
imp_iva = padron.imp_iva
imp_iva = census.imp_iva
if imp_iva == 'S':
imp_iva = 'AC'
elif imp_iva == 'N':
# por ej. monotributista devuelve N
imp_iva = 'NI'

vals = {
'name': padron.denominacion,
'estado_padron': padron.estado,
'street': padron.direccion,
'city': padron.localidad,
'zip': padron.cod_postal,
'actividades_padron': self.actividades_padron.search(
[('code', 'in', padron.actividades)]).ids,
'impuestos_padron': self.impuestos_padron.search(
[('code', 'in', padron.impuestos)]).ids,
'name': census.denominacion,
'street': census.direccion,
'city': census.localidad,
'zip': census.cod_postal,
'imp_iva_padron': imp_iva,
'monotributo_padron': padron.monotributo,
'actividad_monotributo_padron': padron.actividad_monotributo,
'empleador_padron': padron.empleador == 'S' and True,
'integrante_soc_padron': padron.integrante_soc,
'last_update_padron': fields.Date.today(),
'last_update_census': fields.Date.today(),
}

# padron.idProvincia

ganancias_inscripto = [10, 11]
ganancias_exento = [12]
if set(ganancias_inscripto) & set(padron.impuestos):
if set(ganancias_inscripto) & set(census.impuestos):
vals['imp_ganancias_padron'] = 'AC'
elif set(ganancias_exento) & set(padron.impuestos):
elif set(ganancias_exento) & set(census.impuestos):
vals['imp_ganancias_padron'] = 'EX'
elif padron.monotributo == 'S':
elif census.monotributo == 'S':
vals['imp_ganancias_padron'] = 'NC'
else:
_logger.info(
"We couldn't get impuesto a las ganancias from padron, you"
"must set it manually")

if padron.provincia:
if census.provincia:
# depending on the database, caba can have one of this codes
caba_codes = ['C', 'CABA', 'ABA']
# if not localidad then it should be CABA.
if not padron.localidad:
if not census.localidad:
state = self.env['res.country.state'].search([
('code', 'in', caba_codes),
('country_id.code', '=', 'AR')], limit=1)
# If localidad cant be caba
else:
state = self.env['res.country.state'].search([
('name', 'ilike', padron.provincia),
('name', 'ilike', census.provincia),
('code', 'not in', caba_codes),
('country_id.code', '=', 'AR')], limit=1)
if state:
vals['state_id'] = state.id

if imp_iva == 'NI' and padron.monotributo == 'S':
if imp_iva == 'NI' and census.monotributo == 'S':
vals['l10n_ar_afip_responsibility_type_id'] = self.env.ref(
'l10n_ar.res_RM').id
elif imp_iva == 'AC':
Expand All @@ -131,4 +87,45 @@ def get_data_from_padron_afip(self):
"We couldn't infer the AFIP responsability from padron, you"
"must set it manually.")

return vals
return vals

def get_data_from_padron_afip(self):
self.ensure_one()
cuit = self.ensure_vat()

# GET COMPANY
# if there is certificate for user company, use that one, if not
# use the company for the first certificate found
company = self.env.user.company_id
env_type = company._get_environment_type()
try:
certificate = company.get_key_and_certificate(
company._get_environment_type())
except Exception:
certificate = self.env['afipws.certificate'].search([
('alias_id.type', '=', env_type),
('state', '=', 'confirmed'),
], limit=1)
if not certificate:
raise UserError(_(
'Not confirmed certificate found on database'))
company = certificate.alias_id.company_id

# consultamos a5 ya que extiende a4 y tiene validez de constancia
padron = company.get_connection('ws_sr_padron_a5').connect()
error_msg = _(
'No pudimos actualizar desde padron afip al partner %s (%s).\n'
'Recomendamos verificar manualmente en la página de AFIP.\n'
'Obtuvimos este error: %s')
try:
padron.Consultar(cuit)
except SoapFault as e:
raise UserError(error_msg % (self.name, cuit, e.faultstring))
except Exception as e:
raise UserError(error_msg % (self.name, cuit, e))

if not padron.denominacion or padron.denominacion == ', ':
raise UserError(error_msg % (
self.name, cuit, 'La afip no devolvió nombre'))
vals = self.parce_census_vals(padron)
return vals
32 changes: 10 additions & 22 deletions l10n_ar_afipws/wizard/res_partner_update_from_padron_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ class ResPartnerUpdateFromPadronField(models.TransientModel):
'Wizard',
)
field = fields.Char(
'name'
)
old_value = fields.Char(
'old Value'
)
new_value = fields.Char(
'new Value'
)


Expand Down Expand Up @@ -59,21 +62,11 @@ def default_get(self, fields):
def _get_domain(self):
fields_names = [
'name',
'estado_padron',
'street',
'city',
'zip',
'actividades_padron',
'impuestos_padron',
'imp_iva_padron',
'state_id',
'imp_ganancias_padron',
'monotributo_padron',
'actividad_monotributo_padron',
'empleador_padron',
'integrante_soc_padron',
'last_update_padron',
'afip_responsability_type_id',
'l10n_ar_afip_responsibility_type_id',
'last_update_census'
]
return [
('model', '=', 'res.partner'),
Expand Down Expand Up @@ -137,7 +130,6 @@ def get_fields(self):
required=True,
)


@api.onchange('partner_id')
def change_partner(self):
self.ensure_one()
Expand All @@ -147,15 +139,17 @@ def change_partner(self):
if partner:
partner_vals = partner.get_data_from_padron_afip()
lines = []
for key, new_value in partner_vals.items():
fields_names = list(set(partner_vals) & set(fields_names))
for key in fields_names:
old_value = partner[key]
new_value = partner_vals[key]
if new_value == '':
new_value = False
if self.title_case and key in ('name', 'city', 'street'):
new_value = new_value and new_value.title()
if key in ('impuestos_padron', 'actividades_padron'):
old_value = old_value.ids
elif key in ('state_id', 'afip_responsability_type_id'):
elif key in ('state_id', 'l10n_ar_afip_responsibility_type_id'):
old_value = old_value.id
if new_value and key in fields_names and \
old_value != new_value:
Expand All @@ -168,7 +162,6 @@ def change_partner(self):
lines.append((0, False, line_vals))
self.field_ids = lines


def _update(self):
self.ensure_one()
vals = {}
Expand All @@ -179,7 +172,6 @@ def _update(self):
vals[field.field] = field.new_value
self.partner_id.write(vals)


def automatic_process_cb(self):
for partner in self.partner_ids:
self.partner_id = partner.id
Expand All @@ -195,7 +187,6 @@ def automatic_process_cb(self):
'target': 'new',
}


def update_selection(self):
self.ensure_one()
if not self.field_ids:
Expand All @@ -210,7 +201,6 @@ def update_selection(self):
self._update()
return self.next_cb()


def next_cb(self):
"""
"""
Expand All @@ -219,7 +209,6 @@ def next_cb(self):
self.write({'partner_ids': [(3, self.partner_id.id, False)]})
return self._next_screen()


def _next_screen(self):
self.ensure_one()
self.refresh()
Expand Down Expand Up @@ -248,10 +237,9 @@ def _next_screen(self):
'target': 'new',
}


def start_process_cb(self):
"""
Start the process.
"""
self.ensure_one()
return self._next_screen()
return self._next_screen()
Loading

0 comments on commit a1b72f7

Please sign in to comment.