1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6
# Copyright (C) 2010-2010 Camptocamp Austria (<http://www.camptocamp.at>)
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU Affero General Public License as
10
# published by the Free Software Foundation, either version 3 of the
11
# License, or (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU Affero General Public License for more details.
18
# You should have received a copy of the GNU Affero General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
##############################################################################
23
from osv import osv, fields
24
import decimal_precision as dp
27
#from _common import rounding
29
from tools.translate import _
33
#----------------------------------------------------------
35
#----------------------------------------------------------
36
class stock_move(osv.osv):
37
_inherit = "stock.move"
39
def _compute_move_value_cost(self, cr, uid, ids, name, args, context):
40
if not ids : return {}
42
for move in self.browse(cr, uid, ids):
43
print >> sys.stderr,'type cost', move.picking_id.type
44
if move.state in ['done','cancel']: return {}
45
if move.purchase_line_id:
46
result[move.id] = move.product_qty * move.purchase_line_id.price_subtotal
48
loc_id = str(move.location_id.id)
49
print >> sys.stderr, 'loc_id ',loc_id
51
sum( case when location_id = '+loc_id+' then -move_value_cost else 0 end + case when location_dest_id = '+loc_id+' then move_value_cost else 0 end) as sum_amount, \
52
sum( case when location_id = '+loc_id+' then -product_qty else 0 end + case when location_dest_id = '+loc_id+' then product_qty else 0 end) as sum_qty \
54
where product_id = '+ str(move.product_id.id) +' \
55
and state = \'done\' \
56
and (location_id = '+loc_id+' or location_dest_id = '+loc_id+')'
58
sql = sql + ' and prodlot_id = ' + str(move.prodlot_id.id )
59
print >> sys.stderr, 'sql ',sql
61
for r in cr.dictfetchall():
62
sum_amount = r['sum_amount']
63
sum_qty = r['sum_qty']
64
print >> sys.stderr, 'sum ', sum_amount,sum_qty
65
if sum_qty and sum_qty > 0.0 and sum_amount > 0.0:
66
avg_price = sum_amount / sum_qty
67
result[move.id] = move.product_qty * avg_price
69
result[move.id] = move.product_qty * move.product_id.standard_price
73
def _compute_move_value_sale(self, cr, uid, ids, name, args, context):
74
print >> sys.stderr, 'value_sale'
77
for move in self.browse(cr, uid, ids):
78
if move.state in ['done','cancel']: return {}
79
print >> sys.stderr,'type sale', move.picking_id.type
81
result[move.id] = move.product_qty * move.sale_line_id.price_subtotal
82
print >> sys.stderr, 'value_sale', result[move.id]
85
def _period_id(self, cr, uid, ids, name, arg, context):
87
for move in self.browse(cr, uid, ids):
88
#period_ids= self.pool.get('account.period').search(cr,uid,[('date_start','<=',move.date),('date_stop','>=',move.date ), ('special','=',False)])
89
period_ids= self.pool.get('account.period').search(cr,uid,[('date_start','<=',move.date),('date_stop','>=',move.date )])
92
result[move.id] = period_ids[0]
97
'move_value_cost' : fields.function(_compute_move_value_cost, method=True, string='Amount', digits_compute=dp.get_precision('Account'),type='float' , store=True, \
98
help="""Product's cost for accounting valuation.""") ,
99
'move_value_sale' : fields.function(_compute_move_value_sale, method=True, string='Amount Sale', digits_compute=dp.get_precision('Account'),type='float' , store=True, \
100
help="""Product's sale value for accounting valuation.""") ,
101
'period_id' : fields.function(_period_id, method=True, string="Period",type='many2one', relation='account.period', store=True, select="1", ),
102
'price_unit_sale' : fields.float('Unit Price Sale', digits_compute=dp.get_precision('Account') ),
103
'analytic_account_id': fields.many2one('account.analytic.account', 'Analytic Account'),
111
update stock_move m set move_value_cost = (select m.product_qty * p.price_unit from purchase_order_line p
112
where p.id = m.purchase_line_id) where move_value_cost is null;
116
update stock_move m set move_value_sale = (select m.product_qty * l.price_unit from sale_order_line l
117
where l.id = m.sale_line_id) where move_value_sale is null;
121
update stock_move m set move_value_cost = (select m.product_qty * t.standard_price from product_product p, product_template t
122
where p.id = m.product_id and t.id = p.product_tmpl_id) where move_value_cost is null;
130
#----------------------------------------------------------
131
# Company Config INHERIT
132
#----------------------------------------------------------
134
#class res_company(osv.osv):
135
# _inherit = "res.company"
139
# 'valuation':fields.selection([('manual_periodic', 'Periodical (manual)'),
140
# ('real_time','Real Time (automated)'),], 'Inventory Valuation',
141
# help="If real-time valuation is enabled for a product, the system will automatically write journal entries corresponding to stock moves." \
142
# "The inventory variation account set on the product category will represent the current inventory value, and the stock input and stock output account will hold the counterpart moves for incoming and outgoing products."
147
## no connection from product to company !!!!
148
#class product_product(osv.osv):
149
# _inherit = "product.product"
151
# def _get_valuation(self, cr, uid, ids, field_name, arg, context=None):
153
# for res in self.browse(cr, uid, ids, context):
159
# 'valuation': fields.function(_get_valuation, method=True, string="Valuation",type='char',size=128),