Last active
August 24, 2018 15:07
-
-
Save oerp-odoo/479f580cbff8614d7011f8a85a7cef24 to your computer and use it in GitHub Desktop.
Move available stock to new location (that is created)
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
# flake8: noqa | |
""" | |
This module must be run when Odoo environment is started using shell, otherwise variables won't be recognized. | |
""" | |
# Models. | |
StockLocation = env['stock.location'] | |
StockQuant = env['stock.quant'] | |
IrModelData = env['ir.model.data'] | |
# Locations to do transfer. | |
FROM_LOCATION = env.ref('stock.stock_location_stock') | |
FROM_LOCATION.name = 'stock' | |
TO_LOCATION = env.ref( | |
'__export__.stock_location_raw_mats_2001', raise_if_not_found=False) | |
if not TO_LOCATION: | |
TO_LOCATION = StockLocation.create( | |
{'name': '2001', 'location_id': FROM_LOCATION.id}) | |
IrModelData.create({ | |
'module': '__export__', | |
'name': 'stock_location_raw_mats_2001', | |
'model': 'stock.location', | |
'res_id': TO_LOCATION.id, | |
}) | |
# Picking data. | |
PICKING_TYPE_ID = env.ref('stock.picking_type_internal').id | |
# Raw products | |
PRODUCTS = env['product.product'].search([('raw_material', '=', True)]) | |
ORIGIN = 'custom' | |
def _prepare_picking(): | |
return { | |
'picking_type_id': PICKING_TYPE_ID, | |
'location_id': FROM_LOCATION.id, | |
'location_dest_id': TO_LOCATION.id, | |
'origin': ORIGIN, | |
} | |
def _prepare_move(product, qty_available): | |
return { | |
'name': ORIGIN, | |
'picking_type_id': PICKING_TYPE_ID, | |
'product_id': product.id, | |
'product_uom_qty': qty_available, | |
'product_uom': product.uom_id.id, | |
'state': 'draft', | |
'procure_method': 'make_to_stock', | |
# Take origin from related MO. | |
'origin': ORIGIN, | |
} | |
def _prepare_moves(PRODUCTS, from_location, to_location): | |
move_lines_data = [] | |
for product in PRODUCTS: | |
qty_available = StockQuant._get_available_quantity( | |
product, from_location) | |
if qty_available > 0: | |
move_lines_data.append( | |
(0, 0, _prepare_move(product, qty_available))) | |
return move_lines_data | |
def prepare_picking(): | |
picking_data = _prepare_picking() | |
moves_data = _prepare_moves(PRODUCTS, FROM_LOCATION, TO_LOCATION) | |
picking_data['move_lines'] = moves_data | |
return picking_data | |
if not env.ref( | |
'__export__.stock_picking_custom_transfer_from_stock_to_new_2001', | |
raise_if_not_found=False): | |
data = prepare_picking() | |
picking = env['stock.picking'].create(data) | |
IrModelData.create({ | |
'module': '__export__', | |
'name': 'stock_picking_custom_transfer_from_stock_to_new_2001', | |
'model': 'stock.picking', | |
'res_id': picking.id, | |
}) | |
picking.action_confirm() | |
picking.action_assign() | |
# Specify that all quantities are done (just use same reserved qties). | |
for move in picking.move_lines: | |
for line in move.move_line_ids: | |
line.qty_done = line.product_uom_qty | |
picking.action_done() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment