1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2011 TeMPO Consulting, MSF
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Affero General Public License as
9
# published by the Free Software Foundation, either version 3 of the
10
# License, or (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Affero General Public License for more details.
17
# You should have received a copy of the GNU Affero General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
##############################################################################
22
from osv import osv, fields
25
from tools.translate import _
27
class stock_warehouse_order_cycle(osv.osv):
28
_name = 'stock.warehouse.order.cycle'
29
_description = 'Order Cycle'
30
_order = 'sequence, id'
32
def create(self, cr, uid, data, context={}):
34
Checks if a frequence was choosen for the cycle
36
if not 'button' in context and (not 'frequence_id' in data or not data.get('frequence_id', False)):
37
raise osv.except_osv(_('Error'), _('You should choose a frequence for this rule !'))
39
return super(stock_warehouse_order_cycle, self).create(cr, uid, data, context=context)
41
def write(self, cr, uid, ids, data, context={}):
43
Checks if a frequence was choosen for the cycle
45
if not 'button' in context and (not 'frequence_id' in data or not data.get('frequence_id', False)):
46
for proc in self.browse(cr, uid, ids):
47
if not proc.frequence_id:
48
raise osv.except_osv(_('Error'), _('You should choose a frequence for this rule !'))
50
return super(stock_warehouse_order_cycle, self).write(cr, uid, ids, data, context=context)
53
def _get_frequence_change(self, cr, uid, ids, context={}):
55
Returns ids when the frequence change
58
for frequence in self.pool.get('stock.frequence').browse(cr, uid, ids, context=context):
59
for cycle in frequence.order_cycle_ids:
64
def _get_frequence_name(self, cr, uid, ids, field_name, arg, context={}):
66
Returns the name_get value of the frequence
69
for proc in self.browse(cr, uid, ids):
70
res[proc.id] = self.pool.get('stock.frequence').name_get(cr, uid, [proc.frequence_id.id])[0][1]
75
'sequence': fields.integer(string='Order', required=True, help='A higher order value means a low priority'),
76
'name': fields.char(size=64, string='Name', required=True),
77
'category_id': fields.many2one('product.category', string='Category'),
78
'product_id': fields.many2one('product.product', string='Specific product'),
79
'warehouse_id': fields.many2one('stock.warehouse', string='Warehouse', required=True),
80
'location_id': fields.many2one('stock.location', string='Location'),
81
'frequence_name': fields.function(_get_frequence_name, method=True, string='Frequence', type='char'),
82
'frequence_id': fields.many2one('stock.frequence', string='Frequence'),
83
'product_ids': fields.many2many('product.product', 'order_cycle_product_rel', 'order_cycle_id', 'product_id', string="Products"),
84
'company_id': fields.many2one('res.company','Company',required=True),
85
'active': fields.boolean('Active', help="If the active field is set to False, it will allow you to hide the automatic supply without removing it."),
86
# Parameters for quantity calculation
87
'leadtime': fields.float(digits=(16,2), string='Delivery lead time to consider', help='Delivery lead time in month'),
88
'order_coverage': fields.float(digits=(16,2), string='Order coverage'),
89
'safety_stock_time': fields.float(digits=(16,2), string='Safety stock in time'),
90
'safety_stock': fields.integer(string='Safety stock (quantity'),
91
'past_consumption': fields.boolean(string='Past monthly consumption'),
92
'reviewed_consumption': fields.boolean(string='Reviewed monthly consumption'),
93
'manual_consumption': fields.float(digits=(16,2), string='Manual monthly consumption'),
94
'next_date': fields.related('frequence_id', 'next_date', string='Next scheduled date', readonly=True, type='date',
95
store={'stock.warehouse.order.cycle': (lambda self, cr, uid, ids, context={}: ids, ['frequence_id'], 20),
96
'stock.frequence': (_get_frequence_change, None, 20)}),
100
'sequence': lambda *a: 10,
101
'past_consumption': lambda *a: 1,
102
'active': lambda *a: 1,
103
'name': lambda x,y,z,c: x.pool.get('ir.sequence').get(y,z,'stock.order.cycle') or '',
104
'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'stock.warehouse.order.cycle', context=c),
105
'order_coverage': lambda *a: 3,
108
def consumption_method_change(self, cr, uid, ids, past_consumption, reviewed_consumption, manual_consumption, product_id, field='past'):
110
Uncheck a box when the other is checked
113
if field == 'past' and past_consumption:
114
v.update({'reviewed_consumption': 0, 'manual_consumption': 0.00})
115
elif field == 'past' and not past_consumption:
116
v.update({'reviewed_consumption': 1, 'manual_consumption': 0.00})
117
elif field == 'review' and reviewed_consumption:
118
v.update({'past_consumption': 0, 'manual_consumption': 0.00})
119
elif field == 'review' and not reviewed_consumption:
120
v.update({'past_consumption': 1, 'manual_consumption': 0.00})
121
elif field == 'manual' and manual_consumption != 0.00 and product_id:
122
v.update({'reviewed_consumption': 0, 'past_consumption': 0})
123
elif field == 'manual' and (manual_consumption == 0.00 or not product_id):
124
v.update({'past_consumption': 1})
128
def choose_change_frequence(self, cr, uid, ids, context={}):
130
Open a wizard to define a frequency for the order cycle
131
or open a wizard to modify the frequency if frequency already exists
133
if isinstance(ids, (int, long)):
140
for proc in self.browse(cr, uid, ids):
142
if proc.frequence_id and proc.frequence_id.id:
143
frequence_id = proc.frequence_id.id
146
context.update({'active_id': res_id,
147
'active_model': 'stock.warehouse.order.cycle',
150
return {'type': 'ir.actions.act_window',
152
'res_model': 'stock.frequence',
154
'view_model': 'form',
156
'res_id': frequence_id}
158
def onchange_warehouse_id(self, cr, uid, ids, warehouse_id, context=None):
159
""" Finds location id for changed warehouse.
160
@param warehouse_id: Changed id of warehouse.
161
@return: Dictionary of values.
164
w = self.pool.get('stock.warehouse').browse(cr, uid, warehouse_id, context=context)
165
v = {'location_id': w.lot_stock_id.id}
169
def unlink(self, cr, uid, ids, context):
170
if isinstance(ids, (int, long)):
173
for auto in self.read(cr, uid, ids, ['frequence_id']):
174
if auto['frequence_id']:
175
freq_ids.append(auto['frequence_id'][0])
177
self.pool.get('stock.frequence').unlink(cr, uid, freq_ids, context)
178
return super(stock_warehouse_order_cycle, self).unlink(cr, uid, ids, context=context)
180
def copy(self, cr, uid, id, default=None, context=None):
183
obj = self.read(cr, uid, id, ['frequence_id'])
184
if obj['frequence_id']:
185
default['frequence_id'] = self.pool.get('stock.frequence').copy(cr, uid, obj['frequence_id'][0], context=context)
188
'name': self.pool.get('ir.sequence').get(cr, uid, 'stock.order.cycle') or '',
190
return super(stock_warehouse_order_cycle, self).copy(cr, uid, id, default, context=context)
192
stock_warehouse_order_cycle()
194
class stock_frequence(osv.osv):
195
_name = 'stock.frequence'
196
_inherit = 'stock.frequence'
199
'order_cycle_ids': fields.one2many('stock.warehouse.order.cycle', 'frequence_id', string='Order Cycle'),
202
def copy(self, cr, uid, id, default=None, context=None):
205
default['order_cycle_ids'] = False
206
return super(stock_frequence, self).copy(cr, uid, id, default, context)
208
def choose_frequency(self, cr, uid, ids, context={}):
210
Adds the support of order cycles on choose frequency method
212
if isinstance(ids, (int, long)):
215
if not context.get('res_ok', False) and 'active_id' in context and 'active_model' in context and \
216
context.get('active_model') == 'stock.warehouse.order.cycle':
217
self.pool.get('stock.warehouse.order.cycle').write(cr, uid, [context.get('active_id')], {'frequence_id': ids[0]})
219
return super(stock_frequence, self).choose_frequency(cr, uid, ids, context=context)
223
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: