~technofluid-team/openobject-addons/technofluid_multiple_installations

« back to all changes in this revision

Viewing changes to product_extended/product_extended.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-dedd7f8a42bd4557112a0513082691b8590ad6cc
New trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#
 
5
# $Id$
 
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
from osv import fields
 
31
from osv import osv
 
32
import ir
 
33
import pooler
 
34
 
 
35
class product_supplierinfo(osv.osv):
 
36
        _inherit = 'product.supplierinfo'
 
37
        _name = "product.supplierinfo"
 
38
 
 
39
        def _last_order(self, cr, uid, ids, name, arg, context):
 
40
                res = {}
 
41
                for supinfo in self.browse(cr, uid, ids):
 
42
                        cr.execute("select po.id, max(po.date_approve) from purchase_order as po, purchase_order_line as line where po.id=line.order_id and product_id=%d and partner_id=%d and state='approved' group by po.id", (supinfo.product_id.id, supinfo.name.id,))
 
43
                        record = cr.fetchone()
 
44
                        if record:
 
45
                                res[supinfo.id] = record[0]
 
46
                        else:
 
47
                                res[supinfo.id] = False
 
48
                return res
 
49
 
 
50
        def _last_order_date(self, cr, uid, ids, name, arg, context):
 
51
                res = {}
 
52
                po = self.pool.get('purchase.order')
 
53
                last_orders = self._last_order(cr, uid, ids, name, arg, context)
 
54
                dates = po.read(cr, uid, filter(None, last_orders.values()), ['date_approve'])
 
55
                for suppinfo in ids:
 
56
                        date_approve = [x['date_approve'] for x in dates if x['id']==last_orders[suppinfo]]
 
57
                        if date_approve:
 
58
                                res[suppinfo] = date_approve[0]
 
59
                        else:
 
60
                                res[suppinfo] = False
 
61
                return res
 
62
 
 
63
        _columns = {
 
64
                'last_order' : fields.function(_last_order, type='many2one', obj='purchase.order', method=True, string='Last Order'),
 
65
                'last_order_date' : fields.function(_last_order_date, type='date', method=True, string='Last Order date'),
 
66
        }
 
67
product_supplierinfo()
 
68
 
 
69
 
 
70
class product_product(osv.osv):
 
71
        _name = 'product.product'
 
72
        _inherit = 'product.product'
 
73
        
 
74
        def _find_op(self, cr, uid, ids, name, arg, context):
 
75
                res = {}
 
76
                for product_id in ids:
 
77
                        cr.execute('SELECT swo.id from stock_warehouse_orderpoint AS swo WHERE product_id=%d', (product_id,))
 
78
                        op_id = cr.fetchone()
 
79
                        if op_id:
 
80
                                res[product_id] = op_id[0]
 
81
                        else:
 
82
                                res[product_id] = False
 
83
                return res
 
84
 
 
85
        def _product_dispo(self, cr, uid, ids, name, arg, context={}):
 
86
                res = {}
 
87
                out = self._product_outgoing_qty(cr, uid, ids, name, arg, context)
 
88
                now = self._product_qty_available(cr, uid, ids, name, arg, context)
 
89
                for p_id in ids:
 
90
                        res[p_id] = now[p_id] + out[p_id]
 
91
                return res
 
92
 
 
93
 
 
94
        _columns = {
 
95
                'calculate_price': fields.boolean('Compute price'),
 
96
                'orderpoint_ids': fields.one2many('stock.warehouse.orderpoint', 'product_id', 'Orderpoints'),
 
97
                'qty_dispo': fields.function(_product_dispo, method=True, type='float', string='Stock disponible'),
 
98
        }
 
99
 
 
100
        _defaults = {
 
101
                'calculate_price': lambda w,x,y,z: False,
 
102
        }
 
103
 
 
104
        def compute_price(self, cr, uid, ids, *args):
 
105
                for prod_id in ids:
 
106
                        bom_ids = pooler.get_pool(cr.dbname).get('mrp.bom').search(cr, uid, [('product_id', '=', prod_id)])
 
107
                        if bom_ids:
 
108
                                for bom in pooler.get_pool(cr.dbname).get('mrp.bom').browse(cr, uid, bom_ids):
 
109
                                        self._calc_price(cr, uid, bom)
 
110
                return True
 
111
                                        
 
112
        def _calc_price(self, cr, uid, bom):
 
113
                if not bom.product_id.calculate_price:
 
114
                        return bom.product_id.standard_price
 
115
                else:
 
116
                        price = 0
 
117
                        if bom.bom_lines:
 
118
                                for sbom in bom.bom_lines:
 
119
                                        price += self._calc_price(cr, uid, sbom) * sbom.product_qty
 
120
                        else:
 
121
                                bom_obj = pooler.get_pool(cr.dbname).get('mrp.bom')
 
122
                                no_child_bom = bom_obj.search(cr, uid, [('product_id', '=', bom.product_id.id), ('bom_id', 'is', None)])
 
123
                                if no_child_bom:
 
124
                                        other_bom = bom_obj.browse(cr, uid, no_child_bom)[0]
 
125
                                        price += bom.product_qty * self._calc_price(cr, uid, other_bom)
 
126
                                elif bom.product_id.seller_ids:
 
127
                                        pricelist_id = bom.product_id.seller_ids[0].name.property_product_pricelist_purchase[0]
 
128
                                        price += pooler.get_pool(cr.dbname).get('product.pricelist').price_get(cr, uid, [pricelist_id], bom.product_id.id, bom.product_qty, 'standard')[pricelist_id]
 
129
                                else:
 
130
                                        price += bom.product_qty * bom.product_id.standard_price
 
131
                                
 
132
                        if bom.routing_id:
 
133
                                for wline in bom.routing_id.workcenter_lines:
 
134
                                        wc = wline.workcenter_id
 
135
                                        cycle = wline.cycle_nbr
 
136
                                        hour = (wc.time_start + wc.time_stop + cycle * wc.time_cycle) *  (wc.time_efficiency or 1.0)
 
137
                                        price += wc.costs_cycle * cycle + wc.costs_hour * hour
 
138
                        if bom.bom_lines:
 
139
                                self.write(cr, uid, [bom.product_id.id], {'standard_price' : price})
 
140
                        return price
 
141
product_product()