File: product.py

package info (click to toggle)
tryton-modules-product-price-list-dates 7.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 380 kB
  • sloc: python: 456; xml: 114; makefile: 11; sh: 3
file content (118 lines) | stat: -rw-r--r-- 3,498 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# This file is part of Tryton.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.

import datetime as dt

from trytond.config import config
from trytond.model import ModelView, fields
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval, If
from trytond.transaction import Transaction

_cache_days = config.getint(
    'product_price_list_dates', 'cache_days', default=2)


class PriceList(metaclass=PoolMeta):
    __name__ = 'product.price_list'

    @classmethod
    def __setup__(cls):
        super(PriceList, cls).__setup__()
        cls._buttons.update({
                'open_lines': {},
                })

    @classmethod
    @ModelView.button_action(
        'product_price_list_dates.act_price_list_line_form')
    def open_lines(cls, price_lists):
        pass

    def compute(self, product, quantity, uom, pattern=None):
        context = Transaction().context
        pattern = pattern.copy() if pattern is not None else {}
        pattern.setdefault('date', context.get('date'))
        return super().compute(product, quantity, uom, pattern=pattern)


class PriceListCache(metaclass=PoolMeta):
    __name__ = 'product.price_list.cache'

    @classmethod
    def patterns(cls, price_list, product):
        pool = Pool()
        Date = pool.get('ir.date')
        today = Date.today()
        for pattern in super().patterns(price_list, product):
            if pattern is None:
                pattern = {}
            for days in range(_cache_days):
                pattern['date'] = today + dt.timedelta(days=days)
                yield pattern

    @classmethod
    def get(cls, price_list, product, pattern=None):
        pool = Pool()
        Date = pool.get('ir.date')
        context = Transaction().context
        today = Date.today()
        pattern = pattern.copy() if pattern else {}
        pattern['date'] = pattern.get('date', context.get('date')) or today
        return super().get(price_list, product, pattern=pattern)


class PriceListLine(metaclass=PoolMeta):
    __name__ = 'product.price_list.line'

    start_date = fields.Date(
        "Start Date",
        domain=[
            If(Eval('start_date') & Eval('end_date'),
                ('start_date', '<=', Eval('end_date')),
                ()),
            ])
    end_date = fields.Date(
        "End Date",
        domain=[
            If(Eval('start_date') & Eval('end_date'),
                ('end_date', '>=', Eval('start_date')),
                ()),
            ])

    def match(self, pattern):
        pool = Pool()
        Date = pool.get('ir.date')

        pattern = pattern.copy()
        date = pattern.pop('date', None) or Date.today()
        if self.start_date and self.start_date > date:
            return False
        if self.end_date and self.end_date < date:
            return False
        return super(PriceListLine, self).match(pattern)


class PriceListLineContext(ModelView):
    "Price List Line Context"
    __name__ = 'product.price_list.line.context'

    date = fields.Date("Date")

    @classmethod
    def default_date(cls):
        pool = Pool()
        Date = pool.get('ir.date')
        return Date.today()


class SaleContext(metaclass=PoolMeta):
    __name__ = 'product.sale.context'

    date = fields.Function(
        fields.Date("Date"),
        'on_change_with_date')

    @fields.depends('sale_date')
    def on_change_with_date(self, name=None):
        return self.sale_date