]> git.stg.codes - stg.git/commitdiff
Merge branch 'new-daily-fee'
authorMaxim Mamontov <faust.madf@gmail.com>
Mon, 6 Jan 2014 15:51:09 +0000 (17:51 +0200)
committerMaxim Mamontov <faust.madf@gmail.com>
Fri, 9 Jan 2015 19:50:56 +0000 (21:50 +0200)
Conflicts:
include/stg/tariff_conf.h
projects/stargazer/plugins/store/firebird/Makefile
projects/stargazer/plugins/store/firebird/firebird_store.cpp
projects/stargazer/plugins/store/firebird/firebird_store.h
projects/stargazer/plugins/store/mysql/Makefile
projects/stargazer/plugins/store/mysql/mysql_store.cpp
projects/stargazer/plugins/store/mysql/mysql_store.h
projects/stargazer/plugins/store/postgresql/Makefile
projects/stargazer/plugins/store/postgresql/postgresql_store.cpp
projects/stargazer/plugins/store/postgresql/postgresql_store.h
projects/stargazer/plugins/store/postgresql/postgresql_store_tariffs.cpp

20 files changed:
doc/xmlrpc/API-tariffs.xml
include/stg/tariff.h
include/stg/tariff_conf.h
projects/stargazer/inst/var/01-alter-02.mysql.sql [new file with mode: 0644]
projects/stargazer/inst/var/01-alter-02.postgresql.sql [new file with mode: 0644]
projects/stargazer/inst/var/01-alter-02.sql [new file with mode: 0644]
projects/stargazer/plugins/configuration/rpcconfig/tariff_helper.cpp
projects/stargazer/plugins/configuration/sgconfig/parser_tariff.cpp
projects/stargazer/plugins/store/files/file_store.cpp
projects/stargazer/plugins/store/firebird/firebird_store.cpp
projects/stargazer/plugins/store/firebird/firebird_store.h
projects/stargazer/plugins/store/firebird/firebird_store_tariffs.cpp
projects/stargazer/plugins/store/mysql/mysql_store.cpp
projects/stargazer/plugins/store/mysql/mysql_store.h
projects/stargazer/plugins/store/postgresql/postgresql_store.cpp
projects/stargazer/plugins/store/postgresql/postgresql_store_tariffs.cpp
projects/stargazer/tariff_impl.h
projects/stargazer/user_impl.cpp
projects/stargazer/user_impl.h
projects/stargazer/users_impl.cpp

index d36b01dda869a561907cdf65b3b2779bf464d071..9b84889aee87560161d650904d0485c9ad5e9e5c 100644 (file)
                                 </para>
                             </listitem>
                         </varlistentry>
+                        <varlistentry>
+                            <term>string <parameter>period</parameter></term>
+                            <listitem>
+                                <para>Периодичность снятия абонплаты:
+                                    <simplelist type="vert">
+                                        <member>month &#151; ежемесячное снятие</member>
+                                        <member>day &#151; ежедневное снятие</member>
+                                    </simplelist>
+                                </para>
+                            </listitem>
+                        </varlistentry>
                         <varlistentry>
                             <term>array of struct <parameter>dirprices</parameter></term>
                             <listitem>
                                 </para>
                             </listitem>
                         </varlistentry>
+                        <varlistentry>
+                            <term>string <parameter>period</parameter></term>
+                            <listitem>
+                                <para>Периодичность снятия абонплаты:
+                                    <simplelist type="vert">
+                                        <member>month &#151; ежемесячное снятие</member>
+                                        <member>day &#151; ежедневное снятие</member>
+                                    </simplelist>
+                                </para>
+                            </listitem>
+                        </varlistentry>
                         <varlistentry>
                             <term>array of struct <parameter>dirprices</parameter></term>
                             <listitem>
                                         </para>
                                     </listitem>
                                 </varlistentry>
