/usr/lib/python3/dist-packages/trytond/modules/stock/period.py is in tryton-modules-stock 4.6.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | # 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 itertools import chain
from sql import Literal, For
from trytond.model import Workflow, ModelView, ModelSQL, fields
from trytond.pyson import Eval, If
from trytond.transaction import Transaction
from trytond.pool import Pool
from trytond.tools import grouped_slice
__all__ = ['Period', 'Cache']
class Period(Workflow, ModelSQL, ModelView):
'Stock Period'
__name__ = 'stock.period'
date = fields.Date('Date', required=True, states={
'readonly': Eval('state') == 'closed',
}, depends=['state'])
company = fields.Many2One('company.company', 'Company', required=True,
domain=[
('id', If(Eval('context', {}).contains('company'), '=', '!='),
Eval('context', {}).get('company', -1)),
])
caches = fields.One2Many('stock.period.cache', 'period', 'Caches',
readonly=True)
state = fields.Selection([
('draft', 'Draft'),
('closed', 'Closed'),
], 'State', select=True, readonly=True)
@classmethod
def __setup__(cls):
super(Period, cls).__setup__()
cls._error_messages.update({
'close_period_future_today': ('You can not close a period '
'in the future or today.'),
'close_period_assigned_move': (
'You can not close a period when '
'there still are assigned moves.'),
})
cls._transitions |= set((
('draft', 'closed'),
('closed', 'draft'),
))
cls._buttons.update({
'draft': {
'invisible': Eval('state') == 'draft',
},
'close': {
'invisible': Eval('state') == 'closed',
},
})
@staticmethod
def default_company():
return Transaction().context.get('company')
@staticmethod
def default_state():
return 'draft'
@staticmethod
def groupings():
return [('product',)]
@staticmethod
def get_cache(grouping):
pool = Pool()
if grouping == ('product',):
return pool.get('stock.period.cache')
def get_rec_name(self, name):
return str(self.date)
@classmethod
@ModelView.button
@Workflow.transition('draft')
def draft(cls, periods):
for grouping in cls.groupings():
Cache = cls.get_cache(grouping)
caches = []
for sub_periods in grouped_slice(periods):
caches.append(Cache.search([
('period', 'in',
[p.id for p in sub_periods]),
], order=[]))
Cache.delete(list(chain(*caches)))
@classmethod
@ModelView.button
@Workflow.transition('closed')
def close(cls, periods):
pool = Pool()
Product = pool.get('product.product')
Location = pool.get('stock.location')
Move = pool.get('stock.move')
Date = pool.get('ir.date')
transaction = Transaction()
connection = transaction.connection
database = transaction.database
# XXX: A move in the period could be inserted before the lock
# from a different transaction. It will not be taken in the pbl
# computation but it is quite rare because only past periods are
# closed.
database.lock(connection, Move._table)
if database.has_select_for():
move = Move.__table__()
query = move.select(Literal(1), for_=For('UPDATE', nowait=True))
with connection.cursor() as cursor:
cursor.execute(*query)
locations = Location.search([
('type', 'not in', ['warehouse', 'view']),
], order=[])
today = Date.today()
recent_date = max(period.date for period in periods)
if recent_date >= today:
cls.raise_user_error('close_period_future_today')
if Move.search([
('state', '=', 'assigned'),
['OR', [
('effective_date', '=', None),
('planned_date', '<=', recent_date),
],
('effective_date', '<=', recent_date),
]]):
cls.raise_user_error('close_period_assigned_move')
for grouping in cls.groupings():
Cache = cls.get_cache(grouping)
to_create = []
for period in periods:
with Transaction().set_context(
stock_date_end=period.date,
stock_date_start=None,
stock_assign=False,
forecast=False,
stock_destinations=None,
):
pbl = Product.products_by_location(
[l.id for l in locations], grouping=grouping)
for key, quantity in pbl.items():
values = {
'location': key[0],
'period': period.id,
'internal_quantity': quantity,
}
for i, field in enumerate(grouping, 1):
values[field] = key[i]
to_create.append(values)
if to_create:
Cache.create(to_create)
class Cache(ModelSQL, ModelView):
'''
Stock Period Cache
It is used to store cached computation of stock quantities.
'''
__name__ = 'stock.period.cache'
period = fields.Many2One('stock.period', 'Period', required=True,
readonly=True, select=True, ondelete='CASCADE')
location = fields.Many2One('stock.location', 'Location', required=True,
readonly=True, select=True, ondelete='CASCADE')
product = fields.Many2One('product.product', 'Product', required=True,
readonly=True, select=True, ondelete='CASCADE')
internal_quantity = fields.Float('Internal Quantity', readonly=True)
|