-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommission.py
59 lines (49 loc) · 2.05 KB
/
commission.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
# This file is part account_invoice_posted2draft module for Tryton.
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
from trytond.pool import Pool, PoolMeta
from trytond.tools import grouped_slice
from trytond.transaction import Transaction
from sql.operators import Concat
class Invoice(metaclass=PoolMeta):
__name__ = 'account.invoice'
@classmethod
def get_allow_draft(cls, invoices, name):
pool = Pool()
Commission = pool.get('commission')
Line = pool.get('account.invoice.line')
res = super().get_allow_draft(invoices, name)
line = Line.__table__()
commission = Commission.__table__()
invoice_ids = [i.id for i in invoices]
query = line.join(commission,
condition=commission.origin == Concat('account.invoice.line,', line.id)
).select(line.invoice,
where=(line.invoice.in_(invoice_ids)
& (commission.invoice_line != None)))
cursor = Transaction().connection.cursor()
cursor.execute(*query)
for record in cursor.fetchall():
res[record[0]] = False
return res
@classmethod
def draft(cls, invoices):
pool = Pool()
Commission = pool.get('commission')
to_delete = []
for sub_invoices in grouped_slice(invoices):
ids = [i.id for i in sub_invoices]
to_delete = Commission.search([
('origin.invoice', 'in', ids, 'account.invoice.line'),
('invoice_line', '=', None),
])
if to_delete:
to_delete_origin = Commission.search([
('origin.id', 'in',
[x.id for x in to_delete], 'commission'),
('invoice_line', '=', None),
])
if to_delete_origin:
to_delete += to_delete_origin
Commission.delete(to_delete)
return super(Invoice, cls).draft(invoices)