class TARIFF {
public:
+ enum CHANGE_POLICY { ALLOW = 0, TO_CHEAP, TO_EXPENSIVE, DENY };
+
enum PERIOD { DAY = 0, MONTH };
enum TRAFF_TYPE { TRAFF_UP = 0, TRAFF_DOWN, TRAFF_UP_DOWN, TRAFF_MAX };
+ static std::string ChangePolicyToString(CHANGE_POLICY changePolicy);
+ static CHANGE_POLICY StringToChangePolicy(const std::string& value);
+
static std::string PeriodToString(PERIOD period);
static PERIOD StringToPeriod(const std::string& value);
virtual double GetFee() const = 0;
virtual double GetFree() const = 0;
virtual PERIOD GetPeriod() const = 0;
+ virtual CHANGE_POLICY GetChangePolicy() const = 0;
virtual const std::string & GetName() const = 0;
virtual void SetName(const std::string & name) = 0;
virtual int64_t GetTraffByType(uint64_t up, uint64_t down) const = 0;
virtual int GetThreshold(int dir) const = 0;
virtual const TARIFF_DATA & GetTariffData() const = 0;
+ virtual std::string TariffChangeIsAllowed(const TARIFF & to) const = 0;
};
+inline
+std::string TARIFF::ChangePolicyToString(TARIFF::CHANGE_POLICY changePolicy)
+{
+switch (changePolicy)
+ {
+ case ALLOW: return "allow";
+ case TO_CHEAP: return "to_cheap";
+ case TO_EXPENSIVE: return "to_expensive";
+ case DENY: return "deny";
+ }
+return "allow"; // Classic behaviour.
+}
+
+inline
+TARIFF::CHANGE_POLICY TARIFF::StringToChangePolicy(const std::string& value)
+{
+if (strcasecmp(value.c_str(), "to_cheap") == 0)
+ return TO_CHEAP;
+if (strcasecmp(value.c_str(), "to_expensive") == 0)
+ return TO_EXPENSIVE;
+if (strcasecmp(value.c_str(), "deny") == 0)
+ return DENY;
+return ALLOW; // Classic behaviour.
+}
+
inline
std::string TARIFF::PeriodToString(TARIFF::PERIOD period)
{
double passiveCost;
std::string name;
TARIFF::PERIOD period;
+ TARIFF::CHANGE_POLICY changePolicy;
TARIFF_CONF()
: fee(0),
traffType(TARIFF::TRAFF_UP_DOWN),
passiveCost(0),
name(),
- period(TARIFF::MONTH)
+ period(TARIFF::MONTH),
+ changePolicy(TARIFF::ALLOW)
{}
TARIFF_CONF(const std::string & n)
traffType(TARIFF::TRAFF_UP_DOWN),
passiveCost(0),
name(n),
- period(TARIFF::MONTH)
+ period(TARIFF::MONTH),
+ changePolicy(TARIFF::ALLOW)
{}
};
//-----------------------------------------------------------------------------
traffType(),
passiveCost(),
name(),
- period()
+ period(),
+ changePolicy()
{}
TARIFF_CONF_RES & operator=(const TARIFF_CONF & tc)
passiveCost = tc.passiveCost;
name = tc.name;
period = tc.period;
+ changePolicy = tc.changePolicy;
return *this;
}
passiveCost.maybeSet(tc.passiveCost);
traffType.maybeSet(tc.traffType);
period.maybeSet(tc.period);
+ changePolicy.maybeSet(tc.changePolicy);
return tc;
}
RESETABLE<double> passiveCost;
RESETABLE<std::string> name;
RESETABLE<TARIFF::PERIOD> period;
+ RESETABLE<TARIFF::CHANGE_POLICY> changePolicy;
};
//-----------------------------------------------------------------------------
struct TARIFF_DATA
dirPrice(DIR_NUM)
{}
+ TARIFF_DATA_RES & operator=(const TARIFF_DATA & td)
+ {
+ tariffConf = td.tariffConf;
+ for (size_t i = 0; i < DIR_NUM; ++i)
+ dirPrice[i] = td.dirPrice[i];
+ return *this;
+ }
+
TARIFF_DATA GetData() const
{
TARIFF_DATA td;
--- /dev/null
+/*
+ * DB migration from v02 to v03 (postgres)
+ */
+BEGIN;
+
+CREATE DOMAIN DM_TARIFF_CHANGE_POLICY AS TEXT NOT NULL
+ CONSTRAINT valid_value CHECK (VALUE IN ('allow', 'to_cheap', 'to_expensive', 'deny'));
+
+ALTER TABLE tb_tariffs ADD change_policy DM_TARIFF_CHANGE_POLICY DEFAULT 'allow';
+
+UPDATE tb_info SET version = 8;
+
+COMMIT;
--- /dev/null
+/*
+ * DB migration from v02 to v03 (firebird)
+ */
+
+CREATE DOMAIN DM_TARIFF_CHANGE_POLICY AS VARCHAR(32) NOT NULL
+ CHECK (VALUE IN ('allow', 'to_cheap', 'to_expensive', 'deny'));
+
+ALTER TABLE tb_tariffs ADD change_policy DM_TARIFF_CHANGE_POLICY DEFAULT 'allow';
+
+UPDATE tb_info SET version = 2;
structVal["passivecost"] = xmlrpc_c::value_double(data.tariffConf.passiveCost);
structVal["traffType"] = xmlrpc_c::value_int(data.tariffConf.traffType);
structVal["period"] = xmlrpc_c::value_string(TARIFF::PeriodToString(data.tariffConf.period));
+structVal["changePolicy"] = xmlrpc_c::value_string(TARIFF::ChangePolicyToString(data.tariffConf.changePolicy));
std::vector<xmlrpc_c::value> prices(DIR_NUM);
data.tariffConf.period = TARIFF::StringToPeriod(xmlrpc_c::value_string(it->second));
}
+if ((it = structVal.find("changePolicy")) != structVal.end())
+ {
+ data.tariffConf.changePolicy = TARIFF::StringToChangePolicy(xmlrpc_c::value_string(it->second));
+ }
+
if ((it = structVal.find("dirprices")) != structVal.end())
{
std::vector<xmlrpc_c::value> prices(
tariff = tariff.substr(0, pos);
}
- if (tariffs->FindByName(tariff))
- if (ptr->GetProperty().tariffName.Get() != tariff)
- if (!ptr->GetProperty().tariffName.Set(tariff,
- admin,
- login,
- &store))
- return true;
+ const TARIFF * newTariff = tariffs->FindByName(tariff);
+ if (newTariff)
+ {
+ const TARIFF * currentTariff = ptr->GetTariff();
+ std::string message = currentTariff->TariffChangeIsAllowed(*newTariff);
+ if (message.empty())
+ {
+ if (ptr->GetProperty().tariffName.Get() != tariff)
+ {
+ if (!ptr->GetProperty().tariffName.Set(tariff,
+ admin,
+ login,
+ &store))
+ return true;
+ }
+ }
+ else
+ {
+ GetStgLogger()("Tariff change is prohibited for user %s. %s", ptr->GetLogin().c_str(), message.c_str());
+ }
+ }
if (nextTariff != "" &&
tariffs->FindByName(nextTariff))
}
else
{
- if (u->GetProperty().tariffName.Set(tariff,
+ const TARIFF * newTariff = tariffs->FindByName(tariff);
+ if (newTariff)
+ {
+ const TARIFF * currentTariff = u->GetTariff();
+ std::string message = currentTariff->TariffChangeIsAllowed(*newTariff);
+ if (message.empty())
+ {
+ if (u->GetProperty().tariffName.Set(tariff,
admin,
login,
store,
comment))
- {
- u->ResetNextTariff();
- u->WriteConf();
- *retvalPtr = xmlrpc_c::value_boolean(true);
- return;
+ {
+ u->ResetNextTariff();
+ u->WriteConf();
+ *retvalPtr = xmlrpc_c::value_boolean(true);
+ return;
+ }
+ }
+ else
+ {
+ GetStgLogger()("Tariff change is prohibited for user %s. %s", u->GetLogin().c_str(), message.c_str());
+ }
}
}
}
"<Free value=\"" + x2str(it->tariffConf.free) + "\"/>" +
"<TraffType value=\"" + TARIFF::TraffTypeToString(it->tariffConf.traffType) + "\"/>" +
"<Period value=\"" + TARIFF::PeriodToString(it->tariffConf.period) + "\"/>" +
+ "<ChangePolicy value=\"" + TARIFF::ChangePolicyToString(it->tariffConf.changePolicy) + "\"/>" +
"</tariff>";
}
{
if (strcasecmp(el, m_tag.c_str()) == 0)
{
- td.tariffConf.name = attr[1];
+ const TARIFF * tariff = m_tariffs.FindByName(attr[1]);
+ if (tariff != NULL)
+ td = tariff->GetTariffData();
+ else
+ return -1;
return 0;
}
}
td.tariffConf.period = TARIFF::StringToPeriod(attr[1]);
return 0;
}
+
+ if (strcasecmp(el, "ChangePolicy") == 0)
+ {
+ td.tariffConf.changePolicy = TARIFF::StringToChangePolicy(attr[1]);
+ return 0;
+ }
}
return -1;
}
if (!m_ucr.tariffName.empty())
{
- if (m_tariffs.FindByName(m_ucr.tariffName.const_data()))
+ const TARIFF * newTariff = m_tariffs.FindByName(m_ucr.tariffName.const_data());
+ if (newTariff)
{
- if (!u->GetProperty().tariffName.Set(m_ucr.tariffName.const_data(), &m_currAdmin, m_login, &m_store))
- return -1;
- u->ResetNextTariff();
+ const TARIFF * tariff = u->GetTariff();
+ std::string message = tariff->TariffChangeIsAllowed(*newTariff);
+ if (message.empty())
+ {
+ if (!u->GetProperty().tariffName.Set(m_ucr.tariffName.const_data(), &m_currAdmin, m_login, &m_store))
+ return -1;
+ u->ResetNextTariff();
+ }
+ else
+ {
+ GetStgLogger()("Tariff change is prohibited for user %s. %s", u->GetLogin().c_str(), message.c_str());
+ }
}
else
{
td->tariffConf.period = TARIFF::MONTH;
else
td->tariffConf.period = TARIFF::StringToPeriod(str);
+
+if (conf.ReadString("ChangePolicy", &str, "allow") < 0)
+ td->tariffConf.changePolicy = TARIFF::ALLOW;
+else
+ td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(str);
return 0;
}
//-----------------------------------------------------------------------------
cf.WriteDouble("Free", td.tariffConf.free);
cf.WriteString("TraffType", TARIFF::TraffTypeToString(td.tariffConf.traffType));
cf.WriteString("Period", TARIFF::PeriodToString(td.tariffConf.period));
+ cf.WriteString("ChangePolicy", TARIFF::ChangePolicyToString(td.tariffConf.changePolicy));
}
return 0;
int32_t id;
st->Get(1, id);
st->Close();
+
+ std::string query = "update tb_tariffs set \
+ fee = ?, \
+ free = ?, \
+ passive_cost = ?, \
+ traff_type = ?";
+
+ if (schemaVersion > 0)
+ query += ", period = ?";
+ if (schemaVersion > 1)
+ query += ", change_policy = ?";
+
+ query += " where pk_tariff = ?";
+
+ st->Prepare(query);
+ st->Set(1, td.tariffConf.fee);
+ st->Set(2, td.tariffConf.free);
+ st->Set(3, td.tariffConf.passiveCost);
+ st->Set(4, td.tariffConf.traffType);
+
if (schemaVersion > 0)
- {
- st->Prepare("update tb_tariffs set \
- fee = ?, \
- free = ?, \
- passive_cost = ?, \
- traff_type = ?, \
- period = ? \
- where pk_tariff = ?");
- st->Set(1, td.tariffConf.fee);
- st->Set(2, td.tariffConf.free);
- st->Set(3, td.tariffConf.passiveCost);
- st->Set(4, td.tariffConf.traffType);
st->Set(5, TARIFF::PeriodToString(td.tariffConf.period));
- st->Set(6, id);
- }
- else
- {
- st->Prepare("update tb_tariffs set \
- fee = ?, \
- free = ?, \
- passive_cost = ?, \
- traff_type = ? \
- where pk_tariff = ?");
- st->Set(1, td.tariffConf.fee);
- st->Set(2, td.tariffConf.free);
- st->Set(3, td.tariffConf.passiveCost);
- st->Set(4, td.tariffConf.traffType);
- st->Set(5, id);
- }
+ if (schemaVersion > 1)
+ st->Set(6, TARIFF::ChangePolicyToString(td.tariffConf.changePolicy));
+
+ st->Set(7, id);
st->Execute();
st->Close();
td->tariffConf.traffType = TARIFF::IntToTraffType(Get<int>(st, 6));
if (schemaVersion > 0)
td->tariffConf.period = TARIFF::StringToPeriod(Get<std::string>(st, 7));
+ if (schemaVersion > 1)
+ td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(Get<std::string>(st, 8));
st->Close();
st->Prepare("select * from tb_tariffs_params where fk_tariff = ?");
st->Set(1, id);
res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"
"Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '',"
- "period VARCHAR(32) NOT NULL DEFAULT 'month')";
+ "period VARCHAR(32) NOT NULL DEFAULT 'month',"
+ "change_policy VARCHAR(32) NOT NULL DEFAULT 'allow')";
if(MysqlQuery(res.c_str(),sock))
{
res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
"SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
- "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down',period='month'";
+ "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down',period='month',"\
+ "change_policy='allow'";
if(MysqlQuery(res.c_str(),sock))
{
mysql_close(sock);
return -1;
}
- schemaVersion = 1;
+ schemaVersion = 2;
}
//users-----------------------------------------------------------------------
schemaVersion = 1;
logger("MYSQL_STORE: Updated DB schema to version %d", schemaVersion);
}
+
+if (schemaVersion < 2)
+ {
+ if (MysqlQuery("ALTER TABLE tariffs ADD change_policy VARCHAR(32) NOT NULL DEFAULT 'allow'", sock))
+ {
+ errorStr = "Couldn't update tariffs table to version 2. With error:\n";
+ errorStr += mysql_error(sock);
+ mysql_close(sock);
+ return -1;
+ }
+ if (MysqlQuery("UPDATE info SET version = 2", sock))
+ {
+ errorStr = "Couldn't update DB schema version to 2. With error:\n";
+ errorStr += mysql_error(sock);
+ mysql_close(sock);
+ return -1;
+ }
+ schemaVersion = 2;
+ logger("MYSQL_STORE: Updated DB schema to version %d", schemaVersion);
+ }
return 0;
}
//-----------------------------------------------------------------------------
td->tariffConf.period = TARIFF::MONTH;
}
+if (schemaVersion > 1)
+{
+ str = row[6+8*DIR_NUM];
+ param = "ChangePolicy";
+
+ if (str.length() == 0)
+ {
+ mysql_free_result(res);
+ errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
+ mysql_close(sock);
+ return -1;
+ }
+
+ td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(str);
+ }
+else
+ {
+ td->tariffConf.changePolicy = TARIFF::ALLOW;
+ }
+
mysql_free_result(res);
mysql_close(sock);
return 0;
if (schemaVersion > 0)
res += ", Period='" + TARIFF::PeriodToString(td.tariffConf.period) + "'";
+if (schemaVersion > 1)
+ res += ", change_policy='" + TARIFF::ChangePolicyToString(td.tariffConf.changePolicy) + "'";
+
strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
res += param;
if (version > 6)
query << ", period = '" << TARIFF::PeriodToString(td.tariffConf.period) << "'";
+ if (version > 7)
+ query << ", change_policy = '" << TARIFF::ChangePolicyToString(td.tariffConf.changePolicy) << "'";
+
query << " WHERE pk_tariff = " << id;
result = PQexec(connection, query.str().c_str());
if (version > 6)
query << ", period";
+if (version > 7)
+ query << ", change_policy";
+
query << " FROM tb_tariffs WHERE name = '" << ename << "'";
result = PQexec(connection, query.str().c_str());
if (version > 6)
td->tariffConf.period = TARIFF::StringToPeriod(PQgetvalue(result, 0, 5));
+if (version > 7)
+ td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(PQgetvalue(result, 0, 6));
+
PQclear(result);
query.str("");
return tariffData.dirPrice[dir].priceDayA;
}
//-----------------------------------------------------------------------------
+std::string TARIFF_IMPL::TariffChangeIsAllowed(const TARIFF & to) const
+{
+switch (GetChangePolicy())
+ {
+ case TARIFF::ALLOW:
+ return "";
+ case TARIFF::TO_CHEAP:
+ if (to.GetFee() < GetFee())
+ return "";
+ else
+ return "New tariff '" + to.GetName() + "' is more expensive than current tariff '" + GetName() + "'. The policy is '" + TARIFF::ChangePolicyToString(GetChangePolicy()) + "'.";
+ case TARIFF::TO_EXPENSIVE:
+ if (to.GetFee() > GetFee())
+ return "";
+ else
+ return "New tariff '" + to.GetName() + "' is more cheap than current tariff '" + GetName() + "'. The policy is '" + TARIFF::ChangePolicyToString(GetChangePolicy()) + "'.";
+ case TARIFF::DENY:
+ return "Current tariff '" + GetName() + "', new tariff '" + to.GetName() + "'. The policy is '" + TARIFF::ChangePolicyToString(GetChangePolicy()) + "'.";
+ }
+}
+//-----------------------------------------------------------------------------
double GetFee() const { return tariffData.tariffConf.fee; }
double GetFree() const { return tariffData.tariffConf.free; }
PERIOD GetPeriod() const { return tariffData.tariffConf.period; }
+ CHANGE_POLICY GetChangePolicy() const { return tariffData.tariffConf.changePolicy; }
void Print() const;
TARIFF_IMPL & operator=(const TARIFF_IMPL & t);
bool operator==(const TARIFF_IMPL & rhs) const { return GetName() == rhs.GetName(); }
bool operator!=(const TARIFF_IMPL & rhs) const { return GetName() != rhs.GetName(); }
+ std::string TariffChangeIsAllowed(const TARIFF & to) const;
private:
TARIFF_DATA tariffData;
{
const TARIFF * nt = tariffs->FindByName(nextTariff);
if (nt == NULL)
+ {
WriteServLog("Cannot change tariff for user %s. Tariff %s not exist.",
login.c_str(), property.tariffName.Get().c_str());
+ }
else
- property.tariffName.Set(nextTariff, sysAdmin, login, store);
+ {
+ std::string message = tariff->TariffChangeIsAllowed(*nt);
+ if (message.empty())
+ {
+ property.tariffName.Set(nextTariff, sysAdmin, login, store);
+ }
+ else
+ {
+ WriteServLog("Tariff change is prohibited for user %s. %s",
+ login.c_str(),
+ message.c_str());
+ }
+ }
ResetNextTariff();
WriteConf();
}
ensure_equals("1101 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 6 > 4 DA (ignore night)
ensure_equals("1110 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 0 < 4 DA (ignore night)
ensure_equals("1111 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 6 > 4 DA (ignore night)
+ }
+
+ template<>
+ template<>
+ void testobject::test<7>()
+ {
+ set_test_name("Check changePolicy - ALLOW");
+
+ TARIFF_DATA td("test");
+ td.tariffConf.changePolicy = TARIFF::ALLOW;
+ td.tariffConf.fee = 100;
+ TARIFF_IMPL tariff(td);
+
+ td.tariffConf.fee = 50;
+ TARIFF_IMPL cheaper(td);
+
+ ensure_equals("Allow cheaper", tariff.TariffChangeIsAllowed(cheaper).empty(), true);
+
+ td.tariffConf.fee = 100;
+ TARIFF_IMPL equals(td);
+
+ ensure_equals("Allow equal", tariff.TariffChangeIsAllowed(equals).empty(), true);
+
+ td.tariffConf.fee = 150;
+ TARIFF_IMPL expensive(td);
+
+ ensure_equals("Allow expensive", tariff.TariffChangeIsAllowed(expensive).empty(), true);
+ }
+
+ template<>
+ template<>
+ void testobject::test<8>()
+ {
+ set_test_name("Check changePolicy - TO_CHEAP");
+
+ TARIFF_DATA td("test");
+ td.tariffConf.changePolicy = TARIFF::TO_CHEAP;
+ td.tariffConf.fee = 100;
+ TARIFF_IMPL tariff(td);
+
+ td.tariffConf.fee = 50;
+ TARIFF_IMPL cheaper(td);
+
+ ensure_equals("Allow cheaper", tariff.TariffChangeIsAllowed(cheaper).empty(), true);
+
+ td.tariffConf.fee = 100;
+ TARIFF_IMPL equals(td);
+
+ ensure_equals("Allow equal", !tariff.TariffChangeIsAllowed(equals).empty(), true);
+
+ td.tariffConf.fee = 150;
+ TARIFF_IMPL expensive(td);
+
+ ensure_equals("Allow expensive", !tariff.TariffChangeIsAllowed(expensive).empty(), true);
+ }
+
+ template<>
+ template<>
+ void testobject::test<9>()
+ {
+ set_test_name("Check changePolicy - TO_EXPENSIVE");
+
+ TARIFF_DATA td("test");
+ td.tariffConf.changePolicy = TARIFF::TO_EXPENSIVE;
+ td.tariffConf.fee = 100;
+ TARIFF_IMPL tariff(td);
+
+ td.tariffConf.fee = 50;
+ TARIFF_IMPL cheaper(td);
+
+ ensure_equals("Allow cheaper", !tariff.TariffChangeIsAllowed(cheaper).empty(), true);
+
+ td.tariffConf.fee = 100;
+ TARIFF_IMPL equals(td);
+
+ ensure_equals("Allow equal", !tariff.TariffChangeIsAllowed(equals).empty(), true);
+
+ td.tariffConf.fee = 150;
+ TARIFF_IMPL expensive(td);
+
+ ensure_equals("Allow expensive", tariff.TariffChangeIsAllowed(expensive).empty(), true);
+ }
+
+ template<>
+ template<>
+ void testobject::test<10>()
+ {
+ set_test_name("Check changePolicy - DENY");
+
+ TARIFF_DATA td("test");
+ td.tariffConf.changePolicy = TARIFF::DENY;
+ td.tariffConf.fee = 100;
+ TARIFF_IMPL tariff(td);
+
+ td.tariffConf.fee = 50;
+ TARIFF_IMPL cheaper(td);
+
+ ensure_equals("Allow cheaper", !tariff.TariffChangeIsAllowed(cheaper).empty(), true);
+
+ td.tariffConf.fee = 100;
+ TARIFF_IMPL equals(td);
+
+ ensure_equals("Allow equal", !tariff.TariffChangeIsAllowed(equals).empty(), true);
+
+ td.tariffConf.fee = 150;
+ TARIFF_IMPL expensive(td);
+
+ ensure_equals("Allow expensive", !tariff.TariffChangeIsAllowed(expensive).empty(), true);
}
}