Exemple #1
0
def main():
    journal, s = common.load_journal_and_settings_for_gui(read_journal=False)
    klass = AddTransApp
    win = klass(journal, s)
    win.connect("delete-event", Gtk.main_quit)
    GObject.idle_add(win.show_all)
    Gtk.main()
def main(argv):
    p = get_argparser()
    args = p.parse_args(argv[1:])
    journal, settings = ledgerhelpers.load_journal_and_settings_for_gui(
        price_file_mandatory=True
    )
    klass = UpdatePricesApp if not args.batch else UpdatePricesCommon
    app = klass(journal, settings)
    return app.run()
Exemple #3
0
def main():
    journal, s = common.load_journal_and_settings_for_gui()
    args = sys.argv[1:]
    klass = BuyApp
    if args and args[0] == "-n":
        klass = InnocuousBuyApp
        args = args[1:]
    win = klass(journal, s, " ".join(args) if args else None)
    win.connect("delete-event", Gtk.main_quit)
    GObject.idle_add(win.show_all)
    Gtk.main()
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)