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

« back to all changes in this revision

Viewing changes to product_extended/product_extended.py

  • Committer: Cubic ERP
  • Date: 2012-11-28 00:54:54 UTC
  • mfrom: (5823.1.19 openobject-addons)
  • Revision ID: info@cubicerp.com-20121128005454-bbtsmufafj4hwady
[UPG]

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
##############################################################################
2
 
#    
3
 
#    OpenERP, Open Source Management Solution
4
 
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
5
 
#
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.
10
 
#
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.
15
 
#
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/>.     
18
 
#
19
 
##############################################################################
20
 
 
21
 
from osv import fields
22
 
from osv import osv
23
 
#import ir
24
 
import pooler
25
 
 
26
 
class product_supplierinfo(osv.osv):
27
 
    _inherit = 'product.supplierinfo'
28
 
    _name = "product.supplierinfo"
29
 
 
30
 
    def _last_order(self, cr, uid, ids, name, arg, context):
31
 
        res = {}
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()
35
 
            if record:
36
 
                res[supinfo.id] = record[0]
37
 
            else:
38
 
                res[supinfo.id] = False
39
 
        return res
40
 
 
41
 
    def _last_order_date(self, cr, uid, ids, name, arg, context):
42
 
        res = {}
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'])
46
 
        for suppinfo in ids:
47
 
            date_approve = [x['date_approve'] for x in dates if x['id']==last_orders[suppinfo]]
48
 
            if date_approve:
49
 
                res[suppinfo] = date_approve[0]
50
 
            else:
51
 
                res[suppinfo] = False
52
 
        return res
53
 
 
54
 
    _columns = {
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'),
57
 
    }
58
 
product_supplierinfo()
59
 
 
60
 
 
61
 
class product_product(osv.osv):
62
 
    _name = 'product.product'
63
 
    _inherit = 'product.product'
64
 
    
65
 
    def _find_op(self, cr, uid, ids, name, arg, context):
66
 
        res = {}
67
 
        for product_id in ids:
68
 
            cr.execute('SELECT swo.id from stock_warehouse_orderpoint AS swo WHERE product_id=%s', (product_id,))
69
 
            op_id = cr.fetchone()
70
 
            if op_id:
71
 
                res[product_id] = op_id[0]
72
 
            else:
73
 
                res[product_id] = False
74
 
        return res
75
 
 
76
 
    def _product_dispo(self, cr, uid, ids, name, arg, context={}):
77
 
        res = {}
78
 
        out = self._product_outgoing_qty(cr, uid, ids, name, arg, context)
79
 
        now = self._product_qty_available(cr, uid, ids, name, arg, context)
80
 
        for p_id in ids:
81
 
            res[p_id] = now[p_id] + out[p_id]
82
 
        return res
83
 
 
84
 
 
85
 
    _columns = {
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'),
89
 
    }
90
 
 
91
 
    _defaults = {
92
 
        'calculate_price': lambda w,x,y,z: False,
93
 
    }
94
 
 
95
 
    def compute_price(self, cr, uid, ids, *args):
96
 
        for prod_id in ids:
97
 
            bom_ids = pooler.get_pool(cr.dbname).get('mrp.bom').search(cr, uid, [('product_id', '=', prod_id)])
98
 
            if bom_ids:
99
 
                for bom in pooler.get_pool(cr.dbname).get('mrp.bom').browse(cr, uid, bom_ids):
100
 
                    self._calc_price(cr, uid, bom)
101
 
        return True
102
 
                    
103
 
    def _calc_price(self, cr, uid, bom):
104
 
        if not bom.product_id.calculate_price:
105
 
            return bom.product_id.standard_price
106
 
        else:
107
 
            price = 0
108
 
            if bom.bom_lines:
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                    
112
 
            else:
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
119
 
                    else:
120
 
#                        price += other_bom.product_qty * other_bom.product_id.standard_price
121
 
                        price += other_bom.product_id.standard_price
122
 
                else:
123
 
#                    price += bom.product_qty * bom.product_id.standard_price
124
 
                    price += bom.product_id.standard_price
125
 
#                if no_child_bom:
126
 
#                    other_bom = bom_obj.browse(cr, uid, no_child_bom)[0]
127
 
#                    price += bom.product_qty * self._calc_price(cr, uid, other_bom)
128
 
#                else:
129
 
#                    price += bom.product_qty * bom.product_id.standard_price
130
 
            if bom.routing_id:
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)
137
 
            if bom.bom_lines:
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)
141
 
            return price
142
 
product_product()
143
 
 
144
 
class product_bom(osv.osv):
145
 
    _inherit = 'mrp.bom'
146
 
            
147
 
    _columns = {
148
 
        'standard_price': fields.related('product_id','standard_price',type="float",relation="product.product",string="Standard Price",store=False)
149
 
    }
150
 
 
151
 
product_bom()
152
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
153