void ResourceModelTester::internalAppointments()
{
    ScheduleManager *sm = m_project->createScheduleManager( "Test Plan" );
    m_project->addScheduleManager( sm );
    sm->createSchedules();
    m_project->calculate( *sm );
    long id = sm->scheduleId();
    m_model.setScheduleManager( sm );

    //printDebug( sm->scheduleId() );

    QModelIndex idx;
    // resource group
    int rows = m_model.rowCount( idx );
    QCOMPARE( rows, 1 );
    QModelIndex gidx = m_model.index( 0, 0, idx );
    QVERIFY( gidx.isValid() );

    // reosurce
    rows = m_model.rowCount( gidx );
    QCOMPARE( rows, 1 );
    QModelIndex ridx = m_model.index( 0, 0, gidx );
    QCOMPARE( m_resource->name(), m_model.data( ridx ).toString() );

    ridx = m_model.index( m_resource );
    QVERIFY( ridx.isValid() );

    // appointment
    rows = m_model.rowCount( ridx );
    QCOMPARE( rows, m_resource->numAppointments( id ) );
    QCOMPARE( rows, 1 );

    QModelIndex aidx = m_model.index( 0, 0, ridx ); // first appointment
    QVERIFY( aidx.isValid() );
    rows = m_model.rowCount( aidx ); // num intervals
    QCOMPARE( rows, 1 );

    // interval
    QModelIndex iidx = m_model.index( 0, 0, aidx ); // first interval
    QVERIFY( iidx.isValid() );
    rows = m_model.rowCount( iidx ); // intervals don't have children
    QCOMPARE( rows, 0 );

    // appointment
    idx = m_model.parent( iidx );
    QCOMPARE( idx, aidx );
    // resource
    idx = m_model.parent( aidx );
    QCOMPARE( idx, ridx );

    // resource group
    idx = m_model.parent( ridx );
    QCOMPARE( idx, gidx );

    // top
    idx = m_model.parent( gidx );
    QVERIFY( ! idx.isValid() );

}
void ResourceModelTester::externalOverbook()
{
    DateTime targetstart = m_project->constraintStartTime();
    DateTime targetend = m_project->constraintEndTime();
    Task *t = m_task;
    Resource *r = m_resource;

    r->addExternalAppointment( "Ext-1", "External project 1", targetstart, targetstart.addDays( 1 ), 100 );
    r->addExternalAppointment( "Ext-1", "External project 1", targetend.addDays( -1 ), targetend, 100 );

    ScheduleManager *sm = m_project->createScheduleManager( "Test Plan" );
    m_project->addScheduleManager( sm );
    sm->setAllowOverbooking( true );
    sm->createSchedules();
    m_project->calculate( *sm );
    long id = sm->scheduleId();
    m_model.setScheduleManager( sm );
    //printSchedulingLog( *sm );
    //printDebug( id );

    // resource group
    QModelIndex idx;
    int rows = m_model.rowCount( idx );
    QCOMPARE( rows, 1 );
    idx = m_model.index( 0, 0, idx );
    QVERIFY( idx.isValid() );
    
    // resource
    rows = m_model.rowCount( idx );
    QCOMPARE( rows, 1 );
    idx = m_model.index( 0, 0, idx );
    QCOMPARE( m_resource->name(), m_model.data( idx ).toString() );

    idx = m_model.index( m_resource );
    QVERIFY( idx.isValid() );

    // appointments
    rows = m_model.rowCount( idx );
    QCOMPARE( rows, m_resource->numAppointments( id ) + m_resource->numExternalAppointments() );
    QCOMPARE( rows, 2 ); // one internal, one external

    // internal appointment
    QModelIndex aidx = m_model.index( 0, 0, idx ); // first appointment (internal)
    QVERIFY( aidx.isValid() );
    rows = m_model.rowCount( aidx ); // num intervals
    QCOMPARE( rows, 1 );

    QModelIndex iidx = m_model.index( 0, 0, aidx ); // first interval
    QVERIFY( iidx.isValid() );
    rows = m_model.rowCount( iidx ); // intervals don't have children
    QCOMPARE( rows, 0 );

    // external appointment
    aidx = m_model.index( 1, 0, idx ); // second appointment (external)
    QVERIFY( aidx.isValid() );
    rows = m_model.rowCount( aidx ); // num intervals
    QCOMPARE( rows, 2 );

    iidx = m_model.index( 0, 0, aidx ); // first interval
    QVERIFY( iidx.isValid() );
    rows = m_model.rowCount( iidx ); // intervals don't have children
    QCOMPARE( rows, 0 );

    iidx = m_model.index( 1, 0, aidx ); // second interval
    QVERIFY( iidx.isValid() );
    rows = m_model.rowCount( iidx ); // intervals don't have children
    QCOMPARE( rows, 0 );


    QCOMPARE( t->startTime(), m_calendar->firstAvailableAfter( targetstart, t->endTime() ) );
    QCOMPARE( t->endTime(), t->startTime() + Duration( 0, 8, 0 ) );

}