]> git.ipfire.org Git - ipfire-2.x.git/blame - src/squid-accounting/acct-lib.pl
core90: Add updated netovpnsrv.cgi to update
[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 {
0fa8d28e
AM
96 $dbh->do("insert into ACCT_HIST select datetime(TIME_RUN,'unixepoch'),NAME,SUM(BYTES) from ACCT where datetime(TIME_RUN,'unixepoch') < datetime('now','start of month') group by NAME,datetime(TIME_RUN,'unixepoch');");
97 $dbh->do("DELETE FROM ACCT WHERE datetime(TIME_RUN,'unixepoch') < date('now','start of month');");
db8a01e0
AM
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;
0fa8d28e
AM
122 if ($table eq 'ACCT_HIST'){
123 $res = $dbh->selectall_arrayref( "SELECT strftime('%d.%m.%Y',TIME_RUN),(SELECT SUM(BYTES)/1024/1024 FROM ACCT_HIST WHERE TIME_RUN <= ah.TIME_RUN and TIME_RUN > date($from,'unixepoch') and NAME = '".$name."') kum_bytes FROM ACCT_HIST ah WHERE date(TIME_RUN) > date(".$from.",'unixepoch') AND date(TIME_RUN) < date(".$till.",'unixepoch') AND NAME = '".$name."' group by date(TIME_RUN);");
db8a01e0 124 }else{
0fa8d28e 125 $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 TIME_RUN > ".$from." and NAME = '".$name."') kum_bytes FROM (SELECT NAME,date(TIME_RUN,'unixepoch') tag,SUM(BYTES)/1024/1024 sbytes FROM ACCT WHERE NAME='".$name."' and TIME_RUN between ".$from." and ".$till." GROUP by NAME,date(TIME_RUN,'unixepoch')) xx;");
db8a01e0
AM
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);
92a6d92e
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;
218d2d75 395 $month = '12' if $month == 0;
db8a01e0
AM
396 my $year = $_[2];
397 my $mwst = $_[3];
398 my @address_cust= @{$_[4]}; #Array which contains customer and hoster adresses and some additional info from billgroup
399 my @address_host= @{$_[5]};
400 my @billpos = @{$_[6]};
401 my $grp = $_[7];
402 my $cur = $_[8]; #(Eur,USD,...)
403 my $preview = $_[9];
404 my $no = &getBillNr;
405 my $name = $month."-".$year."-".$no.".pdf";
406 my $path ="/var/ipfire/accounting/bill/";
407 my $filename = "$path/$grp/$name";
408 my @summen; #Used for counting the sums
409 my $x = 500;
410 my $y = 1;
411 my $zwsum;
412 my $pages = 0;
413 my $anzbillpos = @billpos;
414 my $anz = (@billar+$anzbillpos)/18; #Total pages
415 $anz = ceil($anz); #round the $anz value
416 my $aktpage=1;
417 my $sum=0;
418 my $sum1=0;
419 my $lines;
420 my $title;
421 my $txt;
422 my $txt1;
423 my $txt2;
424 my $txt3;
425 my $txt4;
426 my $txt5;
427 my $fnt;
428 my $fnt1;
429 my $fulldate = strftime('%d.%m.%Y',localtime(time()));
430 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);
431 my($company_cust,$type_cust,$name1_cust,$str_cust,$str_nr_cust,$plz_cust,$city_cust);
432
433 #First of all check if directory exists, else create it
434 if(! -d "$path/$grp" && $preview ne 'on'){
435 mkdir("$path/$grp",0777);
436 }
437
438 #Check if we are creating a preview or a real bill
439 if($preview eq 'on'){
a5f5ccfc 440 $filename="$path/".tempfile( SUFFIX => ".pdf", );
db8a01e0
AM
441 }
442 ####################################################################
443 #Prepare DATA from arrays
444 ####################################################################
445 #Get HOSTER for this grp
446 foreach my $addrline (@address_host){
447 ($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;
448 }
449 #Get CUST for this grp
450 foreach my $addrline_cust (@address_cust){
451 ($company_cust,$type_cust,$name1_cust,$str_cust,$str_nr_cust,$plz_cust,$city_cust)=@$addrline_cust;
452 }
453
454
455 #Generate PDF File
456 my $pdf = PDF::API2->new(-file => $filename);
457 $pdf->mediabox('A4');
458 my $page = $pdf->page;
459 $fnt = $pdf->corefont('Helvetica');
460 $fnt1 = $pdf->corefont('HelveticaBold');
461
462 #Set lines
463 $lines = $page->gfx;
464 $title = $page->gfx;
465 $lines->strokecolor('grey');
466 $lines->linewidth('0.5');
467
468 #Fill BILL DATA into PDF
db8a01e0
AM
469 foreach (@billar) {
470 my ($a1,$a2) = split( /\,/, $_ );
471 $a2=sprintf"%.2f",($a2/1024/1024);
472 my $sum=(($a2)*$cent);
473 $sum = sprintf"%.2f",($sum);
474 # Seitenwechsel ermitteln
475 if ($y % 18 == 0) {
476 $txt1->translate(390, 120);
477 $txt1->text($Lang::tr{'acct pdf zwsum'}); #Pos
478 $zwsum=sprintf("%.2f",($zwsum));
479 $txt1->translate(540, 120);
480 $txt1->text_right("$zwsum".decode('utf8',$cur)); #Pos
481 $zwsum=0;
482 $pages++;
483 $aktpage++;
484 $x=500;
485 $page=$pdf->page;
486 #draw lines
487 $lines = $page->gfx;
488 $title = $page->gfx;
489 $lines->strokecolor('grey');
490 $lines->linewidth('0.5');
491 }
492
493 #TITLES
494 $title->linewidth(14);
495 $title->move(385, 168);
496 $title->line(545, 168); #Title of SUMBOX
497 $title->move(60, 523);
498 $title->line(545, 523);#Bottom horiz. line of Title
499
500 # Generate Tables
501 $lines->move(59, 745);
502 $lines->line(545, 745);
503 $lines->move(59, 563);
504 $lines->line(545, 563);
505
506 # Addressbox
507 $lines->move(61, 710);
508 $lines->line(61, 715, 66, 715); #TL
509 $lines->move(61, 610);
510 $lines->line(61, 605, 66, 605); #BL
511 $lines->move(285, 715);
512 $lines->line(290, 715, 290, 710); #TR
513 $lines->move(290, 610);
514 $lines->line(290, 605, 285, 605); #BR
515
516 # Table for positions
517 $lines->move(60, 530);
518 $lines->line(60, 200); #First vert. line POS
519 $lines->move(90, 523);
520 $lines->line(90, 200); #Second vert. line
521 $lines->move(280, 523);
522 $lines->line(280, 200); #third vert. line
523 $lines->move(385, 523);
524 $lines->line(385, 200); #third vert. line
525 $lines->move(430, 523);
526 $lines->line(430, 200); #fourth vert. line
527 $lines->move(545, 530);
528 $lines->line(545, 200); #fifth vert. line
529 $lines->move(60, 200);
530 $lines->line(545, 200); #Bottom horizontal line
531
532 #SUM BOX
533 $lines->move(385, 175);
534 $lines->line(385, 115); #Left vert. line of SUMBOX
535 $lines->move(545, 175);
536 $lines->line(545, 115); #Right vert. line of SUMBOX
537 $lines->move(385, 115);
538 $lines->line(545, 115); #Bottom horiz. line of SUMBOX
539
540 #Lines on right side after sender and after "bank"
541 $lines->move(420, 723);
542 $lines->line(545, 723);# Line "Sender"
543 $lines->move(420, 648);
544 $lines->line(545, 648);# Line "Bank"
545 $lines->move(420, 600);
546 $lines->line(545, 600);# Line HRB/USTID
547
548 #Make lines Visible
549 $lines->stroke;
550 $title->stroke;
551 if (-f "/srv/web/ipfire/html/accounting/logo/$grp/logo.png"){
552 #Image LOGO
553 my $gfx = $page->gfx;
554 my $image = $pdf->image_png("/srv/web/ipfire/html/accounting/logo/$grp/logo.png");
555 my $width= $image->width;
556 my $height= $image->height;
557 $gfx->image($image, (545+($width/2))-$width, 750,0.5);
558 }
559
560 #Set Fonts
561 $txt = $page->text;
562 $txt1 = $page->text;
563 $txt2 = $page->text;
564 $txt3 = $page->text;
565 $txt4 = $page->text;
566 $txt5 = $page->text;
567 $txt->textstart; #Begin Text
568 $txt->font($fnt, 10); #Set fontsize for font1
569 $txt1->font($fnt, 8); #Set fontsize for font2
570 $txt2->font($fnt1, 10); #Set fontsize for font3
571 $txt3->font($fnt1, 16); #Set fontsize for font4
572 $txt4->font($fnt, 6); #Set fontsize for font5
573 $txt5->font($fnt1, 6); #Set fontsize for font6
574
575 #if $cent not set, set it to 0.5
576 if(!$cent){$cent='0.005';}
577
578 #if MWst not set, set it to 19%
579 if(!$mwst){$mwst='19';}
580
581 # Titles
582 $txt1->translate(65,520);
583 $txt1->text($Lang::tr{'acct pos'}); #Pos
584 $txt1->translate(95, 520);
585 $txt1->text($Lang::tr{'acct name'}); #Host/Name
586 $txt1->translate(285, 520);
587 $txt1->text($Lang::tr{'acct amount'}); #Traffic
588 $txt1->translate(390, 520);
589 $txt1->text($Lang::tr{'acct cent1'}); #Price /MB
590 $txt1->translate(435, 520);
591 $txt1->text($Lang::tr{'acct pdf price'}); #Sum
592
593 ####################################################################
594 #Fill Recipient address
595 my $rec_name= "$company_cust";
596 my $rec_name1="$name1_cust";
597 my $rec_str = "$str_cust $str_nr_cust";
598 my $rec_city = "$plz_cust $city_cust";
599 #INSERT RECIPIENT
600 my $o=675;
601 $txt2->translate(78, 685);
602 $txt2->text(decode('utf8',$rec_name));
603 if($rec_name1){
604 $txt1->translate(78, $o);
605 $txt1->text(decode('utf8',$rec_name1));
606 $o=$o-15;
607 }else{
608 $o=$o-15;
609 }
610 $txt1->translate(78, $o);
611 $txt1->text(decode('utf8',$rec_str));
612 $o=$o-10;
613 $txt1->translate(78, $o);
614 $txt1->text(decode('utf8',$rec_city));
615
616 # INSERT SENDER
617 my $send_name= "$company_host";
618 my $send_str = "$str_host $str_nr_host";
619 my $send_city = "$plz_host $city_host";
620 my $send_bank ="$bank";
621
622 $txt5->translate(420, 725);
623 $txt5->text(decode('utf8',$Lang::tr{'acct pdf prov'}));
624 $txt5->translate(420, 715);
625 $txt5->text(decode('utf8',$send_name));
626 my $j=705;
627 if($name1_host){
628 $txt4->translate(420, $j);
629 $txt4->text(decode('utf8',$name1_host));
630 $j=$j-8;
631 }
632 $txt4->translate(420, $j);
633 $txt4->text(decode('utf8',$send_str)); #STR
634 $j=$j-8;
635 $txt4->translate(420, $j);
636 $txt4->text(decode('utf8',$send_city)); #PLZ.City
637 #Print optional Values tel,fax
638 my $i=680;
639 if($tel_host){
640 $txt4->translate(420, $i);
641 $txt4->text($Lang::tr{'acct tel'}); #Tel
642 $txt4->translate(480, $i);
643 $txt4->text($tel_host); #Telnr
644 $i=$i-8;
645 }
646 if($fax_host){
647 $txt4->translate(420, $i);
648 $txt4->text($Lang::tr{'acct fax'}); #Fax
649 $txt4->translate(480, $i);
650 $txt4->text($fax_host); #Faxnr
651 $i=$i-8;
652 }
653 if($internet){
654 $txt4->translate(420, $i);
655 $txt4->text($Lang::tr{'acct inet'}); #Internet
656 $txt4->translate(480, $i);
657 $txt4->text($internet); #www-address
658 $i=$i-8;
659 }
660 $txt5->translate(420, 650);
661 $txt5->text(decode('utf8',$Lang::tr{'acct bank'})); #"BANK"
662 $txt4->lead(7);
663 $txt4->translate(420, 640);
664 $txt4->paragraph(decode('utf8',$bank), 130, 20, -align => "justify"); #Bankname
665 if($iban){
666 $txt4->translate(420, 625);
667 $txt4->text($Lang::tr{'acct iban'}); #iban
668 $txt4->translate(480, 625);
669 $txt4->text(decode('utf8',$iban)); #iban
670 $txt4->translate(420, 619);
671 $txt4->text($Lang::tr{'acct bic'}); #bic
672 $txt4->translate(480, 619);
673 $txt4->text(decode('utf8',$bic)); #bic
674 }
675 if($blz){
676 $txt4->translate(420, 613);
677 $txt4->text($Lang::tr{'acct blz'}); #blz
678 $txt4->translate(420, 607);
679 $txt4->text($Lang::tr{'acct kto'}); #kto
680 $txt4->translate(480, 613);
681 $txt4->text(decode('utf8',$blz)); #blz
682 $txt4->translate(480, 607);
683 $txt4->text(decode('utf8',$kto)); #kto
684 }
685
686 #Print USTID and optional HRB
687 $txt4->translate(420, 590);
688 $txt4->text($Lang::tr{'acct ustid'}); #USTID
689 $txt4->translate(480, 590);
690 $txt4->text($stnr); #ustid
691 if($hrb){
692 $txt4->translate(420, 580);
693 $txt4->text($Lang::tr{'acct hrb'}); #USTID
694 $txt4->translate(480, 580);
695 $txt4->text($hrb); #ustid
696 }
697 ################################################################
698
699 #Print Date, Pages ....
700 $txt3->translate(59, 545);
701 $txt3->text($Lang::tr{'acct pdf billtxt'});
702 $txt1->translate(160, 545);
703 $txt1->text("$no $Lang::tr{'acct billnr'}");
704 $txt1->translate(60, 532);
705 $txt1->text("$Lang::tr{'acct pdf time'} $month/$year");
706 $txt1->translate(545, 550);
707 $txt1->text_right("$Lang::tr{'acct pdf date'} $fulldate");
708 $txt1->translate(545, 532);
709 $txt1->text_right("$Lang::tr{'acct pdf page'} $aktpage / $anz");
710
711 if ($a1 eq '999'){last;}
712 #Print DATA from array to Position table
713 $txt1->translate(80, $x);
714 $txt1->text_right($y);
715 $txt1->translate(95, $x);
716 $txt1->text($a1);
717 $txt1->translate(380, $x);
718 $txt1->text_right("$a2 MB");
719 $txt1->translate(425, $x);
720 $txt1->text_right("$cent ".decode('utf8',$cur));
721 $txt1->translate(540, $x);
722 $txt1->text_right("$sum ".decode('utf8',$cur));
723
724 #Build SUMMARY
725 $summen[$y-1]="$y,$a2,$sum";
726 $zwsum=$zwsum+$sum;
727 $x=$x-15;
728 $y++;
729 }
730 #Print extra billpositions
731 foreach my $line (@billpos){
732 my ($grp,$amount,$art,$price)=@$line;
733 #Print DATA from array to Position table
734 $txt1->translate(80, $x);
735 $txt1->text_right($y);
736 $txt1->translate(95, $x);
737 $txt1->text(decode('utf8',$art));
738 $txt1->translate(380, $x);
739 $txt1->text_right($amount." pcs");
740 $txt1->translate(540, $x);
741 $txt1->text_right("$price ".decode('utf8',$cur));
742 #Build SUMMARY
743 my $zu=$amount * $price;
744 $summen[$y-1]="$y,'0',$zu";
745 $zwsum=$zwsum+$zu;
746 $x=$x-15;
747 $y++;
748 }
749 foreach (@summen){
750 my ($a1,$a2,$a3) = split( /\,/, $_ );
751 $sum=$sum+$a2;
752 $sum1=$sum1+$a3;
753 }
754
755 # Last Line in positiontable prints the sum of all traffic (therefor txt2 which is BOLD)
756 $txt2->translate(95, 205);
757 $txt2->text($Lang::tr{'acct pdf sum1'}); #SUM
758 $txt2->translate(427, 205);
759 $txt2->text_right($cent); #cent
760 $txt2->translate(380, 205);
761 $txt2->text_right("$sum MB"); #MB
762 $sum1=sprintf("%.2f",($sum1));
763 $txt2->translate(540, 205);
764 $txt2->text_right("$sum1 ".decode('utf8',$cur)); #SUM Eur
765 $txt->translate(390, 150);
766 $txt->text($Lang::tr{'acct pdf sum1'});
767 $txt->translate(540, 150);
768 $txt->text_right("$sum1 ".decode('utf8',$cur));
769 $txt->translate(390, 135);
770 my $endsum=$sum1;
771 $txt->text("$Lang::tr{'acct mwst_name'} $mwst%");
772 my $sum1=sprintf("%.2f",($sum1/100*$mwst));
773 $txt->translate(540, 135);
774 $txt->text_right("$sum1 ".decode('utf8',$cur));
775 $txt2->translate(390, 120);
776 $txt2->text($Lang::tr{'acct sum total'});
777 my $endsum=sprintf("%.2f",($sum1+$endsum));
778 $txt2->translate(540, 120);
779 $txt2->text_right("$endsum ".decode('utf8',$cur));
780
781 #Print the optional Billtext if any
782 $txt4->translate(60, 170);
783 $txt4->paragraph(decode('utf8',$text), 300, 40, -align => "justify"); #Bankname
784
785 #Watermark if preview
786 if ($preview eq 'on'){
787 my $eg_trans = $pdf->egstate();
788 $eg_trans->transparency(0.9);
789 $txt5->egstate($eg_trans);
790 $txt5->textlabel(80, 400, $fnt, 60, "PDF preview", -rotate => 40);
791 $txt5->textlabel(150, 330, $fnt, 60, "IPFire accounting", -rotate => 40);
792 }
793
794 $txt->textend; #END Text
795 $pdf->save; #Save pdf
796 $pdf->end( ); #END
797 if ($preview ne 'on'){
798 &fillBill($path.$grp,$name,$no,$grp);
799 }
a5f5ccfc
AM
800 if($preview eq 'on'){
801 return $filename;
802 }
db8a01e0
AM
803 return '0';
804}
805
806sub getBillNr {
807 $dbh=&connectdb;
808 my $year1=$year+1900;
809 my $no1;
810 my $res=$dbh->selectall_arrayref("SELECT MAX(NO) FROM BILLS;");
811 foreach my $row (@$res){
812 ($no1) = @$row;
813 }
814 if(!$no1){$no1=$year1."1000";}
815 $no1++;
816 return $no1;
817}
818
819sub fillBill {
820 my $path=$_[0];
821 my $name=$_[1];
822 my $no=$_[2];
823 my $grp=$_[3];
824 my $sth = $dbh->prepare("INSERT INTO BILLS (NO,GRP,PATH,NAME,DATE) VALUES (?,?,?,?,?);");
825 my $year1=$year+1900;
826 ++$mon;
827 $sth->execute($no,$grp,$path,$name,"$mday.$mon.$year1");
828 $sth->finish();
829 $dbh->disconnect();
830}
831
832sub getbills {
833 my $grp=shift;
834 $dbh=&connectdb;
835 my $res=$dbh->selectall_arrayref("SELECT * FROM BILLS WHERE GRP=?;",undef, $grp);
836 $dbh->disconnect();
837 return $res;
838}
839
840sub pngsize {
841 my $Buffer = shift;
842 my ($width,$height) = ( undef, undef );
843
844 if ($Buffer =~ /IHDR(.{8})/) {
845 my $PNG = $1;
846 ($width,$height) = unpack( "NN", $PNG );
847 } else {
848 $width=$Lang::tr{'acct invalid png'};
849 };
850 return ($width,$height);
851}
852
853sub gifsize {
854 my ($GIF)=@_;
855 my ($type,$a,$b,$c,$d,$s,$width,$height) ;
856 $type=substr($GIF,0,6);
857 if(!($type =~ m/GIF8[7,9]a/) || (length($s=substr($GIF, 6, 4))!=4) ){
858 return;
859 }
860 ($a,$b,$c,$d)=unpack("C"x4,$s);
861
862 $width= $b<<8|$a;
863 $height= $d<<8|$c;
864 return ($width,$height);
865}
866
867sub jpegsize {
868 my ($JPEG)=@ _ ;
869 my ($count)=2 ;
870 my ($length)=length($JPEG) ;
871 my ($ch)="" ;
872 my ($c1,$c2,$a,$b,$c,$d,$width,$height) ;
873
874 while (($ch ne "\xda") && ($count<$length)) {
875 while (($ch ne "\xff") && ($count < $length)) {
876 $ch=substr($JPEG,$count,1);
877 $count++;
878 }
879
880 while (($ch eq "\xff") && ($count<$length)) {
881 $ch=substr($JPEG,$count,1);
882 $count++;
883 }
884
885 if ((ord($ch) >= 0xC0) && (ord($ch) <= 0xC3)) {
886 $count+=3;
887 ($a,$b,$c,$d)=unpack("C"x4,substr($JPEG,$count,4));
888 $width=$c<<8|$d;
889 $height=$a<<8|$b;
890 return($width,$height);
891 }else {
892 ($c1,$c2)= unpack("C"x2,substr($JPEG,$count,2));
893 $count += $c1<<8|$c2;
894 }
895 }
896}
897
898sub time{
899 ($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst)=localtime();
900 $hour=sprintf("%02d",$hour);
901 $min=sprintf("%02d",$min);
902 $sec=sprintf("%02d",$sec);
903 $year +=1900;
904 $mday=sprintf("%02d",$mday);
905 $mon=sprintf("%02d",$mon+1);
906 my $res="$mday.$mon.$year $hour:$min:$sec - ";
907 return $res;
908}
909
910sub logger{
911 my $settings=shift;
912 my $msg=shift;
913 #open LOGFILE
914 if ($settings eq 'on'){
915 open ACCTLOG,">>/var/log/accounting.log" || print "could not open /var/log/accounting.log ";
916 print ACCTLOG &time."$msg";
917 close (ACCTLOG);
918 }
919}
920
921sub updateccaddr {
922 my $addr=shift;
923 my $cust=shift;
924 $dbh=&connectdb;
925 $dbh->do("UPDATE ACCT_ADDR SET CCMAIL=? WHERE COMPANY=? ;",undef, $addr, $cust);
926 $dbh->disconnect();
927}
928return 1;