summaryrefslogtreecommitdiffstats
path: root/kplato/kptappointment.cpp
blob: f09b4bcc603f220335f4598023c9ade7970a7e5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/* This file is part of the KDE project
   Copyright (C) 2005 Dag Andersen <danders@get2net.dk>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation;
   version 2 of the License.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "kptappointment.h"

#include "kptproject.h"
#include "kpttask.h"
#include "kptdatetime.h"
#include "kptcalendar.h"
#include "kpteffortcostmap.h"
#include "kptschedule.h"

#include <kdebug.h>

namespace KPlato
{

class Resource;

AppointmentInterval::AppointmentInterval() {
    m_load = 100.0; 
}
AppointmentInterval::AppointmentInterval(const AppointmentInterval &interval) {
    //kdDebug()<<k_funcinfo<<endl;
    m_start = interval.startTime(); 
    m_end = interval.endTime(); 
    m_load = interval.load(); 
}
AppointmentInterval::AppointmentInterval(const DateTime &start, const DateTime end, double load) {
    //kdDebug()<<k_funcinfo<<endl;
    m_start = start; 
    m_end = end; 
    m_load = load; 
}
AppointmentInterval::~AppointmentInterval() {
    //kdDebug()<<k_funcinfo<<endl;
}

Duration AppointmentInterval::effort(const DateTime &start, const DateTime end) const {
    if (start >= m_end || end <= m_start) {
        return Duration::zeroDuration;
    }
    DateTime s = (start > m_start ? start : m_start);
    DateTime e = (end < m_end ? end : m_end);
    return (e - s) * m_load / 100;
}

Duration AppointmentInterval::effort(const DateTime &time, bool upto) const {
    if (upto) {
        if (time <= m_start) {
            return Duration::zeroDuration;
        }
        DateTime e = (time < m_end ? time : m_end);
        return (e - m_start) * m_load / 100;
    }
    // from time till end
    if (time >= m_end) {
        return Duration::zeroDuration;
    }
    DateTime s = (time > m_start ? time : m_start);
    return (m_end - s) * m_load / 100;
}

bool AppointmentInterval::loadXML(TQDomElement &element) {
    //kdDebug()<<k_funcinfo<<endl;
    bool ok;
    TQString s = element.attribute("start");
    if (s != "")
        m_start = DateTime::fromString(s);
    s = element.attribute("end");
    if (s != "")
        m_end = DateTime::fromString(s);
    m_load = element.attribute("load", "100").toDouble(&ok);
    if (!ok) m_load = 100;
    return m_start.isValid() && m_end.isValid();
}

void AppointmentInterval::saveXML(TQDomElement &element) const {
    TQDomElement me = element.ownerDocument().createElement("interval");
    element.appendChild(me);

    me.setAttribute("start", m_start.toString(Qt::ISODate));
    me.setAttribute("end", m_end.toString(Qt::ISODate));
    me.setAttribute("load", m_load);
}

bool AppointmentInterval::isValid() const {
    return m_start.isValid() && m_end.isValid();
}

AppointmentInterval AppointmentInterval::firstInterval(const AppointmentInterval &interval, const DateTime &from) const {
    //kdDebug()<<k_funcinfo<<interval.startTime().toString()<<" - "<<interval.endTime().toString()<<" from="<<from.toString()<<endl;
    DateTime f = from;
    DateTime s1 = m_start;
    DateTime e1 = m_end;
    DateTime s2 = interval.startTime();
    DateTime e2 = interval.endTime();
    AppointmentInterval a;
    if (f.isValid() && f >= e1 && f >= e2) {
        return a;
    }
    if (f.isValid()) {
        if (s1 < f && f < e1) {
            s1 = f;
        }
        if (s2 < f && f < e2) {
            s2 = f;
        }
    } else {
        f = s1 < s2 ? s1 : s2;
    }
    if (s1 < s2) {
        a.setStartTime(s1);
        if (e1 <= s2) {
            a.setEndTime(e1);
        } else {
            a.setEndTime(s2);
        }
        a.setLoad(m_load);
    } else if (s1 > s2) {
        a.setStartTime(s2);
        if (e2 <= s1) {
            a.setEndTime(e2);
        } else {
            a.setEndTime(s1);
        }
        a.setLoad(interval.load());
    } else {
        a.setStartTime(s1);
        if (e1 <= e2)
            a.setEndTime(e1);
        else 
            a.setEndTime(e2);
        a.setLoad(m_load + interval.load());
    }
    //kdDebug()<<k_funcinfo<<a.startTime().toString()<<" - "<<a.endTime().toString()<<" load="<<a.load()<<endl;
    return a;
}

//////

Appointment::UsedEffortItem::UsedEffortItem(TQDate date, Duration effort, bool overtime) {
    m_date = date;
    m_effort = effort;
    m_overtime = overtime;
}
TQDate Appointment::UsedEffortItem::date() { 
    return m_date; 
}
Duration Appointment::UsedEffortItem::effort() {
    return m_effort; 
}
bool Appointment::UsedEffortItem::isOvertime() { 
    return m_overtime; 
}

Appointment::UsedEffort::UsedEffort() { 
    setAutoDelete(true); 
}

void Appointment::UsedEffort::inSort(TQDate date, Duration effort, bool overtime) {
    UsedEffortItem *item = new UsedEffortItem(date, effort, overtime);
    TQPtrList<UsedEffortItem>::inSort(item);
}

Duration Appointment::UsedEffort::usedEffort(bool includeOvertime) const {
    Duration eff;
    TQPtrListIterator<UsedEffortItem> it(*this);
    for (; it.current(); ++it) {
        if (includeOvertime || !it.current()->isOvertime()) {
            eff += it.current()->effort();
        }
    }
    return eff;
}
        
Duration Appointment::UsedEffort::usedEffort(const TQDate &date, bool includeOvertime) const {
    Duration eff;
    TQPtrListIterator<UsedEffortItem> it(*this);
    for (; it.current(); ++it) {
        if ((includeOvertime || !it.current()->isOvertime()) && 
            it.current()->date() == date) {
            eff += it.current()->effort();
        }
    }
    return eff;
}

Duration Appointment::UsedEffort::usedEffortTo(const TQDate &date, bool includeOvertime) const {
    Duration eff;
    TQPtrListIterator<UsedEffortItem> it(*this);
    for (; it.current(); ++it) {
        if ((includeOvertime || !it.current()->isOvertime()) && 
            it.current()->date() <= date) {
            eff += it.current()->effort();
        }
    }
    return eff;
}

Duration Appointment::UsedEffort::usedOvertime() const {
    UsedEffortItem *item = getFirst();
    return item==0 ? Duration::zeroDuration : usedOvertime(item->date());
}

Duration Appointment::UsedEffort::usedOvertime(const TQDate &date) const {
    Duration eff;
    TQPtrListIterator<UsedEffortItem> it(*this);
    for (; it.current(); ++it) {
        if (it.current()->isOvertime() && it.current()->date() == date) {
            eff += it.current()->effort();
        }
    }
    return eff;
}

Duration Appointment::UsedEffort::usedOvertimeTo(const TQDate &date) const {
    Duration eff;
    TQPtrListIterator<UsedEffortItem> it(*this);
    for (; it.current(); ++it) {
        if (it.current()->isOvertime() && it.current()->date() <= date) {
            eff += it.current()->effort();
        }
    }
    return eff;
}

bool Appointment::UsedEffort::load(TQDomElement &element) {
    TQString s;
    TQDomNodeList list = element.childNodes();
    for (unsigned int i=0; i<list.count(); ++i) {
        if (list.item(i).isElement()) {
            TQDomElement e = list.item(i).toElement();
            if (e.tagName() == "actual-effort") {
                TQDate date;
                s = e.attribute("date");
                if (s != "")
                    date = TQDate::fromString(s, Qt::ISODate);
                Duration eff = Duration::fromString(e.attribute("effort"));
                bool ot = e.attribute("overtime", "0").toInt();
                if (date.isValid()) {
                    inSort(date, eff, ot);
                } else {
                    kdError()<<k_funcinfo<<"Load failed, illegal date: "<<e.attribute("date")<<endl;
                }
            }
        }   
    }
    return true;
}

void Appointment::UsedEffort::save(TQDomElement &element) const {
    if (isEmpty()) return;
    TQPtrListIterator<UsedEffortItem> it = *this;
    for (; it.current(); ++it) {
        TQDomElement me = element.ownerDocument().createElement("actual-effort");
        element.appendChild(me);
        me.setAttribute("date",it.current()->date().toString(Qt::ISODate));
        me.setAttribute("effort",it.current()->effort().toString());
        me.setAttribute("overtime",it.current()->isOvertime());
    }
}

int Appointment::UsedEffort::compareItems(TQPtrCollection::Item item1, TQPtrCollection::Item item2) {
    TQDate d1 = static_cast<UsedEffortItem*>(item1)->date();
    TQDate d2 = static_cast<UsedEffortItem*>(item2)->date();
    if (d1 > d2) return 1;
    if (d1 < d2) return -1;
    return 0;
}

////
Appointment::Appointment() 
    : m_extraRepeats(), m_skipRepeats() {
    //kdDebug()<<k_funcinfo<<"("<<this<<")"<<endl;
    m_resource=0;
    m_node=0;
    m_repeatInterval=Duration();
    m_repeatCount=0;

    m_intervals.setAutoDelete(true);
}

Appointment::Appointment(Schedule *resource, Schedule *node, DateTime start, DateTime end, double load) 
    : m_extraRepeats(), 
      m_skipRepeats() {
    //kdDebug()<<k_funcinfo<<"("<<this<<")"<<endl;
    m_node = node;
    m_resource = resource;
    m_repeatInterval = Duration();
    m_repeatCount = 0;

    addInterval(start, end, load);

    m_intervals.setAutoDelete(true);
}

Appointment::Appointment(Schedule *resource, Schedule *node, DateTime start, Duration duration, double load) 
    : m_extraRepeats(), 
      m_skipRepeats() {
    //kdDebug()<<k_funcinfo<<"("<<this<<")"<<endl;
    m_node = node;
    m_resource = resource;
    m_repeatInterval = Duration();
    m_repeatCount = 0;

    addInterval(start, duration, load);
    
    m_intervals.setAutoDelete(true);
}

Appointment::~Appointment() {
    //kdDebug()<<k_funcinfo<<"("<<this<<")"<<endl;
    detach();
}

void Appointment::addInterval(AppointmentInterval *a) {
    //kdDebug()<<k_funcinfo<<m_resource->name()<<" to "<<m_node->name()<<endl;
    m_intervals.inSort(a);
}
void Appointment::addInterval(const DateTime &start, const DateTime &end, double load) {
    addInterval(new AppointmentInterval(start, end, load));
}
void Appointment::addInterval(const DateTime &start, const Duration &duration, double load) {
    DateTime e = start+duration;
    addInterval(start, e, load);
}

double Appointment::maxLoad() const {
    double v = 0.0;
    TQPtrListIterator<AppointmentInterval> it = m_intervals;
    for (; it.current(); ++it) {
        if (v < it.current()->load())
            v = it.current()->load();
    }
    return v;
}

DateTime Appointment::startTime() const {
    DateTime t;
    TQPtrListIterator<AppointmentInterval> it = m_intervals;
    for (; it.current(); ++it) {
        if (!t.isValid() || t > it.current()->startTime())
            t = it.current()->startTime();
    }
    return t;
}

DateTime Appointment::endTime() const {
    DateTime t;
    TQPtrListIterator<AppointmentInterval> it = m_intervals;
    for (; it.current(); ++it) {
        if (!t.isValid() || t < it.current()->endTime())
            t = it.current()->endTime();
    }
    return t;
}

void Appointment::deleteAppointmentFromRepeatList(DateTime) {
}

void Appointment::addAppointmentToRepeatList(DateTime) {
}

bool Appointment::isBusy(const DateTime &/*start*/, const DateTime &/*end*/) {
    return false;
}

