unstable-Zweig als Kopie des "alten" trunks erstellt.
[kivitendo-erp.git] / SL / USTVA.pm
1 #=====================================================================
2 # Lx-Office ERP
3 # Copyright (c) 2004 by Udo Spallek, Aachen
4 #
5 #  Author: Udo Spallek
6 #   Email: udono@gmx.net
7 #     Web: http://www.lx-office.org
8 #
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #======================================================================
23 # Utilities for ustva 
24 #=====================================================================
25
26 package USTVA;
27
28 sub create_steuernummer {
29   $main::lxdebug->enter_sub();
30
31
32   $part=$form->{part};
33   $patterncount = $form->{patterncount};
34   $delimiter = $form->{delimiter};
35   $elster_pattern = $form->{elster_pattern};
36   
37   # rebuild steuernummer and elstersteuernummer
38   # es gibt eine gespeicherte steuernummer $form->{steuernummer}
39   # und die parts und delimiter 
40   
41   my $h =0;
42   my $i =0;
43   
44   $steuernummer_new = $part; 
45   $elstersteuernummer_new = $elster_FFFF;
46   $elstersteuernummer_new .= '0';
47   
48   for ( $h = 1; $h < $patterncount; $h++) {
49     $steuernummer_new .= qq|$delimiter|;
50     for ( $i = 1; $i <= length($elster_pattern); $i++ ) {
51       $steuernummer_new .=$form->{"part_$h\_$i"};
52       $elstersteuernummer_new .=$form->{"part_$h\_$i"};
53     }
54   }
55   if ($form->{steuernummer} ne $steuernummer_new){
56     $form->{steuernummer} = $steuernummer_new;
57     $form->{elstersteuernummer} = $elstersteuernummer_new;
58     $form->{steuernummer_new} = $steuernummer_new;
59   }
60   $main::lxdebug->leave_sub();
61   return ($steuernummer_new, $elstersteuernummer_new);
62 }
63
64
65 sub steuernummer_input {
66   $main::lxdebug->enter_sub();
67
68
69   ($elsterland, $elsterFFFF, $steuernummer ) = @_;
70   
71   $elster_land=$elsterland;
72   $elster_FFFF=$elsterFFFF;
73   $steuernummer = '0000000000' if ( $steuernummer eq '' );
74   # $steuernummer formatieren (nur Zahlen) -> $stnr
75   $stnr = $steuernummer;
76   $stnr =~ s/\D+//g;
77   
78   #Pattern description Elstersteuernummer
79   my %elster_STNRformat =  ( 
80         'Mecklenburg Vorpommern' => 'FFF/BBB/UUUUP',    # '/' 3
81         'Hessen' => '0FF BBB UUUUP',                    # ' ' 3
82         'Nordrhein Westfalen' => 'FFF/BBBB/UUUP',       # '/' 3
83         'Schleswig Holstein' => 'FF BBB UUUUP',         # ' ' 2
84         'Berlin' => 'FF/BBB/UUUUP',                     # '/' 3
85         'Thüringen' => 'FFF/BBB/UUUUP',                 # '/' 3
86         'Sachsen' => 'FFF/BBB/UUUUP',                   # '/' 3
87         'Hamburg' => 'FF/BBB/UUUUP',                    # '/' 3
88         'Baden Würtemberg' => 'FF/BBB/UUUUP',           # '/' 2
89         'Sachsen Anhalt' => 'FFF/BBB/UUUUP',            # '/' 3
90         'Saarland' => 'FFF/BBB/UUUUP',                  # '/' 3
91         'Bremen' => 'FF BBB UUUUP',                     # ' ' 3
92         'Bayern' => 'FFF/BBB/UUUUP',                    # '/' 3
93         'Rheinland Pfalz' => 'FF/BBB/UUUU/P',           # '/' 4
94         'Niedersachsen' => 'FF/BBB/UUUUP',              # '/' 3
95         'Brandenburg' => 'FFF/BBB/UUUUP',               # '/' 3
96   );
97   
98   #split the pattern
99   $elster_pattern = $elster_STNRformat{$elster_land};
100   my @elster_pattern = split(' ', $elster_pattern);
101   my $delimiter='&nbsp;';
102   my $patterncount = @elster_pattern;
103   if ($patterncount < 2) {
104      @elster_pattern=();
105      @elster_pattern = split('/', $elster_pattern);
106      $delimiter='/';
107      $patterncount = @elster_pattern;
108   };
109
110   # no we have an array of patternparts and a delimiter
111   # create the first automated and fixed part and delimiter
112   
113   print qq|<b><font size="+1">|;  
114   $part='';
115   SWITCH: {
116   $elster_pattern[0] eq 'FFF' && do {
117                                      $part= substr($elster_FFFF,1,4);
118                                      print qq|$part|;
119                                      last SWITCH;
120                                     };
121   $elster_pattern[0] eq '0FF' && do {
122                                      $part= '0'.substr($elster_FFFF,2,4);
123                                      print qq|$part|;
124                                      last SWITCH;
125                                     };
126   $elster_pattern[0] eq 'FF' && do {
127                                      $part= substr($elster_FFFF,2,4);
128                                      print qq|$part|;
129                                      last SWITCH;
130                                     };
131                         1==1 && do {
132                                     print qq|Fehler!|;;
133                                     last SWITCH;
134                                     };
135   }
136   #now the rest of the Steuernummer ...
137   print qq|</b></font>|;
138   print qq|\n
139            <input type=hidden name="elster_pattern" value="$elster_pattern">
140            <input type=hidden name="patterncount" value="$patterncount">
141            <input type=hidden name="patternlength" value="$patterncount">
142            <input type=hidden name="delimiter" value="$delimiter">
143            <input type=hidden name="part" value="$part">  
144   |;
145   my $h =0;
146   my $i =0;
147   my $j = 0;
148   $k = 0;
149
150   for ( $h = 1; $h < $patterncount; $h++) {
151     print qq|&nbsp;$delimiter&nbsp;\n|;
152     for ( $i = 1; $i <= length($elster_pattern[$h] ); $i++ ) {
153       print qq|<select name="part_$h\_$i">\n|;
154       
155       for ($j = 0; $j <= 9; $j++){
156           print qq|      <option value="$j"|;
157           if ($steuernummer ne ''){
158             if ( $j eq substr($stnr, length($part) + $k, 1) ) {
159               print  qq| selected|;
160             }
161           }
162           print qq|>$j</option>\n|;
163       }
164       $k++;
165       print qq|</select>\n|;
166     }
167   }
168   $main::lxdebug->leave_sub();
169 }
170
171 sub fa_auswahl {
172   $main::lxdebug->enter_sub();
173
174   
175   use SL::Form;
176
177   # Referenz wird übergeben, hash of hash wird nicht 
178   # in neues  Hash kopiert, sondern direkt über die Referenz verändert
179   # Prototyp für diese Konstruktion
180
181   my ($land, $elsterFFFF, $elster_init) = @_; #Referenz auf Hash von Hash übergeben
182   my $terminal ='';
183   my $FFFF=$elsterFFFF;
184   my $ffff='';
185   my $checked='';
186   $checked='checked' if ($elsterFFFF eq '' and $land eq '');
187   #if ($ENV{HTTP_USER_AGENT} =~ /(mozilla|opera|w3m)/i){
188   #$terminal='mozilla';
189   #} elsif ($ENV{HTTP_USER_AGENT} =~ /(links|lynx)/i){
190   #$terminal = 'lynx';
191   #}
192   
193   
194   #if ( $terminal eq 'mozilla' or $terminal eq 'js' ) {
195           print qq|    
196         <br>
197         <script language="Javascript">
198         function update_auswahl()
199         {
200                 var elsterBLAuswahl = document.verzeichnis.elsterland_new;
201                 var elsterFAAuswahl = document.verzeichnis.elsterFFFF_new;
202
203                 elsterFAAuswahl.options.length = 0; // dropdown aufräumen
204                 |;
205
206
207            foreach $elster_land ( sort keys %$elster_init ) {
208              print qq|
209                if (elsterBLAuswahl.options[elsterBLAuswahl.selectedIndex].
210                value == "$elster_land")
211                {
212                |;
213              my $j=0;
214              my %elster_land_fa =();
215              $FFFF = '';
216              for $FFFF ( keys %{ $elster_init->{$elster_land} } ) {
217                 $elster_land_fa{$FFFF} = $elster_init->{$elster_land}->{$FFFF}->[0]; 
218              }
219                         foreach $ffff ( sort {
220            $elster_land_fa{$a} cmp $elster_land_fa{$b}
221            } keys(%elster_land_fa)) {
222                print qq|
223                    elsterFAAuswahl.options[$j] = new Option("$elster_land_fa{$ffff} ($ffff)","$ffff");|;
224                $j++;
225              }
226              print qq|
227                }|;
228           }
229           print qq| 
230         }
231         </script>
232
233         <table width="100%">
234           <tr>
235             <td>
236                Bundesland
237             </td>
238             <td>
239               <select size="1" name="elsterland_new" onchange="update_auswahl()">|;
240           if ($land eq ''){
241             print qq|<option value="Auswahl" $checked>hier auswählen...</option>\n|;
242           }  
243           foreach $elster_land ( sort keys %$elster_init ) {
244              print qq|
245                   <option value="$elster_land"|;
246              if ($elster_land eq $land and $checked eq '') {
247                print qq| selected|;
248              }   
249              print qq|>$elster_land</option>
250              |;   
251           }
252           print qq|
253             </td>
254           </tr>
255           |;
256
257          my $elster_land = ''; 
258          $elster_land = ( $land ne ''  ) ? $land:'';
259            %elster_land_fa =();
260            for $FFFF ( keys % { $elster_init->{$elster_land} } ) {
261              $elster_land_fa{$FFFF} = $elster_init->{$elster_land}->{$FFFF}->[0]
262            }
263            
264            print qq|
265            <tr>
266               <td>Finanzamt
267               </td> 
268               <td>
269                  <select size="1" name="elsterFFFF_new">|;
270            if ($elsterFFFF eq ''){   
271            print qq|<option value="Auswahl" $checked>hier auswählen...</option>|;
272            }else {
273                foreach $ffff ( sort {
274                $elster_land_fa{$a} cmp $elster_land_fa{$b}
275                } keys(%elster_land_fa)) {
276
277                print qq|
278                         <option value="$ffff"|;
279                if ( $ffff eq $elsterFFFF and $checked eq '') {
280                print qq| selected|;
281                       }
282                print qq|>$elster_land_fa{$ffff} ($ffff)</option>|;
283                  }
284           }
285           print qq|
286                  </td>
287               </tr>
288             </table>    
289             </select>|;
290         
291   $main::lxdebug->leave_sub();
292 }
293
294 sub info {
295   $main::lxdebug->enter_sub();
296
297   my $msg = $_[0];
298
299   if ($ENV{HTTP_USER_AGENT}) {
300     $msg =~ s/\n/<br>/g;
301
302     print qq|<body><h2 class=info>Hinweis</h2>
303
304     <p><b>$msg</b>
305     <br>
306     <br>
307     <hr>
308     <input type=button value="zurück" onClick="history.go(-1)">
309     </body>
310     |;
311
312     exit;
313
314   } else {
315   
316     if ($form->{error_function}) {
317       &{ $form->{error_function} }($msg);
318     } else {
319       die "Hinweis: $msg\n";
320     }
321   }
322   
323   $main::lxdebug->leave_sub();
324 }
325                                                                         
326 sub stichtag {
327   $main::lxdebug->enter_sub();
328
329   # noch nicht fertig
330   # soll mal eine Erinnerungsfunktion für USTVA Abgaben werden, die automatisch 
331   # den Termin der nächsten USTVA anzeigt.
332   # 
333   #
334   my ($today, $FA_dauerfrist, $FA_voranmeld) = @_;
335   #$today zerlegen:
336   
337   #$today =today * 1;
338   $today =~ /(\d\d\d\d)(\d\d)(\d\d)/;
339   $year   = $1;
340   $month  = $2;
341   $day    = $3;
342   $yy     = $year;
343   $mm     = $month;
344   $yymmdd = "$year$month$day" * 1;
345   $mmdd   = "$month$day" * 1;
346   $stichtag = '';
347   #$tage_bis = '1234';
348   #$ical = '...vcal format';  
349   
350   #if ($FA_voranmeld eq 'month'){
351     
352   %liste =  ("0110" => 'December',
353              "0210" => 'January', 
354              "0310" => 'February', 
355              "0410" => 'March', 
356              "0510" => 'April',
357              "0610" => 'May',
358              "0710" => 'June',
359              "0810" => 'July',
360              "0910" => 'August',
361              "1010" => 'September',
362              "1110" => 'October',
363              "1210" => 'November'
364             );
365
366   #$mm += $dauerfrist
367   #$month *= 1;
368   $month += 1  if ( $day > 10 );
369   $month = sprintf("%02d", $month);
370   $stichtag = $year.$month."10"; 
371   $ust_va = $month."10";
372   
373     
374   foreach $date ( %liste ){
375     $ust_va = $liste{$date} if ($date eq $stichtag);
376   }
377     
378   
379   #} elsif ($FA_voranmeld eq 'quarter'){
380   #1;
381   
382   #}
383   
384   #@stichtag = ('10.04.2004', '10.05.2004');
385   
386   #@liste = ['0110', '0210', '0310', '0410', '0510', '0610', '0710', '0810', '0910',
387   #          '1010', '1110', '1210', ];
388   #                                                                                                                                                             
389   #foreach $key (@liste){
390   #  #if ($ddmm < ('0110' * 1));
391   #  if ($ddmm ){}
392   #  $stichtag = $liste[$key - 1] if ($ddmm > $key);
393   #
394   #}
395   #
396   #$stichtag =~ /([\d]\d)(\d\d)$/
397   #$stichtag = "$1.$2.$yy"
398   #$stichtag=$1;
399   $main::lxdebug->leave_sub();
400   return ($stichtag, $description, $tage_bis, $ical);
401 }
402
403 sub query_finanzamt {
404   $main::lxdebug->enter_sub();
405
406   
407   my ($myconfig, $form) =@_;
408   my $dbh = $form->dbconnect($myconfig) or $self->error(DBI->errstr);
409   #Test, if table finanzamt exist
410   my $table= 'finanzamt';
411   my $filename= "sql/$table.sql";
412
413   my $tst = $dbh->prepare("SELECT * FROM $table");
414   $tst->execute ;
415   if ( $DBI::err ) {
416     #There is no table, read the table from sql/finanzamt.sql
417     print qq|<p>Bitte warten, Tabelle $table wird einmalig in Datenbank: 
418     $myconfig->{dbname} als Benutzer: $myconfig->{dbuser} hinzugefügt...</p>|;
419     process_query($form, $dbh, $filename)||$self->error(DBI->errstr);
420     #execute second last call
421     my $dbh = $form->dbconnect($myconfig) or $self->error(DBI->errstr);
422     $dbh->disconnect();
423   };
424   $tst->finish();
425   #$dbh->disconnect();
426   
427   my @vars = (
428          'FA_Land_Nr',                                  #  0
429          'FA_BUFA_Nr',                                  #  1 
430          #'FA_Verteiler',                               #  2
431          'FA_Name',                                     #  3 
432          'FA_Strasse',                                  #  4
433          'FA_PLZ',                                      #  5
434          'FA_Ort',                                      #  6
435          'FA_Telefon',                                  #  7
436          'FA_Fax',                                      #  8 
437          'FA_PLZ_Grosskunden',                          #  9
438          'FA_PLZ_Postfach',                             # 10
439          'FA_Postfach',                                 # 11
440          'FA_BLZ_1',                                    # 12
441          'FA_Kontonummer_1',                            # 13
442          'FA_Bankbezeichnung_1',                        # 14
443          #'FA_BankIBAN_1',                              # 15
444          #'FA_BankBIC_1',                               # 16
445          #'FA_BankInhaber_BUFA_Nr_1',                   # 17
446          'FA_BLZ_2',                                    # 18
447          'FA_Kontonummer_2',                            # 19
448          'FA_Bankbezeichnung_2',                        # 20 
449          #'FA_BankIBAN_2',                              # 21
450          #'FA_BankBIC_2',                               # 22
451          #'FA_BankInhaber_BUFA_Nr_2',                   # 23
452          'FA_Oeffnungszeiten',                          # 24
453          'FA_Email',                                    # 25
454          'FA_Internet'                                  # 26
455          #'FA_zustaendige_Hauptstelle_BUFA_Nr',         # 27
456          #'FA_zustaendige_vorgesetzte_Finanzbehoerde'   # 28
457          );
458
459   my $field = join(', ', @vars);
460   
461   my $query = "SELECT $field FROM finanzamt ORDER BY FA_Land_nr";
462   my $sth = $dbh->prepare($query) or $self->error($dbh->errstr);
463   $sth->execute || $form->dberror($query);
464   my $array_ref = $sth->fetchall_arrayref(  );
465   my $land = '';
466   foreach my $row ( @$array_ref ) {  
467     my $FA_finanzamt = $row;
468     $land = 'Schleswig Holstein' if ( @$FA_finanzamt[0] eq '1' );
469     $land = 'Hamburg' if ( @$FA_finanzamt[0] eq '2' );
470     $land = 'Niedersachsen' if ( @$FA_finanzamt[0] eq '3' );
471     $land = 'Bremen' if ( @$FA_finanzamt[0] eq '4' );
472     $land = 'Nordrhein Westfalen' if ( @$FA_finanzamt[0] eq '5' );
473     $land = 'Hessen' if ( @$FA_finanzamt[0] eq '6' );
474     $land = 'Rheinland Pfalz' if ( @$FA_finanzamt[0] eq '7' );
475     $land = 'Baden Würtemberg' if ( @$FA_finanzamt[0] eq '8' );
476     $land = 'Bayern' if ( @$FA_finanzamt[0] eq '9' );
477     $land = 'Saarland' if ( @$FA_finanzamt[0] eq '10' );
478     $land = 'Berlin' if ( @$FA_finanzamt[0] eq '11' );
479     $land = 'Brandenburg' if ( @$FA_finanzamt[0] eq '12' );
480     $land = 'Mecklenburg Vorpommern' if ( @$FA_finanzamt[0] eq '13' );
481     $land = 'Sachsen' if ( @$FA_finanzamt[0] eq '14' );
482     $land = 'Sachsen Anhalt' if ( @$FA_finanzamt[0] eq '15' );
483     $land = 'Thüringen' if ( @$FA_finanzamt[0] eq '16' );
484     
485     my $ffff = @$FA_finanzamt[1];
486         
487     my $rec = {};
488     $rec->{$land} = $ffff;     
489
490     shift @$row;
491     shift @$row;
492
493     $finanzamt{$land}{$ffff} = [ @$FA_finanzamt ] ;
494   } 
495
496   $sth->finish();
497   $dbh->disconnect();
498
499   $main::lxdebug->leave_sub();
500
501   return \%finanzamt;
502 }
503
504
505 sub process_query {
506   $main::lxdebug->enter_sub();
507
508 # Copyright D. Simander -> SL::Form under Gnu GPL.
509   my ($form, $dbh, $filename) = @_;
510   
511 #  return unless (-f $filename);
512   
513   open(FH, "$filename") or $form->error("$filename : $!\n");
514   my $query = "";
515   my $sth;
516   my @quote_chars;
517
518   while (<FH>) {
519     # Remove DOS and Unix style line endings.
520     s/[\r\n]//g;
521
522     # don't add comments or empty lines
523     next if /^(--.*|\s+)$/;
524
525     for (my $i = 0; $i < length($_); $i++) {
526       my $char = substr($_, $i, 1);
527
528       # Are we inside a string?
529       if (@quote_chars) {
530         if ($char eq $quote_chars[-1]) {
531           pop(@quote_chars);
532         }
533         $query .= $char;
534
535       } else {
536         if (($char eq "'") || ($char eq "\"")) {
537           push(@quote_chars, $char);
538
539         } elsif ($char eq ";") {
540           # Query is complete. Send it.
541
542           $sth = $dbh->prepare($query);
543           $sth->execute || $form->dberror($query);
544           $sth->finish;
545
546           $char = "";
547           $query = "";
548         }
549
550         $query .= $char;
551       }
552     }
553   }
554
555   close FH;
556
557   $main::lxdebug->leave_sub();
558 }
559
560
561
562 1;
563
564