;csv: doc: setting amounts: tweaks

This commit is contained in:
Simon Michael 2021-01-07 09:13:32 -08:00
parent 4952ac22a1
commit 6035cdf1e0

View File

@ -3922,47 +3922,72 @@ data. See:
### Setting amounts ### Setting amounts
Here's a review of the [amount-setting rules](#amount) discussed above. Some tips on using the [amount-setting rules](#amount) discussed above.
A posting's amount can be set in one of these ways:
Here are the ways to set a posting's amount:
1. **If the CSV has a single amount field:**\ 1. **If the CSV has a single amount field:**\
by assigning (via a [fields list](#fields) or a [field assignment](#field-assignment)) Assign (via a [fields list](#fields) or a [field assignment](#field-assignment)) to `amountN`.
to `amountN`, which sets posting N's amount. N is usually 1 or 2 but can go up to 99. This sets the Nth posting's amount. N is usually 1 or 2 but can go up to 99.
2. **If the CSV has separate Debit and Credit amount fields:**\ 2. **If the CSV has separate Debit and Credit amount fields:**\
by assigning to `amountN-in` and `amountN-out`, Assign to `amountN-in` and `amountN-out`.
which sets posting N's amount to whichever of these has a non-zero value, guessing the appropriate sign. This sets posting N's amount to whichever of these has a non-zero value,
guessing an appropriate sign.
- **If both fields contain a non-zero value:**\
you'll need to use some custom conditional rules, as hledger requires one of these fields to be empty or at least zero.
- **If hledger guesses the wrong sign:**\ - **If hledger guesses the wrong sign:**\
you can prepend a minus sign to flip it. You can prepend a minus sign to flip it. But, do this only for non-empty values (see below). Eg:
Due to the above restriction, this should be done conditionally, eg:
```rules ```rules
# If amount-out is non-empty, flip its sign: # flip amount-out's sign, if it is non-empty:
if %amount-out . if %amount-out .
amount-out -%amount-out amount-out -%amount-out
``` ```
3. **Using old numberless syntax:**\ - **If both fields contain a non-zero value:**\
by assigning to `amount` (or to `amount-in` and `amount-out`), which sets posting 1's and posting 2's amounts. The `amountN-in`/`amountN-out` rules require that each CSV record has a non-zero value in exactly one of the two fields,
It also converts posting 2's amount to cost (see the [amount](#amount) discussion above). so that hledger knows which to choose. So these would all be rejected:
```csv
"", ""
"0", "0"
"1", "1"
```
If you have CSV like this, use [conditional rules](#if-block) instead of `amountN-in`/`amountN-out`.
For example, if you want hledger to use the value containing non-zero digits:
```rules
fields date, description, in, out
if %in [1-9]
amount1 %in
if %out [1-9]
amount1 %out
```
3. **Using the old numberless syntax:**\
Assign to `amount` (or to `amount-in` and `amount-out`).
This sets posting 1's and posting 2's amounts (and converts posting 2's amount to cost).
This is supported for backwards compatibility (and occasional convenience). This is supported for backwards compatibility (and occasional convenience).
4. **If the CSV has the new balance instead of the posted amount:**\ 4. **If the CSV has the balance instead of the posted amount:**\
by assigning to `balanceN`, which sets posting N's amount indirectly via a Assign to `balanceN`, which sets posting N's amount indirectly via a
[balance assignment](hledger.html#balance-assignments). [balance assignment](hledger.html#balance-assignments).
(Old syntax: `balance`, equivalent to `balance1`.) (Old syntax: `balance`, equivalent to `balance1`.)
- **If hledger guesses the wrong default account name:**\ - **If hledger guesses the wrong default account name:**\
When setting the amount via balance assertion, hledger may guess the wrong default account name, When setting the amount via balance assertion, hledger may guess the wrong default account name.
so you should set the account name explicitly. So, set the account name explicitly. Eg:
```rules
fields date, description, balance1
account1 assets:checking
```
There is some automatic special handling for an amount's sign, for convenience: There is some automatic special handling for an amount's sign, for convenience:
- **If an amount value begins with a plus sign:**\ - **If an amount value begins with a plus sign:**\
it will be removed: `+AMT` becomes `AMT` that will be removed: `+AMT` becomes `AMT`
- **If an amount value is parenthesised:**\ - **If an amount value is parenthesised:**\
it will be de-parenthesised and sign-flipped: `(AMT)` becomes `-AMT` it will be de-parenthesised and sign-flipped: `(AMT)` becomes `-AMT`