~agilebg/openerp-product-attributes/adding_product_pack_7

« back to all changes in this revision

Viewing changes to product_pack/pack.py

  • Committer: Alex Comba
  • Date: 2013-08-26 10:57:18 UTC
  • Revision ID: alex.comba@agilebg.com-20130826105718-y39b6zcz0zxmu48k
[REF] product_stock module porting to 7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
17
17
#
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
##################################################################################
22
22
 
23
23
import math
24
 
from osv import fields,osv
25
 
 
26
 
class product_pack( osv.osv ):
 
24
from openerp.osv import fields, orm
 
25
 
 
26
 
 
27
class product_pack(orm.Model):
27
28
    _name = 'product.pack.line'
28
29
    _rec_name = 'product_id'
29
30
    _columns = {
32
33
        'product_id': fields.many2one( 'product.product', 'Product', required=True ),
33
34
    }
34
35
 
35
 
class product_product( osv.osv ):
 
36
class product_product(orm.Model):
36
37
    _inherit = 'product.product'
37
38
    _columns = {
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.' ),
41
42
    }
42
43
 
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 """
45
46
        result={}
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)
48
49
 
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]
52
53
                continue
53
54
 
54
55
            first_subproduct = True
58
59
            if product.pack_line_ids:
59
60
 
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)
62
63
 
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:
68
69
                        continue
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:
73
74
                            continue
74
75
 
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
78
79
                        continue
79
80
 
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:
84
85
                        continue
85
 
                    pack_stock_next = math.floor( subproduct_stock_next / subproduct_quantity_next )
 
86
                    pack_stock_next = math.floor(subproduct_stock_next / subproduct_quantity_next)
86
87
 
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
90
91
 
91
92
                # result is the minimum stock of all subproducts
92
 
                result[ product.id ] = pack_stock
 
93
                result[product.id] = pack_stock
93
94
            else:
94
 
                result[ product.id ] = stock[ product.id ]
 
95
                result[product.id] = stock[product.id]
95
96
        return result
96
97
 
97
 
class sale_order_line(osv.osv):
 
98
 
 
99
class sale_order_line(orm.Model):
98
100
    _inherit = 'sale.order.line'
99
101
    _columns = {
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,
106
108
    }
107
109
 
108
 
class sale_order(osv.osv):
 
110
 
 
111
class sale_order(orm.Model):
109
112
    _inherit = 'sale.order'
110
113
 
111
114
    def create(self, cr, uid, vals, context=None):
247
250
            self.expand_packs(cr, uid, ids, context, depth+1)
248
251
        return
249
252
 
250
 
class purchase_order_line(osv.osv):
 
253
 
 
254
class purchase_order_line(orm.Model):
251
255
    _inherit = 'purchase.order.line'
252
256
    _columns = {
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,
260
264
    }
261
265
 
262
 
class purchase_order(osv.osv):
 
266
 
 
267
class purchase_order(orm.Model):
263
268
    _inherit = 'purchase.order'
264
269
 
265
270
    def create(self, cr, uid, vals, context=None):