[IMP] refactoring of the module to simplify

This commit is contained in:
jbeficent
2016-02-24 00:08:56 +01:00
committed by Jordi Ballester Alomar
parent da2613d4a2
commit c0f3567970
12 changed files with 330 additions and 379 deletions

View File

@@ -21,6 +21,7 @@
"views/stock_inventory_revaluation_view.xml",
"views/product_view.xml",
"data/stock_inventory_revaluation_data.xml",
"wizards/stock_inventory_revaluation_mass_post_view.xml",
],
'installable': True,
}

View File

@@ -12,7 +12,7 @@
<field name="name">Stock Inventory Revaluation</field>
<field name="code">stock.inventory.revaluation</field>
<field name="prefix">IR/</field>
<field name="padding">5</field>
<field name="padding">7</field>
<field name="number_next">1</field>
<field name="number_increment">1</field>
</record>

View File

@@ -23,6 +23,31 @@ class StockInventoryRevaluation(models.Model):
res = self.env['account.journal'].search([('type', '=', 'general')])
return res and res[0] or False
@api.one
def _get_product_template_qty(self):
self.qty_available = 0
for prod_variant in self.product_template_id.product_variant_ids:
self.qty_available += prod_variant.qty_available
@api.one
def _calc_product_template_value(self):
qty_available = 0
current_value = 0.0
quant_obj = self.env['stock.quant']
for prod_variant in self.product_template_id.product_variant_ids:
qty_available += prod_variant.qty_available
if self.product_template_id.cost_method == 'real':
quants = quant_obj.search([('product_id', '=',
prod_variant.id),
('location_id.usage', '=',
'internal')])
for quant in quants:
current_value += quant.cost
else:
current_value = \
self.product_template_id.standard_price * qty_available
self.current_value = current_value
name = fields.Char('Reference',
help="Reference for the journal entry",
readonly=True,
@@ -76,103 +101,6 @@ class StockInventoryRevaluation(models.Model):
readonly=True,
states={'draft': [('readonly', False)]})
line_ids = fields.One2many('stock.inventory.revaluation.line',
'revaluation_id',
string='Revaluation lines',
readonly=False,
states={'posted': [('readonly', True)]})
@api.model
def create(self, values):
sequence_obj = self.env['ir.sequence']
if values.get('name', '/') == '/':
values['name'] = sequence_obj.get('stock.inventory.revaluation')
return super(StockInventoryRevaluation, self).create(values)
@api.one
def post(self):
for line in self.line_ids:
if line.product_template_id.valuation != 'real_time':
continue
line.post()
return True
@api.multi
def button_post(self):
self.post()
self.write({'state': 'posted'})
return True
@api.multi
def button_draft(self):
self.write({'state': 'draft'})
return True
@api.multi
def button_cancel(self):
moves = self.env['account.move']
for line in self.line_ids:
if line.move_id:
moves += line.move_id
for line_quant in line.line_quant_ids:
if line_quant.move_id:
moves += line_quant.move_id
line_quant.quant_id.write({'cost': line_quant.old_cost})
if moves:
# second, invalidate the move(s)
moves.button_cancel()
# delete the move this revaluation was pointing to
# Note that the corresponding move_lines and move_reconciles
# will be automatically deleted too
moves.unlink()
self.write({'state': 'cancel'})
return True
class StockInventoryRevaluationLine(models.Model):
_name = 'stock.inventory.revaluation.line'
_description = 'Inventory revaluation line'
@api.one
def _get_product_template_qty(self):
self.qty_available = 0
for prod_variant in self.product_template_id.product_variant_ids:
self.qty_available += prod_variant.qty_available
@api.one
def _calc_product_template_value(self):
qty_available = 0
current_value = 0.0
quant_obj = self.env['stock.quant']
for prod_variant in self.product_template_id.product_variant_ids:
qty_available += prod_variant.qty_available
if self.product_template_id.cost_method == 'real':
quants = quant_obj.search([('product_id', '=',
prod_variant.id),
('location_id.usage', '=',
'internal')])
for quant in quants:
current_value += quant.cost
else:
current_value = \
self.product_template_id.standard_price * qty_available
self.current_value = current_value
@api.one
@api.depends("product_template_id", "product_template_id.standard_price")
def _calc_current_cost(self):
self.current_cost = self.product_template_id.standard_price
revaluation_id = fields.Many2one('stock.inventory.revaluation',
'Stock Inventory Revaluation',
required=True,
ondelete='cascade')
state = fields.Selection(selection=_STATES,
string='UoM', readonly=True,
related="revaluation_id.state")
product_template_id = fields.Many2one('product.template', 'Product',
required=True,
domain=[('type', '=', 'product')])
@@ -239,26 +167,17 @@ class StockInventoryRevaluationLine(models.Model):
"the transaction created by the revaluation. The Decrease "
"Account is used when the inventory value is decreased.")
company_id = fields.Many2one(
comodel_name='res.company', string='Company', readonly=True,
related="revaluation_id.company_id")
move_id = fields.Many2one('account.move', 'Account move', readonly=True,
copy=False)
move_id = fields.Many2one('account.move', 'Account move', readonly=True)
reval_quant_ids = fields.One2many('stock.inventory.revaluation.quant',
'revaluation_id',
string='Revaluation line quants')
revaluation_type = fields.Selection(
string="Revaluation Type", readonly=True,
related='revaluation_id.revaluation_type',
default='price_change')
line_quant_ids = fields.One2many('stock.inventory.revaluation.line.quant',
'line_id',
string='Revaluation line quants')
_sql_constraints = [
('inv_valu_line_prod_temp_uniq',
'unique (revaluation_id, product_template_id)',
_('Cannot enter the same product multiple times in the same '
'inventory valuation!'))]
@api.one
@api.depends("product_template_id", "product_template_id.standard_price")
def _calc_current_cost(self):
self.current_cost = self.product_template_id.standard_price
@api.one
@api.constrains('product_template_id', 'company_id')
@@ -277,7 +196,6 @@ class StockInventoryRevaluationLine(models.Model):
self.decrease_account_id = self.product_template_id.categ_id and \
self.product_template_id.categ_id.\
property_inventory_revaluation_decrease_account_categ
self.revaluation_type = self.revaluation_id.revaluation_type
@api.model
def _prepare_move_data(self, date_move):
@@ -285,17 +203,17 @@ class StockInventoryRevaluationLine(models.Model):
period = self.env['account.period'].find(date_move)[0]
return {
'narration': self.revaluation_id.remarks,
'narration': self.remarks,
'date': date_move,
'ref': self.revaluation_id.name,
'journal_id': self.revaluation_id.journal_id.id,
'ref': self.name,
'journal_id': self.journal_id.id,
'period_id': period.id,
}
@api.model
def _prepare_debit_move_line_data(self, amount, account_id, prod_id):
return {
'name': self.revaluation_id.name,
'name': self.name,
'date': self.move_id.date,
'product_id': prod_id,
'account_id': account_id,
@@ -306,7 +224,7 @@ class StockInventoryRevaluationLine(models.Model):
@api.model
def _prepare_credit_move_line_data(self, amount, account_id, prod_id):
return {
'name': self.revaluation_id.name,
'name': self.name,
'date': self.move_id.date,
'product_id': prod_id,
'account_id': account_id,
@@ -352,15 +270,15 @@ class StockInventoryRevaluationLine(models.Model):
amount_diff = 0.0
if self.product_template_id.cost_method == 'real':
for line_quant in self.line_quant_ids:
amount_diff += line_quant.get_total_value()
line_quant.write_new_cost()
for reval_quant in self.reval_quant_ids:
amount_diff += reval_quant.get_total_value()
reval_quant.write_new_cost()
if amount_diff == 0.0:
return True
else:
if self.product_template_id.cost_method in ['standard', 'average']:
if self.revaluation_id.revaluation_type == 'price_change':
if self.revaluation_type == 'price_change':
diff = self.current_cost - self.new_cost
amount_diff = self.qty_available * diff
else:
@@ -376,7 +294,7 @@ class StockInventoryRevaluationLine(models.Model):
"is 0 or negative" %
self.product_template_id.name))
if self.revaluation_id.revaluation_type == 'price_change':
if self.revaluation_type == 'price_change':
self.old_cost = self.current_cost
self.product_template_id.write({'standard_price':
self.new_cost})
@@ -391,15 +309,50 @@ class StockInventoryRevaluationLine(models.Model):
if self.product_template_id.valuation == 'real_time':
self._create_accounting_entry(amount_diff)
@api.model
def create(self, values):
sequence_obj = self.env['ir.sequence']
if values.get('name', '/') == '/':
values['name'] = sequence_obj.get('stock.inventory.revaluation')
return super(StockInventoryRevaluation, self).create(values)
class StockInventoryRevaluationLineQuant(models.Model):
@api.multi
def button_post(self):
self.post()
self.write({'state': 'posted'})
return True
_name = 'stock.inventory.revaluation.line.quant'
_description = 'Inventory revaluation line quant'
@api.multi
def button_draft(self):
self.write({'state': 'draft'})
return True
line_id = fields.Many2one('stock.inventory.revaluation.line',
'Revaluation Line', required=True,
readonly=True)
@api.multi
def button_cancel(self):
moves = self.env['account.move']
if self.move_id:
moves += self.move_id
for reval_quant in self.reval_quant_ids:
reval_quant.quant_id.write({'cost': reval_quant.old_cost})
if moves:
# second, invalidate the move(s)
moves.button_cancel()
# delete the move this revaluation was pointing to
# Note that the corresponding move_lines and move_reconciles
# will be automatically deleted too
moves.unlink()
self.write({'state': 'cancel'})
return True
class StockInventoryRevaluationQuant(models.Model):
_name = 'stock.inventory.revaluation.quant'
_description = 'Inventory revaluation quant'
revaluation_id = fields.Many2one('stock.inventory.revaluation',
'Revaluation', required=True,
readonly=True)
quant_id = fields.Many2one('stock.quant', 'Quant', required=True,
readonly=True,
@@ -434,10 +387,14 @@ class StockInventoryRevaluationLineQuant(models.Model):
digits=dp.get_precision('Product Price'),
copy=False)
company_id = fields.Many2one(
comodel_name='res.company', string='Company', readonly=True,
related="revaluation_id.company_id")
def get_total_value(self):
amount_diff = 0.0
if self.product_id.product_tmpl_id.cost_method == 'real':
if self.line_id.revaluation_id.revaluation_type != 'price_change':
if self.revaluation_id.revaluation_type != 'price_change':
raise UserError(_("You can only post quant cost changes."))
else:
diff = self.current_cost - self.new_cost

