From: <ste...@us...> - 2011-05-29 13:26:01
|
Revision: 1301 http://moneymanagerex.svn.sourceforge.net/moneymanagerex/?rev=1301&view=rev Author: stef145g Date: 2011-05-29 13:25:53 +0000 (Sun, 29 May 2011) Log Message: ----------- Fixed import CSV and cosmetic changes Modified Paths: -------------- trunk/mmex/src/util.cpp Modified: trunk/mmex/src/util.cpp =================================================================== --- trunk/mmex/src/util.cpp 2011-05-28 20:55:10 UTC (rev 1300) +++ trunk/mmex/src/util.cpp 2011-05-29 13:25:53 UTC (rev 1301) @@ -47,77 +47,78 @@ wxString mmCleanString( const wxString& orig ) { - wxString toReturn = orig; - toReturn.Replace( wxT( "'" ), wxT( "''" ) ); - return toReturn; + wxString toReturn = orig; + toReturn.Replace( wxT( "'" ), wxT( "''" ) ); + toReturn.Replace( wxT( "\"" ), wxT( "" ) ); // remove quotes from string + return toReturn; } //---------------------------------------------------------------------------- int CaseInsensitiveCmp( const wxString &s1, const wxString &s2 ) { - return s1.CmpNoCase( s2 ); + return s1.CmpNoCase( s2 ); } //---------------------------------------------------------------------------- wxString selectLanguageDlg( wxWindow *parent, const wxString &langPath, bool verbose ) { - wxString lang; + wxString lang; - wxArrayString lang_files; - size_t cnt = wxDir::GetAllFiles( langPath, &lang_files, wxT( "*.mo" ) ); + wxArrayString lang_files; + size_t cnt = wxDir::GetAllFiles( langPath, &lang_files, wxT( "*.mo" ) ); - if ( !cnt ) { + if ( !cnt ) { - if ( verbose ) { - wxString s = wxT( "Can't find language files (.mo) at \"" ); - s << langPath << wxT( '\"' ); + if ( verbose ) { + wxString s = wxT( "Can't find language files (.mo) at \"" ); + s << langPath << wxT( '\"' ); - wxMessageDialog dlg( parent, s, wxT( "Error" ), wxICON_ERROR ); - dlg.ShowModal(); - } - - return lang; + wxMessageDialog dlg( parent, s, wxT( "Error" ), wxICON_ERROR ); + dlg.ShowModal(); } - for ( size_t i = 0; i < cnt; ++i ) { - wxFileName fname( lang_files[i] ); - lang_files[i] = fname.GetName(); - } + return lang; + } - lang_files.Sort( CaseInsensitiveCmp ); - lang = wxGetSingleChoice( wxT( "Please choose language" ), wxT( "Languages" ), lang_files, parent ); + for ( size_t i = 0; i < cnt; ++i ) { + wxFileName fname( lang_files[i] ); + lang_files[i] = fname.GetName(); + } - return lang; + lang_files.Sort( CaseInsensitiveCmp ); + lang = wxGetSingleChoice( wxT( "Please choose language" ), wxT( "Languages" ), lang_files, parent ); + + return lang; } //---------------------------------------------------------------------------- double mmRound(double x) { - x += (x < 0 ? -0.5 : 0.5); + x += (x < 0 ? -0.5 : 0.5); - double n = 0; - modf(x, &n); + double n = 0; + modf(x, &n); - return n; + return n; } //---------------------------------------------------------------------------- double mmMoneyInt(double x, int scale) { - double n = 0; - modf(x/scale, &n); - return n; + double n = 0; + modf(x/scale, &n); + return n; } //---------------------------------------------------------------------------- int mmCents(double x, int scale) { - x += (x < 0 ? -0.5 : 0.5); + x += (x < 0 ? -0.5 : 0.5); - double dummy = 0; - double fract = modf(x/scale, &dummy); + double dummy = 0; + double fract = modf(x/scale, &dummy); - return static_cast<int>(fract*scale); + return static_cast<int>(fract*scale); } //---------------------------------------------------------------------------- @@ -137,84 +138,84 @@ */ wxString format_groups(const mmex::CurrencyFormatter &fmt, double x, size_t grp_sz) { - wxString val; - val.Printf(wxT("%.0f"), x); + wxString val; + val.Printf(wxT("%.0f"), x); - wxChar grp_sep = fmt.getGroupSeparator(); + wxChar grp_sep = fmt.getGroupSeparator(); - if (wxIsprint(grp_sep) && val.length() > grp_sz) { // there will be groups + if (wxIsprint(grp_sep) && val.length() > grp_sz) { // there will be groups - wchar_t sep = get_group_separator(grp_sep); + wchar_t sep = get_group_separator(grp_sep); - std::wstring s; - s.reserve(val.length() + val.length()/grp_sz); - s.assign(val.wc_str(*wxConvCurrent), val.length()); + std::wstring s; + s.reserve(val.length() + val.length()/grp_sz); + s.assign(val.wc_str(*wxConvCurrent), val.length()); - for (size_t i = 0, j = i + grp_sz*(i+1); j < s.length(); ++i, j = i + grp_sz*(i+1)) { - std::wstring::iterator it = s.begin(); - std::advance(it, s.length() - j); - s.insert(it, sep); - } + for (size_t i = 0, j = i + grp_sz*(i+1); j < s.length(); ++i, j = i + grp_sz*(i+1)) { + std::wstring::iterator it = s.begin(); + std::advance(it, s.length() - j); + s.insert(it, sep); + } - val = wxString(s.data(), *wxConvCurrent, s.length()); - } + val = wxString(s.data(), *wxConvCurrent, s.length()); + } - return val; + return val; } //---------------------------------------------------------------------------- wxString format_cents(const mmex::CurrencyFormatter &f, int cents) { - const wxChar* fmt[] = { wxT("%02d"), wxT("%01d"), wxT("%03d"), wxT("%04d") }; + const wxChar* fmt[] = { wxT("%02d"), wxT("%01d"), wxT("%03d"), wxT("%04d") }; - wxASSERT(g_def_scale == 100); - size_t i = 0; // "%02d" for g_def_scale + wxASSERT(g_def_scale == 100); + size_t i = 0; // "%02d" for g_def_scale - int scale = f.getScale(); + int scale = f.getScale(); - if (scale >= 10000) { - i = 3; - } else if (scale >= 1000) { - i = 2; - } else if (scale < 100) { + if (scale >= 10000) { + i = 3; + } else if (scale >= 1000) { + i = 2; + } else if (scale < 100) { i = 1; } wxASSERT(i < sizeof(fmt)/sizeof(*fmt)); - wxString s = f.getDecimalPoint(); - s += wxString::Format(fmt[i], cents); + wxString s = f.getDecimalPoint(); + s += wxString::Format(fmt[i], cents); - return s; + return s; } //---------------------------------------------------------------------------- void DoubleToCurrency(const mmex::CurrencyFormatter &fmt, double val, wxString& rdata, bool for_edit) { - wxString s; - s.Alloc(32); + wxString s; + s.Alloc(32); - if (!for_edit) { - wxString pfx = fmt.getPrefix(); - if (!pfx.empty()) { - s += pfx; - s += wxT(' '); - } - } + if (!for_edit) { + wxString pfx = fmt.getPrefix(); + if (!pfx.empty()) { + s += pfx; + s += wxT(' '); + } + } - if (val < 0) - s += wxT('-'); // "minus" sign + if (val < 0) + s += wxT('-'); // "minus" sign - int scale = fmt.getScale(); - double abs_val = fabs(mmRound(val*scale)); + int scale = fmt.getScale(); + double abs_val = fabs(mmRound(val*scale)); - s += format_groups(fmt, mmMoneyInt(abs_val, scale), 3); - s += format_cents(fmt, mmCents(abs_val, scale)); + s += format_groups(fmt, mmMoneyInt(abs_val, scale), 3); + s += format_cents(fmt, mmCents(abs_val, scale)); - if (!for_edit) - s += fmt.getSuffix(); + if (!for_edit) + s += fmt.getSuffix(); - rdata = s; + rdata = s; } //---------------------------------------------------------------------------- @@ -322,1137 +323,1132 @@ // --------------------------------------------------------------------------- void mmPlayTransactionSound( wxSQLite3Database* db_ ) { - wxString useSound = mmDBWrapper::getINISettingValue( db_, wxT( "USETRANSSOUND" ), wxT( "TRUE" ) ); + wxString useSound = mmDBWrapper::getINISettingValue( db_, wxT( "USETRANSSOUND" ), wxT( "TRUE" ) ); - if ( useSound == wxT( "TRUE" ) ) { - wxSound registerSound( mmex::getPathResource( mmex::TRANS_SOUND ) ); + if ( useSound == wxT( "TRUE" ) ) { + wxSound registerSound( mmex::getPathResource( mmex::TRANS_SOUND ) ); - if ( registerSound.IsOk() ) - registerSound.Play( wxSOUND_ASYNC ); - } + if ( registerSound.IsOk() ) + registerSound.Play( wxSOUND_ASYNC ); + } } /* - locale.AddCatalog(lang) calls wxLogWarning and returns true for corrupted .mo file, - so I should use locale.IsLoaded(lang) also. + locale.AddCatalog(lang) calls wxLogWarning and returns true for corrupted .mo file, + so I should use locale.IsLoaded(lang) also. */ wxString mmSelectLanguage( wxWindow *parent, wxSQLite3Database* inidb, bool forced_show_dlg ) { - wxString lang; + wxString lang; - const wxString langPath = mmex::getPathShared( mmex::LANG_DIR ); - wxLocale &locale = wxGetApp().getLocale(); - bool verbose = forced_show_dlg; + const wxString langPath = mmex::getPathShared( mmex::LANG_DIR ); + wxLocale &locale = wxGetApp().getLocale(); + bool verbose = forced_show_dlg; - if ( wxDir::Exists( langPath ) ) { - locale.AddCatalogLookupPathPrefix( langPath ); - } else { - if ( verbose ) { - wxString s = wxT( "Directory of language files does not exist:\n\"" ); - s << langPath << wxT( '\"' ); + if ( wxDir::Exists( langPath ) ) { + locale.AddCatalogLookupPathPrefix( langPath ); + } else { + if ( verbose ) { + wxString s = wxT( "Directory of language files does not exist:\n\"" ); + s << langPath << wxT( '\"' ); - wxMessageDialog dlg( parent, s, wxT( "Error" ), wxICON_ERROR ); - dlg.ShowModal(); - } - - return lang; + wxMessageDialog dlg( parent, s, wxT( "Error" ), wxICON_ERROR ); + dlg.ShowModal(); } - const wxString param_lang( wxT( "LANGUAGE" ) ); + return lang; + } - if ( !forced_show_dlg ) { + const wxString param_lang( wxT( "LANGUAGE" ) ); - lang = mmDBWrapper::getINISettingValue( inidb, param_lang ); + if ( !forced_show_dlg ) { - if ( !lang.empty() && locale.AddCatalog( lang ) && locale.IsLoaded( lang ) ) { - mmOptions::language = lang; - return lang; - } + lang = mmDBWrapper::getINISettingValue( inidb, param_lang ); + + if ( !lang.empty() && locale.AddCatalog( lang ) && locale.IsLoaded( lang ) ) { + mmOptions::language = lang; + return lang; } + } - lang = selectLanguageDlg( parent, langPath, verbose ); + lang = selectLanguageDlg( parent, langPath, verbose ); - if ( !lang.empty() ) { + if ( !lang.empty() ) { - bool ok = locale.AddCatalog( lang ) && locale.IsLoaded( lang ); + bool ok = locale.AddCatalog( lang ) && locale.IsLoaded( lang ); - if ( !ok ) { - lang.clear(); // bad .mo file - } - - mmOptions::language = lang; - mmDBWrapper::setINISettingValue( inidb, param_lang, lang ); + if ( !ok ) { + lang.clear(); // bad .mo file } - return lang; + mmOptions::language = lang; + mmDBWrapper::setINISettingValue( inidb, param_lang, lang ); + } + + return lang; } wxString mmReadyDisplayString( const wxString& orig ) { - wxString toReturn = orig; - toReturn.Replace( wxT( "&" ), wxT( "&&" ) ); - return toReturn; + wxString toReturn = orig; + toReturn.Replace( wxT( "&" ), wxT( "&&" ) ); + return toReturn; } wxString mmUnCleanString( const wxString& orig ) { - wxString toReturn = orig; - toReturn.Replace( wxT( "''" ), wxT( "'" ) ); - return toReturn; + wxString toReturn = orig; + toReturn.Replace( wxT( "''" ), wxT( "'" ) ); + return toReturn; } wxString mmGetNiceMonthName( int month ) { - static const wxString mon[12] = { - _( "January" ), _( "February" ), _( "March" ), _( "April" ), _( "May " ), _( "June" ), - _( "July" ), _( "August" ), _( "September" ), _( "October" ), _( "November" ), _( "December" ) - }; + static const wxString mon[12] = { + _( "January" ), _( "February" ), _( "March" ), _( "April" ), _( "May " ), _( "June" ), + _( "July" ), _( "August" ), _( "September" ), _( "October" ), _( "November" ), _( "December" ) + }; - wxASSERT( month >= 0 && month < 12 ); - return mon[month]; + wxASSERT( month >= 0 && month < 12 ); + return mon[month]; } wxString mmGetNiceShortMonthName( int month ) { - static const wxString mon[12] = { - _( "Jan" ), _( "Feb" ), _( "Mar" ), _( "Apr" ), _( "May" ), _( "Jun" ), - _( "Jul" ), _( "Aug" ), _( "Sep" ), _( "Oct" ), _( "Nov" ), _( "Dec" ) - }; + static const wxString mon[12] = { + _( "Jan" ), _( "Feb" ), _( "Mar" ), _( "Apr" ), _( "May" ), _( "Jun" ), + _( "Jul" ), _( "Aug" ), _( "Sep" ), _( "Oct" ), _( "Nov" ), _( "Dec" ) + }; - wxASSERT( month >= 0 && month < 12 ); - return mon[month]; + wxASSERT( month >= 0 && month < 12 ); + return mon[month]; } wxString mmGetNiceDateString( const wxDateTime &dt ) { - static const wxString gDaysInWeek[7] = { - _( "Sunday" ), _( "Monday" ), _( "Tuesday" ), _( "Wednesday" ), - _( "Thursday" ), _( "Friday" ), _( "Saturday" ) - }; + static const wxString gDaysInWeek[7] = { + _( "Sunday" ), _( "Monday" ), _( "Tuesday" ), _( "Wednesday" ), + _( "Thursday" ), _( "Friday" ), _( "Saturday" ) + }; - wxString dts( gDaysInWeek[dt.GetWeekDay()] + wxString( wxT( ", " ) ) ); + wxString dts( gDaysInWeek[dt.GetWeekDay()] + wxString( wxT( ", " ) ) ); -// Discover the date format set by the user - wxString dateFmt = mmOptions::dateFormat.Mid(1,1).MakeUpper(); +// Discover the date format set by the user + wxString dateFmt = mmOptions::dateFormat.Mid(1,1).MakeUpper(); -// Format date as: DDD, DD MMM YYYY - if ( dateFmt == wxT("D")) { - dts += wxString::Format( wxT( "%d" ), dt.GetDay() ) + wxString( wxT( " " ) ); - dts += mmGetNiceMonthName( dt.GetMonth() ) + wxT( " " ); - dts += wxString::Format( wxT( "%d" ), dt.GetYear() ); +// Format date as: DDD, DD MMM YYYY + if ( dateFmt == wxT("D")) { + dts += wxString::Format( wxT( "%d" ), dt.GetDay() ) + wxString( wxT( " " ) ); + dts += mmGetNiceMonthName( dt.GetMonth() ) + wxT( " " ); + dts += wxString::Format( wxT( "%d" ), dt.GetYear() ); -// Format date as: DDD, YYYY MMM DD - } else if ( dateFmt == wxT("Y")) { - dts += wxString::Format( wxT( "%d" ), dt.GetYear() ) + wxString( wxT( " " ) ); - dts += mmGetNiceMonthName( dt.GetMonth() ) + wxT( " " ); - dts += wxString::Format( wxT( "%d" ), dt.GetDay() ); +// Format date as: DDD, YYYY MMM DD + } else if ( dateFmt == wxT("Y")) { + dts += wxString::Format( wxT( "%d" ), dt.GetYear() ) + wxString( wxT( " " ) ); + dts += mmGetNiceMonthName( dt.GetMonth() ) + wxT( " " ); + dts += wxString::Format( wxT( "%d" ), dt.GetDay() ); -// Format date as: DDD, MMM DD, YYYY - } else { - dts += mmGetNiceMonthName( dt.GetMonth() ) + wxString( wxT( " " ) ); - dts += wxString::Format( wxT( "%d" ), dt.GetDay() ) + wxT( ", " ) - + wxString::Format( wxT( "%d" ), dt.GetYear() ); - } +// Format date as: DDD, MMM DD, YYYY + } else { + dts += mmGetNiceMonthName( dt.GetMonth() ) + wxString( wxT( " " ) ); + dts += wxString::Format( wxT( "%d" ), dt.GetDay() ) + wxT( ", " ) + + wxString::Format( wxT( "%d" ), dt.GetYear() ); + } - return dts; + return dts; } wxString mmGetNiceDateSimpleString( const wxDateTime &dt ) { - wxString dts = mmGetNiceMonthName( dt.GetMonth() ) + wxString( wxT( " " ) ); + wxString dts = mmGetNiceMonthName( dt.GetMonth() ) + wxString( wxT( " " ) ); -// Discover the date format set by the user - wxString dateFmt = mmOptions::dateFormat.Mid(1,1).MakeUpper(); -// Format date as: DD MMM YYYY - if ( dateFmt == wxT("D")) { - dts = wxString::Format( wxT( "%d" ), dt.GetDay() ) + wxString( wxT( " " ) ) +// Discover the date format set by the user + wxString dateFmt = mmOptions::dateFormat.Mid(1,1).MakeUpper(); +// Format date as: DD MMM YYYY + if ( dateFmt == wxT("D")) { + dts = wxString::Format( wxT( "%d" ), dt.GetDay() ) + wxString( wxT( " " ) ) + dts + wxString::Format( wxT( "%d" ), dt.GetYear() ); -// Format date as: YYYY MMM DD - } else if ( dateFmt == wxT("Y")) { - dts = wxString::Format( wxT( "%d" ), dt.GetYear() ) + wxString( wxT( " " ) ) - + dts - + wxString::Format( wxT( "%d" ), dt.GetDay() ); +// Format date as: YYYY MMM DD + } else if ( dateFmt == wxT("Y")) { + dts = wxString::Format( wxT( "%d" ), dt.GetYear() ) + wxString( wxT( " " ) ) + + dts + + wxString::Format( wxT( "%d" ), dt.GetDay() ); -// Format date as: MMM DD, YYYY - } else { - dts += wxString::Format( wxT( "%d" ), dt.GetDay() ) + wxT( ", " ) - + wxString::Format( wxT( "%d" ), dt.GetYear() ); - } +// Format date as: MMM DD, YYYY + } else { + dts += wxString::Format( wxT( "%d" ), dt.GetDay() ) + wxT( ", " ) + + wxString::Format( wxT( "%d" ), dt.GetYear() ); + } - return dts; + return dts; } void mmShowErrorMessage( wxWindow *parent, const wxString &message, const wxString &messageheader ) { - wxMessageDialog msgDlg( parent, message, messageheader ); - msgDlg.ShowModal(); + wxMessageDialog msgDlg( parent, message, messageheader ); + msgDlg.ShowModal(); } void mmShowErrorMessageInvalid( wxWindow *parent, const wxString &message ) { - wxString msg = _( "Entry " ) + message + _( " is invalid." ); - mmShowErrorMessage( parent, msg, _( "Invalid Entry" ) ); + wxString msg = _( "Entry " ) + message + _( " is invalid." ); + mmShowErrorMessage( parent, msg, _( "Invalid Entry" ) ); } wxString mmNotes4ExportString( const wxString& orig ) { - wxString toReturn = orig; + wxString toReturn = orig; - if ( !toReturn.IsEmpty() ) { - //It's should be 1 line for each transaction in the exported file - toReturn.Replace( wxT( "\n" ), wxT( " " ) ); - toReturn.Trim(); - //Double quotas should be doubled in the notes. - toReturn.Replace( wxT( "\"" ), wxT( "\"\"" ) ); - wxString text = wxT( "\"" ) + toReturn + wxT( "\"" ); - toReturn = text; - } + if ( !toReturn.IsEmpty() ) { + //It's should be 1 line for each transaction in the exported file + toReturn.Replace( wxT( "\n" ), wxT( " " ) ); + toReturn.Trim(); + //Double quotas should be doubled in the notes. + toReturn.Replace( wxT( "\"" ), wxT( "\"\"" ) ); + wxString text = wxT( "\"" ) + toReturn + wxT( "\"" ); + toReturn = text; + } - return toReturn; + return toReturn; } void mmExportCSV( wxSQLite3Database* db_ ) { - if ( mmDBWrapper::getNumAccounts( db_ ) == 0 ) { - mmShowErrorMessage( 0, _( "No Account available! Cannot Export!" ), _( "Error" ) ); - return; - } + if ( mmDBWrapper::getNumAccounts( db_ ) == 0 ) { + mmShowErrorMessage( 0, _( "No Account available! Cannot Export!" ), _( "Error" ) ); + return; + } - wxArrayString as; + wxArrayString as; - { - wxSQLite3ResultSet q1 = db_->ExecuteQuery( g_AccountNameSQL ); + { + wxSQLite3ResultSet q1 = db_->ExecuteQuery( g_AccountNameSQL ); - while ( q1.NextRow() ) { - as.Add( q1.GetString( wxT( "ACCOUNTNAME" ) ) ); - } + while ( q1.NextRow() ) { + as.Add( q1.GetString( wxT( "ACCOUNTNAME" ) ) ); + } - q1.Finalize(); - } + q1.Finalize(); + } - wxString delimit = mmDBWrapper::getInfoSettingValue( db_, wxT( "DELIMITER" ), mmex::DEFDELIMTER ); - wxString q = wxT("\""); + wxString delimit = mmDBWrapper::getInfoSettingValue( db_, wxT( "DELIMITER" ), mmex::DEFDELIMTER ); + wxString q = wxT("\""); - wxSingleChoiceDialog scd( 0, _( "Choose Account to Export from:" ), _( "CSV Export" ), as); + wxSingleChoiceDialog scd( 0, _( "Choose Account to Export from:" ), _( "CSV Export" ), as); - if ( scd.ShowModal() != wxID_OK ) - return; + if ( scd.ShowModal() != wxID_OK ) + return; - wxString acctName = scd.GetStringSelection(); - int fromAccountID = mmDBWrapper::getAccountID( db_, acctName ); + wxString acctName = scd.GetStringSelection(); + int fromAccountID = mmDBWrapper::getAccountID( db_, acctName ); - wxString fileName = wxFileSelector( _( "Choose CSV data file to Export" ), - wxT( "" ), wxT( "" ), wxT( "" ), wxT( "*.csv" ), wxSAVE | wxOVERWRITE_PROMPT ); + wxString fileName = wxFileSelector( _( "Choose CSV data file to Export" ), + wxT( "" ), wxT( "" ), wxT( "" ), wxT( "*.csv" ), wxSAVE | wxOVERWRITE_PROMPT ); - if ( fileName.empty() ) - return; + if ( fileName.empty() ) + return; - wxFileOutputStream output( fileName ); - wxTextOutputStream text( output ); + wxFileOutputStream output( fileName ); + wxTextOutputStream text( output ); - static const char sql[] = - "SELECT TRANSID, TRANSDATE, " - "TRANSCODE, TRANSAMOUNT, SUBCATEGID, " - "CATEGID, PAYEEID, " - "TRANSACTIONNUMBER, NOTES, TOACCOUNTID, ACCOUNTID " - "FROM CHECKINGACCOUNT_V1 " - "where ACCOUNTID = ? OR TOACCOUNTID = ?" - "ORDER BY TRANSDATE"; + static const char sql[] = + "SELECT TRANSID, TRANSDATE, " + "TRANSCODE, TRANSAMOUNT, SUBCATEGID, " + "CATEGID, PAYEEID, " + "TRANSACTIONNUMBER, NOTES, TOACCOUNTID, ACCOUNTID " + "FROM CHECKINGACCOUNT_V1 " + "where ACCOUNTID = ? OR TOACCOUNTID = ?" + "ORDER BY TRANSDATE"; - wxSQLite3Statement st = db_->PrepareStatement( sql ); - st.Bind( 1, fromAccountID ); - st.Bind( 2, fromAccountID ); + wxSQLite3Statement st = db_->PrepareStatement( sql ); + st.Bind( 1, fromAccountID ); + st.Bind( 2, fromAccountID ); - wxSQLite3ResultSet q1 = st.ExecuteQuery(); - int numRecords = 0; + wxSQLite3ResultSet q1 = st.ExecuteQuery(); + int numRecords = 0; - while ( q1.NextRow() ) { - wxString transid = q1.GetString( wxT( "TRANSID" ) ); - wxString dateDBString = q1.GetString( wxT( "TRANSDATE" ) ); - wxDateTime dtdt = mmGetStorageStringAsDate( dateDBString ); - wxString dateString = mmGetDateForDisplay( db_, dtdt ); + while ( q1.NextRow() ) { + wxString transid = q1.GetString( wxT( "TRANSID" ) ); + wxString dateDBString = q1.GetString( wxT( "TRANSDATE" ) ); + wxDateTime dtdt = mmGetStorageStringAsDate( dateDBString ); + wxString dateString = mmGetDateForDisplay( db_, dtdt ); - int sid, cid; - wxString payee = mmDBWrapper::getPayee( db_, q1.GetInt( wxT( "PAYEEID" ) ), sid, cid ); - wxString type = q1.GetString( wxT( "TRANSCODE" ) ); - wxString sign = wxT( "" ); - wxString amount = q1.GetString( wxT( "TRANSAMOUNT" ) ); + int sid, cid; + wxString payee = mmDBWrapper::getPayee( db_, q1.GetInt( wxT( "PAYEEID" ) ), sid, cid ); + wxString type = q1.GetString( wxT( "TRANSCODE" ) ); + wxString sign = wxT( "" ); + wxString amount = q1.GetString( wxT( "TRANSAMOUNT" ) ); - //Amount should be formated if delimiters is not "." or "," - if ( delimit != wxT( "." ) && delimit != wxT( "," ) ) { - double value = 0; - mmex::formatCurrencyToDouble( amount, value ); - mmex::formatDoubleToCurrencyEdit( value, amount ); - } + //Amount should be formated if delimiters is not "." or "," + if ( delimit != wxT( "." ) && delimit != wxT( "," ) ) { + double value = 0; + mmex::formatCurrencyToDouble( amount, value ); + mmex::formatDoubleToCurrencyEdit( value, amount ); + } - wxString categ = mmDBWrapper::getCategoryName( db_, q1.GetInt( wxT( "CATEGID" ))); - wxString subcateg = mmDBWrapper::getSubCategoryName( db_, - q1.GetInt( wxT( "CATEGID" ) ), q1.GetInt( wxT( "SUBCATEGID" ))); - wxString transNum = q1.GetString( wxT( "TRANSACTIONNUMBER" ) ) ; - wxString notes = q1.GetString( wxT( "NOTES" ) ); - wxString origtype = type; + wxString categ = mmDBWrapper::getCategoryName( db_, q1.GetInt( wxT( "CATEGID" ))); + wxString subcateg = mmDBWrapper::getSubCategoryName( db_, + q1.GetInt( wxT( "CATEGID" ) ), q1.GetInt( wxT( "SUBCATEGID" ))); + wxString transNum = q1.GetString( wxT( "TRANSACTIONNUMBER" ) ) ; + wxString notes = q1.GetString( wxT( "NOTES" ) ); + wxString origtype = type; - if ( type == wxT( "Transfer" ) ) - { - int tAccountID = q1.GetInt( wxT( "TOACCOUNTID" ) ); - int fAccountID = q1.GetInt( wxT( "ACCOUNTID" ) ); + if ( type == wxT( "Transfer" ) ) + { + int tAccountID = q1.GetInt( wxT( "TOACCOUNTID" ) ); + int fAccountID = q1.GetInt( wxT( "ACCOUNTID" ) ); - wxString fromAccount = mmDBWrapper::getAccountName( db_, fAccountID ); - wxString toAccount = mmDBWrapper::getAccountName( db_, tAccountID ); + wxString fromAccount = mmDBWrapper::getAccountName( db_, fAccountID ); + wxString toAccount = mmDBWrapper::getAccountName( db_, tAccountID ); - if ( tAccountID == fromAccountID ) { - type = wxT( "Deposit" ); - payee = fromAccount; - } else if ( fAccountID == fromAccountID ) { - type = wxT( "Withdrawal" ); - payee = toAccount; - //transfer = wxT("T"); - } - } + if ( tAccountID == fromAccountID ) { + type = wxT( "Deposit" ); + payee = fromAccount; + } else if ( fAccountID == fromAccountID ) { + type = wxT( "Withdrawal" ); + payee = toAccount; + //transfer = wxT("T"); + } + } - //It should be negative amounts for withdrwal - //if ( type == wxT( "Withdrawal" ) ) - // sign = wxT( "-" ); + //It should be negative amounts for withdrwal + //if ( type == wxT( "Withdrawal" ) ) + // sign = wxT( "-" ); - if ( categ.IsEmpty() && subcateg.IsEmpty() ) { - static const char sql4splitedtrx[] = - "SELECT SUBCATEGID, CATEGID, SPLITTRANSAMOUNT " - "FROM splittransactions_v1 " - "WHERE TRANSID = ?"; + if ( categ.IsEmpty() && subcateg.IsEmpty() ) { + static const char sql4splitedtrx[] = + "SELECT SUBCATEGID, CATEGID, SPLITTRANSAMOUNT " + "FROM splittransactions_v1 " + "WHERE TRANSID = ?"; - wxSQLite3Statement st2 = db_->PrepareStatement( sql4splitedtrx ); - st2.Bind( 1, transid ); + wxSQLite3Statement st2 = db_->PrepareStatement( sql4splitedtrx ); + st2.Bind( 1, transid ); - wxSQLite3ResultSet q2 = st2.ExecuteQuery(); + wxSQLite3ResultSet q2 = st2.ExecuteQuery(); - while ( q2.NextRow() ) { - wxString splitamount = q2.GetString( wxT( "SPLITTRANSAMOUNT" ) ); + while ( q2.NextRow() ) { + wxString splitamount = q2.GetString( wxT( "SPLITTRANSAMOUNT" ) ); - //Amount should be formated if delimiters is not "." or "," - if ( delimit != wxT( "." ) && delimit != wxT( "," ) ) { - double value = 0; - mmex::formatCurrencyToDouble( splitamount, value ); - mmex::formatDoubleToCurrencyEdit( value, splitamount ); - } + //Amount should be formated if delimiters is not "." or "," + if ( delimit != wxT( "." ) && delimit != wxT( "," ) ) { + double value = 0; + mmex::formatCurrencyToDouble( splitamount, value ); + mmex::formatDoubleToCurrencyEdit( value, splitamount ); + } - wxString splitcateg = mmDBWrapper::getCategoryName( db_, q2.GetInt( wxT( "CATEGID" ) ) ); - wxString splitsubcateg = mmDBWrapper::getSubCategoryName( db_, q2.GetInt( wxT( "CATEGID" ) ), q2.GetInt( wxT( "SUBCATEGID" ) ) ); - // We have a fixed format for now - // date, payeename, "withdrawal/deposit", amount, category, subcategory, transactionnumber, notes - text << dateString << delimit - << payee << delimit - << origtype << delimit - << splitamount << delimit - << splitcateg << delimit - << splitsubcateg << delimit - << transNum << delimit - << notes << endl; + wxString splitcateg = mmDBWrapper::getCategoryName( db_, q2.GetInt( wxT( "CATEGID" ) ) ); + wxString splitsubcateg = mmDBWrapper::getSubCategoryName( db_, q2.GetInt( wxT( "CATEGID" ) ), q2.GetInt( wxT( "SUBCATEGID" ) ) ); + // We have a fixed format for now + // date, payeename, "withdrawal/deposit", amount, category, subcategory, transactionnumber, notes + text << dateString << delimit + << payee << delimit + << origtype << delimit + << splitamount << delimit + << splitcateg << delimit + << splitsubcateg << delimit + << transNum << delimit + << notes << endl; - } + } - st2.Finalize(); - } else { - text << dateString << delimit - << payee << delimit - << origtype << delimit - << amount << delimit - << categ << delimit - << subcateg << delimit - << transNum << delimit - << notes << endl; - } - - numRecords++; + st2.Finalize(); + } else { + text << dateString << delimit + << payee << delimit + << origtype << delimit + << amount << delimit + << categ << delimit + << subcateg << delimit + << transNum << delimit + << notes << endl; } - st.Finalize(); + numRecords++; + } - wxString msg = wxString::Format( wxT( "%d transactions exported" ), numRecords ); - mmShowErrorMessage( 0, msg, _( "Export to CSV" ) ); + st.Finalize(); + + wxString msg = wxString::Format( wxT( "%d transactions exported" ), numRecords ); + mmShowErrorMessage( 0, msg, _( "Export to CSV" ) ); } void mmExportQIF( wxSQLite3Database* db_ ) { - if ( mmDBWrapper::getNumAccounts( db_ ) == 0 ) { - mmShowErrorMessage( 0, _( "No Account available! Cannot Export!" ), _( "Error" ) ); - return; - } + if ( mmDBWrapper::getNumAccounts( db_ ) == 0 ) { + mmShowErrorMessage( 0, _( "No Account available! Cannot Export!" ), _( "Error" ) ); + return; + } - wxArrayString as; + wxArrayString as; - wxSQLite3ResultSet q3 = db_->ExecuteQuery( g_AccountNameSQL ); + wxSQLite3ResultSet q3 = db_->ExecuteQuery( g_AccountNameSQL ); - while ( q3.NextRow() ) { - as.Add( q3.GetString( wxT( "ACCOUNTNAME" ) ) ); - } + while ( q3.NextRow() ) { + as.Add( q3.GetString( wxT( "ACCOUNTNAME" ) ) ); + } - q3.Finalize(); + q3.Finalize(); - wxString delimit = mmDBWrapper::getInfoSettingValue( db_, wxT( "DELIMITER" ), mmex::DEFDELIMTER ); - wxString q = wxT("\""); + wxString delimit = mmDBWrapper::getInfoSettingValue( db_, wxT( "DELIMITER" ), mmex::DEFDELIMTER ); + wxString q = wxT("\""); - wxSingleChoiceDialog scd( 0, _( "Choose Account to Export from:" ),_( "QIF Export" ), as ); + wxSingleChoiceDialog scd( 0, _( "Choose Account to Export from:" ),_( "QIF Export" ), as ); - wxString acctName; + wxString acctName; - if ( scd.ShowModal() == wxID_OK ) { - acctName = scd.GetStringSelection(); - } + if ( scd.ShowModal() == wxID_OK ) { + acctName = scd.GetStringSelection(); + } - if ( acctName.IsEmpty() ) { - return; - } + if ( acctName.IsEmpty() ) { + return; + } - wxString fileName = wxFileSelector( _( "Choose QIF data file to Export" ), wxT( "" ), wxT( "" ), wxT( "" ), wxT( "*.qif" ), wxSAVE | wxOVERWRITE_PROMPT ); + wxString fileName = wxFileSelector( _( "Choose QIF data file to Export" ), wxT( "" ), wxT( "" ), wxT( "" ), wxT( "*.qif" ), wxSAVE | wxOVERWRITE_PROMPT ); - if ( fileName.IsEmpty() ) { - return; - } + if ( fileName.IsEmpty() ) { + return; + } - wxFileOutputStream output( fileName ); - wxTextOutputStream text( output ); - int fromAccountID = mmDBWrapper::getAccountID( db_, acctName ); + wxFileOutputStream output( fileName ); + wxTextOutputStream text( output ); + int fromAccountID = mmDBWrapper::getAccountID( db_, acctName ); - static const char sql[] = - "SELECT transid, transdate as DATE, " - "transcode as TRANSACTIONTYPE, transamount as AMOUNT, totransamount as TOAMOUNT, " - " SUBCATEGID, " - "CATEGID, PAYEEID, " - "TRANSACTIONNUMBER, NOTES, TOACCOUNTID, ACCOUNTID " - "FROM checkingaccount_v1 " - "WHERE ACCOUNTID = ? OR TOACCOUNTID = ?" - "ORDER BY transdate"; + static const char sql[] = + "SELECT transid, transdate as DATE, " + "transcode as TRANSACTIONTYPE, transamount as AMOUNT, totransamount as TOAMOUNT, " + " SUBCATEGID, " + "CATEGID, PAYEEID, " + "TRANSACTIONNUMBER, NOTES, TOACCOUNTID, ACCOUNTID " + "FROM checkingaccount_v1 " + "WHERE ACCOUNTID = ? OR TOACCOUNTID = ?" + "ORDER BY transdate"; - wxSQLite3Statement st = db_->PrepareStatement( sql ); - st.Bind( 1, fromAccountID ); - st.Bind( 2, fromAccountID ); + wxSQLite3Statement st = db_->PrepareStatement( sql ); + st.Bind( 1, fromAccountID ); + st.Bind( 2, fromAccountID ); - wxSQLite3ResultSet q1 = st.ExecuteQuery(); - int numRecords = 0; + wxSQLite3ResultSet q1 = st.ExecuteQuery(); + int numRecords = 0; - text - << wxT( "!Account" ) << endl - << wxT( "N" ) << acctName << endl - << wxT( "TChecking" ) << endl - << wxT( "^" ) << endl - << wxT( "!Type:Cash" ) << endl; + text << wxT( "!Account" ) << endl + << wxT( "N" ) << acctName << endl + << wxT( "TChecking" ) << endl + << wxT( "^" ) << endl + << wxT( "!Type:Cash" ) << endl; - while ( q1.NextRow() ) { - wxString transid = q1.GetString( wxT( "TRANSID" ) ); - wxString dateDBString = q1.GetString( wxT( "DATE" ) ); - wxDateTime dtdt = mmGetStorageStringAsDate( dateDBString ); - wxString dateString = mmGetDateForDisplay( db_, dtdt ); + while ( q1.NextRow() ) { + wxString transid = q1.GetString( wxT( "TRANSID" ) ); + wxString dateDBString = q1.GetString( wxT( "DATE" ) ); + wxDateTime dtdt = mmGetStorageStringAsDate( dateDBString ); + wxString dateString = mmGetDateForDisplay( db_, dtdt ); - int sid, cid; - wxString payee = mmDBWrapper::getPayee( db_, q1.GetInt( wxT( "PAYEEID" ) ), sid, cid ); - wxString type = q1.GetString( wxT( "TRANSACTIONTYPE" ) ); + int sid, cid; + wxString payee = mmDBWrapper::getPayee( db_, q1.GetInt( wxT( "PAYEEID" ) ), sid, cid ); + wxString type = q1.GetString( wxT( "TRANSACTIONTYPE" ) ); - wxString amount = q1.GetString( wxT( "AMOUNT" ) ); - //Amount should be formated - double value = 0.0; - mmex::formatCurrencyToDouble( amount, value ); - mmex::formatDoubleToCurrencyEdit( value, amount ); + wxString amount = q1.GetString( wxT( "AMOUNT" ) ); + //Amount should be formated + double value = 0.0; + mmex::formatCurrencyToDouble( amount, value ); + mmex::formatDoubleToCurrencyEdit( value, amount ); - wxString toamount = q1.GetString( wxT( "TOAMOUNT" ) ); - //Amount should be formated - value = 0.0; - mmex::formatCurrencyToDouble( toamount, value ); - mmex::formatDoubleToCurrencyEdit( value, toamount ); + wxString toamount = q1.GetString( wxT( "TOAMOUNT" ) ); + //Amount should be formated + value = 0.0; + mmex::formatCurrencyToDouble( toamount, value ); + mmex::formatDoubleToCurrencyEdit( value, toamount ); - wxString transNum = q1.GetString( wxT( "TRANSACTIONNUMBER" ) ); - wxString categ = mmDBWrapper::getCategoryName( db_, q1.GetInt( wxT( "CATEGID" ) ) ); - wxString subcateg = mmDBWrapper::getSubCategoryName( db_, - q1.GetInt( wxT( "CATEGID" ) ), q1.GetInt( wxT( "SUBCATEGID" ) ) ); - wxString notes = mmUnCleanString( q1.GetString( wxT( "NOTES" ) ) ); + wxString transNum = q1.GetString( wxT( "TRANSACTIONNUMBER" ) ); + wxString categ = mmDBWrapper::getCategoryName( db_, q1.GetInt( wxT( "CATEGID" ) ) ); + wxString subcateg = mmDBWrapper::getSubCategoryName( db_, + q1.GetInt( wxT( "CATEGID" ) ), q1.GetInt( wxT( "SUBCATEGID" ) ) ); + wxString notes = mmUnCleanString( q1.GetString( wxT( "NOTES" ) ) ); - // - notes.Replace( wxT( "\n" ), wxT( " " ) ); - wxString subcategStr = wxT ("") ; + // + notes.Replace( wxT( "\n" ), wxT( " " ) ); + wxString subcategStr = wxT ("") ; - if ( type == wxT( "Transfer" ) ) - { - subcategStr = type; - int tAccountID = q1.GetInt( wxT( "TOACCOUNTID" ) ); - int fAccountID = q1.GetInt( wxT( "ACCOUNTID" ) ); + if ( type == wxT( "Transfer" ) ) + { + subcategStr = type; + int tAccountID = q1.GetInt( wxT( "TOACCOUNTID" ) ); + int fAccountID = q1.GetInt( wxT( "ACCOUNTID" ) ); - wxString fromAccount = mmDBWrapper::getAccountName( db_, fAccountID ); - wxString toAccount = mmDBWrapper::getAccountName( db_, tAccountID ); + wxString fromAccount = mmDBWrapper::getAccountName( db_, fAccountID ); + wxString toAccount = mmDBWrapper::getAccountName( db_, tAccountID ); - if ( tAccountID == fromAccountID ) { - payee = fromAccount; - amount = toamount; - } else if ( fAccountID == fromAccountID ) { - payee = toAccount; - amount = wxT ('-') + amount; - } - } - else - { - subcategStr << categ << ( subcateg != wxT( "" ) ? wxT( ":" ) : wxT( "" ) ) << subcateg; - } + if ( tAccountID == fromAccountID ) { + payee = fromAccount; + amount = toamount; + } else if ( fAccountID == fromAccountID ) { + payee = toAccount; + amount = wxT ('-') + amount; + } + } + else + { + subcategStr << categ << ( subcateg != wxT( "" ) ? wxT( ":" ) : wxT( "" ) ) << subcateg; + } - text << wxT( 'D' ) << dateString << endl - << wxT( 'T' ) << ( type == wxT( "Withdrawal" ) ? wxT( "-" ) : wxT( "" ) ) << amount << endl //FIXME: is T needed when Transfer? - << wxT( 'P' ) << payee << endl - << wxT( 'N' ) << transNum << endl - //Category or Transfer - << wxT( 'L' ) << subcategStr << endl - << wxT( 'M' ) << notes << endl; - if ( type == wxT( "Transfer" ) ) - { - text << wxT('$') << amount << endl; - } + text << wxT( 'D' ) << dateString << endl + << wxT( 'T' ) << ( type == wxT( "Withdrawal" ) ? wxT( "-" ) : wxT( "" ) ) << amount << endl //FIXME: is T needed when Transfer? + << wxT( 'P' ) << payee << endl + << wxT( 'N' ) << transNum << endl + //Category or Transfer + << wxT( 'L' ) << subcategStr << endl + << wxT( 'M' ) << notes << endl; + if ( type == wxT( "Transfer" ) ) + { + text << wxT('$') << amount << endl; + } - //if categ id is empty the transaction has been splited - if ( categ.IsEmpty() && subcateg.IsEmpty() ) { - static const char sql4splitedtrx[] = - "SELECT SUBCATEGID, CATEGID, SPLITTRANSAMOUNT " - "FROM splittransactions_v1 " - "WHERE TRANSID = ?"; + //if categ id is empty the transaction has been splited + if ( categ.IsEmpty() && subcateg.IsEmpty() ) { + static const char sql4splitedtrx[] = + "SELECT SUBCATEGID, CATEGID, SPLITTRANSAMOUNT " + "FROM splittransactions_v1 " + "WHERE TRANSID = ?"; - wxSQLite3Statement st2 = db_->PrepareStatement( sql4splitedtrx ); - st2.Bind( 1, transid ); + wxSQLite3Statement st2 = db_->PrepareStatement( sql4splitedtrx ); + st2.Bind( 1, transid ); - wxSQLite3ResultSet q2 = st2.ExecuteQuery(); + wxSQLite3ResultSet q2 = st2.ExecuteQuery(); - while ( q2.NextRow() ) { - wxString splitamount = q2.GetString( wxT( "SPLITTRANSAMOUNT" ) ); - //Amount should be formated - value = 0.0; - mmex::formatCurrencyToDouble( splitamount, value ); - mmex::formatDoubleToCurrencyEdit( value, splitamount ); - wxString splitcateg = mmDBWrapper::getCategoryName( db_, q2.GetInt( wxT( "CATEGID" ) ) ); - wxString splitsubcateg = mmDBWrapper::getSubCategoryName( db_, - q2.GetInt( wxT( "CATEGID" ) ), q2.GetInt( wxT( "SUBCATEGID" ) ) ); - text << wxT( 'S' ) << splitcateg << ( splitsubcateg != wxT( "" ) ? wxT( ":" ) : wxT( "" ) ) << splitsubcateg << endl - << wxT( '$' ) << ( type == wxT( "Withdrawal" ) ? wxT( "-" ) : wxT( "" ) ) << splitamount << endl - // E Split memo — any text to go with this split item. I saggest Category:Subcategory = Amount for earch line - << wxT( 'E' ) << splitcateg << ( splitsubcateg != wxT( "" ) ? wxT( ":" ) : wxT( "" ) ) << splitsubcateg << ( type == wxT( "Withdrawal" ) ? wxT( " -" ) : wxT( " " ) ) << splitamount << endl; - } + while ( q2.NextRow() ) { + wxString splitamount = q2.GetString( wxT( "SPLITTRANSAMOUNT" ) ); + //Amount should be formated + value = 0.0; + mmex::formatCurrencyToDouble( splitamount, value ); + mmex::formatDoubleToCurrencyEdit( value, splitamount ); + wxString splitcateg = mmDBWrapper::getCategoryName( db_, q2.GetInt( wxT( "CATEGID" ) ) ); + wxString splitsubcateg = mmDBWrapper::getSubCategoryName( db_, + q2.GetInt( wxT( "CATEGID" ) ), q2.GetInt( wxT( "SUBCATEGID" ) ) ); + text << wxT( 'S' ) << splitcateg << ( splitsubcateg != wxT( "" ) ? wxT( ":" ) : wxT( "" ) ) << splitsubcateg << endl + << wxT( '$' ) << ( type == wxT( "Withdrawal" ) ? wxT( "-" ) : wxT( "" ) ) << splitamount << endl + // E Split memo — any text to go with this split item. I saggest Category:Subcategory = Amount for earch line + << wxT( 'E' ) << splitcateg << ( splitsubcateg != wxT( "" ) ? wxT( ":" ) : wxT( "" ) ) << splitsubcateg << ( type == wxT( "Withdrawal" ) ? wxT( " -" ) : wxT( " " ) ) << splitamount << endl; + } - q2.Finalize(); - } - - text << wxT( '^' ) << endl; - numRecords++; + q2.Finalize(); } - q1.Finalize(); + text << wxT( '^' ) << endl; + numRecords++; + } - wxString msg = wxString::Format( wxT( "%d transactions exported" ), numRecords ); - mmShowErrorMessage( 0, msg, _( "Export to QIF" ) ); + q1.Finalize(); + + wxString msg = wxString::Format( wxT( "%d transactions exported" ), numRecords ); + mmShowErrorMessage( 0, msg, _( "Export to QIF" ) ); } void mmSetCategory( mmCoreDB* core, wxString categ, wxString subcateg, int& categID, int& subCategID ) { - bool isUnknown = false; + bool isUnknown = false; - if ( categ.Trim().IsEmpty() ) { - categ = wxT( "Unknown" ); - isUnknown = true; - } + if ( categ.Trim().IsEmpty() ) { + categ = wxT( "Unknown" ); + isUnknown = true; + } - // Create category (unknown or otherwise) if it doesn't already exist - categID = core->categoryList_.getCategoryID( categ ); + // Create category (unknown or otherwise) if it doesn't already exist + categID = core->categoryList_.getCategoryID( categ ); - if ( categID == -1 ) { - categID = core->categoryList_.addCategory( categ ); - } + if ( categID == -1 ) { + categID = core->categoryList_.addCategory( categ ); + } - // Create sub category, provided it is not for the Unknown category - if ( !isUnknown && !subcateg.Trim().IsEmpty() ) { - subCategID = core->categoryList_.getSubCategoryID( categID, subcateg ); + // Create sub category, provided it is not for the Unknown category + if ( !isUnknown && !subcateg.Trim().IsEmpty() ) { + subCategID = core->categoryList_.getSubCategoryID( categID, subcateg ); - if ( subCategID == -1 ) { - subCategID = core->categoryList_.addSubCategory( categID, subcateg ); - } + if ( subCategID == -1 ) { + subCategID = core->categoryList_.addSubCategory( categID, subcateg ); } + } } int mmImportCSV( mmCoreDB* core ) { - wxSQLite3Database* db_ = core->db_.get(); + wxSQLite3Database* db_ = core->db_.get(); - if ( mmDBWrapper::getNumAccounts( db_ ) == 0 ) { - mmShowErrorMessage( 0, _( "No Account available! Cannot Import! Create a new account first!" ), - _( "Error" ) ); - return -1; - } + if ( mmDBWrapper::getNumAccounts( db_ ) == 0 ) { + wxMessageBox(_("No Account available! Cannot Import! Create a new account first!" ), _( "Import from CSV" ),wxICON_ERROR); + return -1; + } - wxArrayString as; - int fromAccountID = -1; + wxArrayString as; + int fromAccountID = -1; - wxSQLite3ResultSet q1 = db_->ExecuteQuery( g_AccountNameSQL ); + wxSQLite3ResultSet q1 = db_->ExecuteQuery( g_AccountNameSQL ); - while ( q1.NextRow() ) { - as.Add( q1.GetString( wxT( "ACCOUNTNAME" ) ) ); - } + while ( q1.NextRow() ) { + as.Add( q1.GetString( wxT( "ACCOUNTNAME" ) ) ); + } - q1.Finalize(); + q1.Finalize(); - wxString delimit = mmDBWrapper::getInfoSettingValue( db_, wxT( "DELIMITER" ), mmex::DEFDELIMTER ); - wxString q = wxT("\""); + wxString delimit = mmDBWrapper::getInfoSettingValue( db_, wxT( "DELIMITER" ), mmex::DEFDELIMTER ); + wxString q = wxT("\""); - wxSingleChoiceDialog scd( 0, _( "Choose Account to import to:" ), _( "CSV Import" ), as ); + wxSingleChoiceDialog scd( 0, _( "Choose Account to import to:" ), _( "Import from CSV" ), as ); - if ( scd.ShowModal() == wxID_OK ) { - wxString acctName = scd.GetStringSelection(); - fromAccountID = mmDBWrapper::getAccountID( db_, acctName ); - boost::shared_ptr<mmCurrency> pCurrencyPtr = core->accountList_.getCurrencyWeakPtr( fromAccountID ).lock(); - wxASSERT( pCurrencyPtr ); + if ( scd.ShowModal() == wxID_OK ) { + wxString acctName = scd.GetStringSelection(); + fromAccountID = mmDBWrapper::getAccountID( db_, acctName ); + boost::shared_ptr<mmCurrency> pCurrencyPtr = core->accountList_.getCurrencyWeakPtr( fromAccountID ).lock(); + wxASSERT( pCurrencyPtr ); - wxString fileName = wxFileSelector( wxT( "Choose CSV data file to import" ), - wxT( "" ), wxT( "" ), wxT( "" ), wxT( "*.csv" ), wxFILE_MUST_EXIST ); + wxString fileName = wxFileSelector( wxT( "Choose CSV data file to import" ), + wxT( "" ), wxT( "" ), wxT( "" ), wxT( "*.csv" ), wxFILE_MUST_EXIST ); - if ( !fileName.empty() ) { - wxFileInputStream input( fileName ); - wxTextInputStream text( input ); + if ( !fileName.empty() ) { + wxFileInputStream input( fileName ); + wxTextInputStream text( input ); - wxFileName logFile = mmex::GetLogDir( true ); - logFile.SetFullName( fileName ); - logFile.SetExt( wxT( "txt" ) ); + wxFileName logFile = mmex::GetLogDir( true ); + logFile.SetFullName( fileName ); + logFile.SetExt( wxT( "txt" ) ); - wxFileOutputStream outputLog( logFile.GetFullPath() ); - wxTextOutputStream log( outputLog ); + wxFileOutputStream outputLog( logFile.GetFullPath() ); + wxTextOutputStream log( outputLog ); - // We have a fixed format for now - // date, payeename, "withdrawal/deposit", amount, category, subcategory, transactionnumber, notes - int countNumTotal = 0; - int countImported = 0; + // We have a fixed format for now + // date, payeename, "withdrawal/deposit", amount, category, subcategory, transactionnumber, notes + int countNumTotal = 0; + int countImported = 0; - while ( !input.Eof() ) { - wxString line = text.ReadLine(); + while ( !input.Eof() ) { + wxString line = text.ReadLine(); - if ( !line.IsEmpty() ) - ++countNumTotal; - else - continue; + if ( !line.IsEmpty() ) + ++countNumTotal; + else + continue; - wxString dt = wxDateTime::Now().FormatISODate(); - wxString payee; - wxString type; - wxString amount; - wxString categ; - wxString subcateg; - wxString transNum; - wxString notes; - wxString transfer; + wxString dt = wxDateTime::Now().FormatISODate(); + wxString payee; + wxString type; + wxString amount; + wxString categ; + wxString subcateg; + wxString transNum; + wxString notes; + wxString transfer; - wxStringTokenizer tkz( line, delimit, wxTOKEN_RET_EMPTY_ALL ); + wxStringTokenizer tkz( line, delimit, wxTOKEN_RET_EMPTY_ALL ); - if ( tkz.HasMoreTokens() ) - dt = tkz.GetNextToken(); - else { - log << _( "Line : " ) << countNumTotal - << _( " missing date, skipping." ) << endl; - continue; - } + if ( tkz.HasMoreTokens() ) + dt = mmCleanString( tkz.GetNextToken() ); + else { + log << _( "Line : " ) << countNumTotal << _( " missing date, skipping." ) << endl; + continue; + } - if ( tkz.HasMoreTokens() ) - payee = mmCleanString( tkz.GetNextToken() ); - else { - log << _( "Line : " ) << countNumTotal - << _( " missing payee, skipping." ) << endl; - continue; - } + if ( tkz.HasMoreTokens() ) + payee = mmCleanString( tkz.GetNextToken() ); + else { + log << _( "Line : " ) << countNumTotal << _( " missing payee, skipping." ) << endl; + continue; + } - if ( tkz.HasMoreTokens() ) - type = mmCleanString( tkz.GetNextToken() ); - else { - log << _( "Line : " ) << countNumTotal - << _( " missing transaction type, skipping." ) << endl; - continue; - } + if ( tkz.HasMoreTokens() ) + type = mmCleanString( tkz.GetNextToken() ); + else { + log << _( "Line : " ) << countNumTotal << _( " missing transaction type, skipping." ) << endl; + continue; + } - if ( tkz.HasMoreTokens() ) - amount = mmCleanString( tkz.GetNextToken() ); - else { - log << _( "Line : " ) << countNumTotal - << _( " missing amount, skipping." ) << endl; - continue; - } + if ( tkz.HasMoreTokens() ) + amount = mmCleanString( tkz.GetNextToken() ); + else { + log << _( "Line : " ) << countNumTotal << _( " missing amount, skipping." ) << endl; + continue; + } - if ( tkz.HasMoreTokens() ) - categ = mmCleanString( tkz.GetNextToken() ); + if ( tkz.HasMoreTokens() ) + categ = mmCleanString( tkz.GetNextToken() ); - if ( tkz.HasMoreTokens() ) - subcateg = mmCleanString( tkz.GetNextToken() ); + // Assuming that category is not a number. If it is, it could be part of the amount + if ( categ.IsNumber() ){ + log << _( "Line : " ) << countNumTotal << _( " check delimiters. Possible amount error, skipping." ) << endl; + continue; + } - if ( tkz.HasMoreTokens() ) - transNum = mmCleanString( tkz.GetNextToken() ); + if ( tkz.HasMoreTokens() ) + subcateg = mmCleanString( tkz.GetNextToken() ); - if ( tkz.HasMoreTokens() ) - notes = mmCleanString( tkz.GetNextToken() ); + if ( tkz.HasMoreTokens() ) + transNum = mmCleanString( tkz.GetNextToken() ); - if ( tkz.HasMoreTokens() ) - transfer = mmCleanString( tkz.GetNextToken() ); + if ( tkz.HasMoreTokens() ) + notes = mmCleanString( tkz.GetNextToken() ); - if ( dt.Trim().IsEmpty() || payee.Trim().IsEmpty() || - type.Trim().IsEmpty() || amount.Trim().IsEmpty() ) { - log << _( "Line : " ) << countNumTotal - << _( "one of the following fields: date, payee, transaction type, amount strings is empty, skipping" ) << endl; - continue; - } + if ( tkz.HasMoreTokens() ) + transfer = mmCleanString( tkz.GetNextToken() ); - if ( type.Cmp( wxT( "Withdrawal" ) ) && type.Cmp( wxT( "Deposit" ) ) ) { - log << _( "Line :... [truncated message content] |