Jahresberechnung
[kivitendo-erp.git] / bin / mozilla / rp.pl
index 9e3bc9f..56e7f49 100644 (file)
 #======================================================================
 
 require "$form->{path}/arap.pl";
+require "bin/mozilla/common.pl";
 
 use SL::PE;
 use SL::RP;
+use SL::USTVA;
 
 1;
 
@@ -108,9 +110,7 @@ sub report {
   $accrual = ($eur) ? ""        : "checked";
   $cash    = ($eur) ? "checked" : "";
 
-  ($null, $null, $null, $null, $null, $year, $null, $null, $null) =
-    localtime();
-  $year += 1900;
+  $year = (localtime)[5] + 1900;
 
   # get departments
   $form->all_departments(\%myconfig);
@@ -130,6 +130,21 @@ sub report {
        </tr>
 | if $form->{selectdepartment};
 
+  $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
+                                   "all" => 1 });
+
+  my %project_labels = ();
+  my @project_values = ("");
+  foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
+    push(@project_values, $item->{"id"});
+    $project_labels{$item->{"id"}} = $item->{"projectnumber"};
+  }
+
+  my $projectnumber =
+    NTI($cgi->popup_menu('-name' => "project_id",
+                         '-values' => \@project_values,
+                         '-labels' => \%project_labels));
+
   # use JavaScript Calendar or not
   $form->{jsscript} = $jscalendar;
   $jsscript = "";
@@ -183,7 +198,7 @@ sub report {
     if ($name_1 eq "") {
 
       $button1 = qq|
-         <input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}">|;
+         <input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
       $button1_2 = qq|
         <input type=button name=$name_2 id="$trigger_2" value=|
         . $locale->text('button') . qq|>|;
@@ -193,12 +208,12 @@ sub report {
         Form->write_trigger(\%myconfig, "1", "$name_2", "BR", "$trigger_2");
     } else {
       $button1 = qq|
-         <input name=$name_1 id=$id_1 size=11 title="$myconfig{dateformat}" value=$value_1>|;
+         <input name=$name_1 id=$id_1 size=11 title="$myconfig{dateformat}" value="$value_1" onBlur=\"check_right_date_format(this)\">|;
       $button1_2 = qq|
         <input type=button name=$name_1 id="$trigger_1" value=|
         . $locale->text('button') . qq|>|;
       $button2 = qq|
-         <input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}">|;
+         <input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
       $button2_2 = qq|
          <input type=button name=$name_2 id="$trigger_2" value=|
         . $locale->text('button') . qq|>
@@ -214,19 +229,21 @@ sub report {
     # without JavaScript Calendar
     if ($name_1 eq "") {
       $button1 =
-        qq|<input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}">|;
+        qq|<input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
     } else {
       $button1 =
-        qq|<input name=$name_1 id=$id_1 size=11 title="$myconfig{dateformat}" value=$value_1>|;
+        qq|<input name=$name_1 id=$id_1 size=11 title="$myconfig{dateformat}" value=$value_1 onBlur=\"check_right_date_format(this)\">|;
       $button2 =
-        qq|<input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}">|;
+        qq|<input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
     }
   }
-
+  $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
   $form->header;
-
+  $onload = qq|focus()|;
+  $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
+  $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
   print qq|
-<body>
+<body onLoad="$onload">
 
 <form method=post action=$form->{script}>
 
@@ -280,7 +297,7 @@ $jsscript
     print qq|
        <tr>
          <th align=right nowrap>| . $locale->text('Project') . qq|</th>
-         <td colspan=3><input name=projectnumber size=25</td>
+         <td colspan=3>$projectnumber</td>
        </tr>
         <input type=hidden name=nextsub value=generate_income_statement>
 </table>
@@ -390,7 +407,7 @@ $jsscript
     print qq|
        <tr>
          <th align=right nowrap>| . $locale->text('Project') . qq|</th>
-         <td colspan=3><input name=projectnumber size=25</td>
+         <td colspan=3>$projectnumber</td>
        </tr>
         <input type=hidden name=nextsub value=generate_bwa>
 </table>
@@ -495,7 +512,7 @@ $checked></td>
          <th align=right colspan=4>|
       . $locale->text('Decimalplaces')
       . qq|</th>
-             <td><input name=decimalplaces size=3></td>
+             <td><input name=decimalplaces size=3 value="2"></td>
          </tr>
                                     
 $jsscript
@@ -620,7 +637,7 @@ $checked></td>
        </tr>
        <tr>
          <th align=right>| . $locale->text('Decimalplaces') . qq|</th>
-         <td><input name=decimalplaces size=3></td>
+         <td><input name=decimalplaces size=3 value="2"></td>
        </tr>
       </table>
     </td>
@@ -687,8 +704,6 @@ $jsscript
   }
 
   if ($form->{report} =~ /^tax_/) {
-    $gifi = "";
-
     $form->{db} = ($form->{report} =~ /_collected/) ? "ar" : "ap";
 
     RP->get_taxaccounts(\%myconfig, \%$form);
@@ -725,32 +740,6 @@ $jsscript
 
          </td>
        </tr>
-|;
-
-    if (@{ $form->{gifi_taxaccounts} }) {
-      print qq|
-        <tr>
-         <th align=right>| . $locale->text('GIFI') . qq|</th>
-         <td colspan=3>
-|;
-
-      foreach $ref (@{ $form->{gifi_taxaccounts} }) {
-
-        print
-          qq|<input name=accno class=radio type=radio value="gifi_$ref->{accno}">&nbsp;$ref->{description}
-
-      <input name="gifi_$ref->{accno}_description" type=hidden value="$ref->{description}">
-      <input name="gifi_$ref->{accno}_rate" type=hidden value="$ref->{rate}">|;
-
-      }
-
-      print qq|
-         </td>
-       </tr>
-|;
-    }
-
-    print qq|
        <tr>
          <th align=right>| . $locale->text('Method') . qq|</th>
          <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>|
@@ -807,8 +796,6 @@ $jsscript
   }
 
   if ($form->{report} =~ /^nontaxable_/) {
-    $gifi = "";
-
     $form->{db} = ($form->{report} =~ /_sales/) ? "ar" : "ap";
 
     print qq|
@@ -872,8 +859,6 @@ $jsscript
   }
 
   if (($form->{report} eq "ar_aging") || ($form->{report} eq "ap_aging")) {
-    $gifi = "";
-
     if ($form->{report} eq 'ar_aging') {
       $label = $locale->text('Customer');
       $form->{vc} = 'customer';
@@ -922,8 +907,6 @@ $jsscript
   # above action can be removed if there is more than one input field
 
   if ($form->{report} =~ /(receipts|payments)$/) {
-    $gifi = "";
-
     $form->{db} = ($form->{report} =~ /payments$/) ? "ap" : "ar";
 
     RP->paymentaccounts(\%myconfig, \%$form);
@@ -1001,9 +984,8 @@ $jsscript
     . $locale->text('Continue') . qq|">
 |;
 
-  # Hier Aufruf von get_config aus bin/mozilla/fa.pl zum
-  # Einlesen der Finanzamtdaten
-  get_config($userspath, 'finanzamt.ini');
+  # Hier Aufruf von get_config zum Einlesen der Finanzamtdaten
+  USTVA->get_config($userspath, 'finanzamt.ini');
 
   $disabled = qq|disabled="disabled"|;
   $disabled = '' if ($form->{elster} eq '1');
@@ -1030,7 +1012,7 @@ $jsscript
   $lxdebug->leave_sub();
 }
 
-sub continue { &{ $form->{nextsub} } }
+sub continue { call_sub($form->{"nextsub"}); }
 
 sub get_project {
   $lxdebug->enter_sub();
@@ -1062,10 +1044,6 @@ sub generate_income_statement {
   $form->{endbold} = "</b>";
   $form->{br}      = "<br>";
 
-  &get_project(generate_income_statement);
-
-  $form->{projectnumber} = $form->{projectnumber_1};
-
   if ($form->{reporttype} eq "custom") {
 
     #forgotten the year --> thisyear
@@ -1253,7 +1231,8 @@ sub generate_balance_sheet {
   $form->{IN} = "balance_sheet.html";
 
   # setup company variables for the form
-  map { $form->{$_} = $myconfig{$_} }
+  map { $form->{$_} = $myconfig{$_};
+        $form->{$_} =~ s/\\n/\n/g; }
     (qw(company address businessnumber nativecurr));
 
   $form->{templates} = $myconfig{templates};
@@ -1345,11 +1324,6 @@ sub list_accounts {
   $column_header{endbalance} =
     qq|<th class=listheading>| . $locale->text('Balance') . qq|</th>|;
 
-  if ($form->{accounttype} eq 'gifi') {
-    $column_header{accno} =
-      qq|<th class=listheading>| . $locale->text('GIFI') . qq|</th>|;
-  }
-
   $form->header;
 
   print qq|
@@ -1380,20 +1354,12 @@ sub list_accounts {
     $description = $form->escape($ref->{description});
 
     $href =
-      qq|ca.pl?path=$form->{path}&action=list_transactions&accounttype=$form->{accounttype}&login=$form->{login}&password=$form->{password}&fromdate=$form->{fromdate}&todate=$form->{todate}&sort=transdate&l_heading=$form->{l_heading}&l_subtotal=$form->{l_subtotal}&department=$department&eur=$form->{eur}&projectnumber=$projectnumber&project_id=$form->{project_id}&title=$title&nextsub=$form->{nextsub}|;
-
-    if ($form->{accounttype} eq 'gifi') {
-      $href .= "&gifi_accno=$ref->{accno}&gifi_description=$description";
-      $na = $locale->text('N/A');
-      map { $ref->{$_} = $na } qw(accno description) unless $ref->{accno};
-    } else {
-      $href .= "&accno=$ref->{accno}&description=$description";
-    }
+      qq|ca.pl?path=$form->{path}&action=list_transactions&accounttype=$form->{accounttype}&login=$form->{login}&password=$form->{password}&fromdate=$form->{fromdate}&todate=$form->{todate}&sort=transdate&l_heading=$form->{l_heading}&l_subtotal=$form->{l_subtotal}&department=$department&eur=$form->{eur}&projectnumber=$projectnumber&project_id=$form->{project_id}&title=$title&nextsub=$form->{nextsub}&accno=$ref->{accno}&description=$description|;
 
     $ml = ($ref->{category} =~ /(A|C|E)/) ? -1 : 1;
 
-    $debit  = $form->format_amount(\%myconfig, $ref->{debit},  2, "&nbsp;");
-    $credit = $form->format_amount(\%myconfig, $ref->{credit}, 2, "&nbsp;");
+    $debit  = ($ref->{debit} != 0) ? $form->format_amount(\%myconfig, $ref->{debit},  2, "&nbsp;") : "&nbsp;";
+    $credit = ($ref->{credit} != 0) ? $form->format_amount(\%myconfig, $ref->{credit}, 2, "&nbsp;") : "&nbsp;";
     $begbalance =
       $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2, "&nbsp;");
     $endbalance =
@@ -1672,14 +1638,14 @@ sub aging {
       $i++;
 
       if ($subtotal) {
-        $c0subtotal =
-          $form->format_amount(\%myconfig, $c0subtotal, 2, "&nbsp");
-        $c30subtotal =
-          $form->format_amount(\%myconfig, $c30subtotal, 2, "&nbsp");
-        $c60subtotal =
-          $form->format_amount(\%myconfig, $c60subtotal, 2, "&nbsp");
-        $c90subtotal =
-          $form->format_amount(\%myconfig, $c90subtotal, 2, "&nbsp");
+        $c0subtotal = ($c0subtotal != 0) ? 
+          $form->format_amount(\%myconfig, $c0subtotal, 2, "&nbsp") : "";
+        $c30subtotal = ($c30subtotal != 0) ?
+          $form->format_amount(\%myconfig, $c30subtotal, 2, "&nbsp") : "";
+        $c60subtotal = ($c60subtotal != 0) ?
+          $form->format_amount(\%myconfig, $c60subtotal, 2, "&nbsp") : "";
+        $c90subtotal = ($c90subtotal != 0) ?
+          $form->format_amount(\%myconfig, $c90subtotal, 2, "&nbsp") : "";
       }
 
       $column_data{ct}        = qq|<th>&nbsp;</th>|;
@@ -1735,10 +1701,10 @@ sub aging {
     $c60total += $ref->{c60};
     $c90total += $ref->{c90};
 
-    $ref->{c0}  = $form->format_amount(\%myconfig, $ref->{c0},  2, "&nbsp;");
-    $ref->{c30} = $form->format_amount(\%myconfig, $ref->{c30}, 2, "&nbsp;");
-    $ref->{c60} = $form->format_amount(\%myconfig, $ref->{c60}, 2, "&nbsp;");
-    $ref->{c90} = $form->format_amount(\%myconfig, $ref->{c90}, 2, "&nbsp;");
+    $ref->{c0}  = ($ref->{c0} != 0) ? $form->format_amount(\%myconfig, $ref->{c0},  2, "&nbsp;") : "";
+    $ref->{c30} = ($ref->{c30} != 0) ? $form->format_amount(\%myconfig, $ref->{c30}, 2, "&nbsp;") : "";
+    $ref->{c60} = ($ref->{c60} != 0) ?  $form->format_amount(\%myconfig, $ref->{c60}, 2, "&nbsp;") : "";
+    $ref->{c90} = ($ref->{c90} != 0) ?  $form->format_amount(\%myconfig, $ref->{c90}, 2, "&nbsp;") : "";
 
     $href =
       qq|$ref->{module}.pl?path=$form->{path}&action=edit&id=$ref->{id}&login=$form->{login}&password=$form->{password}&callback=|
@@ -1850,16 +1816,10 @@ sub aging {
 <input class=submit type=submit name=action value="|
       . $locale->text('Print') . qq|">
 <input class=submit type=submit name=action value="|
-      . $locale->text('E-mail') . qq|">
-|;
-  }
-
-  if ($form->{menubar}) {
-    require "$form->{path}/menu.pl";
-    &menubar;
-  }
+      . $locale->text('E-mail') . qq|">|;
+}
 
-  print qq|
+print qq|
 </form>
 
 </body>
@@ -2043,21 +2003,36 @@ sub print {
 sub print_form {
   $lxdebug->enter_sub();
 
+  my %replacements =
+    (
+     "ä" => "ae", "ö" => "oe", "ü" => "ue",
+     "Ä" => "Ae", "Ö" => "Oe", "Ü" => "Ue",
+     "ß" => "ss",
+     " " => "_"
+    );
+
   $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
 
   $form->{templates} = "$myconfig{templates}";
 
-  $form->{IN} = "$form->{type}.html";
-
+  my $suffix = "html";
+  my $attachment_suffix = "html";
   if ($form->{format} eq 'postscript') {
     $form->{postscript} = 1;
-    $form->{IN} =~ s/html$/tex/;
-  }
-  if ($form->{format} eq 'pdf') {
+    $suffix = "tex";
+    $attachment_suffix = "ps";
+  } elsif ($form->{format} eq 'pdf') {
     $form->{pdf} = 1;
-    $form->{IN} =~ s/html$/tex/;
+    $suffix = "tex";
+    $attachment_suffix = "pdf";
   }
 
+  $form->{IN} = "$form->{type}.$suffix";
+
+
+  # Save $form->{email} because it will be overwritten.
+  $form->{EMAIL_RECIPIENT} = $form->{email};
+
   $i = 0;
   while (@{ $form->{AG} }) {
 
@@ -2114,12 +2089,22 @@ sub print_form {
             $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
         } (c0, c30, c60, c90, "");
 
+        $form->{attachment_filename} = $locale->text("Statement") . "_$form->{todate}.$attachment_suffix";
+        map({ $form->{attachment_filename} =~ s/$_/$replacements{$_}/g; } keys(%replacements));
+
         $form->parse_template(\%myconfig, $userspath);
 
       }
     }
   }
-
+  # saving the history
+  if(!exists $form->{addition} && $form->{id} ne "") {
+    $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
+       $form->{addition} = "PRINTED";
+       $form->{what_done} = $form->{type};
+       $form->save_history($form->dbconnect(\%myconfig));
+  }
+  # /saving the history 
   $lxdebug->leave_sub();
 }
 
@@ -2132,10 +2117,10 @@ sub statement_details {
   push @{ $form->{duedate} },   $ref->{duedate};
 
   foreach $item (qw(c0 c30 c60 c90)) {
-    eval {
+    if ($ref->{exchangerate} * 1) {
       $ref->{$item} =
         $form->round_amount($ref->{$item} / $ref->{exchangerate}, 2);
-    };
+    }
     $form->{"${item}total"} += $ref->{$item};
     $form->{total}          += $ref->{$item};
     push @{ $form->{$item} },
@@ -2154,12 +2139,6 @@ sub generate_tax_report {
   $description = $form->escape($form->{$descvar});
   $ratevar     = "$form->{accno}_rate";
 
-  if ($form->{accno} =~ /^gifi_/) {
-    $descvar     = "gifi_$form->{accno}_description";
-    $description = $form->escape($form->{$descvar});
-    $ratevar     = "gifi_$form->{accno}_rate";
-  }
-
   $department = $form->escape($form->{department});
 
   # construct href
@@ -2172,9 +2151,6 @@ sub generate_tax_report {
   $callback    =
     "$form->{script}?path=$form->{path}&action=generate_tax_report&login=$form->{login}&password=$form->{password}&fromdate=$form->{fromdate}&todate=$form->{todate}&db=$form->{db}&method=$form->{method}&accno=$form->{accno}&$descvar=$description&department=$department&$ratevar=$taxrate&report=$form->{report}";
 
-  $form->{title} = $locale->text('GIFI') . " - "
-    if ($form->{accno} =~ /^gifi_/);
-
   $title = $form->escape($form->{title});
   $href .= "&title=$title";
   $title = $form->escape($form->{title}, 1);
@@ -2637,7 +2613,16 @@ sub print_options {
   $lxdebug->enter_sub();
 
   $form->{sendmode} = "attachment";
-  $form->{copies}   = 2 unless $form->{copies};
+
+  $form->{"format"} =
+    $form->{"format"} ? $form->{"format"} :
+    $myconfig{"template_format"} ? $myconfig{"template_format"} :
+    "pdf";
+
+  $form->{"copies"} =
+    $form->{"copies"} ? $form->{"copies"} :
+    $myconfig{"copies"} ? $myconfig{"copies"} :
+    2;
 
   $form->{PD}{ $form->{type} }     = "selected";
   $form->{DF}{ $form->{format} }   = "selected";
@@ -2710,8 +2695,6 @@ sub generate_bwa {
   $form->{endbold} = "</b>";
   $form->{br}      = "<br>";
 
-  #  &get_project(generate_bwa);
-
   if ($form->{reporttype} eq "custom") {
 
     #forgotten the year --> thisyear
@@ -2900,9 +2883,8 @@ sub generate_bwa {
 sub generate_ustva {
   $lxdebug->enter_sub();
 
-  # Hier Aufruf von get_config aus bin/mozilla/fa.pl zum
-  # Einlesen der Finanzamtdaten
-  get_config($userspath, 'finanzamt.ini');
+  # Hier Aufruf von get_config zum Einlesen der Finanzamtdaten
+  USTVA->get_config($userspath, 'finanzamt.ini');
 
   #  &get_project(generate_bwa);
   @anmeldungszeitraum =