[MERGE]with lp:~openerp-dev/openobject-addons/trunk-wms

bzr revid: prajapatijitendra7969@gmail.com-20131209085230-l6ti4c2cqrfhch00
This commit is contained in:
Jitendra Prajapati (OpenERP)
2013-12-09 14:22:30 +05:30
5 changed files with 108 additions and 82 deletions
+64 -1
View File
@@ -21,6 +21,7 @@
import math
import re
import time
from _common import rounding
@@ -294,6 +295,37 @@ class product_category(osv.osv):
]
class prices_history(osv.osv):
"""
Keep track of the ``product.template`` standard prices as they are changed.
"""
_name = 'prices.history'
_rec_name = 'datetime'
_order = 'datetime desc'
_columns = {
'company_id': fields.many2one('res.company', required=True),
'product_template_id': fields.many2one('product.template', 'Product Template', required=True),
'datetime': fields.datetime('Historization Time'),
'cost': fields.float('Historized Cost'),
'reason': fields.char('Reason'),
}
def _get_default_company(self, cr, uid, context=None):
if 'force_company' in context:
return context['force_company']
else:
company = self.pool['res.users'].browse(cr, uid, uid,
context=context).company_id
return company.id if company else False
_defaults = {
'datetime': fields.datetime.now,
'company_id': _get_default_company,
}
#----------------------------------------------------------
# Products
@@ -365,13 +397,33 @@ class product_template(osv.osv):
return {'value': {'uom_po_id': uom_id}}
return {}
def create(self, cr, uid, vals, context=None):
''' Store the initial standard price in order to be able to retrieve the cost of a product template for a given date'''
product_template_id = super(product_template, self).create(cr, uid, vals, context=context)
price_history_obj = self.pool['prices.history']
price_history_obj.create(cr, uid, {
'product_template_id': product_template_id,
'cost': vals.get('standard_price', 0.0),
'reason': _('Product template created and standard price set'),
}, context=context)
return product_template_id
def write(self, cr, uid, ids, vals, context=None):
''' Store the standard price change in order to be able to retrieve the cost of a product template for a given date'''
if 'uom_po_id' in vals:
new_uom = self.pool.get('product.uom').browse(cr, uid, vals['uom_po_id'], context=context)
for product in self.browse(cr, uid, ids, context=context):
old_uom = product.uom_po_id
if old_uom.category_id.id != new_uom.category_id.id:
raise osv.except_osv(_('Unit of Measure categories Mismatch!'), _("New Unit of Measure '%s' must belong to same Unit of Measure category '%s' as of old Unit of Measure '%s'. If you need to change the unit of measure, you may deactivate this product from the 'Procurements' tab and create a new one.") % (new_uom.name, old_uom.category_id.name, old_uom.name,))
if 'standard_price' in vals:
price_history_obj = self.pool['prices.history']
for prod_template_id in ids:
price_history_obj.create(cr, uid, {
'product_template_id': prod_template_id,
'cost': vals['standard_price'],
'reason': _('standard price is changed.'),
}, context=context)
return super(product_template, self).write(cr, uid, ids, vals, context=context)
_defaults = {
@@ -419,9 +471,20 @@ class product_product(osv.osv):
context = {}
res = super(product_product, self).view_header_get(cr, uid, view_id, view_type, context)
if (context.get('categ_id', False)):
return _('Products: ')+self.pool.get('product.category').browse(cr, uid, context['categ_id'], context=context).name
return _('Products: ') + self.pool.get('product.category').browse(cr, uid, context['categ_id'], context=context).name
return res
def get_history_price(self, cr, uid, product_id, company_id, context=None):
if context is None:
context = {}
product = self.browse(cr, uid, product_id, context=context)
date = context.get('history_date', time.strftime('%Y-%m-%d %H:%M:%s'))
prices_history_obj = self.pool.get('prices.history')
history_ids = prices_history_obj.search(cr, uid, [('company_id', '=', company_id), ('product_template_id', '=', product.product_tmpl_id.id), ('datetime', '<=', date)], limit=1)
if history_ids:
return prices_history_obj.read(cr, uid, history_ids[0], ['cost'], context=context)['cost']
raise osv.except_osv(_('Error!'), _("No standard price associated for product %s for the given date" % (product.name)))
def _product_price(self, cr, uid, ids, name, arg, context=None):
res = {}
if context is None:
+2 -3
View File
@@ -1254,7 +1254,7 @@ class stock_move(osv.osv):
'priority': fields.selection([('0', 'Not urgent'), ('1', 'Urgent')], 'Priority'),
'create_date': fields.datetime('Creation Date', readonly=True, select=True),
'date': fields.datetime('Date', required=True, select=True, help="Move date: scheduled date until move is done, then date of actual move processing", states={'done': [('readonly', True)]}),
'date_expected': fields.datetime('Scheduled Date', states={'done': [('readonly', True)]}, required=True, select=True, help="Scheduled date for the processing of this move"),
'date_expected': fields.datetime('Expected Date', states={'done': [('readonly', True)]}, required=True, select=True, help="Scheduled date for the processing of this move"),
'product_id': fields.many2one('product.product', 'Product', required=True, select=True, domain=[('type', '<>', 'service')], states={'done': [('readonly', True)]}),
# TODO: improve store to add dependency on product UoM
'product_qty': fields.function(_quantity_normalize, type='float', store=True, string='Quantity',
@@ -2532,7 +2532,6 @@ class stock_warehouse(osv.osv):
output_loc = warehouse.lot_stock_id
picking_type_obj.write(cr, uid, warehouse.in_type_id.id, {'default_location_dest_id': input_loc.id}, context=context)
picking_type_obj.write(cr, uid, warehouse.out_type_id.id, {'default_location_src_id': output_loc.id}, context=context)
picking_type_obj.write(cr, uid, warehouse.int_type_id.id, {'active': new_reception_step != 'one_step'}, context=context)
picking_type_obj.write(cr, uid, warehouse.pick_type_id.id, {'active': new_delivery_step != 'ship_only'}, context=context)
picking_type_obj.write(cr, uid, warehouse.pack_type_id.id, {'active': new_delivery_step == 'pick_pack_ship'}, context=context)
@@ -2666,7 +2665,7 @@ class stock_warehouse(osv.osv):
'sequence_id': int_seq_id,
'default_location_src_id': wh_stock_loc.id,
'default_location_dest_id': wh_stock_loc.id,
'active': reception_steps != 'one_step',
'active': True,
'pack': False,
'color': color}, context=context)
pack_type_id = picking_type_obj.create(cr, uid, vals={
-68
View File
@@ -21,55 +21,10 @@
from openerp.osv import fields, osv
from openerp.tools.translate import _
import time
class prices_history(osv.osv):
"""
Keep track of the ``product.product`` standard prices as they are changed.
"""
_name = 'prices.history'
_rec_name = 'datetime'
_order = 'datetime desc'
_columns = {
'company_id': fields.many2one('res.company', required=True),
'product_id': fields.many2one('product.product', 'Product', required=True),
'datetime': fields.datetime('Historization Time'),
'cost': fields.float('Historized Cost'),
'reason': fields.char('Reason'),
# TODO 'origin': openerp.osv.fields.reference(),
#'quant_id': openerp.osv.fields.many2one('stock.quant'),
}
def _get_default_company(self, cr, uid, context=None):
if 'force_company' in context:
return context['force_company']
else:
company = self.pool['res.users'].browse(cr, uid, uid,
context=context).company_id
return company.id if company else False
_defaults = {
#'quant_id': False,
'datetime': fields.datetime.now,
'company_id': _get_default_company,
}
class product_product(osv.osv):
_inherit = "product.product"
def get_history_price(self, cr, uid, product_id, company_id, context=None):
if context is None:
context = {}
date = context.get('history_date', time.strftime('%Y-%m-%d %H:%M:%s'))
prices_history_obj = self.pool.get('prices.history')
history_ids = prices_history_obj.search(cr, uid, [('company_id', '=', company_id), ('product_id', '=', product_id), ('datetime', '<=', date)], limit=1)
if history_ids:
return prices_history_obj.read(cr, uid, history_ids[0], ['cost'], context=context)['cost']
raise osv.except_osv(_('Error!'), _("No standard price associated for product with ID %d for the given date" % (product_id)))
def get_product_accounts(self, cr, uid, product_id, context=None):
""" To get the stock input account, stock output account and stock journal related to product.
@param product_id: product id
@@ -210,29 +165,6 @@ class product_product(osv.osv):
return move_ids
def create(self, cr, uid, vals, context=None):
''' Store the initial standard price in order to be able to retrieve the cost of a product for a given date (to make stock valuation at date)'''
product_id = super(product_product, self).create(cr, uid, vals, context=context)
price_history_obj = self.pool['prices.history']
price_history_obj.create(cr, uid, {
'product_id': product_id,
'cost': vals.get('standard_price', 0.0),
'reason': _('Product created and standard price set'),
}, context=context)
return product_id
def write(self, cr, uid, ids, values, context=None):
''' Store the standard price change in order to be able to retrieve the cost of a product for a given date (to make stock valuation at date)'''
if 'standard_price' in values:
price_history_obj = self.pool['prices.history']
for product in self.browse(cr, uid, ids, context=context):
price_history_obj.create(cr, uid, {
'product_id': product.id,
'cost': values['standard_price'],
'reason': _('standard price is changed.'),
}, context=context)
return super(product_product, self).write(cr, uid, ids, values, context=context)
_columns = {
'valuation': fields.property(type='selection', selection=[('manual_periodic', 'Periodical (manual)'),
('real_time', 'Real Time (automated)')], string='Inventory Valuation',
@@ -21,7 +21,8 @@ class wizard_valuation_history(osv.osv_memory):
data = self.read(cr, uid, ids, context=context)[0]
ctx = context.copy()
ctx['history_date'] = data['date']
ctx['group_by'] = ['product_id']
ctx['search_default_group_by_product'] = True
ctx['search_default_group_by_location'] = True
return {
'domain': "[('date', '<=', '" + data['date'] + "')]",
'name': _('Stock Value At Date'),
@@ -36,6 +37,7 @@ class wizard_valuation_history(osv.osv_memory):
class stock_history(osv.osv):
_name = 'stock.history'
_auto = False
_order = 'date asc'
def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False):
res = super(stock_history, self).read_group(cr, uid, domain, fields, groupby, offset=offset, limit=limit, context=context, orderby=orderby)
@@ -54,7 +56,7 @@ class stock_history(osv.osv):
res = {}
for line in self.browse(cr, uid, ids, context=context):
if line.product_id.cost_method == 'real':
res[line.id] = line.quantity * line.price_unit_on_move
res[line.id] = line.quantity * line.price_unit_on_quant
else:
res[line.id] = line.quantity * product_obj.get_history_price(cr, uid, line.product_id.id, line.company_id.id, context=context)
return res
@@ -68,7 +70,7 @@ class stock_history(osv.osv):
'product_categ_id': fields.many2one('product.category', 'Product Category', required=True),
'quantity': fields.integer('Quantity'),
'date': fields.datetime('Operation Date'),
'price_unit_on_move': fields.float('Value'),
'price_unit_on_quant': fields.float('Value'),
'cost_method': fields.char('Cost Method'),
'inventory_value': fields.function(_get_inventory_value, string="Inventory Value", type='float', readonly=True),
}
@@ -77,23 +79,52 @@ class stock_history(osv.osv):
tools.drop_view_if_exists(cr, 'stock_history')
cr.execute("""
CREATE OR REPLACE VIEW stock_history AS (
SELECT
stock_move.id AS id,
(SELECT
stock_move.id::text || '-' || quant.id::text AS id,
stock_move.id AS move_id,
stock_move.location_dest_id AS location_id,
stock_move.product_id AS product_id,
product_template.categ_id AS product_categ_id,
stock_move.product_qty AS quantity,
quant.qty AS quantity,
stock_move.date AS date,
ir_property.value_text AS cost_method,
stock_move.price_unit as price_unit_on_move
quant.cost as price_unit_on_quant
FROM
stock_move
LEFT JOIN
stock_quant quant ON quant.id IN (SELECT quant_id FROM stock_quant_move_rel WHERE move_id = stock_move.id)
LEFT JOIN
stock_location location ON stock_move.location_dest_id = location.id
LEFT JOIN
product_product ON product_product.id = stock_move.product_id
LEFT JOIN
product_template ON product_template.id = product_product.product_tmpl_id
LEFT JOIN
ir_property ON (ir_property.name = 'cost_method' and ir_property.res_id = 'product.template,' || product_template.id::text)
WHERE stock_move.state = 'done'
WHERE stock_move.state = 'done' AND location.usage = 'internal'
) UNION
(SELECT
'-' || stock_move.id::text || '-' || quant.id::text AS id,
stock_move.id AS move_id,
stock_move.location_id AS location_id,
stock_move.product_id AS product_id,
product_template.categ_id AS product_categ_id,
- quant.qty AS quantity,
stock_move.date AS date,
ir_property.value_text AS cost_method,
quant.cost as price_unit_on_quant
FROM
stock_move
LEFT JOIN
stock_quant quant ON quant.id IN (SELECT quant_id FROM stock_quant_move_rel WHERE move_id = stock_move.id)
LEFT JOIN
stock_location location ON stock_move.location_id = location.id
LEFT JOIN
product_product ON product_product.id = stock_move.product_id
LEFT JOIN
product_template ON product_template.id = product_product.product_tmpl_id
LEFT JOIN
ir_property ON (ir_property.name = 'cost_method' and ir_property.res_id = 'product.template,' || product_template.id::text)
WHERE stock_move.state = 'done' AND location.usage = 'internal'
)
)""")
@@ -38,7 +38,7 @@
<field name="name">Stock Value At Date</field>
<field name="model">stock.history</field>
<field name="arch" type="xml">
<tree string="Stock Value At Date">
<tree string="Stock Value At Date" create="0">
<field name="location_id" invisible="1"/>
<field name="product_id" invisible="1"/>
<field name="move_id"/>
@@ -60,7 +60,8 @@
<group expand="1" string="Group By...">
<filter string="Product" icon="terp-accessories-archiver" context="{'group_by':'product_id'}" name='group_by_product'/>
<filter string="Product Category" icon="terp-accessories-archiver" context="{'group_by':'product_categ_id'}"/>
<filter string="Location" icon="terp-accessories-archiver" context="{'group_by':'location_id'}"/>
<filter string="Location" icon="terp-accessories-archiver" context="{'group_by':'location_id'}" name='group_by_location'/>
<filter string="Move" icon="terp-accessories-archiver" context="{'group_by':'move_id'}"/>
<filter string="Company" icon="terp-go-home" context="{'group_by':'company_id'}" groups="base.group_multi_company"/>
</group>
</search>