Add cost value to location / product contextualized views

review246002
This commit is contained in:
Nicolas Évrard committed 2012-03-04 16:09:08 +01:00
1 parent 4cc2d7648e
commit 5f2b135b09
3 files changed
+43 -7

No files matched your search

+19
View File
@@ -1,6 +1,7 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
import datetime
from decimal import Decimal
from trytond.model import ModelView, ModelSQL, fields
from trytond.wizard import Wizard, StateView, Button, StateAction
from trytond.backend import TableHandler
@@ -82,6 +83,8 @@ class Location(ModelSQL, ModelView):
quantity = fields.Function(fields.Float('Quantity'), 'get_quantity')
forecast_quantity = fields.Function(fields.Float('Forecast Quantity'),
'get_quantity')
cost_value = fields.Function(fields.Numeric('Cost Value'),
'get_cost_value')
def __init__(self):
super(Location, self).__init__()
@@ -173,6 +176,22 @@ class Location(ModelSQL, ModelView):
return dict([(loc,qty) for (loc,prod), qty in pbl])
def get_cost_value(self, ids, name):
product_obj = Pool().get('product.product')
trans_context = Transaction().context
product_id = trans_context.get('product')
if not product_id:
return dict((id, False) for id in ids)
cost_values, context = {}, {}
if 'stock_date_end' in trans_context:
context['_datetime'] = trans_context['stock_date_end']
with Transaction().set_context(context):
product = product_obj.browse(product_id)
for location in self.browse(ids):
cost_values[location.id] = (Decimal(str(location.quantity))
* product.cost_price)
return cost_values
def _set_warehouse_parent(self, locations):
'''
Set the parent of child location of warehouse if not set
+21 -6
View File
@@ -1,6 +1,7 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
import datetime
from decimal import Decimal
from trytond.model import ModelView, ModelSQL, fields
from trytond.wizard import Wizard, StateView, StateAction, Button
from trytond.pyson import PYSONEncoder
@@ -12,17 +13,19 @@ from trytond.pool import Pool
class Template(ModelSQL, ModelView):
_name = "product.template"
quantity = fields.Function(fields.Float('Quantity'), 'get_quantity')
quantity = fields.Function(fields.Float('Quantity'), 'sum_product')
forecast_quantity = fields.Function(fields.Float('Forecast Quantity'),
'get_quantity')
'sum_product')
cost_value = fields.Function(fields.Numeric('Cost Value'),
'sum_product')
def get_quantity(self, ids, name):
def sum_product(self, ids, name):
res = {}
if name not in ('quantity', 'forecast_quantity'):
if name not in ('quantity', 'forecast_quantity', 'cost_value'):
raise Exception('Bad argument')
for template in self.browse(ids):
res[template.id] = 0.0
res[template.id] = 0. if name != 'cost_value' else Decimal(0)
for product in template.products:
res[template.id] += product[name]
return res
@@ -66,6 +69,8 @@ class Product(ModelSQL, ModelView):
searcher='search_quantity')
forecast_quantity = fields.Function(fields.Float('Forecast Quantity'),
'get_quantity', searcher='search_quantity')
cost_value = fields.Function(fields.Numeric('Cost Value'),
'get_cost_value')
def get_quantity(self, ids, name):
date_obj = Pool().get('ir.date')
@@ -139,7 +144,17 @@ class Product(ModelSQL, ModelView):
if self._search_quantity_eval_domain(line, domain)]
return [('id', 'in', res)]
def get_cost_value(self, ids, name):
cost_values = {}
context = {}
trans_context = Transaction().context
if 'stock_date_end' in context:
context['_datetime'] = trans_context['stock_date_end']
with Transaction().set_context(context):
for product in self.browse(ids):
cost_values[product.id] = (Decimal(str(product.quantity))
* product.cost_price)
return cost_values
def products_by_location(self, location_ids, product_ids=None,
with_childs=False, skip_zero=True):
+3 -1
View File
@@ -13,6 +13,7 @@ this repository contains the full copyright notices and license terms. -->
<field name="name"/>
<field name="code"/>
<field name="quantity"/>
<field name="cost_value"/>
<field name="forecast_quantity"/>
<field name="default_uom"/>
<field name="type"/>
@@ -33,6 +34,7 @@ this repository contains the full copyright notices and license terms. -->
<field name="name"/>
<field name="quantity"/>
<field name="forecast_quantity"/>
<field name="cost_value"/>
<field name="parent" tree_invisible="1"/>
<field name="childs" tree_invisible="1"/>
</tree>
@@ -41,7 +43,7 @@ this repository contains the full copyright notices and license terms. -->
</record>
<record model="ir.action.act_window" id="act_location_quantity_tree">
<field name="name">Location Quantity</field>
<field name="name">Location Quantity &amp; Cost Value</field>
<field name="res_model">stock.location</field>
<field name="domain">[('parent', '=', False)]</field>
<field name="window_name" eval="True"/>