#!/bin/perl # Copyright (c) 2008 by Emin Gabrielyan, Switzernet $"=" "; $_="@ARGV"; if(!/^\s*xml=([^\s]+)(?:\s+save=(\d{6})-(\d{6})\s+htm=([^\s]+)(?:\s+watch=([^\s]+)\s+exclude=(yes|no))?)?\s*$/) { print STDERR "wrong arguments: "; print STDERR; print STDERR "\narguments: xml= [save=yymmdd-YYMMDD htm= [watch=RE exclude=[yes|no]]]\n"; print STDERR "Example: xml=in.xml save=080130-080131 htm=out.htm watch=\"(telekurs\\s+multipay|ordre\\s+debit\\s+d irect|online-shopping\\s+deferred)\" exclude=no\n"; exit 1; } $xmlfname=$1; $save_start=$2; $save_end=$3; $htmfname=$4; $watch=$5; $exclude=$6; if(defined($save_start) && defined($save_end)) { $save=1; if($save_start gt $save_end) { print STDERR "start date $save_start is greater than end date $save_end\n"; exit 1; } } $empty="-"; if(!open fh,$xmlfname) { print STDERR "cannot open file $xmlfname\n"; exit 1; } @lines=; close fh; $"=""; $_="@lines"; s/[\r\n]//sg; @rows=split//; $s="\\s*"; $tag="<[^>]*>[^<]*]*>"; $sign="]*>([+-])"; $lin="$s]*Value=\"(\\w+)\">$s$s"; $moa="$s$s$s([\\d\\.]+)$s$s$sign$s"; $dtm="$s$s$tag$s(\\d+)$s$s"; $ftx="$s$tag$s$tag$s$s((?:[^<]*$s)+)$s"; $rff="$s$s$s(\\d+)$s$s"; # $sg5="$s$moa$s$dtm$s"; # $sg6="$s(?:$rff$s)*(?:$dtm$s)?$moa$s(?:$ftx$s)?"; # $sg4="$s$lin$s(?:$ftx$s)?(?:$sg5$s)?(?:$sg6$s)*"; $verbose=0; $verbose*=$save; if($verbose) { printf STDERR "%8s %23s %3s %8s %25s\n","Date","Red slip scan","+/-","CHF","Text"; printf STDERR "%8s %23s %3s %8s %25s\n","----","-------------","---","---","----"; } if($save) { if(!open htm,">".$htmfname) { print STDERR "cannot open file $htmfname\n"; exit 1; } print htm "\r\n"; print htm "\r\n"; print htm "\r\n"; print htm "\r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm "\r\n"; } foreach $_ (@rows) { if(/$sg4/) { $LIN=$1; # $FTX=$2; # $SG5_MOA=$3; # $balance=$4; # $balance_sign=$5; # $date=$6; # $FTX=~s//[/g; $FTX=~s/<\/D_4440>$s/] /g; $balance=$balance_sign.$balance; if(defined($date) && defined($prev_date)) { if($date ne $prev_date) { $end_date=$prev_date; if(!defined($start_date)) { $start_date=$date; } } } $prev_date=$date; if($LIN eq "LST" && $SG5_MOA == 315) { if(!defined($opening_balance)) { $opening_balance=$balance; $opening_date=$date; } } elsif($LIN eq "LEN" && $SG5_MOA == 343) { $closing_balance=$balance; $closing_date=$date; } else { @subrows=split//; foreach $_ (@subrows) { if(/$sg6/) { $D_1153=$1; # $D_1154=$2; # $value=$3; # $SG6_MOA=$4; # $amount=$5; # $amount_sign=$6; # $SG6_FTX=$7; # $scan=$D_1153 eq "ZZZ"?$D_1154:"wire"; $SG6_FTX=~s/<\/D_4440>$s/) (/g; $SG6_FTX=~s//(/g; $SG6_FTX=~s/<\/D_4440>$s/)/g; $info=substr($SG6_FTX,0,25); $information=$FTX.$SG6_FTX; $amount=$amount_sign.$amount; if(($LIN eq "LNE" || $LIN eq "LNS") && $SG5_MOA == 15) { if($verbose) { printf STDERR "%8s %23s %3s %8s %25s..\n",$date,$scan,$SG6_MOA==210?"cre":"DEB",$amount,$info; } if($save && substr($date,2,6) ge $save_start && substr($date,2,6) le $save_end){ if($exclude ne "yes" || ($exclude eq "yes" && $SG6_MOA==210 && $information!~/$watch/i)) { printf htm "\r\n"; $print_date=$date=~/^(\d{4})(\d{2})(\d{2})$/?"$1-$2-$3":$date; printf htm " \r\n",$print_date; printf htm " \r\n"; printf htm " \r\n"; $print_amount=$amount=~/^\+([\d.]+)$/?$1:$amount; printf htm " \r\n"; printf htm " \r\n","no"; printf htm " \r\n",$empty; printf htm " \r\n",$empty; printf htm " \r\n",$empty; printf htm " \r\n",$empty; printf htm "\r\n"; if($SG6_MOA==210){$save_credits+=$amount;} elsif($SG6_MOA==211){$save_debits+=$amount;} $save_transactions++; } } if($SG6_MOA==210) { $sum_credits+=$amount; } elsif($SG6_MOA==211) { $sum_debits+=$amount; } $cnt++; } elsif($LIN eq "LTI") { if($SG6_MOA==210) { $bank_credits+=$amount; } elsif($SG6_MOA==211) { $bank_debits+=$amount; } } } } } } } if(!defined($cnt)) { print STDERR "Error: no transaction is found\n"; exit 2; } if(!defined($bank_credits) || !defined($bank_debits)) { print STDERR "Error: no bank's total record is found\n"; exit 2; } if(!defined($opening_date) || !defined($closing_date) || !defined($opening_balance) || !defined($closing_balance)) { print STDERR "Error: opening and closing balance entries are not found\n"; exit 2; } $err_tolerance=0.01; if(abs($sum_credits-$bank_credits)>$err_tolerance || abs($sum_debits-$bank_debits)>$err_tolerance) { print STDERR "[ERROR] the summed credits and debits ($sum_credits, $sum_debits) do not match the bank totals ($bank_credits, $bank_debits)\n"; exit 3; } $computed_balance=$opening_balance+$sum_credits-$sum_debits; if(abs($computed_balance-$closing_balance)>$err_tolerance) { print STDERR "[ERROR] the opening balance ($opening_balance) plus[+] credits ($sum_credits) minus[-] debits ($sum_debits) is equal[=] to $computed_balance, which is NOT the closing balance ($closing_balance)\n"; exit 3; } if(!defined($start_date) || !defined($end_date)) { print STDERR "[ERROR] at least one date change must occur in the statement (the opening and closing dates cannot be the same)\n"; exit 4; } if($start_date!~/^\d{8}$/ || $end_date!~/^\d{8}$/) { print STDERR "[ERROR] start end date format error\n"; exit 4; } $start_date=substr($start_date,2,6); $end_date=substr($end_date,2,6); if($opening_date!~/^\d{8}$/ || $closing_date!~/^\d{8}$/) { print STDERR "[ERROR] opening closing date format error\n"; exit 4; } $opening_date=substr($opening_date,2,6); $closing_date=substr($closing_date,2,6); print "opening_date=$opening_date\n"; print "closing_date=$closing_date\n"; print "opening_balance=$opening_balance\n"; print "closing_balance=$closing_balance\n"; print "transactions=$cnt\n"; print "credits=$bank_credits\n"; print "debits=$bank_debits\n"; print "start_date=$start_date\n"; print "end_date=$end_date\n"; if($save) { print htm "\r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm "\r\n"; print htm "
Date: YYYY-MM-DDInformationScanCHFBilling EnteredEntered Date: YYYY-MM-DD (Alt-Insert-Time-Enter)Customer NamePhone (account) number: 02x-550-xxxxRemarks
%s"; if($information=~/$watch/i) {printf htm "%s",$information;} else {printf htm "%s",$information;} printf htm ""; if($scan=~/^\d{23}$/) { printf htm "",$scan; printf htm "",$scan; printf htm ""; } else { printf htm "%s",$scan; } printf htm ""; if($SG6_MOA==211) {printf htm "-%s",$print_amount;} elsif($SG6_MOA==210) {printf htm "%s",$print_amount;} else {printf htm "%s",$print_amount;} printf htm "%s%s%s%s%s
$empty$emptyTotal:Replace
by: Alt
Table
Formula
=SUM (ABOVE)
$empty$empty$empty$empty$empty
\r\n"; print htm "

* * *

\r\n"; print htm "

\r\n"; print htm "\r\n"; print htm "\r\n"; print htm "\r\n"; print htm "\r\n"; print htm "\r\n"; print htm "\r\n"; print htm "
Input (XML)Saved (HTM/DOC)
\r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm "
ParameterValue
opening_date:$opening_date
closing_date:$closing_date
opening_balance:$opening_balance
closing_balance:$closing_balance
transactions:$cnt
credits:$bank_credits
debits:$bank_debits
start_date:$start_date
end_date:$end_date
\r\n"; print htm "
\r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm " \r\n"; print htm "
ParameterValue
save_start:$save_start
save_end:$save_end
watch:$watch
exclude:$exclude
save_credits:$save_credits
save_debits:$save_debits
save_transactions:$save_transactions
\r\n"; print htm "
\r\n"; print htm "

\r\n"; print htm "\r\n"; print htm "\r\n"; close htm; }