bool Appointment::loadXML(TQDomElement &element, Project &project, Schedule &sch) {
    //kdDebug()<<k_funcinfo<<endl;
    TQDictIterator<Node> it = project.nodeDict();
/*    for (; it.current(); ++it) {
        kdDebug()<<" Node="<<it.current()->name()<<" id="<<it.currentKey()<<endl;
    }*/
    Node *node = project.findNode(element.attribute("task-id"));
    if (node == 0) {
        kdError()<<k_funcinfo<<"The referenced task does not exists: "<<element.attribute("task-id")<<endl;
        return false;
    }
    Resource *res = project.resource(element.attribute("resource-id"));
    if (res == 0) {
        kdError()<<k_funcinfo<<"The referenced resource does not exists: resource id="<<element.attribute("resource-id")<<endl;
        return false;
    }
    if (!res->addAppointment(this, sch)) {
        kdError()<<k_funcinfo<<"Failed to add appointment to resource: "<<res->name()<<endl;
        return false;
    }
    if (!node->addAppointment(this, sch)) {
        kdError()<<k_funcinfo<<"Failed to add appointment to node: "<<node->name()<<endl;
        m_resource->takeAppointment(this);
        return false;
    }
    //kdDebug()<<k_funcinfo<<"res="<<m_resource<<" node="<<m_node<<endl;
    TQDomNodeList list = element.childNodes();
    for (unsigned int i=0; i<list.count(); ++i) {
        if (list.item(i).isElement()) {
            TQDomElement e = list.item(i).toElement();
            if (e.tagName() == "interval") {
            AppointmentInterval *a = new AppointmentInterval();
                if (a->loadXML(e)) {
                    addInterval(a);
                } else {
                    kdError()<<k_funcinfo<<"Could not load interval"<<endl;
                    delete a;
                }
            }
        }
    }
    if (m_intervals.isEmpty()) {
        return false; 
    }
    m_actualEffort.load(element);
    return true;
}

