-
Notifications
You must be signed in to change notification settings - Fork 0
/
party.py
144 lines (122 loc) · 5.03 KB
/
party.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# This file is part of the account_arba module for Tryton.
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
import logging
from pyafipws.iibb import IIBB as WSIIBB
from calendar import monthrange
from decimal import Decimal
from trytond.model import ModelView
from trytond.pool import PoolMeta, Pool
from trytond.transaction import Transaction
from trytond.exceptions import UserError
from trytond.i18n import gettext
logger = logging.getLogger(__name__)
class Party(metaclass=PoolMeta):
__name__ = 'party.party'
@classmethod
def __setup__(cls):
super().__setup__()
cls._buttons.update({
'get_arba_data': {},
})
@classmethod
@ModelView.button
def get_arba_data(cls, parties):
cls.import_arba_census(parties)
@classmethod
def import_arba_census(cls, parties):
pool = Pool()
Date = pool.get('ir.date')
Company = pool.get('company.company')
PartyWithholdingIIBB = pool.get('party.retencion.iibb')
ws = cls.get_ws_arba()
if not ws:
return
company = Company(Transaction().context['company'])
arba_regimen_retencion = company.arba_regimen_retencion
arba_regimen_percepcion = company.arba_regimen_percepcion
if not arba_regimen_retencion and not arba_regimen_percepcion:
return
today = Date.today()
_, end_date = monthrange(today.year, today.month)
fecha_desde = today.strftime('%Y%m') + '01'
fecha_hasta = today.strftime('%Y%m') + str(end_date)
logger.info('fecha_desde: %s | fecha_hasta: %s' %
(fecha_desde, fecha_hasta))
for party in parties:
if not party.vat_number:
continue
data = cls.get_arba_party_data(ws, party, fecha_desde, fecha_hasta)
if data is None:
continue
clause = [('party', '=', party)]
if arba_regimen_retencion:
clause.append(('regimen_retencion', '=', arba_regimen_retencion))
if arba_regimen_percepcion:
clause.append(('regimen_percepcion', '=', arba_regimen_percepcion))
iibb_regimenes = PartyWithholdingIIBB.search(clause)
if iibb_regimenes:
arba_regimen = iibb_regimenes[0]
else:
arba_regimen = PartyWithholdingIIBB(
party=party,
regimen_retencion=arba_regimen_retencion,
regimen_percepcion=arba_regimen_percepcion,
)
iibb_rate_percepcion = data.AlicuotaPercepcion
iibb_rate_retencion = data.AlicuotaRetencion
logger.info('Party: %s | Percepción: %s | Retención: %s' %
(party.vat_number, iibb_rate_percepcion, iibb_rate_retencion))
if iibb_rate_percepcion != '':
arba_regimen.rate_percepcion = Decimal(
iibb_rate_percepcion.replace(',', '.'))
if iibb_rate_retencion != '':
arba_regimen.rate_retencion = Decimal(
iibb_rate_retencion.replace(',', '.'))
arba_regimen.save()
Transaction().commit()
@classmethod
def get_ws_arba(cls):
pool = Pool()
Company = pool.get('company.company')
if Transaction().context.get('company'):
company = Company(Transaction().context['company'])
else:
logger.error('The company is not defined')
raise UserError(gettext(
'party_ar.msg_company_not_defined'))
ws = WSIIBB()
ws.Usuario = company.party.vat_number
ws.Password = company.arba_password
if company.arba_mode_cert == 'homologacion':
URL = ('https://dfe.test.arba.gov.ar/DomicilioElectronico/'
'SeguridadCliente/dfeServicioConsulta.do')
elif company.arba_mode_cert == 'produccion':
URL = ('https://dfe.arba.gov.ar/DomicilioElectronico/'
'SeguridadCliente/dfeServicioConsulta.do')
else:
logger.error('Certification mode is not defined in company')
return None
ws.Conectar(URL, cacert=None)
return ws
@classmethod
def get_arba_party_data(cls, ws, party, fecha_desde, fecha_hasta):
ws.ConsultarContribuyentes(fecha_desde, fecha_hasta, party.vat_number)
if ws.Excepcion:
logger.error('Excepcion: %s\n%s' % (ws.Excepcion, ws.Traceback))
while ws.LeerContribuyente():
return ws
@classmethod
def import_cron_arba(cls):
logger.info('Import ARBA Census::Start')
parties = cls.search([('vat_number', '!=', None)])
cls.import_arba_census(parties)
logger.info('Import ARBA Census::End')
class Cron(metaclass=PoolMeta):
__name__ = 'ir.cron'
@classmethod
def __setup__(cls):
super().__setup__()
cls.method.selection.extend([
('party.party|import_cron_arba', 'Import ARBA Census'),
])