From: Steinar H. Gunderson Date: Tue, 22 Jan 2013 16:17:25 +0000 (+0100) Subject: Initial checkin for move to Git (no prior version history available). X-Git-Url: https://git.sesse.net/?p=autoeconomy;a=commitdiff_plain Initial checkin for move to Git (no prior version history available). --- c761f97512d262bcd0f8fb4686d0df1681b412b6 diff --git a/autoeconomy.pm b/autoeconomy.pm new file mode 100644 index 0000000..7bacf72 --- /dev/null +++ b/autoeconomy.pm @@ -0,0 +1,17 @@ +#! /usr/bin/perl +use DBI; +require './config.pm'; +package autoeconomy; + +sub db_connect { + my $dbh = DBI->connect("dbi:Pg:" . + "dbname=" . $autoeconomy::config::dbname . ";" . + "host=" . $autoeconomy::config::dbhost, + $autoeconomy::config::dbuser, + $autoeconomy::config::dbpass) + or die "Couldn't connect to database: " . DBI::errstr(); + $dbh->{RaiseError} = 1; + return $dbh; +} + +1; diff --git a/autoeconomy.sql b/autoeconomy.sql new file mode 100644 index 0000000..0b2259a --- /dev/null +++ b/autoeconomy.sql @@ -0,0 +1,13 @@ +CREATE TABLE transaction ( + account varchar not null, + accounting_date date not null, + interest_date date not null, + use_date date not null, + reference varchar primary key not null, + trans_type varchar not null, + text varchar not null, + amount_out numeric(9,2), + amount_in numeric(9,2), + + CHECK ((amount_in IS NULL) <> (amount_out IS NULL)) +); diff --git a/classify.crm b/classify.crm new file mode 100644 index 0000000..c0117e8 --- /dev/null +++ b/classify.crm @@ -0,0 +1,15 @@ +#! /usr/bin/crm +# (account, usedate, weekday, type, text, direction, amount, cssfiles) + +# prefix each text token with text_ +{ + match [:text:] (:place:) /[[:graph:]]+/ + alter (:place:) /text_:*:place:/ + liaf +} + +isolate (:m_text:) /account_:*:account: usedate_:*:usedate: weekday_:*:weekday: type_:*:type: :*:text: direction_:*:direction: amount_:*:amount:/ +isolate (:classify_status:) // +classify [:m_text:] (:*:cssfiles:) (:classify_status:) +output /:*:classify_status:/ +exit diff --git a/config.pm b/config.pm new file mode 100644 index 0000000..2272e11 --- /dev/null +++ b/config.pm @@ -0,0 +1,10 @@ +#! /usr/bin/perl +package autoeconomy::config; +our $dbname = 'autoeconomy'; +our $dbhost = 'pannekake.samfundet.no'; +our $dbuser = 'autoeconomy'; +our $dbpass = ''; + +require 'config.local.pm'; + +1; diff --git a/create-css.sh b/create-css.sh new file mode 100755 index 0000000..e37b1df --- /dev/null +++ b/create-css.sh @@ -0,0 +1,4 @@ +#! /bin/sh +for CSS in mat husleie investeringer renter studielaan diverse intern; do + cssutil -b -r $CSS.css +done diff --git a/db-classify.pl b/db-classify.pl new file mode 100644 index 0000000..fe7d37d --- /dev/null +++ b/db-classify.pl @@ -0,0 +1,36 @@ +#! /usr/bin/perl +use strict; +use warnings; +require './autoeconomy.pm'; + +my $dbh = autoeconomy::db_connect(); +my $reference = $ARGV[0]; +my $cssfile = $ARGV[1]; + +my $ref = $dbh->selectrow_hashref('SELECT * FROM transaction WHERE reference=?', undef, + $reference); +if (!defined($ref)) { + die "Couldn't find row '$reference'"; +} + +my @parms = ( + "--account=" . $ref->{'account'}, + "--usedate=" . $ref->{'use_date'}, + "--reference=" . $ref->{'reference'}, + "--type=" . $ref->{'trans_type'}, + "--text=" . $ref->{'text'}, + "--direction=" . (defined($ref->{'amount_in'}) ? 'in' : 'out'), + "--amount=" . ($ref->{'amount_in'} || $ref->{'amount_out'}) +); + +if (defined($cssfile)) { + system("crm", "learn.crm", + @parms, + "--cssfile=" . $cssfile + ); +} else { + system("crm", "classify.crm", + @parms, + "--cssfiles=" . join(' ', <*.css>) + ); +} diff --git a/input.pl b/input.pl new file mode 100644 index 0000000..e8eb0c2 --- /dev/null +++ b/input.pl @@ -0,0 +1,39 @@ +#! /usr/bin/perl +use Text::CSV_XS; +use utf8; +use Encode; +require './autoeconomy.pm'; + +binmode STDIN, ':utf8'; +binmode STDOUT, ':utf8'; + +my $csv = Text::CSV_XS->new({ sep_char => "\t", binary => 1 }); +my $dbh = autoeconomy::db_connect(); +my $num = 0; + +my $account = $ARGV[0]; + +while () { + $csv->parse($_); + my ($accounting_date,$interest_date,$use_date,$reference,$type,$text,$out,$in) = $csv->fields; + next if ($interest_date eq 'RENTEDATO'); + + # Find out if it's there already (FIXME: might need a text+type update?) + my ($count) = $dbh->selectrow_array('SELECT COUNT(*) AS cnt FROM transaction WHERE reference=?', undef, $reference); + next if ($count > 0); + + $type = Encode::decode("iso8859-1", $type); + $text = Encode::decode("iso8859-1", $text); + $out =~ s/,/./; + $in =~ s/,/./; + $out = undef if ($out eq ''); + $in = undef if ($in eq ''); + print "$text\n"; + + my $q = $dbh->prepare('INSERT INTO transaction (account,accounting_date,interest_date,use_date,reference,trans_type,text,amount_out,amount_in) VALUES (?,?,?,?,?,?,?,?,?)'); + $q->execute($account,$accounting_date,$interest_date,$use_date,$reference,$type,$text,$out,$in); + + ++$num; +} + +print "$num records inserted.\n"; diff --git a/learn.crm b/learn.crm new file mode 100644 index 0000000..1e7ec7f --- /dev/null +++ b/learn.crm @@ -0,0 +1,13 @@ +#! /usr/bin/crm +# (account, usedate, weekday, type, text, direction, amount, cssfile) + +# prefix each text token with text_ +{ + match [:text:] (:place:) /[[:graph:]]+/ + alter (:place:) /text_:*:place:/ + liaf +} + +isolate (:m_text:) /account_:*:account: usedate_:*:usedate: weekday_:*:weekday: type_:*:type: :*:text: direction_:*:direction: amount_:*:amount:/ +learn [:m_text:] (:*:cssfile:) +exit