void Appointment::saveXML(TQDomElement &element) const {
    if (m_intervals.isEmpty()) {
        kdError()<<k_funcinfo<<"Incomplete appointment data: No intervals"<<endl;
    }
    if (m_resource == 0 || m_resource->resource() == 0) {
        kdError()<<k_funcinfo<<"Incomplete appointment data: No resource"<<endl;
        return;
    }
    if (m_node == 0 || m_node->node() == 0) {
        kdError()<<k_funcinfo<<"Incomplete appointment data: No node"<<endl;
        return; // shouldn't happen
    }
    //kdDebug()<<k_funcinfo<<endl;
    TQDomElement me = element.ownerDocument().createElement("appointment");
    element.appendChild(me);

    me.setAttribute("resource-id", m_resource->resource()->id());
    me.setAttribute("task-id", m_node->node()->id());
    TQPtrListIterator<AppointmentInterval> it = m_intervals;
    for (; it.current(); ++it) {
        it.current()->saveXML(me);
    }
    m_actualEffort.save(me);
}

// Returns the total actual effort for this appointment
Duration Appointment::plannedEffort() const {
    Duration d;
    TQPtrListIterator<AppointmentInterval> it = m_intervals;
    for (; it.current(); ++it) {
        d += it.current()->effort();
    }
    return d;
}

// Returns the planned effort on the date
Duration Appointment::plannedEffort(const TQDate &date) const {
    Duration d;
    DateTime s(date);
    DateTime e(date.addDays(1));
    TQPtrListIterator<AppointmentInterval> it = m_intervals;
    for (; it.current(); ++it) {
        d += it.current()->effort(s, e);
    }
    return d;
}

// Returns the planned effort upto and including the date
Duration Appointment::plannedEffortTo(const TQDate& date) const {
    Duration d;
    DateTime e(date.addDays(1));
    TQPtrListIterator<AppointmentInterval> it = m_intervals;
    for (; it.current(); ++it) {
        d += it.current()->effort(e, true);
    }
    return d;
}

// Returns a list of efforts pr day for interval start, end inclusive
// The list only includes days with any planned effort
EffortCostMap Appointment::plannedPrDay(const TQDate& start, const TQDate& end) const {
    //kdDebug()<<k_funcinfo<<m_node->id()<<", "<<m_resource->id()<<endl;
    EffortCostMap ec;
    Duration eff;
    DateTime dt(start);
    DateTime ndt(dt.addDays(1));
    double rate = m_resource->normalRatePrHour();
    AppointmentIntervalListIterator it = m_intervals;
    for (; it.current(); ++it) {
        DateTime st = it.current()->startTime();
        DateTime e = it.current()->endTime();
        if (end < st.date())
            break;
        if (dt.date() < st.date()) {
            dt.setDate(st.date());
        }
        ndt = TQDateTime(dt.addDays(1));
        while (dt.date() <= e.date()) {
            eff = it.current()->effort(dt, ndt);
            ec.add(dt.date(), eff, eff.toDouble(Duration::Unit_h) * rate);
            if (dt.date() < e.date() ) {
                // loop trough the interval (it spans dates)
                dt = ndt;
                ndt = TQDateTime(ndt.addDays(1));
            } else {
                break;
            }
        }
    }
    return ec;
}


