13
13
# This program is distributed in the hope that it will be useful,
14
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
16
# GNU Affero General Public License for more details.
18
18
# You should have received a copy of the GNU Affero General Public License
19
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
21
##################################################################################
24
from osv import fields,osv
26
class product_pack( osv.osv ):
24
from openerp.osv import fields, orm
27
class product_pack(orm.Model):
27
28
_name = 'product.pack.line'
28
29
_rec_name = 'product_id'
32
33
'product_id': fields.many2one( 'product.product', 'Product', required=True ),
35
class product_product( osv.osv ):
36
class product_product(orm.Model):
36
37
_inherit = 'product.product'
38
39
'stock_depends': fields.boolean( 'Stock depends of components', help='Mark if pack stock is calcualted from component stock' ),
40
41
'pack_line_ids': fields.one2many( 'product.pack.line','parent_product_id', 'Pack Products', help='List of products that are part of this pack.' ),
43
def get_product_available( self, cr, uid, ids, context=None ):
44
def get_product_available(self, cr, uid, ids, context=None):
44
45
""" Calulate stock for packs, return maximum stock that lets complete pack """
46
47
for product in self.browse( cr, uid, ids, context=context ):
47
stock = super( product_product, self ).get_product_available( cr, uid, [ product.id ], context=context )
48
stock = super(product_product, self).get_product_available(cr, uid, [product.id], context=context)
49
50
# Check if product stock depends on it's subproducts stock.
50
51
if not product.stock_depends:
51
result[ product.id ] = stock[ product.id ]
52
result[product.id] = stock[product.id]
54
55
first_subproduct = True
58
59
if product.pack_line_ids:
60
61
# Take the stock/virtual stock of all subproducts
61
subproducts_stock = self.get_product_available( cr, uid, [ line.product_id.id for line in product.pack_line_ids ], context=context )
62
subproducts_stock = self.get_product_available(cr, uid, [line.product_id.id for line in product.pack_line_ids], context=context)
63
64
# Go over all subproducts, take quantity needed for the pack and its available stock
64
65
for subproduct in product.pack_line_ids:
69
70
if first_subproduct:
70
71
subproduct_quantity = subproduct.quantity
71
subproduct_stock = subproducts_stock[ subproduct.product_id.id ]
72
subproduct_stock = subproducts_stock[subproduct.product_id.id]
72
73
if subproduct_quantity == 0:
75
76
# Calculate real stock for current pack from the subproduct stock and needed quantity
76
pack_stock = math.floor( subproduct_stock / subproduct_quantity )
77
pack_stock = math.floor(subproduct_stock / subproduct_quantity)
77
78
first_subproduct = False
80
81
# Take the info of the next subproduct
81
82
subproduct_quantity_next = subproduct.quantity
82
subproduct_stock_next = subproducts_stock[ subproduct.product_id.id ]
83
subproduct_stock_next = subproducts_stock[subproduct.product_id.id]
83
84
if subproduct_quantity_next == 0 or subproduct_quantity_next == 0.0:
85
pack_stock_next = math.floor( subproduct_stock_next / subproduct_quantity_next )
86
pack_stock_next = math.floor(subproduct_stock_next / subproduct_quantity_next)
87
88
# compare the stock of a subproduct and the next subproduct
88
89
if pack_stock_next < pack_stock:
89
90
pack_stock = pack_stock_next
91
92
# result is the minimum stock of all subproducts
92
result[ product.id ] = pack_stock
93
result[product.id] = pack_stock
94
result[ product.id ] = stock[ product.id ]
95
result[product.id] = stock[product.id]
97
class sale_order_line(osv.osv):
99
class sale_order_line(orm.Model):
98
100
_inherit = 'sale.order.line'
100
102
'pack_depth': fields.integer('Depth', required=True, help='Depth of the product if it is part of a pack.'),
105
107
'pack_depth': lambda *a: 0,
108
class sale_order(osv.osv):
111
class sale_order(orm.Model):
109
112
_inherit = 'sale.order'
111
114
def create(self, cr, uid, vals, context=None):
247
250
self.expand_packs(cr, uid, ids, context, depth+1)
250
class purchase_order_line(osv.osv):
254
class purchase_order_line(orm.Model):
251
255
_inherit = 'purchase.order.line'
253
257
'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of purchase order lines."),
259
263
'pack_depth': lambda *a: 0,
262
class purchase_order(osv.osv):
267
class purchase_order(orm.Model):
263
268
_inherit = 'purchase.order'
265
270
def create(self, cr, uid, vals, context=None):