Skip to content

Instantly share code, notes, and snippets.

@sharoonthomas
Created September 23, 2016 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharoonthomas/f89f51ab2b76e58f20a828c7fa2db8a4 to your computer and use it in GitHub Desktop.
Save sharoonthomas/f89f51ab2b76e58f20a828c7fa2db8a4 to your computer and use it in GitHub Desktop.
An example script to move all the inventory from one location to another. Usually used when you are merging location for example.
import os
from fulfil_client import Client
client = Client(
os.environ['FULFIL_SUBDOMAIN'],
os.environ['FULFIL_API_KEY']
)
FROM_LOCATION = 'FROM-LOCATION-CODE'
TO_LOCATION = 'TO-LOCATION-CODE'
Location = client.model('stock.location')
Product = client.model('product.product')
InternalShipment = client.model('stock.shipment.internal')
def get_location_inventory(location_id):
"""
Return product and quantity for the given location
"""
products = Product.search_read(
[], None, None, None, ['id', 'code', 'default_uom']
)
inventory = Product.get_product_inventory(
[p['id'] for p in products],
[location_id]
)
on_hand_inventory = {}
for product, loc_inventory in inventory.iteritems():
for location, inv_data in loc_inventory.iteritems():
qoh = inv_data['quantity_on_hand']
if not qoh:
continue
on_hand_inventory[int(product)] = qoh
return products, on_hand_inventory
def move_inventory(from_location, to_location):
from_location, = Location.search_read(
[('code', '=', from_location)],
None, None, None,
['name', 'code']
)
to_location, = Location.search_read(
[('code', '=', to_location)],
None, None, None,
['name', 'code']
)
# Now fetch the inventory of the location
products, inventory = get_location_inventory(from_location['id'])
products = dict([(p['id'], p) for p in products])
for product, qoh in inventory.iteritems():
if qoh < 0.0:
product = Product.read([product], ['code'])[0]
raise Exception(
"Negative inventory (%s) for %s" % (qoh, product['code'])
)
# Create a new internal shipment
shipment, = InternalShipment.create([{
'from_location': from_location['id'],
'to_location': to_location['id'],
'reference': 'Auto Gen from API',
'moves': [('create', [
{
'product': p,
'quantity': q,
'internal_quantity': q,
'from_location': from_location['id'],
'to_location': to_location['id'],
'uom': products[p]['default_uom'],
} for p, q in inventory.iteritems() if q > 0
])]
}])
print "Shipment draft is ready"
print "https://%s.fulfil.io/client/#/model/stock.shipment.internal/%s" % (
os.environ['FULFIL_SUBDOMAIN'],
shipment
)
if __name__ == '__main__':
move_inventory(FROM_LOCATION, TO_LOCATION)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment