'Import item de-duplication
@todo: additionally have an onaccept sanitizing overlapping
allocations? (may be too simple, though)'

@staticmethod
def budget_allocation_duplicate(item):

    data = item.data
    budget_entity_id = data.get('budget_entity_id')
    cost_item_id = data.get('cost_item_id')
    if (budget_entity_id and cost_item_id):
        table = item.table
        start_date = data.get('start_date')
        end_date = data.get('end_date')
        query = (((table.budget_entity_id == budget_entity_id) & (table.cost_item_id == cost_item_id)) & ((table.start_date == None) | (table.start_date == start_date)))
        duplicate = current.db(query).select(table.id, limitby=(0, 1)).first()
        if duplicate:
            item.id = duplicate.id
            item.method = item.METHOD.UPDATE
    return
