If you're referring to using an if-statement for the general idea of adding
to some account if the account balance is above a certain value, then I
think the value expression in the automatic transaction is essentially
that:
```
= expr "account =~ /Income:Hooli/ and total_before('Income:Hooli', O) <=
-200000 USD"
    (Liabilities:Taxes:Medicare:Additional)     .009
```

If you're referring to using an if-statement to combine the two cases I
mentioned in my last message, one where we're already above the limit and
one where we just crossed the limit, then I think this could be handled
with an if-statement (using the ternary `?:` operator, I'm not sure if
there are other conditionals I can use), but personally I think it looks a
lot messier:
```
= expr "account =~ /Income:Hooli/ and
(should_compute_additional_medicare('Income:Hooli', O) or
total_before('Income:Hooli', O) <= -200000 USD)"
    (Liabilities:Taxes:Medicare:Additional)
(should_compute_additional_medicare('Income:Hooli',
O)?(total_after('Income:Hooli') + 200000 USD) * {0.009}:O * {.009})
```


On Tue, Apr 19, 2022 at 5:08 AM o1bigtenor <[email protected]> wrote:

> On Mon, Apr 18, 2022 at 11:57 PM Gulshan Singh <[email protected]>
> wrote:
> >
> > Thanks! Even with the issue you mention, when my income exceeds
> > $200,000, the paycheck that puts it over that amount isn't going to
> > put it over by a large amount, so I think I could live with that
> > margin of error or correct it manually later. I can't find anything
> > related to the `account()` function in the docs, is it not documented
> > or am I just missing it?
> >
> > While this solution would probably be good enough, I've figured out an
> > improvement which allows me to calculate this exactly. The idea is
> > that if the account total before the transaction is greater than
> > -200,000, then we can apply the .9% tax as normal. But if the account
> > total before the transaction is greater than -200,000 and the total
> > after is less than -200,000, then we calculate the difference between
> > the total after and -200,000, and apply the .9% tax just to this
> > amount, and just for this one transaction.
> >
> > ```
> > account Assets:Bank
> > account Income:Hooli
> >
> > account Liabilities:Taxes:Medicare
> > account Liabilities:Taxes:Medicare:Additional
> >
> > define total_after(account_name) = account(account_name).total
> > define total_before(account_name, amount) = account(account_name).total
> - amount
> > define did_cross_threshold(low, high, threshold) = low < threshold and
> > high > threshold
> > define should_compute_additional_medicare(account_name, amount) =
> > did_cross_threshold(total_after(account_name),
> > total_before(account_name, amount), -200000)
> >
> > = expr "account =~ /Income:Hooli/ and
> > should_compute_additional_medicare('Income:Hooli', O)"
> >     (Liabilities:Taxes:Medicare:Additional)
> > ((total_after('Income:Hooli') + 200000 USD) * {0.009})
> >
> > = expr "account =~ /Income:Hooli/ and total_before('Income:Hooli', O)
> > <= -200000 USD"
> >     (Liabilities:Taxes:Medicare:Additional)     .009
> >
> > = expr "account =~ /Income:Hooli/"
> >     (Liabilities:Taxes:Medicare)                .0145
> >
> > 2022-04-29 * Payroll
> >     Income:Hooli                               -150,000 USD
> >     Assets:Bank
> >
> > 2022-04-15 * Payroll
> >     Income:Hooli                               -150,000 USD
> >     Assets:Bank
> >
> > 2022-04-01 * Payroll
> >     Income:Hooli                               -150,000 USD
> >     Assets:Bank
> > ```
> >
> > The result is:
> > ```
> >           -8,775 USD  Liabilities:Taxes:Medicare
> >           -2,250 USD    Additional
> > --------------------
> >           -8,775 USD
> > ```
> > Which shows that the additional tax was computed on the $250,000 that
> > was above $200,000, while the 1.45% tax was computed on the entire
> > $450,000 amount.
> >
> > Thanks for putting me on the right track!
> >
> > On Mon, Apr 18, 2022 at 4:36 PM Martin Michlmayr <[email protected]> wrote:
> > >
> > > * Gulshan Singh <[email protected]> [2022-04-17 00:06]:
> > > > I've tried something like this for the Medicare example, but it
> > > > doesn't work as abs(O) is dealing with the transaction amount, and
> > > > not the total balance of the account:
> > >
> > > There is a syntax to refer to the total of an account:
> > > account("...").total
> > >
> > > However, that's not going to work for your cause.  If you go from,
> > > let's say, $180k income to $240k, there's afaik no way to tell ledger
> > > to apply something exactly at $200k.
> > >
> > >
> > > account Assets:Bank
> > > account Income:Hooli
> > >
> > > account Liabilities:Taxes:Medicare
> > > account Liabilities:Taxes:Medicare:Additional
> > >
> > > = expr "account =~ /Income:Hooli/ and account('Income:Hooli').total <=
> -200000 USD"
> > >     (Liabilities:Taxes:Medicare:Additional)     .009
> > >
> > > = expr "account =~ /Income:Hooli/"
> > >     (Liabilities:Taxes:Medicare)                .0145
> > >
> > > 2022-04-15 * Payroll
> > >     Income:Hooli                               -150,000 USD
> > >     Assets:Bank
> > >
> > > 2022-04-01 * Payroll
> > >     Income:Hooli                               -150,000 USD
> > >     Assets:Bank
> > >
> > > 2022-04-01 * Payroll
> > >     Income:Hooli                               -150,000 USD
> > >     Assets:Bank
> > >
>
>
> I am NOT a programmer so perhaps I shouldn't be saying anything - - - but
> - - - IMHO wouldn't this be a good place to use the logic flow if --
> then -- else?
>
> (meaning - - - a calculation happens, if value is less than or equal to y
> then
> action a happens, if the value is larger than y - - -well then action b
> happens)
>
> (Hopefully not quite a stupid interjection!)
>
> Regards
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "Ledger" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/ledger-cli/VPXzQsto4KE/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ledger-cli/CAPpdf5-fXs%3DTBrE3dRydMAT8GjYRU188B3g4JpryWKLJtjRi-Q%40mail.gmail.com
> .
>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"Ledger" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ledger-cli/CANEZYrdF4TaDaNuZtKoSz_EFCHU2rGZ3W88a56mG153BpNDANA%40mail.gmail.com.

Reply via email to