~openerp-commiter/openobject-addons/extra-6.0

« back to all changes in this revision

Viewing changes to nan_product_bulk/product.py

  • Committer: Albert Cervera i Areny
  • Date: 2011-03-06 23:27:11 UTC
  • Revision ID: albert@nan-tic.com-20110306232711-vnnpmlkq9oqv0pou
[ADD] nan_product_bulk: Add new nan_product_bulk module.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: latin-1 -*-
 
2
##############################################################################
 
3
#
 
4
# Copyright (c) 2009 �ngel �lvarez - NaN  (http://www.nan-tic.com) All Rights Reserved.
 
5
#
 
6
#
 
7
# WARNING: This program as such is intended to be used by professional
 
8
# programmers who take the whole responsability of assessing all potential
 
9
# consequences resulting from its eventual inadequacies and bugs
 
10
# End users who are looking for a ready-to-use solution with commercial
 
11
# garantees and support are strongly adviced to contract a Free Software
 
12
# Service Company
 
13
#
 
14
# This program is Free Software; you can redistribute it and/or
 
15
# modify it under the terms of the GNU General Public License
 
16
# as published by the Free Software Foundation; either version 2
 
17
# of the License, or (at your option) any later version.
 
18
#
 
19
# This program is distributed in the hope that it will be useful,
 
20
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
22
# GNU General Public License for more details.
 
23
#
 
24
# You should have received a copy of the GNU General Public License
 
25
# along with this program; if not, write to the Free Software
 
26
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
27
#
 
28
##############################################################################
 
29
 
 
30
 
 
31
from osv import osv, fields
 
32
import pooler
 
33
 
 
34
from tools import config
 
35
from tools.translate import _
 
36
 
 
37
class product_product(osv.osv):
 
38
    _inherit = 'product.product'
 
39
    _name = 'product.product'
 
40
 
 
41
    def rounding(self,f, r):
 
42
        if not r:
 
43
            return f
 
44
        return round(f / r) * r
 
45
 
 
46
    def _search_values(self,cr,uid,product_id,bulk_id,context=None):
 
47
        result = {'qty':None, 'uom':None }
 
48
        bom_ids = self.pool.get('mrp.bom').search(cr,uid,[('product_id','=', product_id)],context=context)
 
49
        for bom in self.pool.get('mrp.bom').browse(cr, uid, bom_ids, context=context):
 
50
            value = False
 
51
            for line in bom.bom_lines:
 
52
                if line.product_id.id == bulk_id:
 
53
                    value += round(line.product_qty,5)
 
54
                    uom_id = line.product_uom.id
 
55
                    # break  NOTE: I remove break because I found more than one bom with same components more than one time.
 
56
                    result['qty'] = round( value, 5 )
 
57
                    result['uom'] = uom_id
 
58
        return result
 
59
 
 
60
    def _get_qty_available( self, cr, uid, ids, field_name, field_value, context=None):
 
61
        result = {}
 
62
        for product in self.browse(cr, uid, ids, context):
 
63
            #search stock of this product
 
64
            if field_name == 'bulk_qty_available':
 
65
                product_stock_qty = product.qty_available
 
66
            else:
 
67
                product_stock_qty = product.virtual_available
 
68
 
 
69
            #search stock of bulk 
 
70
            #--------------------
 
71
            bulk_stock = 0.0
 
72
 
 
73
            for product_bulk in product.bulk_of_product_ids:
 
74
                #search unit_convertion       
 
75
                factor = 0.0
 
76
 
 
77
                #seach bom factor and uom_dest
 
78
                res = self._search_values(cr,uid,product_bulk.id, product.id,context=context)
 
79
                factor = res['qty']
 
80
                uom_dest = res['uom']
 
81
 
 
82
                #search stock and uom of bulk
 
83
                #for bulk_product in self.browse(cr, uid, [product_bulk.id], context=context):
 
84
                if field_name == 'bulk_qty_available':
 
85
                    bulk_qty = product_bulk.qty_available
 
86
                else:
 
87
                    bulk_qty = product_bulk.virtual_available
 
88
                uom_id = product.uom_id.id
 
89
                
 
90
                #convert bulk stock to Udm of this product
 
91
                amount = self.pool.get('product.uom')._compute_qty(cr, uid, uom_dest, factor, uom_id)
 
92
                if factor > 0.0:
 
93
                    bulk_stock += amount * bulk_qty
 
94
 
 
95
            #sumatory of stocks
 
96
            result[product.id] = self.rounding(product_stock_qty + bulk_stock, product.uom_id.rounding)
 
97
 
 
98
        return result
 
99
 
 
100
    def _bulk( self, cr, uid, ids, field_name, field_value, context=None):
 
101
        result = {}
 
102
        for product in self.browse(cr, uid, ids, context):
 
103
<<<<<<< HEAD
 
104
            if product.bulk_of_product_ids and len(product.bulk_of_product_ids) > 0:
 
105
                result[product.id] = True
 
106
            else:
 
107
                result[product.id] = False
 
108
=======
 
109
            for bulks in product.bulk_of_product_ids:
 
110
                if product.bulk_of_product_ids and len(product.bulk_of_product_ids) > 0:
 
111
                    result[product.id] = True
 
112
                else:
 
113
                    result[product.id] = False
 
114
>>>>>>> 6dfe98a788cc2c7a754f46028a8cd135f6c63cfc
 
115
        return result
 
116
 
 
117
    _columns = {
 
118
        'bulk_id': fields.many2one('product.product', 'Bulk', select=1, help='Bulk related with this product'),
 
119
        'bulk_of_product_ids': fields.one2many('product.product', 'bulk_id', 'Bulk Of', help='List of all products that have the current one as a bulk.'),
 
120
        'bulk': fields.function(_bulk, method=True, type='boolean', string='Is Bulk?', help='A product is a bulk if there are products that have this as bulk.'),
 
121
        'bulk_qty_available': fields.function(_get_qty_available, method=True, type='float', string='Bulk Stock'),
 
122
        'bulk_virtual_available': fields.function(_get_qty_available, method=True, type='float', string='Virt. bulk stock'),
 
123
 
 
124
    }
 
125
 
 
126
product_product()
 
127
 
 
128
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: