use File::Path qw/make_path/;
use Data::Dumper;
use File::Copy qw/copy/;
+use XML::LibXML;
+use JSON::PP;
use lib('.');
use sqlite;
use utf8;
my $toolsdir="";
my $delorig=0;
my $totext =1;
-my $template = "";
+my $xmlfile = "";
my $log ="";
my $db = undef;
my $cmonth = "none";
my $frmonth = {"Janvier" => '01',"Février"=> '02',"Mars" => '03',"Avril" => '04', "Mai" => '05',"Juin" => '06',"Juillet" => '07',"Août" => '08',"Septembre" => '09',"Octobre" => '10',"Novembre" => '11',"Décembre" => '12'};
-#-db "C:\\Users\\ksaff\\DKS\\projects\\Creorga\\Calimero\\20170302\\diff\\5531423c-b85a-4305-9372-c62a293d0c84.sqlite" -p "C:\\Users\\ksaff\\DKS\\projects\\Creorga\\Calimero\\facture\\factures201712 (1).pdf" -t "C:\\Users\\ksaff\\Workspace\\creorga\\Tools" -x inv -l "C:\\Users\\ksaff\\DKS\\projects\\Creorga\\pdfextract.log" -o "C:\\Users\\ksaff\\DKS\\projects\\Creorga\\Calimero\\20170302\\diff\\imports\\5531423c-b85a-4305-9372-c62a293d0c84"
+# -db "C:\\Users\\ksaff\\DKS\\projects\\Creorga\\Calimero\\20170327\\basch\\9475a95d-e2ad-4586-8432-d44a604b3fd3.sqlite" -p "C:\\Users\\ksaff\\DKS\\projects\\Creorga\\Calimero\\2018.3.pdf" -o "C:\\Users\\ksaff\\DKS\\projects\\Creorga\\Calimero\\20170327\\basch\\imports\\9475a95d-e2ad-4586-8432-d44a604b3fd3" -t "C:\\Debug\\Creorga\\Tools" -l "C:\\Users\\ksaff\\AppData\\Roaming\\Creorga\\Profiles\\09coeoa7.default\\lastimport.json"
GetOptions("dbfile|db=s" => \$dbfile,
"pdf|p=s" => \$pdffile,
"outdir|o=s" => \$outputdir,
"toolsdir|t=s" => \$toolsdir,
- "type|x=s" => \$template, #template => [inv|stmt]
+ "xml|x=s" => \$xmlfile, #template => [inv|stmt]
"log|l=s" => \$log
);
my $sep = '/';
print "PDF:".$pdffile."\n";
print "OUTPUTDIR:".$outputdir."\n";
print "TOOLSDIR:".$toolsdir."\n";
-print "TEMPLATE:".$template."\n";
+print "XML:".$xmlfile."\n";
print "LOG:".$log."\n";
-open(LOG,">>".$log);
-
-print LOG "DB:".$dbfile."\r\n";
-print LOG "PDF:".$pdffile."\r\n";
-print LOG "OUTPUTDIR:".$outputdir."\r\n";
-print LOG "TOOLSDIR:".$toolsdir."\r\n";
-print LOG "TEMPLATE:".$template."\r\n";
-print LOG "LOG:".$log."\r\n";
-close(LOG);
+#open(LOG,">>".$log);
+#
+#print LOG "DB:".$dbfile."\r\n";
+#print LOG "PDF:".$pdffile."\r\n";
+#print LOG "OUTPUTDIR:".$outputdir."\r\n";
+#print LOG "TOOLSDIR:".$toolsdir."\r\n";
+#print LOG "TEMPLATE:".$xmlfile."\r\n";
+#print LOG "LOG:".$log."\r\n";
+#close(LOG);
my $pdftotext = "";
if ($^O eq "MSWin32") {
#}
-if (($log ne "") && (! -e $log)){
- open(LOG,">".$log);
- close(LOG);
+if (($log ne "") && (-e $log)){
+ unlink($log);
}
#my $filename = "C:\\projects\\creorga\\calimero\\factures201512.pdf";
if ((! -e $dbfile)) {
- open(LOG,">>".$log);
- print LOG localtime().":ERROR: no database incomplete input!\n";
- close(LOG);
+ #open(LOG,">>".$log);
+ print localtime().":ERROR: no database incomplete input!\n";
+ #close(LOG);
exit(1);
}
if ((! -e $pdffile)) {
- open(LOG,">>".$log);
- print LOG localtime().":ERROR: no pdf incomplete input!\n";
- close(LOG);
+ #open(LOG,">>".$log);
+ print localtime().":ERROR: no pdf incomplete input!\n";
+ #close(LOG);
exit(1);
}
if ((! -d $outputdir)) {
- open(LOG,">>".$log);
- print LOG localtime().":ERROR: no outputdir incomplete input!\n";
- close(LOG);
+ #open(LOG,">>".$log);
+ print localtime().":ERROR: no outputdir incomplete input!\n";
+ #close(LOG);
exit(1);
}
if ((! -d $toolsdir)) {
- open(LOG,">>".$log);
- print LOG localtime().":ERROR: no tools dir incomplete input!\n";
- close(LOG);
+ #open(LOG,">>".$log);
+ print localtime().":ERROR: no tools dir incomplete input!\n";
+ #close(LOG);
exit(1);
}
print "1: Split du PDF en pages!\r\n";
my $oldpdf = PDF::API2->open($pdffile);
#1.split pdffile
-$template = lc($template);
+#$template = lc($template);
my @nfiles = ();
my $xx = $oldpdf->pages;
for my $page_nb (1..$xx) {
my $newpdf = PDF::API2->new;
my $page = $newpdf->importpage($oldpdf, $page_nb);
- my $npdfname = $outputdir.'/'.$template.substr(basename($pdffile),0,-4).".".$page_nb.".pdf";
+ my $npdfname = $outputdir.'/'.'facture'.substr(basename($pdffile),0,-4).".".$page_nb.".pdf";
push @nfiles,$npdfname;
if (-e $npdfname){
unlink($npdfname);
#$dbfile =~ s/\\\\/\\/g;
$db = sqlite->new($dbfile);
}
+my $xmlres = ();
+if ($xmlfile ne "" && -e $xmlfile){
+ my $xmldata = XML::LibXML->load_xml(location => $xmlfile);
+ foreach my $data ($xmldata->findnodes('//factureExterne')) {
+ my $ref = $data->findvalue('./referenceExterne');
+ $xmlres->{$ref}->{reference} = $ref;
+ $xmlres->{$ref}->{crechename} = $data->findvalue('./objetBasDeFacture');
+ $xmlres->{$ref}->{datefacture} = $data->findvalue('./DateFacture');
+ $xmlres->{$ref}->{totalamount} = $data->findvalue('./totalTtc');
+ $xmlres->{$ref}->{totalamount} = s/\,/\./g;
+ $xmlres->{$ref}->{totalamount} = s/\s+//g;
+ my @cast = map {$_->to_literal();} $data->findnodes('./lignesFacture/ligneFacture/objetLigneFacture');
+ my @numchks = split("\n",$cast[0]);
+ my $nx = $numchks[0];
+ $xmlres->{$ref}->{checkservice} = $nx;
+ }
+}
+my $allchilddata = ();
if ($totext == 1) {
foreach my $n (@nfiles){
- if (-e $n.'.txt'){
- unlink($n.'.txt');
- }
- unlink($n.'.txt');
+ if (-e $n.'.txt'){ unlink($n.'.txt');}
+ #unlink($n.'.txt');
print "Lire des donnees de la page: ".basename($n)."\n";
my $cmd = '"'.$pdftotext.'" -q -table -eol unix "'.$n.'" "'.$n.'.txt"';
}
}
close(PDFDATA);
- if (lc($template) eq "inv") {
+ # if (lc($template) eq "inv") {
my $childdata = &parseinvoicedata(\@pdata);
- print Dumper($childdata);
+ #print Dumper($childdata);
print "Import des donnees Check-Service No.: ".$childdata->{checkservice}."\n";
+ if (exists($xmlres->{$childdata->{reference}})){
+ $childdata->{checkservice} = $xmlres->{$childdata->{reference}}->{checkservice};
+ $childdata->{crechename} = $xmlres->{$childdata->{reference}}->{crechename};
+ if (!defined($childdata->{totalamount})){
+ $childdata->{totalamount} = $xmlres->{$childdata->{reference}}->{totalamount};
+ }
+
+ }
print "\n----\n";
- &importinvoicedata($childdata,$n);
- }elsif (lc($template eq "stmt")){
- my $stmtdata = &parsestatementdata(\@pdata);
- print "Import des données Page: ".basename($n)."\n";
- &importstatementdata($stmtdata,$n);
- }
- unlink("$n.txt");
+ my ($impfile,$sqlres) = &importinvoicedata($childdata,$n);
+ #}
+ $childdata->{newpdffile} = $impfile;
+ $childdata->{sqlresult} = $sqlres;
+# elsif (lc($template eq "stmt")){
+# my $stmtdata = &parsestatementdata(\@pdata);
+# print "Import des données Page: ".basename($n)."\n";
+# &importstatementdata($stmtdata,$n);
+# }
+ #print Dumper($childdata);
+ #print "\n==================\n";
+ #unlink("$n.txt");
+ $allchilddata->{$childdata->{reference}} = $childdata;
}
}
+
+}
+my $newpdf = PDF::API2->new();
+#foreach my $f (@files){
+#
+#
+#}
+#$newpdf->saveas($inputdir.'/'.$searchstr.'.pdf');
+print "Create sorted PDF of month!\n";
+my $npdfname = "";
+foreach my $ac (sort {$a cmp $b} keys(%{$allchilddata})){
+ if ($npdfname eq ""){
+ $npdfname = substr($allchilddata->{$ac}->{reference},0,rindex($allchilddata->{$ac}->{reference},'.')).'.pdf';
+ }
+ if (($allchilddata->{$ac}->{newpdffile} ne "" && -f $outputdir.'/'.$allchilddata->{$ac}->{newpdffile}) ){
+ my $cpdf = PDF::API2->open($outputdir.'/'.$allchilddata->{$ac}->{newpdffile});
+ $newpdf->import_page($cpdf,1);
+ }
}
+$newpdf->saveas($outputdir.'/'.$npdfname);
+open(LOG,">".$log);
+print LOG JSON::PP::encode_json($allchilddata);
+close(LOG);
+
+print "Normal End!\n";
+
+
sub parseinvoicedata(){
my $tmpdata = shift;
my @invoicedata = @{$tmpdata};
my ($tmp) = $p =~ m/.+\s\((\d+)\).+$/;
$pxdata->{checkservice} = $tmp;
}
- if (($p =~ /\s+\(\d+\)\s*$/)) {
+ if (($p =~ /\s+\(\d+\)\s*$/)&& (!defined($pxdata->{checkservice}))) {
my ($tmp) = $p =~ m/\s+\((\d+)\s*\)$/;
$pxdata->{checkservice} = $tmp;
}
+ if (($p =~ /\s+\(\d+\).+$/)&& (!defined($pxdata->{checkservice}))) {
+ my ($tmp) = $p =~ m/\s+\((\d+).+\)$/;
+ $pxdata->{checkservice} = $tmp;
+ }
if (($p =~ /^\s+\(\d+\)$/) && (!defined($pxdata->{checkservice}))) {
my ($tmp) = $p =~ m/\s+\((\d+)\)$/;
$pxdata->{checkservice} = $tmp;
$e1 =~ s/\ //;
$pxdata->{benefitamount} = $e1;
}
- if (($p =~ /Montant\s.\sr.gler/)) {
- my ($m1) = $p =~ m/.+Montant\s.\sr.gler\s+([\s|\d]+,\d{1,2}).+$/;
+ if (($p =~ /Montant\s.+\sr.+gler/)) {
+ my ($m1) = $p =~ m/.+Montant\s.+\sr.+gler\s+([\s|\d]+,\d{1,2}).+$/;
$m1 =~ s/,/\./;
$m1 =~ s/\ //;
$pxdata->{totalamount} = $m1;
}
- if (($p =~ /Montante a pagar/)) {
- my ($m1) = $p =~ m/.+Montante\sa\spagarr\s+([\s|\d]+,\d{1,2}).+$/;
+ if (($p =~ /Montante\sa\spagar/)) {
+ my ($m1) = $p =~ m/.+Montante\sa\spagar\s+([\s|\d]+,\d{1,2}).+$/;
$m1 =~ s/,/\./;
$m1 =~ s/\ //;
$pxdata->{totalamount} = $m1;
return $pxdata;
}
-sub parsestatementdata(){
- my $tmpdata = shift;
- my @xstmtdata = @{$tmpdata};
- my $sxdata = ();
-
- foreach my $p (@xstmtdata){
- if ($p =~ /P.riode/) {
- my ($m1,$y1,$m2,$y2) = $p =~ m/.+\s(.+)\s+(\d+)\s+.\s+(.+)\s+(\d+)$/;
- if (($m1 eq $m2) && ($y1 eq $y2)){
- $cmonth=$y1.'-'.$frmonth->{$m1};
- }
- }
- if ($p =~ /\d{13,}/) {
- my ($csnum,$am) = $p =~ m/.+\s+(\d{13,})\s+([\d|\ |,]+)$/;
- $am =~ s/\s+//;
- $am =~ s/,/./;
- $sxdata->{$cmonth}->{$csnum}=$am;
- }
- #print $p."\n";
- }
- return $sxdata;
-}
+#sub parsestatementdata(){
+# my $tmpdata = shift;
+# my @xstmtdata = @{$tmpdata};
+# my $sxdata = ();
+#
+# foreach my $p (@xstmtdata){
+# if ($p =~ /P.riode/) {
+# my ($m1,$y1,$m2,$y2) = $p =~ m/.+\s(.+)\s+(\d+)\s+.\s+(.+)\s+(\d+)$/;
+# if (($m1 eq $m2) && ($y1 eq $y2)){
+# $cmonth=$y1.'-'.$frmonth->{$m1};
+# }
+# }
+# if ($p =~ /\d{13,}/) {
+# my ($csnum,$am) = $p =~ m/.+\s+(\d{13,})\s+([\d|\ |,]+)$/;
+# $am =~ s/\s+//;
+# $am =~ s/,/./;
+# $sxdata->{$cmonth}->{$csnum}=$am;
+# }
+# #print $p."\n";
+# }
+# return $sxdata;
+#}
sub importinvoicedata(){
my $impdata = shift;
my $fname = shift;
+ my $insdbfile = "";
+ my $sqlmsg = "";
if (defined($db)){
my $child = $db->dbquerysorted("select uuid from childs where replace(checkservicenumber,' ','') = '".$impdata->{checkservice}."';");
if (keys(%{$child}) == 1) {
my @upd = ();
if (exists($impdata->{reference})) {
push @upd,"reference='".$impdata->{reference}."'";
- my $nfname = dirname($fname).$sep.$template.'_'.$impdata->{checkservice}.'_'.$impdata->{reference}.".pdf";
+ my $nfname = dirname($fname).$sep.'facture'.'_'.$impdata->{checkservice}.'_'.$impdata->{reference}.".pdf";
print "Copy to new file: ".$nfname."\n";
copy ($fname,$nfname);
- my $insdbfile = basename($nfname);
+ $insdbfile = basename($nfname);
if (-e $nfname){
print "remove extracted file: ".$fname."\n";
unlink($fname);
#print $sql."\n";
my $r = $db->dbexec($sql);
#my $r = 1;
- if (($log ne "") && (-e $log)){
- if (!defined($r)) {
- open(LOG,">>".$log);
- print LOG localtime().":ERROR:".$sql."\n";
- close(LOG);
+ if (!defined($r)) {
+ $sqlmsg = "Update ERROR";
} else {
- open(LOG,">>".$log);
- print LOG localtime().":SUCCESS:".$sql."\n";
- close(LOG);
+ $sqlmsg = "Update OK";
}
- }
}else {
my @ins1 = ();
my @ins2 = ();
}
if (exists($impdata->{reference})) {
push(@ins1,"reference");push (@ins2,"'".$impdata->{reference}."'");
- my $nfname = dirname($fname).$sep.$template.'_'.$impdata->{checkservice}.'_'.$impdata->{reference}.".pdf";
+ my $nfname = dirname($fname).$sep.'facture'.'_'.$impdata->{checkservice}.'_'.$impdata->{reference}.".pdf";
print "Copy to new file: ".$nfname."\n";
copy ($fname,$nfname);
- my $insdbfile = basename($nfname);
+ $insdbfile = basename($nfname);
if (-e $nfname){
print "remove extracted file: ".$fname."\n";
unlink($fname);
#print $sql."\n";
my $r = $db->dbexec($sql);
- if (($log ne "") && (-e $log)){
+ #if (($log ne "") && (-e $log)){
if (!defined($r)) {
- open(LOG,">>".$log);
- print LOG localtime()." : ERROR : ".$sql."\n";
- close(LOG);
+ $sqlmsg = "insert ERROR";
} else {
- open(LOG,">>".$log);
- print LOG localtime()." : SUCCESS : ".$sql."\n";
- close(LOG);
+ $sqlmsg = "insert OK";
}
- }
+ #}
}
}
}
+ else {
+ $sqlmsg = "Enfant pas existant! No check-service :".$impdata->{checkservice}."!";
+ }
+ return ($insdbfile,$sqlmsg);
}
-sub importstatementdata(){
- my $simpdata = shift;
- my $fname = shift;
- #print Dumper($simpdata);
- my $n=0;
- foreach my $m (keys(%{$simpdata})){
- $n++;
- if (-e dirname($fname).$sep."prestation.".$m."-".$n.".pdf"){
- unlink(dirname($fname).$sep."prestation.".$m."-".$n.".pdf");
- }
- rename($fname,dirname($fname).$sep."prestation.".$m."-".$n.".pdf");
- foreach my $csnum (keys(%{$simpdata->{$m}})){
- $simpdata->{$m}->{fnum} = $n;
- }
- }
- foreach my $m (keys(%{$simpdata})){
- $n++;
- if ($m =~ /\d{4,}-\d{2,}/) {
- foreach my $csnum (keys(%{$simpdata->{$m}})){
- print "Import Check-Service no: " + $csnum + "\n";
- if (defined($db)){
- my $child = $db->dbquerysorted("select uuid from childs where replace(checkservicenumber,' ','') = '".$csnum."';");
- if (keys(%{$child}) == 1) {
- my $accdata = $db->dbquerysorted("select accmonth,childuuid from accounting where childuuid='".$child->{0}->{uuid}."' and accmonth=date('".substr($m,0,4).'-'.substr($m,5,2)."-01');");
- if (keys(%{$accdata}) == 1) {
- #make update
- my @upd = ();
- push @upd,"benefitamount='".$simpdata->{$m}->{$csnum}."'";
- push @upd,"benefitfile='prestation.".$m."-".$simpdata->{$m}->{fnum}.".pdf'";
- my $sql = "update accounting set ".join(',',@upd)." where childuuid='".$child->{0}->{uuid}."' and accmonth=date('".substr($m,0,4).'-'.substr($m,5,2)."-01');";
- #print $sql."\n";
- my $r = $db->dbexec($sql);
- if (($log ne "") && (-e $log)){
- if (!defined($r)) {
- open(LOG,">>".$log);
- print LOG localtime().":ERROR:".$sql."\n";
- close(LOG);
- } else {
- open(LOG,">>".$log);
- print LOG localtime().":SUCCESS:".$sql."\n";
- close(LOG);
- }
- }
- }else {
- my @ins1 = ();
- my @ins2 = ();
- push(@ins1,"accmonth");push (@ins2,"date('".substr($m,0,4).'-'.substr($m,5,2)."-01')");
- push(@ins1,"childuuid");push (@ins2,"'".$child->{0}->{uuid}."'");
- push(@ins1,"benefitamount");push (@ins2,"".$simpdata->{$m}->{$csnum}."");
- push(@ins1,"benefitfile");push (@ins2,"'prestation.".$m."-".$simpdata->{$m}->{fnum}.".pdf'");
-
- #accmonth,childuuid,invoicedate,invoiceamount,reference
- my $sql = "insert into accounting (".join(',',@ins1).") VALUES (".join(',',@ins2).");";
- #print $sql."\n";
- my $r = $db->dbexec($sql);
- if (($log ne "") && (-e $log)){
- if (!defined($r)) {
- open(LOG,">>".$log);
- print LOG localtime().":ERROR:".$sql."\n";
- close(LOG);
- } else {
- open(LOG,">>".$log);
- print LOG localtime().":SUCCESS:".$sql."\n";
- close(LOG);
- }
- }
- }
- }
- }
- }
- }
- }
-}
\ No newline at end of file
+#sub importstatementdata(){
+# my $simpdata = shift;
+# my $fname = shift;
+# #print Dumper($simpdata);
+# my $n=0;
+# foreach my $m (keys(%{$simpdata})){
+# $n++;
+# if (-e dirname($fname).$sep."prestation.".$m."-".$n.".pdf"){
+# unlink(dirname($fname).$sep."prestation.".$m."-".$n.".pdf");
+# }
+# rename($fname,dirname($fname).$sep."prestation.".$m."-".$n.".pdf");
+# foreach my $csnum (keys(%{$simpdata->{$m}})){
+# $simpdata->{$m}->{fnum} = $n;
+# }
+# }
+# foreach my $m (keys(%{$simpdata})){
+# $n++;
+# if ($m =~ /\d{4,}-\d{2,}/) {
+# foreach my $csnum (keys(%{$simpdata->{$m}})){
+# print "Import Check-Service no: " + $csnum + "\n";
+# if (defined($db)){
+# my $child = $db->dbquerysorted("select uuid from childs where replace(checkservicenumber,' ','') = '".$csnum."';");
+# if (keys(%{$child}) == 1) {
+# my $accdata = $db->dbquerysorted("select accmonth,childuuid from accounting where childuuid='".$child->{0}->{uuid}."' and accmonth=date('".substr($m,0,4).'-'.substr($m,5,2)."-01');");
+# if (keys(%{$accdata}) == 1) {
+# #make update
+# my @upd = ();
+# push @upd,"benefitamount='".$simpdata->{$m}->{$csnum}."'";
+# push @upd,"benefitfile='prestation.".$m."-".$simpdata->{$m}->{fnum}.".pdf'";
+# my $sql = "update accounting set ".join(',',@upd)." where childuuid='".$child->{0}->{uuid}."' and accmonth=date('".substr($m,0,4).'-'.substr($m,5,2)."-01');";
+# #print $sql."\n";
+# my $r = $db->dbexec($sql);
+# if (($log ne "") && (-e $log)){
+# if (!defined($r)) {
+# open(LOG,">>".$log);
+# print LOG localtime().":ERROR:".$sql."\n";
+# close(LOG);
+# } else {
+# open(LOG,">>".$log);
+# print LOG localtime().":SUCCESS:".$sql."\n";
+# close(LOG);
+# }
+# }
+# }else {
+# my @ins1 = ();
+# my @ins2 = ();
+# push(@ins1,"accmonth");push (@ins2,"date('".substr($m,0,4).'-'.substr($m,5,2)."-01')");
+# push(@ins1,"childuuid");push (@ins2,"'".$child->{0}->{uuid}."'");
+# push(@ins1,"benefitamount");push (@ins2,"".$simpdata->{$m}->{$csnum}."");
+# push(@ins1,"benefitfile");push (@ins2,"'prestation.".$m."-".$simpdata->{$m}->{fnum}.".pdf'");
+#
+# #accmonth,childuuid,invoicedate,invoiceamount,reference
+# my $sql = "insert into accounting (".join(',',@ins1).") VALUES (".join(',',@ins2).");";
+# #print $sql."\n";
+# my $r = $db->dbexec($sql);
+# if (($log ne "") && (-e $log)){
+# if (!defined($r)) {
+# open(LOG,">>".$log);
+# print LOG localtime().":ERROR:".$sql."\n";
+# close(LOG);
+# } else {
+# open(LOG,">>".$log);
+# print LOG localtime().":SUCCESS:".$sql."\n";
+# close(LOG);
+# }
+# }
+# }
+# }
+# }
+# }
+# }
+# }
+#}
\ No newline at end of file
my $key = shift;
my $stat = shift;
my $retdata =();
- my $dbh = DBI->connect('DBI:SQLite:dbname='.$self->{dbfile},"","",{PrintError=>1,RaiseError=>1,AutoCommit=>1}) or die "dbquery Connection Error!".$!;
+ my $dbh = DBI->connect('DBI:SQLite:dbname='.$self->{dbfile},"","",{PrintError=>1,RaiseError=>1,AutoCommit=>1}) or return "dbquery Connection Error!".$!;
$stat = encode("utf8", $stat);
#open FILE,">>/tmp/sql.log";
my $self = shift;
my $stat = shift;
my $retdata = ();
- my $dbh = DBI->connect('DBI:SQLite:dbname='.$self->{dbfile},"","",{PrintError=>1,RaiseError=>1,AutoCommit=>1}) or die "dbquery Connection Error!".$!;
+ my $dbh = DBI->connect('DBI:SQLite:dbname='.$self->{dbfile},"","",{PrintError=>1,RaiseError=>1,AutoCommit=>1}) or return "dbquery Connection Error!".$!;
$stat = encode("utf8", $stat);
#open FILE,">>/tmp/sql.log";
#print "$stat\n";
sub dbexec(){
my $self = shift;
my $stat = shift;
- my $dbh = DBI->connect('DBI:SQLite:dbname='.$self->{dbfile},"","",{PrintError=>1,AutoCommit=>1}) or die "dbexec Connection Error!".$!;
+ my $dbh = DBI->connect('DBI:SQLite:dbname='.$self->{dbfile},"","",{PrintError=>1,AutoCommit=>1}) or return "dbexec Connection Error!".$!;
$stat = encode("utf8", $stat);
#print $stat."\n";
#open FILE,">>/Users/kilian/sql.log";
#print FILE "$stat\n";
#close FILE;
my $sth = $dbh->prepare($stat);
- my $rv =$dbh->do($stat) or print "Failed dbexec:\n'".$stat. "'\n\n";
+ my $rv =$dbh->do($stat) or return "SQL ERROR:".$dbh->errstr;
$dbh->disconnect();
return $rv;
}
<button class="btn btn-primary" onclick="onpreviousmonth();" ><span class="glyphicon glyphicon-chevron-left"></span></button>
<button class="btn btn-primary" onclick="onnextmonth();" ><span class="glyphicon glyphicon-chevron-right"></span></button>
<button class="btn btn-primary" onclick="generatecsv();" ><span class="glyphicons glyphicons-file-export"></span> Export CSV Check-Service</button>
+ <button class="btn btn-info" onclick="openmonthlypdf();" style="display: none;" id="monthlypdf"><span class="glyphicons glyphicons-file"></span> PDF des Factures importées et triées</button>
<!--<div class="input-group">
<div class="input-group-addon">Colonnes <span class="glyphicons glyphicons-eye-close" aria-hidden="true"></span></div>
<select id="hidden_columns" multiple="multiple" onchange="onhidecolumn(this.id);">
<menuitem label="par mois" oncommand="navigation.load_appview('accounting','accounting', null,'Décompte par mois');" id="mnuaccountingmonth"/>
<menuitem label="par enfant" oncommand="navigation.load_appview('accountingchild','accounting',null,'Décompte par enfant');" id="mnuaccountingchild"/>
<menuitem label="factures ouvertes" oncommand="navigation.load_appview('accountingopeninvoice','accounting',null,'Factures ouvertes');" id="mnuaccountingopeninvoice"/>
- <menu label="Import PDF" id="mnuimportpdf">
+ <menuseparator />
+ <menuitem label="import Check-Services" oncommand="navigation.load_appview('checkservice','accounting',null,'Import Check Service');" id="mnuaccountingimport"/>
+ <!--<menu label="Import PDF" id="mnuimportpdf">
<menupopup style="text-align: left;">
<menuitem label="Factures Check-Service" oncommand="tools.readinvoicepdf();" id="mnuaccountingimportinvoice"/>
<menuitem label="Décompte des prestations" oncommand="tools.readbenefitstatement();" id="mnuaccountingbenefitstatement"/>
</menupopup>
- </menu>
+ </menu>-->
<menuseparator />
<label value="Bilan" style="font-weight: bold;"/>
<menuitem label="Bilan annuelle" oncommand="navigation.load_appview('accountingyear','accounting',null,'Bilan annuelle');" id="mnuaccountingyear"/>
--- /dev/null
+
+<!DOCTYPE html>
+
+<html lang="fr">
+<head>
+ <meta charset="utf-8">
+ <title>Décompte</title>
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+ <meta http-equiv="cache-control" content="max-age=0" />
+ <meta http-equiv="cache-control" content="no-cache" />
+ <meta http-equiv="expires" content="0" />
+ <meta http-equiv="expires" content="Tue, 01 Jan 1970 1:00:00 GMT" />
+ <meta http-equiv="pragma" content="no-cache" />
+ <link href="../../web/css/bootstrap.min.css" rel="stylesheet" type="text/css">
+ <!--<link href="../../web/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css">-->
+ <link href="../../web/css/datatables.min.css" rel="stylesheet" type="text/css">
+ <link href="../../web/css/dataTables.bootstrap.min.css" rel="stylesheet" type="text/css">
+ <link href="../../web/css/bootstrap-datetimepicker.min.css" rel="stylesheet" type="text/css">
+ <link href="../../web/css/bootstrap-timepicker.min.css" rel="stylesheet" type="text/css">
+ <link href="../../web/css/bootstrap-multiselect.css" rel="stylesheet" type="text/css">
+ <link href="../../web/css/glyphicons.css" rel="stylesheet" type="text/css">
+ <link href="../../web/css/creorga.css" rel="stylesheet" type="text/css">
+ <style type="text/css">
+ .dataTables_filter {
+ margin-top: -30px;
+ }
+ </style>
+</head>
+
+<body>
+ <div class="container-fluid" style="padding-top: 5px;">
+ <div class="row">
+ <div class="col-sm-6">
+ <div class="panel panel-default">
+ <div class="panel-heading">
+ <h3 class="panel-title"> Fichiers PDF <span style="font-weight: bold;">(requis pour import)</span></h3>
+ </div>
+ <div class="panel-body" id="infopanelpdf">
+ <div class="form-group">
+ <label for="importfileinvoice">Fichier PDF:</label>
+ <input type="text" class="form-control" id="importfileinvoice" readonly="1">
+ <button class="btn btn-primary" id="btnselectpdf" onclick="loadinvoicepdf();">Charger fichier PDF</button>
+ <!--<label for="importtype">Type:</label>
+ <select class="formcontrol" id="importtype">
+ <option value="inv">Factures Check-Service</option>
+ <option value="stmt">Prestations Check-Service</option>
+ <option value="invold">Anciennes Factures Check-Service</option>
+ </select>-->
+
+ </div>
+
+
+ </div>
+ </div>
+
+ </div>
+ <div class="col-sm-6">
+ <div class="panel panel-default">
+ <div class="panel-heading">
+ <h3 class="panel-title"> Fichier XML <span style="font-weight: bold;">(optionel pour import)</span></h3>
+ </div>
+ <div class="panel-body" id="infopanelxml">
+ <div class="form-group">
+ <label for="importfilexml">Fichier XML:</label>
+ <input type="text" class="form-control" id="importfilexml" readonly="1">
+ <button class="btn btn-primary" id="btnselectxml" onclick="loadinvoicexml();">Charger fichier XML</button>
+
+
+ </div>
+
+
+ </div>
+ </div>
+
+ </div>
+ <div class="col-md-12">
+ <button class="btn btn-primary" id="btnimportfiles" onclick="importfiles();">Importer</button>
+ </div>
+ <div class="col-md-12" style="text-align: center;display: none;" id="importprogress">
+ <p style="font-weight: bold;">Attendez s.v.p.! Import des fichiers en cours!</p>
+ <progress style="width:50%;"></progress>
+ </div>
+ <div class="col-md-12" style="display: none;" id="importresult">
+ <table class="display compact dataTable cell-border" style="width: 100%;" id="tbl_importresult" role="grid">
+ <thead><tr><th>Reference</th><th>No Check-Service</th><th>Status d'import<th></tr></thead>
+ <tbody></tbody>
+ </table>
+ </div>
+ </div>
+
+ </div>
+
+ <script src="../../web/js/jquery.min.js" type="text/javascript"></script>
+ <script src="../../web/js/bootstrap.min.js" type="text/javascript"></script>
+ <script src="../../web/js/datatables.min.js" type="text/javascript"></script>
+ <script src="../../web/js/moment-with-locales.js" type="text/javascript"></script>
+ <script src="../../web/js/bootstrap-datetimepicker.min.js" type="text/javascript"></script>
+ <script src="../../web/js/bootstrap-timepicker.min.js" type="text/javascript"></script>
+ <script src="../../web/js/bootstrap-multiselect.js" type="text/javascript"></script>
+ <script src="chrome://creorga/content/js/navigation.js" type="text/javascript"></script>
+ <script src="chrome://creorga/content/js/preferences.js" type="text/javascript"></script>
+ <script src="chrome://creorga/content/js/system.js" type="text/javascript"></script>
+ <script src="chrome://creorga/content/js/database.js" type="text/javascript"></script>
+
+ <script src="../../web/js/labels.js" type="text/javascript"></script>
+ <script src="js/checkservice.js" type="text/javascript"></script>
+ <script src="../../web/js/creorga.js" type="text/javascript"></script>
+
+</body>
+</html>
var strminweek = null;
var monthmin = null;
var monthmax = null;
+var mpdffile = null;
var aax = ['mon','tue','wed','thu','fri'];
//var lictype = apppref.getpreference("support.lictype");
var amonth = ['','Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Aôut','Septembre','Octobre','Novembre','Décembre'];
pconfig.accmonth=initdate.toLocaleFormat('%m.%Y');
}
cdate = initdate.toLocaleFormat('%Y-%m-%d');
+ var cmonth = initdate.getMonth() +1;
+ mpdffile = system.getDirectory(appdb.dbFile.path) + system.sep() + 'imports' + system.sep() + crecheuuid +system.sep() + initdate.getFullYear() + "." + cmonth + ".pdf";
+ if (system.fileexists(mpdffile)){
+ $("#monthlypdf").show();
+ }
//lictype = apppref.getpreference("support.lictype");
//if (lictype != 'free'){
// $("#invoicefile").removeAttr("disabled");
"LEFT JOIN accounting acc ON ( ttl.childuuid = acc.childuuid AND acc.accmonth='"+ initdate.toLocaleFormat("%Y-%m-%d")+ "') where calweek is not null) group by childuuid) d1 ";
accsql += " on (planned.childuuid=d1.childuuid) order by planned.childname;";
- dump("ACCSQL:" + accsql + "\n");
+ //dump("ACCSQL:" + accsql + "\n");
var accdata = appdb.dbquery(accsql);
numchilds = accdata.sqldata.length;
var headerdata = [];
system.WriteTextFile(expdata,expfolder.path + system.sep() + val + "_export_checkservice.csv");
//dump(expdata);
}
+
+function openmonthlypdf(){
+ dump("PDF to Open: " + mpdffile + "\n");
+ var ff = system.openFile(mpdffile);
+}
"case when acc.invoiceamount is not null then printf(\"%.2f\",COALESCE(acc.invoiceamount,0.00)) || '€' || case when acc.invoicedate is not null then '<br/>(' || strftime('%d.%m.%Y',acc.invoicedate) || ')' || CASE WHEN acc.reminderdate1 IS NOT NULL THEN '<br/>R1:' || strftime('%d.%m.%Y',acc.reminderdate1) ELSE '' END || CASE WHEN acc.reminderdate2 IS NOT NULL THEN '<br/>R2:' || strftime('%d.%m.%Y',acc.reminderdate2) ELSE '' END || CASE WHEN acc.reminderdate3 IS NOT NULL THEN '<br/>R3:' || strftime('%d.%m.%Y',acc.reminderdate3) ELSE '' END else '' end else '' end as invoiced, " +
"case when acc.payedamount is not null then printf(\"%.2f\",COALESCE(acc.payedamount,0.00)) || '€' || case when acc.payeddate is not null then '<br/>(' || strftime('%d.%m.%Y',acc.payeddate) || ')' else '' end else '' end as payement," +
"case when acc.benefitamount is not null then printf(\"%.2f\",COALESCE(acc.benefitamount,0.00)) || '€' else '' end as benefitamount," +
-"'<button type=\"button\" class=\"btn btn-primary\" onclick=\"dlg_accounting(''' || ttl.childuuid || ''');\" title=\"editer\"><span class=\"glyphicon glyphicon-pencil\"></span></button>' || CASE WHEN acc.invoicefile IS NOT NULL and acc.invoicefile != '' THEN '<button type=\"button\" class=\"btn btn-primary\" onclick=\"openfile(''' || acc.invoicefile || ''');\" title=\"editer\"><span class=\"glyphicon glyphicon-open\"></span>' else '' end || CASE WHEN acc.childuuid IS NOT NULL AND acc.accmonth IS NOT NULL THEN '<button type=\"button\" class=\"btn btn-danger\" onclick=\" confirm_delete_accounting(''' || acc.childuuid || ''',''' || acc.accmonth || ''');\" title=\"supprimer\"><span class=\"glyphicon glyphicon-remove\"></span></button>' ELSE '' END AS act " +
+"'<button type=\"button\" class=\"btn btn-primary\" onclick=\"dlg_accounting(''' || acc.accmonth || ''');\" title=\"editer\"><span class=\"glyphicon glyphicon-pencil\"></span></button>' || CASE WHEN acc.invoicefile IS NOT NULL and acc.invoicefile != '' THEN '<button type=\"button\" class=\"btn btn-primary\" onclick=\"openfile(''' || acc.invoicefile || ''');\" title=\"editer\"><span class=\"glyphicon glyphicon-open\"></span>' else '' end || CASE WHEN acc.childuuid IS NOT NULL AND acc.accmonth IS NOT NULL THEN '<button type=\"button\" class=\"btn btn-danger\" onclick=\" confirm_delete_accounting(''' || acc.childuuid || ''',''' || acc.accmonth || ''');\" title=\"supprimer\"><span class=\"glyphicon glyphicon-remove\"></span></button>' ELSE '' END AS act " +
"FROM ( SELECT pch.childname, pch.childuuid, px.calweek, px.calmonth, px.sumdurationcalweek, px.sumlunchcalweek, " +
"CASE WHEN px.sumdurationcalweek IS NOT NULL AND pch.checkservicenumber IS NOT NULL THEN ( SELECT coalesce(wc0.costsperhour,0.00) FROM costs wc0 WHERE wc0.startdate < date('"+yearmin+"','+' || caldays || ' days') and wc0.weeklyhourslimit <= px.sumdurationcalweek ORDER BY wc0.startdate DESC,wc0.weeklyhourslimit DESC LIMIT 1 ) WHEN px.sumdurationcalweek IS NOT NULL AND pch.checkservicenumber IS NULL THEN ( SELECT coalesce(wc0.costperhourfallback,0.00) FROM costs wc0 WHERE wc0.startdate < date('"+yearmin+"','+' || caldays || ' days') and wc0.weeklyhourslimit <= px.sumdurationcalweek ORDER BY wc0.startdate DESC,wc0.weeklyhourslimit DESC LIMIT 1 ) ELSE NULL END AS weekhourcosts,"+
function dlg_accounting(accmonth){
-
- var acc1sql = "SELECT accmonth,childuuid,case when invoicedate is not null then strftime('%d.%m.%Y',invoicedate) else null end as invoicedate, case when payeddate is not null then strftime('%d.%m.%Y',payeddate) else null end as payeddate, payedamount, benefitamount,invoiceamount, reference, case when reminderdate1 is not null then strftime('%d.%m.%Y',reminderdate1) else null end as reminderdate1, case when reminderdate2 is not null then strftime('%d.%m.%Y',reminderdate2) else null end as reminderdate2, case when reminderdate3 is not null then strftime('%d.%m.%Y',reminderdate3) else null end as reminderdate3,case when invoicefile is not null and invoicefile != '' then '"+ importpath.path + system.sep() +"' || invoicefile else null end as invoicefile,'upd' as action FROM accounting where accmonth = '"+ accmonth.substring(3) + "-" + accmonth.substring(0,2) +"-01' and childuuid='"+pconfig.accchild+"';";
-
+
+ var acc1sql = "SELECT accmonth,childuuid,case when invoicedate is not null then strftime('%d.%m.%Y',invoicedate) else null end as invoicedate, case when payeddate is not null then strftime('%d.%m.%Y',payeddate) else null end as payeddate, payedamount, benefitamount,invoiceamount, reference, case when reminderdate1 is not null then strftime('%d.%m.%Y',reminderdate1) else null end as reminderdate1, case when reminderdate2 is not null then strftime('%d.%m.%Y',reminderdate2) else null end as reminderdate2, case when reminderdate3 is not null then strftime('%d.%m.%Y',reminderdate3) else null end as reminderdate3,case when invoicefile is not null and invoicefile != '' then '"+ importpath.path + system.sep() +"' || invoicefile else null end as invoicefile,'upd' as action FROM accounting where accmonth = '" + accmonth +"' and childuuid='"+pconfig.accchild+"';";
+
var acc1data = appdb.dbquery(acc1sql);
-
+
if (acc1data.sqldata.length == 0) {
acc1sql = "SELECT '"+ accmonth.substring(3) + "-" + accmonth.substring(0,2) +"-01' as accmonth,'"+ pconfig.accchild+"' as childuuid, null as invoicedate, null as payeddate, null as payedamount, null as invoiceamount, null as reference, null as reminderdate1, null as reminderdate2, null as reminderdate3,null as benefitamount ,null as invoicefile, 'ins' as action;";
--- /dev/null
+function initdata(){
+ curcfg.loadconfig(apppref.getpreference("support.database.currentdb.num"));
+ load_helpers();
+// //var importpath = {path : apppref.curcfg.dbpath + '/' +apppref.curcfg.db.substring(0,-6) +'/import/' };
+//
+//// pconfig.printlayout = 'list';
+// var ccreche = appdb.dbquery("select uuid from creche LIMIT 1;");
+// crecheuuid = ccreche.sqldata[0].uuid;
+//
+//
+// if ((apppref.curcfg.pageconfig.data) && (apppref.curcfg.pageconfig.data.accmonth)) {
+//
+// initdate = new Date(apppref.curcfg.pageconfig.data.accmonth);
+//
+// }
+// $("#accmonth").val(formatMonthYearDate(initdate));
+// apppref.setpreference("data","accmonth",formatisodate(initdate));
+// cdate = initdate.toISOString().substring(0,10);
+//
+// load_accounting_table();
+// var bfile = appdb.dbquery("select benefitfile from accounting where accmonth = date('"+apppref.curcfg.pageconfig.data.accmonth+"') group by benefitfile LIMIT 1;");
+//
+// if ((bfile.sqldata) && (bfile.sqldata.length == 1)) {
+// $('#mnuacc').append("<button type=\"button\" class=\"btn btn-primary\" onclick=\"openfile('"+ bfile.sqldata[0].benefitfile +"');\" title=\"editer\"><span class=\"glyphicon glyphicon-open\"></span> fichier des prestations</button>");
+// }
+// $("#accmonth").on("change", function (e) {
+// set_accmonth();
+// });
+
+}
+
+function loadinvoicepdf(){
+ var lastdir = apppref.getpreference("support.lastfiledir");
+ var nsIFilePicker = Components.interfaces.nsIFilePicker;
+ var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
+ fp.init(window, "selectionner fichier PDF", nsIFilePicker.modeOpen);
+ fp.appendFilter("PDF", "*.pdf");
+ if (lastdir != '') {
+ var nsILocalFile= Components.interfaces.nsILocalFile;
+ var f = Components.classes["@mozilla.org/file/local;1"].createInstance(nsILocalFile);
+ f.initWithPath(lastdir);
+ fp.displayDirectory = f;
+ }
+ var res = fp.show();
+ if (res != nsIFilePicker.returnCancel) {
+ var pdffile = fp.file.path;
+ dump("PDF File Selected: " + pdffile + "\n");
+ apppref.setpreference("support.lastfiledir",system.getDirectory(pdffile));
+ $("#importfileinvoice").val(pdffile);
+ }
+}
+
+function loadinvoicexml(){
+ var lastdir = apppref.getpreference("support.lastfiledir");
+ var nsIFilePicker = Components.interfaces.nsIFilePicker;
+ var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
+ fp.init(window, "selectionner fichier XML", nsIFilePicker.modeOpen);
+ fp.appendFilter("XML", "*.xml");
+ if (lastdir != '') {
+ var nsILocalFile= Components.interfaces.nsILocalFile;
+ var f = Components.classes["@mozilla.org/file/local;1"].createInstance(nsILocalFile);
+ f.initWithPath(lastdir);
+ fp.displayDirectory = f;
+ }
+ var res = fp.show();
+ if (res != nsIFilePicker.returnCancel) {
+ var xmlfile = fp.file.path;
+ dump("XML File Selected: " + xmlfile + "\n");
+ apppref.setpreference("support.lastfiledir",system.getDirectory(xmlfile));
+ $('#importfilexml').val(xmlfile);
+ }
+}
+
+function importfiles(){
+ $("#btnimportfiles").hide();
+ $("#importresult").hide();
+ $("#importprogress").show();
+
+ OS.File.remove(system.profiledir+ system.sep() + 'lastimport.json',{ignoreAbsent:true});
+ var cname = appdb.dbquery("select uuid from creche LIMIT 1;");
+ cruuid = cname.sqldata[0].uuid;
+ dump(curcfg.path.local + system.sep() +"imports" + system.sep() + cruuid + "\n");
+ var importpath = FileUtils.File(curcfg.path.local + system.sep() +"imports" + system.sep() + cruuid);
+ var args = ["-db",'"' + appdb.dbFile.path + '"',"-p",'"' +$("#importfileinvoice").val() + '"',"-o",'"' + importpath.path + '"',"-t",'"' + system.toolsdir() + '"',"-l", '"' + system.profiledir()+ system.sep() + 'lastimport.json'];
+ if ($('#importfilexml').val() != ""){
+ args.push("-x");
+ args.push('"' + $('#importfilexml').val() + '"');
+ }
+
+ var binpdf = system.toolsdir() + system.sep() + "pdfextract.exe";
+ if (system.os != "WINNT") {
+ binpdf = system.toolsdir() + system.sep() + "pdfextract";
+ }
+ //dump(binpdf + " " +JSON.stringify(args) + "\n");
+ //system.popup("Import Factures!","Import du PDF '"+ pdffile.path +"' en cours!\n");
+ system.runcmdline(binpdf,args,setresult);
+
+}
+
+function setresult(data){
+ var strx = apppref.getpreference("pageconfig.lastpage");
+ var strjresult = system.readLocalTextFile(system.profiledir()+ system.sep() + 'lastimport.json');
+ var strx = apppref.getpreference("pageconfig.lastpage");
+ var x = JSON.parse(strx);
+
+ if (x.module == 'accounting') {
+ $("#tbl_importresult > tbody").html("");
+ var tblx = sortByKey(JSON.parse(strjresult));
+ for (var i in tblx){
+ dump(i + "\n" + JSON.stringify(tblx[i][1]) + "\n-\n");
+ var xd = '<tr>';
+ xd += '<td>'+tblx[i][1].reference+'</td>';
+ xd += '<td>'+tblx[i][1].checkservice+'</td>';
+ xd += '<td>'+tblx[i][1].sqlresult+'</td>';
+ xd += '</tr>';
+ $("#tbl_importresult > tbody").append(xd);
+ }
+ $("#importresult").html();
+ $("#importprogress").hide();
+ $("#btnimportfiles").show();
+ $("#importresult").show();
+ }
+
+ }
+
+function sortByKey(jsObj){
+ var sortedArray = [];
+
+ // Push each JSON Object entry in array by [key, value]
+ for(var i in jsObj)
+ {
+ sortedArray.push([i, jsObj[i]]);
+ }
+
+ // Run native sort function and returns sorted array.
+ return sortedArray.sort();
+}
use strict;
chdir("C:\\Users\\ksaff\\Workspace\\creorga\\Tools");
-system('"C:\\Strawberry\\perl\\site\\bin\\pp" -o "C:\\Users\\ksaff\\Release\\dist\\Tools\\pdfextract.exe" pdfextract.pl');
\ No newline at end of file
+system('"C:\\Strawberry\\perl\\site\\bin\\pp" -o "C:\\Debug\\Creorga\\Tools\\pdfextract.exe" pdfextract.pl');