Last active
January 14, 2019 19:59
-
-
Save SantosJMM/4f1710fc361d2220baaba42650006c8f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -- coding: utf-8 -- | |
from odoo import models, api, fields, _ | |
from odoo.exceptions import ValidationError | |
class StockMove(models.Model): | |
_inherit = 'stock.move' | |
def _update_qty_done(self, qty_done, product_id=None, move_line_ids=None, location_id=None, location_dest_id=None, | |
lot_id=None, package_id=None, owner_id=None, has_existence=True, done_move=False): | |
""" | |
Esta funcion se encarga de actualizar el qty_done de las lineas de movimiento, | |
si el movimiento no tiene lineas de movimiento es porque el producto no tiene existencia, en | |
tal caso se crea la linea de movimiento para actualizar el quantity_done del movimiento y de este modo | |
despachar el conduce aunque el producto no este en existencia en la actualidad. | |
:param qty_done: cantidad a verificar | |
:param product_id: producto | |
:param move_line_ids: | |
:param location_id: | |
:param location_dest_id: | |
:param lot_id: | |
:param package_id: | |
:param owner_id: | |
:param has_existence: | |
:return: | |
""" | |
self.ensure_one() | |
if not product_id: | |
product_id = self.product_id | |
if not location_id: | |
location_id = self.location_id | |
if not location_dest_id: | |
location_dest_id = self.location_dest_id.get_putaway_strategy(product_id) or self.location_dest_id | |
if not move_line_ids: | |
move_line_ids = self.move_line_ids | |
dict_move_line = {'qty_done': qty_done} | |
try: | |
if 'done_move' in self.fields_get_keys() and done_move: | |
dict_move_line.update({'done_move': done_move}) | |
except Exception: | |
pass | |
# Si el movimiento no tiene lineas de movimiento, | |
# creamos la linea de movimiento. | |
if not move_line_ids: | |
dict_move_line.update({'product_id': product_id.id, | |
'location_id': location_id.id, | |
'location_dest_id': location_dest_id.id, | |
'product_uom_id': product_id.uom_id.id}) | |
self.write({'move_line_ids': [(0, 0, dict_move_line)]}) | |
return self.move_line_ids, self.quantity_done | |
# actualizamos el qty_done de la linea de movimiento. | |
self.write({'move_line_ids': [(1, move_line_ids[0].id, dict_move_line)]}) | |
return self.move_line_ids, self.quantity_done | |
class StockPicking(models.Model): | |
_inherit = 'stock.picking' | |
product_barcode = fields.Char(string='Código de barras') | |
product_name = fields.Char(string=u'Último producto procesado') | |
initial = fields.Float(string="Cantidad inicial") | |
@api.multi | |
@api.onchange('product_barcode') | |
def product_barcode_change(self): | |
if not self.product_barcode: | |
return | |
if not self.move_lines: | |
raise ValidationError(u"Esta recepción/despacho no tiene líneas de producto.") | |
exists = False | |
for move in self.move_lines: | |
if move.product_id.barcode == self.product_barcode: | |
exists = True | |
# available_quantity = self.env['stock.quant']._get_available_quantity(move.product_id, move.location_id) | |
before_qty_done = 0.0 | |
if move.move_line_ids: | |
# TODO: Se debe proporcionar la linea de movimiento a modificar, mientras siempre sera el indice 0 | |
before_qty_done += move.move_line_ids[0].qty_done or 0.0 | |
qty_done = 1 + before_qty_done | |
move_line_ids, quantity_done = move._update_qty_done(qty_done, product_id=move.product_id) | |
self.product_name = move.product_id.name | |
self.initial = move.product_uom_qty | |
break | |
if not exists: | |
raise ValidationError(u"Este producto no está en esta recepción/despacho.") | |
self.product_barcode = None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment