subscription.py 1.71 KB
Newer Older
Luca Cristaldi's avatar
Luca Cristaldi committed
1
from trytond.model import ModelView, fields, Workflow
2
from trytond.pool import PoolMeta,Pool
3 4 5 6 7 8 9 10 11 12
from trytond.pyson import Eval

__all__ = ['Subscription']

class Subscription(metaclass=PoolMeta):
    "Subscription"
    __name__ = 'sale.subscription'

    include = fields.Boolean("Include", states={'readonly': Eval('state') != 'draft'})

Luca Cristaldi's avatar
Luca Cristaldi committed
13 14 15 16 17 18 19
#    def compute_next_invoice_date(self):
#        date = super(Subscription, self).compute_next_invoice_date()
#        if(self.include and not self.next_invoice_date):
#            return self.invoice_start_date or self.start_date
#        else:
#            return date

20 21 22
    def default_include():
        return False

Luca Cristaldi's avatar
Luca Cristaldi committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    @classmethod
    @ModelView.button
    @Workflow.transition('running')
    def run(cls, subscriptions):
        pool = Pool()
        Line = pool.get('sale.subscription.line')
        lines = []
        for subscription in subscriptions:
            if not subscription.next_invoice_date:
                if subscription.include:
                    subscription.next_invoice_date = (subscription.invoice_start_date or subscription.start_date)
                else:
                    subscription.next_invoice_date = (subscription.compute_next_invoice_date())
            for line in subscription.lines:
                if (line.next_consumption_date is None
                        and not line.consumed):
                    if subscription.include:
40
                        line.next_consumption_date = (subscription.invoice_start_date or subscription.start_date)
Luca Cristaldi's avatar
Luca Cristaldi committed
41 42 43 44
                    else:
                        line.next_consumption_date = (line.compute_next_consumption_date())
            lines.extend(subscription.lines)
        Line.save(lines)
45
        cls.save(subscriptions)