+                                <varlistentry>
+                                    <term>string <parameter>period</parameter></term>
+                                    <listitem>
+                                        <para>Периодичность снятия абонплаты:
+                                            <simplelist type="vert">
+                                                <member>month &#151; ежемесячное снятие</member>
+                                                <member>day &#151; ежедневное снятие</member>
+                                            </simplelist>
+                                        </para>
+                                    </listitem>
+                                </varlistentry>
                                 <varlistentry>
                                     <term>array of struct <parameter>dirprices</parameter></term>
                                     <listitem>
index a5b1d2e9253ad3a441e283e3409e19c4dd0ae6a4..d0bf20b6dd268ddf0f7b2fa772cedad1e89ebdb1 100644 (file)
 #ifndef TARIFF_H
 #define TARIFF_H
 
-#include <ctime>
+#include "os_int.h"
 
 #include <string>
+#include <cstring>
+#include <ctime>
 
-#include "os_int.h"
-#include "tariff_conf.h"
+struct TARIFF_DATA;
 
 class TARIFF {
 public:
+    enum PERIOD { DAY = 0, MONTH };
+
+    static std::string PeriodToString(PERIOD period);
+    static PERIOD StringToPeriod(const std::string& value);
+
     virtual ~TARIFF() {}
     virtual double  GetPriceWithTraffType(uint64_t up,
                                           uint64_t down,
@@ -39,6 +45,7 @@ public:
     virtual double  GetPassiveCost() const = 0;
     virtual double  GetFee() const = 0;
     virtual double  GetFree() const = 0;
+    virtual PERIOD  GetPeriod() const = 0;
 
     virtual const   std::string & GetName() const = 0;
     virtual void    SetName(const std::string & name) = 0;
@@ -49,4 +56,23 @@ public:
     virtual const TARIFF_DATA & GetTariffData() const = 0;
 };
 
+inline
+std::string TARIFF::PeriodToString(TARIFF::PERIOD period)
+{
+switch (period)
+    {
+    case DAY: return "day";
+    case MONTH: return "month";
+    }
+return "month"; // Classic behaviour.
+}
+
+inline
+TARIFF::PERIOD TARIFF::StringToPeriod(const std::string& value)
+{
+if (strcasecmp(value.c_str(), "day") == 0)
+    return DAY;
+return MONTH; // Classic behaviour.
+}
+
 #endif
index b554e14ac63f761c8c35f192653114fda156bf8e..6a296230d8413e3b148d3969ffc69992fb115ae2 100644 (file)
 #ifndef TARIFF_CONF_H
 #define TARIFF_CONF_H
 
-#include <string>
-#include <vector>
-
+#include "tariff.h"
 #include "resetable.h"
 #include "const.h"
 
+#include <string>
+#include <vector>
+
 //-----------------------------------------------------------------------------
 enum
 {
@@ -135,18 +136,20 @@ struct DIRPRICE_DATA_RES
 //-----------------------------------------------------------------------------
 struct TARIFF_CONF
 {
-    double      fee;
-    double      free;
-    int         traffType;
-    double      passiveCost;
-    std::string name;
+    double         fee;
+    double         free;
+    int            traffType;
+    double         passiveCost;
+    std::string    name;
+    TARIFF::PERIOD period;
 
     TARIFF_CONF()
         : fee(0),
           free(0),
           traffType(TRAFF_UP_DOWN),
           passiveCost(0),
-          name()
+          name(),
+          period(TARIFF::MONTH)
         {}
 
     TARIFF_CONF(const std::string & n)
@@ -154,7 +157,8 @@ struct TARIFF_CONF
           free(0),
           traffType(TRAFF_UP_DOWN),
           passiveCost(0),
-          name(n)
+          name(n),
+          period(TARIFF::MONTH)
         {}
 };
 //-----------------------------------------------------------------------------
@@ -165,7 +169,8 @@ struct TARIFF_CONF_RES
           free(),
           traffType(),
           passiveCost(),
-          name()
+          name(),
+          period()
         {}
 
     TARIFF_CONF_RES & operator=(const TARIFF_CONF & tc)
@@ -175,6 +180,7 @@ struct TARIFF_CONF_RES
         traffType   = tc.traffType;
         passiveCost = tc.passiveCost;
         name        = tc.name;
+        period      = tc.period;
         return *this;
         }
 
@@ -186,14 +192,16 @@ struct TARIFF_CONF_RES
         tc.name        = name.data();
         tc.passiveCost = passiveCost.data();
         tc.traffType   = traffType.data();
+        tc.period      = period.data();
         return tc;
         }
 
