send invoice
authorKilian Saffran <ksaffran@dks.lu>
Fri, 15 Mar 2019 14:44:59 +0000 (15:44 +0100)
committerKilian Saffran <ksaffran@dks.lu>
Fri, 15 Mar 2019 14:44:59 +0000 (15:44 +0100)
index.php
lib/createpdfA4invoice
lib/processdata.php
lib/sendemail.php
tmpl/pages/profile/payement.php

index 354dd72..97c6ef4 100644 (file)
--- a/index.php
+++ b/index.php
@@ -66,7 +66,7 @@
 <main role="main">
   <div class="container-fluid">
       <?php 
-      //print_r($_POST);
+      // echo '<pre>'.$p.'</pre>';
       if ($msg != null){
         echo $msg;
       }
index cd525f9..0c569ab 100644 (file)
@@ -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 = <DATA>){
-  $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!<br/>";
+    return $section->{y};
+  }
+  $data->{src} = dirname($0).'/'.$data->{src};
+  # print "Image datasource:".$data->{src}.'<br/>';
   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<scalar(@{$section->{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<scalar(@{$section->{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 = <DATA>){
+  $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!<br/>";
+
+
+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<br/>";
+    
+    $endpoints->{$s}->{y} = addimage($pdf,$page,$pdfdata->{section}->{$s},$pdfdata->{data}->{$s});
+  } elsif ($pdfdata->{section}->{$s}->{type} eq "line"){
+    # print "Add Line $s<br/>";
+    $endpoints->{$s}->{y} = addline($page,$pdfdata->{section}->{$s});
+  } elsif ($pdfdata->{section}->{$s}->{type} eq "text"){
+    # print "Add Text $s<br/>";
+    $endpoints->{$s}->{y} = addtext($pdf,$page,$pdfdata->{section}->{$s},$pdfdata->{data}->{$s});
+  } elsif ($pdfdata->{section}->{$s}->{type} eq "table"){
+    # print "Add Table $s<br/>";
+    ($endpoints->{$s}->{lastpage}, $endpoints->{$s}->{pages}, $endpoints->{$s}->{y}) = addtable($pdf,$page,$pdfdata->{section}->{$s},$pdfdata->{data}->{$s});
+  }
+}
+# print "save pdf to:".$pdfout."<br/>";
+$pdf->saveas($pdfout);
+$pdf->end;
+if (! -e $pdfout){
+  exit(3);
+}
+
+#Functions
+
+
 
 
 
index 727a4ac..411587c 100644 (file)
                     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;
                   
     }
 
     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 .= '<br/>'.$pdffile;
-      // if ($pdffile != ""){
-      //   $send = sendemail('user_sendinvoice',array("email" => $email),array($pdffile));
-      //   if ($send == 0){
-      //           $msg = '<div class="alert alert-success">Nous vous avons envoyé un email avec la facture à payer!</div>';
-      //       } else {
-      //           $msg = '<div class="alert alert-danger">ERROR d\'envoie, reessayer plus tard!</div>';
-      //       }
-      // } else {
-      //   $msg = '<div class="alert alert-danger">ERREUR!, reessayer plus tard</div>';
-      // }
-      $p = 'profile/payement';
+      if ($pdffile != ""){
+         $send = sendemail('user_invoice',array("email" => $user["useremail"],"id" => $user["id"]),array($pdffile));
+         if ($send == 0){
+                 $msg = '<div class="alert alert-success">Nous vous avons envoyé un email avec la facture à payer!</div>';
+             } else {
+                 $msg = '<div class="alert alert-danger">ERROR d\'envoie, reessayer plus tard!</div>';
+             }
+       } else {
+         $msg = '<div class="alert alert-danger">ERREUR!, reessayer plus tard</div>';
+      }
+      $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":"€",
   "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 .= '<br/>'.$ret.'<br/>'.$status.'<br/>'.$cmd;
+  // $msg .= '<br/>'.$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 "";
   }
index 40c077f..eb965e6 100644 (file)
@@ -1,6 +1,6 @@
 <?php
      
-    function sendemail($template,$data){
+    function sendemail($template,$data,$attach=null){
         global $db,$baseurl;
         $body = "";
         $subject = "";
             $cmd .= '-o tls=auto ';
             $cmd .= '-o message-content-type=html ';
             $cmd .= '-o message-charset=utf-8 ';
+            if ($attach){
+              $cmd .= " -a";
+              foreach ($attach as $a){
+                $cmd .= " ".$a." ";
+              }
+            }
             #$cmd .= '-o message-header="Content-Type: text/html; charset=utf-8" ';
             $cmd .= '-s mail.your-server.de:587 -xu "ksaffran@dks.lu" -xp "FB1ia1ka" -q';
             $status = 1;
index e69de29..15c5adc 100644 (file)
@@ -0,0 +1,3 @@
+<?php
+
+?>
\ No newline at end of file