|
|
|
@@ -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, );
|
|
|
|
|
|
|
|
|
|