-    RESETABLE<double>      fee;
-    RESETABLE<double>      free;
-    RESETABLE<int>         traffType;
-    RESETABLE<double>      passiveCost;
-    RESETABLE<std::string> name;
+    RESETABLE<double>         fee;
+    RESETABLE<double>         free;
+    RESETABLE<int>            traffType;
+    RESETABLE<double>         passiveCost;
+    RESETABLE<std::string>    name;
+    RESETABLE<TARIFF::PERIOD> period;
 };
 //-----------------------------------------------------------------------------
 struct TARIFF_DATA
diff --git a/projects/stargazer/inst/var/01-alter-02.mysql.sql b/projects/stargazer/inst/var/01-alter-02.mysql.sql
new file mode 100644 (file)
index 0000000..1f1b044
--- /dev/null
@@ -0,0 +1,12 @@
+/*
+ *  DB migration from v01 to v02 (mysql)
+ */
+
+ALTER TABLE tariffs ADD period VARCHAR(32) NOT NULL DEFAULT 'month';
+
+CREATE TABLE info
+(
+    version INTEGER NOT NULL
+);
+
+INSERT INTO info VALUES (1);
diff --git a/projects/stargazer/inst/var/01-alter-02.postgresql.sql b/projects/stargazer/inst/var/01-alter-02.postgresql.sql
new file mode 100644 (file)
index 0000000..ef6a98c
--- /dev/null
@@ -0,0 +1,13 @@
+/*
+ *  DB migration from v01 to v02 (postgres)
+ */
+BEGIN;
+
+CREATE DOMAIN DM_TARIFF_PERIOD AS TEXT NOT NULL
+    CONSTRAINT valid_value CHECK (VALUE = 'month' OR VALUE = 'day');
+
+ALTER TABLE tb_tariffs ADD period DM_TARIFF_PERIOD DEFAULT 'month';
+
+UPDATE tb_info SET version = 7;
+
+COMMIT;
diff --git a/projects/stargazer/inst/var/01-alter-02.sql b/projects/stargazer/inst/var/01-alter-02.sql
new file mode 100644 (file)
index 0000000..628a016
--- /dev/null
@@ -0,0 +1,15 @@
+/*
+ *  DB migration from v01 to v02 (firebird)
+ */
+
+CREATE DOMAIN DM_TARIFF_PERIOD AS VARCHAR(32) NOT NULL
+    CHECK (VALUE = 'month' OR VALUE = 'day');
+
+ALTER TABLE tb_tariffs ADD period DM_TARIFF_PERIOD DEFAULT 'month';
+
+CREATE TABLE tb_info
+(
+    version INTEGER NOT NULL
+);
+
+INSERT INTO tb_info VALUES (1);
index fad12886f651d870866a459225efbf5e591416b9..85abf8205a5e59822db40850c3b37d4de905be45 100644 (file)
@@ -12,6 +12,7 @@ structVal["fee"] = xmlrpc_c::value_double(data.tariffConf.fee);
 structVal["freemb"] = xmlrpc_c::value_double(data.tariffConf.free);
 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));
 
 std::vector<xmlrpc_c::value> prices(DIR_NUM);
 
@@ -65,6 +66,11 @@ if ((it = structVal.find("traffType")) != structVal.end())
     data.tariffConf.traffType = xmlrpc_c::value_int(it->second);
     }
 
+if ((it = structVal.find("period")) != structVal.end())
+    {
+    data.tariffConf.period = TARIFF::StringToPeriod(xmlrpc_c::value_string(it->second));
+    }
+
 if ((it = structVal.find("dirprices")) != structVal.end())
     {
     std::vector<xmlrpc_c::value> prices(
index 7be1af9645a0107ff39a0ec839e45780f1817b08..0e001b224a559fdfee6f94411275387e7ec62926 100644 (file)
@@ -145,6 +145,8 @@ for (; it != dataList.end(); ++it)
             break;
         }
 
+    answerList->push_back("<Period value=\"" + TARIFF::PeriodToString(it->tariffConf.period) + "\"/>");
+
     answerList->push_back("</tariff>");
     }
 answerList->push_back("</Tariffs>");
