Created
August 29, 2013 18:11
-
-
Save aliomattux/6381444 to your computer and use it in GitHub Desktop.
This file contains 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
def transact_bin_move(self, cr, uid, move, context=None): | |
""" | |
Transaction Movement Method | |
:param :move Browse record of stock.move | |
Description: Update the inventory assignment table | |
with data from stock.move | |
""" | |
lock = Lock() | |
lock.acquire() | |
if not context: | |
context = {} | |
#Try to avoid an uncessary read | |
from_created = False | |
to_created = False | |
product_tmpl_id = move.product_id.product_tmpl_id.id | |
from_dynamic = False | |
to_dynamic = False | |
#STEP 1: Determine the type of move. Some types require multiple adjustments | |
if move.type in ['internal', 'transfer']: | |
#STEP: Internal movements and inventory transfers contain 2 moves. Deduct from a current bin and add to a bin | |
#Find an existing bin pair (Move Bin, Product) | |
filters = [('bin_id', '=', move.from_bin_id.id), ('product_tmpl_id', '=', product_tmpl_id)] | |
if move.from_bin_id.storage_type_id.type == 'fixed': | |
filters.append(('brand_id', '=', move.product_id.brand_id.id)) | |
else: | |
from_dynamic = True | |
from_inventory_ids = self.search(cr, uid, filters) | |
#If no inventory ids are found, this bin pair does not exist so we attempt to create it | |
if not from_inventory_ids: | |
from_created = True | |
vals = { | |
'product_tmpl_id': product_tmpl_id, | |
'bin_id': move.from_bin_id.id | |
} | |
if not from_dynamic: | |
vals['brand_id'] = move.product_id.brand_id.id | |
inventory_id = self.create(cr, uid, vals) | |
else: | |
inventory_id = from_inventory_ids[0] | |
#Get the current onhand balance | |
if not from_created: | |
from_bin_balance = self.browse(cr, uid, inventory_id).qty_onhand | |
#It is pointless to read a stock level from a newly created location | |
else: | |
from_bin_balance = 0.0 | |
from_bin_balance -= round(move.move_qty, 12) | |
#Write the new balance | |
self.write(cr, uid, inventory_id, {'qty_onhand': from_bin_balance}) | |
#STEP 2: Process the movement | |
#Find an existing bin pair (Move Bin, Product) | |
to_filters = [('bin_id', '=', move.to_bin_id.id), ('product_tmpl_id', '=', product_tmpl_id)] | |
if move.to_bin_id.storage_type_id.type == 'fixed': | |
to_filters.append(('brand_id', '=', move.product_id.brand_id.id)) | |
else: | |
to_dynamic = True | |
to_inventory_ids = self.search(cr, uid, to_filters) | |
if not to_inventory_ids: | |
to_created = True | |
vals = { | |
'product_tmpl_id': product_tmpl_id, | |
'bin_id': move.to_bin_id.id | |
} | |
if not to_dynamic: | |
vals['brand_id'] = move.product_id.brand_id.id | |
inventory_id = self.create(cr, uid, vals) | |
else: | |
inventory_id = to_inventory_ids[0] | |
#Get the current onhand balance | |
if not to_created: | |
to_bin_balance = self.browse(cr, uid, inventory_id).qty_onhand | |
#It is pointless to read a stock level from a newly created location | |
else: | |
to_bin_balance = 0.0 | |
to_bin_balance += round(move.move_qty, 12) | |
lock.release() | |
#Write the new balance | |
self.write(cr, uid, inventory_id, {'qty_onhand': to_bin_balance}) | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment