Create trans/split props only when needed and have them keep their own value of date/currency format

This commit is contained in:
Geert Janssens 2016-12-11 14:54:48 +01:00 committed by Geert Janssens
parent 443237f2b9
commit 848c7b8f8e
3 changed files with 25 additions and 14 deletions

View File

@ -209,12 +209,12 @@ static boost::optional<gnc_numeric> parse_amount (const std::string &str, int cu
} }
void GncPreTrans::set_property (GncTransPropType prop_type, const std::string& value, int date_format) void GncPreTrans::set_property (GncTransPropType prop_type, const std::string& value)
{ {
switch (prop_type) switch (prop_type)
{ {
case GncTransPropType::DATE: case GncTransPropType::DATE:
m_date = parse_date (value.c_str(), date_format); // Throws if parsing fails m_date = parse_date (value, m_date_format); // Throws if parsing fails
break; break;
case GncTransPropType::NUM: case GncTransPropType::NUM:
@ -297,7 +297,7 @@ bool GncPreTrans::is_part_of (std::shared_ptr<GncPreTrans> parent)
(!m_differ || m_differ == parent->m_differ); (!m_differ || m_differ == parent->m_differ);
} }
void GncPreSplit::set_property (GncTransPropType prop_type, const std::string& value, int currency_format) void GncPreSplit::set_property (GncTransPropType prop_type, const std::string& value)
{ {
Account *acct = nullptr; Account *acct = nullptr;
switch (prop_type) switch (prop_type)
@ -347,13 +347,13 @@ void GncPreSplit::set_property (GncTransPropType prop_type, const std::string& v
break; break;
case GncTransPropType::BALANCE: case GncTransPropType::BALANCE:
m_balance = parse_amount (value, currency_format); // Will throw if parsing fails m_balance = parse_amount (value, m_currency_format); // Will throw if parsing fails
break; break;
case GncTransPropType::DEPOSIT: case GncTransPropType::DEPOSIT:
m_deposit = parse_amount (value, currency_format); // Will throw if parsing fails m_deposit = parse_amount (value, m_currency_format); // Will throw if parsing fails
break; break;
case GncTransPropType::WITHDRAWAL: case GncTransPropType::WITHDRAWAL:
m_withdrawal = parse_amount (value, currency_format); // Will throw if parsing fails m_withdrawal = parse_amount (value, m_currency_format); // Will throw if parsing fails
break; break;
default: default:

View File

@ -92,7 +92,9 @@ time64 parse_date (const std::string &date_str, int format);
struct GncPreTrans struct GncPreTrans
{ {
public: public:
void set_property (GncTransPropType prop_type, const std::string& value, int date_format = 0); GncPreTrans(int date_format) : m_date_format{date_format} {};
void set_property (GncTransPropType prop_type, const std::string& value);
std::string verify_essentials (void); std::string verify_essentials (void);
Transaction *create_trans (QofBook* book, gnc_commodity* currency); Transaction *create_trans (QofBook* book, gnc_commodity* currency);
@ -113,6 +115,7 @@ public:
bool is_part_of (std::shared_ptr<GncPreTrans> parent); bool is_part_of (std::shared_ptr<GncPreTrans> parent);
private: private:
int m_date_format;
boost::optional<time64> m_date; boost::optional<time64> m_date;
boost::optional<std::string> m_num; boost::optional<std::string> m_num;
boost::optional<std::string> m_desc; boost::optional<std::string> m_desc;
@ -124,7 +127,9 @@ private:
struct GncPreSplit struct GncPreSplit
{ {
public: public:
void set_property (GncTransPropType prop_type, const std::string& value, int currency_format = 0); GncPreSplit (int date_format, int currency_format) : m_date_format{date_format},
m_currency_format{currency_format}{};
void set_property (GncTransPropType prop_type, const std::string& value);
std::string verify_essentials (void); std::string verify_essentials (void);
boost::optional<gnc_numeric> create_split(Transaction* trans); boost::optional<gnc_numeric> create_split(Transaction* trans);
@ -132,6 +137,8 @@ public:
void set_account (Account* acct) { if (acct) m_account = acct; else m_account = boost::none; } void set_account (Account* acct) { if (acct) m_account = acct; else m_account = boost::none; }
private: private:
int m_date_format;
int m_currency_format;
boost::optional<std::string> m_action; boost::optional<std::string> m_action;
boost::optional<Account*> m_account; boost::optional<Account*> m_account;
boost::optional<gnc_numeric> m_deposit; boost::optional<gnc_numeric> m_deposit;

View File

@ -174,7 +174,7 @@ void GncTxImport::tokenize (bool guessColTypes)
for (auto tokenized_line : tokenizer->get_tokens()) for (auto tokenized_line : tokenizer->get_tokens())
{ {
parsed_lines.push_back (std::make_tuple (tokenized_line, std::string(), parsed_lines.push_back (std::make_tuple (tokenized_line, std::string(),
std::make_shared<GncPreTrans>(), std::make_shared<GncPreSplit>())); nullptr, nullptr));
auto length = tokenized_line.size(); auto length = tokenized_line.size();
if (length > max_cols) if (length > max_cols)
max_cols = length; max_cols = length;
@ -338,9 +338,9 @@ void GncTxImport::create_transaction (std::vector<parse_line_t>::iterator& parse
{ {
StrVec line; StrVec line;
std::string error_message; std::string error_message;
std::shared_ptr<GncPreTrans> trans_props; auto trans_props = std::make_shared<GncPreTrans>(date_format);
std::shared_ptr<GncPreSplit> split_props; auto split_props = std::make_shared<GncPreSplit>(date_format, currency_format);
std::tie(line, error_message, trans_props, split_props) = *parsed_line; std::tie(line, error_message, std::ignore, std::ignore) = *parsed_line;
error_message.clear(); error_message.clear();
/* Convert all tokens in this line into transaction/split properties. */ /* Convert all tokens in this line into transaction/split properties. */
@ -359,10 +359,10 @@ void GncTxImport::create_transaction (std::vector<parse_line_t>::iterator& parse
{ {
if (multi_split && line_it->empty()) if (multi_split && line_it->empty())
continue; // In multi-split mode, transaction properties can be empty continue; // In multi-split mode, transaction properties can be empty
trans_props->set_property(*col_types_it, *line_it, date_format); trans_props->set_property(*col_types_it, *line_it);
} }
else else
split_props->set_property(*col_types_it, *line_it, currency_format); split_props->set_property(*col_types_it, *line_it);
} }
catch (const std::exception& e) catch (const std::exception& e)
{ {
@ -393,10 +393,13 @@ void GncTxImport::create_transaction (std::vector<parse_line_t>::iterator& parse
error_message = _("First line of this transaction has errors."); error_message = _("First line of this transaction has errors.");
} }
else else
{
std::get<2>(*parsed_line) = trans_props;
/* This line starts a new transaction, set it as parent for /* This line starts a new transaction, set it as parent for
* subsequent lines. */ * subsequent lines. */
parent = trans_props; parent = trans_props;
} }
}
if (!error_message.empty()) if (!error_message.empty())
throw std::invalid_argument (error_message); throw std::invalid_argument (error_message);
@ -418,6 +421,7 @@ void GncTxImport::create_transaction (std::vector<parse_line_t>::iterator& parse
throw std::invalid_argument(error_message); throw std::invalid_argument(error_message);
} }
} }
std::get<3>(*parsed_line) = split_props;
/* If column parsing was successful, convert trans properties into a draft transaction. */ /* If column parsing was successful, convert trans properties into a draft transaction. */
try try