1 /* This file is part of the KDE project
2    Copyright (C) 2010 Dag Andersen <danders@get2net.dk>
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation;
7    version 2 of the License.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13 
14    You should have received a copy of the GNU Library General Public License
15    along with this library; see the file COPYING.LIB.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17    Boston, MA 02110-1301, USA.
18 */
19 
20 // clazy:excludeall=qstring-arg
21 #include "kpteffortcostmap.h"
22 
23 namespace KPlato
24 {
25 
add(const Duration & effort,double cost,double bcwpEffort,double bcwpCost)26 void EffortCost::add(const Duration &effort, double cost, double bcwpEffort, double bcwpCost)
27 {
28     m_effort += effort;
29     m_cost += cost;
30     m_bcwpEffort += bcwpEffort;
31     m_bcwpCost += bcwpCost;
32 }
33 
34 //-----------------------
EffortCostMap(const EffortCostMap & map)35 EffortCostMap::EffortCostMap(const EffortCostMap &map)
36 {
37     m_days = map.m_days;
38 }
39 
insert(const QDate & date,const EffortCost & ec)40 void EffortCostMap::insert(const QDate &date, const EffortCost &ec)
41 {
42     Q_ASSERT(date.isValid());
43     m_days[ date ] = ec;
44 }
45 
operator =(const EffortCostMap & ec)46 EffortCostMap &EffortCostMap::operator=(const EffortCostMap &ec)
47 {
48     m_days = ec.m_days;
49     return *this;
50 }
51 
operator +=(const EffortCostMap & ec)52 EffortCostMap &EffortCostMap::operator+=(const EffortCostMap &ec) {
53     //debugPlan<<"me="<<m_days.count()<<" ec="<<ec.days().count();
54     if (ec.isEmpty()) {
55         return *this;
56     }
57     if (isEmpty()) {
58         m_days = ec.days();
59         return *this;
60     }
61     EffortCostMap other = ec;
62     QDate oed = other.endDate();
63     QDate ed = endDate();
64     // get bcwp of the last entries
65     EffortCost last_oec = other.m_days[oed];
66     last_oec.setEffort(Duration::zeroDuration);
67     last_oec.setCost(0.0);
68     EffortCost last_ec = m_days[ed];
69     last_ec.setEffort(Duration::zeroDuration);
70     last_ec.setCost(0.0);
71     if (oed > ed) {
72         // expand my last entry to match other
73         for (QDate d = ed.addDays(1); d <= oed; d = d.addDays(1)) {
74             m_days[ d ] = last_ec ;
75         }
76     }
77     EffortCostDayMap::const_iterator it;
78     for(it = ec.days().constBegin(); it != ec.days().constEnd(); ++it) {
79         add(it.key(), it.value());
80     }
81     if (oed < ed) {
82         // add others last entry to my trailing entries
83         for (QDate d = oed.addDays(1); d <= ed; d = d.addDays(1)) {
84             m_days[ d ] += last_oec;
85         }
86     }
87     return *this;
88 }
89 
addBcwpCost(const QDate & date,double cost)90 void EffortCostMap::addBcwpCost(const QDate &date, double cost)
91 {
92     EffortCost ec = m_days[ date ];
93     ec.setBcwpCost(ec.bcwpCost() + cost);
94     m_days[ date ] = ec;
95 }
96 
bcwpCost(const QDate & date) const97 double EffortCostMap::bcwpCost(const QDate &date) const
98 {
99     double v = 0.0;
100     for (EffortCostDayMap::const_iterator it = m_days.constBegin(); it != m_days.constEnd(); ++it) {
101         if (it.key() > date) {
102             break;
103         }
104         v = it.value().bcwpCost();
105     }
106     return v;
107 }
108 
bcwpEffort(const QDate & date) const109 double EffortCostMap::bcwpEffort(const QDate &date) const
110 {
111     double v = 0.0;
112     for (EffortCostDayMap::const_iterator it = m_days.constBegin(); it != m_days.constEnd(); ++it) {
113         if (it.key() > date) {
114             break;
115         }
116         v = it.value().bcwpEffort();
117     }
118     return v;
119 }
120 
121 #ifndef QT_NO_DEBUG_STREAM
debug(QDebug dbg) const122 QDebug EffortCostMap::debug(QDebug dbg) const
123 {
124     dbg.nospace()<<"EffortCostMap[";
125     if (! m_days.isEmpty()) {
126         dbg<<startDate().toString(Qt::ISODate)<<" "<<endDate().toString(Qt::ISODate)
127             <<" total bcws="<<totalEffort().toDouble(Duration::Unit_h)<<", "<<totalCost()<<" bcwp="<<bcwpTotalEffort()<<" "<<bcwpTotalCost();
128     }
129     dbg.nospace()<<']';
130     if (! m_days.isEmpty()) {
131         QMap<QDate, KPlato::EffortCost>::ConstIterator it = days().constBegin();
132         for (; it != days().constEnd(); ++it) {
133             dbg<<endl;
134             dbg<<"     "<<it.key().toString(Qt::ISODate)<<" "<<it.value();
135         }
136         dbg<<endl;
137     }
138     return dbg;
139 }
140 #endif
141 
142 } //namespace KPlato
143 
144 #ifndef QT_NO_DEBUG_STREAM
operator <<(QDebug dbg,const KPlato::EffortCost & ec)145 QDebug operator<<(QDebug dbg, const KPlato::EffortCost &ec)
146 {
147     dbg.nospace()<<"EffortCost[ bcws effort="<<ec.hours()<<" cost="<<ec.cost()<<" : bcwp effort="<<ec.bcwpEffort()<<" cost="<<ec.bcwpCost()<<"]";
148     return dbg;
149 }
operator <<(QDebug dbg,const KPlato::EffortCost * ec)150 QDebug operator<<(QDebug dbg, const KPlato::EffortCost *ec)
151 {
152     return operator<<(dbg, *ec);
153 }
154 
operator <<(QDebug dbg,const KPlato::EffortCostMap & i)155 QDebug operator<<(QDebug dbg, const KPlato::EffortCostMap &i)
156 {
157     return i.debug(dbg);
158 }
159 #endif
160