Changes to improve performance.
This commit is contained in:
parent
903f7f5885
commit
9321d57967
81
aeat.py
81
aeat.py
|
@ -462,9 +462,6 @@ class SIIReport(Workflow, ModelSQL, ModelView):
|
|||
@ModelView.button
|
||||
@Workflow.transition('sent')
|
||||
def send(cls, reports):
|
||||
pool = Pool()
|
||||
Invoice = pool.get('account.invoice')
|
||||
|
||||
for report in reports:
|
||||
if report.state != 'confirmed':
|
||||
continue
|
||||
|
@ -489,19 +486,6 @@ class SIIReport(Workflow, ModelSQL, ModelView):
|
|||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
to_save = []
|
||||
for report in reports:
|
||||
if report.operation_type == 'C0':
|
||||
continue
|
||||
for line in report.lines:
|
||||
invoice = line.invoice
|
||||
if invoice:
|
||||
invoice.sii_communication_type = report.operation_type
|
||||
invoice.sii_state = line.state
|
||||
|
||||
to_save.append(invoice)
|
||||
|
||||
Invoice.save(to_save)
|
||||
cls.write(reports, {
|
||||
'send_date': datetime.now(),
|
||||
})
|
||||
|
@ -513,7 +497,7 @@ class SIIReport(Workflow, ModelSQL, ModelView):
|
|||
def process_response(cls, reports):
|
||||
for report in reports:
|
||||
if report.response:
|
||||
cls._save_response(report.response)
|
||||
report._save_response(report.response)
|
||||
report.save()
|
||||
|
||||
@classmethod
|
||||
|
@ -802,9 +786,12 @@ class SIIReport(Workflow, ModelSQL, ModelView):
|
|||
self._save_response(self.response)
|
||||
|
||||
def _save_response(self, res):
|
||||
ReportLine = Pool().get('aeat.sii.report.lines')
|
||||
|
||||
if res:
|
||||
response = json.loads(res, object_hook=lambda d: namedtuple(
|
||||
'SII', d.keys())(*d.values()))
|
||||
lines2save = []
|
||||
for (report_line, response_line) in zip(
|
||||
self.lines, response.RespuestaLinea):
|
||||
if not report_line.communication_code:
|
||||
|
@ -812,7 +799,9 @@ class SIIReport(Workflow, ModelSQL, ModelView):
|
|||
report_line.communication_code = response_line.CodigoErrorRegistro
|
||||
report_line.communication_msg = (
|
||||
response_line.DescripcionErrorRegistro)
|
||||
report_line.save()
|
||||
lines2save.append(report_line)
|
||||
if lines2save:
|
||||
ReportLine.save(lines2save)
|
||||
if not self.communication_state:
|
||||
self.communication_state = response.EstadoEnvio
|
||||
if not self.csv:
|
||||
|
@ -1069,34 +1058,40 @@ class SIIReportLine(ModelSQL, ModelView):
|
|||
|
||||
actions = iter(args)
|
||||
|
||||
to_write = []
|
||||
to_check = []
|
||||
for lines, values in zip(actions, actions):
|
||||
invoice_values = {
|
||||
'sii_pending_sending': False,
|
||||
}
|
||||
if values.get('state', None) == 'Correcto':
|
||||
invoices = [x.invoice for x in lines]
|
||||
else:
|
||||
invoices = [x.invoice for x in lines
|
||||
if x.state == 'Correcto']
|
||||
if invoices:
|
||||
to_write.extend((invoices, invoice_values))
|
||||
|
||||
invoice_vals = {
|
||||
'sii_pending_sending': False,
|
||||
'sii_state': 'duplicated_unsubscribed',
|
||||
}
|
||||
if values.get('communication_code', None) in (3000, 3001):
|
||||
invoices = [x.invoice for x in lines]
|
||||
else:
|
||||
invoices = [x.invoice for x in lines
|
||||
if x.communication_code in (3000, 3001)]
|
||||
if invoices:
|
||||
to_write.extend((invoices, invoice_vals))
|
||||
if ('state' in values
|
||||
or 'communication_code' in values
|
||||
or 'invoice' in values):
|
||||
to_check.extend(lines)
|
||||
|
||||
super(SIIReportLine, cls).write(*args)
|
||||
if to_write:
|
||||
Invoice.write(*to_write)
|
||||
|
||||
if to_check:
|
||||
to_check = cls.browse(to_check)
|
||||
to_write = {}
|
||||
for line in to_check:
|
||||
invoice = line.invoice
|
||||
if not invoice:
|
||||
continue
|
||||
key_changes = {}
|
||||
if invoice.sii_state != line.state:
|
||||
key_changes['sii_state'] = line.state
|
||||
if line.report.operation_type != 'C0':
|
||||
key_changes['sii_communication_type'] = \
|
||||
line.report.operation_type
|
||||
if line.state in ('Correcto', 'Correcta'):
|
||||
key_changes['sii_pending_sending'] = False
|
||||
if line.communication_code in (3000, 3001):
|
||||
key_changes['sii_pending_sending'] = False
|
||||
key_changes['sii_state'] = 'duplicated_unsubscribed'
|
||||
if key_changes:
|
||||
to_write.setdefault(
|
||||
tuple((k, v) for k, v in key_changes.items()),
|
||||
[]).append(invoice)
|
||||
|
||||
for changes, invoices in to_write.items():
|
||||
Invoice.write(*[invoices, dict(changes)])
|
||||
|
||||
@classmethod
|
||||
def delete(cls, lines):
|
||||
|
|
|
@ -407,7 +407,7 @@ class Invoice(metaclass=PoolMeta):
|
|||
def reset_sii_keys(cls, invoices):
|
||||
to_write = []
|
||||
for invoice in invoices:
|
||||
if invoice.state != 'draft':
|
||||
if invoice.sii_state in ('Correcto', 'Correcta'):
|
||||
continue
|
||||
for field in _SII_INVOICE_KEYS:
|
||||
setattr(invoice, field, None)
|
||||
|
|
Loading…
Reference in New Issue