stock: fix account move for product with average price
add a column price_unit to stock.move to compute the account move line with the right price bzr revid: ced-c07540545d5966bbd0d6b48b54547c6fa9933625
This commit is contained in:
parent
4e9a0c1ef3
commit
7ffec71af2
|
@ -32,6 +32,7 @@ import time
|
|||
import netsvc
|
||||
from osv import fields,osv
|
||||
import ir
|
||||
from tools import config
|
||||
|
||||
|
||||
#----------------------------------------------------------
|
||||
|
@ -669,6 +670,8 @@ class stock_move(osv.osv):
|
|||
'note': fields.text('Notes'),
|
||||
|
||||
'state': fields.selection([('draft','Draft'),('waiting','Waiting'),('confirmed','Confirmed'),('assigned','Assigned'),('done','Done'),('cancel','cancel')], 'State', readonly=True, select=True),
|
||||
'price_unit': fields.float('Unit Price',
|
||||
digits=(16, int(config['price_accuracy']))),
|
||||
}
|
||||
_defaults = {
|
||||
'state': lambda *a: 'draft',
|
||||
|
@ -820,19 +823,43 @@ class stock_move(osv.osv):
|
|||
journal_id = move.product_id.categ_id.property_stock_journal[0]
|
||||
if acc_src != acc_dest:
|
||||
ref = move.picking_id and move.picking_id.name or False
|
||||
amount = move.product_qty * move.product_id.standard_price
|
||||
|
||||
if move.product_id.cost_method == 'average' and move.price_unit:
|
||||
amount = move.product_qty * move.price_unit
|
||||
else:
|
||||
amount = move.product_qty * move.product_id.standard_price
|
||||
|
||||
date = time.strftime('%Y-%m-%d')
|
||||
lines = [
|
||||
(0,0,{'name': move.name, 'quantity': move.product_qty, 'credit': amount, 'account_id': acc_src, 'ref': ref}),
|
||||
(0,0,{'name': move.name, 'quantity': move.product_qty, 'debit': amount, 'account_id': acc_dest, 'ref': ref})
|
||||
(0, 0, {
|
||||
'name': move.name,
|
||||
'quantity': move.product_qty,
|
||||
'credit': amount,
|
||||
'account_id': acc_src,
|
||||
'ref': ref,
|
||||
'date': date}),
|
||||
(0, 0, {
|
||||
'name': move.name,
|
||||
'quantity': move.product_qty,
|
||||
'debit': amount,
|
||||
'account_id': acc_dest,
|
||||
'ref': ref,
|
||||
'date': date})
|
||||
]
|
||||
self.pool.get('account.move').create(cr, uid, {'name': move.name, 'journal_id': journal_id, 'line_id': lines, 'ref': ref})
|
||||
self.pool.get('account.move').create(cr, uid,
|
||||
{
|
||||
'name': move.name,
|
||||
'journal_id': journal_id,
|
||||
'line_id': lines,
|
||||
'ref': ref,
|
||||
})
|
||||
self.write(cr, uid, ids, {'state':'done'})
|
||||
|
||||
wf_service = netsvc.LocalService("workflow")
|
||||
for id in ids:
|
||||
wf_service.trg_trigger(uid, 'stock.move', id, cr)
|
||||
|
||||
return True
|
||||
|
||||
stock_move()
|
||||
|
||||
class stock_inventory(osv.osv):
|
||||
|
|
|
@ -38,7 +38,10 @@ from osv import osv
|
|||
_moves_arch = UpdateableStr()
|
||||
_moves_fields = {}
|
||||
|
||||
_moves_arch_end = '''<?xml version="1.0"?><form string="Packing result"><label string="The packing has been successfully made !" colspan="4" /></form>'''
|
||||
_moves_arch_end = '''<?xml version="1.0"?>
|
||||
<form string="Packing result">
|
||||
<label string="The packing has been successfully made !" colspan="4"/>
|
||||
</form>'''
|
||||
_moves_fields_end = {}
|
||||
|
||||
def make_default(val):
|
||||
|
@ -50,28 +53,47 @@ def _get_moves(self, cr, uid, data, context):
|
|||
pick_obj = pooler.get_pool(cr.dbname).get('stock.picking')
|
||||
pick = pick_obj.browse(cr, uid, [data['id']])[0]
|
||||
res = {}
|
||||
|
||||
_moves_fields.clear()
|
||||
_moves_arch_lst = ['<?xml version="1.0"?>', '<form string="Make packing">']
|
||||
|
||||
for m in pick.move_lines:
|
||||
quantity = m.product_qty
|
||||
if m.state<>'assigned':
|
||||
quantity = 0
|
||||
|
||||
_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)}
|
||||
_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 col="6"><field name="uom%s"/><field name="price%s"/>' % (m.id,m.id,))
|
||||
_moves_fields['price%s' % m.id] = {'string': 'Unit Price', 'type': 'float', 'required': True, 'default': make_default(price)}
|
||||
_moves_fields['uom%s' % m.id] = {'string': 'UOM', 'type': 'many2one', 'relation': 'product.uom', 'required': True, 'default': make_default(m.product_uom.id)}
|
||||
|
||||
_moves_arch_lst.append('<group col="6"><field name="uom%s"/>\
|
||||
<field name="price%s"/>' % (m.id,m.id,))
|
||||
|
||||
_moves_fields['price%s' % m.id] = {'string': 'Unit Price',
|
||||
'type': 'float', 'required': True, 'default': make_default(price)}
|
||||
|
||||
_moves_fields['uom%s' % m.id] = {'string': 'UOM', 'type': 'many2one',
|
||||
'relation': 'product.uom', 'required': True,
|
||||
'default': make_default(m.product_uom.id)}
|
||||
|
||||
_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_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)
|
||||
return res
|
||||
|
@ -92,9 +114,7 @@ def _do_split(self, cr, uid, data, context):
|
|||
else:
|
||||
too_many.append(move)
|
||||
|
||||
#
|
||||
# Average price computation
|
||||
#
|
||||
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')
|
||||
|
@ -112,23 +132,54 @@ def _do_split(self, cr, uid, data, context):
|
|||
qty = uom_obj._compute_qty(cr, uid, uom, qty, product.uom_id.id)
|
||||
|
||||
if qty > 0:
|
||||
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})
|
||||
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})
|
||||
move_obj.write(cr, uid, [move.id], {'price_unit': new_price})
|
||||
|
||||
for move in too_few:
|
||||
if not new_picking:
|
||||
new_picking = pick_obj.copy(cr, uid, pick.id, {'name' : '%s (splitted)' % pick.name, 'move_lines' : [], 'state':'draft'})
|
||||
new_obj = move_obj.copy(cr, uid, move.id, {'product_qty' : data['form']['move%s' % move.id], 'product_uos_qty':data['form']['move%s' % move.id], 'picking_id' : new_picking, 'state': 'assigned', 'move_dest_id': False})
|
||||
move_obj.write(cr, uid, [move.id], {'product_qty' : move.product_qty - data['form']['move%s' % move.id], 'product_uos_qty':move.product_qty - data['form']['move%s' % move.id]})
|
||||
new_picking = pick_obj.copy(cr, uid, pick.id,
|
||||
{
|
||||
'name' : '%s (splitted)' % pick.name,
|
||||
'move_lines' : [],
|
||||
'state':'draft'
|
||||
})
|
||||
new_obj = move_obj.copy(cr, uid, move.id,
|
||||
{
|
||||
'product_qty' : data['form']['move%s' % move.id],
|
||||
'product_uos_qty':data['form']['move%s' % move.id],
|
||||
'picking_id' : new_picking,
|
||||
'state': 'assigned',
|
||||
'move_dest_id': False,
|
||||
'price_unit': False,
|
||||
})
|
||||
move_obj.write(cr, uid, [move.id],
|
||||
{
|
||||
'product_qty' : move.product_qty - data['form']['move%s' % move.id],
|
||||
'product_uos_qty':move.product_qty - data['form']['move%s' % move.id],
|
||||
})
|
||||
|
||||
if new_picking:
|
||||
move_obj.write(cr, uid, [c.id for c in complete], {'picking_id': new_picking})
|
||||
for move in too_many:
|
||||
move_obj.write(cr, uid, [move.id], {'product_qty' : data['form']['move%s' % move.id],'product_uos_qty':data['form']['move%s' % move.id] , 'picking_id': new_picking})
|
||||
move_obj.write(cr, uid, [move.id],
|
||||
{
|
||||
'product_qty' : data['form']['move%s' % move.id],
|
||||
'product_uos_qty': data['form']['move%s' % move.id],
|
||||
'picking_id': new_picking,
|
||||
})
|
||||
else:
|
||||
for move in too_many:
|
||||
move_obj.write(cr, uid, [move.id], {'product_qty' : data['form']['move%s' % move.id],'product_uos_qty':data['form']['move%s' % move.id]})
|
||||
move_obj.write(cr, uid, [move.id],
|
||||
{
|
||||
'product_qty': data['form']['move%s' % move.id],
|
||||
'product_uos_qty': data['form']['move%s' % move.id]
|
||||
})
|
||||
|
||||
# At first we confirm the new picking (if necessary)
|
||||
wf_service = netsvc.LocalService("workflow")
|
||||
|
@ -144,20 +195,31 @@ def _do_split(self, cr, uid, data, context):
|
|||
wf_service.trg_validate(uid, 'stock.picking', pick.id, 'button_done', cr)
|
||||
return {}
|
||||
|
||||
|
||||
class partial_picking(wizard.interface):
|
||||
|
||||
|
||||
states = {
|
||||
'init' : {
|
||||
'actions' : [ _get_moves ],
|
||||
'result' : { 'type' : 'form', 'arch' : _moves_arch, 'fields' : _moves_fields, 'state' : (('end', 'Cancel'),('split', 'Make Picking') )},
|
||||
'init': {
|
||||
'actions': [ _get_moves ],
|
||||
'result': {'type': 'form', 'arch': _moves_arch, 'fields': _moves_fields,
|
||||
'state' : (
|
||||
('end', 'Cancel'),
|
||||
('split', 'Make Picking')
|
||||
)
|
||||
},
|
||||
},
|
||||
'split' : {
|
||||
'actions' : [ _do_split ],
|
||||
'result' : { 'type' : 'state', 'state' : 'end' },
|
||||
'split': {
|
||||
'actions': [ _do_split ],
|
||||
'result': {'type': 'state', 'state': 'end'},
|
||||
},
|
||||
'end2' : {
|
||||
'actions' : [ ],
|
||||
'result' : { 'type' : 'form', 'arch' : _moves_arch_end, 'fields' : _moves_fields_end, 'state' : (('end', 'Close'),) },
|
||||
'end2': {
|
||||
'actions': [ ],
|
||||
'result': {'type': 'form', 'arch': _moves_arch_end,
|
||||
'fields': _moves_fields_end,
|
||||
'state': (
|
||||
('end', 'Close'),
|
||||
)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue