'.$p.''; if ($msg != null){ echo $msg; } diff --git a/lib/createpdfA4invoice b/lib/createpdfA4invoice index cd525f9..0c569ab 100644 --- a/lib/createpdfA4invoice +++ b/lib/createpdfA4invoice @@ -4,83 +4,31 @@ use strict; use File::Basename; use Getopt::Long; use utf8; +use Data::Dumper; use lib (dirname($0).'/lib'); use PDF::API2; use PDF::Table; use Image::Size; use Encode; -use JSON::PP; +use JSON::PP qw/encode_json decode_json/; #use Data::Dumper; my $pdf = PDF::API2->new(); my $strpdfdata =""; my $templatefile = dirname($0)."/invoicetemplate.json"; -my $data = ""; +my $xindata = ""; my $pdfout = ""; my $lang = "fr"; -# my $datafile=dirname($0).'/pdftest.pdf'; -GetOptions ("data|d=s" => \$data, "output|o=s" => \$pdfout, "lang|l=s" =>\$lang); - -if (! -e $templatefile){ - print "file $templatefile does not exist!\n"; - exit(1); -} -# print $data."\n"; -if (($data !~ /^{/) && ($data !~ /}$/)){ - print "no data input!\n"; - exit(2); -} -if (-e $pdfout){ - unlink($pdfout); -} - -open(DATA,$templatefile); -while (my $l = ){ - $strpdfdata .= $l; -} -close(DATA); -my $templatedata = JSON::PP::decode_json($strpdfdata); - -my $pdfdata->{section} = $templatedata->{section}; - -$pdfdata->{data} = &mergedata(decode("utf-8",JSON::PP::encode_json($templatedata->{data}->{$lang})),$data); - -# exit(0); -$pdf->preferences({-fitwindow => 1}); - - - -my $page = $pdf->page(); -$page->mediabox('A4'); - my $endpoints = (); -foreach my $s (sort keys(%{$pdfdata->{section}})){ - $endpoints->{$s} = {final_y => '', lastpage => 1,pages => 1}; - if ($pdfdata->{section}->{$s}->{type} eq "image"){ - #print "Add Image $s\n"; - - $endpoints->{$s}->{y} = &addimage($pdf,$page,$pdfdata->{section}->{$s},$pdfdata->{data}->{$s}); - } elsif ($pdfdata->{section}->{$s}->{type} eq "line"){ - #print "Add Line $s\n"; - $endpoints->{$s}->{y} = &addline($page,$pdfdata->{section}->{$s}); - } elsif ($pdfdata->{section}->{$s}->{type} eq "text"){ - #print "Add Text $s\n"; - $endpoints->{$s}->{y} = &addtext($pdf,$page,$pdfdata->{section}->{$s},$pdfdata->{data}->{$s}); - } elsif ($pdfdata->{section}->{$s}->{type} eq "table"){ - #print "Add Table $s\n"; - ($endpoints->{$s}->{lastpage}, $endpoints->{$s}->{pages}, $endpoints->{$s}->{y}) = &addtable($pdf,$page,$pdfdata->{section}->{$s},$pdfdata->{data}->{$s}); - } -} - -$pdf->saveas($pdfout); -$pdf->end; - -#Functions +$| = 1; +# my $datafile=dirname($0).'/pdftest.pdf'; +GetOptions ("data|d=s" => \$xindata, "output|o=s" => \$pdfout, "lang|l=s" =>\$lang); -sub mergedata(){ +#FUNCTIONS BEGIN +sub mergedata($$){ my $strdtmpl = shift; - my $strdvalues = shift; - my $dvalues = JSON::PP::decode_json($strdvalues); + my $dvalues = shift; + #my $dvalues = JSON::PP::decode_json($strdvalues); foreach my $k (keys(%{$dvalues})){ @@ -113,20 +61,27 @@ sub mergedata(){ } } - my $dtmpl = JSON::PP::decode_json(encode("utf-8",$strdtmpl)); + my $dtmpl = decode_json(encode("utf-8",$strdtmpl)); return $dtmpl; } -sub addimage(){ +sub addimage($$$$){ my $pdf = shift; my $page = shift; my $section = shift; my $data = shift; my $gfx = $page->gfx(); + + if (! -e dirname($0).'/'.$data->{src}){ + # print "Image:".dirname($0).'/'.$data->{src}." does not exist!
"; + return $section->{y}; + } + $data->{src} = dirname($0).'/'.$data->{src}; + # print "Image datasource:".$data->{src}.'
'; my ($iw, $ih) = imgsize($data->{src}); + my $nimgw = $section->{width}; my $nimgh = ($nimgw/$iw) * $ih; - #print "New Imagesize: w:".$nimgw." h:".$nimgh."\n"; $gfx->translate($section->{x},$section->{y}); my $img = $pdf->image_png($data->{src}); $gfx->image($img,0,0,$nimgw,$nimgh); @@ -135,7 +90,7 @@ sub addimage(){ return $nimgh + $section->{y}; } -sub addline(){ +sub addline($$){ my $page = shift; my $section = shift; my $line = $page->gfx(); @@ -149,7 +104,7 @@ sub addline(){ return $section->{end}->{y}; } -sub addtext(){ +sub addtext($$$$){ my $pdf = shift; my $page = shift; my $section = shift; @@ -162,7 +117,6 @@ sub addtext(){ $section->{y} = $endpoints->{$sec}->{$k}; } } - #print "X:".$section->{x}." Y:".$section->{y}."\n"; $textobj->translate($section->{x}, $section->{y}); $textobj->font($pdf->corefont($section->{font}->{name}),$section->{font}->{size}); foreach my $d (@{$data->{text}}){ @@ -180,7 +134,7 @@ sub addtext(){ return $section->{y}; } -sub addtable(){ +sub addtable($$$$){ my $pdf = shift; my $page = shift; my $section = shift; @@ -196,7 +150,6 @@ sub addtable(){ $section->{start_y} = $endpoints->{$sec}->{$k}; } } - #print "HeadProps\n"; if (exists($section->{header_props})){ foreach (my $i=0;$i{header_props}});$i++){ my $tmpdata = @{$section->{header_props}}[$i]; @@ -206,7 +159,7 @@ sub addtable(){ @{$section->{header_props}}[$i] = $tmpdata; } } - #print "ColProps\n"; + if (exists($section->{column_props})){ foreach (my $i=0;$i{column_props}});$i++){ my $tmpdata = @{$section->{column_props}}[$i]; @@ -216,11 +169,80 @@ sub addtable(){ @{$section->{column_props}}[$i] = $tmpdata; } } - #print Dumper($section); - #print ref %{$section}."\n"; + my ($lastpage, $tblpages, $final_y) = $pdftbl->table($pdf,$page, $data->{text}, %{$section}); return ($lastpage, $tblpages, $final_y); } +#FUNCTIONS END + + +if (! -e $templatefile){ + # print "file $templatefile does not exist!\n"; + exit(1); +} +$xindata =~ s/\r//g; +if (($xindata !~ /^{/) && ($xindata !~ /}$/)){ + # print "no data input!\n"; + exit(2); +} +if (-e $pdfout){ + unlink($pdfout); +} + +open(DATA,$templatefile); +while (my $l = ){ + $strpdfdata .= $l; +} +close(DATA); + +# print "Test get Template!\n"; +my $templatedata = decode_json($strpdfdata); +my $pdfdata->{section} = $templatedata->{section}; +my $decoded = decode("utf-8",encode_json($templatedata->{data}->{$lang})); +my $mergedata = (); +# eval { + $mergedata = decode_json($xindata); +# 1; +# } or do { +# my $e = $@; +# print "$e\n"; +# }; +$pdfdata->{data} = mergedata($decoded,$mergedata); +$pdf->preferences({-fitwindow => 1}); +# print "Start PDF creation!
"; + + +my $page = $pdf->page(); +$page->mediabox('A4'); + + +foreach my $s (sort keys(%{$pdfdata->{section}})){ + $endpoints->{$s} = {final_y => '', lastpage => 1,pages => 1}; + if ($pdfdata->{section}->{$s}->{type} eq "image"){ + # print "Add Image $s
"; + + $endpoints->{$s}->{y} = addimage($pdf,$page,$pdfdata->{section}->{$s},$pdfdata->{data}->{$s}); + } elsif ($pdfdata->{section}->{$s}->{type} eq "line"){ + # print "Add Line $s
"; + $endpoints->{$s}->{y} = addline($page,$pdfdata->{section}->{$s}); + } elsif ($pdfdata->{section}->{$s}->{type} eq "text"){ + # print "Add Text $s
"; + $endpoints->{$s}->{y} = addtext($pdf,$page,$pdfdata->{section}->{$s},$pdfdata->{data}->{$s}); + } elsif ($pdfdata->{section}->{$s}->{type} eq "table"){ + # print "Add Table $s
"; + ($endpoints->{$s}->{lastpage}, $endpoints->{$s}->{pages}, $endpoints->{$s}->{y}) = addtable($pdf,$page,$pdfdata->{section}->{$s},$pdfdata->{data}->{$s}); + } +} +# print "save pdf to:".$pdfout."
"; +$pdf->saveas($pdfout); +$pdf->end; +if (! -e $pdfout){ + exit(3); +} + +#Functions + + diff --git a/lib/processdata.php b/lib/processdata.php index 727a4ac..411587c 100644 --- a/lib/processdata.php +++ b/lib/processdata.php @@ -22,16 +22,8 @@ save_rendezvous($pdt); break; case 'submit_profile': - //if (!isset($pdt["uservisibility"])) { $pdt["uservisibility"] = "";} save_data('users',$pdt); break; - // case 'submit_profileetude': - // if (!isset($pdt["companyvisibility"])) { $pdt["companyvisibility"] = "";} - // save_data('company',$pdt); - // break; - // case 'submit_deletecompanyprofile': - // delete_profile_company($pdt); - // break; case 'submit_deleteuserprofile': delete_profile_user($pdt); break; @@ -43,8 +35,6 @@ break; case 'submit_invoicedata': save_data('invoicedata',$pdt); - - // $pdfpath = create_pdf_invoice(); send_invoice(); break; @@ -231,28 +221,41 @@ } function send_invoice(){ - global $db,$msg,$user,$p; - $sql = "select * from invoicedata where id_user=".$user["id"]." order by invoicedate DESC,id DESC LIMIT 1;"; + global $db,$msg,$user,$p,$sp; + $sql = "select id, id_user, email, recipient, address, zip, city, countryshort, reference, pdfname,clientnumber,unit, + REPLACE(product,'\n','\\n') as product, + DATE_FORMAT(invoicedate, '%d.%m.%Y') as invoicedate, + DATE_FORMAT(reminderdate,'%d.%m.%Y') as reminderdate, + FORMAT(quantity, 0) as quantity, + REPLACE(FORMAT(unitamount, 2),'.',',') as unitamount, + REPLACE(FORMAT(netamount, 2),'.',',') as netamount, + REPLACE(FORMAT(sumnetamount, 2),'.',',') as sumnetamount, + REPLACE(FORMAT(vatpercent * 100, 2),'.',',') as vatpercent, + REPLACE(FORMAT(sumvatamount, 2),'.',',') as sumvatamount, + REPLACE(FORMAT(sumgrossamount, 2),'.',',') as sumgrossamount, + REPLACE(FORMAT(payedamount, 2),'.',',') as payedamount from invoicedata where id_user=".$user["id"]." order by invoicedate DESC,id DESC LIMIT 1;"; $pdfdata = $db->dbquery($sql); $pdffile = create_pdfinvoice($pdfdata); $msg .= '
'.$pdffile; - // if ($pdffile != ""){ - // $send = sendemail('user_sendinvoice',array("email" => $email),array($pdffile)); - // if ($send == 0){ - // $msg = '
Nous vous avons envoyé un email avec la facture à payer!
'; - // } else { - // $msg = '
ERROR d\'envoie, reessayer plus tard!
'; - // } - // } else { - // $msg = '
ERREUR!, reessayer plus tard
'; - // } - $p = 'profile/payement'; + if ($pdffile != ""){ + $send = sendemail('user_invoice',array("email" => $user["useremail"],"id" => $user["id"]),array($pdffile)); + if ($send == 0){ + $msg = '
Nous vous avons envoyé un email avec la facture à payer!
'; + } else { + $msg = '
ERROR d\'envoie, reessayer plus tard!
'; + } + } else { + $msg = '
ERREUR!, reessayer plus tard
'; + } + $p = 'profile'; + $sp = 'profile/payement'; } function create_pdfinvoice($idata){ global $baseurl,$msg; $idata["recipient"] = str_replace("'","\\'",$idata["recipient"]); $idata["recipient"] = str_replace('"','\\"',$idata["recipient"]); + $invlang="fr"; $invdata='{ "CURRENCY":"€", @@ -280,19 +283,20 @@ "SUMVATAMOUNT":"'.$idata["sumvatamount"].'", "SUMGROSSAMOUNT":"'.$idata["sumgrossamount"].'" }'; - + $invdata = str_replace("\n",'',$invdata); + $invdata = str_replace("\r",'',$invdata); $pdffile=$idata["pdfname"]; - $cmd = "perl -c ".$_SERVER['DOCUMENT_ROOT'].''.$baseurl.'/lib/createpdfA4invoice'; + $cmd = "perl ".$_SERVER['DOCUMENT_ROOT'].''.$baseurl.'/lib/createpdfA4invoice'; $cmd .= ' -d \''.$invdata.'\''; $cmd .= ' -o "'.$_SERVER['DOCUMENT_ROOT'].''.$baseurl.'/data/invoices/'.$pdffile.'"'; $cmd .= ' -l "'.$invlang.'"'; $status = 1; - - $ret = shell_exec(); - $msg .= '
'.$ret.'
'.$status.'
'.$cmd; + // $msg .= '
'.$cmd; + $ret = system($cmd,$status); + // $msg .= "$ret"; if ($status == 0){ - return $_SERVER['DOCUMENT_ROOT'].''.$baseurl.'/data/invoices/'.$pdffile; + return $_SERVER['DOCUMENT_ROOT'].''.$baseurl.'/data/invoices/'.$pdffile; } return ""; } diff --git a/lib/sendemail.php b/lib/sendemail.php index 40c077f..eb965e6 100644 --- a/lib/sendemail.php +++ b/lib/sendemail.php @@ -1,6 +1,6 @@ \ No newline at end of file -- 2.39.5