1
##############################################################################
3
# OpenERP, Open Source Management Solution
4
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU Affero General Public License as
8
# published by the Free Software Foundation, either version 3 of the
9
# License, or (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU Affero General Public License for more details.
16
# You should have received a copy of the GNU Affero General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
##############################################################################
21
from osv import fields
26
class product_supplierinfo(osv.osv):
27
_inherit = 'product.supplierinfo'
28
_name = "product.supplierinfo"
30
def _last_order(self, cr, uid, ids, name, arg, context):
32
for supinfo in self.browse(cr, uid, ids):
33
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 line.product_id=%s and po.partner_id=%s and po.state='approved' group by po.id", (supinfo.product_id.id, supinfo.name.id,))
34
record = cr.fetchone()
36
res[supinfo.id] = record[0]
38
res[supinfo.id] = False
41
def _last_order_date(self, cr, uid, ids, name, arg, context):
43
po = self.pool.get('purchase.order')
44
last_orders = self._last_order(cr, uid, ids, name, arg, context)
45
dates = po.read(cr, uid, filter(None, last_orders.values()), ['date_approve'])
47
date_approve = [x['date_approve'] for x in dates if x['id']==last_orders[suppinfo]]
49
res[suppinfo] = date_approve[0]
55
'last_order' : fields.function(_last_order, type='many2one', obj='purchase.order', method=True, string='Last Order'),
56
'last_order_date' : fields.function(_last_order_date, type='date', method=True, string='Last Order date'),
58
product_supplierinfo()
61
class product_product(osv.osv):
62
_name = 'product.product'
63
_inherit = 'product.product'
65
def _find_op(self, cr, uid, ids, name, arg, context):
67
for product_id in ids:
68
cr.execute('SELECT swo.id from stock_warehouse_orderpoint AS swo WHERE product_id=%s', (product_id,))
71
res[product_id] = op_id[0]
73
res[product_id] = False
76
def _product_dispo(self, cr, uid, ids, name, arg, context={}):
78
out = self._product_outgoing_qty(cr, uid, ids, name, arg, context)
79
now = self._product_qty_available(cr, uid, ids, name, arg, context)
81
res[p_id] = now[p_id] + out[p_id]
86
'calculate_price': fields.boolean('Compute standard price', help="Check this box if the standard price must be computed from the BoM."),
87
'orderpoint_ids': fields.one2many('stock.warehouse.orderpoint', 'product_id', 'Orderpoints'),
88
'qty_dispo': fields.function(_product_dispo, method=True, type='float', string='Stock available'),
92
'calculate_price': lambda w,x,y,z: False,
95
def compute_price(self, cr, uid, ids, *args):
97
bom_ids = pooler.get_pool(cr.dbname).get('mrp.bom').search(cr, uid, [('product_id', '=', prod_id)])
99
for bom in pooler.get_pool(cr.dbname).get('mrp.bom').browse(cr, uid, bom_ids):
100
self._calc_price(cr, uid, bom)
103
def _calc_price(self, cr, uid, bom):
104
if not bom.product_id.calculate_price:
105
return bom.product_id.standard_price
109
for sbom in bom.bom_lines:
110
my_qty = sbom.bom_lines and 1.0 or sbom.product_qty
111
price += self._calc_price(cr, uid, sbom) * my_qty
113
bom_obj = pooler.get_pool(cr.dbname).get('mrp.bom')
114
no_child_bom = bom_obj.search(cr, uid, [('product_id', '=', bom.product_id.id), ('bom_id', '=', False)])
115
if no_child_bom and bom.id not in no_child_bom:
116
other_bom = bom_obj.browse(cr, uid, no_child_bom)[0]
117
if not other_bom.product_id.calculate_price:
118
price += self._calc_price(cr, uid, other_bom) * other_bom.product_qty
120
# price += other_bom.product_qty * other_bom.product_id.standard_price
121
price += other_bom.product_id.standard_price
123
# price += bom.product_qty * bom.product_id.standard_price
124
price += bom.product_id.standard_price
126
# other_bom = bom_obj.browse(cr, uid, no_child_bom)[0]
127
# price += bom.product_qty * self._calc_price(cr, uid, other_bom)
129
# price += bom.product_qty * bom.product_id.standard_price
131
for wline in bom.routing_id.workcenter_lines:
132
wc = wline.workcenter_id
133
cycle = wline.cycle_nbr
134
hour = (wc.time_start + wc.time_stop + cycle * wc.time_cycle) * (wc.time_efficiency or 1.0)
135
price += wc.costs_cycle * cycle + wc.costs_hour * hour
136
price = self.pool.get('product.uom')._compute_price(cr,uid,bom.product_uom.id,price,bom.product_id.uom_id.id)
138
self.write(cr, uid, [bom.product_id.id], {'standard_price' : price/bom.product_qty})
139
if bom.product_uom.id != bom.product_id.uom_id.id:
140
price = self.pool.get('product.uom')._compute_price(cr,uid,bom.product_uom.id,price,bom.product_id.uom_id.id)
144
class product_bom(osv.osv):
148
'standard_price': fields.related('product_id','standard_price',type="float",relation="product.product",string="Standard Price",store=False)
152
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: