summaryrefslogtreecommitdiffstats
path: root/kplato/kptschedule.h
blob: e1525c7908d70ec873094d80a7c5ac2d173141e0 (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
/* 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.
*/

#ifndef KPTSCHEDULE_H
#define KPTSCHEDULE_H

#include "kpteffortcostmap.h"
#include "kptresource.h"

#include <tqintdict.h>
#include <tqptrlist.h>
#include <tqstring.h>

class TQDomElement;
class TQStringList;

namespace KPlato
{

class Appointment;
class DateTime;
class Duration;
class Node;
class Task;

/**
 * The Schedule class holds data calculated during project
 * calculation and scheduling, eg start- and end-times and
 * appointments.
 * There is one schedule per node and one per resource.
 * Schedules can be of type Expected, Optimistic or Pessimistic
 * refering to which estimate is used for the calculation.
 * Schedule is subclassed into:
 * MainSchedule     Used by the main project.
 * NodeSchedule     Used by all other nodes (tasks).
 * ResourceSchedule Used by resources.
 */
class Schedule
{
public:
    //NOTE: Must match Effort::Use atm.
    enum Type { Expected=0, //Effort::Use_Expected
                Optimistic=1, //Effort::Use_Optimistic
                Pessimistic=2 //Effort::Use_Pessimistic
    };
    
    Schedule();
    Schedule(Schedule *tqparent);
    Schedule(TQString name, Type type, long id);
    ~Schedule();
    
    TQString name() const { return m_name; }
    void setName(TQString name) { m_name = name; }
    Type type() const { return m_type; }
    void setType(Type type) { m_type = type; }
    void setType(TQString type);
    TQString typeToString(bool translate=false) const;
    long id() const { return m_id; }
    void setId(long id) { m_id = id; }
    void setParent(Schedule *tqparent);
    Schedule *tqparent() const { return m_parent; }
    virtual bool isDeleted() const;
    virtual void setDeleted(bool on);
    
    virtual Resource *resource() const { return 0; }
    virtual Node *node() const { return 0; }
    
    virtual bool loadXML(const TQDomElement &element);
    virtual void saveXML(TQDomElement &element) const;
    void saveCommonXML(TQDomElement &element) const;
    void saveAppointments(TQDomElement &element) const;
    
    /// Return the list of appointments
    TQPtrList<Appointment> &appointments() { return m_appointments; }
    /// Adds appointment to this schedule only
    virtual bool add(Appointment *appointment);
    /// Adds appointment to both this resource schedule and node schedule
    virtual void addAppointment(Schedule */*other*/, DateTime &/*start*/, DateTime &/*end*/, double /*load*/=100) {}
    /// removes appointment and deletes it (independent of setAutoDelete)
    void removeAppointment(Appointment *appointment);
    /// removes appointment without deleting it (independent of setAutoDelete)
    void takeAppointment(Appointment *appointment);
    Appointment *findAppointment(Schedule *resource, Schedule *node);
    
    Appointment appointmentIntervals() const;
    
    virtual bool isOverbooked() const { return false; }
    virtual bool isOverbooked(const DateTime &/*start*/, const DateTime &/*end*/) const { return false; }
    virtual TQStringList overbookedResources() const;
    
    virtual EffortCostMap plannedEffortCostPrDay(const TQDate &start, const TQDate &end) const;
        
    /// Returns the total planned effort for this task (or subtasks) 
    virtual Duration plannedEffort() const;
    /// Returns the total planned effort for this task (or subtasks) on date
    virtual Duration plannedEffort(const TQDate &date) const;
    /// Returns the planned effort up to and including date
    virtual Duration plannedEffortTo(const TQDate &date) const;
    
    /// Returns the total actual effort for this task (or subtasks) 
    virtual Duration actualEffort() const;
    /// Returns the total actual effort for this task (or subtasks) on date
    virtual Duration actualEffort(const TQDate &date) const;
    /// Returns the total actual effort for this task (or subtasks) up to and including date
    virtual Duration actualEffortTo(const TQDate &date) const;
    
    /**
     * Planned cost is the sum total of all resources and other costs
     * planned for this node.
     */
    virtual double plannedCost() const; 
    
    /// Planned cost on date
    virtual double plannedCost(const TQDate &date) const;
    /**
     * Planned cost from start of activity up to and including date
     * is the sum of all resource costs and other costs planned for this node.
     */
    virtual double plannedCostTo(const TQDate &date) const;
    /**
     * Actual cost is the sum total of the reported costs actually used
     * for this node.
     */
    virtual double actualCost() const;
    /// Actual cost on date
    virtual double actualCost(const TQDate &date) const;
    /// Actual cost up to and including date
    virtual double actualCostTo(const TQDate &date) const;
    
    /// Effort based performance index
    double effortPerformanceIndex(const TQDate &/*date*/, bool */*error=0*/) { return 0.0; }
    /// Cost performance index
    double costPerformanceIndex(const TQDate &/*date*/, bool */*error=0*/) { return 0.0; }

    virtual double normalRatePrHour() const { return 0.0; }

    void setEarliestStart(DateTime &dt) { earliestStart = dt; }
    void setLatestFinish(DateTime &dt) { latestFinish = dt; }

    virtual void initiateCalculation();
    virtual void calcResourceOverbooked();

    void setScheduled(bool on) { notScheduled = !on; }
    bool isScheduled() const { return !notScheduled; }
    
    DateTime start() const { return startTime; }
    DateTime end() const { return endTime; }
    
protected:
    TQString m_name;
    Type m_type;
    long m_id;
    bool m_deleted;

    TQPtrList<Appointment> m_appointments;
    Schedule *m_parent;
    
    friend class Node;
    friend class Task;
    friend class Project;
    friend class Resource;
    friend class RecalculateProjectCmd;
    /** 
      * earliestStart is calculated by PERT/CPM.
      * A task may be scheduled to start later because of constraints
      * or resource availability etc.
      */
    DateTime earliestStart;
    /** 
      * latestFinish is calculated by PERT/CPM.
      * A task may be scheduled to finish earlier because of constraints
      * or resource availability etc.
      */
    DateTime latestFinish;
    /**  startTime is the scheduled start time.
      *  It depends on constraints (i.e. ASAP/ALAP) and resource availability.
      *  It will always be later or equal to @ref earliestStart
      */
    DateTime startTime;
    /**  
      *  m_endTime is the scheduled finish time.
      *  It depends on constraints (i.e. ASAP/ALAP) and resource availability.
      *  It will always be earlier or equal to @ref latestFinish
      */
    DateTime endTime;
    /**  
      *  duration is the scheduled duration which depends on
      *  e.g. estimated effort, allocated resources and risk
      */
    Duration duration;

    /// Set if EffortType == Effort, but no resource is requested
    bool resourceError;
    /// Set if the assigned resource is overbooked
    bool resourceOverbooked;
    /// Set if the requested resource is not available
    bool resourceNotAvailable;
    /// Set if the task cannot be scheduled to fullfill all the constraints
    bool schedulingError;
    /// Set if the node has not been scheduled
    bool notScheduled;

    DateTime workStartTime;
    DateTime workEndTime;
    bool inCriticalPath;

#ifndef NDEBUG
public:
    virtual void printDebug(TQString ident);
#endif
};

/**
 * NodeSchedule holds scheduling information for a node (task).
 * 
 */
class NodeSchedule : public Schedule
{
public:
    NodeSchedule();
    NodeSchedule(Node *node, TQString name, Schedule::Type type, long id);
    NodeSchedule(Schedule *tqparent, Node *node);
    ~NodeSchedule();
    
    virtual bool isDeleted() const 
        { return m_parent == 0 ? true : m_parent->isDeleted(); }
    void setDeleted(bool on);
    
    virtual bool loadXML(const TQDomElement &element);
    virtual void saveXML(TQDomElement &element) const;

// tasks------------>
    virtual void addAppointment(Schedule *resource, DateTime &start, DateTime &end, double load=100);
    
    virtual Node *node() const { return m_node; }
    virtual void setNode(Node *n) { m_node = n; }
    
protected:
    void init();

private:
    Node *m_node;
    
#ifndef NDEBUG
public:
    virtual void printDebug(TQString ident);
#endif
};

/**
 * ResourceSchedule holds scheduling information for a resource.
 * 
 */
class ResourceSchedule : public Schedule
{
public:
    ResourceSchedule();
    ResourceSchedule(Resource *Resource, TQString name, Schedule::Type type, long id);
    ResourceSchedule(Schedule *tqparent, Resource *Resource);
    ~ResourceSchedule();
    
    virtual bool isDeleted() const 
        { return m_parent == 0 ? true : m_parent->isDeleted(); }
    virtual void addAppointment(Schedule *node, DateTime &start, DateTime &end, double load=100);
    
    virtual bool isOverbooked() const;
    virtual bool isOverbooked(const DateTime &start, const DateTime &end) const;
    Appointment appointmentIntervals() const;
    
    virtual Resource *resource() const { return m_resource; }
    virtual double normalRatePrHour() const;
    
private:
    Resource *m_resource;
    Schedule *m_parent;

#ifndef NDEBUG
public:
    virtual void printDebug(TQString ident);
#endif
};

/**
 * MainSchedule holds scheduling information for the main project node.
 * 
 */
class MainSchedule : public NodeSchedule
{
public:
    MainSchedule();
    MainSchedule(Node *node, TQString name, Schedule::Type type, long id);
    ~MainSchedule();
    virtual bool isDeleted() const { return m_deleted; }

    virtual bool loadXML(const TQDomElement &element, Project &project);
    virtual void saveXML(TQDomElement &element) const;

private:

#ifndef NDEBUG
public:
    virtual void printDebug(TQString ident);
#endif
};


} //namespace KPlato

#endif