parent
a205f30d4f
commit
8d4a86fc7c
86
aeat.py
86
aeat.py
|
@ -20,8 +20,10 @@ from trytond.transaction import Transaction
|
|||
from trytond.config import config
|
||||
from trytond.i18n import gettext
|
||||
from trytond.exceptions import UserError
|
||||
from trytond.tools import reduce_ids
|
||||
from . import tools
|
||||
|
||||
from sql import With
|
||||
from sql.aggregate import Max
|
||||
|
||||
__all__ = [
|
||||
'SIIReport',
|
||||
|
@ -314,7 +316,7 @@ class SIIReport(Workflow, ModelSQL, ModelView):
|
|||
('sending', 'Sending'),
|
||||
('cancelled', 'Cancelled'),
|
||||
('sent', 'Sent'),
|
||||
], 'State', readonly=True)
|
||||
], 'State', select=True, readonly=True)
|
||||
communication_state = fields.Selection(AEAT_COMMUNICATION_STATE,
|
||||
'Communication State', readonly=True)
|
||||
csv = fields.Char('CSV', readonly=True)
|
||||
|
@ -951,8 +953,8 @@ class SIIReportLine(ModelSQL, ModelView):
|
|||
__name__ = 'aeat.sii.report.lines'
|
||||
|
||||
report = fields.Many2One(
|
||||
'aeat.sii.report', 'Issued Report', ondelete='CASCADE')
|
||||
invoice = fields.Many2One('account.invoice', 'Invoice',
|
||||
'aeat.sii.report', 'Issued Report', select=True, ondelete='CASCADE')
|
||||
invoice = fields.Many2One('account.invoice', 'Invoice', select=True,
|
||||
states={
|
||||
'required': Eval('_parent_report', {}).get(
|
||||
'operation_type') != 'C0',
|
||||
|
@ -1100,28 +1102,58 @@ class SIIReportLine(ModelSQL, ModelView):
|
|||
def delete(cls, lines):
|
||||
pool = Pool()
|
||||
Invoice = pool.get('account.invoice')
|
||||
Report = pool.get('aeat.sii.report')
|
||||
sql_table = cls.__table__()
|
||||
sql_table2 = cls.__table__()
|
||||
report = Report.__table__()
|
||||
report2 = Report.__table__()
|
||||
cursor = Transaction().connection.cursor()
|
||||
|
||||
to_save = []
|
||||
for line in lines:
|
||||
invoice = line.invoice
|
||||
if invoice:
|
||||
last_line = cls.search([
|
||||
('invoice', '=', invoice),
|
||||
('id', '!=', line.id),
|
||||
('report.operation_type', '!=', 'C0'),
|
||||
], order=[('report', 'DESC')], limit=1)
|
||||
last_line = last_line[0] if last_line else None
|
||||
if last_line:
|
||||
invoice.sii_communication_type = (
|
||||
last_line.report.operation_type)
|
||||
invoice.sii_state = last_line.state
|
||||
to_save.append(invoice)
|
||||
else:
|
||||
invoice.sii_communication_type = None
|
||||
invoice.sii_state = None
|
||||
to_save.append(invoice)
|
||||
if to_save:
|
||||
Invoice.save(to_save)
|
||||
changes = {}
|
||||
invoices = [l.invoice for l in lines if l.invoice]
|
||||
invoiceid2record = {i.id: i for i in invoices}
|
||||
if invoices:
|
||||
subquery = With()
|
||||
# get last report line for each invoice
|
||||
subquery.query = sql_table.join(report, condition=(
|
||||
report.id == sql_table.report)
|
||||
).select(
|
||||
Max(sql_table.report).as_('report'),
|
||||
Max(sql_table.id).as_('line'),
|
||||
sql_table.invoice,
|
||||
where=(
|
||||
reduce_ids(sql_table.invoice, list(map(int, invoices)))
|
||||
& ~sql_table.id.in_(list(map(int, lines)))
|
||||
& (report.operation_type != 'C0')),
|
||||
group_by=(sql_table.invoice)
|
||||
)
|
||||
cursor.execute(*sql_table2.join(subquery, condition=(
|
||||
subquery.line == sql_table2.id)
|
||||
).join(report2, condition=(report2.id == sql_table2.report)
|
||||
).select(
|
||||
sql_table2.invoice,
|
||||
sql_table2.state,
|
||||
report2.operation_type,
|
||||
with_=[subquery]))
|
||||
|
||||
def compose_key(state, type):
|
||||
return (
|
||||
('sii_state', state),
|
||||
('sii_communication_type', type),
|
||||
)
|
||||
|
||||
for invoice_id, sii_state, operation_type in cursor.fetchall():
|
||||
invoice = invoiceid2record.pop(invoice_id)
|
||||
key = compose_key(sii_state, operation_type)
|
||||
changes.setdefault(key, []).append(invoice)
|
||||
if invoiceid2record:
|
||||
changes[compose_key(None, None)] = list(
|
||||
invoiceid2record.values())
|
||||
if changes:
|
||||
new_changes = []
|
||||
for values, items in changes.items():
|
||||
new_changes.extend([items, dict(values)])
|
||||
Invoice.write(*new_changes)
|
||||
super(SIIReportLine, cls).delete(lines)
|
||||
|
||||
|
||||
|
@ -1132,8 +1164,8 @@ class SIIReportLineTax(ModelSQL, ModelView):
|
|||
__name__ = 'aeat.sii.report.line.tax'
|
||||
|
||||
line = fields.Many2One(
|
||||
'aeat.sii.report.lines', 'Report Line', ondelete='CASCADE')
|
||||
|
||||
'aeat.sii.report.lines', 'Report Line', select=True,
|
||||
ondelete='CASCADE')
|
||||
base = fields.Numeric('Base', readonly=True)
|
||||
rate = fields.Numeric('Rate', readonly=True)
|
||||
amount = fields.Numeric('Amount', readonly=True)
|
||||
|
|
Loading…
Reference in New Issue