// Returns the total actual effort for this appointment
Duration Appointment::actualEffort() const {
    return m_actualEffort.usedEffort();
}

// Returns the actual effort on the date
Duration Appointment::actualEffort(const TQDate &date) const {
    return m_actualEffort.usedEffort(date);
}

// Returns the actual effort upto and including date
Duration Appointment::actualEffortTo(const TQDate &date) const {
    return m_actualEffort.usedEffortTo(date);
}

double Appointment::plannedCost() {
    if (m_resource && m_resource->resource()) {
        return plannedEffort().toDouble(Duration::Unit_h) * m_resource->resource()->normalRate(); //FIXME overtime
    }
    return 0.0;
}

//Calculates the planned cost on date
double Appointment::plannedCost(const TQDate &date) {
    if (m_resource && m_resource->resource()) {
        return plannedEffort(date).toDouble(Duration::Unit_h) * m_resource->resource()->normalRate(); //FIXME overtime
    }
    return 0.0;
}

//Calculates the planned cost upto and including date
double Appointment::plannedCostTo(const TQDate &date) {
    if (m_resource && m_resource->resource()) {
        return plannedEffortTo(date).toDouble(Duration::Unit_h) * m_resource->resource()->normalRate(); //FIXME overtime
    }
    return 0.0;
}

// Calculates the total actual cost for this appointment
double Appointment::actualCost() {
    //kdDebug()<<k_funcinfo<<m_actualEffort.usedEffort(false /*ex. overtime*/).toDouble(Duration::Unit_h)<<endl;
    if (m_resource && m_resource->resource()) {
        return (m_actualEffort.usedEffort(false /*ex. overtime*/).toDouble(Duration::Unit_h)*m_resource->resource()->normalRate()) + (m_actualEffort.usedOvertime().toDouble(Duration::Unit_h)*m_resource->resource()->overtimeRate());
    }
    return 0.0;
}

// Calculates the actual cost on date
double Appointment::actualCost(const TQDate &date) {
    if (m_resource && m_resource->resource()) {
        return (m_actualEffort.usedEffort(date, false /*ex. overtime*/).toDouble(Duration::Unit_h)*m_resource->resource()->normalRate()) + (m_actualEffort.usedOvertime(date).toDouble(Duration::Unit_h)*m_resource->resource()->overtimeRate());
    }
    return 0.0;
}

// Calculates the actual cost upto and including date
double Appointment::actualCostTo(const TQDate &date) {
    if (m_resource && m_resource->resource()) {
        return (m_actualEffort.usedEffortTo(date, false /*ex. overtime*/).toDouble(Duration::Unit_h)*m_resource->resource()->normalRate()) + (m_actualEffort.usedOvertimeTo(date).toDouble(Duration::Unit_h)*m_resource->resource()->overtimeRate());
    }
    return 0.0;
}

void Appointment::addActualEffort(TQDate date, Duration effort, bool overtime) {
    m_actualEffort.inSort(date, effort, overtime);
}

bool Appointment::attach() { 
    //kdDebug()<<k_funcinfo<<"("<<this<<")"<<endl;
    if (m_resource && m_node) {
        m_resource->add(this);
        m_node->add(this);
        return true;
    }
    kdWarning()<<k_funcinfo<<"Failed: "<<(m_resource ? "" : "resource=0 ") 
                                       <<(m_node ? "" : "node=0")<<endl;
    return false;
}

void Appointment::detach() {
    //kdDebug()<<k_funcinfo<<"("<<this<<")"<<endl;
    if (m_resource) {
        m_resource->takeAppointment(this); // takes from node also
    }
    if (m_node) {
        m_node->takeAppointment(this); // to make it robust
    }
}

