-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathaccount_move.py
359 lines (323 loc) · 14.6 KB
/
account_move.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
##############################################################################
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################
from odoo import fields, models, api, _
from odoo.exceptions import UserError
from odoo.tools import float_repr
from odoo.addons.l10n_ar_afipws_fe.afip_utils import get_invoice_number_from_response
import base64
base64.encodestring = base64.encodebytes
import json
import logging
import sys
import traceback
from datetime import datetime
_logger = logging.getLogger(__name__)
class AccountMove(models.Model):
_inherit = "account.move"
afip_auth_mode = fields.Selection(
[("CAE", "CAE"), ("CAI", "CAI"), ("CAEA", "CAEA")],
string="AFIP authorization mode",
copy=False,
readonly=True,
states={"draft": [("readonly", False)]},
)
afip_auth_code = fields.Char(
copy=False,
string="CAE/CAI/CAEA Code",
readonly=True,
size=24,
states={"draft": [("readonly", False)]},
)
afip_auth_code_due = fields.Date(
copy=False,
readonly=True,
string="CAE/CAI/CAEA due Date",
states={"draft": [("readonly", False)]},
)
afip_associated_period_from = fields.Date(
'AFIP Period from'
)
afip_associated_period_to = fields.Date(
'AFIP Period to'
)
afip_qr_code = fields.Char(compute="_compute_qr_code", string="AFIP QR code")
afip_message = fields.Text(
string="AFIP Message",
copy=False,
)
afip_xml_request = fields.Text(
string="AFIP XML Request",
copy=False,
)
afip_xml_response = fields.Text(
string="AFIP XML Response",
copy=False,
)
afip_result = fields.Selection(
[("", "n/a"), ("A", "Aceptado"), ("R", "Rechazado"), ("O", "Observado")],
"Resultado",
readonly=True,
states={"draft": [("readonly", False)]},
copy=False,
help="AFIP request result",
)
validation_type = fields.Char(
"Validation Type",
compute="_compute_validation_type",
)
afip_fce_es_anulacion = fields.Boolean(
string="FCE: Es anulacion?",
help="Solo utilizado en comprobantes MiPyMEs (FCE) del tipo débito o crédito. Debe informar:\n"
"- SI: sí el comprobante asociado (original) se encuentra rechazado por el comprador\n"
"- NO: sí el comprobante asociado (original) NO se encuentra rechazado por el comprador",
)
asynchronous_post = fields.Boolean()
l10n_ar_payment_foreign_currency_default = fields.Selection(related="company_id.l10n_ar_payment_foreign_currency")
l10n_ar_payment_foreign_currency = fields.Selection(
[("S", "Yes"), ("N", "No")],
compute="compute_l10n_ar_payment_foreign_currency",
store=True,
)
l10n_ar_currency_code = fields.Char("Currency Code", related="currency_id.name")
@api.onchange("currency_id", "line_ids")
@api.depends("currency_id")
def compute_l10n_ar_payment_foreign_currency(self):
self.l10n_ar_payment_foreign_currency = False
for move in self:
default_value = move.l10n_ar_payment_foreign_currency_default
if default_value == "account":
account = move.line_ids.account_id.filtered(lambda x: x.account_type == "asset_receivable")
default_value = "S" if account.currency_id and account.currency_id != move.company_currency_id else "N"
move.l10n_ar_payment_foreign_currency = default_value
@api.depends('journal_id', 'l10n_latam_document_type_id')
def _compute_highest_name(self):
manual_records = self.filtered(lambda move: move.journal_id.afip_ws in ['wsfe', 'wsfex', 'wsbfe'])
manual_records.highest_name = ''
super(AccountMove, self - manual_records)._compute_highest_name()
def cron_asynchronous_post(self):
queue_limit = self.env['ir.config_parameter'].sudo().get_param('l10n_ar_afipws_fe.queue_limit', 20)
queue = self.search([
('asynchronous_post', '=', True),'|',
('afip_result', '=', False),
('afip_result', '=', ''),
], limit=queue_limit)
if queue:
queue._post()
def _get_starting_sequence(self):
""" If use documents then will create a new starting sequence using the document type code prefix and the
journal document number with a 8 padding number """
if self.journal_id.l10n_latam_use_documents and self.company_id.account_fiscal_country_id.code == "AR" and self.journal_id.afip_ws:
if self.l10n_latam_document_type_id :
number = int(
self.journal_id.get_pyafipws_last_invoice(
self.l10n_latam_document_type_id
)
)
return self._get_formatted_sequence(number)
return super()._get_starting_sequence()
def _set_next_sequence(self):
self.ensure_one()
if self.afip_auth_code and self.journal_id.afip_ws and self.afip_xml_response:
invoice_number = get_invoice_number_from_response(self.afip_xml_response, self.journal_id.afip_ws)
if invoice_number:
last_sequence = self._get_formatted_sequence(invoice_number)
format, format_values = self._get_sequence_format_param(last_sequence)
format_values['year'] = self[self._sequence_date_field].year % (10 ** format_values['year_length'])
format_values['month'] = self[self._sequence_date_field].month
format_values['seq'] = invoice_number
self[self._sequence_field] = format.format(**format_values)
return
super()._set_next_sequence()
# TODO Esto se deprecaria si la secuencia solo viene de result de afip
# def _get_last_sequence(self, relaxed=False, with_prefix=None, lock=True):
# if self._name == 'account.move' and \
# self.journal_id.l10n_latam_use_documents and \
# self.company_id.account_fiscal_country_id.code == "AR" and \
# not self.afip_auth_code and \
# self.journal_id.afip_ws and self.l10n_latam_document_type_id:
# number = int(
# self.journal_id.get_pyafipws_last_invoice(
# self.l10n_latam_document_type_id
# )
# )
# res = self._get_formatted_sequence(number)
# else:
# res = super()._get_last_sequence(relaxed=relaxed, with_prefix=with_prefix, lock=lock)
# return res
@api.depends("journal_id", "afip_auth_code")
def _compute_validation_type(self):
for rec in self:
if rec.journal_id.afip_ws and not rec.afip_auth_code:
validation_type = self.env["res.company"]._get_environment_type()
# if we are on homologation env and we dont have certificates
# we validate only locally
if validation_type == "homologation":
try:
rec.company_id.get_key_and_certificate(validation_type)
except Exception:
validation_type = False
rec.validation_type = validation_type
else:
rec.validation_type = False
@api.depends("afip_auth_code")
def _compute_qr_code(self):
for rec in self:
if rec.afip_auth_mode in ["CAE", "CAEA"] and rec.afip_auth_code:
number_parts = self._l10n_ar_get_document_number_parts(
rec.l10n_latam_document_number, rec.l10n_latam_document_type_id.code
)
qr_dict = {
"ver": 1,
"fecha": str(rec.invoice_date),
"cuit": int(rec.company_id.partner_id.l10n_ar_vat),
"ptoVta": number_parts["point_of_sale"],
"tipoCmp": int(rec.l10n_latam_document_type_id.code),
"nroCmp": number_parts["invoice_number"],
"importe": float(float_repr(rec.amount_total, 2)),
"moneda": rec.currency_id.l10n_ar_afip_code,
"ctz": float(float_repr(rec.l10n_ar_currency_rate, 2)),
"tipoCodAut": "E" if rec.afip_auth_mode == "CAE" else "A",
"codAut": int(rec.afip_auth_code),
}
if (
len(rec.commercial_partner_id.l10n_latam_identification_type_id)
and rec.commercial_partner_id.vat
):
qr_dict["tipoDocRec"] = int(
rec.commercial_partner_id.l10n_latam_identification_type_id.l10n_ar_afip_code
)
qr_dict["nroDocRec"] = int(
rec.commercial_partner_id.vat.replace("-", "").replace(".", "")
)
qr_data = base64.encodestring(
json.dumps(qr_dict, indent=None).encode("ascii")
).decode("ascii")
qr_data = str(qr_data).replace("\n", "")
rec.afip_qr_code = "https://www.afip.gob.ar/fe/qr/?p=%s" % qr_data
else:
rec.afip_qr_code = False
def get_related_invoices_data(self):
"""
List related invoice information to fill CbtesAsoc.
"""
self.ensure_one()
if self.l10n_latam_document_type_id.internal_type == "credit_note":
return self.reversed_entry_id
elif self.l10n_latam_document_type_id.internal_type == "debit_note":
return self.debit_origin_id
else:
return self.browse()
def _post(self, soft=True):
request_cae_invoices = self.filtered(
lambda x: x.company_id.country_id.code == "AR"
and x.is_invoice()
and x.move_type in ["out_invoice", "out_refund"]
and x.journal_id.afip_ws
and not x.afip_auth_code
)
a_invoices , r_invoices = request_cae_invoices.do_pyafipws_request_cae()
if len(self) == 1 and r_invoices:
raise (UserError(r_invoices.afip_message))
return super(AccountMove, self - r_invoices)._post(soft=soft)
def do_pyafipws_request_cae(self):
"Request to AFIP the invoices' Authorization Electronic Code (CAE)"
a_invoices = r_invoices = self.env['account.move']
for inv in self:
afip_ws = inv.journal_id.afip_ws
if not afip_ws:
continue
# if no validation type and we are on electronic invoice, it means
# that we are on a testing database without homologation
# certificates
if not inv.validation_type:
msg = (
"Factura validada solo localmente por estar en ambiente "
"de homologación sin claves de homologación"
)
inv.sudo().write(
{
"afip_auth_mode": "CAE",
"afip_auth_code": "68448767638166",
"afip_auth_code_due": inv.invoice_date,
"afip_result": "",
"afip_message": msg,
}
)
inv.message_post(body=msg)
a_invoices += inv
continue
# Inicio conexion
ws = inv.company_id.get_connection(afip_ws).connect()
# Preparo los datos
invoice_info = inv.map_invoice_info(afip_ws)
# Esto no es necesario ahora ya que el numero se obtiene desde el result
# document_number = inv._get_formatted_sequence(int(invoice_info["ws_next_invoice_number"]))
# doc_code_prefix = inv.l10n_latam_document_type_id.doc_code_prefix
# if doc_code_prefix and document_number:
# document_number = document_number.split(" ", 1)[-1]
# inv.l10n_latam_document_number = document_number
# Creo la factura en el ambito de pyafipws
inv.pyafipws_create_invoice(ws, invoice_info)
# Agrego informacion a la factura dentro de pyafipws
inv.pyafipws_add_info(ws, afip_ws, invoice_info)
# Request the authorization! (call the AFIP webservice method)
vto = None
msg = False
try:
# Pido autorizacion
inv.pyafipws_request_autorization(ws, afip_ws)
except Exception as e:
msg = e
except Exception:
if ws.Excepcion:
# get the exception already parsed by the helper
msg = ws.Excepcion
else:
# avoid encoding problem when raising error
msg = traceback.format_exception_only(sys.exc_type, sys.exc_value)[
0
]
if msg:
_logger.error(
_("AFIP Validation Error. %s" % msg)
+ " XML Request: %s XML Response: %s"
% (ws.XmlRequest, ws.XmlResponse)
)
msg = "\n".join([ws.Obs or "", ws.ErrMsg or ""])
if not ws.CAE or ws.Resultado != "A":
r_invoices += inv
vals = {
"name":'/',
"afip_result": 'R',
"afip_message": msg,
"afip_xml_request": ws.XmlRequest or '',
"afip_xml_response": ws.XmlResponse or '',
}
inv.sudo().write(vals)
inv._cr.commit()
continue
if hasattr(ws, "Vencimiento"):
vto = datetime.strptime(ws.Vencimiento, "%Y%m%d").date()
if hasattr(ws, "FchVencCAE"):
vto = datetime.strptime(ws.FchVencCAE, "%Y%m%d").date()
_logger.info(
"CAE solicitado con exito. CAE: %s. Resultado %s"
% (ws.CAE, ws.Resultado)
)
vals = {
"afip_auth_mode": "CAE",
"afip_auth_code": ws.CAE,
"afip_auth_code_due": vto,
"afip_result": ws.Resultado,
"afip_message": msg,
"afip_xml_request": ws.XmlRequest,
"afip_xml_response": ws.XmlResponse,
}
inv.sudo().write(vals)
inv._cr.commit()
# si obtuvimos el cae hacemos el commit porque estoya no se puede
# volver atras
a_invoices += inv
return (a_invoices, r_invoices)