diff --git a/commission.py b/commission.py index 80a6a16..72218ef 100644 --- a/commission.py +++ b/commission.py @@ -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): diff --git a/invoice.py b/invoice.py index 321917d..d8b5764 100644 --- a/invoice.py +++ b/invoice.py @@ -9,7 +9,6 @@ class Invoice(metaclass=PoolMeta): __name__ = 'account.invoice' - allow_draft = fields.Function( fields.Boolean("Allow Draft Invoice"), 'get_allow_draft') @@ -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):