[IMP] stock_quant_manual_assign:

* Better layout
* Remove active_id dependency in some computed fields
* Clean code
* Refine constraint
* Take into account if the current line is previously reserved before clicking on the button.
This commit is contained in:
Pedro M. Baeza
2018-11-20 22:37:12 +01:00
committed by Tony Gu
parent e48574cf3b
commit 2111e5368f
7 changed files with 93 additions and 63 deletions

View File

@@ -66,12 +66,12 @@ Contributors
~~~~~~~~~~~~
* Mikel Arregi <mikelarregi@avanzosc.es>
* Pedro M. Baeza <pedro.baeza@tecnativa.com>
* Ana Juaristi <anajuaristi@avanzosc.es>
* Oihane Crucelaegui <oihanecrucelaegi@avanzosc.es>
* Fanha Giang <fanha99@hotmail.com>
* `Tecnativa <https://www.tecnativa.com>`_:
* Pedro M. Baeza
* Vicent Cubells <vicent.cubells@tecnativa.com>
Maintainers

View File

@@ -1,6 +1,8 @@
# Copyright 2015 Mikel Arregi - AvanzOSC
# Copyright 2016 Tecnativa - Pedro M. Baeza
# Copyright 2017 Eficent - Jordi Ballester
# Copyright 2018 Fanha Giang
# Copyright 2018 Tecnativa - Vicent Cubells
# Copyright 2016-2018 Tecnativa - Pedro M. Baeza
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
{

View File

@@ -1,8 +1,8 @@
* Mikel Arregi <mikelarregi@avanzosc.es>
* Pedro M. Baeza <pedro.baeza@tecnativa.com>
* Ana Juaristi <anajuaristi@avanzosc.es>
* Oihane Crucelaegui <oihanecrucelaegi@avanzosc.es>
* Fanha Giang <fanha99@hotmail.com>
* `Tecnativa <https://www.tecnativa.com>`_:
* Pedro M. Baeza
* Vicent Cubells <vicent.cubells@tecnativa.com>

View File

@@ -414,11 +414,11 @@ If you spotted it first, help us smashing it by providing a detailed and welcome
<h2><a class="toc-backref" href="#id5">Contributors</a></h2>
<ul class="simple">
<li>Mikel Arregi &lt;<a class="reference external" href="mailto:mikelarregi&#64;avanzosc.es">mikelarregi&#64;avanzosc.es</a>&gt;</li>
<li>Pedro M. Baeza &lt;<a class="reference external" href="mailto:pedro.baeza&#64;tecnativa.com">pedro.baeza&#64;tecnativa.com</a>&gt;</li>
<li>Ana Juaristi &lt;<a class="reference external" href="mailto:anajuaristi&#64;avanzosc.es">anajuaristi&#64;avanzosc.es</a>&gt;</li>
<li>Oihane Crucelaegui &lt;<a class="reference external" href="mailto:oihanecrucelaegi&#64;avanzosc.es">oihanecrucelaegi&#64;avanzosc.es</a>&gt;</li>
<li>Fanha Giang &lt;<a class="reference external" href="mailto:fanha99&#64;hotmail.com">fanha99&#64;hotmail.com</a>&gt;</li>
<li><a class="reference external" href="https://www.tecnativa.com">Tecnativa</a>:<ul>
<li>Pedro M. Baeza</li>
<li>Vicent Cubells &lt;<a class="reference external" href="mailto:vicent.cubells&#64;tecnativa.com">vicent.cubells&#64;tecnativa.com</a>&gt;</li>
</ul>
</li>

View File

@@ -84,7 +84,7 @@ class TestStockQuantManualAssign(TransactionCase):
wizard.quants_lines[0].write({
'selected': True,
})
wizard.quants_lines[0].onchange_selected()
wizard.quants_lines[0]._onchange_selected()
wizard.quants_lines[1].write({
'selected': True,
'qty': 50.0,

View File

@@ -1,5 +1,8 @@
# Copyright 2015 Mikel Arregi - AvanzOSC
# Copyright 2015 Oihane Crucelaegui - AvanzOSC
# Copyright 2018 Fanha Giang
# Copyright 2018 Tecnativa - Vicent Cubells
# Copyright 2018 Tecnativa - Pedro M. Baeza
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
from odoo import _, api, fields, models
@@ -16,20 +19,19 @@ class AssignManualQuants(models.TransientModel):
def _check_qty(self):
precision_digits = self.env[
'decimal.precision'].precision_get('Product Unit of Measure')
move = self.env['stock.move'].browse(self.env.context['active_id'])
for record in self.filtered('quants_lines'):
if float_compare(record.lines_qty, move.product_qty,
if float_compare(record.lines_qty, record.move_id.product_qty,
precision_digits=precision_digits) > 0:
raise UserError(
_('Quantity is higher than the needed one'))
@api.depends('quants_lines', 'quants_lines.qty')
@api.depends('move_id', 'quants_lines', 'quants_lines.qty')
def _compute_qties(self):
move = self.env['stock.move'].browse(self.env.context['active_id'])
lines_qty = sum(quant_line.qty for quant_line in self.quants_lines
if quant_line.selected)
self.lines_qty = lines_qty
self.move_qty = move.product_qty - lines_qty
for record in self:
record.lines_qty = sum(
record.quants_lines.filtered('selected').mapped('qty')
)
record.move_qty = record.move_id.product_qty - record.lines_qty
lines_qty = fields.Float(
string='Reserved qty', compute='_compute_qties',
@@ -78,74 +80,99 @@ class AssignManualQuants(models.TransientModel):
available_quants = self.env['stock.quant'].search([
('location_id', 'child_of', move.location_id.id),
('product_id', '=', move.product_id.id),
('quantity', '>', 0)
('quantity', '>', 0),
])
quants_lines = []
for quant in available_quants:
line = {}
line['quant'] = quant.id
line['lot_id'] = quant.lot_id.id
line['quant_id'] = quant.id
line['on_hand'] = quant.quantity
line['in_date'] = quant.in_date
line['package_id'] = quant.package_id.id
line['selected'] = False
line['qty'] = 0
move_lines = move.move_line_ids.filtered(
lambda ml:
ml.location_id == quant.location_id and
ml.lot_id == quant.lot_id)
for ml in move_lines:
line['qty'] = line['qty'] + ml.ordered_qty
line['selected'] = True
lambda ml: (ml.location_id == quant.location_id and
ml.lot_id == quant.lot_id)
)
line['qty'] = sum(move_lines.mapped('ordered_qty'))
line['selected'] = bool(line['qty'])
line['reserved'] = quant.reserved_quantity - line['qty']
line['location_id'] = quant.location_id.id
quants_lines.append(line)
res.update({'quants_lines': quants_lines, 'move_id': move.id})
res = self._convert_to_write(self._convert_to_cache(res))
res.update({
'quants_lines': [(0, 0, x) for x in quants_lines],
'move_id': move.id,
})
return res
class AssignManualQuantsLines(models.TransientModel):
_name = 'assign.manual.quants.lines'
_rec_name = 'quant'
@api.multi
@api.onchange('selected')
def onchange_selected(self):
for record in self:
if not record.selected:
record.qty = False
elif not record.qty:
quant = record.quant
quant_qty = quant.quantity - quant.reserved_quantity
remaining_qty = record.assign_wizard.move_qty
record.qty = (quant_qty if quant_qty < remaining_qty else
remaining_qty)
_rec_name = 'quant_id'
assign_wizard = fields.Many2one(
comodel_name='assign.manual.quants', string='Move', required=True,
ondelete='cascade')
quant = fields.Many2one(
quant_id = fields.Many2one(
comodel_name='stock.quant', string='Quant', required=True,
ondelete='cascade')
ondelete='cascade', oldname='quant')
location_id = fields.Many2one(
comodel_name='stock.location', string='Location',
related='quant.location_id', readonly=True)
related='quant_id.location_id', readonly=True)
lot_id = fields.Many2one(
comodel_name='stock.production.lot', string='Lot',
related='quant.lot_id', readonly=True,
related='quant_id.lot_id', readonly=True,
groups="stock.group_production_lot")
in_date = fields.Date(
string='Incoming Date', readonly=True)
package_id = fields.Many2one(
comodel_name='stock.quant.package', string='Package',
related='quant.package_id', readonly=True,
related='quant_id.package_id', readonly=True,
groups="stock.group_tracking_lot")
# This is not correctly shown as related or computed, so we make it regular
on_hand = fields.Float(
string='On Hand', digits=dp.get_precision('Product Unit of Measure'))
readonly=True,
string='On Hand',
digits=dp.get_precision('Product Unit of Measure'),
)
reserved = fields.Float(
string='Others Reserved',
digits=dp.get_precision('Product Unit of Measure'))
selected = fields.Boolean(string='Select')
qty = fields.Float(
string='QTY', digits=dp.get_precision('Product Unit of Measure'))
@api.multi
@api.onchange('selected')
def _onchange_selected(self):
for record in self:
if not record.selected:
record.qty = 0
elif not record.qty:
# This takes current "snapshot" situation, so that we don't
# have to compute each time if current reserved quantity is
# for this current move. If other operations change available
# quantity on quant, a constraint would be raised later on
# validation.
quant_qty = record.on_hand - record.reserved
remaining_qty = record.assign_wizard.move_qty
record.qty = min(quant_qty, remaining_qty)
@api.multi
@api.constrains('qty')
def _check_qty(self):
precision_digits = self.env[
'decimal.precision'
].precision_get('Product Unit of Measure')
for record in self.filtered('qty'):
quant = record.quant_id
move_lines = record.assign_wizard.move_id.move_line_ids.filtered(
lambda ml: (ml.location_id == quant.location_id and
ml.lot_id == quant.lot_id)
)
reserved = (
quant.reserved_quantity - sum(move_lines.mapped('ordered_qty'))
)
if float_compare(record.qty, record.quant_id.quantity - reserved,
precision_digits=precision_digits) > 0:
raise UserError(
_('Selected line quantity is higher than the available '
'one. Maybe an operation with this product has been '
'done meanwhile or you have manually increased the '
'suggested value.')
)

View File

@@ -4,21 +4,22 @@
<field name="name">assign.manual.quants.form</field>
<field name="model">assign.manual.quants</field>
<field name="arch" type="xml">
<form name="Manual Quants">
<form>
<field name='quants_lines' colspan="4">
<tree editable="top" delete="0" create="0">
<field name="lot_id" readonly="1"/>
<field name="on_hand" readonly="1"/>
<field name="reserved" readonly="1"/>
<field name="location_id" readonly="1"/>
<field name="package_id" readonly="1"/>
<field name="selected"/>
<field name="qty" attrs="{'readonly':[('selected', '=', False)]}" sum="qty"/>
</tree>
</field>
<group col='4' colspan="4">
<field name='quants_lines' colspan="4">
<tree editable="top" delete="0" create="0">
<field name="lot_id" readonly="1"/>
<field name="on_hand" readonly="1"/>
<field name="reserved" readonly="1"/>
<field name="location_id" readonly="1"/>
<field name="package_id" readonly="1"/>
<field name="selected"/>
<field name="qty" attrs="{'readonly':[('selected', '=', False)]}" sum="qty"/>
</tree>
</field>
<field name="move_qty" colspan="2" readonly="1"/>
<field name="lines_qty" invisible="1" />
<field name="move_id" invisible="1"/>
</group>
<footer>
<button name="assign_quants" type="object"
@@ -30,7 +31,7 @@
</field>
</record>
<act_window name="Manual quants" res_model="assign.manual.quants"
<act_window name="Manual assignment" res_model="assign.manual.quants"
src_model="stock.move" view_mode="form" target="new"
id="assign_manual_quants_action" />
</odoo>