@@ -451,6 +453,12 @@ else
             }
         return 0;
         }
+
+    if (strcasecmp(el, "Period") == 0)
+        {
+        td.tariffConf.period = TARIFF::StringToPeriod(attr[1]);
+        return 0;
+        }
     }
 return -1;
 }
index 0f55b5300adcc4357eeb6ce95748c130833c7255..0466b82490c97bfd576a2e477a7d0514257ab388 100644 (file)
@@ -1483,6 +1483,11 @@ else
                 printfd(__FILE__, "FILES_STORE::RestoreTariff - invalid trafftype for tariff '%s'\n", tariffName.c_str());
                 return -1;
                 }
+
+if (conf.ReadString("Period", &str, "month") < 0)
+    td->tariffConf.period = TARIFF::MONTH;
+else
+    td->tariffConf.period = TARIFF::StringToPeriod(str);
 return 0;
 }
 //-----------------------------------------------------------------------------
@@ -1558,6 +1563,8 @@ std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
             cf.WriteString("TraffType", "max");
             break;
         }
+
+    cf.WriteString("Period", TARIFF::PeriodToString(td.tariffConf.period));
     }
 
 return 0;
@@ -1851,7 +1858,7 @@ if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
     {
     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
     errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
-    printfd(__FILE__, "FILES_STORE::SaveTariff - rename failed. Message: '%s'\n", strerror(errno));
+    printfd(__FILE__, "FILES_STORE::EditMessage - rename failed. Message: '%s'\n", strerror(errno));
     return -1;
     }
 
index 7e57483e24c349a380722fbbe54236797577cfa9..8e3c9d90f0432bd70c0c716c04a1e37a14677682 100644 (file)
@@ -123,6 +123,7 @@ try
     {
     db = IBPP::DatabaseFactory(db_server, db_database, db_user, db_password, "", "KOI8U", "");
     db->Connect();
+    return CheckVersion();
     }
 catch (IBPP::Exception & ex)
     {
@@ -134,3 +135,39 @@ catch (IBPP::Exception & ex)
 return 0;
 }
 //-----------------------------------------------------------------------------
+int FIREBIRD_STORE::CheckVersion()
+{
+IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
+IBPP::Statement st = IBPP::StatementFactory(db, tr);
+
+string name;
+
+try
+    {
+    tr->Start();
+    st->Execute("SELECT RDB$RELATION_NAME FROM RDB$RELATIONS WHERE RDB$SYSTEM_FLAG=0 AND RDB$RELATION_NAME = 'TB_INFO'");
+    if (!st->Fetch())
+        {
+        schemaVersion = 0;
+        }
+    else
+        {
+        st->Execute("SELECT version FROM tb_info");
+        while (st->Fetch())
+            st->Get(1, schemaVersion);
+        }
+    tr->Commit();
+    WriteServLog("FIREBIRD_STORE: Current DB schema version: %d", schemaVersion);
+    }
+
+catch (IBPP::Exception & ex)
+    {
+    tr->Rollback();
+    strError = "IBPP exception";
+    printfd(__FILE__, ex.what());
+    return -1;
+    }
+
+return 0;
+}
+//-----------------------------------------------------------------------------
index 638067f2011516f1228af789cb3769f0757d4278..688f0e88651da56258aad827b29d4ff92a21c6d7 100644 (file)
@@ -125,9 +125,11 @@ private:
     mutable pthread_mutex_t mutex;
     IBPP::TIL til;
     IBPP::TLR tlr;
+    int schemaVersion;
     PLUGIN_LOGGER logger;
 
     int SaveStat(const USER_STAT & stat, const std::string & login, int year = 0, int month = 0) const;
+    int CheckVersion();
 };
 
 time_t ts2time_t(const IBPP::Timestamp & ts);
