From 2111e5368fd56f5b097b74a97bb15c26b42d35ba Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Tue, 20 Nov 2018 22:37:12 +0100 Subject: [PATCH] [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. --- stock_quant_manual_assign/README.rst | 2 +- stock_quant_manual_assign/__manifest__.py | 4 +- .../readme/CONTRIBUTORS.rst | 2 +- .../static/description/index.html | 2 +- .../tests/test_stock_quant_manual_assign.py | 2 +- .../wizard/assign_manual_quants.py | 117 +++++++++++------- .../wizard/assign_manual_quants_view.xml | 27 ++-- 7 files changed, 93 insertions(+), 63 deletions(-) diff --git a/stock_quant_manual_assign/README.rst b/stock_quant_manual_assign/README.rst index 06840b0a1..feb816a1a 100644 --- a/stock_quant_manual_assign/README.rst +++ b/stock_quant_manual_assign/README.rst @@ -66,12 +66,12 @@ Contributors ~~~~~~~~~~~~ * Mikel Arregi -* Pedro M. Baeza * Ana Juaristi * Oihane Crucelaegui * Fanha Giang * `Tecnativa `_: + * Pedro M. Baeza * Vicent Cubells Maintainers diff --git a/stock_quant_manual_assign/__manifest__.py b/stock_quant_manual_assign/__manifest__.py index a442a715d..b4e35283a 100644 --- a/stock_quant_manual_assign/__manifest__.py +++ b/stock_quant_manual_assign/__manifest__.py @@ -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 { diff --git a/stock_quant_manual_assign/readme/CONTRIBUTORS.rst b/stock_quant_manual_assign/readme/CONTRIBUTORS.rst index c551e9c6b..828c173fa 100644 --- a/stock_quant_manual_assign/readme/CONTRIBUTORS.rst +++ b/stock_quant_manual_assign/readme/CONTRIBUTORS.rst @@ -1,8 +1,8 @@ * Mikel Arregi -* Pedro M. Baeza * Ana Juaristi * Oihane Crucelaegui * Fanha Giang * `Tecnativa `_: + * Pedro M. Baeza * Vicent Cubells diff --git a/stock_quant_manual_assign/static/description/index.html b/stock_quant_manual_assign/static/description/index.html index 4b73bcd24..6f1f34f7d 100644 --- a/stock_quant_manual_assign/static/description/index.html +++ b/stock_quant_manual_assign/static/description/index.html @@ -414,11 +414,11 @@ If you spotted it first, help us smashing it by providing a detailed and welcome

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:
  • diff --git a/stock_quant_manual_assign/tests/test_stock_quant_manual_assign.py b/stock_quant_manual_assign/tests/test_stock_quant_manual_assign.py index f73494d54..601d393fc 100644 --- a/stock_quant_manual_assign/tests/test_stock_quant_manual_assign.py +++ b/stock_quant_manual_assign/tests/test_stock_quant_manual_assign.py @@ -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, diff --git a/stock_quant_manual_assign/wizard/assign_manual_quants.py b/stock_quant_manual_assign/wizard/assign_manual_quants.py index b31dfbcab..685f564aa 100644 --- a/stock_quant_manual_assign/wizard/assign_manual_quants.py +++ b/stock_quant_manual_assign/wizard/assign_manual_quants.py @@ -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.') + ) diff --git a/stock_quant_manual_assign/wizard/assign_manual_quants_view.xml b/stock_quant_manual_assign/wizard/assign_manual_quants_view.xml index f4f44342b..02485b89b 100644 --- a/stock_quant_manual_assign/wizard/assign_manual_quants_view.xml +++ b/stock_quant_manual_assign/wizard/assign_manual_quants_view.xml @@ -4,21 +4,22 @@ assign.manual.quants.form assign.manual.quants -
    + + + + + + + + + + + + - - - - - - - - - - - +