#!/usr/bin/perl

use Text::Iconv;
use Digest::MD5 qw(md5_hex);
use Time::Local;
use DBI;
use MIME::Parser;

$red="[31m";
$green="[32m";
$yellow="[33m";
$blue="[34m";
$normal="[00m";

$wdir='/tmp/wyciagi';

$converter=Text::Iconv->new("UTF-8","ISO-8859-2");
$dsn = "DBI:mysql:database=lms;host=jakis.host";
$baza = DBI->connect($dsn,'user','haslo');
my $parser = new MIME::Parser;
mkdir $wdir;
$parser->output_under($wdir);
$entity = $parser->parse(\*STDIN);
#$entity->dump_skeleton;

opendir($dir,$wdir);
while ($ndir=readdir($dir)) {
	if ($ndir!~/^\./ and -d "$wdir/$ndir") 
	{
		opendir($dir1,"$wdir/$ndir");
		while ($ndir1=readdir($dir1)) {	
			if ($ndir1=~/^wyciag.*\.pdf$/i) {
				$plik="$wdir/$ndir/$ndir1";
				print "Ladujemy wyciagi z $plik\n";
				last;
			}
		}
		last if (defined($plik));
		print "Zla nazwa pliku!\n";
		exit(0);
	}
}
closedir $dir;

open (F, "/usr/bin/pdftohtml -enc UTF-8 -q -i -noframes -stdout \"$plik\"|");
$linia='';
$i=0;
while (<F>) {
	$_=$converter->convert($_);
	#print "$i: $_\n";
	if ((/^[0-9]{2}.[0-9]{2}.[0-9]{4}<br>/i) or /Saldo końcowe/i) {
		if ($i<5) { $linia.=$_; $i++; next; }
		if ($linia!~/^$/) {
			$linia=~s/\n//g;
			$linia=~s/<hr>.*$//i;
			$linia=~s/'/ /g;
			#$linia=~s/([0-9]) ([0-9])/$1$2/;
			if ($linia=~/Data wyciagu/) {
				$linia='';
			} elsif
			($linia=~/^([0-9]{2}.[0-9]{2}.[0-9]{4})<br>[^<]*<br>([0-9,]+ ?[0-9,]+)<br>.*id=([0-9]+) cl=2011 ([^<]+)<br>([^<]*).*/i) {
				#print $blue.$linia.$normal."\n";
				$time=$1;
				$value=$2;
				$customerid=$3;
				$customer=$5;
				$comment=$4;
				$closed=1;
				$value=~s/\.//;
				$value=~s/ //;
				$value=~s/,/./;
				if ($time=~/^([0-9]{2}).([0-9]{2}).([0-9]{4})/) {
					$time=timelocal(0,0,0,$1,$2-1,$3);
				}
				$hash = md5_hex($time.$value.$customer.$comment);
				#print "##############################\n";
				#print "data: $time\n";
				#print "kwota: $value\n";
				#print "klient: $customer\n";
				#print "klientid: $customerid\n";
				#print "tytul: $comment\n";
				#print "hash: $hash\n";
				$query=$baza->prepare("SELECT * FROM cashimport WHERE hash='$hash'");
				$query->execute;
				if (!$query->rows) {
					if ($customerid>0) {
						$baza->do("INSERT INTO cash (time,type,value,customerid,comment) VALUES ($time+57600,3,$value,$customerid,'Wpłata na konto (PKO BP)')\n");
						print $green."INSERT INTO cash (time,type,value,customerid,comment) VALUES ($time+57600,3,$value,$customerid,'Wpłata na konto (PKO BP)');".$normal."\n";
					}
					#print "$customerid\n";
					$baza->do("INSERT INTO cashimport (date, value, customer, customerid, description, hash, closed,sourceid) VALUES ($time,$value,'$customer',$customerid,'$comment','$hash', $closed, 2)");
					print $yellow."INSERT INTO cashimport (date, value, customer, customerid, description, hash, closed) VALUES ($time,$value,'$customer',$customerid,'$comment','$hash', $closed);".$normal."\n";
				} else {
					print $red."Operacja (".$customer.",".$value.") już zaimportowana!".$normal."\n";
				}
			} else {
				print $red."BLAD: ".$linia.$normal."\n";
			}
		}
		$linia=$_;
		$i=1;
	} elsif ($linia!~/^$/) {
		#print "#";
		$linia.=$_;
		$i++;
	}
}

close F;

$parser->filer->purge;
rmdir "$wdir/$ndir";