index 1d74cf07e29d3f5978f0db3e3c2d415bfad66889..325e7d7f10b48c79978a04f52655f65a4f9d653c 100644 (file)
@@ -146,17 +146,36 @@ try
     }
     st->Get(1, id);
     st->Close();
-    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 > 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);
+        }
     st->Execute();
     st->Close();
 
@@ -200,7 +219,7 @@ try
             threshold = ?, \
             time_day_begins = ?, \
             time_day_ends = ? \
-             where fk_tariff = ? and dir_num = ?");
+            where fk_tariff = ? and dir_num = ?");
     st->Set(1, pda);
     st->Set(2, pdb);
     st->Set(3, pna);
@@ -246,7 +265,7 @@ td->tariffConf.name = tariffName;
 try
     {
     tr->Start();
-    st->Prepare("select * from tb_tariffs where name = ?");
+    st->Prepare("select * from tb_tariffs where name = ?"); // TODO: explicit field order!
     st->Set(1, tariffName);
     st->Execute();
     if (!st->Fetch())
@@ -261,6 +280,12 @@ try
     st->Get(4, td->tariffConf.free);
     st->Get(5, td->tariffConf.passiveCost);
     st->Get(6, td->tariffConf.traffType);
+    if (schemaVersion > 0)
+        {
+        std::string period;
+        st->Get(7, period);
+        td->tariffConf.period = TARIFF::StringToPeriod(period);
+        }
     st->Close();
     st->Prepare("select * from tb_tariffs_params where fk_tariff = ?");
     st->Set(1, id);
index b4ac87155a762d5510d4700f624decf959956fd2..11c2def3b5c1d2ca0dc3b82fb036fa6afcc00e71 100644 (file)
@@ -12,6 +12,7 @@
 #include "stg/user_stat.h"
 #include "stg/blowfish.h"
 #include "stg/plugin_creator.h"
+#include "stg/logger.h"
 #include "mysql_store.h"
 
 #define adm_enc_passwd "cjeifY8m3"
@@ -235,12 +236,18 @@ else
                  }
                  ret = CheckAllTables(sock);
             }
-         }
-         else
-             ret = CheckAllTables(sock);
-         mysql_close(sock);
+        }
+        else
+        {
+            ret = CheckAllTables(sock);
+        }
+        if (!ret)
+        {
+            WriteServLog("MYSQL_STORE: Current DB schema version: %d", schemaVersion);
+            MakeUpdates(sock);
+        }
+        mysql_close(sock);
     }
-    
 }
 return ret;
 }
