~bfiedler/lutils

b5593c404b8c692843ffdb76f02f754588b810ad — Ben Fiedler 1 year, 6 months ago defd3a3
import: Allow setting account name
M doc/lutil-import.1.scd => doc/lutil-import.1.scd +4 -0
@@ 19,6 19,10 @@ them on stdout. _type_ determins the semantics of the *lutil import* csv file.
	Use _filename_ to determine whether transactions have already been booked.
	Booked transactions are skipped during import.

*-a* _account_, *--account* _account_
	Use _account_ as source account for transactions. All transactions are
	booked against _account_ and balanced with "TODO".

# EXAMPLES

Read a Kontoauszug and append ledger transactions to _ledger.leg_ using the

M importer/import.go => importer/import.go +18 -5
@@ 23,14 23,23 @@ import (
)

var ledgerFile *string
var accountName *string

var importers = map[string]types.Importer{
	"zkb":    &zkb.Importer{},
	"viseca": &viseca.Importer{},
}
var (
	importers = map[string]types.Importer{
		"zkb":    &zkb.Importer{},
		"viseca": &viseca.Importer{},
	}

	defaultAccountNames = map[string]string{
		"zkb":    "ZKB",
		"viseca": "ZKB Kreditkarte",
	}
)

func initFlags() {
	ledgerFile = flag.StringP("ledger", "l", "", "Existing ledger file (eliminate duplicate transactions)")
	accountName = flag.StringP("account", "a", "", "Source account for transactions")
}

func RunImport() {


@@ 50,6 59,10 @@ func RunImport() {
		fmt.Fprintf(os.Stderr, "unknown importer: %v\n", i)
	}

	if *accountName == "" {
		*accountName = defaultAccountNames[strings.ToLower(i)]
	}

	ts, err := importer.Parse(os.Stdin)
	if err != nil {
		log.Fatalf("error parsing input: %v", err)


@@ 74,7 87,7 @@ func RunImport() {
			continue
		}

		_, err := t.WriteTo(os.Stdout)
		_, err := t.WriteTo(os.Stdout, *accountName)
		if err != nil {
			log.Fatalf("could not write transaction: %v", err)
		}

M importer/types/types.go => importer/types/types.go +3 -3
@@ 21,10 21,10 @@ type Importer interface {
}

type Transaction interface {
	// Return the transaction's unique identifier. Identifiers may be
	// reused across different importers.
	// Return the transaction's unique identifier. Identifiers may be reused
	// across different importers.
	ID() string

	// Writes a transaction to w in a ledger-compatible format
	WriteTo(w io.Writer) (int64, error)
	WriteTo(w io.Writer, accountName string) (int64, error)
}

M importer/viseca/viseca.go => importer/viseca/viseca.go +2 -14
@@ 22,8 22,6 @@ const (
	urlPre  = "https://api.one.viseca.ch/v1/card/"
	urlPost = "/transactions?stateType=unknown&offset=0&pagesize=1000&dateFrom=2021-01-01"

	ZKBKreditkarte = "ZKB Kreditkarte"

	ID           = 0
	Datum        = 1
	MerchantName = 2


@@ 39,10 37,6 @@ type Transaction struct {
	amount       float64
}

type Transactions struct {
	Transactions []Transaction `json:"list"`
}

type Importer struct{}

func (i Importer) Name() string { return "Viseca" }


@@ 107,7 101,7 @@ func (t *Transaction) ID() string {
	return t.id
}

func (t *Transaction) WriteTo(w io.Writer) (n int64, err error) {
func (t *Transaction) WriteTo(w io.Writer, accountName string) (n int64, err error) {
	date := t.date.Format("2006/01/02")
	m, err := fmt.Fprintf(w, "%s *\n", date)
	n += int64(m)


@@ 121,13 115,7 @@ func (t *Transaction) WriteTo(w io.Writer) (n int64, err error) {
		return n, fmt.Errorf("error writing transaction: %v", err)
	}

	//m, err = fmt.Fprintf(w, "    ; details \"%s\"\n", t.details)
	//n += int64(m)
	//if err != nil {
	//	return n, fmt.Errorf("error writing transaction: %v", err)
	//}

	m, err = fmt.Fprintf(w, "    %-50s  %5.2f CHF\n", ZKBKreditkarte, -t.amount)
	m, err = fmt.Fprintf(w, "    %-50s  %5.2f CHF\n", accountName, -t.amount)
	n += int64(m)
	if err != nil {
		return n, fmt.Errorf("error writing transaction: %v", err)

M importer/zkb/zkb.go => importer/zkb/zkb.go +5 -7
@@ 24,8 24,6 @@ const (
	ZKBReferenz      = 2
	Belastung        = 4
	Gutschrift       = 5

	ZKBKonto string = "ZKB"
)

const formatString = `%s


@@ 43,9 41,9 @@ type Transaction struct {
	gutschrift   string // in CHF, format 1000.00
}

func (i *Importer) Name() string { return "ZKB" }
func (i Importer) Name() string { return "ZKB" }

func (i *Importer) Parse(inp io.Reader) ([]types.Transaction, error) {
func (i Importer) Parse(inp io.Reader) ([]types.Transaction, error) {
	in := csv.NewReader(inp)
	in.Comma = ';'
	in.LazyQuotes = true


@@ 95,7 93,7 @@ func (t *Transaction) ID() string {
	return t.zkbReferenz
}

func (t *Transaction) WriteTo(w io.Writer) (n int64, err error) {
func (t *Transaction) WriteTo(w io.Writer, accountName string) (n int64, err error) {
	date := t.date.Format("2006/01/02")
	m, err := fmt.Fprintf(w, "%s *\n", date)
	n += int64(m)


@@ 115,7 113,7 @@ func (t *Transaction) WriteTo(w io.Writer) (n int64, err error) {
	}

	if t.gutschrift != "" {
		m, err = fmt.Fprintf(w, "    %-50s  %8s CHF\n", ZKBKonto, t.gutschrift)
		m, err = fmt.Fprintf(w, "    %-50s  %8s CHF\n", accountName, t.gutschrift)
		n += int64(m)
		if err != nil {
			return n, fmt.Errorf("error writing transaction: %v", err)


@@ 129,7 127,7 @@ func (t *Transaction) WriteTo(w io.Writer) (n int64, err error) {
	}

	if t.belastung != "" {
		m, err = fmt.Fprintf(w, "    %-50s  %8s CHF\n", ZKBKonto, "-"+t.belastung)
		m, err = fmt.Fprintf(w, "    %-50s  %8s CHF\n", accountName, "-"+t.belastung)
		n += int64(m)
		if err != nil {
			return n, fmt.Errorf("error writing transaction: %v", err)