View File

@@ -1,4 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_stock_inventory_revaluation,stock.inventory.revaluation,model_stock_inventory_revaluation,stock_account.group_inventory_valuation,1,1,1,1
access_stock_inventory_revaluation_line,stock.inventory.revaluation.line,model_stock_inventory_revaluation_line,stock_account.group_inventory_valuation,1,1,1,1
access_stock_inventory_revaluation_line_quant,stock.inventory.revaluation.line.quant,model_stock_inventory_revaluation_line_quant,stock_account.group_inventory_valuation,1,1,1,1
access_stock_inventory_revaluation_quant,stock.inventory.revaluation.quant,model_stock_inventory_revaluation_quant,stock_account.group_inventory_valuation,1,1,1,1
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 access_stock_inventory_revaluation stock.inventory.revaluation model_stock_inventory_revaluation stock_account.group_inventory_valuation 1 1 1 1
3 access_stock_inventory_revaluation_line access_stock_inventory_revaluation_quant stock.inventory.revaluation.line stock.inventory.revaluation.quant model_stock_inventory_revaluation_line model_stock_inventory_revaluation_quant stock_account.group_inventory_valuation 1 1 1 1
access_stock_inventory_revaluation_line_quant stock.inventory.revaluation.line.quant model_stock_inventory_revaluation_line_quant stock_account.group_inventory_valuation 1 1 1 1