@@ -267,6 +274,43 @@ return num_rows == 1;
 //-----------------------------------------------------------------------------
 int MYSQL_STORE::CheckAllTables(MYSQL * sock)
 {
+//info-------------------------------------------------------------------------
+if(!IsTablePresent("info",sock))
+{
+    sprintf(qbuf,"CREATE TABLE info (version INTEGER NOT NULL)");
+
+    if(MysqlQuery(qbuf,sock))
+        {
+        errorStr = "Couldn't create info table With error:\n";
+        errorStr += mysql_error(sock);
+        mysql_close(sock);
+        return -1;
+        }
+
+    sprintf(qbuf,"INSERT INTO info SET version=0");
+
+    if(MysqlQuery(qbuf,sock))
+        {
+        errorStr = "Couldn't write default version. With error:\n";
+        errorStr += mysql_error(sock);
+        mysql_close(sock);
+        return -1;
+        }
+    schemaVersion = 0;
+}
+else
+{
+    std::vector<std::string> info;
+    if (GetAllParams(&info, "info", "version"))
+        schemaVersion = 0;
+    else
+    {
+        if (info.empty())
+            schemaVersion = 0;
+        else
+            GetInt(info.front(), &schemaVersion, 0);
+    }
+}
 //admins-----------------------------------------------------------------------
 if(!IsTablePresent("admins",sock))
 {
@@ -330,8 +374,9 @@ if(!IsTablePresent("tariffs",sock))
         res += param;
         }
     
-    res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"\
-        "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '')";
+    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')";
     
     if(MysqlQuery(res.c_str(),sock))
     {
@@ -385,7 +430,7 @@ if(!IsTablePresent("tariffs",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'";
+        "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down',period='month'";
     
     if(MysqlQuery(res.c_str(),sock))
     {
@@ -394,6 +439,17 @@ if(!IsTablePresent("tariffs",sock))
         mysql_close(sock);
         return -1;
     }
+
+    sprintf(qbuf,"UPDATE info SET version=1");
+
+    if(MysqlQuery(qbuf,sock))
+    {
+        errorStr = "Couldn't write default version. With error:\n";
+        errorStr += mysql_error(sock);
+        mysql_close(sock);
+        return -1;
+    }
+    schemaVersion = 1;
 }
 
 //users-----------------------------------------------------------------------
@@ -525,6 +581,29 @@ if(!IsTablePresent("stat",sock))
 return 0;
 }
 //-----------------------------------------------------------------------------
+int MYSQL_STORE::MakeUpdates(MYSQL * sock)
+{
+if (schemaVersion  < 1)
+    {
+    if (MysqlQuery("ALTER TABLE tariffs ADD period VARCHAR(32) NOT NULL DEFAULT 'month'", sock))
+        {
+        errorStr = "Couldn't update tariffs table to version 1. With error:\n";
+        errorStr += mysql_error(sock);
+        mysql_close(sock);
+        return -1;
+        }
+    if (MysqlQuery("UPDATE info SET version = 1", sock))
+        {
+        errorStr = "Couldn't update DB schema version to 1. With error:\n";
+        errorStr += mysql_error(sock);
+        mysql_close(sock);
+        return -1;
+        }
+    schemaVersion = 1;
+    WriteServLog("MYSQL_STORE: Updated DB schema to version %d", schemaVersion);
+    }
+return 0;
+}
 //-----------------------------------------------------------------------------
 
 int MYSQL_STORE::GetAllParams(std::vector<std::string> * ParamList, 
@@ -1561,6 +1640,26 @@ else
                 return -1;
                 }
 
+if (schemaVersion > 0)
+{
+    str = row[5+8*DIR_NUM];
+    param = "Period";
+
+    if (str.length() == 0)
+        {
+        mysql_free_result(res);
+        errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
+        mysql_close(sock);
+        return -1;
+        }
+
+    td->tariffConf.period = TARIFF::StringToPeriod(str);
+    }
+else
+    {
+    td->tariffConf.period = TARIFF::MONTH;
+    }
+
 mysql_free_result(res);
 mysql_close(sock);
 return 0;
@@ -1638,12 +1737,16 @@ switch (td.tariffConf.traffType)
         res += " TraffType='max'";
         break;
     }
+
+if (schemaVersion > 0)
+    res += ", Period='" + TARIFF::PeriodToString(td.tariffConf.period) + "'";
+
 strprintf(&param, " WHERE name='%s' LIMIT 1", tariffName.c_str());
 res += param;
 
 if(MysqlSetQuery(res.c_str()))
 {
-    errorStr = "Couldn't save admin:\n";
+    errorStr = "Couldn't save tariff:\n";
     //errorStr += mysql_error(sock);
     return -1;
 }
index af747c14518662884bbbf27359b22e7b8bff6758..4a492b3833694ece04130acf72fc81be96e78034 100644 (file)
@@ -132,6 +132,7 @@ private:
     virtual int WriteLogString(const std::string & str, const std::string & login) const;
     int GetAllParams(std::vector<std::string> * ParamList, const std::string & table, const std::string & name) const;
     int CheckAllTables(MYSQL * sock);
+    int MakeUpdates(MYSQL * sock);
     bool IsTablePresent(const std::string & str,MYSQL * sock);
     mutable std::string          errorStr;
     int                        MysqlQuery(const char* sQuery,MYSQL * sock) const;
@@ -141,6 +142,7 @@ private:
     std::string                  version;
     MYSQL_STORE_SETTINGS    storeSettings;
     MODULE_SETTINGS         settings;
