human
Complete this python program:
def inv_stock_movements(resource, selectors, orderby):
assistant
#Here is the Python code you requested.

    """Extraction method for stock movements report
    @param resource: the S3Resource (inv_inv_item)
    @param selectors: the field selectors
    @param orderby: orderby expression
    @note: transactions can be filtered by earliest/latest date
    using an S3DateFilter with selector="_transaction.date"
    @todo: does not take manual stock adjustments into account
    @todo: does not represent sites or Waybill/GRN as
    links (breaks PDF export, but otherwise it\s useful)
    """
    selectors = ['id', 'site_id', 'site_id$name', 'item_id$item_category_id', 'bin', 'item_id$name', 'quantity']
    data = resource.select(selectors, limit=None, orderby=orderby, raw_data=True, represent=True)
    inv_item_ids = [row['_row']['inv_inv_item.id'] for row in data.rows]
    convert = S3TypeConverter.convert
    request = current.request
    get_vars = request.get_vars
    dtstr = get_vars.get('_transaction.date__ge')
    earliest = (convert(datetime.datetime, dtstr) if dtstr else None)
    dtstr = get_vars.get('_transaction.date__le')
    latest = (convert(datetime.datetime, dtstr) if dtstr else request.utcnow)
    def item_dict():
        '    Stock    movement    data    per    inventory    item    '
        return {'quantity_in': 0, 'quantity_out': 0, 'quantity_in_after': 0, 'quantity_out_after': 0, 'sites': [], 'documents': []}
    movements = {}
    all_sites = set()
    s3db = current.s3db
    query = FS('recv_inv_item_id').belongs(inv_item_ids)
    if earliest:
        query &= (FS('recv_id$date') >= earliest)
    incoming = s3db.resource('inv_track_item', filter=query)
    transactions = incoming.select(['recv_id$date', 'recv_id$from_site_id', 'recv_id$recv_ref', 'recv_inv_item_id', 'recv_quantity'], limit=None, raw_data=True, represent=True)
    for transaction in transactions.rows:
        raw = transaction['_row']
        inv_item_id = raw['inv_track_item.recv_inv_item_id']
        if (inv_item_id in movements):
            item_data = movements[inv_item_id]
        else:
            movements[inv_item_id] = item_data = item_dict()
        quantity_in = raw['inv_track_item.recv_quantity']
        if quantity_in:
            if (raw['inv_recv.date'] > latest):
                item_data['quantity_in_after'] += quantity_in
                continue
            else:
                item_data['quantity_in'] += quantity_in
        sites = item_data['sites']
        from_site = raw['inv_recv.from_site_id']
        if (from_site and (from_site not in sites)):
           ...