This repository has been archived by the owner on Dec 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stock.py
73 lines (66 loc) · 2.75 KB
/
stock.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from trytond.model import fields
from trytond.pool import Pool, PoolMeta
from trytond.tools import grouped_slice
from trytond.transaction import Transaction
class Lot(metaclass=PoolMeta):
__name__ = 'stock.lot'
subscription_services = fields.Many2Many(
'sale.subscription.service-stock.lot.asset',
'lot', 'service', "Services")
subscription_lines = fields.One2Many(
'sale.subscription.line', 'asset_lot', "Subscription Lines")
subscribed = fields.Function(
fields.Many2One('sale.subscription.line', "Subscribed"),
'get_subscribed', searcher='search_subscribed')
@classmethod
def get_subscribed(cls, lots, name):
pool = Pool()
Date = pool.get('ir.date')
SubscriptionLine = pool.get('sale.subscription.line')
subscribed_lines = {l.id: None for l in lots}
date = Transaction().context.get('date', Date.today())
for sub_lots in grouped_slice(lots):
lines = SubscriptionLine.search([
('asset_lot', 'in', [l.id for l in sub_lots]),
[
('start_date', '<=', date),
['OR',
('end_date', '=', None),
('end_date', '>', date),
],
]
])
subscribed_lines.update((s.asset_lot.id, s.id) for s in lines)
return subscribed_lines
@classmethod
def search_subscribed(cls, name, clause):
pool = Pool()
Date = pool.get('ir.date')
name, operator, value = clause[:3]
date = Transaction().context.get('date', Date.today())
domain = [
('asset_lot', '!=', None),
('start_date', '<=', date),
['OR',
('end_date', '=', None),
('end_date', '>', date),
],
]
if '.' in name:
_, target_name = name.split('.', 1)
domain.append((target_name,) + tuple(clause[1:]))
return [('subscription_lines', 'where', domain)]
else:
if (operator, value) == ('=', None):
return [('subscription_lines', 'not where', domain)]
elif (operator, value) == ('!=', None):
return [('subscription_lines', 'where', domain)]
else:
if isinstance(value, str):
target_name = 'rec_name'
else:
target_name = 'id'
domain.append((target_name,) + tuple(clause[1:]))
return [('subscription_lines', 'where', domain)]