Customize account2 of CSV rules depending on the file parsed

Hello,

I have a expenses.csv.rules file which is included by the rules files of different bank accounts: personal-bank.csv.rules and family-bank.csv.rules. Bank rule files parse the CSV fields and set the account1 whereas the common expenses csv file sets the description and account2 (the category of expense). The description and category of the expenses are the same for my personal and familiar books.

One example of one entry of my expenses.csv.rules:

if carrefour
    description carrefour | some explanation
    account2 expenses:supermarket

What I would like to get is to set account2 toexpenses:family:supermarket if the expenses.csv.rules file was included by the family-bank.csv.rules file and to set expenses:personal:supermarket if the expenses.csv.ruleswas included by personal-bank.csv.rules.

Is that possible?

The rules can't detect which files are including them; but you can just add overriding rules in each including file, after the include.

Do you mean overriding rules after the include such as:

include categorization.rules
if %account2 expenses:(.*)
    account2 expenses:family:\1

I tried this, but it does nothing. I think the issue is that field matchers should only be CVS field names or numbers and account2 is not a CSV field name.

Yes that's what I meant, and yes you're right. hledger fields can only be written to, not read.
CSV fields and hledger fields

1 Like

Can we have a rule for a specific day ?

Say for example that if I spent money at a place, I’d like it to be expense:place but if it was on the 1st of january, I’d like it to be expense:party.

Possible ?

Sure. Assuming ISO date format,

if %date -01-01
 ...

if ! %date -01-01
 ...

and can have multiple if-conditions ?

Yes, you can see the syntax here:
https://hledger.org/hledger.html#if-block

1 Like

Wow perfect !