'Update stock.move.lot records, according to the new qty currently
produced.'

@api.onchange('qty_producing')
def _onchange_qty_producing(self):

    moves = self.move_raw_ids.filtered((lambda move: ((move.state not in ('done', 'cancel')) and (move.product_id.tracking != 'none') and (move.product_id.id != self.production_id.product_id.id))))
    for move in moves:
        move_lots = self.active_move_lot_ids.filtered((lambda move_lot: (move_lot.move_id == move)))
        if (not move_lots):
            continue
        new_qty = (move.unit_factor * self.qty_producing)
        if (move.product_id.tracking == 'lot'):
            move_lots[0].quantity = new_qty
            move_lots[0].quantity_done = new_qty
        elif (move.product_id.tracking == 'serial'):
            qty_todo = (new_qty - sum(move_lots.mapped('quantity')))
            if (float_compare(qty_todo, 0.0, precision_rounding=move.product_uom.rounding) > 0):
                while (float_compare(qty_todo, 0.0, precision_rounding=move.product_uom.rounding) > 0):
                    self.active_move_lot_ids += self.env['stock.move.lots'].new({'move_id': move.id, 'product_id': move.product_id.id, 'lot_id': False, 'quantity': min(1.0, qty_todo), 'quantity_done': min(1.0, qty_todo), 'workorder_id': self.id, 'done_wo': False})
                    qty_todo -= 1
            elif (float_compare(qty_todo, 0.0, precision_rounding=move.product_uom.rounding) < 0):
                qty_todo = abs(qty_todo)
                for move_lot in move_lots:
                    if (qty_todo <= 0):
                        break
                    if ((not move_lot.lot_id) and (qty_todo >= move_lot.quantity)):
                        qty_todo = (qty_todo - move_lot.quantity)
                        self.active_move_lot_ids -= move_lot
                    else:
                        move_lot.quantity = (move_lot.quantity - qty_todo)
                        if ((move_lot.quantity_done - qty_todo) > 0):
                            move_lot.quantity_done = (move_lot.quantity_done - qty_todo)
                        else:
                            move_lot.quantity_done = 0
                        qty_todo = 0
