Add average price
authorced <>
Mon, 18 Dec 2006 08:53:45 +0000 (08:53 +0000)
committerced <>
Mon, 18 Dec 2006 08:53:45 +0000 (08:53 +0000)
bzr revid: ced-988d63a062f858f1ffc47f729a7601b5b444bb52

addons/product/product.py
addons/product/product_view.xml
addons/purchase/__init__.py
addons/purchase/purchase.py
addons/purchase/stock.py
addons/stock/wizard/wizard_partial_picking.py

index bc91ff2..ff4a380 100644 (file)
@@ -178,10 +178,10 @@ class product_template(osv.osv):
                'rental': fields.boolean('Rentable product'),
                'categ_id': fields.many2one('product.category','Category', required=True, change_default=True),
                'list_price': fields.float('List Price'),
-               'standard_price': fields.float('Standard Price', required=True, digit=(12,6)),
+               'standard_price': fields.float('Cost Price', required=True, digit=(12,6)),
                'volume': fields.float('Volume'),
                'weight': fields.float('Weight'),
-               'cost_method': fields.selection([('standard','Standard Price'), ('pmp','PMP (Not implemented!)'), ('fifo','FIFO')], 'Costing Method', required=True),
+               'cost_method': fields.selection([('standard','Standard Price'), ('average','Average Price')], 'Costing Method', required=True),
                'warranty': fields.float('Warranty (months)'),
                'sale_ok': fields.boolean('Can be sold', help="Determine if the product can be visible in the list of product within a selection from a sale order line."),
                'purchase_ok': fields.boolean('Can be Purchased', help="Determine if the product is visible in the list of products within a selection from a purchase order line."),
index b01215a..ab86209 100644 (file)
                                                        <field name="warranty"/>
                                                        <field name="rental" select="1"/>
                                                        <field name="list_price"/>
+                                                       <newline/>
                                                        <field name="standard_price"/>
+                                                       <field name="cost_method"/>
                                                        <!-- 
                                                        <field name="price_margin"/>
                                                        <field name="price_extra"/>
                                                        -->
                                                        <field name="uos_id" />
                                                        <field name="uos_coeff" />
-                                                       <field name="cost_method"/>
                                                        <newline />
                                                        <field name="seller_ids" colspan="4" select="1" nolabel="1" widget="one2many_list"/>
                                                </page>
index d0e7a81..c4dc0a0 100644 (file)
@@ -31,3 +31,4 @@ import partner
 import stock
 import wizard
 import report
+import stock
index 822508b..955ea80 100644 (file)
@@ -93,7 +93,7 @@ class purchase_order(osv.osv):
                'validator' : fields.many2one('res.users', 'Validated by', readonly=True),
                'notes': fields.text('Notes'),
                'invoice_id': fields.many2one('account.invoice', 'Invoice', readonly=True),
-               'picking_ids': fields.one2many('stock.picking', 'purchase_id', 'Picking List', readonly=True, help="This is the list of picking list that have been generated for this invoice"),
+               'picking_ids': fields.one2many('stock.picking', 'purchase_id', 'Picking List', readonly=True, help="This is the list of picking list that have been generated for this purchase"),
                'shipped':fields.boolean('Received', readonly=True, select=True),
                'invoiced':fields.boolean('Invoiced & Paid', readonly=True, select=True),
                'invoice_method': fields.selection([('manual','Manual'),('order','From order'),('picking','From picking')], 'Invoicing method', required=True),
@@ -231,7 +231,7 @@ class purchase_order(osv.osv):
                                'type': 'in',
                                'address_id': order.dest_address_id.id or order.partner_address_id.id,
                                'invoice_state': istate,
-                               'purchase_id': order.id
+                               'purchase_id': order.id,
                        })
                        for order_line in order.order_line:
                                if not order_line.product_id:
@@ -250,7 +250,8 @@ class purchase_order(osv.osv):
                                                'location_dest_id': dest,
                                                'picking_id': picking_id,
                                                'move_dest_id': order_line.move_dest_id.id,
