Skip to content

Commit

Permalink
Replace get_allow_draft to classmethod
Browse files Browse the repository at this point in the history
#167334
  • Loading branch information
raimonesteve committed Jul 10, 2024
1 parent 20fb71c commit 2e82562
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
29 changes: 20 additions & 9 deletions commission.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,35 @@
# 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'

def get_allow_draft(self, name):
@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))

result = super().get_allow_draft(name)
cursor = Transaction().connection.cursor()
cursor.execute(*query)
for record in cursor.fetchall():
res[record[0]] = False

invoiced = Commission.search([
('origin.invoice', '=', self.id, 'account.invoice.line'),
('invoice_line', '!=', None),
])
if invoiced:
result = False
return result
return res

@classmethod
def draft(cls, invoices):
Expand Down
45 changes: 25 additions & 20 deletions invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

class Invoice(metaclass=PoolMeta):
__name__ = 'account.invoice'

allow_draft = fields.Function(
fields.Boolean("Allow Draft Invoice"), 'get_allow_draft')

Expand All @@ -20,25 +19,31 @@ def __setup__(cls):
cls._buttons['draft']['invisible'] = ~Eval('allow_draft', False)
cls._buttons['draft']['depends'] += tuple(['allow_draft'])

def get_allow_draft(self, name):
# when IN invoice is validate from scratch, the move is in 'draft'
# state, so in this case could be draft in a "normal" way
if (self.state == 'validated' and self.move
and self.move.state != 'draft'):
return False
elif self.state == 'cancelled' and self.number is not None:
return False
elif self.state in {'paid', 'draft'}:
return False
elif self.state == 'posted':
lines_to_pay = [l for l in self.lines_to_pay
if not l.reconciliation]
# Invoice already paid or partial paid, should not be possible
# to change state to draft.
if (not lines_to_pay
or self.amount_to_pay != self.total_amount):
return False
return True
@classmethod
def get_allow_draft(cls, invoices, name):
res = dict((x.id, False) for x in invoices)

for invoice in invoices:
# when IN invoice is validate from scratch, the move is in 'draft'
# state, so in this case could be draft in a "normal" way
if (invoice.state == 'validated' and invoice.move
and invoice.move.state != 'draft'):
continue
elif invoice.state == 'cancelled' and invoice.number is not None:
continue
elif invoice.state in {'paid', 'draft'}:
continue
elif invoice.state == 'posted':
lines_to_pay = [l for l in invoice.lines_to_pay
if not l.reconciliation]
# Invoice already paid or partial paid, should not be possible
# to change state to draft.
if (not lines_to_pay
or invoice.amount_to_pay != invoice.total_amount):
continue
# in case not continue, set to True
res[invoice.id] = True
return res

@classmethod
def draft(cls, invoices):
Expand Down

0 comments on commit 2e82562

Please sign in to comment.