]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/squid-accounting/acct-lib.pl
Merge remote-tracking branch 'ummeegge/OpenVPN_validating_N2N' into next
[people/pmueller/ipfire-2.x.git] / src / squid-accounting / acct-lib.pl
CommitLineData
db8a01e0
AM
1#!/usr/bin/perl
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2014 IPFire Team <alexander.marx@ipfire.org> #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21package ACCT;
22
23use DBI;
24use POSIX;
25use Time::Local;
26use PDF::API2;
27use utf8;
28use Encode;
29use File::Copy;
a5f5ccfc 30use File::Temp qw/ tempfile tempdir /;
db8a01e0
AM
31
32###############################################################################
33my $dbh;
34my $dsn="dbi:SQLite:dbname=/var/ipfire/accounting/acct.db";
35my ($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst)=localtime();
36my %mainsettings;
37###############################################################################
38
39&General::readhash("/var/ipfire/main/settings", \%mainsettings);
40my $uplang=uc($mainsettings{'LANGUAGE'});
41
42#############
43# Functions #
44#############
45
46sub connectdb {
47 $dbh = DBI->connect($dsn, "", "",{RaiseError => 1, AutoCommit => 1})or die "ERROR $!";
48 return $dbh;
49}
50
51sub closedb {
52 $dbh->disconnect();
53 return $dbh;
54}
55
56sub getminmax {
57 my $min;
58 my $max;
59 $dbh=&connectdb;
60 my $sth = $dbh->prepare('Select min(TIME_RUN),max(TIME_RUN) from ACCT;');
61 $sth->execute;
62 while ( my @row = $sth->fetchrow_array ) {
63 $min=$row[0];
64 $max=$row[1];
65 }
66 $dbh->disconnect();
67 return ($min,$max);
68}
69
70sub cleardbtraf {
71 &connectdb;
72 $dbh->do("DELETE FROM ACCT;");
73 $dbh->do("DELETE FROM ACCT_HIST;");
74 &closedb;
75}
76
77sub cleardb {
78 &connectdb;
79 $dbh->do("DELETE FROM ACCT;");
80 $dbh->do("DELETE FROM ACCT_HIST;");
81 $dbh->do("DELETE FROM ACCT_ADDR ");
82 $dbh->do("DELETE FROM BILLINGGRP");
83 $dbh->do("DELETE FROM BILLINGHOST");
84 &closedb;
85}
86
87sub delbefore {
88 my $till=$_[0];
89 &connectdb;
90 $dbh->do("DELETE FROM ACCT WHERE TIME_RUN < ".$till.";");
91 $dbh->do("DELETE FROM ACCT_HIST WHERE TIME_RUN < date('".$till."','unixepoch');");
92 &closedb;
93}
94
95sub movedbdata {
96 $dbh->do("insert into ACCT_HIST select datetime(TIME_RUN,'unixepoch'),NAME,SUM(BYTES) from ACCT where date(TIME_RUN,'unixepoch') < date('now','-2 months') group by NAME,date(TIME_RUN,'unixepoch');");
97 $dbh->do("DELETE FROM ACCT WHERE datetime(TIME_RUN,'unixepoch') < date('now','-2 months');");
98}
99
100sub gethourgraphdata {
101 my $table=$_[0];
102 my $from=$_[1];
103 my $till=$_[2];
104 my $name=$_[3];
105 my $res;
106 $dbh=connectdb;
107 if ($table eq 'ACCT'){
108 $res = $dbh->selectall_arrayref( "SELECT TIME_RUN,BYTES FROM ACCT WHERE TIME_RUN BETWEEN ".$from." AND ".$till." AND NAME = '".$name."';");
109 }else{
110 $res = $dbh->selectall_arrayref( "SELECT TIME_RUN,BYTES FROM ACCT_HIST WHERE TIME_RUN BETWEEN date(".$from.",'unixepoch') AND date(".$till.",'unixepoch') AND NAME = '".$name."';");
111 }
112 return $res;
113}
114
115sub getmonthgraphdata {
116 my $table=$_[0];
117 my $from=$_[1];
118 my $till=$_[2];
119 my $name=$_[3];
120 my $res;
121 $dbh=connectdb;
122 if ($table eq 'ACCT'){
123 $res = $dbh->selectall_arrayref( "SELECT strftime('%d.%m.%Y',xx.tag),(SELECT SUM(BYTES)/1024/1024 FROM ACCT WHERE date(TIME_RUN,'unixepoch') <= xx.tag and NAME = '".$name."') kum_bytes FROM (SELECT date(TIME_RUN,'unixepoch') tag,SUM(BYTES)/1024/1024 sbytes FROM ACCT WHERE NAME='".$name."' and TIME_RUN between ".$from." and ".$till." GROUP by date(TIME_RUN,'unixepoch')) xx;");
124 }else{
125 $res = $dbh->selectall_arrayref( "SELECT TIME_RUN, (SELECT SUM(BYTES)/1024/1024 FROM ACCT_HIST WHERE TIME_RUN <= ah.TIME_RUN and NAME = '".$name."') kum_bytes FROM ACCT_HIST ah WHERE TIME_RUN BETWEEN date(".$from.",'unixepoch') AND date(".$till.",'unixepoch') AND NAME = '".$name."' group by TIME_RUN;");
126 }
127 $dbh=closedb;
128 return $res;
129}
130
131sub writeaddr {
132 my $comp = $_[0];
133 my $type = $_[1];
134 my $name1 = $_[2];
135 my $str = $_[3];
136 my $nr = $_[4];
137 my $post = $_[5];
138 my $city = $_[6];
139 my $bank = $_[7];
140 my $iban = $_[8];
141 my $bic = $_[9];
142 my $blz = $_[10];
143 my $kto = $_[11];
144 my $mail = $_[12];
145 my $inet = $_[13];
146 my $hrb = $_[14];
147 my $ustid = $_[15];
148 my $tel = $_[16];
149 my $fax = $_[17];
150 $dbh=&connectdb;
151 #COMPANY,TYPE,NAME1,STR,NR,POSTCODE,CITY,BANK,IBAN,BLZ,ACCOUNT,EMAIL,INTERNET,HRB,USTID,TEL,FAX
152 my $sql = "INSERT INTO ACCT_ADDR (COMPANY,TYPE,NAME1,STR,NR,POSTCODE,CITY,BANK,IBAN,BIC,BLZ,ACCOUNT,EMAIL,INTERNET,HRB,USTID,TEL,FAX) VALUES ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
153 my $sth = $dbh->prepare( $sql );
154 $sth->execute( $comp,$type,$name1,$str,$nr,$post,$city,$bank,$iban,$bic,$blz,$kto,$mail,$inet,$hrb,$ustid,$tel,$fax );
155 $dbh=&closedb;
156}
157
158sub updateaddr {
159 my $type = $_[0];
160 my $comp = $_[1];
161 my $name1 = $_[2];
162 my $str = $_[3];
163 my $nr = $_[4];
164 my $post = $_[5];
165 my $city = $_[6];
166 my $bank = $_[7];
167 my $iban = $_[8];
168 my $bic = $_[9];
169 my $blz = $_[10];
170 my $kto = $_[11];
171 my $mail = $_[12];
172 my $inet = $_[13];
173 my $hrb = $_[14];
174 my $ustid = $_[15];
175 my $tel = $_[16];
176 my $fax = $_[17];
177 my $oldname = $_[18];
178 $dbh=&connectdb;
179 $dbh->do("UPDATE ACCT_ADDR SET COMPANY=?,TYPE=?,NAME1=?,STR=?,NR=?,POSTCODE=?,CITY=?,BANK=?,IBAN=?,BIC=?,BLZ=?,ACCOUNT=?,EMAIL=?,INTERNET=?,HRB=?,USTID=?,TEL=?,FAX=? WHERE COMPANY=?", undef,$comp,$type,$name1,$str,$nr,$post,$city,$bank,$iban,$bic,$blz,$kto,$mail,$inet,$hrb,$ustid,$tel,$fax,$oldname)or die "Could not UPDATE Address.";
180 $dbh=&closedb;
181}
182
183sub deladdr {
184 my $comp = $_[0];
185 $dbh=&connectdb;
186 $dbh->do("DELETE FROM ACCT_ADDR WHERE COMPANY=?", undef,$comp )or die "Could not delete address $comp!";
187 $dbh=&closedb;
188}
189
190sub getaddresses {
191 $dbh=&connectdb;
192 my $res=$dbh->selectall_arrayref("SELECT (SELECT COUNT(NAME) FROM BILLINGGRP GROUP BY NAME),* FROM ACCT_ADDR ORDER BY COMPANY;");
193 $dbh=&closedb;
194 return $res;
195}
196
197sub gethosts{
198 $dbh=&connectdb;
199 my $res =$dbh->selectall_arrayref("SELECT NAME from ACCT GROUP BY NAME ORDER BY NAME;");
200 $dbh=&closedb;
201 return $res;
202}
203
204sub getbillgroups {
205 $dbh=connectdb;
206 my $res=$dbh->selectall_arrayref("SELECT NAME,HOST,CUST,BILLTEXT,(SELECT COUNT(HOST) FROM BILLINGHOST WHERE BILLINGGRP.NAME=BILLINGHOST.GRP),CENT FROM BILLINGGRP ORDER BY NAME;");
207 return $res;
208 $dbh->disconnect();
209}
210
211sub getextrabillpos {
212 my $grp=$_[0];
213 $dbh=&connectdb;
214 my $res=$dbh->selectall_arrayref("SELECT * from BILLPOS WHERE GRP =?", undef,$grp);
215 $dbh=&closedb;
216 return $res;
217}
218
219sub savebillgroup {
220 my $grp=$_[0];
221 my $txt=$_[1];
222 my $host=$_[2];
223 my $cust=$_[3];
224 my $ust=$_[4];
225 my @ips=@{$_[5]};
226 $dbh=&connectdb;
227 my $sql = "INSERT INTO BILLINGGRP (NAME,BILLTEXT,HOST,CUST,CENT) VALUES (?,?,?,?,?)";
228 my $sth = $dbh->prepare( $sql );
229 $sth->execute( $grp,$txt,$host,$cust,$ust );
230 foreach my $ip (@ips){
231 my $sql = "INSERT INTO BILLINGHOST (GRP,HOST) VALUES (?,?)";
232 my $sth = $dbh->prepare( $sql ) or die "Could not prepare insert into BILLINGHOST $!";
233 $sth->execute( $grp,$ip ) or die "Could not execute INSERT into BILLINGHOST $!";
234 }
235 $dbh=&closedb;
236}
237
238sub updatebillgrouphost {
239 my $oldgrp=$_[0];
240 my $newgrp=$_[1];
241 $dbh=&connectdb;
242 my $sql = "UPDATE BILLINGGRP SET HOST=? WHERE HOST=?;";
243 my $sth = $dbh->prepare( $sql );
244 $sth->execute( $newgrp,$oldgrp );
245 $dbh=&closedb;
246}
247
248sub updatebillgroupcust {
249 my $oldgrp=$_[0];
250 my $newgrp=$_[1];
251 $dbh=&connectdb;
252 my $sql = "UPDATE BILLINGGRP SET CUST=? WHERE CUST=?;";
253 my $sth = $dbh->prepare( $sql );
254 $sth->execute( $newgrp,$oldgrp );
255 $dbh=&closedb;
256}
257
258sub deletebillgroup {
259 my $name=shift;
260 $dbh=connectdb;
261 $dbh->do("DELETE FROM BILLINGGRP WHERE NAME=?;", undef,$name);
262 $dbh->do("DELETE FROM BILLINGHOST WHERE GRP=?;", undef,$name);
263 &closedb;
264}
265
266sub savebillpos {
267 my $grp=$_[0];
268 my $amnt=$_[1];
269 my $pos=$_[2];
270 my $price=$_[3];
271 $dbh=&connectdb;
272 my $sql = "INSERT INTO BILLPOS (GRP,AMOUNT,POS,PRICE) VALUES (?,?,?,?)";
273 my $sth = $dbh->prepare( $sql )or die "Could not prepare insert into BILLINGPOS $!";
274 $sth->execute( $grp,$amnt,$pos,$price ) or die "Could not execute INSERT into BILLINGHOST $!";
275 $dbh->disconnect();
276}
277
278sub updatebillpos {
279 my $oldgrp=shift;
280 my $newgrp=shift;
281 $dbh=&connectdb;
282 my $sql = "UPDATE BILLPOS SET GRP=? WHERE GRP=?;";
283 my $sth = $dbh->prepare( $sql );
284 $sth->execute( $newgrp,$oldgrp );
285 my $sql1 = "UPDATE BILLS SET GRP=? WHERE GRP=?;";
286 my $sth1 = $dbh->prepare( $sql1 );
287 $sth1->execute( $newgrp,$oldgrp );
288 $dbh=&closedb;
289 #Now rename directories
290 rename ("/srv/web/ipfire/html/accounting/logo/$oldgrp","/srv/web/ipfire/html/accounting/logo/$newgrp");
291 rename ("/var/ipfire/accounting/bill/$oldgrp","/var/ipfire/accounting/bill/$newgrp")
292
293}
294
295sub delbillpos_single {
296 my $pos=$_[0];
297 my $grp=$_[1];
298 my $sql = "DELETE FROM BILLPOS WHERE GRP=? AND POS=?;";
299 $dbh=&connectdb;
300 my $sth = $dbh->prepare( $sql )or die "Could not prepare DELETE POS from BILLINGPOS $!";
301 $sth->execute( $grp,$pos ) or die "Could not execute DELETE from BILLINGHOST $!";
302 $dbh=&closedb;
303}
304
305sub delbillpos {
306 my $grp=$_[0];
307 my $sql = "DELETE FROM BILLPOS WHERE GRP=?;";
308 $dbh=&connectdb;
309 my $sth = $dbh->prepare( $sql )or die "Could not prepare DELETE POS from BILLINGPOS $!";
310 $sth->execute( $grp ) or die "Could not execute DELETE from BILLINGHOST $!";
311 $dbh=&closedb;
312}
313
314sub listhosts{
315 my $name=$_[0];
316 my $a;
317 my $res=$dbh->selectall_arrayref("SELECT * FROM BILLINGHOST WHERE GRP='".$name."';");
318 foreach my $gzu (@$res){
319 my ($x,$y)=@$gzu;
320 $a.= "|$y";
321 }
322 return $a;
323}
324
325sub checkusergrp {
326 $dbh=connectdb;
327 my $res=$dbh->selectall_arrayref("SELECT * FROM BILLINGHOST;");
328 $dbh->disconnect();
329 return $res;
330}
331
332sub getmonth{
333 #GET : 1. month 2. year
334 #GIVES: 1.day of given month AND last day of given month in seconds since 1.1.1970
335 ($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst)=localtime();
336 my $jahr=$_[1];
337 my $monat=$_[0]-1 if($_[0]);
338 my $tag=1;
339 my $time1=timelocal(0,0,0,$tag,$monat,$jahr);
ddfc0693
AM
340 my $time2=0;
341 if (($monat+1) == 12){
342 $time2=timelocal(0,0,0,$tag,0,$jahr+1);
343 }else{
344 $time2=timelocal(0,0,0,$tag,$monat+1,$jahr);
345 }
db8a01e0
AM
346 --$time2;
347 return ($time1,$time2);
348}
349
350sub GetTaValues {
351 $dbh=&connectdb;
352 my $from = $_[0]; #unixtimestamp
353 my $till = $_[1]; #unixtimestamp
354 my $grp = $_[2]; #Billgroupname
355 my $all = $dbh->selectall_arrayref("SELECT bh.HOST,SUM(ac.BYTES) sbytes,bh.GRP FROM ACCT ac ,BILLINGHOST bh WHERE ac.NAME=bh.HOST AND bh.GRP=? AND ac.TIME_RUN between ? AND ? GROUP BY bh.GRP,bh.HOST;", undef, $grp, $from, $till) or die "Could not fetch Groupdata $!";
356 my $nri1 = @$all;
357 my @return;
358 my $cnt=0;
359 if ($nri1 eq "0"){
360 $return[$cnt]="999";
361 }
362 else
363 {
364 foreach my $row (@$all){
365 my ($bytes,$billgrp,$host) = @$row;
366 $return[$cnt]="$bytes,$billgrp,$host";
367 $cnt++;
368 }
369 }
370 &closedb;
371 return @return;
372}
373
374sub getTaAddress {
375 my $grp=$_[0];
376 my $type=$_[1];
377 $dbh=&connectdb;
378 my $res = $dbh->selectall_arrayref("select * from ACCT_ADDR,BILLINGGRP where (BILLINGGRP.HOST=ACCT_ADDR.COMPANY AND BILLINGGRP.NAME=? AND ACCT_ADDR.TYPE=?) or (BILLINGGRP.CUST=ACCT_ADDR.COMPANY and BILLINGGRP.NAME=? AND ACCT_ADDR.TYPE=?);", undef, $grp,$type,$grp,$type);
379 &closedb;
380 return $res;
381}
382
383sub checkbillgrp {
384 my $comp=$_[0];
385 $dbh=&connectdb;
386 my $res=$dbh->selectall_arrayref("SELECT NAME,HOST,CUST FROM BILLINGGRP;");
387 &closedb;
388 return $res;
389}
390
391sub pdf2 {
392 my @billar = @{$_[0]}; #DATA from sendbill (just host/values)
393 my $month = $_[1];
394 $month = '0'.$month if $month < 10;
395 my $year = $_[2];
396 my $mwst = $_[3];
397 my @address_cust= @{$_[4]}; #Array which contains customer and hoster adresses and some additional info from billgroup
398 my @address_host= @{$_[5]};
399 my @billpos = @{$_[6]};
400 my $grp = $_[7];
401 my $cur = $_[8]; #(Eur,USD,...)
402 my $preview = $_[9];
403 my $no = &getBillNr;
404 my $name = $month."-".$year."-".$no.".pdf";
405 my $path ="/var/ipfire/accounting/bill/";
406 my $filename = "$path/$grp/$name";
407 my @summen; #Used for counting the sums
408 my $x = 500;
409 my $y = 1;
410 my $zwsum;
411 my $pages = 0;
412 my $anzbillpos = @billpos;
413 my $anz = (@billar+$anzbillpos)/18; #Total pages
414 $anz = ceil($anz); #round the $anz value
415 my $aktpage=1;
416 my $sum=0;
417 my $sum1=0;
418 my $lines;
419 my $title;
420 my $txt;
421 my $txt1;
422 my $txt2;
423 my $txt3;
424 my $txt4;
425 my $txt5;
426 my $fnt;
427 my $fnt1;
428 my $fulldate = strftime('%d.%m.%Y',localtime(time()));
429 my($company_host,$type_host,$name1_host,$str_host,$str_nr_host,$plz_host,$city_host,$bank,$iban,$bic,$blz,$kto,$email,$internet,$hrb,$stnr,$tel_host,$fax_host,$ccmail,$billgrp,$text,$host,$cust,$cent);
430 my($company_cust,$type_cust,$name1_cust,$str_cust,$str_nr_cust,$plz_cust,$city_cust);
431
432 #First of all check if directory exists, else create it
433 if(! -d "$path/$grp" && $preview ne 'on'){
434 mkdir("$path/$grp",0777);
435 }
436
437 #Check if we are creating a preview or a real bill
438 if($preview eq 'on'){
a5f5ccfc 439 $filename="$path/".tempfile( SUFFIX => ".pdf", );
db8a01e0
AM
440 }
441 ####################################################################
442 #Prepare DATA from arrays
443 ####################################################################
444 #Get HOSTER for this grp
445 foreach my $addrline (@address_host){
446 ($company_host,$type_host,$name1_host,$str_host,$str_nr_host,$plz_host,$city_host,$bank,$iban,$bic,$blz,$kto,$email,$internet,$hrb,$stnr,$tel_host,$fax_host,$ccmail,$billgrp,$text,$host,$cust,$cent)=@$addrline;
447 }
448 #Get CUST for this grp
449 foreach my $addrline_cust (@address_cust){
450 ($company_cust,$type_cust,$name1_cust,$str_cust,$str_nr_cust,$plz_cust,$city_cust)=@$addrline_cust;
451 }
452
453
454 #Generate PDF File
455 my $pdf = PDF::API2->new(-file => $filename);
456 $pdf->mediabox('A4');
457 my $page = $pdf->page;
458 $fnt = $pdf->corefont('Helvetica');
459 $fnt1 = $pdf->corefont('HelveticaBold');
460
461 #Set lines
462 $lines = $page->gfx;
463 $title = $page->gfx;
464 $lines->strokecolor('grey');
465 $lines->linewidth('0.5');
466
467 #Fill BILL DATA into PDF
db8a01e0
AM
468 foreach (@billar) {
469 my ($a1,$a2) = split( /\,/, $_ );
470 $a2=sprintf"%.2f",($a2/1024/1024);
471 my $sum=(($a2)*$cent);
472 $sum = sprintf"%.2f",($sum);
473 # Seitenwechsel ermitteln
474 if ($y % 18 == 0) {
475 $txt1->translate(390, 120);
476 $txt1->text($Lang::tr{'acct pdf zwsum'}); #Pos
477 $zwsum=sprintf("%.2f",($zwsum));
478 $txt1->translate(540, 120);
479 $txt1->text_right("$zwsum".decode('utf8',$cur)); #Pos
480 $zwsum=0;
481 $pages++;
482 $aktpage++;
483 $x=500;
484 $page=$pdf->page;
485 #draw lines
486 $lines = $page->gfx;
487 $title = $page->gfx;
488 $lines->strokecolor('grey');
489 $lines->linewidth('0.5');
490 }
491
492 #TITLES
493 $title->linewidth(14);
494 $title->move(385, 168);
495 $title->line(545, 168); #Title of SUMBOX
496 $title->move(60, 523);
497 $title->line(545, 523);#Bottom horiz. line of Title
498
499 # Generate Tables
500 $lines->move(59, 745);
501 $lines->line(545, 745);
502 $lines->move(59, 563);
503 $lines->line(545, 563);
504
505 # Addressbox
506 $lines->move(61, 710);
507 $lines->line(61, 715, 66, 715); #TL
508 $lines->move(61, 610);
509 $lines->line(61, 605, 66, 605); #BL
510 $lines->move(285, 715);
511 $lines->line(290, 715, 290, 710); #TR
512 $lines->move(290, 610);
513 $lines->line(290, 605, 285, 605); #BR
514
515 # Table for positions
516 $lines->move(60, 530);
517 $lines->line(60, 200); #First vert. line POS
518 $lines->move(90, 523);
519 $lines->line(90, 200); #Second vert. line
520 $lines->move(280, 523);
521 $lines->line(280, 200); #third vert. line
522 $lines->move(385, 523);
523 $lines->line(385, 200); #third vert. line
524 $lines->move(430, 523);
525 $lines->line(430, 200); #fourth vert. line
526 $lines->move(545, 530);
527 $lines->line(545, 200); #fifth vert. line
528 $lines->move(60, 200);
529 $lines->line(545, 200); #Bottom horizontal line
530
531 #SUM BOX
532 $lines->move(385, 175);
533 $lines->line(385, 115); #Left vert. line of SUMBOX
534 $lines->move(545, 175);
535 $lines->line(545, 115); #Right vert. line of SUMBOX
536 $lines->move(385, 115);
537 $lines->line(545, 115); #Bottom horiz. line of SUMBOX
538
539 #Lines on right side after sender and after "bank"
540 $lines->move(420, 723);
541 $lines->line(545, 723);# Line "Sender"
542 $lines->move(420, 648);
543 $lines->line(545, 648);# Line "Bank"
544 $lines->move(420, 600);
545 $lines->line(545, 600);# Line HRB/USTID
546
547 #Make lines Visible
548 $lines->stroke;
549 $title->stroke;
550 if (-f "/srv/web/ipfire/html/accounting/logo/$grp/logo.png"){
551 #Image LOGO
552 my $gfx = $page->gfx;
553 my $image = $pdf->image_png("/srv/web/ipfire/html/accounting/logo/$grp/logo.png");
554 my $width= $image->width;
555 my $height= $image->height;
556 $gfx->image($image, (545+($width/2))-$width, 750,0.5);
557 }
558
559 #Set Fonts
560 $txt = $page->text;
561 $txt1 = $page->text;
562 $txt2 = $page->text;
563 $txt3 = $page->text;
564 $txt4 = $page->text;
565 $txt5 = $page->text;
566 $txt->textstart; #Begin Text
567 $txt->font($fnt, 10); #Set fontsize for font1
568 $txt1->font($fnt, 8); #Set fontsize for font2
569 $txt2->font($fnt1, 10); #Set fontsize for font3
570 $txt3->font($fnt1, 16); #Set fontsize for font4
571 $txt4->font($fnt, 6); #Set fontsize for font5
572 $txt5->font($fnt1, 6); #Set fontsize for font6
573
574 #if $cent not set, set it to 0.5
575 if(!$cent){$cent='0.005';}
576
577 #if MWst not set, set it to 19%
578 if(!$mwst){$mwst='19';}
579
580 # Titles
581 $txt1->translate(65,520);
582 $txt1->text($Lang::tr{'acct pos'}); #Pos
583 $txt1->translate(95, 520);
584 $txt1->text($Lang::tr{'acct name'}); #Host/Name
585 $txt1->translate(285, 520);
586 $txt1->text($Lang::tr{'acct amount'}); #Traffic
587 $txt1->translate(390, 520);
588 $txt1->text($Lang::tr{'acct cent1'}); #Price /MB
589 $txt1->translate(435, 520);
590 $txt1->text($Lang::tr{'acct pdf price'}); #Sum
591
592 ####################################################################
593 #Fill Recipient address
594 my $rec_name= "$company_cust";
595 my $rec_name1="$name1_cust";
596 my $rec_str = "$str_cust $str_nr_cust";
597 my $rec_city = "$plz_cust $city_cust";
598 #INSERT RECIPIENT
599 my $o=675;
600 $txt2->translate(78, 685);
601 $txt2->text(decode('utf8',$rec_name));
602 if($rec_name1){
603 $txt1->translate(78, $o);
604 $txt1->text(decode('utf8',$rec_name1));
605 $o=$o-15;
606 }else{
607 $o=$o-15;
608 }
609 $txt1->translate(78, $o);
610 $txt1->text(decode('utf8',$rec_str));
611 $o=$o-10;
612 $txt1->translate(78, $o);
613 $txt1->text(decode('utf8',$rec_city));
614
615 # INSERT SENDER
616 my $send_name= "$company_host";
617 my $send_str = "$str_host $str_nr_host";
618 my $send_city = "$plz_host $city_host";
619 my $send_bank ="$bank";
620
621 $txt5->translate(420, 725);
622 $txt5->text(decode('utf8',$Lang::tr{'acct pdf prov'}));
623 $txt5->translate(420, 715);
624 $txt5->text(decode('utf8',$send_name));
625 my $j=705;
626 if($name1_host){
627 $txt4->translate(420, $j);
628 $txt4->text(decode('utf8',$name1_host));
629 $j=$j-8;
630 }
631 $txt4->translate(420, $j);
632 $txt4->text(decode('utf8',$send_str)); #STR
633 $j=$j-8;
634 $txt4->translate(420, $j);
635 $txt4->text(decode('utf8',$send_city)); #PLZ.City
636 #Print optional Values tel,fax
637 my $i=680;
638 if($tel_host){
639 $txt4->translate(420, $i);
640 $txt4->text($Lang::tr{'acct tel'}); #Tel
641 $txt4->translate(480, $i);
642 $txt4->text($tel_host); #Telnr
643 $i=$i-8;
644 }
645 if($fax_host){
646 $txt4->translate(420, $i);
647 $txt4->text($Lang::tr{'acct fax'}); #Fax
648 $txt4->translate(480, $i);
649 $txt4->text($fax_host); #Faxnr
650 $i=$i-8;
651 }
652 if($internet){
653 $txt4->translate(420, $i);
654 $txt4->text($Lang::tr{'acct inet'}); #Internet
655 $txt4->translate(480, $i);
656 $txt4->text($internet); #www-address
657 $i=$i-8;
658 }
659 $txt5->translate(420, 650);
660 $txt5->text(decode('utf8',$Lang::tr{'acct bank'})); #"BANK"
661 $txt4->lead(7);
662 $txt4->translate(420, 640);
663 $txt4->paragraph(decode('utf8',$bank), 130, 20, -align => "justify"); #Bankname
664 if($iban){
665 $txt4->translate(420, 625);
666 $txt4->text($Lang::tr{'acct iban'}); #iban
667 $txt4->translate(480, 625);
668 $txt4->text(decode('utf8',$iban)); #iban
669 $txt4->translate(420, 619);
670 $txt4->text($Lang::tr{'acct bic'}); #bic
671 $txt4->translate(480, 619);
672 $txt4->text(decode('utf8',$bic)); #bic
673 }
674 if($blz){
675 $txt4->translate(420, 613);
676 $txt4->text($Lang::tr{'acct blz'}); #blz
677 $txt4->translate(420, 607);
678 $txt4->text($Lang::tr{'acct kto'}); #kto
679 $txt4->translate(480, 613);
680 $txt4->text(decode('utf8',$blz)); #blz
681 $txt4->translate(480, 607);
682 $txt4->text(decode('utf8',$kto)); #kto
683 }
684
685 #Print USTID and optional HRB
686 $txt4->translate(420, 590);
687 $txt4->text($Lang::tr{'acct ustid'}); #USTID
688 $txt4->translate(480, 590);
689 $txt4->text($stnr); #ustid
690 if($hrb){
691 $txt4->translate(420, 580);
692 $txt4->text($Lang::tr{'acct hrb'}); #USTID
693 $txt4->translate(480, 580);
694 $txt4->text($hrb); #ustid
695 }
696 ################################################################
697
698 #Print Date, Pages ....
699 $txt3->translate(59, 545);
700 $txt3->text($Lang::tr{'acct pdf billtxt'});
701 $txt1->translate(160, 545);
702 $txt1->text("$no $Lang::tr{'acct billnr'}");
703 $txt1->translate(60, 532);
704 $txt1->text("$Lang::tr{'acct pdf time'} $month/$year");
705 $txt1->translate(545, 550);
706 $txt1->text_right("$Lang::tr{'acct pdf date'} $fulldate");
707 $txt1->translate(545, 532);
708 $txt1->text_right("$Lang::tr{'acct pdf page'} $aktpage / $anz");
709
710 if ($a1 eq '999'){last;}
711 #Print DATA from array to Position table
712 $txt1->translate(80, $x);
713 $txt1->text_right($y);
714 $txt1->translate(95, $x);
715 $txt1->text($a1);
716 $txt1->translate(380, $x);
717 $txt1->text_right("$a2 MB");
718 $txt1->translate(425, $x);
719 $txt1->text_right("$cent ".decode('utf8',$cur));
720 $txt1->translate(540, $x);
721 $txt1->text_right("$sum ".decode('utf8',$cur));
722
723 #Build SUMMARY
724 $summen[$y-1]="$y,$a2,$sum";
725 $zwsum=$zwsum+$sum;
726 $x=$x-15;
727 $y++;
728 }
729 #Print extra billpositions
730 foreach my $line (@billpos){
731 my ($grp,$amount,$art,$price)=@$line;
732 #Print DATA from array to Position table
733 $txt1->translate(80, $x);
734 $txt1->text_right($y);
735 $txt1->translate(95, $x);
736 $txt1->text(decode('utf8',$art));
737 $txt1->translate(380, $x);
738 $txt1->text_right($amount." pcs");
739 $txt1->translate(540, $x);
740 $txt1->text_right("$price ".decode('utf8',$cur));
741 #Build SUMMARY
742 my $zu=$amount * $price;
743 $summen[$y-1]="$y,'0',$zu";
744 $zwsum=$zwsum+$zu;
745 $x=$x-15;
746 $y++;
747 }
748 foreach (@summen){
749 my ($a1,$a2,$a3) = split( /\,/, $_ );
750 $sum=$sum+$a2;
751 $sum1=$sum1+$a3;
752 }
753
754 # Last Line in positiontable prints the sum of all traffic (therefor txt2 which is BOLD)
755 $txt2->translate(95, 205);
756 $txt2->text($Lang::tr{'acct pdf sum1'}); #SUM
757 $txt2->translate(427, 205);
758 $txt2->text_right($cent); #cent
759 $txt2->translate(380, 205);
760 $txt2->text_right("$sum MB"); #MB
761 $sum1=sprintf("%.2f",($sum1));
762 $txt2->translate(540, 205);
763 $txt2->text_right("$sum1 ".decode('utf8',$cur)); #SUM Eur
764 $txt->translate(390, 150);
765 $txt->text($Lang::tr{'acct pdf sum1'});
766 $txt->translate(540, 150);
767 $txt->text_right("$sum1 ".decode('utf8',$cur));
768 $txt->translate(390, 135);
769 my $endsum=$sum1;
770 $txt->text("$Lang::tr{'acct mwst_name'} $mwst%");
771 my $sum1=sprintf("%.2f",($sum1/100*$mwst));
772 $txt->translate(540, 135);
773 $txt->text_right("$sum1 ".decode('utf8',$cur));
774 $txt2->translate(390, 120);
775 $txt2->text($Lang::tr{'acct sum total'});
776 my $endsum=sprintf("%.2f",($sum1+$endsum));
777 $txt2->translate(540, 120);
778 $txt2->text_right("$endsum ".decode('utf8',$cur));
779
780 #Print the optional Billtext if any
781 $txt4->translate(60, 170);
782 $txt4->paragraph(decode('utf8',$text), 300, 40, -align => "justify"); #Bankname
783
784 #Watermark if preview
785 if ($preview eq 'on'){
786 my $eg_trans = $pdf->egstate();
787 $eg_trans->transparency(0.9);
788 $txt5->egstate($eg_trans);
789 $txt5->textlabel(80, 400, $fnt, 60, "PDF preview", -rotate => 40);
790 $txt5->textlabel(150, 330, $fnt, 60, "IPFire accounting", -rotate => 40);
791 }
792
793 $txt->textend; #END Text
794 $pdf->save; #Save pdf
795 $pdf->end( ); #END
796 if ($preview ne 'on'){
797 &fillBill($path.$grp,$name,$no,$grp);
798 }
a5f5ccfc
AM
799 if($preview eq 'on'){
800 return $filename;
801 }
db8a01e0
AM
802 return '0';
803}
804
805sub getBillNr {
806 $dbh=&connectdb;
807 my $year1=$year+1900;
808 my $no1;
809 my $res=$dbh->selectall_arrayref("SELECT MAX(NO) FROM BILLS;");
810 foreach my $row (@$res){
811 ($no1) = @$row;
812 }
813 if(!$no1){$no1=$year1."1000";}
814 $no1++;
815 return $no1;
816}
817
818sub fillBill {
819 my $path=$_[0];
820 my $name=$_[1];
821 my $no=$_[2];
822 my $grp=$_[3];
823 my $sth = $dbh->prepare("INSERT INTO BILLS (NO,GRP,PATH,NAME,DATE) VALUES (?,?,?,?,?);");
824 my $year1=$year+1900;
825 ++$mon;
826 $sth->execute($no,$grp,$path,$name,"$mday.$mon.$year1");
827 $sth->finish();
828 $dbh->disconnect();
829}
830
831sub getbills {
832 my $grp=shift;
833 $dbh=&connectdb;
834 my $res=$dbh->selectall_arrayref("SELECT * FROM BILLS WHERE GRP=?;",undef, $grp);
835 $dbh->disconnect();
836 return $res;
837}
838
839sub pngsize {
840 my $Buffer = shift;
841 my ($width,$height) = ( undef, undef );
842
843 if ($Buffer =~ /IHDR(.{8})/) {
844 my $PNG = $1;
845 ($width,$height) = unpack( "NN", $PNG );
846 } else {
847 $width=$Lang::tr{'acct invalid png'};
848 };
849 return ($width,$height);
850}
851
852sub gifsize {
853 my ($GIF)=@_;
854 my ($type,$a,$b,$c,$d,$s,$width,$height) ;
855 $type=substr($GIF,0,6);
856 if(!($type =~ m/GIF8[7,9]a/) || (length($s=substr($GIF, 6, 4))!=4) ){
857 return;
858 }
859 ($a,$b,$c,$d)=unpack("C"x4,$s);
860
861 $width= $b<<8|$a;
862 $height= $d<<8|$c;
863 return ($width,$height);
864}
865
866sub jpegsize {
867 my ($JPEG)=@ _ ;
868 my ($count)=2 ;
869 my ($length)=length($JPEG) ;
870 my ($ch)="" ;
871 my ($c1,$c2,$a,$b,$c,$d,$width,$height) ;
872
873 while (($ch ne "\xda") && ($count<$length)) {
874 while (($ch ne "\xff") && ($count < $length)) {
875 $ch=substr($JPEG,$count,1);
876 $count++;
877 }
878
879 while (($ch eq "\xff") && ($count<$length)) {
880 $ch=substr($JPEG,$count,1);
881 $count++;
882 }
883
884 if ((ord($ch) >= 0xC0) && (ord($ch) <= 0xC3)) {
885 $count+=3;
886 ($a,$b,$c,$d)=unpack("C"x4,substr($JPEG,$count,4));
887 $width=$c<<8|$d;
888 $height=$a<<8|$b;
889 return($width,$height);
890 }else {
891 ($c1,$c2)= unpack("C"x2,substr($JPEG,$count,2));
892 $count += $c1<<8|$c2;
893 }
894 }
895}
896
897sub time{
898 ($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst)=localtime();
899 $hour=sprintf("%02d",$hour);
900 $min=sprintf("%02d",$min);
901 $sec=sprintf("%02d",$sec);
902 $year +=1900;
903 $mday=sprintf("%02d",$mday);
904 $mon=sprintf("%02d",$mon+1);
905 my $res="$mday.$mon.$year $hour:$min:$sec - ";
906 return $res;
907}
908
909sub logger{
910 my $settings=shift;
911 my $msg=shift;
912 #open LOGFILE
913 if ($settings eq 'on'){
914 open ACCTLOG,">>/var/log/accounting.log" || print "could not open /var/log/accounting.log ";
915 print ACCTLOG &time."$msg";
916 close (ACCTLOG);
917 }
918}
919
920sub updateccaddr {
921 my $addr=shift;
922 my $cust=shift;
923 $dbh=&connectdb;
924 $dbh->do("UPDATE ACCT_ADDR SET CCMAIL=? WHERE COMPANY=? ;",undef, $addr, $cust);
925 $dbh->disconnect();
926}
927return 1;