-                                               'state': 'assigned'
+                                               'state': 'assigned',
+                                               'purchase_line_id': order_line.id,
                                        })
                                        if order_line.move_dest_id:
                                                self.pool.get('stock.move').write(cr, uid, [order_line.move_dest_id.id], {'location_id':order.location_id.id})
@@ -265,7 +266,7 @@ class purchase_order(osv.osv):
                        'shipped':False,
                        'invoiced':False,
                        'invoice_id':False,
-                       'picking_ids':[],
+                       'picking_ids':False,
                        'name': self.pool.get('ir.sequence').get(cr, uid, 'purchase.order'),
                })
                return super(purchase_order, self).copy(cr, uid, id, default, context)
index 2cfd521..a0e616e 100644 (file)
 
 from osv import osv, fields
 
+class stock_move(osv.osv):
+       _inherit = 'stock.move'
+       _columns = {
+               'purchase_line_id': fields.many2one('purchase.order.line', 'Purchase Order Line', ondelete='set null', select=True),
+       }
+       _defaults = {
+               'purchase_line_id': lambda *a:False
+       }
+stock_move()
+
 #
 # Inherit of picking to add the link to the PO
 #
index 7a11a50..7407b88 100644 (file)
@@ -56,8 +56,20 @@ def _get_moves(self, cr, uid, data, context):
                quantity = m.product_qty
                if m.state<>'assigned':
                        quantity = 0
-               _moves_arch_lst.append('<field name="move%s" />\n<newline />' % (m.id,))
+               _moves_arch_lst.append('<field name="move%s" />' % (m.id,))
                _moves_fields['move%s' % m.id] = {'string' : '%s - %s' % (m.product_id.code, m.product_id.name), 'type' : 'float', 'required' : True, 'default' : make_default(quantity)}
+               if (pick.type == 'in') and (m.product_id.cost_method == 'average'):
+                       price=0
+                       if hasattr(m, 'purchase_line_id') and m.purchase_line_id:
+                               price=m.purchase_line_id.price_unit
+                       currency=0
+                       if hasattr(pick, 'purchase_id') and pick.purchase_id:
+                               currency=pick.purchase_id.pricelist_id.currency_id.id
+                       _moves_arch_lst.append('<group><field name="price%s"/>' % (m.id,))
+                       _moves_fields['price%s' % m.id] = {'string': 'Unit Price', 'type': 'float', 'required': True, 'default': make_default(price)}
+                       _moves_arch_lst.append('<field name="currency%d"/></group>' % (m.id,))
+                       _moves_fields['currency%s' % m.id] = {'string': 'Currency', 'type': 'many2one', 'relation': 'res.currency', 'required': True, 'default': make_default(currency)}
+               _moves_arch_lst.append('<newline/>')
                res.setdefault('moves', []).append(m.id)
        _moves_arch_lst.append('</form>')
        _moves_arch.string = '\n'.join(_moves_arch_lst)
@@ -78,6 +90,21 @@ def _do_split(self, cr, uid, data, context):
                        too_few.append(move)
                else:
                        too_many.append(move)
+               if (pick.type == 'in') and (move.product_id.cost_method == 'average'):
+                       product_obj = pooler.get_pool(cr.dbname).get('product.product')
+                       currency_obj = pooler.get_pool(cr.dbname).get('res.currency')
+                       users_obj = pooler.get_pool(cr.dbname).get('res.users')
+
+                       product = product_obj.browse(cr, uid, [move.product_id.id])[0]
+                       user = users_obj.browse(cr, uid, [uid])[0]
+
+                       qty = data['form']['move%s' % move.id]
+                       price = data['form']['price%s' % move.id]
+                       currency = data['form']['currency%s' % move.id]
+
+                       new_price = currency_obj.compute(cr, uid, currency, user.company_id.currency_id.id, price)
+                       new_std_price = ((product.standard_price * product.qty_available) + (new_price * qty))/(product.qty_available + qty)
+                       product_obj.write(cr, uid, [product.id], {'standard_price': new_std_price})
 
        for move in too_few:
                if not new_picking: