csv: accept a balance field assignment instead of an amount (#1000)

This commit is contained in:
Simon Michael 2019-04-03 10:46:26 -07:00
parent 72377a54c4
commit 61e8ccbf88
2 changed files with 13 additions and 5 deletions

View File

@ -452,13 +452,19 @@ validateRules :: CsvRules -> ExceptT String IO CsvRules
validateRules rules = do
unless (isAssigned "date") $ ExceptT $ return $ Left "Please specify (at top level) the date field. Eg: date %1\n"
unless ((amount && not (amountin || amountout)) ||
(not amount && (amountin && amountout)))
$ ExceptT $ return $ Left "Please specify (at top level) either the amount field, or both the amount-in and amount-out fields. Eg: amount %2\n"
(not amount && (amountin && amountout)) ||
balance)
$ ExceptT $ return $ Left $ unlines [
"Please specify (as a top level CSV rule) either the amount field,"
,"both the amount-in and amount-out fields, or the balance field. Eg:"
,"amount %2\n"
]
ExceptT $ return $ Right rules
where
amount = isAssigned "amount"
amountin = isAssigned "amount-in"
amount = isAssigned "amount"
amountin = isAssigned "amount-in"
amountout = isAssigned "amount-out"
balance = isAssigned "balance" || isAssigned "balance1" || isAssigned "balance2"
isAssigned f = isJust $ getEffectiveAssignment rules [] f
-- parsers

View File

@ -35,7 +35,9 @@ You can override this with the `--rules-file` option.
If the rules file does not exist, hledger will auto-create one with
some example rules, which you'll need to adjust.
At minimum, the rules file must identify the `date` and `amount` fields.
At minimum, the rules file must identify the date (`date`) and
amount (`amount`, or `amount-in` and `amount-out`, or one of `balance`/`balance1`/`balance2`)
fields.
It may also be necessary to specify the date format, and the number of header lines to skip. Eg:
```
fields date, _, _, amount