Add "transfert" feature (don't show tansferts into statistics)

Update package.sh
This commit is contained in:
2010-08-21 09:25:35 +02:00
parent 8977c820fc
commit 3802460429
10 changed files with 172 additions and 11 deletions
+99 -2
View File
@@ -316,6 +316,7 @@ void Database::LoadYear(User* user, int year)
op.category = set.GetAsString(wxT("category"));
op.fix_cost = set.GetBool(wxT("fix_cost"));
op.checked = set.GetBool(wxT("checked"));
op.transfert = set.GetAsString(wxT("transfert"));
op.formula = set.GetAsString(wxT("formula"));
(*user->_operations[op.year])[op.month].push_back(op);
}
@@ -349,9 +350,93 @@ double Database::GetAccountAmount(const wxString& id, int month, int year)
return res;
}
bool Database::GetOperation(const wxString& id, Operation* op)
{
wxSQLite3ResultSet set;
wxString req;
req = wxT("SELECT * FROM operation WHERE id='") + id + wxT("'");
EXECUTE_SQL_QUERY(req, set, false);
if (!set.NextRow()) return false;
op->id = set.GetAsString(wxT("id"));
op->parent = set.GetAsString(wxT("parent"));
op->account = set.GetAsString(wxT("account"));
op->day = set.GetInt(wxT("day"));
op->month = set.GetInt(wxT("month"));
op->year = set.GetInt(wxT("year"));
op->amount = set.GetDouble(wxT("amount"));
op->description = set.GetAsString(wxT("description"));
op->category = set.GetAsString(wxT("category"));
op->fix_cost = set.GetBool(wxT("fix_cost"));
op->checked = set.GetBool(wxT("checked"));
op->transfert = set.GetAsString(wxT("transfert"));
op->formula = set.GetAsString(wxT("formula"));
return true;
}
void Database::LinkOrUnlinkOperation(Operation& op)
{
Operation linked;
wxString req;
wxSQLite3ResultSet set;
if (op.transfert.Length())
{
// No one or not linked
if (!GetOperation(op.transfert, &linked) || op.description != linked.description || op.amount != -linked.amount)
{
req = wxT("UPDATE operation SET transfert='' where id='") + op.id + wxT("'") ;
EXECUTE_SQL_UPDATE(req, );
return;
}
}
// Not Linked
else
{
req = wxT("SELECT id FROM operation WHERE transfert='") + op.id + wxT("'");
EXECUTE_SQL_QUERY(req, set, );
if (set.NextRow())
{
req = wxT("UPDATE operation SET transfert='' where id='") + set.GetAsString(wxT("id")) + wxT("'") ;
EXECUTE_SQL_UPDATE(req, );
}
req = wxT("SELECT id FROM operation WHERE description='") + op.description + wxT("'");
req += wxT(" AND month='") + wxString::Format(wxT("%d"), op.month) + wxT("'");
req += wxT(" AND year='") + wxString::Format(wxT("%d"), op.year) + wxT("'");
req += wxT(" AND amount='") + DoubleToString(-op.amount) + wxT("'");
req += wxT(" AND transfert=''");
EXECUTE_SQL_QUERY(req, set, );
// Don't need to link
if (!set.NextRow()) return ;
// Link
linked.id = set.GetAsString(wxT("id"));
op.transfert = linked.id;
req = wxT("UPDATE operation SET transfert='") + linked.id + wxT("' where id='") + op.id + wxT("'") ;
EXECUTE_SQL_UPDATE(req, );
req = wxT("UPDATE operation SET transfert='") + op.id + wxT("' where id='") + linked.id + wxT("'") ;
EXECUTE_SQL_UPDATE(req, );
}
}
void Database::UpdateOperation(Operation& op)
{
wxString req;
LinkOrUnlinkOperation(op);
req = wxT("UPDATE operation SET ") ;
req += wxT("parent='") + op.parent + wxT("'");
req += wxT(", account='") + op.account + wxT("'");
@@ -365,10 +450,13 @@ void Database::UpdateOperation(Operation& op)
req += wxT(", checked='1'");
else
req += wxT(", checked='0'");
req += wxT(", transfert='") + op.transfert + wxT("'");
req += wxT(", formula='") + op.formula + wxT("'");
req += wxT(" WHERE id='") + op.id + wxT("'");
EXECUTE_SQL_UPDATE(req, );
LinkOrUnlinkOperation(op);
}
wxString Database::AddOperation(User* user, Operation& op)
@@ -376,7 +464,7 @@ wxString Database::AddOperation(User* user, Operation& op)
wxString req, res;
wxSQLite3ResultSet set;
req = wxT("INSERT INTO operation ('user', 'parent', 'account', 'year', 'month', 'day', 'amount', 'description', 'category', 'fix_cost', 'formula') VALUES ('") ;
req = wxT("INSERT INTO operation ('user', 'parent', 'account', 'year', 'month', 'day', 'amount', 'description', 'category', 'fix_cost', 'formula', 'transfert') VALUES ('") ;
req += user->_id + wxT("'");
req += wxT(", '") + op.parent + wxT("'");
req += wxT(", '") + op.account + wxT("'");
@@ -391,6 +479,7 @@ wxString Database::AddOperation(User* user, Operation& op)
else
req += wxT(", '0'") ;
req += wxT(", '") + op.formula + wxT("'");
req += wxT(", '") + op.transfert + wxT("'");
req += wxT(")");
EXECUTE_SQL_UPDATE(req, wxT("0"));
@@ -410,6 +499,7 @@ wxString Database::AddOperation(User* user, Operation& op)
else
req += wxT(" AND fix_cost='0'") ;
req += wxT(" AND formula='") + op.formula + wxT("'");
req += wxT(" AND transfert='") + op.transfert + wxT("'");
req += wxT("ORDER BY id DESC") ;
EXECUTE_SQL_QUERY(req , set, wxT("0"));
@@ -421,6 +511,10 @@ wxString Database::AddOperation(User* user, Operation& op)
set.Finalize();
op.id = res;
LinkOrUnlinkOperation(op);
return res;
}
@@ -430,6 +524,8 @@ void Database::DeleteOperation(Operation& op)
req = wxT("DELETE FROM operation WHERE id='") + op.id + wxT("'");
EXECUTE_SQL_UPDATE(req, );
LinkOrUnlinkOperation(op);
}
void Database::DeleteOperations(User* user, int month, int year)
@@ -939,7 +1035,7 @@ std::vector<Operation>* Database::Search(User* user, wxString* description, wxDa
if (description)
{
req += wxT("description LIKE '%") + *description + wxT("%'");
req += wxT("description='") + *description + wxT("'");
firstCond = true;
}
@@ -1099,6 +1195,7 @@ void Database::GetStats(User* user, const wxString& monthFrom, const wxString& y
req += wxT(" AND (year > '") + yearFrom + wxT("' OR (year == '") + yearFrom + wxT("' AND month >= '") + monthFrom + wxT("'))");
req += wxT(" AND (year < '") + yearTo + wxT("' OR (year == '") + yearTo + wxT("' AND month <= '") + monthTo + wxT("'))");
req += wxT(" AND amount < 0");
req += wxT(" AND transfert=''");
EXECUTE_SQL_QUERY(req, set, );
+2
View File
@@ -77,11 +77,13 @@ class Database
std::map<wxString, double>* categories);
void KillMe(User* user);
bool GetOperation(const wxString& id, Operation* op);
private:
wxSQLite3Database _db;
void CreateDatabase();
wxString HashPassword(const wxString& password);
void LinkOrUnlinkOperation(Operation& op);
};
#endif
+1
View File
@@ -33,6 +33,7 @@ class Operation {
bool fix_cost;
wxString account;
bool checked;
wxString transfert;
wxString formula;
} ;
+31
View File
@@ -117,3 +117,34 @@ wxLanguage User::GetLanguage()
return (wxLanguage)val;
}
void User::LinkOrUnlinkOperation(Operation& op)
{
std::vector<Operation>::iterator it;
// Not Linked
if (!op.transfert.Length())
{
for (it = (*_operations[op.year])[op.month].begin(); it != (*_operations[op.year])[op.month].end(); it++)
{
if (it->id != op.id && it->transfert == op.id)
{
it->transfert = wxT("");
return;
}
}
}
// Linked
else
{
for (it = (*_operations[op.year])[op.month].begin(); it != (*_operations[op.year])[op.month].end(); it++)
{
if (it->id != op.id && it->id == op.transfert)
{
it->transfert = op.id;
return;
}
}
op.transfert = wxT("");
}
}
+1
View File
@@ -51,6 +51,7 @@ class User
int GetAccountsNumber();
int GetOperationsNumber(int month, int year);
wxLanguage GetLanguage();
void LinkOrUnlinkOperation(Operation& op);
};
#endif