summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/csv.cc18
-rw-r--r--src/csv.h11
-rw-r--r--test/baseline/cmd-convert.test20
-rw-r--r--test/baseline/cmd-convert5.dat3
-rw-r--r--test/baseline/cmd-convert6.dat2
5 files changed, 45 insertions, 9 deletions
diff --git a/src/csv.cc b/src/csv.cc
index 40b8e01b..4ee7ff60 100644
--- a/src/csv.cc
+++ b/src/csv.cc
@@ -115,8 +115,10 @@ void csv_reader::read_index(std::istream& in)
index.push_back(FIELD_CODE);
else if (payee_mask.match(field))
index.push_back(FIELD_PAYEE);
- else if (amount_mask.match(field))
- index.push_back(FIELD_AMOUNT);
+ else if (credit_mask.match(field))
+ index.push_back(FIELD_CREDIT);
+ else if (debit_mask.match(field))
+ index.push_back(FIELD_DEBIT);
else if (cost_mask.match(field))
index.push_back(FIELD_COST);
else if (total_mask.match(field))
@@ -199,12 +201,19 @@ xact_t * csv_reader::read_xact(bool rich_data)
break;
}
- case FIELD_AMOUNT: {
+ case FIELD_DEBIT:
+ case FIELD_CREDIT: {
+ if (field.length() == 0)
+ break;
std::istringstream amount_str(field);
amt.parse(amount_str, PARSE_NO_REDUCE);
if (! amt.has_commodity() &&
commodity_pool_t::current_pool->default_commodity)
amt.set_commodity(*commodity_pool_t::current_pool->default_commodity);
+ if (index[n] == FIELD_DEBIT)
+ amt = -amt;
+ if (!post->amount.is_null())
+ throw_(csv_error, _("Cannot have two values for a single transaction"));
post->amount = amt;
break;
}
@@ -214,8 +223,7 @@ xact_t * csv_reader::read_xact(bool rich_data)
amt.parse(amount_str, PARSE_NO_REDUCE);
if (! amt.has_commodity() &&
commodity_pool_t::current_pool->default_commodity)
- amt.set_commodity
- (*commodity_pool_t::current_pool->default_commodity);
+ amt.set_commodity(*commodity_pool_t::current_pool->default_commodity);
post->cost = amt;
break;
}
diff --git a/src/csv.h b/src/csv.h
index 274ecc7e..9747a775 100644
--- a/src/csv.h
+++ b/src/csv.h
@@ -51,6 +51,8 @@ class xact_t;
class journal_t;
class account_t;
+DECLARE_EXCEPTION(csv_error, std::runtime_error);
+
class csv_reader
{
parse_context_t context;
@@ -60,7 +62,8 @@ class csv_reader
FIELD_DATE_AUX,
FIELD_CODE,
FIELD_PAYEE,
- FIELD_AMOUNT,
+ FIELD_CREDIT,
+ FIELD_DEBIT,
FIELD_COST,
FIELD_TOTAL,
FIELD_NOTE,
@@ -72,7 +75,8 @@ class csv_reader
mask_t date_aux_mask;
mask_t code_mask;
mask_t payee_mask;
- mask_t amount_mask;
+ mask_t credit_mask;
+ mask_t debit_mask;
mask_t cost_mask;
mask_t total_mask;
mask_t note_mask;
@@ -87,7 +91,8 @@ public:
date_aux_mask("posted( ?date)?"),
code_mask("code"),
payee_mask("(payee|desc(ription)?|title)"),
- amount_mask("amount"),
+ credit_mask("credit|amount"),
+ debit_mask("debit"),
cost_mask("cost"),
total_mask("total"),
note_mask("note") {
diff --git a/test/baseline/cmd-convert.test b/test/baseline/cmd-convert.test
index 8ee5bb2e..e59b727d 100644
--- a/test/baseline/cmd-convert.test
+++ b/test/baseline/cmd-convert.test
@@ -21,7 +21,7 @@ While parsing file "$sourcepath/test/baseline/cmd-convert3.dat", line 1:
While parsing CSV line:
01/01/2011,,
-Error: No quantity specified for amount
+Error: Only one posting with null amount allowed per transaction
end test
test -f /dev/null convert test/baseline/cmd-convert4.dat -> 1
@@ -33,3 +33,21 @@ While parsing CSV line:
Error: Invalid date: bogus
end test
+test -f /dev/null --input-date-format "%m/%d/%Y" convert test/baseline/cmd-convert5.dat
+2011/01/01 * test1
+ Expenses:Unknown 20.00 EUR
+ Equity:Unknown
+
+2011/01/02 * test2
+ Expenses:Unknown -10.00 EUR
+ Equity:Unknown
+end test
+
+test -f /dev/null --input-date-format "%m/%d/%Y" convert test/baseline/cmd-convert6.dat -> 1
+__ERROR__
+While parsing file "$sourcepath/test/baseline/cmd-convert6.dat", line 1:
+While parsing CSV line:
+ 01/01/2011,20.00 EUR,10.00 EUR,test1,
+
+Error: Cannot have two values for a single transaction
+end test
diff --git a/test/baseline/cmd-convert5.dat b/test/baseline/cmd-convert5.dat
new file mode 100644
index 00000000..f9c683a1
--- /dev/null
+++ b/test/baseline/cmd-convert5.dat
@@ -0,0 +1,3 @@
+date,credit,debit,desc,
+01/01/2011,20.00 EUR,,test1,
+01/02/2011,,10.00 EUR,test2, \ No newline at end of file
diff --git a/test/baseline/cmd-convert6.dat b/test/baseline/cmd-convert6.dat
new file mode 100644
index 00000000..d778d555
--- /dev/null
+++ b/test/baseline/cmd-convert6.dat
@@ -0,0 +1,2 @@
+date,credit,debit,desc,
+01/01/2011,20.00 EUR,10.00 EUR,test1, \ No newline at end of file