[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 Alexis de Lattre
parent 45b7d5b48f
commit c559485d23
7 changed files with 93 additions and 63 deletions

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.')
)