def main():
    s = common.Settings.load_or_defaults(os.path.expanduser("~/.ledgerhelpers.ini"))
    journal = common.Journal.from_file(common.find_ledger_file(), None)
    accts, commodities = journal.accounts_and_last_commodities()


    when = common.prompt_for_date(
        sys.stdin, sys.stdout,
        "When?",
        s.get("last_date", datetime.date.today())
    )
    if when == datetime.date.today():
        del s["last_date"]
    else:
        s["last_date"] = when

    asset1 = common.prompt_for_account(
        sys.stdin, sys.stdout,
        accts, "From where?",
        s.get("last_withdrawal_account", None)
    )
    assert asset1, "Not an account: %s" % asset1
    s["last_withdrawal_account"] = asset1
    asset1_currency = commodities.get(asset1, ledger.Amount("$ 1"))

    asset2 = common.prompt_for_account(
        sys.stdin, sys.stdout,
        accts, "To where?",
        s.get("last_deposit_account", None)
    )
    assert asset2, "Not an account: %s" % asset2
    s["last_deposit_account"] = asset2
    asset2_currency = commodities.get(asset2, ledger.Amount("$ 1"))

    amount1 = common.prompt_for_amount(
        sys.stdin, sys.stdout,
        "How much?", asset1_currency
    )

    amount2 = common.prompt_for_amount(
        sys.stdin, sys.stdout,
        "What was deposited?", asset2_currency
    )

    lines = journal.generate_record("Withdrawal", when, None, [
        (asset1, [-1 * amount1]),
        (asset2, [amount2]),
    ])
    print "========== Record =========="
    print "\n".join(lines)
    save = common.yesno(
        sys.stdin, sys.stderr,
        "Hit ENTER or y to save it to the file, BACKSPACE or n to skip saving: "
    )
    if save:
        journal.add_lines_to_file(lines)
def main():
    s = common.Settings.load_or_defaults(
        os.path.expanduser("~/.ledgerhelpers.ini"))
    j = journal.Journal.from_file(common.find_ledger_file(), None)
    accts, commodities = j.accounts_and_last_commodity_for_account()

    when = common.prompt_for_date(sys.stdin, sys.stdout, "When?",
                                  s.get("last_date", datetime.date.today()))
    if when == datetime.date.today():
        del s["last_date"]
    else:
        s["last_date"] = when

    asset1 = common.prompt_for_account(sys.stdin, sys.stdout, accts,
                                       "From where?",
                                       s.get("last_withdrawal_account", None))
    assert asset1, "Not an account: %s" % asset1
    s["last_withdrawal_account"] = asset1
    asset1_currency = commodities.get(asset1, ledger.Amount("$ 1"))

    asset2 = common.prompt_for_account(sys.stdin, sys.stdout, accts,
                                       "To where?",
                                       s.get("last_deposit_account", None))
    assert asset2, "Not an account: %s" % asset2
    s["last_deposit_account"] = asset2
    asset2_currency = commodities.get(asset2, ledger.Amount("$ 1"))

    amount1 = common.prompt_for_amount(sys.stdin, sys.stdout, "How much?",
                                       asset1_currency)

    amount2 = common.prompt_for_amount(sys.stdin, sys.stdout,
                                       "What was deposited?", asset2_currency)

    lines = j.generate_record("Withdrawal", when, None, "", [
        (asset1, -1 * amount1),
        (asset2, amount2),
    ])
    print "========== Record =========="
    print "\n".join(lines)
    save = common.yesno(
        sys.stdin, sys.stderr,
        "Hit ENTER or y to save it to the file, BACKSPACE or n to skip saving: "
    )
    if save:
        j.add_text_to_file(lines)
def main():
    journal, s = common.load_journal_and_settings_for_gui()
    accts, commodities = journal.accounts_and_last_commodities()

    saleacct = common.prompt_for_account(
        sys.stdin, sys.stdout,
        accts, "Which account was the sold commodity stored in?",
        s.get("last_sellstock_account", None)
    )
    assert saleacct, "Not an account: %s" % saleacct
    s["last_sellstock_account"] = saleacct

    commissionsaccount = common.prompt_for_account(
        sys.stdin, sys.stdout,
        accts, "Which account to account for commissions?",
        s.get("last_commissions_account", None)
    )
    assert commissionsaccount, "Not an account: %s" % commissionsaccount
    s["last_commissions_account"] = commissionsaccount

    gainslossesacct = common.prompt_for_account(
        sys.stdin, sys.stdout,
        accts, "Which account to credit gains and losses?",
        s.get("last_gainslosses_account",
              "Capital:Recognized gains and losses")
    )
    assert gainslossesacct, "Not an account: %s" % gainslossesacct
    s["last_gainslosses_account"] = gainslossesacct

    target_amount = common.prompt_for_amount(
        sys.stdin, sys.stdout,
        "How many units of what commodity?", ledger.Amount("$ 1")
    )
    target_sale_price = common.prompt_for_amount(
        sys.stdin, sys.stdout,
        "What is the sale price of the commodity?", ledger.Amount("$ 1")
    )
    commission = common.prompt_for_amount(
        sys.stdin, sys.stdout,
        "What was the commission of the trade?", ledger.Amount("$ 1")
    )

    all_lots = Lots()
    lots_text = subprocess.check_output([
        'ledger', 'bal',
        '--lots', '--lot-dates', '--lot-prices',
        '--date-format=%Y-%m-%d', '--sort=date',
        '--balance-format=++ %(account)\n%(amount)\n',
        saleacct
    ])
    all_lots.parse_ledger_bal(lots_text)

    print "=========== Read ==========="
    for l in all_lots:
        print l

    lots_produced = all_lots.subtract(target_amount)

    print "========= Computed ========="
    for l in lots_produced:
        print l

    print "=========== Left ==========="
    for l in all_lots:
        print l

    lines = []
    tpl = "%s {%s}%s @ %s"
    datetpl = ' [%s]'
    for l in lots_produced:
        m = -1 * l.amount
        if m.commodity.details.date:
            datetext = datetpl % m.commodity.details.date.strftime("%Y-%m-%d")
        else:
            datetext = ''
        lines.append((
            l.account,
            [tpl % (m.strip_annotations(),
                    m.commodity.details.price,
                    datetext,
                    target_sale_price)]
        ))
        diff = (l.price - target_sale_price) * l.amount
        lines.append((gainslossesacct, [diff]))
    totalsale = target_sale_price * sum(
        l.amount.number() for l in lots_produced
    )
    lines.append((saleacct, [totalsale - commission]))
    lines.append((commissionsaccount, [commission]))

    lines = journal.generate_record(
        "Sale of %s" % (target_amount),
        datetime.date.today(), None,
        lines,
    )
    print "========== Record =========="
    print "\n".join(lines)
    save = common.yesno(
        sys.stdin, sys.stderr,
        "Hit ENTER or y to save it to the file, BACKSPACE or n to skip saving: "
    )
    if save:
        journal.add_text_to_file(lines)
def main():
    journal, s = common.load_journal_and_settings_for_gui()
    accts, commodities = journal.accounts_and_last_commodities()

    saleacct = common.prompt_for_account(
        sys.stdin, sys.stdout,
        accts, "Which account was the sold commodity stored in?",
        s.get("last_sellstock_account", None)
    )
    assert saleacct, "Not an account: %s" % saleacct
    s["last_sellstock_account"] = saleacct

    commissionsaccount = common.prompt_for_account(
        sys.stdin, sys.stdout,
        accts, "Which account to account for commissions?",
        s.get("last_commissions_account", None)
    )
    assert commissionsaccount, "Not an account: %s" % commissionsaccount
    s["last_commissions_account"] = commissionsaccount

    gainslossesacct = common.prompt_for_account(
        sys.stdin, sys.stdout,
        accts, "Which account to credit gains and losses?",
        s.get("last_gainslosses_account",
              "Capital:Recognized gains and losses")
    )
    assert gainslossesacct, "Not an account: %s" % gainslossesacct
    s["last_gainslosses_account"] = gainslossesacct

    ignoreaccts = ["Funding:Stock vesting"]

    target_amount = common.prompt_for_amount(
        sys.stdin, sys.stdout,
        "How many units of what commodity?", ledger.Amount("$ 1")
    )
    target_sale_price = common.prompt_for_amount(
        sys.stdin, sys.stdout,
        "What is the sale price of the commodity?", ledger.Amount("$ 1")
    )
    commission = common.prompt_for_amount(
        sys.stdin, sys.stdout,
        "What was the commission of the trade?", ledger.Amount("$ 1")
    )

    all_lots = Lots()
    last_xact = None
    for post in journal.query(""):
        if post.xact != last_xact:
            for post in post.xact.posts():
                if str(post.account) in ignoreaccts:
                    continue
                if str(post.amount.commodity) == "$":
                    continue
                if str(post.amount.commodity) != str(target_amount.commodity):
                    continue
                trueamount = ledger.Amount(post.amount.strip_annotations())
                lotprice = post.amount.price() / trueamount
                account = post.account
                date = post.date
                all_lots.register(date, trueamount, lotprice, account)
            all_lots.commit()
            last_xact = post.xact

    print "=========== Read ==========="
    for l in all_lots:
        print l

    lots_produced = all_lots.subtract(target_amount)

    print "========= Computed ========="
    for l in lots_produced:
        print l

    print "=========== Left ==========="
    for l in all_lots:
        print l

    lines = []
    for l in lots_produced:
        lines.append((
            l.accounts_seen[-1],
            ["%s {%s} @ %s" % (-1 * l.amount, l.price, target_sale_price)]
        ))
        diff = (l.price - target_sale_price) * l.amount
        lines.append((gainslossesacct, [diff]))
    totalsale = target_sale_price * sum(
        l.amount.number() for l in lots_produced
    )
    lines.append((saleacct, [totalsale - commission]))
    lines.append((commissionsaccount, [commission]))

    lines = journal.generate_record(
        "Sale of %s" % (target_amount),
        datetime.date.today(), None,
        lines,
    )
    print "========== Record =========="
    print "\n".join(lines)
    save = common.yesno(
        sys.stdin, sys.stderr,
        "Hit ENTER or y to save it to the file, BACKSPACE or n to skip saving: "
    )
    if save:
        journal.add_lines_to_file(lines)
Exemple #5
0
def main():
    journal, s = gui.load_journal_and_settings_for_gui()
    accts, commodities = journal.accounts_and_last_commodity_for_account()

    saleacct = common.prompt_for_account(
        sys.stdin, sys.stdout,
        accts, "Which account was the sold commodity stored in?",
        s.get("last_sellstock_account", None)
    )
    assert saleacct, "Not an account: %s" % saleacct
    s["last_sellstock_account"] = saleacct

    commissionsaccount = common.prompt_for_account(
        sys.stdin, sys.stdout,
        accts, "Which account to account for commissions?",
        s.get("last_commissions_account", None)
    )
    assert commissionsaccount, "Not an account: %s" % commissionsaccount
    s["last_commissions_account"] = commissionsaccount

    gainslossesacct = common.prompt_for_account(
        sys.stdin, sys.stdout,
        accts, "Which account to credit gains and losses?",
        s.get("last_gainslosses_account",
              "Capital:Recognized gains and losses")
    )
    assert gainslossesacct, "Not an account: %s" % gainslossesacct
    s["last_gainslosses_account"] = gainslossesacct

    target_amount = common.prompt_for_amount(
        sys.stdin, sys.stdout,
        "How many units of what commodity?", ledger.Amount("$ 1")
    )
    target_sale_price = common.prompt_for_amount(
        sys.stdin, sys.stdout,
        "What is the sale price of the commodity?", ledger.Amount("$ 1")
    )
    commission = common.prompt_for_amount(
        sys.stdin, sys.stdout,
        "What was the commission of the trade?", ledger.Amount("$ 1")
    )

    all_lots = Lots()
    lots_text = subprocess.check_output([
        'ledger', 'bal',
        '--lots', '--lot-dates', '--lot-prices',
        '--date-format=%Y-%m-%d', '--sort=date',
        '--balance-format=++ %(account)\n%(amount)\n',
        saleacct
    ])
    all_lots.parse_ledger_bal(lots_text)

    print "=========== Read ==========="
    for l in all_lots:
        print l

    lots_produced = all_lots.subtract(target_amount)

    print "========= Computed ========="
    for l in lots_produced:
        print l

    print "=========== Left ==========="
    for l in all_lots:
        print l

    lines = []
    tpl = "%s {%s}%s @ %s"
    datetpl = ' [%s]'
    for l in lots_produced:
        m = -1 * l.amount
        if m.commodity.details.date:
            datetext = datetpl % m.commodity.details.date.strftime("%Y-%m-%d")
        else:
            datetext = ''
        lines.append((
            l.account,
            tpl % (m.strip_annotations(),
                   m.commodity.details.price,
                   datetext,
                   target_sale_price)
        ))
        diff = (l.price - target_sale_price) * l.amount
        lines.append((gainslossesacct, diff))
    totalsale = target_sale_price * sum(
        l.amount.number() for l in lots_produced
    )
    lines.append((saleacct, totalsale - commission))
    lines.append((commissionsaccount, commission))

    lines = journal.generate_record(
        "Sale of %s" % (target_amount),
        datetime.date.today(), None, "",
        lines,
    )
    print "========== Record =========="
    print "\n".join(lines)
    save = common.yesno(
        sys.stdin, sys.stderr,
        "Hit ENTER or y to save it to the file, BACKSPACE or n to skip saving: "
    )
    if save:
        journal.add_text_to_file(lines)