// Returns the effort from start to end
Duration Appointment::effort(const DateTime &start, const DateTime &end) const {
    Duration d;
    TQPtrListIterator<AppointmentInterval> it = m_intervals;
    for (; it.current(); ++it) {
        d += it.current()->effort(start, end);
    }
    return d;
}
// Returns the effort from start for the duration
Duration Appointment::effort(const DateTime &start, const Duration &duration) const {
    Duration d;
    TQPtrListIterator<AppointmentInterval> it = m_intervals;
    for (; it.current(); ++it) {
        d += it.current()->effort(start, start+duration);
    }
    return d;
}
// Returns the effort upto time / from time 
Duration Appointment::effortFrom(const DateTime &time) const {
    Duration d;
    TQPtrListIterator<AppointmentInterval> it = m_intervals;
    for (; it.current(); ++it) {
        d += it.current()->effort(time, false);
    }
    return d;
}

Appointment &Appointment::operator=(const Appointment &app) {
    m_resource = app.resource();
    m_node = app.node();
    m_repeatInterval = app.repeatInterval();
    m_repeatCount = app.repeatCount();

    m_intervals.clear();
    TQPtrListIterator<AppointmentInterval> it = app.intervals();
    for (; it.current(); ++it) {
        addInterval(new AppointmentInterval(*(it.current())));
    }
    return *this;
}

Appointment &Appointment::operator+=(const Appointment &app) {
    *this = *this + app;
    return *this;
}

Appointment Appointment::operator+(const Appointment &app) {
    Appointment a;
    AppointmentIntervalList ai = app.intervals();
    AppointmentInterval i;
    AppointmentInterval *i1 = m_intervals.first();
    AppointmentInterval *i2 = ai.first();
    DateTime from;
    while (i1 || i2) {
        if (!i1) {
            if (!from.isValid() || from < i2->startTime())
                from = i2->startTime();
            a.addInterval(from, i2->endTime(), i2->load());
            //kdDebug()<<"Interval+ (i2): "<<from.toString()<<" - "<<i2->endTime().toString()<<endl;
            from = i2->endTime();
            i2 = ai.next();
            continue;
        }
        if (!i2) {
            if (!from.isValid() || from < i1->startTime())
                from = i1->startTime();
            a.addInterval(from, i1->endTime(), i1->load());
            //kdDebug()<<"Interval+ (i1): "<<from.toString()<<" - "<<i1->endTime().toString()<<endl;
            from = i1->endTime();
            i1 = m_intervals.next();
            continue;
        }
        i =  i1->firstInterval(*i2, from);
        if (!i.isValid()) {
            break;
        }
        a.addInterval(i);
        from = i.endTime();
        //kdDebug()<<"Interval+ (i): "<<i.startTime().toString()<<" - "<<i.endTime().toString()<<" load="<<i.load()<<endl;
        if (i1 && a.endTime() >= i1->endTime()) {
            i1 = m_intervals.next();
        }
        if (i2 && a.endTime() >= i2->endTime()) {
            i2 = ai.next();
        }
    }
    return a;
}

#ifndef NDEBUG
void Appointment::printDebug(TQString indent)
{
    bool err = false;
    if (m_node == 0) {
        kdDebug()<<indent<<"   No node schedule"<<endl;
        err = true;
    } else if (m_node->node() == 0) {
        kdDebug()<<indent<<"   No node"<<endl;
        err = true;
    }
    if (m_resource == 0) {
        kdDebug()<<indent<<"   No resource schedule"<<endl;
        err = true;
    } else if (m_resource->resource() == 0) {
        kdDebug()<<indent<<"   No resource"<<endl;
        err = true;
    }
    if (err)
        return;
    kdDebug()<<indent<<"  + Appointment to schedule: "<<m_node->name()<<" ("<<m_node->type()<<")"<<" resource: "<<m_resource->resource()->name()<<endl;
    indent += "  ! ";
    TQPtrListIterator<AppointmentInterval> it = intervals();
    for (; it.current(); ++it) {
        kdDebug()<<indent<<it.current()->startTime().toString()<<" - "<<it.current()->endTime().toString()<<" load="<<it.current()->load()<<endl;
    }
}
#endif

}  //KPlato namespace