View File

@@ -10,9 +10,9 @@
('company_id','child_of',[user.company_id.id])]</field>
</record>
<record model="ir.rule" id="stock_inventory_revaluation_line_comp_rule">
<field name="name">Stock Inventory Revaluation line multi-company</field>
<field name="model_id" ref="model_stock_inventory_revaluation_line"/>
<record model="ir.rule" id="stock_inventory_revaluation_quant_comp_rule">
<field name="name">Stock Inventory Revaluation quant multi-company</field>
<field name="model_id" ref="model_stock_inventory_revaluation_quant"/>
<field name="global" eval="True"/>
<field name="domain_force">['|',('company_id','=',False),
('company_id','child_of',[user.company_id.id])]</field>

View File

@@ -18,13 +18,12 @@ class TestStockInventoryRevaluation(TransactionCase):
self.product_model = self.env['product.product']
self.product_ctg_model = self.env['product.category']
self.reval_model = self.env['stock.inventory.revaluation']
self.reval_line_model = self.env['stock.inventory.revaluation.line']
self.account_model = self.env['account.account']
self.acc_type_model = self.env['account.account.type']
self.reval_line_quant_model = self.\
env['stock.inventory.revaluation.line.quant']
self.reval_quant_model = self.\
env['stock.inventory.revaluation.quant']
self.get_quant_model = self.\
env['stock.inventory.revaluation.line.get.quant']
env['stock.inventory.revaluation.get.quant']
self.stock_change_model = self.env['stock.change.product.qty']
self.stock_lot_model = self.env['stock.production.lot']
self.stock_location_model = self.env['stock.location']
@@ -128,19 +127,9 @@ class TestStockInventoryRevaluation(TransactionCase):
})
return product
def _create_inventory_revaluation(self, journal, revaluation_type):
"""Create a Inventory Revaluation with revaluation_type set to
price_change to recalculate inventory value according to new price."""
inventory = self.reval_model.create({
'name': 'test_inventory_revaluation',
'document_date': datetime.today(),
'revaluation_type': revaluation_type,
'journal_id': journal.id,
})
return inventory
def _create_inventory_revaluation_line(self, revaluation, product):
"""Create a Inventory Revaluation line by applying
def _create_inventory_revaluation(self, journal, revaluation_type,
product):
"""Create a Inventory Revaluation by applying
increase and decrease account to it."""
self.increase_account_id = product.categ_id and \
product.categ_id.\
@@ -149,13 +138,16 @@ class TestStockInventoryRevaluation(TransactionCase):
product.categ_id.\
property_inventory_revaluation_decrease_account_categ
line = self.reval_line_model.create({
reval = self.reval_model.create({
'name': 'test_inventory_revaluation',
'document_date': datetime.today(),
'revaluation_type': revaluation_type,
'journal_id': journal.id,
'product_template_id': product.id,
'revaluation_id': revaluation.id,
'increase_account_id': self.increase_account_id.id,
'decrease_account_id': self.decrease_account_id.id,
})
return line
return reval
def _update_product_qty(self, product, location, quantity):
"""Update Product quantity."""
@@ -167,74 +159,82 @@ class TestStockInventoryRevaluation(TransactionCase):
product_qty.change_product_qty()
return product_qty
def _get_quant(self, date_from, line):
def _get_quant(self, date_from, revaluation):
"""Get Quants for Inventory Revaluation between the date supplied."""
quant = self.get_quant_model.create({
'date_from': date_from,
'date_to': datetime.today(),
})
line_context = {
'active_id': line.id,
'active_ids': line.ids,
'active_model': 'stock.inventory.revaluation.line',
'active_id': revaluation.id,
'active_ids': revaluation.ids,
'active_model': 'stock.inventory.revaluation',
}
quant.with_context(line_context).process()
for line_quant in line.line_quant_ids:
line_quant.new_cost = 8.0
for reval_quant in revaluation.reval_quant_ids:
reval_quant.new_cost = 8.0
def test_inventory_revaluation_price_change(self):
def test_inventory_revaluation_price_change_real(self):
"""Test that the inventory is revaluated when the
inventory price for any product is changed."""
inventory price for a product managed under real costing method is
changed."""
# Create an Inventory Revaluation
# Create an Inventory Revaluation for real cost product
revaluation_type = 'price_change'
invent_price_change = self._create_inventory_revaluation(
self.journal, revaluation_type)
# Create an Inventory Revaluation Line for real cost product
invent_line_real = \
self._create_inventory_revaluation_line(
invent_price_change, self.product_real.product_tmpl_id)
invent_price_change_real = \
self._create_inventory_revaluation(
self.journal, revaluation_type,
self.product_real.product_tmpl_id)
# Create an Inventory Revaluation Line Quant
date_from = date.today() - timedelta(1)
self._get_quant(date_from, invent_line_real)
self._get_quant(date_from, invent_price_change_real)
# Create an Inventory Revaluation Line for average cost product
invent_line_avg = self._create_inventory_revaluation_line(
invent_price_change, self.product_average.product_tmpl_id)
# Post the inventory revaluation
invent_line_avg.new_cost = 8.00
invent_price_change.button_post()
invent_price_change_real.button_post()
expected_result = (10.00 - 8.00) * 20.00
for line in invent_price_change.line_ids:
for move_line in line.move_id.line_id:
if move_line.debit:
self.assertEqual(move_line.debit, expected_result,
'Incorrect inventory revaluation for '
'type Price Change.')
for move_line in invent_price_change_real.move_id.line_id:
if move_line.debit:
self.assertEqual(move_line.debit, expected_result,
'Incorrect inventory revaluation for '
'type Price Change.')
def test_inventory_revaluation_price_change_average(self):
"""Test that the inventory is revaluated when the
inventory price for a product managed under average costing method is
changed."""
revaluation_type = 'price_change'
# Create an Inventory Revaluation for average cost product
invent_price_change_average = self._create_inventory_revaluation(
self.journal, revaluation_type,
self.product_average.product_tmpl_id)
# Post the inventory revaluation
invent_price_change_average.new_cost = 8.00
invent_price_change_average.button_post()
expected_result = (10.00 - 8.00) * 20.00
for move_line in invent_price_change_average.move_id.line_id:
if move_line.debit:
self.assertEqual(move_line.debit, expected_result,
'Incorrect inventory revaluation for '
'type Price Change.')
def test_inventory_revaluation_value_change(self):
"""Test that the inventory is revaluated when the
inventory price for any product is changed."""
# Create an Inventory Revaluation for value change
# Create an Inventory Revaluation for value change for average
# cost product
revaluation_type = 'inventory_value'
invent_inventory_value = self._create_inventory_revaluation(
self.journal, revaluation_type)
# Create an Inventory Revaluation Line for average cost product
invent_line_average = self._create_inventory_revaluation_line(
invent_inventory_value, self.product_average.product_tmpl_id)
invent_line_average.new_value = 100.00
invent_average = self._create_inventory_revaluation(
self.journal, revaluation_type,
self.product_average.product_tmpl_id)
invent_average.new_value = 100.00
# Post the inventory revaluation
invent_inventory_value.button_post()
invent_average.button_post()
for line in invent_inventory_value.line_ids:
for move_line in line.move_id.line_id:
if move_line.debit:
self.assertEqual(move_line.debit, 100.0,
'Incorrect inventory revaluation for '
'type Inventory Debit/Credit.')
for move_line in invent_average.move_id.line_id:
if move_line.debit:
self.assertEqual(move_line.debit, 100.0,
'Incorrect inventory revaluation for '
'type Inventory Debit/Credit.')

View File

@@ -42,101 +42,71 @@
<field name="remarks"/>
</group>
</group>
<notebook>
<page string="Products">
<field name="line_ids" nolabel="1">
<tree string="Stock Inventory Revaluation Lines">
<field name="state" invisible="True"/>
<field name="revaluation_type"
invisible="1"/>
<field name="product_template_id"/>
<field name="uom_id"/>
<field name="cost_method"/>
<field name="current_cost"/>
<field name="old_cost"
attrs="{'invisible':['|', '|', ('revaluation_type', '!=', 'price_change'), ('cost_method','=', 'real'), ('state', '!=', 'posted')]}"/>
<field name="new_cost"
attrs="{'invisible':['|', ('revaluation_type', '!=', 'price_change'), ('cost_method','=', 'real')]}"/>
<field name="old_value"
attrs="{'invisible':['|', '|', ('revaluation_type', '!=', 'inventory_value'), ('cost_method','=', 'real'), ('state', '!=', 'posted')]}"/>
<field name="current_value"/>
<field name="new_value"
attrs="{'invisible':['|', ('revaluation_type', '!=', 'inventory_value'), ('cost_method','=', 'real')]}"/>
<field name="qty_available"/>
<field name="increase_account_id"/>
<field name="decrease_account_id"/>
<field name="move_id"/>
</tree>
<form string="Stock Inventory Revaluation Lines">
<group>
<field name="state" invisible="True"/>
<group name="product">
<field name="product_template_id"/>
<field name="uom_id"/>
<field name="cost_method"/>
<field name="qty_available"/>
<field name="revaluation_type" invisible="1"/>
</group>
<group name="unit_cost">
<field name="old_cost"
attrs="{'invisible':['|', '|', ('revaluation_type', '!=', 'price_change'), ('cost_method','=', 'real'), ('state', '!=', 'posted')]}"/>
<field name="current_cost"/>
<field name="new_cost"
attrs="{'invisible':['|', ('revaluation_type', '!=', 'price_change'), ('cost_method','=', 'real')]}"/>
<group>
<group name="product">
<field name="product_template_id"/>
<field name="uom_id"/>
<field name="cost_method"/>
<field name="qty_available"/>
</group>
<group name="unit_cost">
<field name="old_cost"
attrs="{'invisible':['|', '|', ('revaluation_type', '!=', 'price_change'), ('cost_method','=', 'real'), ('state', '!=', 'posted')]}"/>
<field name="current_cost"/>
<field name="new_cost"
attrs="{'invisible':['|', ('revaluation_type', '!=', 'price_change'), ('cost_method','=', 'real')]}"/>
</group>
<group name="value">
<field name="old_value"
attrs="{'invisible':['|', '|', ('revaluation_type', '!=', 'inventory_value'), ('cost_method','=', 'real'), ('state', '!=', 'posted')]}"/>
<field name="current_value"/>
<field name="new_value"
attrs="{'invisible':['|', ('revaluation_type', '!=', 'inventory_value'), ('cost_method','=', 'real')]}"/>
</group>
<group name="accounting">
<field name="increase_account_id"/>
<field name="decrease_account_id"/>
<field name="move_id"/>
</group>
</group>
<notebook>
<page name="quant" string="Quants" attrs="{'invisible':[('cost_method','!=', 'real')]}">
<button
name="%(action_stock_inventory_revaluation_line_get_quant)d" string="Get Quants" type="action" class="oe_highlight" />
<field name="line_quant_ids"
nolabel="1">
<tree name="quants"
string="Quants"
create="false"
delete="false" editable="bottom">
<field name="quant_id"/>
<field name="in_date"/>
<field name="product_id"/>
<field name="location_id"/>
<field name="qty"/>
<field name="current_cost"/>
<field name="new_cost"/>
</tree>
</field>
</page>
</notebook>
</form>
</field>
</page>
</notebook>
</group>
<group name="value">
<field name="old_value"
attrs="{'invisible':['|', '|', ('revaluation_type', '!=', 'inventory_value'), ('cost_method','=', 'real'), ('state', '!=', 'posted')]}"/>
<field name="current_value"/>
<field name="new_value"
attrs="{'invisible':['|', ('revaluation_type', '!=', 'inventory_value'), ('cost_method','=', 'real')]}"/>
</group>
<group name="accounting">
<field name="increase_account_id"/>
<field name="decrease_account_id"/>
<field name="move_id"/>
</group>
</group>
<notebook>
<page name="quant" string="Quants" attrs="{'invisible':[('cost_method','!=', 'real')]}">
<button
name="%(action_stock_inventory_revaluation_get_quant)d"
string="Get Quants" type="action" class="oe_highlight"
states="draft"/>
<field name="reval_quant_ids"
nolabel="1">
<tree name="quants"
string="Quants"
create="false"
delete="false" editable="bottom">
<field name="quant_id"/>
<field name="in_date"/>
<field name="product_id"/>
<field name="location_id"/>
<field name="qty"/>
<field name="current_cost"/>
<field name="new_cost"/>
</tree>
</field>
</page>
</notebook>
</sheet>
</form>
</field>
</record>
<record id="view_stock_inventory_revaluation_line_tree"
<record id="view_stock_inventory_revaluation_tree"
model="ir.ui.view">
<field name="name">stock.inventory.revaluation.line.tree</field>
<field name="model">stock.inventory.revaluation.line</field>
<field name="name">stock.inventory.revaluation.tree</field>
<field name="model">stock.inventory.revaluation</field>
<field name="arch" type="xml">
<tree string="Stock Inventory Revaluation Lines">
<field
name="revaluation_type"
invisible="1"/>
<tree string="Stock Inventory Revaluation">
<field name="name"/>
<field name="revaluation_type"/>
<field name="document_date"/>
<field name="product_template_id"/>
<field name="uom_id"/>
<field name="cost_method"/>
@@ -150,60 +120,16 @@
<field name="increase_account_id"/>
<field name="decrease_account_id"/>
<field name="move_id"/>
<field name="state"/>
<field name="company_id" groups="base.group_multi_company"/>
</tree>
</field>
</record>
<record id="view_stock_inventory_revaluation_line_form"
<record id="view_stock_inventory_revaluation_quant_tree"
model="ir.ui.view">
<field name="name">stock.inventory.revaluation.line.form</field>
<field name="model">stock.inventory.revaluation.line</field>
<field name="arch" type="xml">
<form string="Stock Inventory Revaluation Lines">
<group>
<field name="state" invisible="True"/>
<group name="product">
<field name="product_template_id"/>
<field name="uom_id"/>
<field name="cost_method"/>
<field name="qty_available"/>
</group>
<group name="unit_cost">
<field name="old_cost"
attrs="{'invisible':['|', '|', ('revaluation_type', '!=', 'price_change'), ('cost_method','=', 'real'), ('state', '!=', 'posted')]}"/>
<field name="current_cost"/>
<field name="new_cost"
attrs="{'invisible':['|', ('revaluation_type', '!=', 'price_change'), ('cost_method','=', 'real')]}"/>
</group>
<group name="value">
<field name="old_value"
attrs="{'invisible':['|', '|', ('revaluation_type', '!=', 'inventory_value'), ('cost_method','=', 'real'), ('state', '!=', 'posted')]}"/>
<field name="current_value"/>
<field name="new_value"
attrs="{'invisible':['|', ('revaluation_type', '!=', 'inventory_value'), ('cost_method','=', 'real')]}"/>
</group>
<group name="accounting">
<field name="increase_account_id"/>
<field name="decrease_account_id"/>
<field name="move_id"/>
</group>
</group>
<notebook>
<page name="quant" string="Quants" attrs="{'invisible':[('cost_method','!=', 'real_price')]}">
<field name="line_quant_ids"
create="false" delete="false" nolabel="1"
editable="bottom"/>
</page>
</notebook>
</form>
</field>
</record>
<record id="view_stock_inventory_revaluation_line_quant_tree"
model="ir.ui.view">
<field name="name">stock.inventory.revaluation.line.quant.tree</field>
<field name="model">stock.inventory.revaluation.line.quant</field>
<field name="name">stock.inventory.revaluation.quant.tree</field>
<field name="model">stock.inventory.revaluation.quant</field>
<field name="arch" type="xml">
<tree string="Stock Inventory Revaluation Line Quants">
<field name="quant_id"/>
@@ -217,12 +143,12 @@
</field>
</record>
<record id="view_stock_inventory_revaluation_line_quant_form"
<record id="view_stock_inventory_revaluation_quant_form"
model="ir.ui.view">
<field name="name">stock.inventory.revaluation.line.quant.form</field>
<field name="model">stock.inventory.revaluation.line.quant</field>
<field name="name">stock.inventory.revaluation.quant.form</field>
<field name="model">stock.inventory.revaluation.quant</field>
<field name="arch" type="xml">
<form string="Stock Inventory Revaluation Line Quants">
<form string="Stock Inventory Revaluation Quants">
<field name="quant_id"/>
<field name="product_id"/>
<field name="location_id"/>
@@ -233,31 +159,17 @@
</field>
</record>
<record id="view_stock_inventory_revaluation_tree" model="ir.ui.view">
<field name="name">stock.inventory.revaluation.tree</field>
<field name="model">stock.inventory.revaluation</field>
<field name="arch" type="xml">
<tree string="Stock Inventory Revaluation">
<field name="name"/>
<field name="revaluation_type"/>
<field name="document_date"/>
<field name="state"/>
<field name="company_id" groups="base.group_multi_company"/>
</tree>
</field>
</record>
<record id="view_stock_inventory_revaluation_search" model="ir.ui.view">
<field name="name">stock.inventory.revaluation.search</field>
<field name="model">stock.inventory.revaluation</field>
<field name="arch" type="xml">
<search string="Search Stock Inventory Revaluation">
<field name="name"/>
<field name="product_template_id"/>
<field name="revaluation_type"/>
<field name="document_date"/>
<field name="state"/>
<field name="company_id" groups="base.group_multi_company"/>
<field name="line_ids"/>
</search>
</field>
</record>

