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
>
> --
> Martin Michlmayr
> https://www.cyrius.com/
>
> --
>
> ---
> 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/Yl32BAzHYxErEudt%40jirafa.cyrius.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/CANEZYrcbFXyZ82D-k%2BeAiTqHqugq22Tv8aDW7GY9Tzh8CXxa%2Bg%40mail.gmail.com.

Reply via email to