]> git.sesse.net Git - autoeconomy/blob - input.pl
Initial checkin for move to Git (no prior version history available).
[autoeconomy] / input.pl
1 #! /usr/bin/perl
2 use Text::CSV_XS;
3 use utf8;
4 use Encode;
5 require './autoeconomy.pm';
6
7 binmode STDIN, ':utf8';
8 binmode STDOUT, ':utf8';
9
10 my $csv = Text::CSV_XS->new({ sep_char => "\t", binary => 1 });
11 my $dbh = autoeconomy::db_connect();
12 my $num = 0;
13
14 my $account = $ARGV[0];
15
16 while (<STDIN>) {
17         $csv->parse($_);
18         my ($accounting_date,$interest_date,$use_date,$reference,$type,$text,$out,$in) = $csv->fields;
19         next if ($interest_date eq 'RENTEDATO');
20
21         # Find out if it's there already (FIXME: might need a text+type update?)
22         my ($count) = $dbh->selectrow_array('SELECT COUNT(*) AS cnt FROM transaction WHERE reference=?', undef, $reference);
23         next if ($count > 0);
24
25         $type = Encode::decode("iso8859-1", $type);
26         $text = Encode::decode("iso8859-1", $text);
27         $out =~ s/,/./;
28         $in =~ s/,/./;
29         $out = undef if ($out eq '');
30         $in = undef if ($in eq '');
31         print "$text\n";
32
33         my $q = $dbh->prepare('INSERT INTO transaction (account,accounting_date,interest_date,use_date,reference,trans_type,text,amount_out,amount_in) VALUES (?,?,?,?,?,?,?,?,?)');
34         $q->execute($account,$accounting_date,$interest_date,$use_date,$reference,$type,$text,$out,$in);
35
36         ++$num;
37 }
38
39 print "$num records inserted.\n";