+    int                     schemaVersion;
 
     PLUGIN_LOGGER           logger;
 };
index dd2fbbab7a168ed159feff737cf04e63be0f4898..5b55f318a102117e550b22204531ffd6c9f53bf1 100644 (file)
@@ -237,6 +237,8 @@ if (CommitTransaction())
     return -1;
     }
 
+WriteServLog("POSTGRESQL_STORE: Current DB schema version: %d", version);
+
 return 0;
 }
 //-----------------------------------------------------------------------------
index fb8caf9dee0b97b9d9a8a76724bd3f4595ae0301..c9cb7ccfe79f19968eb646c81b2f57652f16985d 100644 (file)
@@ -122,9 +122,9 @@ if (EscapeString(ename))
     {
     printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to escape name'\n");
     if (RollbackTransaction())
-       {
-       printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
-       }
+        {
+        printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
+        }
     return -1;
     }
 
@@ -185,9 +185,9 @@ if (EscapeString(ename))
     {
     printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to escape name'\n");
     if (RollbackTransaction())
-       {
-       printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
-       }
+        {
+        printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
+        }
     return -1;
     }
 
@@ -249,9 +249,9 @@ if (EscapeString(ename))
     {
     printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to escape name'\n");
     if (RollbackTransaction())
-       {
-       printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
-       }
+        {
+        printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
+        }
     return -1;
     }
 
@@ -286,9 +286,9 @@ if (tuples != 1)
     printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
     PQclear(result);
     if (RollbackTransaction())
-       {
-       printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
-       }
+        {
+        printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
+        }
     return -1;
     }
 
@@ -307,8 +307,12 @@ if (tuples != 1)
                   fee = " << td.tariffConf.fee << ", \
                   free = " << td.tariffConf.free << ", \
                   passive_cost = " << td.tariffConf.passiveCost << ", \
-                  traff_type = " << td.tariffConf.traffType << " \
-              WHERE pk_tariff = " << id;
+                  traff_type = " << td.tariffConf.traffType;
+
+    if (version > 6)
+        query << ", period = '" << TARIFF::PeriodToString(td.tariffConf.period) << "'";
+
+    query << " WHERE pk_tariff = " << id;
 
     result = PQexec(connection, query.str().c_str());
     }
@@ -426,9 +430,9 @@ if (EscapeString(ename))
     {
     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to escape name'\n");
     if (RollbackTransaction())
-       {
-       printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
-       }
+        {
+        printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
+        }
     return -1;
     }
 
@@ -437,10 +441,14 @@ td->tariffConf.name = tariffName;
 std::ostringstream query;
 query << "SELECT pk_tariff, \
                  fee, \
-                free, \
-                passive_cost, \
-                traff_type \
-         FROM tb_tariffs WHERE name = '" << ename << "'";
+                 free, \
+                 passive_cost, \
+                 traff_type";
+
+if (version > 6)
+    query << ", period";
+
+query << " FROM tb_tariffs WHERE name = '" << ename << "'";
 
 result = PQexec(connection, query.str().c_str());
 
@@ -464,9 +472,9 @@ if (tuples != 1)
     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
     PQclear(result);
     if (RollbackTransaction())
-       {
-       printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
-       }
+        {
+        printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
+        }
     return -1;
     }
 
@@ -487,21 +495,24 @@ int id;
     tuple >> td->tariffConf.traffType;
     }
 
+if (version > 6)
+    td->tariffConf.period = TARIFF::StringToPeriod(PQgetvalue(result, 0, 5));
+
 PQclear(result);
 
 query.str("");
 query << "SELECT dir_num, \
                  price_day_a, \
                  price_day_b, \
-                price_night_a, \
-                price_night_b, \
-                threshold, \
-                EXTRACT(hour FROM time_day_begins), \
-                EXTRACT(minute FROM time_day_begins), \
-                EXTRACT(hour FROM time_day_ends), \
-                EXTRACT(minute FROM time_day_ends) \
-         FROM tb_tariffs_params \
-         WHERE fk_tariff = " << id;
+                 price_night_a, \
+                 price_night_b, \
+                 threshold, \
+                 EXTRACT(hour FROM time_day_begins), \
+                 EXTRACT(minute FROM time_day_begins), \
+                 EXTRACT(hour FROM time_day_ends), \
+                 EXTRACT(minute FROM time_day_ends) \
+          FROM tb_tariffs_params \
+          WHERE fk_tariff = " << id;
 
 result = PQexec(connection, query.str().c_str());
 
