From 5f2b135b09ee3323ea8b5a9dd3128bafddfcef8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20=C3=89vrard?= Date: Sun, 4 Mar 2012 16:09:08 +0100 Subject: [PATCH] Add cost value to location / product contextualized views review246002 --- location.py | 19 +++++++++++++++++++ product.py | 27 +++++++++++++++++++++------ product.xml | 4 +++- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/location.py b/location.py index 123072bcf7..e00c231a76 100644 --- a/location.py +++ b/location.py @@ -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 diff --git a/product.py b/product.py index 1e6378d478..4845e8c515 100644 --- a/product.py +++ b/product.py @@ -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): diff --git a/product.xml b/product.xml index 3a4dbf9bcc..d73374ced3 100644 --- a/product.xml +++ b/product.xml @@ -13,6 +13,7 @@ this repository contains the full copyright notices and license terms. --> + @@ -33,6 +34,7 @@ this repository contains the full copyright notices and license terms. --> + @@ -41,7 +43,7 @@ this repository contains the full copyright notices and license terms. --> - Location Quantity + Location Quantity & Cost Value stock.location [('parent', '=', False)]