@transaction.atomic
def rebuild_stock_state(case_id, section_id, product_id):

    for action in plan_rebuild_stock_state(case_id, section_id, product_id):
        if isinstance(action, _DeleteStockTransaction):
            action.stock_transaction.delete()
        elif isinstance(action, _SaveStockTransaction):
            action.stock_transaction.stock_on_hand = action.ledger_values.balance
            action.stock_transaction.quantity = action.ledger_values.delta
            action.stock_transaction.save()
        else:
            raise ValueError()
rebuilds the StockState object
dataset/ETHPy150Open dimagi/commcare-hq/corehq/apps/commtrack/processing.py/rebuild_stock_state