View File

@@ -4,3 +4,4 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import stock_inventory_revaluation_get_quants
from . import stock_inventory_revaluation_mass_post

View File

@@ -7,8 +7,8 @@ from openerp import api, fields, models
class StockInventoryRevaluationGetQuants(models.TransientModel):
_name = 'stock.inventory.revaluation.line.get.quant'
_description = 'Inventory revaluation line get Quants'
_name = 'stock.inventory.revaluation.get.quant'
_description = 'Inventory revaluation get Quants'
date_from = fields.Date('Date From')
@@ -24,19 +24,20 @@ class StockInventoryRevaluationGetQuants(models.TransientModel):
return domain
def _select_quants(self, line):
def _select_quants(self, revaluation):
quant_l = []
quant_obj = self.env['stock.quant']
for prod_variant in line.product_template_id.product_variant_ids:
for prod_variant in \
revaluation.product_template_id.product_variant_ids:
search_domain = self._get_quant_search_criteria(prod_variant)
quants = quant_obj.search(search_domain)
for quant in quants:
quant_l.append(quant)
return quant_l
def _prepare_line_quant_data(self, line, quant):
def _prepare_line_quant_data(self, revaluation, quant):
return {
'line_id': line.id,
'revaluation_id': revaluation.id,
'quant_id': quant.id,
'new_cost': quant.cost
}
@@ -44,16 +45,16 @@ class StockInventoryRevaluationGetQuants(models.TransientModel):
@api.one
def process(self):
if self.env.context.get('active_id', False):
line_obj = self.env['stock.inventory.revaluation.line']
line_quant_obj = self.env['stock.inventory.revaluation.line.quant']
line = line_obj.browse(self.env.context['active_id'])
reval_obj = self.env['stock.inventory.revaluation']
reval_quant_obj = self.env['stock.inventory.revaluation.quant']
revaluation = reval_obj.browse(self.env.context['active_id'])
# Delete the previous records
for line_quant in line.line_quant_ids:
line_quant.unlink()
for reval_quant in revaluation.reval_quant_ids:
reval_quant.unlink()
quants = self._select_quants(line)
quants = self._select_quants(revaluation)
for q in quants:
q_data = self._prepare_line_quant_data(line, q)
line_quant_obj.create(q_data)
q_data = self._prepare_line_quant_data(revaluation, q)
reval_quant_obj.create(q_data)
return {'type': 'ir.actions.act_window_close'}

View File

@@ -3,9 +3,9 @@
<data>
<record model="ir.ui.view"
id="stock_inventory_revaluation_line_get_quant_form">
<field name="name">stock.inventory.revaluation.line.get.quant.form</field>
<field name="model">stock.inventory.revaluation.line.get.quant</field>
id="stock_inventory_revaluation_get_quant_form">
<field name="name">stock.inventory.revaluation.get.quant.form</field>
<field name="model">stock.inventory.revaluation.get.quant</field>
<field name="arch" type="xml">
<form string="Get Quants">
<group colspan="2" col="2">
@@ -22,15 +22,15 @@
</field>
</record>
<record id="action_stock_inventory_revaluation_line_get_quant"
<record id="action_stock_inventory_revaluation_get_quant"
model="ir.actions.act_window">
<field name="name">Get Quants for Inventory Revaluation</field>
<field name="res_model">stock.inventory.revaluation.line.get.quant</field>
<field name="res_model">stock.inventory.revaluation.get.quant</field>
<field name="type">ir.actions.act_window</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="view_id"
ref="stock_inventory_revaluation_line_get_quant_form"/>
ref="stock_inventory_revaluation_get_quant_form"/>
<field name="target">new</field>
</record>

View File

@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
# © 2015 Eficent Business and IT Consulting Services S.L.
# - Jordi Ballester Alomar
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from openerp import api, fields, models, _, exceptions
class StockInventoryRevaluationMassPost(models.TransientModel):
_name = 'stock.inventory.revaluation.mass.post'
_description = 'Post multiple inventory revaluations'
@api.model
def default_get(self, fields):
res = super(StockInventoryRevaluationMassPost, self).default_get(
fields)
revaluation_ids = self.env.context['active_ids'] or []
active_model = self.env.context['active_model']
if not revaluation_ids:
return res
assert active_model == 'stock.inventory.revaluation', \
'Bad context propagation'
return res
@api.one
def process(self):
revaluation_ids = self.env.context.get('active_ids', [])
revaluation_obj = self.env['stock.inventory.revaluation']
for revaluation in revaluation_obj.browse(revaluation_ids):
if revaluation.state != 'draft':
raise exceptions.Warning(
_('Revaluation %s is not in Draft state') %
revaluation.name)
revaluation.button_post()
return {'type': 'ir.actions.act_window_close'}

View File

@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record model="ir.ui.view"
id="stock_inventory_revaluation_mass_post_form">
<field name="name">stock.inventory.revaluation.mass.post.form</field>
<field name="model">stock.inventory.revaluation.mass.post</field>
<field name="arch" type="xml">
<form string="Get Quants">
<footer>
<button name="process" string="Post" type="object"
class="oe_highlight"/>
or
<button string="Cancel" class="oe_link" special="cancel"/>
</footer>
</form>
</field>
</record>
<record id="action_stock_inventory_revaluation_mass_post"
model="ir.actions.act_window">
<field name="name">Post Inventory Revaluation</field>
<field name="res_model">stock.inventory.revaluation.mass.post</field>
<field name="type">ir.actions.act_window</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="view_id"
ref="stock_inventory_revaluation_mass_post_form"/>
<field name="target">new</field>
</record>
<record model="ir.values" id="stock_inventory_revaluation_mass_post">
<field name="model_id" ref="model_stock_inventory_revaluation" />
<field name="name">Post Inventory Revaluations</field>
<field name="key2">client_action_multi</field>
<field name="value" eval="'ir.actions.act_window,' + str(ref('action_stock_inventory_revaluation_mass_post'))" />
<field name="key">action</field>
<field name="model">stock.inventory.revaluation</field>
</record>
</data>
</openerp>