2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
21 #include "chg_tariff.h"
23 #include "resetable_utils.h"
25 #include "stg/tariff_conf.h"
26 #include "stg/common.h"
37 template <typename A, typename T>
38 void appendSlashedResetable(std::ostream & stream, const std::string & name, const A & array, T A::value_type:: * field)
41 for (typename A::size_type i = 0; i < array.size(); ++i)
43 if ((array[i].*field).empty()) // All values must be set
47 res += x2str((array[i].*field).data());
49 stream << "<" << name << " value=\"" << res << "\"/>";
52 } // namespace anonymous
54 std::string CHG_TARIFF::Serialize(const TARIFF_DATA_RES & data, const std::string & /*encoding*/)
56 std::ostringstream stream;
58 appendResetable(stream, "fee", data.tariffConf.fee);
59 appendResetable(stream, "passiveCost", data.tariffConf.passiveCost);
60 appendResetable(stream, "free", data.tariffConf.free);
62 if (!data.tariffConf.traffType.empty())
63 switch (data.tariffConf.traffType.data())
65 case TRAFF_UP: stream << "<traffType value=\"up\"/>"; break;
66 case TRAFF_DOWN: stream << "<traffType value=\"down\"/>"; break;
67 case TRAFF_UP_DOWN: stream << "<traffType value=\"up+down\"/>"; break;
68 case TRAFF_MAX: stream << "<traffType value=\"max\"/>"; break;
71 if (!data.tariffConf.period.empty())
72 switch (data.tariffConf.period.data())
74 case TARIFF::DAY: stream << "<period value=\"day\"/>"; break;
75 case TARIFF::MONTH: stream << "<period value=\"month\"/>"; break;
78 for (size_t i = 0; i < DIR_NUM; ++i)
79 if (!data.dirPrice[i].hDay.empty() &&
80 !data.dirPrice[i].mDay.empty() &&
81 !data.dirPrice[i].hNight.empty() &&
82 !data.dirPrice[i].mNight.empty())
83 stream << "<time" << i << " value=\"" << data.dirPrice[i].hDay.data() << ":"
84 << data.dirPrice[i].mDay.data() << "-"
85 << data.dirPrice[i].hNight.data() << ":"
86 << data.dirPrice[i].mNight.data() << "\"/>";
88 appendSlashedResetable(stream, "priceDayA", data.dirPrice, &DIRPRICE_DATA_RES::priceDayA);
89 appendSlashedResetable(stream, "priceDayB", data.dirPrice, &DIRPRICE_DATA_RES::priceDayB);
90 appendSlashedResetable(stream, "priceNightA", data.dirPrice, &DIRPRICE_DATA_RES::priceNightA);
91 appendSlashedResetable(stream, "priceNightB", data.dirPrice, &DIRPRICE_DATA_RES::priceNightB);
92 appendSlashedResetable(stream, "singlePrice", data.dirPrice, &DIRPRICE_DATA_RES::singlePrice);
93 appendSlashedResetable(stream, "noDiscount", data.dirPrice, &DIRPRICE_DATA_RES::noDiscount);
94 appendSlashedResetable(stream, "threshold", data.dirPrice, &DIRPRICE_DATA_RES::threshold);