@@ -511,9 +522,9 @@ if (PQresultStatus(result) != PGRES_TUPLES_OK)
     PQclear(result);
     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
     if (RollbackTransaction())
-       {
-       printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
-       }
+        {
+        printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
+        }
     return -1;
     }
 
index 122dabd044de29178d083c0849c4b4ba857a9fef..f2f84d2630889f73432e9a191459c8174c37adf3 100644 (file)
@@ -71,6 +71,7 @@ public:
     double  GetPassiveCost() const { return tariffData.tariffConf.passiveCost; }
     double  GetFee() const { return tariffData.tariffConf.fee; }
     double  GetFree() const { return tariffData.tariffConf.free; }
+    PERIOD  GetPeriod() const { return tariffData.tariffConf.period; }
 
     void    Print() const;
 
index 7b0348bc57e75928936640cbed90d90bd9b28481..3b2489ad319bc1d660d089f4f5a0b2bf4264e93f 100644 (file)
@@ -1253,6 +1253,9 @@ STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 if (passive.ConstData() || tariff == NULL)
     return;
 
+if (tariff->GetPeriod() != TARIFF::MONTH)
+    return;
+
 double fee = tariff->GetFee() / DaysInCurrentMonth();
 
 if (std::fabs(fee) < 1.0e-3)
@@ -1287,6 +1290,9 @@ STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 if (tariff == NULL)
     return;
 
+if (tariff->GetPeriod() != TARIFF::MONTH)
+    return;
+
 double passiveTimePart = 1.0;
 if (!settings->GetFullFee())
     {
@@ -1348,6 +1354,39 @@ switch (settings->GetFeeChargeType())
     }
 }
 //-----------------------------------------------------------------------------
+void USER_IMPL::ProcessDailyFee()
+{
+STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+
+if (passive.ConstData() || tariff == NULL)
+    return;
+
+if (tariff->GetPeriod() != TARIFF::DAY)
+    return;
+
+double fee = tariff->GetFee();
+
+if (fee == 0.0)
+    return;
+
+double c = cash;
+switch (settings->GetFeeChargeType())
+    {
+    case 0:
+        property.cash.Set(c - fee, sysAdmin, login, store, "Subscriber fee charge");
+        break;
+    case 1:
+        if (c + credit >= 0)
+            property.cash.Set(c - fee, sysAdmin, login, store, "Subscriber fee charge");
+        break;
+    case 2:
+        if (c + credit >= fee)
+            property.cash.Set(c - fee, sysAdmin, login, store, "Subscriber fee charge");
+        break;
+    }
+ResetPassiveTime();
+}
+//-----------------------------------------------------------------------------
 void USER_IMPL::SetPrepaidTraff()
 {
 if (tariff != NULL)
index 68945d2c65547efc9ef230ce3d9ef9810aaf4bcc..90ae01b96669043dfd2a1facf3afa9e0f88668d7 100644 (file)
@@ -215,6 +215,7 @@ public:
     void            ProcessDayFee();
     void            ProcessDayFeeSpread();
     void            ProcessNewMonth();
+    void            ProcessDailyFee();
 
     bool            IsInetable();
     std::string     GetEnabledDirs();
index 4c4d4ce03e4c0dcb9f9c360ea0676f492f49a357..f4b4dea0b800420259afb1caef46753936398332 100644 (file)
@@ -513,6 +513,8 @@ else
         }
     }
 
+std::for_each(users.begin(), users.end(), std::mem_fun_ref(&USER_IMPL::ProcessDailyFee));
+
 if (settings->GetDayFeeIsLastDay())
     {
     printfd(__FILE__, "DayResetTraff - 2 -\n");