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
|
# -*- coding: utf-8 -*-
##############################################################################
#
# This module is copyright (C) 2014 Numérigraphe SARL. All Rights Reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp.osv import orm, fields
import openerp.addons.decimal_precision as dp
# Expose the method as a function, like when the fields are defined,
# and use the pool to call the method from the other modules too.
def _product_available_fnct(self, cr, uid, ids, field_names=None, arg=False,
context=None):
return self.pool['product.product']._product_available(
cr, uid, ids, field_names=field_names, arg=arg, context=context)
class ProductProduct(orm.Model):
"""Add a field for the stock available to promise.
Useful implementations need to be installed through the Settings menu or by
installing one of the modules stock_available_*
"""
_inherit = 'product.product'
def __init__(self, pool, cr):
"""Use _product_available_fnct to compute all the quantities."""
# Doing this lets us change the function and not redefine fields
super(ProductProduct, self).__init__(pool, cr)
for coldef in self._columns.values():
if (isinstance(coldef, fields.function)
and coldef._multi == 'qty_available'):
coldef._fnct = _product_available_fnct
def _product_available(self, cr, uid, ids, field_names=None, arg=False,
context=None):
"""Dummy field for the stock available to promise.
Must be overridden by another module that actually implement
computations.
The sub-modules MUST call super()._product_available BEFORE their own
computations with
context['virtual_is_immediately_usable_qty']=False
AND call _update_virtual_available() AFTER their own computations
with the context from the caller.
@param context: see _update_virtual_available()"""
if context is None:
context = {}
if field_names is None:
field_names = []
else:
# We don't want to change the caller's list
field_names = list(field_names)
# Load virtual_available if it's not already asked for
# We need it to compute immediately_usable_qty
if ('virtual_available' not in field_names
and 'immediately_usable_qty' in field_names):
field_names.append('virtual_available')
if context.get('virtual_is_immediately_usable', False):
# _update_virtual_available will get/set these fields
if 'virtual_available' not in field_names:
field_names.append('virtual_available')
if 'immediately_usable_qty' not in field_names:
field_names.append('immediately_usable_qty')
# Compute the core quantities
res = super(ProductProduct, self)._product_available(
cr, uid, ids, field_names=field_names, arg=arg, context=context)
# By default, available to promise = forecasted quantity
if ('immediately_usable_qty' in field_names):
for stock_qty in res.itervalues():
stock_qty['immediately_usable_qty'] = \
stock_qty['virtual_available']
return self.pool['product.product']._update_virtual_available(
cr, uid, res, context=context)
def _update_virtual_available(self, cr, uid, res, context=None):
"""Copy immediately_usable_qty to virtual_available if context asks
@param context: If the key virtual_is_immediately_usable is True,
then the virtual stock is computed as the stock
available to promise. This lets existing code base
their computations on the new value with a minimum of
change (i.e.: warn salesmen when the stock available
for sale is insufficient to honor a quotation)"""
if (context is None
or not context.get('virtual_is_immediately_usable', False)):
return res
for stock_qty in res.itervalues():
# _product_available makes sure both fields are loaded
# We're changing the caller's state but it's not be a problem
stock_qty['virtual_available'] = \
stock_qty['immediately_usable_qty']
return res
_columns = {
'immediately_usable_qty': fields.function(
_product_available_fnct, multi='qty_available',
type='float',
digits_compute=dp.get_precision('Product Unit of Measure'),
string='Available to promise',
help="Stock for this Product that can be safely proposed "
"for sale to Customers.\n"
"The definition of this value can be configured to suit "
"your needs"),
}
|