Files
tryton/period.py
T

132 lines
4.8 KiB
Python
Raw Normal View History

#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 trytond.model import ModelView, ModelSQL, fields
from trytond.pyson import Equal, Eval, If, In, Get
from trytond.transaction import Transaction
2011-07-03 13:19:14 +02:00
from trytond.pool import Pool
2012-09-24 22:18:17 +02:00
__all__ = ['Period', 'Cache']
class Period(ModelSQL, ModelView):
'Stock Period'
2012-09-24 22:18:17 +02:00
__name__ = 'stock.period'
_rec_name = 'date'
date = fields.Date('Date', required=True, states={
'readonly': Equal(Eval('state'), 'closed'),
2011-08-04 16:57:42 +02:00
}, depends=['state'])
company = fields.Many2One('company.company', 'Company', required=True,
domain=[
('id', If(In('company', Eval('context', {})), '=', '!='),
2013-02-24 16:18:17 +01:00
Get(Eval('context', {}), 'company', 0)),
])
caches = fields.One2Many('stock.period.cache', 'period', 'Caches')
state = fields.Selection([
('draft', 'Draft'),
('closed', 'Closed'),
2012-03-05 10:53:08 +01:00
], 'State', select=True, readonly=True)
2012-09-24 22:18:17 +02:00
@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.'),
})
2012-09-24 22:18:17 +02:00
cls._buttons.update({
2012-03-25 00:18:19 +01:00
'draft': {
'invisible': Eval('state') == 'draft',
},
'close': {
'invisible': Eval('state') == 'closed',
},
})
2012-12-05 15:12:50 +01:00
@staticmethod
def default_state():
return 'draft'
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
2012-09-24 22:18:17 +02:00
def draft(cls, periods):
Cache = Pool().get('stock.period.cache')
caches = []
for i in xrange(0, len(periods), Transaction().cursor.IN_MAX):
caches.append(Cache.search([
('period', 'in', [p.id for p in
periods[i:i + Transaction().cursor.IN_MAX]]),
], order=[]))
Cache.delete(list(chain(*caches)))
cls.write(periods, {
'state': 'draft',
})
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
2012-09-24 22:18:17 +02:00
def close(cls, periods):
2011-07-03 13:19:14 +02:00
pool = Pool()
2012-09-24 22:18:17 +02:00
Product = pool.get('product.product')
Location = pool.get('stock.location')
Cache = pool.get('stock.period.cache')
Move = pool.get('stock.move')
Date = pool.get('ir.date')
2012-09-24 22:18:17 +02:00
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:
2012-09-24 22:18:17 +02:00
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')
2013-01-06 22:18:13 +01:00
to_create = []
for period in periods:
with Transaction().set_context(
2012-09-24 22:18:17 +02:00
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])
for (location_id, product_id), quantity in pbl.iteritems():
2013-01-06 22:18:13 +01:00
to_create.append({
2012-09-24 22:18:17 +02:00
'period': period.id,
'location': location_id,
'product': product_id,
'internal_quantity': quantity,
})
2013-01-06 22:18:13 +01:00
if to_create:
Cache.create(to_create)
2012-09-24 22:18:17 +02:00
cls.write(periods, {
'state': 'closed',
})
class Cache(ModelSQL, ModelView):
'''
Stock Period Cache
It is used to store cached computation of stock quantities.
'''
2012-09-24 22:18:17 +02:00
__name__ = 'stock.period.cache'
period = fields.Many2One('stock.period', 'Period', required=True,
2012-03-05 10:53:08 +01:00
readonly=True, select=True, ondelete='CASCADE')
location = fields.Many2One('stock.location', 'Location', required=True,
2012-03-05 10:53:08 +01:00
readonly=True, select=True, ondelete='CASCADE')
product = fields.Many2One('product.product', 'Product', required=True,
2012-03-05 10:53:08 +01:00
readonly=True, select=True, ondelete='CASCADE')
internal_quantity = fields.Float('Internal Quantity', readonly=True)